type-bridge-contract 2.0.1

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

use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use serde::de::{DeserializeSeed, IgnoredAny, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize};

use crate::capability::{CapabilityId, CapabilitySet};
use crate::codec::{
    from_canonical_json_with_limits, to_canonical_json, to_canonical_json_with_limits,
};
use crate::diagnostic::{
    Diagnostic, DiagnosticCategory, DiagnosticCode, DiagnosticDetailValue, DiagnosticPath,
    DiagnosticPathSegment,
};
use crate::fingerprint::{
    CanonicalizationVersion, Fingerprint, FingerprintDigest, FingerprintDomain,
};
use crate::id::{AttributeId, RoleId, TypeId, TypeKind, is_canonical_thing_iid};
use crate::limits::{
    MAX_CANONICAL_COLLECTION_LEN, MAX_DIAGNOSTIC_BYTES, MAX_REMOTE_ENVELOPE_BYTES,
    MAX_SELECTED_SLOTS, REMOTE_ENVELOPE_CODEC_LIMITS, REMOTE_REQUEST_CODEC_LIMITS,
};
use crate::migration_assertion::BindingId;
use crate::query_plan::{
    CompatibilityValueV2, InputRow, QueryInvocation, QueryOperation, QueryOutput, QueryPlan,
    QueryPlanFingerprint, decode_query_plan,
};
use crate::query_remote::{
    DEFAULT_REMOTE_DEADLINE_MS, MAX_REMOTE_CLOCK_SKEW_MS, MAX_REMOTE_DEADLINE_MS,
    QUERY_REMOTE_REQUEST_FINGERPRINT_DOMAIN, RemoteCapabilities, RemoteCapabilitiesFingerprint,
    RemoteFieldValue, RemoteReplySigner, RemoteReplyVerifier, RemoteSigningPublicKey, RemoteValue,
};
use crate::value::CanonicalValue;

/// The exact wire discriminator for additive V2 remote requests.
pub const QUERY_REMOTE_REQUEST_FORMAT_V2: &str = "typebridge.query-remote-request/v2";
/// The exact wire discriminator for additive V2 remote responses.
pub const QUERY_REMOTE_RESPONSE_FORMAT_V2: &str = "typebridge.query-remote-response/v2";
/// The exact wire discriminator for additive V2 remote failures.
pub const QUERY_REMOTE_FAILURE_FORMAT_V2: &str = "typebridge.query-remote-failure/v2";
/// The distinct canonicalization identifier for V2 request fingerprints.
pub const QUERY_REMOTE_REQUEST_CANONICALIZATION_V2: &str = "typebridge.query-remote-request/v2";

/// V2-plan admission capability.
pub const CAP_QUERY_PLAN_V2: &str = "query.plan.v2";
/// V2 remote-envelope admission capability.
pub const CAP_QUERY_REMOTE_ENVELOPE_V2: &str = "query.remote.envelope-v2";
/// Complete structured remote-diagnostic admission capability.
pub const CAP_QUERY_REMOTE_STRUCTURED_DIAGNOSTIC: &str = "query.remote.structured-diagnostic";
/// Hydrated model-output admission capability.
pub const CAP_QUERY_OUTPUT_HYDRATED: &str = "query.output.hydrated";
/// Same-snapshot hydration admission capability.
pub const CAP_QUERY_SAME_SNAPSHOT_HYDRATION: &str = "query.execution.same-snapshot-hydration";

const NONCE_MIN_BYTES: usize = 16;
const NONCE_MAX_BYTES: usize = 128;
const FAILURE_FRAMING_ALLOWANCE_BYTES: usize = 1_024;

/// Return the exact base capabilities required by every V2 remote envelope.
#[must_use]
pub fn query_remote_v2_required_capabilities(carries_hydration_graph: bool) -> CapabilitySet {
    let mut capabilities = [
        CAP_QUERY_PLAN_V2,
        CAP_QUERY_REMOTE_ENVELOPE_V2,
        CAP_QUERY_REMOTE_STRUCTURED_DIAGNOSTIC,
    ]
    .into_iter()
    .map(static_capability)
    .collect::<CapabilitySet>();
    if carries_hydration_graph {
        capabilities.insert(static_capability(CAP_QUERY_OUTPUT_HYDRATED));
        capabilities.insert(static_capability(CAP_QUERY_SAME_SNAPSHOT_HYDRATION));
    }
    capabilities
}

fn static_capability(value: &'static str) -> CapabilityId {
    CapabilityId::new(value).expect("static V2 remote capability is canonical")
}

/// The response family bound into a V2 request fingerprint.
///
/// The complete model output, hydration projection, ordering, cardinality,
/// page, and root contract lives in the embedded V2 plan. This discriminator
/// prevents a valid plan/request from being replayed under another terminal.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RemoteResultKindV2 {
    /// Low-level positional rows.
    Rows,
    /// Low-level fetched documents.
    Documents,
    /// Low-level scalar count.
    Count,
    /// Low-level scalar existence.
    Exists,
    /// Model-oriented hydrated rows, including the exactly-one contract.
    HydratedRows,
    /// Model-oriented distinct-root page.
    HydratedPage,
    /// Model-oriented distinct-root count.
    DistinctCount,
    /// Model-oriented distinct-root existence.
    DistinctExists,
}

impl RemoteResultKindV2 {
    const fn operation(self) -> QueryOperation {
        match self {
            Self::Rows | Self::Documents | Self::HydratedRows | Self::HydratedPage => {
                QueryOperation::Rows
            }
            Self::Count | Self::DistinctCount => QueryOperation::Count,
            Self::Exists | Self::DistinctExists => QueryOperation::Exists,
        }
    }

    const fn compatibility_terminal(self) -> bool {
        matches!(
            self,
            Self::HydratedRows | Self::HydratedPage | Self::DistinctCount | Self::DistinctExists
        )
    }

    const fn carries_hydration_graph(self) -> bool {
        matches!(self, Self::HydratedRows | Self::HydratedPage)
    }
}

/// Caller-owned V2 transport and hydration budgets bound into one request.
///
/// These limits are additive rather than widening [`crate::query_remote::RemoteLimits`];
/// V1 request bytes and APIs remain exact.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteLimitsV2 {
    /// Optional execution deadline in milliseconds.
    pub deadline_ms: Option<u64>,
    /// Maximum authenticated successful reply bytes.
    pub max_bytes: u64,
    /// Maximum rows, page entries, or scalar evidence.
    pub max_items: u64,
    /// Maximum aggregate output collection members.
    pub max_collection_members: u64,
    /// Maximum hydration graph nodes.
    pub max_graph_nodes: u64,
    /// Maximum aggregate hydration attribute values.
    pub max_attribute_values: u64,
    /// Maximum aggregate hydration role-player references.
    pub max_role_players: u64,
}

/// One complete V2 remote invocation of a reusable V2 plan.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteQueryRequestV2 {
    advertisement: String,
    expires_at_unix_ms: u64,
    format: String,
    limits: RemoteLimitsV2,
    nonce: String,
    plan: serde_json::Value,
    prepared_at_unix_ms: u64,
    result: RemoteResultKindV2,
    rows: Vec<Vec<Option<CanonicalValue>>>,
}

impl RemoteQueryRequestV2 {
    /// Bind one V2 plan invocation, result contract, advertisement, and budget.
    ///
    /// `result` is checked against both the invocation operation and the
    /// embedded plan. Model-oriented result kinds additionally require the
    /// model-query contract carried by the V2 plan.
    pub fn new(
        plan: &QueryPlan,
        invocation: &QueryInvocation,
        result: RemoteResultKindV2,
        advertisement: &RemoteCapabilities,
        limits: RemoteLimitsV2,
        nonce: impl Into<String>,
        prepared_at_unix_ms: u64,
    ) -> Result<Self, Diagnostic> {
        validate_v2_plan_and_result(plan, result)?;
        if !invocation.binds(plan)? || invocation.operation() != result.operation() {
            return Err(remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_invocation_plan_mismatch",
                "V2 invocation, result contract, and plan do not bind one another",
            ));
        }
        let mut required = plan.required_capabilities().clone();
        for capability in invocation.transport_capabilities() {
            required.insert(capability);
        }
        for capability in query_remote_v2_required_capabilities(result.carries_hydration_graph()) {
            required.insert(capability);
        }
        required.ensure_supported_by(advertisement.capabilities())?;

        let nonce = nonce.into();
        check_nonce_v2(&nonce)?;
        validate_remote_limits_v2(limits)?;
        let expires_at_unix_ms = prepared_at_unix_ms
            .checked_add(limits.deadline_ms.unwrap_or(DEFAULT_REMOTE_DEADLINE_MS))
            .ok_or_else(remote_time_invalid_v2)?;
        validate_remote_time_shape_v2(prepared_at_unix_ms, expires_at_unix_ms, limits)?;
        let plan = serde_json::from_slice::<serde_json::Value>(&plan.canonical_bytes()?).map_err(
            |_| {
                remote_v2_failure(
                    DiagnosticCategory::Integrity,
                    "query_remote_v2_plan_unencodable",
                    "the V2 plan cannot be embedded as canonical JSON",
                )
            },
        )?;
        Ok(Self {
            advertisement: advertisement.fingerprint()?.digest_hex(),
            expires_at_unix_ms,
            format: QUERY_REMOTE_REQUEST_FORMAT_V2.to_owned(),
            limits,
            nonce,
            plan,
            prepared_at_unix_ms,
            result,
            rows: invocation
                .inputs()
                .iter()
                .map(|row| row.values().to_vec())
                .collect(),
        })
    }

    /// Encode exact canonical V2 request bytes.
    pub fn encode(&self) -> Result<Vec<u8>, Diagnostic> {
        to_canonical_json_with_limits(self, REMOTE_REQUEST_CODEC_LIMITS)
    }

    /// Decode one canonical V2 request, rejecting V1 and unknown versions
    /// before reconstructing the embedded plan.
    pub fn decode(bytes: &[u8]) -> Result<Self, Diagnostic> {
        require_format(bytes, QUERY_REMOTE_REQUEST_FORMAT_V2, "remote request")?;
        let request = from_canonical_json_with_limits::<Self>(bytes, REMOTE_REQUEST_CODEC_LIMITS)?;
        check_nonce_v2(&request.nonce)?;
        validate_remote_limits_v2(request.limits)?;
        FingerprintDigest::from_hex(&request.advertisement).map_err(|_| {
            remote_v2_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_v2_advertisement_invalid",
                "V2 request carries a malformed advertisement fingerprint",
            )
        })?;
        validate_remote_time_shape_v2(
            request.prepared_at_unix_ms,
            request.expires_at_unix_ms,
            request.limits,
        )?;
        let plan = request.plan()?;
        validate_v2_plan_and_result(&plan, request.result)?;
        request.invocation(&plan)?;
        if request.encode()? != bytes {
            return Err(remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_request_wire_mismatch",
                "V2 request bytes normalize after trusted reconstruction",
            ));
        }
        Ok(request)
    }

    /// Rebuild the trusted V2 plan from the embedded canonical document.
    pub fn plan(&self) -> Result<QueryPlan, Diagnostic> {
        let plan = decode_query_plan(&to_canonical_json(&self.plan)?)?;
        if !is_query_plan_v2(&plan) {
            return Err(remote_v2_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_v2_plan_format_mismatch",
                "a V2 remote request must embed a V2 query plan",
            ));
        }
        Ok(plan)
    }

    /// Rebuild the validated invocation against the carried plan.
    pub fn invocation(&self, plan: &QueryPlan) -> Result<QueryInvocation, Diagnostic> {
        QueryInvocation::new(
            plan,
            self.result.operation(),
            self.rows
                .iter()
                .map(|row| InputRow::new(row.clone()))
                .collect(),
        )
    }

    /// Compute the V2 request fingerprint over exact canonical bytes.
    pub fn fingerprint(&self) -> Result<RemoteRequestFingerprintV2, Diagnostic> {
        RemoteRequestFingerprintV2::compute(&self.encode()?)
    }

    /// Return the caller nonce echoed by a correlated reply.
    #[must_use]
    pub fn nonce(&self) -> &str {
        &self.nonce
    }

    /// Return the response family bound into this request.
    #[must_use]
    pub const fn result_kind(&self) -> RemoteResultKindV2 {
        self.result
    }

    /// Return the caller execution budgets.
    #[must_use]
    pub const fn limits(&self) -> RemoteLimitsV2 {
        self.limits
    }

    /// Return whether this request binds the executor's exact advertisement.
    pub fn binds_advertisement(
        &self,
        advertisement: &RemoteCapabilities,
    ) -> Result<bool, Diagnostic> {
        let expected = advertisement.fingerprint()?;
        let actual = FingerprintDigest::from_hex(&self.advertisement).map_err(|_| {
            remote_v2_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_v2_advertisement_invalid",
                "V2 request carries a malformed advertisement fingerprint",
            )
        })?;
        Ok(actual == expected.as_fingerprint().digest())
    }

    /// Recheck exact advertisement identity and every plan, invocation, and
    /// envelope capability before an executor constructs provider resources.
    pub fn validate_advertisement(
        &self,
        advertisement: &RemoteCapabilities,
    ) -> Result<(), Diagnostic> {
        if !self.binds_advertisement(advertisement)? {
            return Err(remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_advertisement_mismatch",
                "V2 request does not bind this executor advertisement",
            ));
        }
        let plan = self.plan()?;
        let invocation = self.invocation(&plan)?;
        let mut required = plan.required_capabilities().clone();
        for capability in invocation.transport_capabilities() {
            required.insert(capability);
        }
        for capability in
            query_remote_v2_required_capabilities(self.result.carries_hydration_graph())
        {
            required.insert(capability);
        }
        required.ensure_supported_by(advertisement.capabilities())
    }

    /// Validate the absolute request lifetime at one executor clock sample.
    pub fn remaining_lifetime_ms(&self, now_unix_ms: u64) -> Result<u64, Diagnostic> {
        validate_remote_time_shape_v2(
            self.prepared_at_unix_ms,
            self.expires_at_unix_ms,
            self.limits,
        )?;
        if self.prepared_at_unix_ms > now_unix_ms.saturating_add(MAX_REMOTE_CLOCK_SKEW_MS) {
            return Err(remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_time_future",
                "V2 request preparation time exceeds the allowed clock skew",
            ));
        }
        self.expires_at_unix_ms
            .checked_sub(now_unix_ms)
            .filter(|remaining| *remaining > 0)
            .ok_or_else(|| {
                remote_v2_failure(
                    DiagnosticCategory::ResourceLimit,
                    "query_remote_v2_request_expired",
                    "V2 request absolute expiry has elapsed",
                )
            })
    }

    /// Return remaining execution time without granting positive clock skew.
    pub fn remaining_execution_ms(&self, now_unix_ms: u64) -> Result<u64, Diagnostic> {
        let absolute_remaining = self.remaining_lifetime_ms(now_unix_ms)?;
        let declared_lifetime = self
            .expires_at_unix_ms
            .checked_sub(self.prepared_at_unix_ms)
            .ok_or_else(remote_time_invalid_v2)?;
        Ok(absolute_remaining.min(declared_lifetime))
    }

    /// Return the exclusive absolute expiry timestamp.
    #[must_use]
    pub const fn expires_at_unix_ms(&self) -> u64 {
        self.expires_at_unix_ms
    }
}

