type-bridge-orm 2.2.2

Async ORM for TypeDB built on type-bridge-core-lib
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
//! Deterministic TypeQL lowering for validated V2 query plans.
//!
//! One validated plan plus one bound invocation lowers to exact TypeQL text.
//! Fully populated single-row inputs substitute canonical literals directly
//! into the pattern text. Multi-row batches and optional absence use the
//! native driver `given` transport; unsupported scalar domains reject during
//! provider-independent preflight, never through silent row-by-row emulation.

use std::fmt::Write as _;

use type_bridge_contract::diagnostic::{Diagnostic, DiagnosticCategory, DiagnosticCode};
use type_bridge_contract::limits::{MAX_CANONICAL_BYTES, MAX_CANONICAL_COLLECTION_LEN};
use type_bridge_contract::migration_assertion::BindingId;
use type_bridge_contract::query_plan::{
    InputRow, QueryInvocation, QueryOperand, QueryOperation, QueryOutput, QueryPattern, QueryPlan,
    ReadStage,
};
use type_bridge_query::{
    DocumentColumnShape, DocumentSchema, OutputSchema, RowSchema, ValidatedQuery,
};

use crate::migration_assertion::{render_comparator, render_literal};

pub(crate) use crate::query_v2_model::{
    execute_validated_model_query_borrowed, execute_validated_model_query_with_statement_limit,
};

/// Exact provider text and typed shape for one lowered invocation.
#[derive(Clone, Debug, PartialEq)]
pub struct LoweredQuery {
    given: Option<crate::session::backend::GivenRowsSpec>,
    operation: QueryOperation,
    output_schema: OutputSchema,
    typeql: String,
}

impl LoweredQuery {
    /// Return the deterministic provider query text.
    #[must_use]
    pub fn typeql(&self) -> &str {
        &self.typeql
    }

    /// Return the driver-transported input rows for a `given` lowering.
    #[must_use]
    pub const fn given_rows(&self) -> Option<&crate::session::backend::GivenRowsSpec> {
        self.given.as_ref()
    }

    /// Return the validated output shape.
    #[must_use]
    pub const fn output_schema(&self) -> &OutputSchema {
        &self.output_schema
    }

    /// Return the requested closed operation.
    #[must_use]
    pub const fn operation(&self) -> QueryOperation {
        self.operation
    }
}

/// Lower one validated plan and its bound invocation to exact TypeQL.
pub fn lower_validated_query(
    validated: &ValidatedQuery,
    invocation: &QueryInvocation,
) -> Result<LoweredQuery, Diagnostic> {
    lower_validated_query_with_execution_limits(validated, invocation, None, None)
}

/// Lower one execution with finite provider-side answer sentinels.
///
/// These sentinels are deliberately execution-only: they are not part of plan
/// identity or the public deterministic lowering. The item bound clamps an
/// existing limit or is appended before `fetch`; every fetched list becomes a
/// subquery capped at its own `N + 1`. Together they let the consumer inspect
/// first over-budget evidence and still read the statement's terminal frame.
fn lower_validated_query_with_execution_limits(
    validated: &ValidatedQuery,
    invocation: &QueryInvocation,
    max_collection_members: Option<u64>,
    max_provider_items: Option<u64>,
) -> Result<LoweredQuery, Diagnostic> {
    let plan = validated.plan();
    preflight_lowered_structure(validated)?;
    preflight_invocation_transport(plan, invocation)?;
    if let Some(compatibility) = crate::query_v2_compatibility::lower_validated_compatibility_query(
        validated,
        invocation,
        invocation.operation(),
    )? {
        if compatibility.typeql().len() > MAX_CANONICAL_BYTES {
            return Err(failure(
                DiagnosticCategory::ResourceLimit,
                "query_v2_lowered_typeql_byte_limit",
                "lowered TypeQL exceeds the canonical protocol byte ceiling",
            ));
        }
        return Ok(LoweredQuery {
            given: None,
            operation: compatibility.operation(),
            output_schema: validated.output_schema().clone(),
            typeql: compatibility.typeql().to_owned(),
        });
    }
    let (row, given) = match invocation.inputs() {
        [] => (None, None),
        [row] if !invocation_requires_given(invocation) => (Some(row), None),
        rows => (None, Some(given_rows_spec(plan, rows)?)),
    };

    let mut typeql = String::new();
    for function in plan.functions() {
        write!(typeql, "with fun {}(", function.name().label())
            .expect("writing to String cannot fail");
        for (index, label) in function.parameters().iter().enumerate() {
            if index != 0 {
                typeql.push_str(", ");
            }
            write!(
                typeql,
                "${}: {}",
                local_variable(
                    function.bindings(),
                    BindingId::new(u16::try_from(index).expect("dense parameter ordinal"),)?
                )?,
                label.as_str(),
            )
            .expect("writing to String cannot fail");
        }
        writeln!(
            typeql,
            ") -> {}:",
            given_type_keyword(function.returns().value_type())
                .expect("contract admits integer and double local returns"),
        )
        .expect("writing to String cannot fail");
        typeql.push_str("match\n");
        render_scoped_patterns(
            &mut typeql,
            plan,
            function.bindings(),
            function.body(),
            None,
            0,
            None,
        )?;
        let keyword = match function.returns().reducer() {
            type_bridge_contract::query_plan::Reducer::Count => "count",
            type_bridge_contract::query_plan::Reducer::Sum => "sum",
            type_bridge_contract::query_plan::Reducer::Max => "max",
            type_bridge_contract::query_plan::Reducer::Mean => "mean",
            type_bridge_contract::query_plan::Reducer::Median => "median",
            type_bridge_contract::query_plan::Reducer::Min => "min",
            type_bridge_contract::query_plan::Reducer::Std => "std",
        };
        writeln!(
            typeql,
            "return {keyword}(${});",
            local_variable(function.bindings(), function.returns().input())?,
        )
        .expect("writing to String cannot fail");
    }
    if let Some(spec) = &given {
        typeql.push_str("given ");
        for (index, column) in plan.inputs().iter().enumerate() {
            if index != 0 {
                typeql.push_str(", ");
            }
            write!(
                typeql,
                "${}: {}",
                column.public_name().as_str(),
                given_type_keyword(column.value_type())
                    .expect("spec construction admitted this value type"),
            )
            .expect("writing to String cannot fail");
        }
        typeql.push_str(";\n");
        debug_assert_eq!(spec.variables.len(), plan.inputs().len());
    }
    typeql.push_str("match\n");
    let Some(ReadStage::Match { patterns }) = plan.pipeline().first() else {
        return Err(failure(
            DiagnosticCategory::Integrity,
            "query_v2_match_not_first",
            "a validated plan always opens with its match stage",
        ));
    };
    render_patterns(&mut typeql, validated, patterns, row, 0)?;

    // Reachable is an existential predicate over the declared root
    // environment, not a request for one row per proof path. TypeQL distinct
    // does not collapse equal projections produced by different disjunction
    // branches, so aggregate every proof by the complete visible environment
    // and then project the synthetic count away. This happens before any
    // user-authored select/reduce/sort/window stage, and rows that differ in
    // any declared visible binding remain distinct.
    let reachability_projection = contains_reachable(patterns);
    if reachability_projection {
        typeql.push_str("reduce $RreachableProofCount = count groupby ");
        render_variable_list(&mut typeql, plan, validated.root_visibility())?;
        typeql.push_str(";\nselect ");
        render_variable_list(&mut typeql, plan, validated.root_visibility())?;
        typeql.push_str(";\n");
    }

    // A binding established only inside a negation is a witness the
    // provider never returns as a column. When the plan carries no
    // explicit Select or Reduce and a witness narrows the environment,
    // project the validator-derived root visibility exactly, so implicit
    // projection never requests a column no provider row can carry.
    let has_projection_stage = plan
        .pipeline()
        .iter()
        .any(|stage| matches!(stage, ReadStage::Select { .. } | ReadStage::Reduce { .. }));
    if !reachability_projection
        && !has_projection_stage
        && validated.root_visibility().len() < plan.bindings().len()
    {
        typeql.push_str("select ");
        render_variable_list(&mut typeql, plan, validated.root_visibility())?;
        typeql.push_str(";\n");
    }

    let exists_probe = matches!(invocation.operation(), QueryOperation::Exists);
    let max_provider_items = if exists_probe {
        Some(1)
    } else {
        max_provider_items
    };
    let mut rendered_limit = false;
    for stage in &plan.pipeline()[1..] {
        match stage {
            ReadStage::Match { .. } => {
                return Err(failure(
                    DiagnosticCategory::Integrity,
                    "query_v2_match_not_first",
                    "a validated plan carries exactly one match stage",
                ));
            }
            ReadStage::Select { bindings } => {
                typeql.push_str("select ");
                render_variable_list(&mut typeql, plan, bindings)?;
                typeql.push_str(";\n");
            }
            ReadStage::Require { bindings } => {
                typeql.push_str("require ");
                render_variable_list(&mut typeql, plan, bindings)?;
                typeql.push_str(";\n");
            }
            ReadStage::Distinct => typeql.push_str("distinct;\n"),
            ReadStage::Reduce {
                assignments,
                groups,
            } => {
                typeql.push_str("reduce ");
                for (index, assignment) in assignments.iter().enumerate() {
                    if index != 0 {
                        typeql.push_str(", ");
                    }
                    let keyword = match assignment.reducer() {
                        type_bridge_contract::query_plan::Reducer::Count => "count",
                        type_bridge_contract::query_plan::Reducer::Max => "max",
                        type_bridge_contract::query_plan::Reducer::Mean => "mean",
                        type_bridge_contract::query_plan::Reducer::Median => "median",
                        type_bridge_contract::query_plan::Reducer::Min => "min",
                        type_bridge_contract::query_plan::Reducer::Std => "std",
                        type_bridge_contract::query_plan::Reducer::Sum => "sum",
                    };
                    write!(
                        typeql,
                        "${} = {keyword}",
                        variable(plan, assignment.assigned())?,
                    )
                    .expect("writing to String cannot fail");
                    if let Some(input) = assignment.input() {
                        write!(typeql, "(${})", variable(plan, input)?)
                            .expect("writing to String cannot fail");
                    }
                }
                if !groups.is_empty() {
                    typeql.push_str(" groupby ");
                    render_variable_list(&mut typeql, plan, groups)?;
                }
                typeql.push_str(";\n");
            }
            ReadStage::Sort { terms } => {
                typeql.push_str("sort ");
                for (index, term) in terms.iter().enumerate() {
                    if index != 0 {
                        typeql.push_str(", ");
                    }
                    write!(
                        typeql,
                        "${} {}",
                        variable(plan, term.binding())?,
                        match term.direction() {
                            type_bridge_contract::query_plan::OrderDirection::Ascending => "asc",
                            type_bridge_contract::query_plan::OrderDirection::Descending => "desc",
                        },
                    )
                    .expect("writing to String cannot fail");
                }
                typeql.push_str(";\n");
            }
            ReadStage::Offset { rows } => {
                writeln!(typeql, "offset {rows};").expect("writing to String cannot fail");
            }
            ReadStage::Limit { rows } => {
                let rows = max_provider_items.map_or(*rows, |maximum| (*rows).min(maximum));
                writeln!(typeql, "limit {rows};").expect("writing to String cannot fail");
                rendered_limit = true;
            }
        }
    }

    // The execution bound belongs in the TypeQL statement so the runtime can
    // consume its terminal frame instead of abandoning a resumable driver
    // stream. This must precede a document `fetch` output clause. Existing
    // limits are clamped above, so an explicit `limit 0` remains zero.
    if let Some(max_provider_items) = max_provider_items
        && !rendered_limit
    {
        writeln!(typeql, "limit {max_provider_items};").expect("writing to String cannot fail");
    }

    if let type_bridge_contract::query_plan::QueryOutput::Documents { fields } = plan.output() {
        typeql.push_str("fetch {\n");
        for (index, field) in fields.iter().enumerate() {
            write!(typeql, "    \"{}\": ", field.key().as_str())
                .expect("writing to String cannot fail");
            match field.source() {
                type_bridge_contract::query_plan::DocumentSource::Binding { binding } => {
                    write!(typeql, "${}", variable(plan, *binding)?)
                        .expect("writing to String cannot fail");
                }
                type_bridge_contract::query_plan::DocumentSource::AttributeList {
                    attribute,
                    owner,
                } => {
                    if let Some(max_collection_members) = max_collection_members {
                        let sentinel_limit = max_collection_members.saturating_add(1);
                        let fetch_variable = format!("FtbDocumentValue{index}");
                        write!(
                            typeql,
                            "[\n        match ${} has {} ${fetch_variable};\n        \
                             limit {sentinel_limit};\n        return {{ ${fetch_variable} }};\n    ]",
                            variable(plan, *owner)?,
                            attribute.label(),
                        )
                        .expect("writing to String cannot fail");
                    } else {
                        write!(
                            typeql,
                            "[ ${}.{} ]",
                            variable(plan, *owner)?,
                            attribute.label(),
                        )
                        .expect("writing to String cannot fail");
                    }
                }
            }
            typeql.push_str(if index + 1 == fields.len() {
                "\n"
            } else {
                ",\n"
            });
        }
        typeql.push_str("};\n");
    }

    if typeql.len() > MAX_CANONICAL_BYTES {
        return Err(failure(
            DiagnosticCategory::ResourceLimit,
            "query_v2_lowered_typeql_byte_limit",
            "lowered TypeQL exceeds the canonical protocol byte ceiling",
        ));
    }

    Ok(LoweredQuery {
        given,
        operation: invocation.operation(),
        output_schema: validated.output_schema().clone(),
        typeql,
    })
}