/// The canonical fingerprint of one complete V2 request envelope.
///
/// The domain is shared with V1 while the canonicalization identifier is
/// distinct, so identical-looking versioned documents cannot share identity.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RemoteRequestFingerprintV2(Fingerprint);

impl RemoteRequestFingerprintV2 {
    /// Compute a V2 request fingerprint from exact request bytes.
    pub fn compute(request_bytes: &[u8]) -> Result<Self, Diagnostic> {
        Ok(Self(Fingerprint::compute(
            FingerprintDomain::new(QUERY_REMOTE_REQUEST_FINGERPRINT_DOMAIN)?,
            CanonicalizationVersion::new(QUERY_REMOTE_REQUEST_CANONICALIZATION_V2)?,
            None,
            request_bytes,
        )))
    }

    /// Return the generic fingerprint.
    #[must_use]
    pub const fn as_fingerprint(&self) -> &Fingerprint {
        &self.0
    }

    fn digest_hex(&self) -> String {
        self.0.digest().to_hex()
    }
}

/// A strict V2 representation of one complete structured diagnostic.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteDiagnosticV2 {
    category: DiagnosticCategory,
    code: DiagnosticCode,
    details: BTreeMap<String, RemoteDiagnosticDetailV2>,
    message: String,
    path: Vec<RemoteDiagnosticPathSegmentV2>,
}

impl RemoteDiagnosticV2 {
    /// Preserve all diagnostic fields in their typed representation.
    pub fn new(diagnostic: &Diagnostic) -> Result<Self, Diagnostic> {
        let details = diagnostic
            .details()
            .iter()
            .map(|(key, value)| {
                validate_detail_key(key)?;
                Ok((key.clone(), RemoteDiagnosticDetailV2::from(value)))
            })
            .collect::<Result<BTreeMap<_, _>, Diagnostic>>()?;
        let value = Self {
            category: diagnostic.category(),
            code: diagnostic.code().clone(),
            details,
            message: diagnostic.message().to_owned(),
            path: diagnostic
                .path()
                .segments()
                .iter()
                .map(RemoteDiagnosticPathSegmentV2::from)
                .collect(),
        };
        value.validate_size()?;
        Ok(value)
    }

    /// Reconstruct the exact structured diagnostic.
    pub fn diagnostic(&self) -> Result<Diagnostic, Diagnostic> {
        self.validate_size()?;
        let path = DiagnosticPath::from_segments(
            self.path
                .iter()
                .map(DiagnosticPathSegment::from)
                .collect::<Vec<_>>(),
        );
        let mut diagnostic =
            Diagnostic::new(self.category, self.code.clone(), self.message.clone()).with_path(path);
        for (key, value) in &self.details {
            validate_detail_key(key)?;
            diagnostic = diagnostic.with_detail(key.clone(), detail_value_from_wire(value)?);
        }
        Ok(diagnostic)
    }

    fn validate_size(&self) -> Result<(), Diagnostic> {
        validate_remote_diagnostic_wire(self)?;
        let bytes = to_canonical_json(self)?;
        if bytes.len() > MAX_DIAGNOSTIC_BYTES {
            return Err(remote_v2_failure(
                DiagnosticCategory::ResourceLimit,
                "query_remote_v2_diagnostic_too_large",
                "V2 remote diagnostic exceeds the canonical byte ceiling",
            ));
        }
        Ok(())
    }
}

/// One strict typed V2 diagnostic path segment.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(
    tag = "kind",
    content = "value",
    rename_all = "snake_case",
    deny_unknown_fields
)]
pub enum RemoteDiagnosticPathSegmentV2 {
    /// An object field.
    Field(String),
    /// A zero-based collection index.
    Index(u64),
    /// A typed semantic identifier.
    Identifier(String),
}

impl From<&DiagnosticPathSegment> for RemoteDiagnosticPathSegmentV2 {
    fn from(value: &DiagnosticPathSegment) -> Self {
        match value {
            DiagnosticPathSegment::Field(value) => Self::Field(value.clone()),
            DiagnosticPathSegment::Index(value) => Self::Index(*value),
            DiagnosticPathSegment::Identifier(value) => Self::Identifier(value.clone()),
        }
    }
}

impl From<&RemoteDiagnosticPathSegmentV2> for DiagnosticPathSegment {
    fn from(value: &RemoteDiagnosticPathSegmentV2) -> Self {
        match value {
            RemoteDiagnosticPathSegmentV2::Field(value) => Self::Field(value.clone()),
            RemoteDiagnosticPathSegmentV2::Index(value) => Self::Index(*value),
            RemoteDiagnosticPathSegmentV2::Identifier(value) => Self::Identifier(value.clone()),
        }
    }
}

/// One strict typed V2 diagnostic detail value.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(
    tag = "kind",
    content = "value",
    rename_all = "snake_case",
    deny_unknown_fields
)]
pub enum RemoteDiagnosticDetailV2 {
    /// Text context.
    Text(String),
    /// Signed long, encoded canonically as decimal text for binding safety.
    Long(String),
    /// Boolean context.
    Boolean(bool),
    /// Ordered text values.
    TextList(Vec<String>),
}

impl From<&DiagnosticDetailValue> for RemoteDiagnosticDetailV2 {
    fn from(value: &DiagnosticDetailValue) -> Self {
        match value {
            DiagnosticDetailValue::Text(value) => Self::Text(value.clone()),
            DiagnosticDetailValue::Long(value) => Self::Long(value.to_string()),
            DiagnosticDetailValue::Boolean(value) => Self::Boolean(*value),
            DiagnosticDetailValue::TextList(value) => Self::TextList(value.clone()),
        }
    }
}

/// Dense zero-based node identity in one hydration graph.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(transparent)]
pub struct HydrationNodeIdV2(u32);

impl HydrationNodeIdV2 {
    /// Construct one node identity.
    #[must_use]
    pub const fn new(value: u32) -> Self {
        Self(value)
    }

    /// Return the dense ordinal.
    #[must_use]
    pub const fn get(self) -> u32 {
        self.0
    }
}

/// One reference to a canonical graph node under a declared model type.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HydrationReferenceV2 {
    declared: TypeId,
    node: HydrationNodeIdV2,
}

impl HydrationReferenceV2 {
    /// Construct one declared occurrence of a graph node.
    #[must_use]
    pub const fn new(declared: TypeId, node: HydrationNodeIdV2) -> Self {
        Self { declared, node }
    }

    /// Return the declared type under which the occurrence was selected.
    #[must_use]
    pub const fn declared(&self) -> &TypeId {
        &self.declared
    }

    /// Return the referenced dense node.
    #[must_use]
    pub const fn node(&self) -> HydrationNodeIdV2 {
        self.node
    }
}

/// Complete values for one provider attribute in a hydrated concrete model.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HydrationAttributeEvidenceV2 {
    attribute: AttributeId,
    values: Vec<CompatibilityValueV2>,
}

impl HydrationAttributeEvidenceV2 {
    /// Construct one descriptor-qualified attribute evidence entry.
    #[must_use]
    pub const fn new(attribute: AttributeId, values: Vec<CompatibilityValueV2>) -> Self {
        Self { attribute, values }
    }

    /// Return the provider attribute descriptor.
    #[must_use]
    pub const fn attribute(&self) -> &AttributeId {
        &self.attribute
    }

    /// Return canonical or released-only compatibility values in field order.
    #[must_use]
    pub fn values(&self) -> &[CompatibilityValueV2] {
        &self.values
    }
}

/// Complete players for one descriptor-qualified hydrated relation role.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HydrationRoleEvidenceV2 {
    players: Vec<HydrationReferenceV2>,
    role: RoleId,
}

impl HydrationRoleEvidenceV2 {
    /// Construct one ordered role-player evidence entry.
    #[must_use]
    pub const fn new(role: RoleId, players: Vec<HydrationReferenceV2>) -> Self {
        Self { players, role }
    }

    /// Return the exact descriptor-qualified role.
    #[must_use]
    pub const fn role(&self) -> &RoleId {
        &self.role
    }

    /// Return player occurrences in provider/materializer order.
    #[must_use]
    pub fn players(&self) -> &[HydrationReferenceV2] {
        &self.players
    }
}

/// Materializable concept kind of one hydration node.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum HydrationNodeKindV2 {
    /// Entity model.
    Entity,
    /// Relation model.
    Relation,
}

/// One canonical provider identity and its complete concrete model evidence.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HydrationNodeV2 {
    attributes: Vec<HydrationAttributeEvidenceV2>,
    concrete: TypeId,
    id: HydrationNodeIdV2,
    iid: String,
    kind: HydrationNodeKindV2,
    roles: Vec<HydrationRoleEvidenceV2>,
}

impl HydrationNodeV2 {
    /// Construct one node. Whole-graph ordering, uniqueness, and references
    /// are checked by [`HydrationGraphV2::new`].
    #[must_use]
    pub const fn new(
        id: HydrationNodeIdV2,
        iid: String,
        concrete: TypeId,
        kind: HydrationNodeKindV2,
        attributes: Vec<HydrationAttributeEvidenceV2>,
        roles: Vec<HydrationRoleEvidenceV2>,
    ) -> Self {
        Self {
            attributes,
            concrete,
            id,
            iid,
            kind,
            roles,
        }
    }

    /// Return this node's dense identity.
    #[must_use]
    pub const fn id(&self) -> HydrationNodeIdV2 {
        self.id
    }

    /// Return the exact provider IID.
    #[must_use]
    pub fn iid(&self) -> &str {
        &self.iid
    }

    /// Return the exact concrete descriptor.
    #[must_use]
    pub const fn concrete(&self) -> &TypeId {
        &self.concrete
    }

    /// Return entity or relation materialization kind.
    #[must_use]
    pub const fn kind(&self) -> HydrationNodeKindV2 {
        self.kind
    }

    /// Return complete ordered attribute evidence.
    #[must_use]
    pub fn attributes(&self) -> &[HydrationAttributeEvidenceV2] {
        &self.attributes
    }

    /// Return complete ordered relation-role evidence.
    #[must_use]
    pub fn roles(&self) -> &[HydrationRoleEvidenceV2] {
        &self.roles
    }
}

/// One dense, provider-identity-deduplicated hydration graph.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HydrationGraphV2 {
    nodes: Vec<HydrationNodeV2>,
}

impl HydrationGraphV2 {
    /// Validate and construct one complete hydration graph.
    pub fn new(nodes: Vec<HydrationNodeV2>) -> Result<Self, Diagnostic> {
        let graph = Self { nodes };
        graph.validate()?;
        Ok(graph)
    }

    /// Return dense nodes in canonical identity order.
    #[must_use]
    pub fn nodes(&self) -> &[HydrationNodeV2] {
        &self.nodes
    }

    fn validate(&self) -> Result<(), Diagnostic> {
        if self.nodes.len() > MAX_CANONICAL_COLLECTION_LEN {
            return Err(remote_graph_limit_v2());
        }
        let mut iids = BTreeSet::new();
        let mut previous_iid = None;
        for (index, node) in self.nodes.iter().enumerate() {
            if usize::try_from(node.id.get()).ok() != Some(index) {
                return Err(remote_evidence_mismatch_v2(
                    "hydration graph node IDs must be dense zero-based ordinals",
                ));
            }
            if !is_canonical_thing_iid(&node.iid) || !iids.insert(node.iid.as_str()) {
                return Err(remote_evidence_mismatch_v2(
                    "hydration graph provider IIDs must be canonical and unique",
                ));
            }
            if previous_iid.is_some_and(|previous| previous >= node.iid.as_str()) {
                return Err(remote_evidence_mismatch_v2(
                    "hydration graph nodes must be strictly ordered by provider IID",
                ));
            }
            previous_iid = Some(node.iid.as_str());
            let expected_kind = match node.kind {
                HydrationNodeKindV2::Entity => TypeKind::Entity,
                HydrationNodeKindV2::Relation => TypeKind::Relation,
            };
            if node.concrete.kind() != expected_kind {
                return Err(remote_evidence_mismatch_v2(
                    "hydration node kind contradicts its concrete descriptor",
                ));
            }
            if node.kind == HydrationNodeKindV2::Entity && !node.roles.is_empty() {
                return Err(remote_evidence_mismatch_v2(
                    "hydrated entities must not carry relation roles",
                ));
            }
            if node.attributes.windows(2).any(|pair| {
                pair[0].attribute().label().as_str() >= pair[1].attribute().label().as_str()
            }) {
                return Err(remote_evidence_mismatch_v2(
                    "hydration attributes must be strictly descriptor-ordered",
                ));
            }
            if node
                .roles
                .windows(2)
                .any(|pair| pair[0].role >= pair[1].role)
            {
                return Err(remote_evidence_mismatch_v2(
                    "hydration roles must be strictly descriptor-ordered",
                ));
            }
        }
        for node in &self.nodes {
            for role in &node.roles {
                for player in &role.players {
                    let player_index = usize::try_from(player.node.get()).map_err(|_| {
                        remote_evidence_mismatch_v2(
                            "hydration role player references an unknown graph node",
                        )
                    })?;
                    let Some(player_node) = self.nodes.get(player_index) else {
                        return Err(remote_evidence_mismatch_v2(
                            "hydration role player references an unknown graph node",
                        ));
                    };
                    if player.declared.kind() != player_node.concrete.kind() {
                        return Err(remote_evidence_mismatch_v2(
                            "hydration role-player declared and concrete kinds disagree",
                        ));
                    }
                }
            }
        }
        Ok(())
    }
}

/// One hydrated model-result slot.
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum HydrationSlotV2 {
    /// One singular model occurrence.
    Singular {
        /// Exact graph reference.
        value: HydrationReferenceV2,
    },
    /// One ordered model collection preserving multiplicity.
    Collection {
        /// Ordered graph references.
        values: Vec<HydrationReferenceV2>,
    },
}

impl HydrationSlotV2 {
    fn references(&self) -> &[HydrationReferenceV2] {
        match self {
            Self::Singular { value } => std::slice::from_ref(value),
            Self::Collection { values } => values,
        }
    }
}

/// One ordered hydrated output row.
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
#[serde(deny_unknown_fields)]
pub struct HydratedRowV2 {
    slots: Vec<HydrationSlotV2>,
}

impl HydratedRowV2 {
    /// Construct one ordered row.
    #[must_use]
    pub const fn new(slots: Vec<HydrationSlotV2>) -> Self {
        Self { slots }
    }

    /// Return output slots in public order.
    #[must_use]
    pub fn slots(&self) -> &[HydrationSlotV2] {
        &self.slots
    }
}

/// The typed terminal outcome of one V2 remote invocation.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RemoteOutcomeV2 {
    /// Evidence-validated low-level positional rows.
    Rows {
        /// Positional row values.
        rows: Vec<Vec<RemoteValue>>,
    },
    /// Evidence-validated low-level fetched documents.
    Documents {
        /// Positional document fields.
        documents: Vec<Vec<RemoteFieldValue>>,
    },
    /// Low-level answer count.
    Count {
        /// Exact unsigned count.
        value: u64,
    },
    /// Low-level existence verdict.
    Exists {
        /// Exact existence verdict.
        value: bool,
    },
    /// Model-oriented rows sharing one validated hydration graph.
    HydratedRows {
        /// Dense graph used by every row.
        graph: HydrationGraphV2,
        /// Ordered result rows.
        rows: Vec<HydratedRowV2>,
    },
    /// One distinct-root model page and optional same-snapshot total.
    HydratedPage {
        /// Ordered page entries.
        entries: Vec<HydratedRowV2>,
        /// Dense graph used by every entry.
        graph: HydrationGraphV2,
        /// Exact requested page limit.
        limit: u64,
        /// Exact requested page offset.
        offset: u64,
        /// Exact distinct root binding.
        #[serde(deserialize_with = "deserialize_binding_id_v2")]
        root: BindingId,
        /// Optional same-snapshot total.
        total: Option<u64>,
    },
    /// Lossless distinct-root count.
    DistinctCount {
        /// Exact counted root binding.
        #[serde(deserialize_with = "deserialize_binding_id_v2")]
        root: BindingId,
        /// Exact unsigned count.
        value: u64,
    },
    /// Distinct-root existence verdict.
    DistinctExists {
        /// Exact tested root binding.
        #[serde(deserialize_with = "deserialize_binding_id_v2")]
        root: BindingId,
        /// Exact existence verdict.
        value: bool,
    },
}

/// Caller budgets checked before typed V2 reply evidence is allocated.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RemoteReplyDecodeLimitsV2 {
    /// Maximum authenticated successful outer-reply bytes.
    pub max_bytes: u64,
    /// Maximum rows, page entries, count value, or positive existence item.
    pub max_items: u64,
    /// Maximum aggregate output collection members.
    pub max_collection_members: u64,
    /// Maximum hydration graph nodes.
    pub max_graph_nodes: u64,
    /// Maximum canonical values across hydration attribute evidence.
    pub max_attribute_values: u64,
    /// Maximum references across hydration role-player evidence.
    pub max_role_players: u64,
}

impl RemoteReplyDecodeLimitsV2 {
    fn hard_ceiling() -> Self {
        let collection = u64::try_from(MAX_CANONICAL_COLLECTION_LEN).unwrap_or(u64::MAX);
        Self {
            max_bytes: u64::try_from(MAX_REMOTE_ENVELOPE_BYTES).unwrap_or(u64::MAX),
            max_items: u64::MAX,
            max_collection_members: collection,
            max_graph_nodes: collection,
            max_attribute_values: collection,
            max_role_players: collection,
        }
    }
}

/// One successful V2 remote execution bound to its request and plan.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteQueryResponseV2 {
    format: String,
    nonce: String,
    outcome: RemoteOutcomeV2,
    plan: String,
    request: String,
}

impl RemoteQueryResponseV2 {
    /// Bind one V2 outcome to the nonce, V2 plan, whole V2 request, and
    /// request-selected result family.
    pub fn new(
        nonce: impl Into<String>,
        plan: &QueryPlan,
        request: &RemoteRequestFingerprintV2,
        expected_result: RemoteResultKindV2,
        outcome: RemoteOutcomeV2,
    ) -> Result<Self, Diagnostic> {
        let nonce = nonce.into();
        check_nonce_v2(&nonce)?;
        validate_v2_plan_and_result(plan, expected_result)?;
        validate_outcome_kind_v2(&outcome, expected_result)?;
        validate_outcome_evidence_v2(
            &outcome,
            expected_result,
            RemoteReplyDecodeLimitsV2::hard_ceiling(),
            Some(plan),
        )?;
        Ok(Self {
            format: QUERY_REMOTE_RESPONSE_FORMAT_V2.to_owned(),
            nonce,
            outcome,
            plan: plan.fingerprint()?.as_fingerprint().digest().to_hex(),
            request: request.digest_hex(),
        })
    }

    /// Encode one authenticated outer reply using the unchanged signed-reply
    /// and capability-advertisement formats.
    pub fn encode_signed(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        signer: &impl RemoteReplySigner,
    ) -> Result<Vec<u8>, Diagnostic> {
        crate::query_remote::encode_signed_reply_payload(
            &self.encode_payload()?,
            advertisement,
            signer,
        )
    }

    /// Return the exact signed outer-envelope length without signing.
    pub fn signed_encoded_len(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        key: RemoteSigningPublicKey,
    ) -> Result<usize, Diagnostic> {
        Ok(crate::query_remote::signed_reply_payload_encoded_len(
            &self.encode_payload()?,
            advertisement,
            key,
        ))
    }

    /// Return a signed-envelope framing lower bound for one result family.
    ///
    /// Model outcomes may require provider-owned graph evidence before a fully
    /// valid response can be constructed. This method uses the shortest
    /// syntactic member of the selected family, while still validating the
    /// nonce, plan terminal, and correlation fingerprints. Every valid signed
    /// response for the same bindings is at least this large, so executors can
    /// reject an impossible caller byte budget before constructing host state.
    pub fn signed_framing_floor(
        nonce: impl Into<String>,
        plan: &QueryPlan,
        request: &RemoteRequestFingerprintV2,
        expected_result: RemoteResultKindV2,
        advertisement: &RemoteCapabilitiesFingerprint,
        key: RemoteSigningPublicKey,
        max_items: u64,
    ) -> Result<usize, Diagnostic> {
        let nonce = nonce.into();
        check_nonce_v2(&nonce)?;
        validate_v2_plan_and_result(plan, expected_result)?;
        let root = BindingId::new(0)?;
        let outcome = match expected_result {
            RemoteResultKindV2::Rows => RemoteOutcomeV2::Rows { rows: Vec::new() },
            RemoteResultKindV2::Documents => RemoteOutcomeV2::Documents {
                documents: Vec::new(),
            },
            RemoteResultKindV2::Count => RemoteOutcomeV2::Count { value: 0 },
            RemoteResultKindV2::Exists if max_items == 0 => {
                RemoteOutcomeV2::Exists { value: false }
            }
            RemoteResultKindV2::Exists => RemoteOutcomeV2::Exists { value: true },
            RemoteResultKindV2::HydratedRows => RemoteOutcomeV2::HydratedRows {
                graph: HydrationGraphV2 { nodes: Vec::new() },
                rows: Vec::new(),
            },
            RemoteResultKindV2::HydratedPage => RemoteOutcomeV2::HydratedPage {
                entries: Vec::new(),
                graph: HydrationGraphV2 { nodes: Vec::new() },
                limit: 0,
                offset: 0,
                root,
                total: Some(0),
            },
            RemoteResultKindV2::DistinctCount => RemoteOutcomeV2::DistinctCount { root, value: 0 },
            RemoteResultKindV2::DistinctExists if max_items == 0 => {
                RemoteOutcomeV2::DistinctExists { root, value: false }
            }
            RemoteResultKindV2::DistinctExists => {
                RemoteOutcomeV2::DistinctExists { root, value: true }
            }
        };
        let response = Self {
            format: QUERY_REMOTE_RESPONSE_FORMAT_V2.to_owned(),
            nonce,
            outcome,
            plan: plan.fingerprint()?.as_fingerprint().digest().to_hex(),
            request: request.digest_hex(),
        };
        response.signed_encoded_len(advertisement, key)
    }

    fn encode_payload(&self) -> Result<Vec<u8>, Diagnostic> {
        to_canonical_json_with_limits(self, REMOTE_ENVELOPE_CODEC_LIMITS)
    }

    fn decode_bound(
        bytes: &[u8],
        expected_result: RemoteResultKindV2,
        limits: RemoteReplyDecodeLimitsV2,
        plan: Option<&QueryPlan>,
    ) -> Result<Self, Diagnostic> {
        require_format(bytes, QUERY_REMOTE_RESPONSE_FORMAT_V2, "remote response")?;
        let output_width = match expected_result {
            RemoteResultKindV2::Rows | RemoteResultKindV2::Documents => {
                plan.and_then(low_level_output_width)
            }
            RemoteResultKindV2::HydratedRows | RemoteResultKindV2::HydratedPage => {
                model_output_width(plan, expected_result)
            }
            RemoteResultKindV2::Count
            | RemoteResultKindV2::Exists
            | RemoteResultKindV2::DistinctCount
            | RemoteResultKindV2::DistinctExists => None,
        };
        preflight_response_evidence_v2(bytes, expected_result, output_width, limits)?;
        let response =
            from_canonical_json_with_limits::<Self>(bytes, REMOTE_ENVELOPE_CODEC_LIMITS)?;
        validate_outcome_kind_v2(&response.outcome, expected_result)?;
        validate_outcome_evidence_v2(&response.outcome, expected_result, limits, plan)?;
        if response.encode_payload()? != bytes {
            return Err(remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_response_wire_mismatch",
                "V2 response bytes normalize after trusted reconstruction",
            ));
        }
        Ok(response)
    }

    /// Return the typed outcome.
    #[must_use]
    pub const fn outcome(&self) -> &RemoteOutcomeV2 {
        &self.outcome
    }

    /// Consume the response and return its typed outcome.
    #[must_use]
    pub fn into_outcome(self) -> RemoteOutcomeV2 {
        self.outcome
    }
}

fn encode_minimal_signed_failure_v2(
    original: &RemoteQueryFailureV2,
    advertisement: &RemoteCapabilitiesFingerprint,
    signer: &impl RemoteReplySigner,
) -> Vec<u8> {
    const PREFIX: &[u8] = b"{\"category\":\"integrity\",\"code\":\"query_remote_v2_internal_failure\",\"details\":{},\"format\":\"typebridge.query-remote-failure/v2\",\"message\":\"executor could not encode the original V2 remote failure\",\"nonce\":";
    let nonce = original
        .nonce
        .as_deref()
        .filter(|nonce| check_nonce_v2(nonce).is_ok());
    let request = original.request.as_deref().filter(|request| {
        request.len() == 64
            && request
                .bytes()
                .all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
    });
    let mut payload = Vec::with_capacity(PREFIX.len() + 256);
    payload.extend_from_slice(PREFIX);
    append_optional_safe_ascii_v2(&mut payload, nonce);
    payload.extend_from_slice(b",\"path\":[],\"request\":");
    append_optional_safe_ascii_v2(&mut payload, request);
    payload.push(b'}');
    crate::query_remote::encode_signed_reply_payload_unchecked(&payload, advertisement, signer)
}

fn append_optional_safe_ascii_v2(encoded: &mut Vec<u8>, value: Option<&str>) {
    if let Some(value) = value {
        encoded.push(b'"');
        encoded.extend_from_slice(value.as_bytes());
        encoded.push(b'"');
    } else {
        encoded.extend_from_slice(b"null");
    }
}

/// One complete structured V2 remote failure bound to its request when one
/// decoded far enough to establish correlation.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteQueryFailureV2 {
    category: DiagnosticCategory,
    code: DiagnosticCode,
    details: BTreeMap<String, RemoteDiagnosticDetailV2>,
    format: String,
    message: String,
    nonce: Option<String>,
    path: Vec<RemoteDiagnosticPathSegmentV2>,
    request: Option<String>,
}

impl RemoteQueryFailureV2 {
    /// Construct an uncorrelated V2 failure for a pre-request transport error.
    pub fn new(nonce: Option<String>, diagnostic: &Diagnostic) -> Result<Self, Diagnostic> {
        let diagnostic = RemoteDiagnosticV2::new(diagnostic)?;
        if let Some(nonce) = nonce.as_deref() {
            check_nonce_v2(nonce)?;
        }
        Ok(Self {
            category: diagnostic.category,
            code: diagnostic.code,
            details: diagnostic.details,
            format: QUERY_REMOTE_FAILURE_FORMAT_V2.to_owned(),
            message: diagnostic.message,
            nonce,
            path: diagnostic.path,
            request: None,
        })
    }

    /// Bind all five diagnostic fields to the exact V2 request.
    pub fn bound(
        nonce: impl Into<String>,
        request: &RemoteRequestFingerprintV2,
        diagnostic: &Diagnostic,
    ) -> Result<Self, Diagnostic> {
        let nonce = nonce.into();
        check_nonce_v2(&nonce)?;
        let mut failure = Self::new(Some(nonce), diagnostic)?;
        failure.request = Some(request.digest_hex());
        Ok(failure)
    }