/// Charge schema-refined reachability lowering against the validated plan's
/// aggregate predicate-node budget before any provider call.
///
/// Context-free plan validation charges one clause for a zero-hop identity.
/// TypeDB's mixed identity/relation planner workaround instead emits one exact
/// typed identity branch per concrete source-domain type, so that
/// schema-dependent multiplicity must be charged after schema validation.
fn preflight_lowered_structure(validated: &ValidatedQuery) -> Result<(), Diagnostic> {
    let plan = validated.plan();
    let mut nodes = 0usize;
    for function in plan.functions() {
        for pattern in function.body() {
            charge_lowered_pattern(validated, pattern, &mut nodes)?;
        }
    }
    let Some(ReadStage::Match { patterns }) = plan.pipeline().first() else {
        return Err(failure(
            DiagnosticCategory::Integrity,
            "query_v2_match_not_first",
            "a validated plan always opens with its match stage",
        ));
    };
    for pattern in patterns {
        charge_lowered_pattern(validated, pattern, &mut nodes)?;
    }
    Ok(())
}

fn charge_lowered_pattern(
    validated: &ValidatedQuery,
    pattern: &QueryPattern,
    nodes: &mut usize,
) -> Result<(), Diagnostic> {
    let charge = match pattern {
        QueryPattern::Reachable {
            min_depth,
            max_depth,
            source,
            ..
        } => {
            let first_positive = usize::from((*min_depth).max(1));
            let maximum = usize::from(*max_depth);
            let positive_hops = if first_positive <= maximum {
                (first_positive..=maximum).try_fold(0usize, usize::checked_add)
            } else {
                Some(0)
            };
            let zero_branches = if *min_depth == 0 {
                Some(
                    validated
                        .binding_domain(source)
                        .ok_or_else(|| {
                            failure(
                                DiagnosticCategory::Integrity,
                                "query_v2_reachable_source_domain",
                                "a validated reachable source has no schema-derived domain",
                            )
                        })?
                        .type_ids()
                        .len(),
                )
            } else {
                Some(0)
            };
            positive_hops.and_then(|hops| zero_branches.and_then(|zero| hops.checked_add(zero)))
        }
        QueryPattern::Or { branches } => {
            charge_nodes(validated, nodes, 1)?;
            for branch in branches {
                for child in branch {
                    charge_lowered_pattern(validated, child, nodes)?;
                }
            }
            return Ok(());
        }
        QueryPattern::Not { patterns } | QueryPattern::Try { patterns } => {
            charge_nodes(validated, nodes, 1)?;
            for child in patterns {
                charge_lowered_pattern(validated, child, nodes)?;
            }
            return Ok(());
        }
        QueryPattern::Isa { .. }
        | QueryPattern::Has { .. }
        | QueryPattern::Links { .. }
        | QueryPattern::Value { .. }
        | QueryPattern::FunctionCall { .. } => Some(1),
    }
    .ok_or_else(reachability_expansion_limit)?;
    charge_nodes(validated, nodes, charge)
}

fn charge_nodes(
    validated: &ValidatedQuery,
    nodes: &mut usize,
    charge: usize,
) -> Result<(), Diagnostic> {
    *nodes = nodes
        .checked_add(charge)
        .ok_or_else(reachability_expansion_limit)?;
    if !validated.structural_limits().allows_predicate_nodes(*nodes) {
        return Err(reachability_expansion_limit());
    }
    Ok(())
}

fn reachability_expansion_limit() -> Diagnostic {
    failure(
        DiagnosticCategory::ResourceLimit,
        "query_v2_reachable_expansion_limit",
        "schema-refined reachability expansion exceeds the plan pattern-node ceiling",
    )
}

/// Build the driver-transported rows for one exact `given` invocation.
///
/// The portable given vocabulary transports absence and every scalar domain
/// the active driver can carry without narrowing. Provider-domain durations
/// use exact unsigned components after preflight rejects negative or wider
/// contract values.
fn given_rows_spec(
    plan: &QueryPlan,
    rows: &[type_bridge_contract::query_plan::InputRow],
) -> Result<crate::session::backend::GivenRowsSpec, Diagnostic> {
    let variables = plan
        .inputs()
        .iter()
        .map(|column| {
            if given_type_keyword(column.value_type()).is_none() {
                return Err(failure(
                    DiagnosticCategory::InvalidContract,
                    "query_v2_given_value_unsupported",
                    "this input value type has no proven given transport",
                ));
            }
            Ok(column.public_name().as_str().to_owned())
        })
        .collect::<Result<Vec<_>, Diagnostic>>()?;
    let rows = rows
        .iter()
        .map(|row| {
            row.values()
                .iter()
                .map(|value| match value {
                    None => Ok(crate::session::backend::GivenValue::Empty),
                    Some(value) => given_value(value).ok_or_else(|| {
                        failure(
                            DiagnosticCategory::InvalidContract,
                            "query_v2_given_value_unsupported",
                            "this input value has no lossless given transport",
                        )
                    }),
                })
                .collect::<Result<Vec<_>, Diagnostic>>()
        })
        .collect::<Result<Vec<_>, Diagnostic>>()?;
    Ok(crate::session::backend::GivenRowsSpec { variables, rows })
}

const fn given_type_keyword(
    tag: type_bridge_contract::value::ValueTypeTag,
) -> Option<&'static str> {
    use type_bridge_contract::value::ValueTypeTag;
    match tag {
        ValueTypeTag::Boolean => Some("boolean"),
        ValueTypeTag::Long => Some("integer"),
        ValueTypeTag::Double => Some("double"),
        ValueTypeTag::String => Some("string"),
        ValueTypeTag::Date => Some("date"),
        ValueTypeTag::DateTime => Some("datetime"),
        ValueTypeTag::DateTimeTz => Some("datetime-tz"),
        ValueTypeTag::Decimal => Some("decimal"),
        // Provider-domain durations cross `given` as exact components after
        // shared temporal preflight has rejected negative or overflowing
        // values.
        ValueTypeTag::Duration => Some("duration"),
    }
}

fn given_value(
    value: &type_bridge_contract::value::CanonicalValue,
) -> Option<crate::session::backend::GivenValue> {
    use crate::session::backend::GivenValue;
    use type_bridge_contract::value::CanonicalValue;
    match value {
        CanonicalValue::Boolean(value) => Some(GivenValue::Boolean(*value)),
        CanonicalValue::Long(value) => Some(GivenValue::Integer(*value)),
        CanonicalValue::Double(value) => Some(GivenValue::Double(value.get())),
        CanonicalValue::String(value) => Some(GivenValue::String(value.as_str().to_owned())),
        CanonicalValue::Date(value) => Some(GivenValue::Date(value.to_string())),
        CanonicalValue::DateTime(value) => Some(GivenValue::Datetime(value.to_string())),
        CanonicalValue::DateTimeTz(value) => Some(GivenValue::DatetimeTzExact {
            local: value.local().to_string(),
            named_zone: match value.zone() {
                type_bridge_contract::temporal::TimeZoneDesignator::Named(name) => {
                    Some(name.clone())
                }
                type_bridge_contract::temporal::TimeZoneDesignator::Utc
                | type_bridge_contract::temporal::TimeZoneDesignator::OffsetSeconds(_) => None,
            },
            effective_offset_seconds: value.effective_offset_seconds(),
        }),
        CanonicalValue::Decimal(value) => Some(GivenValue::Decimal(value.as_str().to_owned())),
        CanonicalValue::Duration(value) => {
            let (negative, months, days, seconds, nanosecond) = value.components();
            let months = u32::try_from(months).ok()?;
            let days = u32::try_from(days).ok()?;
            let nanos = seconds
                .checked_mul(1_000_000_000)?
                .checked_add(u64::from(nanosecond))?;
            (!negative).then_some(GivenValue::Duration {
                months,
                days,
                nanos,
            })
        }
    }
}

/// Validate invocation-dependent transport before provider or transaction I/O.
pub(crate) fn preflight_invocation_transport(
    plan: &QueryPlan,
    invocation: &QueryInvocation,
) -> Result<(), Diagnostic> {
    if !invocation.binds(plan)? {
        return Err(failure(
            DiagnosticCategory::Integrity,
            "query_v2_invocation_plan_mismatch",
            "invocation does not bind the exact validated plan fingerprint",
        ));
    }
    for value in invocation
        .inputs()
        .iter()
        .flat_map(|row| row.values())
        .flatten()
    {
        type_bridge_schema::validate_provider_temporal_value(value)?;
    }
    if invocation_requires_given(invocation) {
        given_rows_spec(plan, invocation.inputs())?;
    }
    Ok(())
}

fn invocation_requires_given(invocation: &QueryInvocation) -> bool {
    invocation.inputs().len() > 1
        || invocation.inputs().first().is_some_and(|row| {
            row.values().iter().any(|value| {
                value.is_none()
                    || matches!(
                        value,
                        Some(type_bridge_contract::value::CanonicalValue::DateTimeTz(_))
                    )
            })
        })
}

fn render_patterns(
    output: &mut String,
    validated: &ValidatedQuery,
    patterns: &[QueryPattern],
    row: Option<&InputRow>,
    depth: usize,
) -> Result<(), Diagnostic> {
    let plan = validated.plan();
    render_scoped_patterns(
        output,
        plan,
        plan.bindings(),
        patterns,
        row,
        depth,
        Some(validated),
    )
}