    /// Verify the mandatory request correlation of a request-bound failure.
    pub fn verify_binding(
        &self,
        expected_nonce: &str,
        expected_request: &RemoteRequestFingerprintV2,
    ) -> Result<(), Diagnostic> {
        verify_failure_binding_v2(
            self.nonce.as_deref(),
            self.request.as_deref(),
            expected_nonce,
            expected_request,
        )
    }

    /// Encode one authenticated V2 failure in the unchanged signed outer
    /// envelope.
    pub fn encode_signed(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        signer: &impl RemoteReplySigner,
    ) -> Result<Vec<u8>, Diagnostic> {
        crate::query_remote::encode_signed_reply_payload(
            &self.encode_payload()?,
            advertisement,
            signer,
        )
    }

    /// Encode an authenticated failure, replacing an unencodable diagnostic
    /// with a fixed bounded V2 internal failure while retaining safe request
    /// bindings.
    #[must_use]
    pub fn encode_signed_or_fallback(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        signer: &impl RemoteReplySigner,
    ) -> Vec<u8> {
        match self.encode_signed(advertisement, signer) {
            Ok(encoded) => encoded,
            Err(_) => encode_minimal_signed_failure_v2(self, advertisement, signer),
        }
    }

    fn encode_payload(&self) -> Result<Vec<u8>, Diagnostic> {
        self.remote_diagnostic()?.validate_size()?;
        to_canonical_json_with_limits(self, REMOTE_ENVELOPE_CODEC_LIMITS)
    }

    /// Decode an already authenticated V2 failure payload.
    pub fn decode_payload(bytes: &[u8]) -> Result<Self, Diagnostic> {
        require_format(bytes, QUERY_REMOTE_FAILURE_FORMAT_V2, "remote failure")?;
        let maximum = MAX_DIAGNOSTIC_BYTES.saturating_add(FAILURE_FRAMING_ALLOWANCE_BYTES);
        if bytes.len() > maximum {
            return Err(remote_v2_failure(
                DiagnosticCategory::ResourceLimit,
                "query_remote_v2_diagnostic_too_large",
                "V2 remote diagnostic exceeds the canonical byte ceiling",
            ));
        }
        let failure = from_canonical_json_with_limits::<Self>(bytes, REMOTE_ENVELOPE_CODEC_LIMITS)?;
        if let Some(nonce) = failure.nonce.as_deref() {
            check_nonce_v2(nonce)?;
        }
        failure.remote_diagnostic()?.validate_size()?;
        if failure.encode_payload()? != bytes {
            return Err(remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_failure_wire_mismatch",
                "V2 failure bytes normalize after trusted reconstruction",
            ));
        }
        Ok(failure)
    }

    /// Rebuild the exact structured diagnostic.
    pub fn diagnostic(&self) -> Result<Diagnostic, Diagnostic> {
        self.remote_diagnostic()?.diagnostic()
    }

    /// Return the echoed nonce, when request decoding reached it.
    #[must_use]
    pub fn nonce(&self) -> Option<&str> {
        self.nonce.as_deref()
    }

    fn remote_diagnostic(&self) -> Result<RemoteDiagnosticV2, Diagnostic> {
        let diagnostic = RemoteDiagnosticV2 {
            category: self.category,
            code: self.code.clone(),
            details: self.details.clone(),
            message: self.message.clone(),
            path: self.path.clone(),
        };
        validate_remote_diagnostic_wire(&diagnostic)?;
        Ok(diagnostic)
    }
}

/// One decoded and correlated V2 remote reply.
#[derive(Clone, Debug, PartialEq)]
pub enum RemoteReplyV2 {
    /// A successful V2 response.
    Response(RemoteQueryResponseV2),
    /// A fully structured V2 failure.
    Failure(RemoteQueryFailureV2),
}

#[derive(Deserialize)]
struct RemoteReplyBindingPeekV2<'wire> {
    #[serde(borrow)]
    format: &'wire str,
    #[serde(borrow)]
    nonce: Option<&'wire str>,
    #[serde(borrow)]
    plan: Option<&'wire str>,
    #[serde(borrow)]
    request: Option<&'wire str>,
}

/// Authenticate, correlate, preflight, and decode one V2 remote reply.
///
/// Signature and request correlation checks precede allocation of the typed
/// outcome or reconstruction of a payload-selected diagnostic.
#[expect(
    clippy::too_many_arguments,
    reason = "the trust-boundary API keeps every expected binding and verifier explicit"
)]
pub fn decode_remote_reply_v2(
    bytes: &[u8],
    expected_request_envelope: &RemoteQueryRequestV2,
    expected_plan: &QueryPlanFingerprint,
    expected_request: &RemoteRequestFingerprintV2,
    expected_advertisement: &RemoteCapabilitiesFingerprint,
    trusted_key: RemoteSigningPublicKey,
    limits: RemoteReplyDecodeLimitsV2,
    verifier: &impl RemoteReplyVerifier,
) -> Result<RemoteReplyV2, Diagnostic> {
    validate_reply_limits_v2(expected_request_envelope.limits(), limits)?;
    crate::query_remote::preflight_signed_reply_size(bytes, u64::MAX)?;
    let payload = crate::query_remote::verify_signed_reply_payload(
        bytes,
        expected_advertisement,
        trusted_key,
        verifier,
    )?;
    let plan = validate_expected_request_binding_v2(
        expected_request_envelope,
        expected_plan,
        expected_request,
        expected_advertisement,
    )?;
    let peek = peek_remote_reply_binding_v2(payload)?;
    if peek.format == QUERY_REMOTE_RESPONSE_FORMAT_V2 {
        verify_response_binding_v2(
            peek.nonce,
            peek.plan,
            peek.request,
            expected_request_envelope.nonce(),
            expected_plan,
            expected_request,
        )?;
        crate::query_remote::preflight_signed_reply_size(bytes, limits.max_bytes)?;
        let response = RemoteQueryResponseV2::decode_bound(
            payload,
            expected_request_envelope.result_kind(),
            limits,
            Some(&plan),
        )?;
        return Ok(RemoteReplyV2::Response(response));
    }
    if peek.format == QUERY_REMOTE_FAILURE_FORMAT_V2 {
        verify_failure_binding_v2(
            peek.nonce,
            peek.request,
            expected_request_envelope.nonce(),
            expected_request,
        )?;
        let failure = RemoteQueryFailureV2::decode_payload(payload)?;
        failure.verify_binding(expected_request_envelope.nonce(), expected_request)?;
        return Ok(RemoteReplyV2::Failure(failure));
    }
    Err(remote_v2_failure(
        DiagnosticCategory::InvalidContract,
        "query_remote_v2_format_unsupported",
        "signed payload is not a V2 remote response or failure",
    ))
}

fn validate_reply_limits_v2(
    request: RemoteLimitsV2,
    reply: RemoteReplyDecodeLimitsV2,
) -> Result<(), Diagnostic> {
    let widened = reply.max_bytes > request.max_bytes
        || reply.max_items > request.max_items
        || reply.max_collection_members > request.max_collection_members
        || reply.max_graph_nodes > request.max_graph_nodes
        || reply.max_attribute_values > request.max_attribute_values
        || reply.max_role_players > request.max_role_players;
    if widened {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_limits_widened",
            "reply decode budgets must monotonically tighten the request-bound limits",
        ));
    }
    Ok(())
}

fn validate_expected_request_binding_v2(
    request: &RemoteQueryRequestV2,
    expected_plan: &QueryPlanFingerprint,
    expected_request: &RemoteRequestFingerprintV2,
    expected_advertisement: &RemoteCapabilitiesFingerprint,
) -> Result<QueryPlan, Diagnostic> {
    let plan = request.plan()?;
    let actual_advertisement =
        FingerprintDigest::from_hex(&request.advertisement).map_err(|_| {
            remote_v2_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_v2_advertisement_invalid",
                "V2 request carries a malformed advertisement fingerprint",
            )
        })?;
    let coherent = plan.fingerprint()? == *expected_plan
        && request.fingerprint()? == *expected_request
        && actual_advertisement == expected_advertisement.as_fingerprint().digest();
    if !coherent {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_expected_binding_mismatch",
            "trusted V2 reply bindings do not describe the expected request envelope",
        ));
    }
    Ok(plan)
}

/// Authenticate and decode an uncorrelated V2 pre-request failure.
pub fn decode_signed_remote_failure_v2(
    bytes: &[u8],
    expected_advertisement: &RemoteCapabilitiesFingerprint,
    trusted_key: RemoteSigningPublicKey,
    max_bytes: u64,
    verifier: &impl RemoteReplyVerifier,
) -> Result<RemoteQueryFailureV2, Diagnostic> {
    crate::query_remote::preflight_signed_reply_size(bytes, max_bytes)?;
    let payload = crate::query_remote::verify_signed_reply_payload(
        bytes,
        expected_advertisement,
        trusted_key,
        verifier,
    )?;
    let failure = RemoteQueryFailureV2::decode_payload(payload)?;
    if failure.request.is_some() {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_failure_unexpected_binding",
            "a pre-request V2 failure must not carry a request fingerprint",
        ));
    }
    Ok(failure)
}

#[derive(Deserialize)]
struct FormatPeek<'wire> {
    #[serde(borrow)]
    format: &'wire str,
}

fn require_format(bytes: &[u8], expected: &str, subject: &str) -> Result<(), Diagnostic> {
    if bytes.len() > MAX_REMOTE_ENVELOPE_BYTES {
        return Err(remote_v2_failure(
            DiagnosticCategory::ResourceLimit,
            "query_remote_v2_envelope_too_large",
            "V2 remote envelope exceeds the hard byte ceiling",
        ));
    }
    let peek = serde_json::from_slice::<FormatPeek<'_>>(bytes).map_err(|_| {
        remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_format_missing",
            "remote envelope has no readable format discriminator",
        )
    })?;
    if peek.format != expected {
        return Err(remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_format_unsupported",
            match subject {
                "remote request" => "remote request is not the V2 wire format",
                "remote response" => "remote response is not the V2 wire format",
                "remote failure" => "remote failure is not the V2 wire format",
                _ => "remote envelope is not the expected V2 wire format",
            },
        ));
    }
    Ok(())
}

#[derive(Deserialize)]
#[serde(transparent)]
struct BindingIdV2Wire(u16);

fn deserialize_binding_id_v2<'de, D>(deserializer: D) -> Result<BindingId, D::Error>
where
    D: Deserializer<'de>,
{
    let wire = BindingIdV2Wire::deserialize(deserializer)?;
    BindingId::new(wire.0).map_err(serde::de::Error::custom)
}

fn is_query_plan_v2(plan: &QueryPlan) -> bool {
    plan.format() == "typebridge.query-plan/v2"
}

fn validate_v2_plan_and_result(
    plan: &QueryPlan,
    result: RemoteResultKindV2,
) -> Result<(), Diagnostic> {
    if !is_query_plan_v2(plan) {
        return Err(remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_plan_format_mismatch",
            "V2 remote envelopes accept only typebridge.query-plan/v2",
        ));
    }
    let has_model_contract = plan
        .v2_compatibility()
        .and_then(|compatibility| compatibility.model_query())
        .is_some();
    if has_model_contract != result.compatibility_terminal() {
        return Err(remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_result_contract_mismatch",
            "V2 result family does not match the plan's model-query contract",
        ));
    }
    if let Some(model) = plan
        .v2_compatibility()
        .and_then(|compatibility| compatibility.model_query())
    {
        let matches = matches!(
            (result, model),
            (
                RemoteResultKindV2::HydratedRows,
                crate::query_plan::ModelQueryV2::Rows { .. }
            ) | (
                RemoteResultKindV2::HydratedPage,
                crate::query_plan::ModelQueryV2::Page { .. }
            ) | (
                RemoteResultKindV2::DistinctCount,
                crate::query_plan::ModelQueryV2::DistinctCount { .. }
            ) | (
                RemoteResultKindV2::DistinctExists,
                crate::query_plan::ModelQueryV2::DistinctExists { .. }
            )
        );
        if !matches {
            return Err(remote_v2_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_v2_result_contract_mismatch",
                "V2 result family contradicts the plan's exact model-query terminal",
            ));
        }
    } else {
        match (result, plan.output()) {
            (RemoteResultKindV2::Rows, QueryOutput::Rows { .. })
            | (RemoteResultKindV2::Documents, QueryOutput::Documents { .. })
            | (RemoteResultKindV2::Count | RemoteResultKindV2::Exists, _) => {}
            _ => {
                return Err(remote_v2_failure(
                    DiagnosticCategory::InvalidContract,
                    "query_remote_v2_result_contract_mismatch",
                    "low-level result family contradicts the plan output",
                ));
            }
        }
    }
    Ok(())
}

fn check_nonce_v2(nonce: &str) -> Result<(), Diagnostic> {
    let valid = (NONCE_MIN_BYTES..=NONCE_MAX_BYTES).contains(&nonce.len())
        && nonce
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-');
    if valid {
        Ok(())
    } else {
        Err(remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_nonce_invalid",
            "V2 request nonces are 16-128 ASCII alphanumeric or dash bytes",
        ))
    }
}

fn validate_remote_limits_v2(limits: RemoteLimitsV2) -> Result<(), Diagnostic> {
    if limits
        .deadline_ms
        .is_some_and(|value| value > MAX_REMOTE_DEADLINE_MS)
    {
        return Err(remote_v2_failure(
            DiagnosticCategory::ResourceLimit,
            "query_remote_v2_deadline_limit",
            "V2 remote deadline exceeds the maximum supported duration",
        ));
    }
    Ok(())
}

fn validate_remote_time_shape_v2(
    prepared_at_unix_ms: u64,
    expires_at_unix_ms: u64,
    limits: RemoteLimitsV2,
) -> Result<(), Diagnostic> {
    let lifetime = expires_at_unix_ms
        .checked_sub(prepared_at_unix_ms)
        .ok_or_else(remote_time_invalid_v2)?;
    if lifetime != limits.deadline_ms.unwrap_or(DEFAULT_REMOTE_DEADLINE_MS) {
        return Err(remote_time_invalid_v2());
    }
    Ok(())
}

fn remote_time_invalid_v2() -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::InvalidContract,
        "query_remote_v2_time_invalid",
        "V2 request timestamps do not form a bounded absolute lifetime",
    )
}

fn validate_detail_key(key: &str) -> Result<(), Diagnostic> {
    DiagnosticCode::new(key.to_owned())
        .map(|_| ())
        .map_err(|_| {
            remote_v2_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_v2_diagnostic_detail_key_invalid",
                "V2 diagnostic detail keys must be canonical snake-case identifiers",
            )
        })
}

fn detail_value_from_wire(
    value: &RemoteDiagnosticDetailV2,
) -> Result<DiagnosticDetailValue, Diagnostic> {
    match value {
        RemoteDiagnosticDetailV2::Text(value) => Ok(DiagnosticDetailValue::Text(value.clone())),
        RemoteDiagnosticDetailV2::Long(value) => {
            let parsed = value.parse::<i64>().map_err(|_| {
                remote_v2_failure(
                    DiagnosticCategory::InvalidContract,
                    "query_remote_v2_diagnostic_long_invalid",
                    "V2 diagnostic long detail is outside the signed 64-bit range",
                )
            })?;
            if parsed.to_string() != *value {
                return Err(remote_v2_failure(
                    DiagnosticCategory::InvalidContract,
                    "query_remote_v2_diagnostic_long_invalid",
                    "V2 diagnostic long detail is not canonical decimal text",
                ));
            }
            Ok(DiagnosticDetailValue::Long(parsed))
        }
        RemoteDiagnosticDetailV2::Boolean(value) => Ok(DiagnosticDetailValue::Boolean(*value)),
        RemoteDiagnosticDetailV2::TextList(value) => {
            Ok(DiagnosticDetailValue::TextList(value.clone()))
        }
    }
}

fn validate_remote_diagnostic_wire(value: &RemoteDiagnosticV2) -> Result<(), Diagnostic> {
    for key in value.details.keys() {
        validate_detail_key(key)?;
    }
    for detail in value.details.values() {
        detail_value_from_wire(detail)?;
    }
    Ok(())
}

fn peek_remote_reply_binding_v2(bytes: &[u8]) -> Result<RemoteReplyBindingPeekV2<'_>, Diagnostic> {
    if bytes.len() > MAX_REMOTE_ENVELOPE_BYTES {
        return Err(remote_v2_failure(
            DiagnosticCategory::ResourceLimit,
            "query_remote_v2_envelope_too_large",
            "V2 remote reply exceeds the envelope byte ceiling",
        ));
    }
    serde_json::from_slice(bytes).map_err(|_| {
        remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_reply_malformed",
            "V2 remote reply has no readable correlation fields",
        )
    })
}

fn verify_response_binding_v2(
    nonce: Option<&str>,
    plan: Option<&str>,
    request: Option<&str>,
    expected_nonce: &str,
    expected_plan: &QueryPlanFingerprint,
    expected_request: &RemoteRequestFingerprintV2,
) -> Result<(), Diagnostic> {
    if nonce != Some(expected_nonce) {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_nonce_mismatch",
            "V2 response does not echo the request nonce",
        ));
    }
    let Some(plan) = plan else {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_plan_mismatch",
            "V2 response does not bind the invoked plan",
        ));
    };
    let echoed_plan = FingerprintDigest::from_hex(plan)?;
    if echoed_plan != expected_plan.as_fingerprint().digest() {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_plan_mismatch",
            "V2 response does not bind the invoked plan",
        ));
    }
    let Some(request) = request else {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_request_mismatch",
            "V2 response does not bind the exact request",
        ));
    };
    let echoed_request = FingerprintDigest::from_hex(request)?;
    if echoed_request != expected_request.as_fingerprint().digest() {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_request_mismatch",
            "V2 response does not bind the exact request",
        ));
    }
    Ok(())
}

fn verify_failure_binding_v2(
    nonce: Option<&str>,
    request: Option<&str>,
    expected_nonce: &str,
    expected_request: &RemoteRequestFingerprintV2,
) -> Result<(), Diagnostic> {
    let nonce = nonce.ok_or_else(|| {
        remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_failure_unbound",
            "V2 failure is not bound to the request nonce",
        )
    })?;
    if nonce != expected_nonce {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_nonce_mismatch",
            "V2 failure does not echo the request nonce",
        ));
    }
    let request = request.ok_or_else(|| {
        remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_failure_unbound",
            "V2 failure is not bound to the request fingerprint",
        )
    })?;
    let echoed_request = FingerprintDigest::from_hex(request)?;
    if echoed_request != expected_request.as_fingerprint().digest() {
        return Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_request_mismatch",
            "V2 failure does not bind the exact request",
        ));
    }
    Ok(())
}

fn validate_outcome_kind_v2(
    outcome: &RemoteOutcomeV2,
    expected: RemoteResultKindV2,
) -> Result<(), Diagnostic> {
    let actual = match outcome {
        RemoteOutcomeV2::Rows { .. } => RemoteResultKindV2::Rows,
        RemoteOutcomeV2::Documents { .. } => RemoteResultKindV2::Documents,
        RemoteOutcomeV2::Count { .. } => RemoteResultKindV2::Count,
        RemoteOutcomeV2::Exists { .. } => RemoteResultKindV2::Exists,
        RemoteOutcomeV2::HydratedRows { .. } => RemoteResultKindV2::HydratedRows,
        RemoteOutcomeV2::HydratedPage { .. } => RemoteResultKindV2::HydratedPage,
        RemoteOutcomeV2::DistinctCount { .. } => RemoteResultKindV2::DistinctCount,
        RemoteOutcomeV2::DistinctExists { .. } => RemoteResultKindV2::DistinctExists,
    };
    if actual == expected {
        Ok(())
    } else {
        Err(remote_v2_failure(
            DiagnosticCategory::Integrity,
            "query_remote_v2_outcome_mismatch",
            "V2 response outcome kind does not match the request-bound terminal",
        ))
    }
}

/// Validate one locally constructed V2 outcome against the exact plan,
/// terminal, and request-tightened evidence budgets.
///
/// This is the contract-owned gate for compatibility executors before they
/// hand evidence to the response serializer. It checks terminal identity,
/// low-level widths, model projections, hydration graph authority, and every
/// item/collection/graph budget. The authenticated outer-envelope byte budget
/// remains a property of the final signed response and is checked by
/// [`RemoteQueryResponseV2::encode_signed`] consumers.
pub fn validate_remote_outcome_v2(
    outcome: &RemoteOutcomeV2,
    expected: RemoteResultKindV2,
    limits: RemoteReplyDecodeLimitsV2,
    plan: &QueryPlan,
) -> Result<(), Diagnostic> {
    validate_outcome_evidence_v2(outcome, expected, limits, Some(plan))
}

fn validate_outcome_evidence_v2(
    outcome: &RemoteOutcomeV2,
    expected: RemoteResultKindV2,
    limits: RemoteReplyDecodeLimitsV2,
    plan: Option<&QueryPlan>,
) -> Result<(), Diagnostic> {
    validate_outcome_kind_v2(outcome, expected)?;
    match outcome {
        RemoteOutcomeV2::Rows { rows } => {
            check_sequence_item_budget(rows.len(), limits.max_items)?;
            let expected_width = plan.and_then(low_level_output_width);
            validate_low_level_rows(rows, expected_width)?;
        }
        RemoteOutcomeV2::Documents { documents } => {
            check_sequence_item_budget(documents.len(), limits.max_items)?;
            let expected_width = plan.and_then(low_level_output_width);
            let mut members = 0_u64;
            let width = expected_width.or_else(|| documents.first().map(Vec::len));
            for document in documents {
                if width.is_some_and(|width| document.len() != width)
                    || document.is_empty()
                    || document.len() > MAX_SELECTED_SLOTS
                {
                    return Err(remote_evidence_mismatch_v2(
                        "V2 document width contradicts the validated output",
                    ));
                }
                for field in document {
                    if let RemoteFieldValue::List { values } = field {
                        add_budgeted(
                            &mut members,
                            values.len(),
                            limits.max_collection_members,
                            remote_collection_limit_v2,
                        )?;
                    }
                }
            }
        }
        RemoteOutcomeV2::Count { value } => {
            if *value > limits.max_items {
                return Err(remote_item_limit_v2());
            }
        }
        RemoteOutcomeV2::Exists { value } => {
            if *value && limits.max_items == 0 {
                return Err(remote_item_limit_v2());
            }
        }
        RemoteOutcomeV2::HydratedRows { graph, rows } => {
            check_sequence_item_budget(rows.len(), limits.max_items)?;
            validate_hydration_graph_budget(graph, limits)?;
            validate_hydrated_rows(graph, rows, limits, plan, expected)?;
        }
        RemoteOutcomeV2::HydratedPage {
            entries,
            graph,
            limit,
            offset,
            root,
            total,
        } => {
            check_sequence_item_budget(entries.len(), limits.max_items)?;
            if u64::try_from(entries.len()).unwrap_or(u64::MAX) > *limit {
                return Err(remote_evidence_mismatch_v2(
                    "V2 page contains more entries than its exact limit",
                ));
            }
            if let Some(total) = total {
                let remaining = total.saturating_sub(*offset);
                let exact_len = remaining.min(*limit);
                if u64::try_from(entries.len()).unwrap_or(u64::MAX) != exact_len {
                    return Err(remote_evidence_mismatch_v2(
                        "V2 page entries contradict its same-snapshot total and window",
                    ));
                }
            }
            validate_hydration_graph_budget(graph, limits)?;
            validate_hydrated_rows(graph, entries, limits, plan, expected)?;
            validate_page_root_distinct(entries, *root, plan)?;
            validate_page_contract(plan, *root, *offset, *limit, total.is_some())?;
        }
        RemoteOutcomeV2::DistinctCount { root, value } => {
            validate_scalar_root(plan, expected, *root)?;
            if *value > limits.max_items {
                return Err(remote_item_limit_v2());
            }
        }
        RemoteOutcomeV2::DistinctExists { root, value } => {
            validate_scalar_root(plan, expected, *root)?;
            if *value && limits.max_items == 0 {
                return Err(remote_item_limit_v2());
            }
        }
    }
    Ok(())
}

fn low_level_output_width(plan: &QueryPlan) -> Option<usize> {
    match plan.output() {
        QueryOutput::Rows { columns } => Some(columns.len()),
        QueryOutput::Documents { fields } => Some(fields.len()),
    }
}

fn validate_low_level_rows(
    rows: &[Vec<RemoteValue>],
    expected_width: Option<usize>,
) -> Result<(), Diagnostic> {
    let width = expected_width.or_else(|| rows.first().map(Vec::len));
    for row in rows {
        if width.is_some_and(|width| row.len() != width)
            || row.is_empty()
            || row.len() > MAX_SELECTED_SLOTS
        {
            return Err(remote_evidence_mismatch_v2(
                "V2 row width contradicts the validated output",
            ));
        }
        for value in row {
            match value {
                RemoteValue::Thing { iid, type_id } => {
                    if !is_canonical_thing_iid(iid)
                        || !matches!(type_id.kind(), TypeKind::Entity | TypeKind::Relation)
                    {
                        return Err(remote_evidence_mismatch_v2(
                            "V2 thing evidence has an invalid IID or type kind",
                        ));
                    }
                }
                RemoteValue::Attribute { type_id, .. } => {
                    if type_id.kind() != TypeKind::Attribute {
                        return Err(remote_evidence_mismatch_v2(
                            "V2 attribute evidence has a non-attribute type",
                        ));
                    }
                }
                RemoteValue::Value { .. } | RemoteValue::Absent => {}
            }
        }
    }
    Ok(())
}

fn validate_hydration_graph_budget(
    graph: &HydrationGraphV2,
    limits: RemoteReplyDecodeLimitsV2,
) -> Result<(), Diagnostic> {
    graph.validate()?;
    if u64::try_from(graph.nodes.len()).unwrap_or(u64::MAX) > limits.max_graph_nodes {
        return Err(remote_graph_limit_v2());
    }
    let mut attribute_values = 0_u64;
    let mut role_players = 0_u64;
    for node in &graph.nodes {
        for attribute in &node.attributes {
            add_budgeted(
                &mut attribute_values,
                attribute.values.len(),
                limits.max_attribute_values,
                remote_attribute_limit_v2,
            )?;
        }
        for role in &node.roles {
            add_budgeted(
                &mut role_players,
                role.players.len(),
                limits.max_role_players,
                remote_role_player_limit_v2,
            )?;
        }
    }
    Ok(())
}

fn validate_hydrated_rows(
    graph: &HydrationGraphV2,
    rows: &[HydratedRowV2],
    limits: RemoteReplyDecodeLimitsV2,
    plan: Option<&QueryPlan>,
    result: RemoteResultKindV2,
) -> Result<(), Diagnostic> {
    let expected_width = model_output_width(plan, result);
    let width = expected_width.or_else(|| rows.first().map(|row| row.slots.len()));
    let mut collection_members = 0_u64;
    let mut seen_rows = BTreeSet::new();
    for row in rows {
        if width.is_some_and(|width| row.slots.len() != width)
            || row.slots.is_empty()
            || row.slots.len() > MAX_SELECTED_SLOTS
        {
            return Err(remote_evidence_mismatch_v2(
                "hydrated row width contradicts the model output contract",
            ));
        }
        if !seen_rows.insert(row.clone()) {
            return Err(remote_evidence_mismatch_v2(
                "hydrated rows must be distinct selected-identity tuples",
            ));
        }
        for slot in &row.slots {
            if let HydrationSlotV2::Collection { values } = slot {
                add_budgeted(
                    &mut collection_members,
                    values.len(),
                    limits.max_collection_members,
                    remote_collection_limit_v2,
                )?;
            }
            for reference in slot.references() {
                validate_graph_reference(graph, reference)?;
            }
        }
    }
    validate_model_rows_against_plan(rows, plan, result)?;
    validate_graph_against_model_plan(graph, rows, plan, result)?;
    validate_model_order_against_plan(graph, rows, plan, result)
}