fn render_scoped_patterns(
    output: &mut String,
    plan: &QueryPlan,
    bindings: &[type_bridge_contract::migration_assertion::AssertionBinding],
    patterns: &[QueryPattern],
    row: Option<&InputRow>,
    depth: usize,
    validated: Option<&ValidatedQuery>,
) -> Result<(), Diagnostic> {
    let indent = "    ".repeat(depth);
    for (pattern_index, pattern) in patterns.iter().enumerate() {
        match pattern {
            QueryPattern::Isa {
                binding,
                include_subtypes,
                type_id,
            } => {
                writeln!(
                    output,
                    "{indent}${} isa{} {};",
                    local_variable(bindings, *binding)?,
                    if *include_subtypes { "" } else { "!" },
                    type_id.label()
                )
                .expect("writing to String cannot fail");
            }
            QueryPattern::Has {
                attribute,
                attribute_id,
                owner,
            } => {
                writeln!(
                    output,
                    "{indent}${} has {} ${};",
                    local_variable(bindings, *owner)?,
                    attribute_id.label(),
                    local_variable(bindings, *attribute)?,
                )
                .expect("writing to String cannot fail");
                writeln!(
                    output,
                    "{indent}${} isa! {};",
                    local_variable(bindings, *attribute)?,
                    attribute_id.label(),
                )
                .expect("writing to String cannot fail");
            }
            QueryPattern::Links {
                players,
                relation,
                relation_id,
            } => {
                write!(
                    output,
                    "{indent}${} isa! {}, links (",
                    local_variable(bindings, *relation)?,
                    relation_id.label(),
                )
                .expect("writing to String cannot fail");
                for (index, player) in players.iter().enumerate() {
                    if index != 0 {
                        output.push_str(", ");
                    }
                    write!(
                        output,
                        "{}: ${}",
                        player.role().label(),
                        local_variable(bindings, player.player())?,
                    )
                    .expect("writing to String cannot fail");
                }
                output.push_str(");\n");
            }
            QueryPattern::Value {
                comparator,
                left,
                right,
            } => {
                writeln!(
                    output,
                    "{indent}{} {} {};",
                    render_operand(plan, bindings, left, row)?,
                    render_comparator(*comparator),
                    render_operand(plan, bindings, right, row)?,
                )
                .expect("writing to String cannot fail");
            }
            QueryPattern::Or { branches } => {
                for (index, branch) in branches.iter().enumerate() {
                    writeln!(output, "{indent}{}{{", if index == 0 { "" } else { "or " },)
                        .expect("writing to String cannot fail");
                    render_scoped_patterns(
                        output,
                        plan,
                        bindings,
                        branch,
                        row,
                        depth + 1,
                        validated,
                    )?;
                    writeln!(
                        output,
                        "{indent}}}{}",
                        if index + 1 == branches.len() { ";" } else { "" },
                    )
                    .expect("writing to String cannot fail");
                }
            }
            QueryPattern::Not { patterns } => {
                writeln!(output, "{indent}not {{").expect("writing to String cannot fail");
                render_scoped_patterns(
                    output,
                    plan,
                    bindings,
                    patterns,
                    row,
                    depth + 1,
                    validated,
                )?;
                writeln!(output, "{indent}}};").expect("writing to String cannot fail");
            }
            QueryPattern::Try { patterns } => {
                writeln!(output, "{indent}try {{").expect("writing to String cannot fail");
                render_scoped_patterns(
                    output,
                    plan,
                    bindings,
                    patterns,
                    row,
                    depth + 1,
                    validated,
                )?;
                writeln!(output, "{indent}}};").expect("writing to String cannot fail");
            }
            QueryPattern::Reachable {
                min_depth,
                max_depth,
                relation,
                role_from,
                role_to,
                source,
                target,
            } => {
                // Uppercase hop intermediates cannot collide with the
                // lowercase plan variable space; the pattern index keeps
                // them unique across root reachability patterns.
                let source_id = *source;
                let source = local_variable(bindings, source_id)?;
                let target = local_variable(bindings, *target)?;
                let mut branches = Vec::with_capacity(
                    usize::from(*max_depth)
                        .saturating_sub(usize::from(*min_depth))
                        .saturating_add(1),
                );
                for length in usize::from(*min_depth)..=usize::from(*max_depth) {
                    if length == 0 {
                        let validated = validated.ok_or_else(|| {
                            failure(
                                DiagnosticCategory::Integrity,
                                "query_v2_reachable_scope",
                                "a validated reachable pattern appears outside the root query scope",
                            )
                        })?;
                        let source_domain =
                            validated.binding_domain(&source_id).ok_or_else(|| {
                                failure(
                                    DiagnosticCategory::Integrity,
                                    "query_v2_reachable_source_domain",
                                    "a validated reachable source has no schema-derived domain",
                                )
                            })?;
                        if source_domain.type_ids().is_empty() {
                            return Err(failure(
                                DiagnosticCategory::Integrity,
                                "query_v2_reachable_source_domain",
                                "a validated reachable source has no schema-derived domain",
                            ));
                        }
                        // TypeDB 3.12 cannot plan a bare identity branch next
                        // to relation branches. One exact typed witness per
                        // validator-derived concrete source type preserves the
                        // full source domain without widening it.
                        for (type_ordinal, type_id) in source_domain.type_ids().iter().enumerate() {
                            // Every disjunction branch owns its complete local
                            // witness environment. Reusing one variable across
                            // mutually exclusive concrete-type branches makes
                            // TypeDB require that witness in sibling positive
                            // path branches (REP44).
                            let zero_var = format!("R{pattern_index}z{type_ordinal}");
                            branches.push(format!(
                                "${zero_var} isa! {}; ${source} is ${zero_var}; ${target} is ${zero_var};",
                                type_id.label()
                            ));
                        }
                        continue;
                    }
                    let mut branch = String::new();
                    let hop_var = |hop: usize| format!("R{pattern_index}l{length}h{hop}");
                    for hop in 1..=length {
                        if hop != 1 {
                            branch.push(' ');
                        }
                        let from = if hop == 1 {
                            source.to_owned()
                        } else {
                            hop_var(hop - 1)
                        };
                        let to = if hop == length {
                            target.to_owned()
                        } else {
                            hop_var(hop)
                        };
                        // The Reachable contract promises the exact relation
                        // type for every hop; `isa!` keeps subtype instances
                        // out exactly like every other exact-type pattern.
                        write!(
                            branch,
                            "({}: ${from}, {}: ${to}) isa! {};",
                            role_from.label(),
                            role_to.label(),
                            relation.label(),
                        )
                        .expect("writing to String cannot fail");
                    }
                    branches.push(branch);
                }
                if branches.len() == 1 {
                    writeln!(output, "{indent}{}", branches[0])
                        .expect("writing to String cannot fail");
                } else {
                    let joined = branches
                        .iter()
                        .map(|branch| format!("{{ {branch} }}"))
                        .collect::<Vec<_>>()
                        .join(" or ");
                    writeln!(output, "{indent}{joined};").expect("writing to String cannot fail");
                }
            }
            QueryPattern::FunctionCall {
                arguments,
                assigned,
                function,
            } => {
                write!(
                    output,
                    "{indent}let ${} = {}(",
                    local_variable(bindings, *assigned)?,
                    function.label(),
                )
                .expect("writing to String cannot fail");
                for (index, argument) in arguments.iter().enumerate() {
                    if index != 0 {
                        output.push_str(", ");
                    }
                    output.push_str(&render_operand(plan, bindings, argument, row)?);
                }
                output.push_str(");\n");
            }
        }
    }
    Ok(())
}

fn contains_reachable(patterns: &[QueryPattern]) -> bool {
    patterns.iter().any(|pattern| match pattern {
        QueryPattern::Reachable { .. } => true,
        QueryPattern::Or { branches } => branches.iter().any(|branch| contains_reachable(branch)),
        QueryPattern::Not { patterns } | QueryPattern::Try { patterns } => {
            contains_reachable(patterns)
        }
        QueryPattern::Isa { .. }
        | QueryPattern::Has { .. }
        | QueryPattern::Links { .. }
        | QueryPattern::Value { .. }
        | QueryPattern::FunctionCall { .. } => false,
    })
}

fn render_variable_list(
    output: &mut String,
    plan: &QueryPlan,
    bindings: &[BindingId],
) -> Result<(), Diagnostic> {
    for (index, binding) in bindings.iter().enumerate() {
        if index != 0 {
            output.push_str(", ");
        }
        output.push('$');
        output.push_str(variable(plan, *binding)?);
    }
    Ok(())
}

fn render_operand(
    plan: &QueryPlan,
    bindings: &[type_bridge_contract::migration_assertion::AssertionBinding],
    operand: &QueryOperand,
    row: Option<&InputRow>,
) -> Result<String, Diagnostic> {
    match operand {
        QueryOperand::Binding { binding } => {
            Ok(format!("${}", local_variable(bindings, *binding)?))
        }
        QueryOperand::Literal { value } => render_literal(value),
        QueryOperand::Input { column } => match row {
            Some(row) => {
                let value = row
                    .values()
                    .get(usize::from(column.get()))
                    .and_then(Option::as_ref)
                    .ok_or_else(|| {
                        failure(
                            DiagnosticCategory::InvalidContract,
                            "query_v2_missing_input_value",
                            "pattern reads an input column absent from the bound row",
                        )
                    })?;
                render_literal(value)
            }
            // Given lowering: the column is a driver-bound row variable.
            None => {
                let column = plan
                    .inputs()
                    .get(usize::from(column.get()))
                    .ok_or_else(|| {
                        failure(
                            DiagnosticCategory::InvalidContract,
                            "query_v2_missing_input_value",
                            "pattern reads an undeclared input column",
                        )
                    })?;
                Ok(format!("${}", column.public_name().as_str()))
            }
        },
    }
}

fn variable(plan: &QueryPlan, binding: BindingId) -> Result<&str, Diagnostic> {
    local_variable(plan.bindings(), binding)
}

fn local_variable(
    bindings: &[type_bridge_contract::migration_assertion::AssertionBinding],
    binding: BindingId,
) -> Result<&str, Diagnostic> {
    bindings
        .get(usize::from(binding.get()))
        .map(|binding| binding.variable().as_str())
        .ok_or_else(|| {
            failure(
                DiagnosticCategory::Integrity,
                "query_v2_unknown_binding",
                "validated plan references an unknown binding",
            )
        })
}

pub(crate) fn failure(
    category: DiagnosticCategory,
    code: &'static str,
    message: &'static str,
) -> Diagnostic {
    Diagnostic::new(
        category,
        DiagnosticCode::new(code).expect("static query lowering diagnostic code"),
        message,
    )
}

/// Preserve the stable resource diagnostics emitted by the bounded provider
/// seam while redacting arbitrary driver text for every other provider error.
pub(crate) fn provider_diagnostic(
    error: &crate::error::OrmError,
    generic_code: &'static str,
    generic_message: &'static str,
) -> Diagnostic {
    let crate::error::OrmError::Match(error) = error else {
        return failure(DiagnosticCategory::Integrity, generic_code, generic_message);
    };
    let (diagnostic_category, code, message) = match (error.category(), error.code().as_str()) {
        (crate::match_request::MatchErrorCategory::Cancelled, "provider_cancelled") => (
            DiagnosticCategory::Cancelled,
            "provider_cancelled",
            "provider answer processing was cancelled",
        ),
        (
            crate::match_request::MatchErrorCategory::ResourceLimit,
            "transaction_deadline_exceeded",
        ) => (
            DiagnosticCategory::ResourceLimit,
            "transaction_deadline_exceeded",
            "provider transaction deadline expired",
        ),
        (crate::match_request::MatchErrorCategory::ResourceLimit, "processed_item_limit") => (
            DiagnosticCategory::ResourceLimit,
            "processed_item_limit",
            "provider answer exceeded the processed-item ceiling",
        ),
        (crate::match_request::MatchErrorCategory::ResourceLimit, "response_byte_limit") => (
            DiagnosticCategory::ResourceLimit,
            "response_byte_limit",
            "provider answer exceeded the response-byte ceiling",
        ),
        (
            crate::match_request::MatchErrorCategory::ResourceLimit,
            "processed_item_counter_overflow",
        ) => (
            DiagnosticCategory::ResourceLimit,
            "processed_item_counter_overflow",
            "processed provider item counter overflowed",
        ),
        (
            crate::match_request::MatchErrorCategory::ResourceLimit,
            "answer_byte_counter_overflow",
        ) => (
            DiagnosticCategory::ResourceLimit,
            "answer_byte_counter_overflow",
            "provider answer byte counter overflowed",
        ),
        (
            crate::match_request::MatchErrorCategory::ResourceLimit,
            "query_v2_document_member_limit",
        ) => (
            DiagnosticCategory::ResourceLimit,
            "query_v2_document_member_limit",
            "document lists exceed the aggregate member ceiling",
        ),
        (crate::match_request::MatchErrorCategory::ResourceLimit, "collected_concept_limit") => (
            DiagnosticCategory::ResourceLimit,
            "collected_concept_limit",
            "typed reduction cells exceed the collection-member ceiling",
        ),
        (crate::match_request::MatchErrorCategory::ResourceLimit, "reduction_group_limit") => (
            DiagnosticCategory::ResourceLimit,
            "reduction_group_limit",
            "typed reduction groups exceed the result-item ceiling",
        ),
        (crate::match_request::MatchErrorCategory::ResourceLimit, "reduction_overflow") => (
            DiagnosticCategory::ResourceLimit,
            "reduction_overflow",
            "typed reduction arithmetic exceeded its canonical scalar domain",
        ),
        _ => return failure(DiagnosticCategory::Integrity, generic_code, generic_message),
    };
    failure(diagnostic_category, code, message)
}

fn query_v2_provider_resource_error(
    code: &'static str,
    message: impl Into<String>,
) -> crate::error::OrmError {
    crate::match_request::MatchError::new(
        crate::match_request::MatchErrorCategory::ResourceLimit,
        code,
        message,
    )
    .at(crate::match_request::MatchErrorPathSegment::ProviderEvidence)
    .into()
}

fn query_v2_result_validation_error(diagnostic: Diagnostic) -> QueryV2ExecutionError {
    if diagnostic.category() == DiagnosticCategory::ResourceLimit
        && diagnostic.code().as_str() == "query_v2_document_member_limit"
    {
        return QueryV2ExecutionError::Provider(query_v2_provider_resource_error(
            "query_v2_document_member_limit",
            diagnostic.message().to_owned(),
        ));
    }
    QueryV2ExecutionError::Validation(diagnostic)
}

/// Return the output projection columns of one validated plan.
///
/// The provider `select` keeps the plan's visible environment; the exact
/// projection to output columns is enforced in Rust during result
/// validation, mirroring the released V1 executor discipline.
#[must_use]
pub fn output_columns(plan: &QueryPlan) -> &[BindingId] {
    match plan.output() {
        QueryOutput::Rows { columns } => columns,
        QueryOutput::Documents { .. } => &[],
    }
}

/// One typed, evidence-validated output value.
#[derive(Clone, Debug, PartialEq)]
pub enum QueryRowValue {
    /// An entity or relation reference: exact runtime type plus identity.
    Thing {
        /// The validated runtime type.
        type_id: type_bridge_contract::id::TypeId,
        /// The provider instance identity.
        iid: String,
    },
    /// An attribute instance with its parsed canonical value.
    Attribute {
        /// The validated runtime attribute type.
        type_id: type_bridge_contract::id::TypeId,
        /// The exact typed scalar value.
        value: type_bridge_contract::value::CanonicalValue,
    },
    /// A pure typed value produced by a schema-function assignment.
    Value {
        /// The exact typed scalar value.
        value: type_bridge_contract::value::CanonicalValue,
    },
    /// An explicit absence in an optional output column.
    Absent,
}

/// One evidence-validated output row, positional by output column.
#[derive(Clone, Debug, PartialEq)]
pub struct QueryResultRow {
    values: Vec<QueryRowValue>,
}

impl QueryResultRow {
    pub(crate) fn from_values(values: Vec<QueryRowValue>) -> Self {
        Self { values }
    }

    /// Return positional typed values in output-column order.
    #[must_use]
    pub fn values(&self) -> &[QueryRowValue] {
        &self.values
    }
}

/// One typed, evidence-validated document field value.
#[derive(Clone, Debug, PartialEq)]
pub enum DocumentFieldValue {
    /// One exact typed scalar.
    Scalar(type_bridge_contract::value::CanonicalValue),
    /// An explicit absence in an optional scalar field.
    Absent,
    /// A typed list of every owned attribute value.
    List(Vec<type_bridge_contract::value::CanonicalValue>),
}

/// One evidence-validated fetched document, positional by document column.
#[derive(Clone, Debug, PartialEq)]
pub struct QueryResultDocument {
    values: Vec<DocumentFieldValue>,
}

impl QueryResultDocument {
    pub(crate) fn from_values(values: Vec<DocumentFieldValue>) -> Self {
        Self { values }
    }

    /// Return field values in validated document-column order.
    pub fn values(&self) -> &[DocumentFieldValue] {
        &self.values
    }
}

/// The validated terminal result of one executed invocation.
#[derive(Clone, Debug, PartialEq)]
pub enum QueryV2Outcome {
    /// Evidence-validated projected rows in provider order.
    Rows(Vec<QueryResultRow>),
    /// Evidence-validated fetched documents in provider order.
    Documents(Vec<QueryResultDocument>),
    /// The exact number of returned rows.
    Count(u64),
    /// Whether at least one row exists.
    Exists(bool),
}

/// Failure executing one lowered invocation.
#[derive(Debug, thiserror::Error)]
pub enum QueryV2ExecutionError {
    /// The provider call failed.
    #[error("query execution provider call failed: {0}")]
    Provider(crate::error::OrmError),
    /// Lowering, preflight, or result-evidence validation failed.
    #[error("query execution validation failed: {0}")]
    Validation(Diagnostic),
}

/// Internal observer invoked after evidence validation and before retention.
pub(crate) trait QueryV2ValidatedItemObserver: Send {
    fn observe_row(&mut self, row: &QueryResultRow) -> Result<(), Diagnostic>;

    fn observe_document(&mut self, document: &QueryResultDocument) -> Result<(), Diagnostic>;
}

/// Execute one validated invocation through an open read transaction.
///
/// Rows are streamed under the caller's explicit bounded limits and each
/// provider row is evidence-validated against the derived row schema before
/// projection; count and exists reuse the same validated stream and are
/// decided in Rust, mirroring the released V1 executor discipline.
pub async fn execute_validated_query(
    transaction: &mut crate::session::transaction::Transaction,
    validated: &ValidatedQuery,
    invocation: &QueryInvocation,
    limits: crate::session::backend::QueryV2AnswerLimits,
) -> Result<QueryV2Outcome, QueryV2ExecutionError> {
    preflight_invocation_transport(validated.plan(), invocation)
        .map_err(QueryV2ExecutionError::Validation)?;
    let transaction = transaction
        .provider_mut()
        .map_err(QueryV2ExecutionError::Provider)?;
    let mut provider = crate::migration_assertion::TransactionAssertionProvider { transaction };
    execute_with_provider(&mut provider, validated, invocation, limits).await
}

pub(crate) async fn execute_with_provider<
    P: crate::migration_assertion::AssertionProviderCall + ?Sized,
>(
    provider: &mut P,
    validated: &ValidatedQuery,
    invocation: &QueryInvocation,
    limits: crate::session::backend::QueryV2AnswerLimits,
) -> Result<QueryV2Outcome, QueryV2ExecutionError> {
    execute_with_provider_observer(provider, validated, invocation, limits, None).await
}

pub(crate) async fn execute_with_provider_observer<
    P: crate::migration_assertion::AssertionProviderCall + ?Sized,
>(
    provider: &mut P,
    validated: &ValidatedQuery,
    invocation: &QueryInvocation,
    limits: crate::session::backend::QueryV2AnswerLimits,
    mut observer: Option<&mut dyn QueryV2ValidatedItemObserver>,
) -> Result<QueryV2Outcome, QueryV2ExecutionError> {
    let mut semantic_limits = limits;
    semantic_limits.max_collection_members = semantic_limits
        .max_collection_members
        .min(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).expect("canonical limit fits u64"));
    let exists_probe = matches!(invocation.operation(), QueryOperation::Exists);
    let provider_item_sentinel = if exists_probe {
        1
    } else {
        semantic_limits.answer.max_items.saturating_add(1)
    };
    let lowered = lower_validated_query_with_execution_limits(
        validated,
        invocation,
        Some(semantic_limits.max_collection_members),
        Some(provider_item_sentinel),
    )
    .map_err(QueryV2ExecutionError::Validation)?;
    let allow_input_echo = lowered.given_rows().is_some();

    // The statement itself is finite at the semantic item/list sentinels, and
    // the provider/session seam independently retains the finite raw-answer
    // byte and aggregate-member ceilings. The consumer repeats those checks on
    // the projected semantic evidence; neither layer may become unbounded just
    // because a remote caller meters a different canonical wire encoding.
    let mut provider_limits = semantic_limits.clone();
    provider_limits.answer.max_items = provider_item_sentinel;

    let mut rows: Vec<QueryResultRow> = Vec::new();
    let mut documents: Vec<QueryResultDocument> = Vec::new();
    let max_items = semantic_limits.answer.max_items;
    let max_bytes = semantic_limits.answer.max_bytes;
    let max_collection_members = semantic_limits.max_collection_members;
    let mut processed_items = 0_u64;
    let mut response_bytes = 0_u64;
    let mut collection_members = 0_u64;
    let mut deferred_error: Option<QueryV2ExecutionError> = None;
    let mut consumer = |item| {
        // Once a deterministic first failure is known, keep draining the
        // finite statement without parsing or retaining any later evidence.
        if deferred_error.is_some() {
            return Ok(crate::session::backend::AnswerControl::Continue);
        }

        let Some(next_items) = processed_items.checked_add(1) else {
            deferred_error = Some(QueryV2ExecutionError::Provider(
                query_v2_provider_resource_error(
                    "processed_item_counter_overflow",
                    "processed provider item counter overflowed",
                ),
            ));
            return Ok(crate::session::backend::AnswerControl::Continue);
        };
        if next_items > max_items {
            deferred_error = Some(QueryV2ExecutionError::Provider(
                query_v2_provider_resource_error(
                    "processed_item_limit",
                    "provider answer exceeded the processed-item ceiling",
                ),
            ));
            return Ok(crate::session::backend::AnswerControl::Continue);
        }

        let value = match &item {
            crate::session::backend::AnswerItem::Row(value)
            | crate::session::backend::AnswerItem::Document(value) => value,
        };
        let encoded = match serde_json::to_vec(value) {
            Ok(encoded) => encoded,
            Err(error) => {
                deferred_error = Some(QueryV2ExecutionError::Provider(
                    crate::error::OrmError::QueryExecution(format!("Answer encode: {error}")),
                ));
                return Ok(crate::session::backend::AnswerControl::Continue);
            }
        };
        let Ok(encoded_bytes) = u64::try_from(encoded.len()) else {
            deferred_error = Some(QueryV2ExecutionError::Provider(
                query_v2_provider_resource_error(
                    "answer_byte_counter_overflow",
                    "encoded provider answer length exceeds the counter range",
                ),
            ));
            return Ok(crate::session::backend::AnswerControl::Continue);
        };
        let Some(next_bytes) = response_bytes.checked_add(encoded_bytes) else {
            deferred_error = Some(QueryV2ExecutionError::Provider(
                query_v2_provider_resource_error(
                    "answer_byte_counter_overflow",
                    "provider answer byte counter overflowed",
                ),
            ));
            return Ok(crate::session::backend::AnswerControl::Continue);
        };
        if next_bytes > max_bytes {
            deferred_error = Some(QueryV2ExecutionError::Provider(
                query_v2_provider_resource_error(
                    "response_byte_limit",
                    "provider answer exceeded the response-byte ceiling",
                ),
            ));
            return Ok(crate::session::backend::AnswerControl::Continue);
        }
        processed_items = next_items;
        response_bytes = next_bytes;

        let validated_item = match (validated.output_schema(), item) {
            (OutputSchema::Rows(schema), crate::session::backend::AnswerItem::Row(row)) => {
                validate_result_row(&row, validated, schema, allow_input_echo).and_then(|values| {
                    let row = QueryResultRow { values };
                    if let Some(observer) = observer.as_deref_mut() {
                        observer.observe_row(&row)?;
                    }
                    rows.push(row);
                    Ok(())
                })
            }
            (
                OutputSchema::Documents(schema),
                crate::session::backend::AnswerItem::Document(document),
            ) => validate_result_document(
                &document,
                schema,
                &mut collection_members,
                max_collection_members,
            )
            .and_then(|values| {
                let document = QueryResultDocument { values };
                if let Some(observer) = observer.as_deref_mut() {
                    observer.observe_document(&document)?;
                }
                documents.push(document);
                Ok(())
            }),
            (OutputSchema::Rows(_), _) => Err(failure(
                DiagnosticCategory::InvalidContract,
                "query_v2_result_not_row",
                "provider returned a document for a selected-row query",
            )),
            (OutputSchema::Documents(_), _) => Err(failure(
                DiagnosticCategory::InvalidContract,
                "query_v2_result_not_document",
                "provider returned a row for a fetched-document query",
            )),
        };
        match validated_item {
            Ok(()) => Ok(crate::session::backend::AnswerControl::Continue),
            Err(diagnostic) => {
                deferred_error = Some(query_v2_result_validation_error(diagnostic));
                Ok(crate::session::backend::AnswerControl::Continue)
            }
        }
    };
    let provider_result = match lowered.given_rows() {
        Some(spec) => {
            if !provider.supports_given_rows() {
                return Err(QueryV2ExecutionError::Validation(failure(
                    DiagnosticCategory::InvalidContract,
                    "query_v2_given_transport_unsupported",
                    "this invocation requires the native given transport capability",
                )));
            }
            provider
                .query_v2_with_rows_bounded(
                    lowered.typeql(),
                    spec.clone(),
                    provider_limits,
                    &mut consumer,
                )
                .await
        }
        None => {
            provider
                .query_v2_bounded(lowered.typeql(), provider_limits, &mut consumer)
                .await
        }
    };
    // A provider failure observed while draining follows the semantic or
    // evidence failure that made draining necessary. Preserve that first
    // stable failure just as the former immediate-return path did.
    if let Some(error) = deferred_error {
        return Err(error);
    }
    let stats = provider_result.map_err(QueryV2ExecutionError::Provider)?;
    if stats.stopped_early {
        return Err(QueryV2ExecutionError::Validation(failure(
            DiagnosticCategory::InvalidContract,
            "query_v2_provider_stream_not_exhausted",
            "provider stopped before the bounded query statement reached its terminal frame",
        )));
    }

    let answers = rows.len() + documents.len();
    Ok(match lowered.operation() {
        QueryOperation::Rows => match validated.output_schema() {
            OutputSchema::Rows(_) => QueryV2Outcome::Rows(rows),
            OutputSchema::Documents(_) => QueryV2Outcome::Documents(documents),
        },
        QueryOperation::Count => QueryV2Outcome::Count(answers as u64),
        QueryOperation::Exists => QueryV2Outcome::Exists(answers != 0),
    })
}

/// Validate one provider document against the derived document schema.
fn validate_result_document(
    document: &serde_json::Value,
    schema: &DocumentSchema,
    collection_members: &mut u64,
    max_collection_members: u64,
) -> Result<Vec<DocumentFieldValue>, Diagnostic> {
    let object = document.as_object().ok_or_else(|| {
        failure(
            DiagnosticCategory::InvalidContract,
            "query_v2_result_document_malformed",
            "provider document must be a JSON object keyed by fetch keys",
        )
    })?;
    if object.len() != schema.columns().len()
        || schema
            .columns()
            .iter()
            .any(|column| !object.contains_key(column.key().as_str()))
    {
        return Err(failure(
            DiagnosticCategory::InvalidContract,
            "query_v2_result_column_mismatch",
            "provider document keys do not exactly match the fetch schema",
        ));
    }
    let mut values = Vec::with_capacity(schema.columns().len());
    for column in schema.columns() {
        let value = &object[column.key().as_str()];
        values.push(match column.shape() {
            DocumentColumnShape::Scalar {
                value_type,
                optional,
            } => {
                if value.is_null() {
                    if !optional {
                        return Err(failure(
                            DiagnosticCategory::Integrity,
                            "query_v2_result_type_mismatch",
                            "mandatory document field carries an explicit absence",
                        ));
                    }
                    DocumentFieldValue::Absent
                } else {
                    DocumentFieldValue::Scalar(crate::migration_assertion::parse_provider_value(
                        value,
                        *value_type,
                    )?)
                }
            }
            DocumentColumnShape::List { element_type, .. } => {
                let elements = value.as_array().ok_or_else(|| {
                    failure(
                        DiagnosticCategory::Integrity,
                        "query_v2_result_type_mismatch",
                        "attribute list field is not a JSON array",
                    )
                })?;
                let field_members = u64::try_from(elements.len()).map_err(|_| {
                    failure(
                        DiagnosticCategory::ResourceLimit,
                        "query_v2_document_member_limit",
                        "document list member count exceeds the supported counter range",
                    )
                })?;
                let next_members =
                    collection_members
                        .checked_add(field_members)
                        .ok_or_else(|| {
                            failure(
                                DiagnosticCategory::ResourceLimit,
                                "query_v2_document_member_limit",
                                "document list member counter overflowed",
                            )
                        })?;
                if next_members > max_collection_members {
                    return Err(failure(
                        DiagnosticCategory::ResourceLimit,
                        "query_v2_document_member_limit",
                        "document lists exceed the aggregate member ceiling",
                    ));
                }
                *collection_members = next_members;
                DocumentFieldValue::List(
                    elements
                        .iter()
                        .map(|element| {
                            crate::migration_assertion::parse_provider_value(element, *element_type)
                        })
                        .collect::<Result<Vec<_>, _>>()?,
                )
            }
        });
    }
    Ok(values)
}