fn validate_graph_reference(
    graph: &HydrationGraphV2,
    reference: &HydrationReferenceV2,
) -> Result<(), Diagnostic> {
    let index = usize::try_from(reference.node.get())
        .map_err(|_| remote_evidence_mismatch_v2("hydration slot references an unknown node"))?;
    let Some(node) = graph.nodes.get(index) else {
        return Err(remote_evidence_mismatch_v2(
            "hydration slot references an unknown node",
        ));
    };
    if reference.declared.kind() != node.concrete.kind() {
        return Err(remote_evidence_mismatch_v2(
            "hydration slot declared and concrete kinds disagree",
        ));
    }
    Ok(())
}

fn check_item_budget(actual: usize, maximum: u64) -> Result<(), Diagnostic> {
    if u64::try_from(actual).unwrap_or(u64::MAX) > maximum {
        Err(remote_item_limit_v2())
    } else {
        Ok(())
    }
}

fn check_sequence_item_budget(actual: usize, maximum: u64) -> Result<(), Diagnostic> {
    check_item_budget(actual, sequence_item_limit(maximum))
}

fn sequence_item_limit(maximum: u64) -> u64 {
    maximum.min(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).unwrap_or(u64::MAX))
}

fn add_budgeted(
    total: &mut u64,
    increment: usize,
    maximum: u64,
    failure: fn() -> Diagnostic,
) -> Result<(), Diagnostic> {
    *total = total.saturating_add(u64::try_from(increment).unwrap_or(u64::MAX));
    if *total > maximum {
        Err(failure())
    } else {
        Ok(())
    }
}

#[derive(Deserialize)]
struct ResponseOutcomeRaw<'wire> {
    #[serde(borrow)]
    outcome: &'wire serde_json::value::RawValue,
}

fn preflight_response_evidence_v2(
    payload: &[u8],
    expected: RemoteResultKindV2,
    output_width: Option<usize>,
    limits: RemoteReplyDecodeLimitsV2,
) -> Result<(), Diagnostic> {
    let response = serde_json::from_slice::<ResponseOutcomeRaw<'_>>(payload).map_err(|_| {
        remote_v2_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_v2_reply_malformed",
            "V2 response has no readable outcome",
        )
    })?;
    let mut state = StreamingPreflightState::default();
    let mut deserializer = serde_json::Deserializer::from_str(response.outcome.get());
    let result = OutcomeScanSeed {
        expected,
        limits,
        output_width,
        state: &mut state,
    }
    .deserialize(&mut deserializer)
    .and_then(|()| deserializer.end());
    result.map_err(|_| {
        state.failure.map_or_else(
            || {
                remote_v2_failure(
                    DiagnosticCategory::InvalidContract,
                    "query_remote_v2_reply_malformed",
                    "V2 response outcome is malformed",
                )
            },
            StreamingPreflightFailure::diagnostic,
        )
    })
}

#[derive(Clone, Copy)]
enum StreamingPreflightFailure {
    AttributeValues,
    CollectionMembers,
    GraphNodes,
    Items,
    Outcome,
    RolePlayers,
}

impl StreamingPreflightFailure {
    fn diagnostic(self) -> Diagnostic {
        match self {
            Self::AttributeValues => remote_attribute_limit_v2(),
            Self::CollectionMembers => remote_collection_limit_v2(),
            Self::GraphNodes => remote_graph_limit_v2(),
            Self::Items => remote_item_limit_v2(),
            Self::Outcome => remote_v2_failure(
                DiagnosticCategory::Integrity,
                "query_remote_v2_outcome_mismatch",
                "V2 response outcome kind does not match the request-bound terminal",
            ),
            Self::RolePlayers => remote_role_player_limit_v2(),
        }
    }
}

#[derive(Default)]
struct StreamingPreflightState {
    attribute_values: u64,
    collection_members: u64,
    failure: Option<StreamingPreflightFailure>,
    graph_nodes: u64,
    items: u64,
    role_players: u64,
}

impl StreamingPreflightState {
    fn increment(
        &mut self,
        counter: StreamingCounter,
        maximum: u64,
        failure: StreamingPreflightFailure,
    ) -> Result<(), &'static str> {
        let value = match counter {
            StreamingCounter::AttributeValues => &mut self.attribute_values,
            StreamingCounter::CollectionMembers => &mut self.collection_members,
            StreamingCounter::GraphNodes => &mut self.graph_nodes,
            StreamingCounter::Items => &mut self.items,
            StreamingCounter::RolePlayers => &mut self.role_players,
        };
        *value = value.saturating_add(1);
        if *value > maximum {
            self.failure = Some(failure);
            Err("V2 response budget exceeded")
        } else {
            Ok(())
        }
    }
}

#[derive(Clone, Copy)]
enum StreamingCounter {
    AttributeValues,
    CollectionMembers,
    GraphNodes,
    Items,
    RolePlayers,
}

struct OutcomeScanSeed<'scan> {
    expected: RemoteResultKindV2,
    limits: RemoteReplyDecodeLimitsV2,
    output_width: Option<usize>,
    state: &'scan mut StreamingPreflightState,
}

impl<'de> DeserializeSeed<'de> for OutcomeScanSeed<'_> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(OutcomeScanVisitor {
            expected: self.expected,
            limits: self.limits,
            output_width: self.output_width,
            state: self.state,
        })
    }
}

struct OutcomeScanVisitor<'scan> {
    expected: RemoteResultKindV2,
    limits: RemoteReplyDecodeLimitsV2,
    output_width: Option<usize>,
    state: &'scan mut StreamingPreflightState,
}

impl<'de> Visitor<'de> for OutcomeScanVisitor<'_> {
    type Value = ();

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a V2 response outcome")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut kind = None;
        while let Some(field) = map.next_key::<String>()? {
            match (field.as_str(), self.expected) {
                ("kind", _) => kind = Some(map.next_value::<String>()?),
                ("rows", RemoteResultKindV2::Rows) => {
                    map.next_value_seed(SequenceScanSeed {
                        child: ScanChild::Row,
                        counter: Some((
                            StreamingCounter::Items,
                            sequence_item_limit(self.limits.max_items),
                            StreamingPreflightFailure::Items,
                        )),
                        limits: self.limits,
                        local_maximum: None,
                        output_width: self.output_width,
                        state: self.state,
                    })?;
                }
                ("documents", RemoteResultKindV2::Documents) => {
                    map.next_value_seed(SequenceScanSeed {
                        child: ScanChild::Document,
                        counter: Some((
                            StreamingCounter::Items,
                            sequence_item_limit(self.limits.max_items),
                            StreamingPreflightFailure::Items,
                        )),
                        limits: self.limits,
                        local_maximum: None,
                        output_width: self.output_width,
                        state: self.state,
                    })?;
                }
                ("rows", RemoteResultKindV2::HydratedRows)
                | ("entries", RemoteResultKindV2::HydratedPage) => {
                    map.next_value_seed(SequenceScanSeed {
                        child: ScanChild::HydratedRow,
                        counter: Some((
                            StreamingCounter::Items,
                            sequence_item_limit(self.limits.max_items),
                            StreamingPreflightFailure::Items,
                        )),
                        limits: self.limits,
                        local_maximum: None,
                        output_width: self.output_width,
                        state: self.state,
                    })?;
                }
                ("graph", RemoteResultKindV2::HydratedRows | RemoteResultKindV2::HydratedPage) => {
                    map.next_value_seed(ObjectScanSeed {
                        kind: ScanObject::Graph,
                        limits: self.limits,
                        output_width: self.output_width,
                        state: self.state,
                    })?;
                }
                ("value", RemoteResultKindV2::Count | RemoteResultKindV2::DistinctCount) => {
                    if map.next_value::<u64>()? > self.limits.max_items {
                        self.state.failure = Some(StreamingPreflightFailure::Items);
                        return Err(serde::de::Error::custom("V2 item budget exceeded"));
                    }
                }
                ("value", RemoteResultKindV2::Exists | RemoteResultKindV2::DistinctExists) => {
                    if map.next_value::<bool>()? && self.limits.max_items == 0 {
                        self.state.failure = Some(StreamingPreflightFailure::Items);
                        return Err(serde::de::Error::custom("V2 item budget exceeded"));
                    }
                }
                _ => {
                    map.next_value::<IgnoredAny>()?;
                }
            }
        }
        let expected = match self.expected {
            RemoteResultKindV2::Rows => "rows",
            RemoteResultKindV2::Documents => "documents",
            RemoteResultKindV2::Count => "count",
            RemoteResultKindV2::Exists => "exists",
            RemoteResultKindV2::HydratedRows => "hydrated_rows",
            RemoteResultKindV2::HydratedPage => "hydrated_page",
            RemoteResultKindV2::DistinctCount => "distinct_count",
            RemoteResultKindV2::DistinctExists => "distinct_exists",
        };
        if kind.as_deref() != Some(expected) {
            self.state.failure = Some(StreamingPreflightFailure::Outcome);
            return Err(serde::de::Error::custom("V2 outcome kind mismatch"));
        }
        Ok(())
    }
}

#[derive(Clone, Copy)]
enum ScanChild {
    Attribute,
    Document,
    DocumentField,
    GraphNode,
    HydratedRow,
    HydratedSlot,
    Ignored,
    Row,
    Role,
}

struct SequenceScanSeed<'scan> {
    child: ScanChild,
    counter: Option<(StreamingCounter, u64, StreamingPreflightFailure)>,
    limits: RemoteReplyDecodeLimitsV2,
    local_maximum: Option<usize>,
    output_width: Option<usize>,
    state: &'scan mut StreamingPreflightState,
}

impl<'de> DeserializeSeed<'de> for SequenceScanSeed<'_> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(SequenceScanVisitor {
            child: self.child,
            counter: self.counter,
            limits: self.limits,
            local_maximum: self.local_maximum,
            output_width: self.output_width,
            state: self.state,
        })
    }
}

struct SequenceScanVisitor<'scan> {
    child: ScanChild,
    counter: Option<(StreamingCounter, u64, StreamingPreflightFailure)>,
    limits: RemoteReplyDecodeLimitsV2,
    local_maximum: Option<usize>,
    output_width: Option<usize>,
    state: &'scan mut StreamingPreflightState,
}

impl<'de> Visitor<'de> for SequenceScanVisitor<'_> {
    type Value = ();

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a bounded V2 response sequence")
    }

    fn visit_seq<S>(self, mut sequence: S) -> Result<Self::Value, S::Error>
    where
        S: SeqAccess<'de>,
    {
        let mut local_count = 0_usize;
        loop {
            let present = match self.child {
                ScanChild::Ignored => sequence.next_element::<IgnoredAny>()?.is_some(),
                ScanChild::Document => sequence
                    .next_element_seed(SequenceScanSeed {
                        child: ScanChild::DocumentField,
                        counter: None,
                        limits: self.limits,
                        local_maximum: Some(self.output_width.unwrap_or(MAX_SELECTED_SLOTS)),
                        output_width: self.output_width,
                        state: self.state,
                    })?
                    .is_some(),
                ScanChild::Row => sequence
                    .next_element_seed(SequenceScanSeed {
                        child: ScanChild::Ignored,
                        counter: None,
                        limits: self.limits,
                        local_maximum: Some(self.output_width.unwrap_or(MAX_SELECTED_SLOTS)),
                        output_width: self.output_width,
                        state: self.state,
                    })?
                    .is_some(),
                ScanChild::Attribute
                | ScanChild::DocumentField
                | ScanChild::GraphNode
                | ScanChild::HydratedRow
                | ScanChild::HydratedSlot
                | ScanChild::Role => {
                    let kind = match self.child {
                        ScanChild::Attribute => ScanObject::Attribute,
                        ScanChild::DocumentField => ScanObject::DocumentField,
                        ScanChild::GraphNode => ScanObject::GraphNode,
                        ScanChild::HydratedRow => ScanObject::HydratedRow,
                        ScanChild::HydratedSlot => ScanObject::HydratedSlot,
                        ScanChild::Role => ScanObject::Role,
                        ScanChild::Document | ScanChild::Ignored | ScanChild::Row => {
                            return Err(serde::de::Error::custom(
                                "invalid V2 response preflight state",
                            ));
                        }
                    };
                    sequence
                        .next_element_seed(ObjectScanSeed {
                            kind,
                            limits: self.limits,
                            output_width: self.output_width,
                            state: self.state,
                        })?
                        .is_some()
                }
            };
            if !present {
                break;
            }
            local_count = local_count.saturating_add(1);
            if self
                .local_maximum
                .is_some_and(|maximum| local_count > maximum)
            {
                return Err(serde::de::Error::custom(
                    "V2 response sequence exceeds its output width",
                ));
            }
            if let Some((counter, maximum, failure)) = self.counter {
                self.state
                    .increment(counter, maximum, failure)
                    .map_err(serde::de::Error::custom)?;
            }
        }
        Ok(())
    }
}

#[derive(Clone, Copy, Eq, PartialEq)]
enum ScanObject {
    Attribute,
    DocumentField,
    Graph,
    GraphNode,
    HydratedRow,
    HydratedSlot,
    Role,
}

struct ObjectScanSeed<'scan> {
    kind: ScanObject,
    limits: RemoteReplyDecodeLimitsV2,
    output_width: Option<usize>,
    state: &'scan mut StreamingPreflightState,
}

impl<'de> DeserializeSeed<'de> for ObjectScanSeed<'_> {
    type Value = ();

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ObjectScanVisitor {
            kind: self.kind,
            limits: self.limits,
            output_width: self.output_width,
            state: self.state,
        })
    }
}

struct ObjectScanVisitor<'scan> {
    kind: ScanObject,
    limits: RemoteReplyDecodeLimitsV2,
    output_width: Option<usize>,
    state: &'scan mut StreamingPreflightState,
}