/// Validate one provider row and project it to typed output values.
fn validate_result_row(
    row: &serde_json::Value,
    validated: &ValidatedQuery,
    schema: &RowSchema,
    allow_input_echo: bool,
) -> Result<Vec<QueryRowValue>, Diagnostic> {
    use type_bridge_contract::id::{TypeId, TypeKind};

    let object = row.as_object().ok_or_else(|| {
        failure(
            DiagnosticCategory::InvalidContract,
            "query_v2_result_row_malformed",
            "provider row must be a JSON object keyed by selected variables",
        )
    })?;
    let visible = visible_variables(validated);
    // Given lowerings echo the driver-bound input variables in every row;
    // input names are contract-unique against binding names, so tolerating
    // exactly them stays closed.
    let inputs: std::collections::BTreeSet<&str> = if allow_input_echo {
        validated
            .plan()
            .inputs()
            .iter()
            .map(|column| column.public_name().as_str())
            .collect()
    } else {
        std::collections::BTreeSet::new()
    };
    if visible.iter().any(|name| !object.contains_key(*name))
        || object
            .keys()
            .any(|key| !visible.contains(&key.as_str()) && !inputs.contains(key.as_str()))
    {
        return Err(failure(
            DiagnosticCategory::InvalidContract,
            "query_v2_result_column_mismatch",
            "provider row columns do not exactly match the visible row environment",
        ));
    }

    let mut values = Vec::with_capacity(schema.columns().len());
    for column in schema.columns() {
        let domain = column.domain();
        // Optional columns carry an explicit null when their try body did
        // not match; mandatory columns never do.
        if column.optional()
            && object
                .get(column.variable().as_str())
                .is_some_and(serde_json::Value::is_null)
        {
            values.push(QueryRowValue::Absent);
            continue;
        }
        let concept = object
            .get(column.variable().as_str())
            .and_then(serde_json::Value::as_object)
            .ok_or_else(|| {
                failure(
                    DiagnosticCategory::InvalidContract,
                    "query_v2_result_concept_malformed",
                    "output column is not a provider concept object",
                )
            })?;
        // A pure value binding (empty thing domain, exact scalar) carries a
        // value concept. Drivers may include their informational value label,
        // but it is not a schema type identity.
        if domain.type_ids().is_empty() {
            let Some(expected) = domain.value_type() else {
                return Err(failure(
                    DiagnosticCategory::Integrity,
                    "query_v2_result_type_mismatch",
                    "output column has neither a thing domain nor a scalar domain",
                ));
            };
            let category =
                crate::migration_assertion::string_field(concept, "category", column.variable())?;
            if !matches!(category, "value" | "Value") {
                return Err(failure(
                    DiagnosticCategory::Integrity,
                    "query_v2_result_type_mismatch",
                    "value column evidence is not a provider value concept",
                ));
            }
            reject_unexpected_provider_concept_fields(concept, column.variable(), category)?;
            if concept.get("label").is_some_and(|label| !label.is_string()) {
                return Err(failure(
                    DiagnosticCategory::InvalidContract,
                    "query_v2_result_concept_malformed",
                    "provider value concept carries an invalid label",
                ));
            }
            let actual =
                crate::migration_assertion::string_field(concept, "value_type", column.variable())?;
            if crate::migration_assertion::provider_value_type(actual) != Some(expected) {
                return Err(failure(
                    DiagnosticCategory::Integrity,
                    "query_v2_result_type_mismatch",
                    "provider value type differs from the validated scalar domain",
                ));
            }
            let value = crate::migration_assertion::parse_provider_value(
                concept.get("value").ok_or_else(|| {
                    failure(
                        DiagnosticCategory::InvalidContract,
                        "query_v2_result_concept_malformed",
                        "value concept carries no value",
                    )
                })?,
                expected,
            )?;
            values.push(QueryRowValue::Value { value });
            continue;
        }
        let category =
            crate::migration_assertion::string_field(concept, "category", column.variable())?;
        reject_unexpected_provider_concept_fields(concept, column.variable(), category)?;
        let label = crate::migration_assertion::string_field(concept, "label", column.variable())?;
        let kind =
            crate::migration_assertion::provider_concept_kind(category).ok_or_else(|| {
                failure(
                    DiagnosticCategory::Integrity,
                    "query_v2_result_type_mismatch",
                    "provider concept category is outside the validated domain",
                )
            })?;
        let type_id = TypeId::new(kind, label).map_err(|_| {
            failure(
                DiagnosticCategory::InvalidContract,
                "query_v2_result_concept_malformed",
                "provider concept label is not a canonical type identity",
            )
        })?;
        if !domain.type_ids().contains(&type_id) {
            return Err(failure(
                DiagnosticCategory::Integrity,
                "query_v2_result_type_mismatch",
                "provider concept type is outside the validated domain",
            ));
        }
        values.push(match (kind, domain.value_type()) {
            (TypeKind::Entity | TypeKind::Relation, None) => {
                let iid =
                    crate::migration_assertion::string_field(concept, "iid", column.variable())?;
                if !type_bridge_contract::id::is_canonical_thing_iid(iid) {
                    return Err(failure(
                        DiagnosticCategory::InvalidContract,
                        "query_v2_result_concept_malformed",
                        if iid.is_empty() {
                            "thing concept carries an empty instance identity"
                        } else {
                            "thing concept carries a malformed instance identity"
                        },
                    ));
                }
                QueryRowValue::Thing {
                    type_id,
                    iid: iid.to_owned(),
                }
            }
            (TypeKind::Attribute, Some(expected)) => {
                if let Some(iid) = concept.get("iid") {
                    let Some(iid) = iid.as_str() else {
                        return Err(failure(
                            DiagnosticCategory::InvalidContract,
                            "query_v2_result_concept_malformed",
                            "attribute concept carries an invalid instance identity",
                        ));
                    };
                    if !type_bridge_contract::id::is_canonical_thing_iid(iid) {
                        return Err(failure(
                            DiagnosticCategory::InvalidContract,
                            "query_v2_result_concept_malformed",
                            if iid.is_empty() {
                                "attribute concept carries an empty instance identity"
                            } else {
                                "attribute concept carries a malformed instance identity"
                            },
                        ));
                    }
                }
                let actual = crate::migration_assertion::string_field(
                    concept,
                    "value_type",
                    column.variable(),
                )?;
                if crate::migration_assertion::provider_value_type(actual) != Some(expected) {
                    return Err(failure(
                        DiagnosticCategory::Integrity,
                        "query_v2_result_type_mismatch",
                        "provider value type differs from the validated scalar domain",
                    ));
                }
                let value = crate::migration_assertion::parse_provider_value(
                    concept.get("value").ok_or_else(|| {
                        failure(
                            DiagnosticCategory::InvalidContract,
                            "query_v2_result_concept_malformed",
                            "attribute concept carries no value",
                        )
                    })?,
                    expected,
                )?;
                QueryRowValue::Attribute { type_id, value }
            }
            _ => {
                return Err(failure(
                    DiagnosticCategory::Integrity,
                    "query_v2_result_type_mismatch",
                    "provider concept shape disagrees with the validated domain",
                ));
            }
        });
    }
    Ok(values)
}

fn reject_unexpected_provider_concept_fields(
    concept: &serde_json::Map<String, serde_json::Value>,
    binding: &type_bridge_contract::migration_assertion::QueryVariable,
    category: &str,
) -> Result<(), Diagnostic> {
    let allowed: &[&str] = match category {
        "value" | "Value" => &["category", "label", "value", "value_type"],
        "entity" | "Entity" | "relation" | "Relation" => &["category", "iid", "label"],
        "attribute" | "Attribute" => &["category", "iid", "label", "value", "value_type"],
        // The caller reports an unknown category as a domain mismatch. Keep
        // that stable diagnostic instead of letting an incidental field win.
        _ => return Ok(()),
    };
    let mut unexpected = concept
        .keys()
        .filter(|field| !allowed.contains(&field.as_str()))
        .cloned()
        .collect::<Vec<_>>();
    unexpected.sort();
    if unexpected.is_empty() {
        return Ok(());
    }
    Err(failure(
        DiagnosticCategory::InvalidContract,
        "query_v2_result_concept_malformed",
        "provider concept evidence contains unexpected typed fields",
    )
    .with_detail("binding", binding.as_str())
    .with_detail("unexpected_fields", unexpected))
}

/// Return the visible variable names after select and reduce stages.
fn visible_variables(validated: &ValidatedQuery) -> Vec<&str> {
    let plan = validated.plan();
    // A reduce stage replaces the whole row environment with its group
    // keys and assigned results, superseding any earlier select.
    for stage in plan.pipeline() {
        if let ReadStage::Reduce {
            assignments,
            groups,
        } = stage
        {
            return groups
                .iter()
                .copied()
                .chain(assignments.iter().map(|assignment| assignment.assigned()))
                .filter_map(|binding| {
                    plan.bindings()
                        .get(usize::from(binding.get()))
                        .map(|binding| binding.variable().as_str())
                })
                .collect();
        }
    }
    for stage in plan.pipeline() {
        if let ReadStage::Select { bindings } = stage {
            return bindings
                .iter()
                .filter_map(|binding| {
                    plan.bindings()
                        .get(usize::from(binding.get()))
                        .map(|binding| binding.variable().as_str())
                })
                .collect();
        }
    }
    // No Select or Reduce: the environment is the validator-derived root
    // visibility, which lowering also projects explicitly whenever a
    // negation witness narrows it below the declared binding set.
    validated
        .root_visibility()
        .iter()
        .filter_map(|binding| {
            plan.bindings()
                .get(usize::from(binding.get()))
                .map(|binding| binding.variable().as_str())
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use type_bridge_contract::capability::CapabilitySet;
    use type_bridge_contract::codec::FormatVersion;
    use type_bridge_contract::fingerprint::SemanticProfileId;
    use type_bridge_contract::id::{AttributeId, RoleId, TypeId, TypeKind};
    use type_bridge_contract::limits::StructuralLimits;
    use type_bridge_contract::managed_scope::ManagedScopeId;
    use type_bridge_contract::migration_assertion::{
        AssertionBinding, QueryVariable, ValueComparator,
    };
    use type_bridge_contract::query_plan::{
        DocumentField, DocumentSource, InputColumn, InputColumnId, OrderDirection, OrderTerm,
        QueryOutput as PlanOutput,
    };
    use type_bridge_contract::schema::{
        AnnotationFact, AnnotationFactId, AnnotationKindId, AnnotationSubjectId, DeclaredSchema,
        DocumentId, OwnsFact, OwnsFactId, PlaysFact, PlaysFactId, RelatesFact, RelatesFactId,
        SchemaAnnotationValue, SchemaFact, SourceSpan, SourcedSchemaFact, SubFact, SubFactId,
        TypeFact, ValueFact, ValueFactId,
    };
    use type_bridge_contract::value::{CanonicalString, CanonicalValue, ValueTypeTag};
    use type_bridge_query::{MigrationAssertionValidationContext, validate_query_plan};
    use type_bridge_schema::{ManagedDeltaContext, managed_schema_state, resolve};

    use super::*;
    use crate::migration_assertion::AssertionProviderCall;
    use crate::session::backend::{
        AnswerCancellation, AnswerConsumer, AnswerItem, BoundedAnswerLimits, BoundedAnswerReader,
        BoundedAnswerStats, BoxFuture, QueryV2AnswerLimits,
    };

    struct ScriptedProvider {
        documents: bool,
        rows: Vec<serde_json::Value>,
    }

    impl AssertionProviderCall for ScriptedProvider {
        fn query_bounded<'a>(
            &'a mut self,
            _typeql: &'a str,
            limits: BoundedAnswerLimits,
            consumer: &'a mut dyn AnswerConsumer,
        ) -> BoxFuture<'a, Result<BoundedAnswerStats, crate::error::OrmError>> {
            Box::pin(async move {
                let mut processed = 0u64;
                for row in &self.rows {
                    if processed >= limits.max_items {
                        break;
                    }
                    processed += 1;
                    let answer = if self.documents {
                        AnswerItem::Document(row.clone())
                    } else {
                        AnswerItem::Row(row.clone())
                    };
                    if consumer.accept(answer)? == crate::session::backend::AnswerControl::Stop {
                        break;
                    }
                }
                Ok(BoundedAnswerStats {
                    processed_items: processed,
                    response_bytes: 0,
                    stopped_early: false,
                })
            })
        }
    }

    struct DrainProbe {
        documents: bool,
        rows: Vec<serde_json::Value>,
        observed_typeql: String,
        observed_limits: Option<(u64, u64, u64)>,
        controls: Vec<crate::session::backend::AnswerControl>,
        forge_early_stop: bool,
        fail_after_rows: bool,
    }

    impl DrainProbe {
        fn rows(rows: Vec<serde_json::Value>) -> Self {
            Self {
                documents: false,
                rows,
                observed_typeql: String::new(),
                observed_limits: None,
                controls: Vec::new(),
                forge_early_stop: false,
                fail_after_rows: false,
            }
        }

        fn documents(rows: Vec<serde_json::Value>) -> Self {
            Self {
                documents: true,
                ..Self::rows(rows)
            }
        }
    }

    impl AssertionProviderCall for DrainProbe {
        fn query_bounded<'a>(
            &'a mut self,
            _typeql: &'a str,
            _limits: BoundedAnswerLimits,
            _consumer: &'a mut dyn AnswerConsumer,
        ) -> BoxFuture<'a, Result<BoundedAnswerStats, crate::error::OrmError>> {
            panic!("V2 execution must use the additive V2 provider seam")
        }

        fn query_v2_bounded<'a>(
            &'a mut self,
            typeql: &'a str,
            limits: QueryV2AnswerLimits,
            consumer: &'a mut dyn AnswerConsumer,
        ) -> BoxFuture<'a, Result<BoundedAnswerStats, crate::error::OrmError>> {
            Box::pin(async move {
                self.observed_typeql = typeql.to_owned();
                self.observed_limits = Some((
                    limits.answer.max_items,
                    limits.answer.max_bytes,
                    limits.max_collection_members,
                ));
                let mut processed_items = 0_u64;
                let mut response_bytes = 0_u64;
                let mut stopped_early = self.forge_early_stop;
                for row in &self.rows {
                    processed_items += 1;
                    response_bytes +=
                        u64::try_from(serde_json::to_vec(row).expect("JSON value encodes").len())
                            .expect("encoded test value length fits u64");
                    let item = if self.documents {
                        AnswerItem::Document(row.clone())
                    } else {
                        AnswerItem::Row(row.clone())
                    };
                    let control = consumer.accept(item)?;
                    self.controls.push(control);
                    stopped_early |= control == crate::session::backend::AnswerControl::Stop;
                }
                if self.fail_after_rows {
                    return Err(crate::error::OrmError::Transaction(
                        "later drain failure".into(),
                    ));
                }
                Ok(BoundedAnswerStats {
                    processed_items,
                    response_bytes,
                    stopped_early,
                })
            })
        }
    }

    struct RawLimitProbe {
        row: serde_json::Value,
        observed_max_bytes: Option<u64>,
    }

    impl AssertionProviderCall for RawLimitProbe {
        fn query_bounded<'a>(
            &'a mut self,
            _typeql: &'a str,
            _limits: BoundedAnswerLimits,
            _consumer: &'a mut dyn AnswerConsumer,
        ) -> BoxFuture<'a, Result<BoundedAnswerStats, crate::error::OrmError>> {
            panic!("V2 execution must use the additive V2 provider seam")
        }

        fn query_v2_bounded<'a>(
            &'a mut self,
            _typeql: &'a str,
            limits: QueryV2AnswerLimits,
            consumer: &'a mut dyn AnswerConsumer,
        ) -> BoxFuture<'a, Result<BoundedAnswerStats, crate::error::OrmError>> {
            Box::pin(async move {
                self.observed_max_bytes = Some(limits.answer.max_bytes);
                let mut reader = BoundedAnswerReader::new(limits.answer);
                reader.accept(AnswerItem::Row(self.row.clone()), consumer)?;
                Ok(reader.stats())
            })
        }
    }

    fn binding_id(id: u16) -> BindingId {
        BindingId::new(id).expect("binding id")
    }

    fn fixture_with_output(output: PlanOutput) -> (ValidatedQuery, QueryPlan) {
        fixture_with_output_and_tail(output, Vec::new())
    }

    fn fixture_with_output_and_tail(
        output: PlanOutput,
        tail: Vec<ReadStage>,
    ) -> (ValidatedQuery, QueryPlan) {
        let person = TypeId::new(TypeKind::Entity, "person").expect("type");
        let name = AttributeId::new("name").expect("attribute");
        let facts = vec![
            SchemaFact::Type(TypeFact::new(person.clone()).expect("type fact")),
            SchemaFact::Type(
                TypeFact::new(TypeId::new(TypeKind::Attribute, "name").expect("type"))
                    .expect("type fact"),
            ),
            SchemaFact::Value(ValueFact::new(
                ValueFactId::new(name.clone()),
                ValueTypeTag::String,
            )),
            SchemaFact::Owns(OwnsFact::new(
                OwnsFactId::new(person.clone(), name.clone()).expect("owns id"),
            )),
            SchemaFact::Annotation(
                AnnotationFact::new(
                    AnnotationFactId::new(
                        AnnotationSubjectId::Owns(
                            OwnsFactId::new(person.clone(), name).expect("owns id"),
                        ),
                        AnnotationKindId::Unique,
                    ),
                    SchemaAnnotationValue::Presence,
                )
                .expect("unique annotation"),
            ),
        ];
        let sourced = facts.into_iter().enumerate().map(|(index, fact)| {
            let byte = u64::try_from(index).expect("byte");
            let line = u32::try_from(index + 1).expect("line");
            SourcedSchemaFact::new(
                fact,
                SourceSpan::new(
                    DocumentId::new("query-v2-executor-fixture").expect("document"),
                    byte,
                    byte + 1,
                    line,
                    1,
                    line,
                    2,
                )
                .expect("span"),
            )
        });
        let declared = DeclaredSchema::from_facts(FormatVersion::V1, CapabilitySet::new(), sourced)
            .expect("declared schema");
        let profile = SemanticProfileId::new("typedb-3.12.1/v1").expect("profile");
        let context = ManagedDeltaContext::new(
            ManagedScopeId::new("query-v2-executor-scope").expect("scope"),
            profile.clone(),
            CapabilitySet::new(),
        );
        let managed = managed_schema_state(&declared, &context).expect("managed state");
        let resolved = resolve(&declared, &profile).expect("resolved schema");

        let mut pipeline = vec![ReadStage::Match {
            patterns: vec![
                QueryPattern::Isa {
                    binding: binding_id(0),
                    include_subtypes: false,
                    type_id: TypeId::new(TypeKind::Entity, "person").expect("type"),
                },
                QueryPattern::Has {
                    attribute: binding_id(1),
                    attribute_id: AttributeId::new("name").expect("attribute"),
                    owner: binding_id(0),
                },
                QueryPattern::Value {
                    comparator: ValueComparator::GreaterOrEqual,
                    left: QueryOperand::Binding {
                        binding: binding_id(1),
                    },
                    right: QueryOperand::Input {
                        column: InputColumnId::new(0),
                    },
                },
            ],
        }];
        pipeline.extend(tail);
        let plan = QueryPlan::new(
            vec![
                AssertionBinding::new(
                    binding_id(0),
                    QueryVariable::new("person").expect("variable"),
                ),
                AssertionBinding::new(binding_id(1), QueryVariable::new("name").expect("variable")),
            ],
            vec![InputColumn::new(
                InputColumnId::new(0),
                QueryVariable::new("minimum_name").expect("input name"),
                ValueTypeTag::String,
                false,
            )],
            pipeline,
            output,
            managed.managed_semantic_schema().clone(),
        )
        .expect("query plan");
        let validation_context = MigrationAssertionValidationContext::new(&resolved, &managed);
        let validated =
            validate_query_plan(&plan, &validation_context, StructuralLimits::CANONICAL)
                .expect("validated query");
        (validated, plan)
    }

    fn fixture() -> (ValidatedQuery, QueryPlan) {
        fixture_with_output(PlanOutput::Rows {
            columns: vec![binding_id(0), binding_id(1)],
        })
    }

    fn document_fixture() -> (ValidatedQuery, QueryPlan) {
        fixture_with_output(PlanOutput::Documents {
            fields: vec![DocumentField::new(
                QueryVariable::new("names").expect("document key"),
                DocumentSource::AttributeList {
                    attribute: AttributeId::new("name").expect("attribute"),
                    owner: binding_id(0),
                },
            )],
        })
    }

    fn invocation(plan: &QueryPlan, operation: QueryOperation) -> QueryInvocation {
        QueryInvocation::new(
            plan,
            operation,
            vec![InputRow::new(vec![Some(CanonicalValue::String(
                CanonicalString::new("a").expect("canonical string"),
            ))])],
        )
        .expect("invocation")
    }

    fn person_row(iid: &str, name: &str) -> serde_json::Value {
        json!({
            "person": {"category": "entity", "label": "person", "iid": iid},
            "name": {
                "category": "attribute",
                "label": "name",
                "value": name,
                "value_type": "string"
            },
        })
    }

    #[test]
    fn result_rows_allow_input_echoes_only_for_actual_given_lowerings() {
        let (validated, _) = fixture();
        let OutputSchema::Rows(schema) = validated.output_schema() else {
            panic!("fixture has row output")
        };
        let mut row = person_row("0x1", "ada");
        row.as_object_mut()
            .expect("row object")
            .insert("minimum_name".into(), serde_json::json!("provider-forgery"));

        let error = validate_result_row(&row, &validated, schema, false)
            .expect_err("inline lowering cannot produce an echoed input column");
        assert_eq!(error.code().as_str(), "query_v2_result_column_mismatch");

        validate_result_row(&row, &validated, schema, true)
            .expect("given lowerings may echo their driver-bound input columns");
    }

    fn limits() -> QueryV2AnswerLimits {
        QueryV2AnswerLimits {
            answer: BoundedAnswerLimits {
                max_items: 100,
                max_bytes: 1 << 20,
                deadline: None,
                cancellation: AnswerCancellation::default(),
            },
            max_collection_members: 1 << 16,
        }
    }

    #[tokio::test]
    async fn schema_refined_zero_branches_are_rejected_before_provider_call() {
        let node = TypeId::new(TypeKind::Entity, "node").expect("node");
        let child = TypeId::new(TypeKind::Entity, "node-child").expect("child");
        let edge = TypeId::new(TypeKind::Relation, "directed-edge").expect("edge");
        let origin = RoleId::new(edge.label().as_str(), "origin").expect("origin");
        let destination = RoleId::new(edge.label().as_str(), "destination").expect("destination");
        let facts = vec![
            SchemaFact::Type(TypeFact::new(node.clone()).expect("node type")),
            SchemaFact::Type(TypeFact::new(child.clone()).expect("child type")),
            SchemaFact::Sub(SubFact::new(
                SubFactId::new(child, node.clone()).expect("node subtype"),
            )),
            SchemaFact::Type(TypeFact::new(edge.clone()).expect("edge type")),
            SchemaFact::Relates(
                RelatesFact::new(
                    RelatesFactId::new(edge.clone(), origin.clone()).expect("origin role"),
                    None,
                )
                .expect("origin relates"),
            ),
            SchemaFact::Relates(
                RelatesFact::new(
                    RelatesFactId::new(edge.clone(), destination.clone())
                        .expect("destination role"),
                    None,
                )
                .expect("destination relates"),
            ),
            SchemaFact::Plays(PlaysFact::new(
                PlaysFactId::new(node.clone(), origin.clone()).expect("origin plays"),
            )),
            SchemaFact::Plays(PlaysFact::new(
                PlaysFactId::new(node, destination.clone()).expect("destination plays"),
            )),
        ];
        let sourced = facts.into_iter().enumerate().map(|(index, fact)| {
            let byte = u64::try_from(index).expect("byte");
            let line = u32::try_from(index + 1).expect("line");
            SourcedSchemaFact::new(
                fact,
                SourceSpan::new(
                    DocumentId::new("query-v2-reach-limit").expect("document"),
                    byte,
                    byte + 1,
                    line,
                    1,
                    line,
                    2,
                )
                .expect("span"),
            )
        });
        let declared = DeclaredSchema::from_facts(FormatVersion::V1, CapabilitySet::new(), sourced)
            .expect("declared schema");
        let profile = SemanticProfileId::new("typedb-3.12.1/v1").expect("profile");
        let managed = managed_schema_state(
            &declared,
            &ManagedDeltaContext::new(
                ManagedScopeId::new("query-v2-reach-limit").expect("scope"),
                profile.clone(),
                CapabilitySet::new(),
            ),
        )
        .expect("managed state");
        let resolved = resolve(&declared, &profile).expect("resolved schema");
        let plan = QueryPlan::new_v2(
            vec![
                AssertionBinding::new(binding_id(0), QueryVariable::new("source").expect("source")),
                AssertionBinding::new(binding_id(1), QueryVariable::new("target").expect("target")),
            ],
            Vec::new(),
            vec![ReadStage::Match {
                patterns: vec![QueryPattern::Reachable {
                    min_depth: 0,
                    max_depth: 0,
                    relation: edge,
                    role_from: origin,
                    role_to: destination,
                    source: binding_id(0),
                    target: binding_id(1),
                }],
            }],
            PlanOutput::Rows {
                columns: vec![binding_id(0), binding_id(1)],
            },
            managed.managed_semantic_schema().clone(),
        )
        .expect("zero-hop plan");
        let structural_limits = StructuralLimits {
            predicate_nodes: 1,
            ..StructuralLimits::CANONICAL
        };
        let validated = validate_query_plan(
            &plan,
            &MigrationAssertionValidationContext::new(&resolved, &managed),
            structural_limits,
        )
        .expect("schema-valid zero-hop plan");
        assert_eq!(
            validated
                .binding_domain(&binding_id(0))
                .expect("source domain")
                .type_ids()
                .len(),
            2
        );

        let invocation =
            QueryInvocation::new(&plan, QueryOperation::Rows, Vec::new()).expect("invocation");
        let canonical_validated = validate_query_plan(
            &plan,
            &MigrationAssertionValidationContext::new(&resolved, &managed),
            StructuralLimits::CANONICAL,
        )
        .expect("canonical zero-hop plan");
        let lowered = lower_validated_query(&canonical_validated, &invocation)
            .expect("schema-refined zero-hop lowering");
        assert!(
            lowered
                .typeql()
                .contains("$R0z0 isa! node; $source is $R0z0; $target is $R0z0;")
        );
        assert!(
            lowered
                .typeql()
                .contains("$R0z1 isa! node-child; $source is $R0z1; $target is $R0z1;")
        );
        assert!(!lowered.typeql().contains("$R0z isa!"));

        let mut provider = DrainProbe::rows(Vec::new());
        let error = execute_with_provider(&mut provider, &validated, &invocation, limits())
            .await
            .expect_err("two emitted identity branches exceed the one-node validation budget");
        let QueryV2ExecutionError::Validation(diagnostic) = error else {
            panic!("lowering must reject before the provider seam");
        };
        assert_eq!(
            diagnostic.code().as_str(),
            "query_v2_reachable_expansion_limit"
        );
        assert_eq!(diagnostic.category(), DiagnosticCategory::ResourceLimit);
        assert!(provider.observed_typeql.is_empty());
    }

    #[test]
    fn provider_resource_mapping_is_specific_and_other_errors_are_redacted() {
        let cancelled: crate::error::OrmError = crate::match_request::MatchError::new(
            crate::match_request::MatchErrorCategory::Cancelled,
            "provider_cancelled",
            "provider-private detail",
        )
        .into();
        let diagnostic = provider_diagnostic(
            &cancelled,
            "query_remote_provider_failed",
            "the executor provider call failed",
        );
        assert_eq!(diagnostic.category(), DiagnosticCategory::Cancelled);
        assert_eq!(diagnostic.code().as_str(), "provider_cancelled");
        assert!(!diagnostic.message().contains("provider-private"));

        for code in [
            "transaction_deadline_exceeded",
            "processed_item_limit",
            "response_byte_limit",
            "processed_item_counter_overflow",
            "answer_byte_counter_overflow",
            "query_v2_document_member_limit",
            "collected_concept_limit",
            "reduction_group_limit",
            "reduction_overflow",
        ] {
            let provider_error: crate::error::OrmError = crate::match_request::MatchError::new(
                crate::match_request::MatchErrorCategory::ResourceLimit,
                code,
                "provider-private detail",
            )
            .into();
            let diagnostic = provider_diagnostic(
                &provider_error,
                "query_remote_provider_failed",
                "the executor provider call failed",
            );
            assert_eq!(diagnostic.category(), DiagnosticCategory::ResourceLimit);
            assert_eq!(diagnostic.code().as_str(), code);
            assert!(!diagnostic.message().contains("provider-private"));
        }

        let unknown_resource: crate::error::OrmError = crate::match_request::MatchError::new(
            crate::match_request::MatchErrorCategory::ResourceLimit,
            "future_provider_resource_code",
            "secret resource detail",
        )
        .into();
        for provider_error in [
            unknown_resource,
            crate::error::OrmError::Transaction("secret transaction detail".into()),
        ] {
            let diagnostic = provider_diagnostic(
                &provider_error,
                "query_remote_provider_failed",
                "the executor provider call failed",
            );
            assert_eq!(diagnostic.category(), DiagnosticCategory::Integrity);
            assert_eq!(diagnostic.code().as_str(), "query_remote_provider_failed");
            assert_eq!(diagnostic.message(), "the executor provider call failed");
        }
    }

    #[tokio::test]
    async fn rows_count_and_exists_share_one_validated_stream() {
        let (validated, plan) = fixture();
        let mut provider = ScriptedProvider {
            documents: false,
            rows: vec![person_row("0x1", "ada"), person_row("0x2", "grace")],
        };
        let outcome = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            limits(),
        )
        .await
        .expect("rows outcome");
        let QueryV2Outcome::Rows(rows) = outcome else {
            panic!("rows operation returns rows");
        };
        assert_eq!(rows.len(), 2);
        let QueryRowValue::Thing { type_id, iid } = &rows[0].values()[0] else {
            panic!("first column is the person reference");
        };
        assert_eq!(type_id.label().as_str(), "person");
        assert_eq!(iid, "0x1");
        let QueryRowValue::Attribute { value, .. } = &rows[1].values()[1] else {
            panic!("second column is the name attribute");
        };
        assert_eq!(
            value,
            &CanonicalValue::String(CanonicalString::new("grace").expect("canonical string")),
        );

        let outcome = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Count),
            limits(),
        )
        .await
        .expect("count outcome");
        assert_eq!(outcome, QueryV2Outcome::Count(2));

        let outcome = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Exists),
            limits(),
        )
        .await
        .expect("exists outcome");
        assert_eq!(outcome, QueryV2Outcome::Exists(true));

        let mut empty = ScriptedProvider {
            documents: false,
            rows: Vec::new(),
        };
        let outcome = execute_with_provider(
            &mut empty,
            &validated,
            &invocation(&plan, QueryOperation::Exists),
            limits(),
        )
        .await
        .expect("empty exists outcome");
        assert_eq!(outcome, QueryV2Outcome::Exists(false));
    }

    #[tokio::test]
    async fn row_item_budget_uses_n_plus_one_sentinel_and_drains_to_eof() {
        let (validated, plan) = fixture();
        let mut provider =
            DrainProbe::rows(vec![person_row("0x1", "ada"), person_row("0x2", "grace")]);
        let mut bounded = limits();
        bounded.answer.max_items = 1;
        let expected_max_bytes = bounded.answer.max_bytes;
        let expected_max_collection_members = bounded.max_collection_members;

        let error = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            bounded,
        )
        .await
        .expect_err("the sentinel row proves the semantic item budget was exceeded");
        let QueryV2ExecutionError::Provider(crate::error::OrmError::Match(error)) = error else {
            panic!("the released provider resource-error surface is preserved");
        };
        assert_eq!(
            error.category(),
            crate::match_request::MatchErrorCategory::ResourceLimit
        );
        assert_eq!(error.code().as_str(), "processed_item_limit");
        assert_eq!(
            error.path().segments(),
            &[crate::match_request::MatchErrorPathSegment::ProviderEvidence]
        );
        assert!(provider.observed_typeql.ends_with("limit 2;\n"));
        assert_eq!(
            provider.observed_limits,
            Some((2, expected_max_bytes, expected_max_collection_members,))
        );
        assert_eq!(
            provider.controls,
            vec![
                crate::session::backend::AnswerControl::Continue,
                crate::session::backend::AnswerControl::Continue,
            ]
        );
    }

    #[tokio::test]
    async fn deferred_budget_failure_precedes_a_later_provider_drain_failure() {
        let (validated, plan) = fixture();
        let mut provider =
            DrainProbe::rows(vec![person_row("0x1", "ada"), person_row("0x2", "grace")]);
        provider.fail_after_rows = true;
        let mut bounded = limits();
        bounded.answer.max_items = 1;

        let error = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            bounded,
        )
        .await
        .expect_err("the first semantic failure wins over a later drain failure");
        let QueryV2ExecutionError::Provider(crate::error::OrmError::Match(error)) = error else {
            panic!("the item-budget resource error must not be overwritten");
        };
        assert_eq!(error.code().as_str(), "processed_item_limit");
        assert_eq!(
            provider.controls,
            vec![
                crate::session::backend::AnswerControl::Continue,
                crate::session::backend::AnswerControl::Continue,
            ]
        );
    }

    #[tokio::test]
    async fn document_byte_budget_drains_to_eof_before_preserving_resource_error() {
        let (validated, plan) = document_fixture();
        let document = json!({"names": ["ada"]});
        let encoded_bytes = u64::try_from(
            serde_json::to_vec(&document)
                .expect("JSON value encodes")
                .len(),
        )
        .expect("encoded test value length fits u64");
        let mut provider = DrainProbe::documents(vec![document]);
        let mut bounded = limits();
        bounded.answer.max_bytes = encoded_bytes - 1;
        bounded.max_collection_members = 1;

        let error = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            bounded,
        )
        .await
        .expect_err("the first document exceeds the semantic byte budget");
        let QueryV2ExecutionError::Provider(crate::error::OrmError::Match(error)) = error else {
            panic!("the released provider resource-error surface is preserved");
        };
        assert_eq!(error.code().as_str(), "response_byte_limit");
        assert!(provider.observed_typeql.contains("limit 101;\nfetch {\n"));
        assert!(provider.observed_typeql.contains("        limit 2;\n"));
        assert_eq!(provider.observed_limits, Some((101, encoded_bytes - 1, 1)));
        assert_eq!(
            provider.controls,
            vec![crate::session::backend::AnswerControl::Continue]
        );
    }

    #[tokio::test]
    async fn raw_provider_bytes_stay_finite_when_projected_output_would_be_small() {
        let (validated, plan) = fixture();
        let mut row = person_row("0x1", "ada");
        row.as_object_mut().expect("row object").insert(
            "ignored-provider-overhead".to_owned(),
            json!("x".repeat(4096)),
        );
        let mut provider = RawLimitProbe {
            row,
            observed_max_bytes: None,
        };
        let mut bounded = limits();
        bounded.answer.max_bytes = 512;

        let error = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            bounded,
        )
        .await
        .expect_err("raw ignored provider data must still meet the finite provider ceiling");
        let QueryV2ExecutionError::Provider(crate::error::OrmError::Match(error)) = error else {
            panic!("provider byte ceiling retains the stable resource surface");
        };
        assert_eq!(error.code().as_str(), "response_byte_limit");
        assert_eq!(provider.observed_max_bytes, Some(512));
    }

    #[test]
    fn exists_lowering_owns_a_final_semantic_limit_before_document_output() {
        let (validated, plan) = fixture();
        let lowered = lower_validated_query(&validated, &invocation(&plan, QueryOperation::Exists))
            .expect("exists lowering");
        assert!(lowered.typeql().ends_with("limit 1;\n"));

        let (limited, limited_plan) = fixture_with_output_and_tail(
            PlanOutput::Rows {
                columns: vec![binding_id(0), binding_id(1)],
            },
            vec![
                ReadStage::Sort {
                    terms: vec![OrderTerm::new(binding_id(1), OrderDirection::Ascending)],
                },
                ReadStage::Limit { rows: 7 },
            ],
        );
        let lowered =
            lower_validated_query(&limited, &invocation(&limited_plan, QueryOperation::Exists))
                .expect("limited exists lowering");
        assert!(lowered.typeql().ends_with("limit 1;\n"));

        let (zero, zero_plan) = fixture_with_output_and_tail(
            PlanOutput::Rows {
                columns: vec![binding_id(0), binding_id(1)],
            },
            vec![
                ReadStage::Sort {
                    terms: vec![OrderTerm::new(binding_id(1), OrderDirection::Ascending)],
                },
                ReadStage::Limit { rows: 0 },
            ],
        );
        let lowered = lower_validated_query(&zero, &invocation(&zero_plan, QueryOperation::Exists))
            .expect("zero-limit exists lowering");
        assert!(lowered.typeql().ends_with("limit 0;\n"));

        let (documents, document_plan) = document_fixture();
        let lowered = lower_validated_query(
            &documents,
            &invocation(&document_plan, QueryOperation::Exists),
        )
        .expect("document exists lowering");
        assert!(lowered.typeql().contains("limit 1;\nfetch {\n"));
    }

    #[test]
    fn execution_item_sentinel_is_additive_and_clamps_existing_limits() {
        let (validated, plan) = fixture();
        let public = lower_validated_query(&validated, &invocation(&plan, QueryOperation::Rows))
            .expect("public lowering");
        assert!(!public.typeql().contains("\nlimit "));

        let bounded = lower_validated_query_with_execution_limits(
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            None,
            Some(2),
        )
        .expect("execution lowering");
        assert!(bounded.typeql().ends_with("limit 2;\n"));

        let (limited, limited_plan) = fixture_with_output_and_tail(
            PlanOutput::Rows {
                columns: vec![binding_id(0), binding_id(1)],
            },
            vec![
                ReadStage::Sort {
                    terms: vec![OrderTerm::new(binding_id(1), OrderDirection::Ascending)],
                },
                ReadStage::Limit { rows: 7 },
            ],
        );
        let bounded = lower_validated_query_with_execution_limits(
            &limited,
            &invocation(&limited_plan, QueryOperation::Rows),
            None,
            Some(2),
        )
        .expect("clamped execution lowering");
        assert!(bounded.typeql().ends_with("limit 2;\n"));

        let (documents, document_plan) = document_fixture();
        let bounded = lower_validated_query_with_execution_limits(
            &documents,
            &invocation(&document_plan, QueryOperation::Rows),
            Some(3),
            Some(2),
        )
        .expect("bounded document lowering");
        assert!(bounded.typeql().contains("limit 2;\nfetch {\n"));
        assert!(bounded.typeql().contains("        limit 4;\n"));
    }

    #[tokio::test]
    async fn exists_continues_to_provider_eof_and_rejects_false_exhaustion_proof() {
        struct ExhaustionProbe {
            row: serde_json::Value,
            observed_typeql: String,
            control: Option<crate::session::backend::AnswerControl>,
            forge_early_stop: bool,
        }

        impl AssertionProviderCall for ExhaustionProbe {
            fn query_bounded<'a>(
                &'a mut self,
                typeql: &'a str,
                _limits: BoundedAnswerLimits,
                consumer: &'a mut dyn AnswerConsumer,
            ) -> BoxFuture<'a, Result<BoundedAnswerStats, crate::error::OrmError>> {
                Box::pin(async move {
                    self.observed_typeql = typeql.to_owned();
                    let control = consumer.accept(AnswerItem::Row(self.row.clone()))?;
                    self.control = Some(control);
                    Ok(BoundedAnswerStats {
                        processed_items: 1,
                        response_bytes: 0,
                        stopped_early: self.forge_early_stop
                            || control == crate::session::backend::AnswerControl::Stop,
                    })
                })
            }
        }

        let (validated, plan) = fixture();
        let invocation = invocation(&plan, QueryOperation::Exists);
        let mut provider = ExhaustionProbe {
            row: person_row("0x1", "ada"),
            observed_typeql: String::new(),
            control: None,
            forge_early_stop: false,
        };
        let outcome = execute_with_provider(&mut provider, &validated, &invocation, limits())
            .await
            .expect("bounded exists reaches EOF");
        assert_eq!(outcome, QueryV2Outcome::Exists(true));
        assert!(provider.observed_typeql.ends_with("limit 1;\n"));
        assert_eq!(
            provider.control,
            Some(crate::session::backend::AnswerControl::Continue)
        );

        provider.forge_early_stop = true;
        let error = execute_with_provider(&mut provider, &validated, &invocation, limits())
            .await
            .expect_err("early-stop stats cannot prove bounded existence");
        let QueryV2ExecutionError::Validation(diagnostic) = error else {
            panic!("false exhaustion proof is a validation failure")
        };
        assert_eq!(
            diagnostic.code().as_str(),
            "query_v2_provider_stream_not_exhausted"
        );
    }

    #[tokio::test]
    async fn zero_item_exists_distinguishes_empty_from_first_over_budget_row() {
        let (validated, plan) = fixture();
        let mut zero = limits();
        zero.answer.max_items = 0;

        let mut empty = DrainProbe::rows(Vec::new());
        let outcome = execute_with_provider(
            &mut empty,
            &validated,
            &invocation(&plan, QueryOperation::Exists),
            zero.clone(),
        )
        .await
        .expect("an empty result satisfies a zero-item budget");
        assert_eq!(outcome, QueryV2Outcome::Exists(false));
        assert!(empty.observed_typeql.ends_with("limit 1;\n"));

        let (limited, limited_plan) = fixture_with_output_and_tail(
            PlanOutput::Rows {
                columns: vec![binding_id(0), binding_id(1)],
            },
            vec![
                ReadStage::Sort {
                    terms: vec![OrderTerm::new(binding_id(1), OrderDirection::Ascending)],
                },
                ReadStage::Limit { rows: 0 },
            ],
        );
        let mut no_rows = DrainProbe::rows(Vec::new());
        let outcome = execute_with_provider(
            &mut no_rows,
            &limited,
            &invocation(&limited_plan, QueryOperation::Exists),
            zero.clone(),
        )
        .await
        .expect("an explicit zero limit proves false without an answer item");
        assert_eq!(outcome, QueryV2Outcome::Exists(false));
        assert!(no_rows.observed_typeql.ends_with("limit 0;\n"));

        let mut matching = DrainProbe::rows(vec![person_row("0x1", "ada")]);
        let error = execute_with_provider(
            &mut matching,
            &validated,
            &invocation(&plan, QueryOperation::Exists),
            zero,
        )
        .await
        .expect_err("the first matching row exceeds a zero-item budget");
        let QueryV2ExecutionError::Provider(crate::error::OrmError::Match(error)) = error else {
            panic!("the stable provider item-limit surface is preserved");
        };
        assert_eq!(error.code().as_str(), "processed_item_limit");
        assert_eq!(
            matching.controls,
            vec![crate::session::backend::AnswerControl::Continue]
        );
    }

    #[tokio::test]
    async fn malformed_row_evidence_records_first_failure_and_still_drains() {
        let (validated, plan) = fixture();
        let malformed = json!({
            "person": {"category": "entity", "label": "company", "iid": "0x1"},
            "name": {
                "category": "attribute",
                "label": "name",
                "value": "ada",
                "value_type": "string"
            },
        });
        let mut provider = DrainProbe::rows(vec![malformed, person_row("0x2", "grace")]);

        let error = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            limits(),
        )
        .await
        .expect_err("foreign evidence must fail after the finite stream is exhausted");
        let QueryV2ExecutionError::Validation(diagnostic) = error else {
            panic!("forged evidence remains a validation failure");
        };
        assert_eq!(diagnostic.code().as_str(), "query_v2_result_type_mismatch");
        assert_eq!(
            provider.controls,
            vec![
                crate::session::backend::AnswerControl::Continue,
                crate::session::backend::AnswerControl::Continue,
            ]
        );
    }

    #[test]
    fn local_row_evidence_rejects_noncanonical_thing_iids() {
        let (validated, _) = fixture();
        let OutputSchema::Rows(schema) = validated.output_schema() else {
            panic!("row fixture derives a row schema");
        };
        let oversized = format!(
            "0x{}",
            "a".repeat(type_bridge_contract::id::MAX_THING_IID_HEX_DIGITS + 1)
        );
        for malformed in ["0x1; delete $x;", oversized.as_str()] {
            let error =
                validate_result_row(&person_row(malformed, "ada"), &validated, schema, false)
                    .expect_err("malformed Thing IID cannot construct a local typed row");
            assert_eq!(error.code().as_str(), "query_v2_result_concept_malformed");
        }
    }

    #[test]
    fn provider_concept_field_allowlists_cover_every_category_variant() {
        let binding = QueryVariable::new("result").expect("binding");

        for category in ["entity", "Entity", "relation", "Relation"] {
            for (field, value) in [
                ("value", json!("smuggled")),
                ("value_type", json!("string")),
            ] {
                let mut concept = json!({
                    "category": category,
                    "iid": "0x01",
                    "label": "person"
                })
                .as_object()
                .expect("concept object")
                .clone();
                concept.insert(field.to_owned(), value);
                let error = reject_unexpected_provider_concept_fields(&concept, &binding, category)
                    .expect_err("thing concepts must reject scalar-only fields");
                assert_eq!(error.code().as_str(), "query_v2_result_concept_malformed");
            }
        }

        for category in ["value", "Value"] {
            let mut concept = json!({
                "category": category,
                "label": "string",
                "value": "ada",
                "value_type": "string"
            })
            .as_object()
            .expect("concept object")
            .clone();
            reject_unexpected_provider_concept_fields(&concept, &binding, category)
                .expect("the provider's value label is legitimate evidence");
            concept.insert("iid".to_owned(), json!("0x01"));
            let error = reject_unexpected_provider_concept_fields(&concept, &binding, category)
                .expect_err("value concepts must not smuggle thing identity");
            assert_eq!(error.code().as_str(), "query_v2_result_concept_malformed");
        }

        for category in ["attribute", "Attribute"] {
            let mut concept = json!({
                "category": category,
                "iid": "0x01",
                "label": "name",
                "value": "ada",
                "value_type": "string"
            })
            .as_object()
            .expect("concept object")
            .clone();
            reject_unexpected_provider_concept_fields(&concept, &binding, category)
                .expect("attribute identity is an optional provider field");
            concept.insert("entity_type".to_owned(), json!("person"));
            let error = reject_unexpected_provider_concept_fields(&concept, &binding, category)
                .expect_err("attributes must reject fields outside their exact allowlist");
            assert_eq!(error.code().as_str(), "query_v2_result_concept_malformed");
        }
    }

    #[test]
    fn local_row_validation_rejects_entity_scalar_fields_and_checks_attribute_iid() {
        let (validated, _) = fixture();
        let OutputSchema::Rows(schema) = validated.output_schema() else {
            panic!("row fixture derives a row schema");
        };

        for (field, value) in [
            ("value", json!("smuggled")),
            ("value_type", json!("string")),
        ] {
            let mut row = person_row("0x01", "ada");
            row["person"]
                .as_object_mut()
                .expect("person concept")
                .insert(field.to_owned(), value);
            let error = validate_result_row(&row, &validated, schema, false)
                .expect_err("entity scalar fields must fail closed");
            assert_eq!(error.code().as_str(), "query_v2_result_concept_malformed");
        }

        let mut identified_attribute = person_row("0x01", "ada");
        identified_attribute["name"]
            .as_object_mut()
            .expect("attribute concept")
            .insert("iid".to_owned(), json!("0x02"));
        validate_result_row(&identified_attribute, &validated, schema, false)
            .expect("a canonical optional attribute IID remains admissible");

        identified_attribute["name"]
            .as_object_mut()
            .expect("attribute concept")
            .insert("iid".to_owned(), json!("not-an-iid"));
        let error = validate_result_row(&identified_attribute, &validated, schema, false)
            .expect_err("a present attribute IID must itself be valid evidence");
        assert_eq!(error.code().as_str(), "query_v2_result_concept_malformed");
    }

    #[tokio::test]
    async fn every_operation_rejects_a_forged_early_stop_stat() {
        let (validated, plan) = fixture();
        let mut provider = DrainProbe::rows(vec![person_row("0x1", "ada")]);
        provider.forge_early_stop = true;

        let error = execute_with_provider(
            &mut provider,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            limits(),
        )
        .await
        .expect_err("an unterminated rows stream is not complete evidence");
        let QueryV2ExecutionError::Validation(diagnostic) = error else {
            panic!("false exhaustion proof is a validation failure");
        };
        assert_eq!(
            diagnostic.code().as_str(),
            "query_v2_provider_stream_not_exhausted"
        );
        assert_eq!(
            provider.controls,
            vec![crate::session::backend::AnswerControl::Continue]
        );
    }

    #[tokio::test]
    async fn forged_and_malformed_provider_rows_fail_closed() {
        let (validated, plan) = fixture();
        let mut forged = ScriptedProvider {
            documents: false,
            rows: vec![json!({
                "person": {"category": "entity", "label": "company", "iid": "0x1"},
                "name": {
                    "category": "attribute",
                    "label": "name",
                    "value": "ada",
                    "value_type": "string"
                },
            })],
        };
        let error = execute_with_provider(
            &mut forged,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            limits(),
        )
        .await
        .expect_err("a foreign concept type is not evidence");
        let QueryV2ExecutionError::Validation(diagnostic) = error else {
            panic!("forged evidence must surface as validation failure");
        };
        assert_eq!(diagnostic.code().as_str(), "query_v2_result_type_mismatch");

        let mut sparse = ScriptedProvider {
            documents: false,
            rows: vec![json!({
                "person": {"category": "entity", "label": "person", "iid": "0x1"},
            })],
        };
        let error = execute_with_provider(
            &mut sparse,
            &validated,
            &invocation(&plan, QueryOperation::Rows),
            limits(),
        )
        .await
        .expect_err("a sparse row is not evidence");
        let QueryV2ExecutionError::Validation(diagnostic) = error else {
            panic!("sparse evidence must surface as validation failure");
        };
        assert_eq!(
            diagnostic.code().as_str(),
            "query_v2_result_column_mismatch"
        );
    }

    #[tokio::test]
    async fn document_lists_are_rejected_before_member_materialization() {
        let (validated, plan) = document_fixture();
        let invocation = invocation(&plan, QueryOperation::Rows);
        let zero_limit =
            lower_validated_query_with_execution_limits(&validated, &invocation, Some(0), None)
                .expect("bounded document lowering");
        assert!(
            zero_limit
                .typeql()
                .contains("match $person has name $FtbDocumentValue0;\n        limit 1;\n        return { $FtbDocumentValue0 };")
        );
        let canonical_limit = lower_validated_query_with_execution_limits(
            &validated,
            &invocation,
            Some(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).expect("canonical limit")),
            None,
        )
        .expect("canonical document lowering");
        assert!(
            canonical_limit
                .typeql()
                .contains(&format!("limit {};", MAX_CANONICAL_COLLECTION_LEN + 1)),
        );

        let mut provider = DrainProbe::documents(vec![json!({"names": ["ada", "grace"]})]);
        provider.fail_after_rows = true;
        let mut bounded = limits();
        bounded.max_collection_members = 1;
        let error = execute_with_provider(&mut provider, &validated, &invocation, bounded)
            .await
            .expect_err("aggregate list members exceed the caller ceiling");
        let QueryV2ExecutionError::Provider(crate::error::OrmError::Match(error)) = error else {
            panic!("member budget rejection preserves the bounded-provider surface");
        };
        assert_eq!(
            error.category(),
            crate::match_request::MatchErrorCategory::ResourceLimit
        );
        assert_eq!(error.code().as_str(), "query_v2_document_member_limit");
        assert_eq!(
            error.message(),
            "document lists exceed the aggregate member ceiling"
        );
        assert_eq!(
            error.path().segments(),
            &[crate::match_request::MatchErrorPathSegment::ProviderEvidence]
        );
        assert_eq!(
            provider.controls,
            vec![crate::session::backend::AnswerControl::Continue]
        );
    }
}