impl<'de> Visitor<'de> for ObjectScanVisitor<'_> {
    type Value = ();

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("a V2 response evidence object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        while let Some(field) = map.next_key::<String>()? {
            let scan = match (self.kind, field.as_str()) {
                (ScanObject::DocumentField | ScanObject::HydratedSlot, "values") => Some((
                    ScanChild::Ignored,
                    Some((
                        StreamingCounter::CollectionMembers,
                        self.limits.max_collection_members,
                        StreamingPreflightFailure::CollectionMembers,
                    )),
                )),
                (ScanObject::Graph, "nodes") => Some((
                    ScanChild::GraphNode,
                    Some((
                        StreamingCounter::GraphNodes,
                        self.limits.max_graph_nodes,
                        StreamingPreflightFailure::GraphNodes,
                    )),
                )),
                (ScanObject::GraphNode, "attributes") => Some((ScanChild::Attribute, None)),
                (ScanObject::GraphNode, "roles") => Some((ScanChild::Role, None)),
                (ScanObject::Attribute, "values") => Some((
                    ScanChild::Ignored,
                    Some((
                        StreamingCounter::AttributeValues,
                        self.limits.max_attribute_values,
                        StreamingPreflightFailure::AttributeValues,
                    )),
                )),
                (ScanObject::Role, "players") => Some((
                    ScanChild::Ignored,
                    Some((
                        StreamingCounter::RolePlayers,
                        self.limits.max_role_players,
                        StreamingPreflightFailure::RolePlayers,
                    )),
                )),
                (ScanObject::HydratedRow, "slots") => Some((ScanChild::HydratedSlot, None)),
                _ => None,
            };
            if let Some((child, counter)) = scan {
                map.next_value_seed(SequenceScanSeed {
                    child,
                    counter,
                    limits: self.limits,
                    local_maximum: if self.kind == ScanObject::HydratedRow && field == "slots" {
                        Some(self.output_width.unwrap_or(MAX_SELECTED_SLOTS))
                    } else {
                        None
                    },
                    output_width: self.output_width,
                    state: self.state,
                })?;
            } else {
                map.next_value::<IgnoredAny>()?;
            }
        }
        Ok(())
    }
}

fn model_output_width(plan: Option<&QueryPlan>, result: RemoteResultKindV2) -> Option<usize> {
    let compatibility = plan?.v2_compatibility()?;
    let model = compatibility.model_query()?;
    match (result, model) {
        (
            RemoteResultKindV2::HydratedRows,
            crate::query_plan::ModelQueryV2::Rows { output, .. },
        )
        | (
            RemoteResultKindV2::HydratedPage,
            crate::query_plan::ModelQueryV2::Page { output, .. },
        ) => Some(output.slots().len()),
        _ => None,
    }
}

fn validate_model_rows_against_plan(
    rows: &[HydratedRowV2],
    plan: Option<&QueryPlan>,
    result: RemoteResultKindV2,
) -> Result<(), Diagnostic> {
    let Some(plan) = plan else {
        return Ok(());
    };
    let Some(model) = plan
        .v2_compatibility()
        .and_then(|compatibility| compatibility.model_query())
    else {
        return Err(remote_evidence_mismatch_v2(
            "hydrated result has no model-query contract",
        ));
    };
    match (result, model) {
        (
            RemoteResultKindV2::HydratedRows,
            crate::query_plan::ModelQueryV2::Rows {
                cardinality,
                output,
                window,
                ..
            },
        ) => {
            if cardinality.is_exactly_one() && rows.len() != 1 {
                return Err(remote_evidence_mismatch_v2(
                    "exactly-one terminal must carry exactly one hydrated row",
                ));
            }
            if u64::try_from(rows.len()).unwrap_or(u64::MAX) > window.limit() {
                return Err(remote_evidence_mismatch_v2(
                    "hydrated rows exceed the request-bound row window",
                ));
            }
            validate_slot_shapes(rows, output)
        }
        (
            RemoteResultKindV2::HydratedPage,
            crate::query_plan::ModelQueryV2::Page { output, .. },
        ) => validate_slot_shapes(rows, output),
        _ => Err(remote_evidence_mismatch_v2(
            "hydrated outcome contradicts the model-query terminal",
        )),
    }
}

fn validate_graph_against_model_plan(
    graph: &HydrationGraphV2,
    rows: &[HydratedRowV2],
    plan: Option<&QueryPlan>,
    result: RemoteResultKindV2,
) -> Result<(), Diagnostic> {
    let Some(plan) = plan else {
        return Ok(());
    };
    let Some(model) = plan
        .v2_compatibility()
        .and_then(|compatibility| compatibility.model_query())
    else {
        return Err(remote_evidence_mismatch_v2(
            "hydration graph has no request-bound projection",
        ));
    };
    let (output, hydration) = match (result, model) {
        (
            RemoteResultKindV2::HydratedRows,
            crate::query_plan::ModelQueryV2::Rows {
                output, hydration, ..
            },
        )
        | (
            RemoteResultKindV2::HydratedPage,
            crate::query_plan::ModelQueryV2::Page {
                output, hydration, ..
            },
        ) => (output, hydration),
        _ => {
            return Err(remote_evidence_mismatch_v2(
                "hydration graph contradicts the model-query terminal",
            ));
        }
    };

    let descriptors = hydration
        .descriptors()
        .iter()
        .map(|descriptor| (descriptor.descriptor(), descriptor))
        .collect::<BTreeMap<_, _>>();
    let slots = output.slots();
    let binding_projections = hydration
        .bindings()
        .iter()
        .map(|binding| (binding.binding(), binding))
        .collect::<BTreeMap<_, _>>();
    let mut full_nodes = BTreeSet::new();
    let mut frontier = Vec::new();
    for row in rows {
        for (slot, slot_contract) in row.slots.iter().zip(&slots) {
            let Some(binding) = binding_projections.get(&slot_contract.binding()) else {
                return Err(remote_evidence_mismatch_v2(
                    "hydrated output binding is absent from the projection",
                ));
            };
            for reference in slot.references() {
                let node_index = usize::try_from(reference.node.get()).map_err(|_| {
                    remote_evidence_mismatch_v2("hydrated output references an unknown node")
                })?;
                let Some(node) = graph.nodes.get(node_index) else {
                    return Err(remote_evidence_mismatch_v2(
                        "hydrated output references an unknown node",
                    ));
                };
                if reference.declared != *binding.declared_descriptor()
                    || !binding.concrete_descriptors().contains(&node.concrete)
                {
                    return Err(remote_evidence_mismatch_v2(
                        "hydrated output violates declared-to-concrete authority",
                    ));
                }
                full_nodes.insert(reference.node);
                frontier.push(reference.node);
            }
        }
    }
    for node in &graph.nodes {
        let Some(projection) = descriptors.get(&node.concrete) else {
            return Err(remote_evidence_mismatch_v2(
                "hydration node concrete descriptor is outside the projection",
            ));
        };
        validate_node_projection(graph, node, projection, full_nodes.contains(&node.id))?;
    }
    let mut unique_values = BTreeMap::<(TypeId, AttributeId), Vec<&CompatibilityValueV2>>::new();
    for node in &graph.nodes {
        let Some(projection) = descriptors.get(&node.concrete) else {
            return Err(remote_evidence_mismatch_v2(
                "hydration node concrete descriptor is outside the projection",
            ));
        };
        let fields = projection
            .fields()
            .iter()
            .map(|field| (field.attribute(), field))
            .collect::<BTreeMap<_, _>>();
        for evidence in &node.attributes {
            let Some(field) = fields.get(&evidence.attribute) else {
                return Err(remote_evidence_mismatch_v2(
                    "hydration attribute is outside the concrete projection",
                ));
            };
            if field.unique() {
                for owner in field.reference_owners() {
                    let authority = (owner.clone(), evidence.attribute.clone());
                    let seen = unique_values.entry(authority).or_default();
                    for value in &evidence.values {
                        seen.push(value);
                    }
                }
            }
        }
    }
    for values in unique_values.values() {
        if contains_semantic_duplicate(values.iter().copied())? {
            return Err(remote_evidence_mismatch_v2(
                "unique hydration field repeats one value across provider identities",
            ));
        }
    }

    let mut referenced = BTreeSet::new();
    while let Some(node_id) = frontier.pop() {
        if !referenced.insert(node_id) {
            continue;
        }
        let node_index = usize::try_from(node_id.get()).map_err(|_| {
            remote_evidence_mismatch_v2("hydration graph traversal reached an unknown node")
        })?;
        let node = graph.nodes.get(node_index).ok_or_else(|| {
            remote_evidence_mismatch_v2("hydration graph traversal reached an unknown node")
        })?;
        if full_nodes.contains(&node_id) {
            for role in &node.roles {
                for player in &role.players {
                    frontier.push(player.node);
                }
            }
        }
    }
    if referenced.len() != graph.nodes.len() {
        return Err(remote_evidence_mismatch_v2(
            "hydration graph contains unreferenced provider identities",
        ));
    }
    Ok(())
}

fn validate_model_order_against_plan(
    graph: &HydrationGraphV2,
    rows: &[HydratedRowV2],
    plan: Option<&QueryPlan>,
    result: RemoteResultKindV2,
) -> Result<(), Diagnostic> {
    let Some(model) = plan
        .and_then(QueryPlan::v2_compatibility)
        .and_then(|compatibility| compatibility.model_query())
    else {
        return Ok(());
    };
    let (output, row_order) = match (result, model) {
        (
            RemoteResultKindV2::HydratedRows,
            crate::query_plan::ModelQueryV2::Rows { order, output, .. },
        ) => (output, order.as_ref()),
        (
            RemoteResultKindV2::HydratedPage,
            crate::query_plan::ModelQueryV2::Page { order, output, .. },
        ) => (output, Some(order)),
        _ => {
            return Err(remote_evidence_mismatch_v2(
                "hydrated result has no request-bound order contract",
            ));
        }
    };

    validate_collection_orders(graph, rows, output)?;
    if let Some(order) = row_order {
        validate_row_order(graph, rows, output, order)?;
    }
    if result == RemoteResultKindV2::HydratedRows {
        validate_selected_row_identities(rows, output)?;
    }
    Ok(())
}

fn validate_selected_row_identities(
    rows: &[HydratedRowV2],
    output: &crate::query_plan::ModelOutputV2,
) -> Result<(), Diagnostic> {
    let singular_bindings = output
        .slots()
        .into_iter()
        .filter(|slot| !slot.collection())
        .map(crate::query_plan::QueryModelOutputSlotV2::binding)
        .collect::<Vec<_>>();
    let mut identities = BTreeSet::new();
    for row in rows {
        let identity = row_identity(row, output, &singular_bindings)?;
        if !identities.insert(identity) {
            return Err(remote_evidence_mismatch_v2(
                "hydrated rows repeat one selected singular-identity tuple",
            ));
        }
    }
    Ok(())
}

fn validate_row_order(
    graph: &HydrationGraphV2,
    rows: &[HydratedRowV2],
    output: &crate::query_plan::ModelOutputV2,
    order: &crate::query_plan::QueryStableOrderV2,
) -> Result<(), Diagnostic> {
    for pair in rows.windows(2) {
        let ordering = compare_rows_by_order(graph, &pair[0], &pair[1], output, order)?;
        if ordering == Ordering::Greater {
            return Err(remote_evidence_mismatch_v2(
                "hydrated rows are not in the request-bound stable order",
            ));
        }
        if ordering == Ordering::Equal
            && row_identity(&pair[0], output, order.identity_tiebreakers())?
                != row_identity(&pair[1], output, order.identity_tiebreakers())?
        {
            return Err(remote_evidence_mismatch_v2(
                "hydrated row order does not distinguish two selected identities",
            ));
        }
    }
    Ok(())
}

fn compare_rows_by_order(
    graph: &HydrationGraphV2,
    left: &HydratedRowV2,
    right: &HydratedRowV2,
    output: &crate::query_plan::ModelOutputV2,
    order: &crate::query_plan::QueryStableOrderV2,
) -> Result<Ordering, Diagnostic> {
    for term in order.terms() {
        let left = row_binding_reference(left, output, term.field().binding())?;
        let right = row_binding_reference(right, output, term.field().binding())?;
        let ordering = compare_order_values(
            order_value(graph, left, term)?,
            order_value(graph, right, term)?,
            term,
        )?;
        if ordering != Ordering::Equal {
            return Ok(ordering);
        }
    }
    Ok(Ordering::Equal)
}

fn validate_collection_orders(
    graph: &HydrationGraphV2,
    rows: &[HydratedRowV2],
    output: &crate::query_plan::ModelOutputV2,
) -> Result<(), Diagnostic> {
    let output_slots = output.slots();
    for row in rows {
        for (slot, contract) in row.slots().iter().zip(&output_slots) {
            let (
                HydrationSlotV2::Collection { values },
                crate::query_plan::QueryModelOutputSlotV2::Collect { order, .. },
            ) = (slot, *contract)
            else {
                continue;
            };
            for pair in values.windows(2) {
                let ordering = compare_references_by_order(graph, &pair[0], &pair[1], order)?;
                if ordering == Ordering::Greater {
                    return Err(remote_evidence_mismatch_v2(
                        "hydrated collection members are not in the request-bound stable order",
                    ));
                }
                if ordering == Ordering::Equal && pair[0].node() != pair[1].node() {
                    return Err(remote_evidence_mismatch_v2(
                        "hydrated collection order does not distinguish two provider identities",
                    ));
                }
            }
        }
    }
    Ok(())
}

fn compare_references_by_order(
    graph: &HydrationGraphV2,
    left: &HydrationReferenceV2,
    right: &HydrationReferenceV2,
    order: &crate::query_plan::QueryStableOrderV2,
) -> Result<Ordering, Diagnostic> {
    for term in order.terms() {
        let ordering = compare_order_values(
            order_value(graph, left, term)?,
            order_value(graph, right, term)?,
            term,
        )?;
        if ordering != Ordering::Equal {
            return Ok(ordering);
        }
    }
    Ok(Ordering::Equal)
}

fn row_identity(
    row: &HydratedRowV2,
    output: &crate::query_plan::ModelOutputV2,
    bindings: &[BindingId],
) -> Result<Vec<(BindingId, HydrationNodeIdV2)>, Diagnostic> {
    let mut identity = bindings
        .iter()
        .map(|binding| {
            row_binding_reference(row, output, *binding)
                .map(|reference| (*binding, reference.node()))
        })
        .collect::<Result<Vec<_>, _>>()?;
    identity.sort_unstable();
    Ok(identity)
}

fn row_binding_reference<'row>(
    row: &'row HydratedRowV2,
    output: &crate::query_plan::ModelOutputV2,
    binding: BindingId,
) -> Result<&'row HydrationReferenceV2, Diagnostic> {
    let output_slots = output.slots();
    let Some(index) = output_slots
        .iter()
        .position(|slot| slot.binding() == binding)
    else {
        return Err(remote_evidence_mismatch_v2(
            "stable order references a binding absent from the public output",
        ));
    };
    match row.slots().get(index) {
        Some(HydrationSlotV2::Singular { value }) => Ok(value),
        _ => Err(remote_evidence_mismatch_v2(
            "stable row order requires a singular public binding",
        )),
    }
}

fn order_value<'graph>(
    graph: &'graph HydrationGraphV2,
    reference: &HydrationReferenceV2,
    term: &crate::query_plan::QueryOrderTermV2,
) -> Result<Option<&'graph CompatibilityValueV2>, Diagnostic> {
    let index = usize::try_from(reference.node().get()).map_err(|_| {
        remote_evidence_mismatch_v2("stable order references an unknown hydration node")
    })?;
    let node = graph.nodes.get(index).ok_or_else(|| {
        remote_evidence_mismatch_v2("stable order references an unknown hydration node")
    })?;
    let evidence = node
        .attributes
        .iter()
        .find(|evidence| evidence.attribute() == term.field().attribute())
        .ok_or_else(|| {
            remote_evidence_mismatch_v2("stable order attribute is absent from hydration evidence")
        })?;
    if evidence.values().len() > 1 {
        return Err(remote_evidence_mismatch_v2(
            "stable order attribute carries non-scalar hydration evidence",
        ));
    }
    Ok(evidence.values().first())
}

fn compare_order_values(
    left: Option<&CompatibilityValueV2>,
    right: Option<&CompatibilityValueV2>,
    term: &crate::query_plan::QueryOrderTermV2,
) -> Result<Ordering, Diagnostic> {
    let ordering = match (left, right) {
        (None, None) => Ordering::Equal,
        (None, Some(_)) => match term.missing() {
            crate::query_plan::QueryMissingOrderV2::First => Ordering::Less,
            crate::query_plan::QueryMissingOrderV2::Last => Ordering::Greater,
            crate::query_plan::QueryMissingOrderV2::Reject => {
                return Err(remote_evidence_mismatch_v2(
                    "stable order rejects a missing hydration value",
                ));
            }
        },
        (Some(_), None) => match term.missing() {
            crate::query_plan::QueryMissingOrderV2::First => Ordering::Greater,
            crate::query_plan::QueryMissingOrderV2::Last => Ordering::Less,
            crate::query_plan::QueryMissingOrderV2::Reject => {
                return Err(remote_evidence_mismatch_v2(
                    "stable order rejects a missing hydration value",
                ));
            }
        },
        (Some(left), Some(right)) => {
            let ordering = left.semantic_cmp_same_domain(right).ok_or_else(|| {
                remote_evidence_mismatch_v2("stable order values are not semantically comparable")
            })?;
            if term.direction() == crate::query_plan::QueryOrderDirectionV2::Descending {
                ordering.reverse()
            } else {
                ordering
            }
        }
    };
    Ok(ordering)
}

fn validate_node_projection(
    graph: &HydrationGraphV2,
    node: &HydrationNodeV2,
    projection: &crate::query_plan::HydrationDescriptorV2,
    full_output: bool,
) -> Result<(), Diagnostic> {
    if node.attributes.len() != projection.fields().len()
        || (full_output && node.roles.len() != projection.roles().len())
        || (!full_output && !node.roles.is_empty())
    {
        return Err(remote_evidence_mismatch_v2(
            "hydration node contradicts its full-output or shallow-player projection",
        ));
    }
    let fields = projection
        .fields()
        .iter()
        .map(|field| (field.attribute(), field))
        .collect::<BTreeMap<_, _>>();
    for evidence in &node.attributes {
        let Some(field) = fields.get(&evidence.attribute) else {
            return Err(remote_evidence_mismatch_v2(
                "hydration attribute is outside the concrete projection",
            ));
        };
        if !cardinality_allows(field.cardinality(), evidence.values.len())
            || evidence
                .values
                .iter()
                .any(|value| value.value_type() != field.value_type())
        {
            return Err(remote_evidence_mismatch_v2(
                "hydration attribute violates its value type or cardinality",
            ));
        }
        if !field.ordered() {
            for pair in evidence.values.windows(2) {
                let ordering = pair[0].semantic_cmp_same_domain(&pair[1]).ok_or_else(|| {
                    remote_evidence_mismatch_v2(
                        "hydration attribute values are not semantically comparable",
                    )
                })?;
                if ordering != Ordering::Less {
                    return Err(remote_evidence_mismatch_v2(
                        "unordered hydration attribute values must be semantically sorted and unique",
                    ));
                }
            }
        }
        if field.ordered()
            && field.distinct()
            && contains_semantic_duplicate(evidence.values.iter())?
        {
            return Err(remote_evidence_mismatch_v2(
                "distinct ordered hydration attribute contains duplicate values",
            ));
        }
    }
    if !full_output {
        return Ok(());
    }
    let roles = projection
        .roles()
        .iter()
        .map(|role| (role.role(), role))
        .collect::<BTreeMap<_, _>>();
    for evidence in &node.roles {
        let Some(role) = roles.get(&evidence.role) else {
            return Err(remote_evidence_mismatch_v2(
                "hydration role is outside the concrete projection",
            ));
        };
        if !cardinality_allows(role.cardinality(), evidence.players.len()) {
            return Err(remote_evidence_mismatch_v2(
                "hydration role violates its effective cardinality",
            ));
        }
        if !role.ordered()
            && evidence.players.windows(2).any(|pair| {
                (pair[0].node(), pair[0].declared()) > (pair[1].node(), pair[1].declared())
            })
        {
            return Err(remote_evidence_mismatch_v2(
                "unordered hydration role players must be in canonical order",
            ));
        }
        if role.distinct() {
            let distinct_players = evidence
                .players
                .iter()
                .map(HydrationReferenceV2::node)
                .collect::<BTreeSet<_>>();
            if distinct_players.len() != evidence.players.len() {
                return Err(remote_evidence_mismatch_v2(
                    "distinct hydration role contains a repeated provider identity",
                ));
            }
        }
        for player in &evidence.players {
            let index = usize::try_from(player.node.get()).map_err(|_| {
                remote_evidence_mismatch_v2("hydration role references an unknown player")
            })?;
            let Some(player_node) = graph.nodes.get(index) else {
                return Err(remote_evidence_mismatch_v2(
                    "hydration role references an unknown player",
                ));
            };
            let Some(player_authority) = role
                .players()
                .iter()
                .find(|authority| authority.declared_descriptor() == &player.declared)
            else {
                return Err(remote_evidence_mismatch_v2(
                    "hydration role player uses an undeclared descriptor",
                ));
            };
            if !player_authority
                .concrete_descriptors()
                .contains(&player_node.concrete)
            {
                return Err(remote_evidence_mismatch_v2(
                    "hydration role player violates descriptor compatibility",
                ));
            }
        }
    }
    Ok(())
}

fn contains_semantic_duplicate<'value>(
    values: impl IntoIterator<Item = &'value CompatibilityValueV2>,
) -> Result<bool, Diagnostic> {
    let mut values = values.into_iter().collect::<Vec<_>>();
    if let Some(first) = values.first().copied() {
        for value in &values[1..] {
            first.semantic_cmp_same_domain(value).ok_or_else(|| {
                remote_evidence_mismatch_v2(
                    "hydration attribute values are not semantically comparable",
                )
            })?;
        }
    }
    let mut incomparable = false;
    values.sort_by(|left, right| match left.semantic_cmp_same_domain(right) {
        Some(ordering) => ordering.then_with(|| left.cmp(right)),
        None => {
            incomparable = true;
            left.cmp(right)
        }
    });
    if incomparable {
        return Err(remote_evidence_mismatch_v2(
            "hydration attribute values are not semantically comparable",
        ));
    }
    Ok(values
        .windows(2)
        .any(|pair| pair[0].semantic_cmp_same_domain(pair[1]) == Some(Ordering::Equal)))
}

fn cardinality_allows(cardinality: crate::value::Cardinality, count: usize) -> bool {
    let count = u64::try_from(count).unwrap_or(u64::MAX);
    count >= cardinality.min() && cardinality.max().is_none_or(|maximum| count <= maximum)
}

fn validate_page_contract(
    plan: Option<&QueryPlan>,
    root: BindingId,
    offset: u64,
    limit: u64,
    has_total: bool,
) -> Result<(), Diagnostic> {
    let Some(plan) = plan else {
        return Ok(());
    };
    let Some(crate::query_plan::ModelQueryV2::Page {
        root: expected_root,
        window,
        include_total,
        ..
    }) = plan
        .v2_compatibility()
        .and_then(|compatibility| compatibility.model_query())
    else {
        return Err(remote_evidence_mismatch_v2(
            "hydrated page has no request-bound page contract",
        ));
    };
    if root != *expected_root
        || offset != window.offset()
        || limit != window.limit()
        || has_total != *include_total
    {
        return Err(remote_evidence_mismatch_v2(
            "hydrated page root, window, or total presence contradicts the request",
        ));
    }
    Ok(())
}

fn validate_slot_shapes(
    rows: &[HydratedRowV2],
    output: &crate::query_plan::ModelOutputV2,
) -> Result<(), Diagnostic> {
    for row in rows {
        if row.slots.len() != output.slots().len() {
            return Err(remote_evidence_mismatch_v2(
                "hydrated row arity contradicts the model output",
            ));
        }
        for (slot, expected) in row.slots.iter().zip(output.slots()) {
            let shape_matches = match (slot, expected.collection()) {
                (HydrationSlotV2::Singular { value }, false) => {
                    value.declared() == expected.declared()
                }
                (HydrationSlotV2::Collection { values }, true) => values
                    .iter()
                    .all(|value| value.declared() == expected.declared()),
                _ => false,
            };
            if !shape_matches {
                return Err(remote_evidence_mismatch_v2(
                    "hydrated slot shape or declared descriptor contradicts the model output",
                ));
            }
            if expected.distinct()
                && matches!(slot, HydrationSlotV2::Collection { values } if {
                    let unique = values.iter().collect::<BTreeSet<_>>();
                    unique.len() != values.len()
                })
            {
                return Err(remote_evidence_mismatch_v2(
                    "distinct hydrated collection contains duplicate identities",
                ));
            }
        }
    }
    Ok(())
}

fn validate_page_root_distinct(
    entries: &[HydratedRowV2],
    root: BindingId,
    plan: Option<&QueryPlan>,
) -> Result<(), Diagnostic> {
    let Some(plan) = plan else {
        return Ok(());
    };
    let Some(crate::query_plan::ModelQueryV2::Page {
        root: expected_root,
        output,
        ..
    }) = plan
        .v2_compatibility()
        .and_then(|compatibility| compatibility.model_query())
    else {
        return Err(remote_evidence_mismatch_v2(
            "hydrated page has no page contract",
        ));
    };
    if root != *expected_root {
        return Err(remote_evidence_mismatch_v2(
            "hydrated page root binding contradicts the request",
        ));
    }
    let Some(root_index) = output
        .slots()
        .iter()
        .position(|slot| slot.binding() == root)
    else {
        return Err(remote_evidence_mismatch_v2(
            "hydrated page root is absent from its output",
        ));
    };
    let mut roots = BTreeSet::new();
    for entry in entries {
        let Some(HydrationSlotV2::Singular { value }) = entry.slots.get(root_index) else {
            return Err(remote_evidence_mismatch_v2(
                "hydrated page root slot must be singular",
            ));
        };
        if !roots.insert(value.node()) {
            return Err(remote_evidence_mismatch_v2(
                "hydrated page root identities must be distinct",
            ));
        }
    }
    Ok(())
}

fn validate_scalar_root(
    plan: Option<&QueryPlan>,
    result: RemoteResultKindV2,
    root: BindingId,
) -> Result<(), Diagnostic> {
    let Some(plan) = plan else {
        return Ok(());
    };
    let expected = match (
        result,
        plan.v2_compatibility()
            .and_then(|compatibility| compatibility.model_query()),
    ) {
        (
            RemoteResultKindV2::DistinctCount,
            Some(crate::query_plan::ModelQueryV2::DistinctCount { root, .. }),
        )
        | (
            RemoteResultKindV2::DistinctExists,
            Some(crate::query_plan::ModelQueryV2::DistinctExists { root, .. }),
        ) => *root,
        _ => {
            return Err(remote_evidence_mismatch_v2(
                "distinct-root scalar contradicts the model-query terminal",
            ));
        }
    };
    if root != expected {
        return Err(remote_evidence_mismatch_v2(
            "distinct-root scalar binds the wrong root",
        ));
    }
    Ok(())
}

fn remote_item_limit_v2() -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_v2_item_limit",
        "V2 response exceeds the caller item budget",
    )
}

fn remote_collection_limit_v2() -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_v2_collection_member_limit",
        "V2 response exceeds the aggregate collection-member budget",
    )
}

fn remote_graph_limit_v2() -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_v2_graph_node_limit",
        "V2 hydration graph exceeds the node budget",
    )
}

fn remote_attribute_limit_v2() -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_v2_attribute_value_limit",
        "V2 hydration graph exceeds the attribute-value budget",
    )
}

fn remote_role_player_limit_v2() -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_v2_role_player_limit",
        "V2 hydration graph exceeds the role-player budget",
    )
}

fn remote_evidence_mismatch_v2(message: &'static str) -> Diagnostic {
    remote_v2_failure(
        DiagnosticCategory::Integrity,
        "query_remote_v2_evidence_mismatch",
        message,
    )
}

fn remote_v2_failure(
    category: DiagnosticCategory,
    code: &'static str,
    message: &'static str,
) -> Diagnostic {
    Diagnostic::new(
        category,
        DiagnosticCode::new(code).expect("static V2 remote diagnostic code is canonical"),
        message,
    )
}