type-bridge-server 2.1.0

Query-intercepting proxy server for TypeDB with validation and audit logging
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
//! Versioned V2 query endpoints over the remote plan envelope.
//!
//! V2 shares the configured V1 policy chain, rejects replay before opening a
//! provider transaction, and executes under a mandatory server deadline. The
//! typed request/result bytes remain owned by `type-bridge-contract`.

use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, LazyLock, Mutex};
use std::time::{Duration, Instant};

use axum::Router;
use axum::body::to_bytes;
use axum::extract::{Request, State};
use axum::http::HeaderMap;
use axum::http::header::{CONTENT_LENGTH, CONTENT_TYPE};
use axum::response::{IntoResponse, Response};
use axum::routing::{get, post};
use type_bridge_contract::capability::CapabilitySet;
use type_bridge_contract::diagnostic::{Diagnostic, DiagnosticCategory, DiagnosticCode};
use type_bridge_contract::query_remote::{
    RemoteCapabilities, RemoteCapabilitiesFingerprint, RemoteExecutorBinding, RemoteQueryFailure,
};
#[cfg(test)]
use type_bridge_contract::query_remote::{RemoteReply, decode_remote_reply};
use type_bridge_contract::query_remote_v2::query_remote_v2_required_capabilities;
use type_bridge_contract::schema::{DeclaredSchema, DocumentId};
use type_bridge_contract::schema_delta::ManagedSchemaState;
use type_bridge_orm::Transaction;
use type_bridge_orm::query_v2_prepared::{
    acquire_live_authority_rebuild_permit, verify_managed_query_control_scope,
};
#[cfg(test)]
use type_bridge_orm::query_v2_remote::Ed25519RemoteReplyVerifier;
use type_bridge_orm::query_v2_remote::{
    RemoteReplySigningKey, RemoteRequestFormat, execute_admitted_remote_request_versioned,
    preflight_remote_request_versioned, remote_request_format,
};
use type_bridge_orm::session::backend::{MAX_QUERY_V2_SCHEMA_FENCE_DURATION, QueryV2AnswerLimits};
use type_bridge_orm::session::database::Database;
use type_bridge_query::MigrationAssertionValidationContext;
use type_bridge_schema::{ManagedDeltaContext, ResolvedSchema};
use type_bridge_schema_compat::{LiveQueryControlPresence, rebuild_live_query_authority};

use crate::interceptor::{RequestContext, V2PolicyOutcome, V2PolicyRequest};
use crate::pipeline::QueryPipeline;

const DEFAULT_EXECUTION_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_REPLAY_CAPACITY: usize = 100_000;
const RESPONSE_AUDIT_GRACE: Duration = Duration::from_millis(100);
const TRANSACTION_CLOSE_GRACE: Duration = Duration::from_secs(1);
const V2_PREFLIGHT_CONCURRENCY: usize = 4;

fn new_v2_preflight_slots() -> Arc<tokio::sync::Semaphore> {
    Arc::new(tokio::sync::Semaphore::new(V2_PREFLIGHT_CONCURRENCY))
}

static V2_PREFLIGHT_SLOTS: LazyLock<Arc<tokio::sync::Semaphore>> =
    LazyLock::new(new_v2_preflight_slots);

async fn await_before_deadline<F>(deadline: Instant, future: F) -> Option<F::Output>
where
    F: Future,
{
    tokio::time::timeout_at(tokio::time::Instant::from_std(deadline), future)
        .await
        .ok()
}

async fn close_v2_transaction(transaction: &mut Transaction) -> Result<(), ()> {
    match tokio::time::timeout(TRANSACTION_CLOSE_GRACE, transaction.close()).await {
        Ok(Ok(())) => Ok(()),
        Ok(Err(_)) | Err(_) => Err(()),
    }
}

/// Failure returned by an atomic nonce reservation backend.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReplayStoreError {
    /// The executor epoch has already consumed this nonce.
    Replayed,
    /// The bounded store cannot admit another live reservation.
    Capacity,
    /// The store cannot prove whether the nonce was reserved.
    Unavailable,
}

/// Atomic one-shot nonce registry.
///
/// Multi-instance deployments can inject a shared implementation whose
/// reservation operation is globally atomic, but must inject the matching
/// shared executor epoch through [`V2QueryState::with_shared_execution_epoch`]
/// at the same time. The built-in implementation is process-local and bounded,
/// suitable for the standalone single-node server.
pub trait RemoteReplayStore: Send + Sync {
    /// Atomically reserve `(executor_epoch, nonce)` until `retain_until`.
    ///
    /// Authenticated client identity is deliberately not part of replay
    /// ownership: the same captured request must remain one-shot even when it
    /// is resent under different valid credentials.
    fn reserve<'a>(
        &'a self,
        executor_epoch: &'a str,
        nonce: &'a str,
        retain_until: Instant,
    ) -> Pin<Box<dyn Future<Output = Result<(), ReplayStoreError>> + Send + 'a>>;
}

/// Bounded process-local replay registry used by the standalone server.
pub struct InMemoryReplayStore {
    capacity: usize,
    reservations: Mutex<ReplayReservations>,
}

type ReplayKey = (String, String);

#[derive(Default)]
struct ReplayReservations {
    by_key: HashMap<ReplayKey, Instant>,
    by_expiry: BinaryHeap<Reverse<(Instant, ReplayKey)>>,
}

impl ReplayReservations {
    fn discard_expired(&mut self, now: Instant) {
        while let Some(Reverse((expiry, _))) = self.by_expiry.peek() {
            if *expiry > now {
                break;
            }
            let Some(Reverse((expiry, key))) = self.by_expiry.pop() else {
                break;
            };
            if self
                .by_key
                .get(&key)
                .is_some_and(|stored| *stored == expiry)
            {
                self.by_key.remove(&key);
            }
        }
    }
}

impl InMemoryReplayStore {
    /// Create a registry with an explicit maximum live reservation count.
    #[must_use]
    pub fn new(capacity: usize) -> Self {
        Self {
            capacity,
            reservations: Mutex::new(ReplayReservations::default()),
        }
    }
}

impl RemoteReplayStore for InMemoryReplayStore {
    fn reserve<'a>(
        &'a self,
        executor_epoch: &'a str,
        nonce: &'a str,
        retain_until: Instant,
    ) -> Pin<Box<dyn Future<Output = Result<(), ReplayStoreError>> + Send + 'a>> {
        Box::pin(async move {
            let mut reservations = self
                .reservations
                .lock()
                .map_err(|_| ReplayStoreError::Unavailable)?;
            let now = Instant::now();
            reservations.discard_expired(now);
            let key = (executor_epoch.to_owned(), nonce.to_owned());
            if reservations.by_key.contains_key(&key) {
                return Err(ReplayStoreError::Replayed);
            }
            if reservations.by_key.len() >= self.capacity {
                return Err(ReplayStoreError::Capacity);
            }
            reservations.by_key.insert(key.clone(), retain_until);
            reservations.by_expiry.push(Reverse((retain_until, key)));
            Ok(())
        })
    }
}

/// Everything one V2 query executor needs to serve envelopes.
pub struct V2QueryState {
    advertisement: RemoteCapabilities,
    advertisement_fingerprint: RemoteCapabilitiesFingerprint,
    ceilings: QueryV2AnswerLimits,
    database: Database,
    declared: DeclaredSchema,
    delta_context: ManagedDeltaContext,
    managed: ManagedSchemaState,
    resolved: ResolvedSchema,
    execution_timeout: Duration,
    replay_store: Arc<dyn RemoteReplayStore>,
    signer: RemoteReplySigningKey,
    control_policy: V2QueryControlPolicy,
}

#[derive(Clone, Copy)]
enum V2QueryControlPolicy {
    Managed,
    QueryOnly,
}

impl V2QueryState {
    /// Construct fail-closed V2 state with mandatory execution/replay bounds.
    pub fn new(
        advertised: CapabilitySet,
        ceilings: QueryV2AnswerLimits,
        database: Database,
        declared: DeclaredSchema,
        delta_context: ManagedDeltaContext,
        managed: ManagedSchemaState,
        resolved: ResolvedSchema,
    ) -> Result<Self, Diagnostic> {
        Self::new_with_control_policy(
            advertised,
            ceilings,
            database,
            declared,
            delta_context,
            managed,
            resolved,
            V2QueryControlPolicy::Managed,
        )
    }

    /// Construct fail-closed V2 state for an explicitly query-only database.
    ///
    /// This mode rejects both V2 and legacy migration-control partitions. It
    /// cannot be inferred from missing control facts on a managed database.
    pub fn new_query_only(
        advertised: CapabilitySet,
        ceilings: QueryV2AnswerLimits,
        database: Database,
        declared: DeclaredSchema,
        delta_context: ManagedDeltaContext,
        managed: ManagedSchemaState,
        resolved: ResolvedSchema,
    ) -> Result<Self, Diagnostic> {
        Self::new_with_control_policy(
            advertised,
            ceilings,
            database,
            declared,
            delta_context,
            managed,
            resolved,
            V2QueryControlPolicy::QueryOnly,
        )
    }

    #[allow(clippy::too_many_arguments)]
    fn new_with_control_policy(
        mut advertised: CapabilitySet,
        ceilings: QueryV2AnswerLimits,
        database: Database,
        declared: DeclaredSchema,
        delta_context: ManagedDeltaContext,
        managed: ManagedSchemaState,
        resolved: ResolvedSchema,
        control_policy: V2QueryControlPolicy,
    ) -> Result<Self, Diagnostic> {
        // Model evidence is constructed by the sole same-snapshot
        // compatibility executor. Its graph, attribute-value, and role-player
        // ceilings are fixed at the canonical collection ceiling; request
        // values may only tighten them during admission.
        for capability in query_remote_v2_required_capabilities(false) {
            advertised.insert(capability);
        }
        let executor = standalone_executor_binding();
        let signer = RemoteReplySigningKey::generate();
        let advertisement = RemoteCapabilities::new(advertised, executor, signer.public_key());
        let advertisement_fingerprint = advertisement.fingerprint()?;
        Ok(Self {
            advertisement,
            advertisement_fingerprint,
            ceilings,
            database,
            declared,
            delta_context,
            managed,
            resolved,
            execution_timeout: DEFAULT_EXECUTION_TIMEOUT,
            replay_store: Arc::new(InMemoryReplayStore::new(DEFAULT_REPLAY_CAPACITY)),
            signer,
            control_policy,
        })
    }

    /// Replace the replay registry without changing this standalone epoch.
    ///
    /// This does not enable load balancing because peer states still advertise
    /// different epochs. Multi-instance callers must use
    /// [`Self::with_shared_execution_epoch`] instead.
    #[must_use]
    pub fn with_replay_store(mut self, replay_store: Arc<dyn RemoteReplayStore>) -> Self {
        self.replay_store = replay_store;
        self
    }

    /// Configure one shared executor epoch together with its atomic replay store.
    ///
    /// This is the only API that lets multiple state instances advertise the
    /// same identity/epoch. Supplying the epoch and replay authority together
    /// prevents a load-balanced deployment from accidentally claiming shared
    /// replay semantics while retaining process-local nonce state.
    pub fn with_shared_execution_epoch(
        mut self,
        advertisement: RemoteCapabilities,
        replay_store: Arc<dyn RemoteReplayStore>,
        signer: RemoteReplySigningKey,
    ) -> Result<Self, Diagnostic> {
        if advertisement.capabilities() != self.advertisement.capabilities()
            || advertisement.reply_key() != signer.public_key()
        {
            return Err(diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_signer_mismatch",
                "shared executor advertisement does not bind the supplied reply signer",
            ));
        }
        let advertisement_fingerprint = advertisement.fingerprint()?;
        self.advertisement = advertisement;
        self.advertisement_fingerprint = advertisement_fingerprint;
        self.replay_store = replay_store;
        self.signer = signer;
        Ok(self)
    }

    /// Tighten the mandatory server execution duration.
    #[must_use]
    pub fn with_execution_timeout(mut self, timeout: Duration) -> Self {
        self.execution_timeout = timeout;
        self
    }

    fn signed_failure(&self, failure: &RemoteQueryFailure) -> Vec<u8> {
        failure.encode_signed_or_fallback(&self.advertisement_fingerprint, &self.signer)
    }

    fn unbound_failure(&self, diagnostic: &Diagnostic) -> Vec<u8> {
        self.signed_failure(&RemoteQueryFailure::new(None, diagnostic))
    }

    fn unbound_failure_for(&self, format: RemoteRequestFormat, diagnostic: &Diagnostic) -> Vec<u8> {
        match format {
            RemoteRequestFormat::V1 => self.unbound_failure(diagnostic),
            RemoteRequestFormat::V2 => {
                type_bridge_orm::query_v2_remote::encode_unbound_remote_failure_v2(
                    None,
                    diagnostic,
                    &self.advertisement_fingerprint,
                    &self.signer,
                )
            }
        }
    }

    async fn verify_live_authority(&self) -> Result<(), Diagnostic> {
        self.verify_database_profile()?;
        let rebuild_permit = acquire_live_authority_rebuild_permit().await.map_err(|_| {
            diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_live_schema_unavailable",
                "the executor could not reserve live-schema verification capacity",
            )
        })?;
        let export = self.database.schema_text().await.map_err(|_| {
            diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_live_schema_unavailable",
                "the executor could not verify its live schema authority",
            )
        })?;
        self.verify_live_authority_export_with_permit(export, rebuild_permit)
            .await
    }

    fn verify_database_profile(&self) -> Result<(), Diagnostic> {
        let server_version = self.database.server_version().ok_or_else(|| {
            diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_server_identity_unavailable",
                "the executor cannot prove the exact TypeDB semantic profile",
            )
        })?;
        let observed_profile = type_bridge_contract::fingerprint::SemanticProfileId::new(
            type_bridge_core_lib::version::semantic_profile_id(&server_version).ok_or_else(
                || {
                    diagnostic(
                        DiagnosticCategory::Integrity,
                        "query_remote_server_profile_unsupported",
                        "the executor TypeDB version has no supported semantic profile",
                    )
                },
            )?,
        )?;
        if &observed_profile != self.delta_context.semantic_profile() {
            return Err(diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_semantic_profile_mismatch",
                "the executor TypeDB semantic profile differs from its declared authority",
            ));
        }
        Ok(())
    }

    async fn verify_live_authority_export_with_permit(
        &self,
        export: String,
        rebuild_permit: tokio::sync::OwnedSemaphorePermit,
    ) -> Result<(), Diagnostic> {
        let document = DocumentId::new("typebridge-v2-request-live-export.typeql")?;
        let declared = self.declared.clone();
        let context = self.delta_context.clone();
        let live = tokio::task::spawn_blocking(move || {
            let _rebuild_permit = rebuild_permit;
            rebuild_live_query_authority(document, &export, &declared, &context)
        })
        .await
        .map_err(|_| {
            diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_live_schema_invalid",
                "the executor live schema cannot form trusted query authority",
            )
        })?
        .map_err(|_| {
            diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_live_schema_invalid",
                "the executor live schema cannot form trusted query authority",
            )
        })?;
        if live.managed() != &self.managed {
            return Err(diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_stale_schema",
                "the executor live schema no longer matches its declared authority",
            ));
        }
        match (self.control_policy, live.control_presence()) {
            (V2QueryControlPolicy::Managed, LiveQueryControlPresence::ManagedFence)
            | (V2QueryControlPolicy::QueryOnly, LiveQueryControlPresence::Absent) => {}
            (V2QueryControlPolicy::Managed, LiveQueryControlPresence::Absent) => {
                return Err(diagnostic(
                    DiagnosticCategory::Integrity,
                    "query_remote_managed_control_missing",
                    "the managed executor database has no migration control authority",
                ));
            }
            (V2QueryControlPolicy::QueryOnly, LiveQueryControlPresence::ManagedFence) => {
                return Err(diagnostic(
                    DiagnosticCategory::Integrity,
                    "query_remote_query_only_control_present",
                    "the query-only executor database unexpectedly has managed control authority",
                ));
            }
            (_, LiveQueryControlPresence::ManagedFenceWithExtensions) => {
                return Err(diagnostic(
                    DiagnosticCategory::Integrity,
                    "query_remote_live_schema_invalid",
                    "the executor managed control schema carries unsupported released-only extensions",
                ));
            }
        }
        if matches!(self.control_policy, V2QueryControlPolicy::QueryOnly)
            && live.legacy_control_present()
        {
            return Err(diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_query_only_legacy_control_present",
                "the query-only executor database unexpectedly has legacy migration control authority",
            ));
        }
        Ok(())
    }

    /// Prove the configured authority under one bounded schema fence before
    /// exposing this state through a standalone server.
    pub async fn verify_startup_authority(&self) -> Result<(), Diagnostic> {
        let deadline = Instant::now()
            .checked_add(DEFAULT_EXECUTION_TIMEOUT)
            .ok_or_else(|| {
                diagnostic(
                    DiagnosticCategory::ResourceLimit,
                    "transaction_deadline_exceeded",
                    "provider transaction deadline expired",
                )
            })?;
        self.verify_authority_before(deadline).await
    }

    async fn verify_authority_before(&self, deadline: Instant) -> Result<(), Diagnostic> {
        match await_before_deadline(deadline, self.verify_live_authority()).await {
            Some(result) => result?,
            None => {
                return Err(diagnostic(
                    DiagnosticCategory::ResourceLimit,
                    "transaction_deadline_exceeded",
                    "provider transaction deadline expired",
                ));
            }
        }
        let rebuild_permit =
            await_before_deadline(deadline, acquire_live_authority_rebuild_permit())
                .await
                .ok_or_else(|| {
                    diagnostic(
                        DiagnosticCategory::ResourceLimit,
                        "transaction_deadline_exceeded",
                        "provider transaction deadline expired",
                    )
                })?
                .map_err(|_| {
                    diagnostic(
                        DiagnosticCategory::Integrity,
                        "query_remote_live_schema_unavailable",
                        "the executor could not reserve live-schema verification capacity",
                    )
                })?;
        let fence_timeout = deadline
            .saturating_duration_since(Instant::now())
            .min(MAX_QUERY_V2_SCHEMA_FENCE_DURATION);
        if fence_timeout.is_zero() {
            return Err(diagnostic(
                DiagnosticCategory::ResourceLimit,
                "transaction_deadline_exceeded",
                "provider transaction deadline expired",
            ));
        }
        let open = self.database.schema_fenced_read_transaction(fence_timeout);
        let (mut transaction, fenced_schema) = await_before_deadline(deadline, open)
            .await
            .ok_or_else(|| {
                diagnostic(
                    DiagnosticCategory::ResourceLimit,
                    "transaction_deadline_exceeded",
                    "provider transaction deadline expired",
                )
            })?
            .map_err(|_| {
                diagnostic(
                    DiagnosticCategory::Integrity,
                    "query_remote_provider_unavailable",
                    "the executor could not open a schema-fenced startup transaction",
                )
            })?;
        let verified = async {
            await_before_deadline(
                deadline,
                self.verify_live_authority_export_with_permit(fenced_schema, rebuild_permit),
            )
            .await
            .ok_or_else(|| {
                diagnostic(
                    DiagnosticCategory::ResourceLimit,
                    "transaction_deadline_exceeded",
                    "provider transaction deadline expired",
                )
            })??;
            if matches!(self.control_policy, V2QueryControlPolicy::Managed) {
                verify_managed_query_control_scope(
                    &mut transaction,
                    self.delta_context.scope_id(),
                    Some(deadline),
                    &self.ceilings.answer.cancellation,
                )
                .await?;
            }
            Ok::<(), Diagnostic>(())
        }
        .await;
        let closed = close_v2_transaction(&mut transaction).await;
        verified?;
        if closed.is_err() {
            return Err(diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_transaction_close_failed",
                "the executor could not close its startup authority transaction",
            ));
        }
        Ok(())
    }
}

fn standalone_executor_binding() -> RemoteExecutorBinding {
    let identity = format!("standalone-{}", uuid::Uuid::new_v4().simple());
    let epoch = uuid::Uuid::new_v4().simple().to_string();
    RemoteExecutorBinding::new(identity, epoch)
        .expect("generated standalone executor identity and epoch are canonical")
}

struct V2RouterState {
    pipeline: Arc<QueryPipeline>,
    query: Arc<V2QueryState>,
}

/// The remote envelope has its own 32 MiB owning-format budget plus framing
/// slack for the HTTP body collector.
const V2_BODY_LIMIT_BYTES: usize =
    type_bridge_contract::limits::MAX_REMOTE_ENVELOPE_BYTES + 64 * 1024;

fn declared_v2_body_oversized(headers: &HeaderMap) -> bool {
    let Ok(limit) = u64::try_from(V2_BODY_LIMIT_BYTES) else {
        return false;
    };
    headers.get_all(CONTENT_LENGTH).iter().any(|value| {
        value
            .to_str()
            .ok()
            .and_then(|value| value.parse::<u64>().ok())
            .is_some_and(|declared| declared > limit)
    })
}

/// Build the V2 route surface under the same policy pipeline as V1.
pub fn create_v2_router(pipeline: Arc<QueryPipeline>, query: Arc<V2QueryState>) -> Router {
    let state = Arc::new(V2RouterState { pipeline, query });
    Router::new()
        .route("/v2/query", post(handle_v2_query))
        .route("/v2/capabilities", get(handle_v2_capabilities))
        .with_state(state)
}

/// Build the complete router: retained V1 routes beside the V2 surface.
pub fn create_router_with_v2(pipeline: Arc<QueryPipeline>, v2: Arc<V2QueryState>) -> Router {
    super::http::create_router(Arc::clone(&pipeline)).merge(create_v2_router(pipeline, v2))
}

async fn handle_v2_query(State(state): State<Arc<V2RouterState>>, request: Request) -> Response {
    let mut ceilings = state.query.ceilings.clone();
    let mandatory_deadline = match Instant::now().checked_add(state.query.execution_timeout) {
        Some(deadline) => deadline,
        None => {
            return envelope_response(state.query.unbound_failure(&deadline_exceeded()));
        }
    };
    ceilings.answer.deadline = Some(
        ceilings
            .answer
            .deadline
            .map_or(mandatory_deadline, |deadline| {
                deadline.min(mandatory_deadline)
            }),
    );

    // Authenticate/rate-limit from request parts before buffering or decoding
    // attacker-controlled envelope bytes. Legacy-AST-only policies fail
    // closed instead of receiving a misleading empty query.
    let metadata = v2_request_metadata(request.headers(), "query");
    let mut policy_context = match state.pipeline.v2_request_context(metadata) {
        Ok(context) => context,
        Err(_) => {
            return envelope_response(state.query.unbound_failure(&policy_rejected()));
        }
    };
    match await_before_deadline(
        mandatory_deadline,
        state.pipeline.begin_v2_request(&mut policy_context),
    )
    .await
    {
        Some(Ok(())) => {}
        Some(Err(_)) => {
            let failure = state.query.unbound_failure(&policy_rejected());
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "query_remote_policy_rejected",
                failure.clone(),
                failure,
                state.query.unbound_failure(&deadline_exceeded()),
            )
            .await;
        }
        None => {
            let deadline_failure = state.query.unbound_failure(&deadline_exceeded());
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                state.query.unbound_failure(&policy_rejected()),
                deadline_failure,
            )
            .await;
        }
    }

    if declared_v2_body_oversized(request.headers()) {
        let failure = state.query.unbound_failure(&body_oversized());
        return finish_v2_failure(
            &state,
            mandatory_deadline,
            &policy_context,
            "query_remote_envelope_oversized",
            failure.clone(),
            state.query.unbound_failure(&policy_rejected()),
            state.query.unbound_failure(&deadline_exceeded()),
        )
        .await;
    }

    // One owned admission covers both attacker-controlled body buffering and
    // provider-independent blocking work. If any async stage returns early,
    // the local permit drops; once moved, the blocking task owns its release.
    let permit = match await_before_deadline(
        mandatory_deadline,
        Arc::clone(&V2_PREFLIGHT_SLOTS).acquire_owned(),
    )
    .await
    {
        Some(Ok(permit)) => permit,
        Some(Err(_)) => {
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "query_remote_preflight_unavailable",
                state.query.unbound_failure(&unavailable()),
                state.query.unbound_failure(&policy_rejected()),
                state.query.unbound_failure(&deadline_exceeded()),
            )
            .await;
        }
        None => {
            let failure = state.query.unbound_failure(&deadline_exceeded());
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                failure.clone(),
                state.query.unbound_failure(&policy_rejected()),
                failure,
            )
            .await;
        }
    };

    let body = match await_before_deadline(
        mandatory_deadline,
        to_bytes(request.into_body(), V2_BODY_LIMIT_BYTES),
    )
    .await
    {
        Some(Ok(body)) => body,
        Some(Err(_)) => {
            let failure = state.query.unbound_failure(&body_oversized());
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "query_remote_envelope_oversized",
                failure.clone(),
                state.query.unbound_failure(&policy_rejected()),
                state.query.unbound_failure(&deadline_exceeded()),
            )
            .await;
        }
        None => {
            let deadline_failure = state.query.unbound_failure(&deadline_exceeded());
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                state.query.unbound_failure(&policy_rejected()),
                deadline_failure,
            )
            .await;
        }
    };
    // Select the additive payload version from the explicit discriminator
    // before either decoder reconstructs a plan or invocation. Unknown and
    // malformed formats intentionally retain the historical V1 rejection
    // path.
    let request_format = remote_request_format(&body);

    // Contract/schema/capability work is provider-independent and runs in a
    // bounded blocking pool. The owned permit remains in the blocking task if
    // the async request deadline elapses, preventing detached CPU saturation.
    let resolved = state.query.resolved.clone();
    let managed = state.query.managed.clone();
    let advertisement = state.query.advertisement.clone();
    let preflight = tokio::task::spawn_blocking(move || {
        let _permit = permit;
        let context = MigrationAssertionValidationContext::new(&resolved, &managed);
        preflight_remote_request_versioned(&body, &context, &advertisement, ceilings)
            .map_err(Box::new)
    });
    let admitted = match await_before_deadline(mandatory_deadline, preflight).await {
        Some(Ok(Ok(admitted))) => admitted,
        Some(Ok(Err(rejection))) => {
            let rejection = *rejection;
            let code = rejection.diagnostic_code().to_owned();
            let failure = rejection
                .into_failure_envelope(&state.query.advertisement_fingerprint, &state.query.signer);
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                &code,
                failure.clone(),
                state
                    .query
                    .unbound_failure_for(request_format, &policy_rejected()),
                failure,
            )
            .await;
        }
        Some(Err(_)) => {
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "query_remote_preflight_unavailable",
                state
                    .query
                    .unbound_failure_for(request_format, &unavailable()),
                state
                    .query
                    .unbound_failure_for(request_format, &policy_rejected()),
                state
                    .query
                    .unbound_failure_for(request_format, &deadline_exceeded()),
            )
            .await;
        }
        None => {
            let failure = state
                .query
                .unbound_failure_for(request_format, &deadline_exceeded());
            return finish_v2_failure(
                &state,
                mandatory_deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                failure.clone(),
                state
                    .query
                    .unbound_failure_for(request_format, &policy_rejected()),
                failure,
            )
            .await;
        }
    };
    let deadline = admitted.deadline().unwrap_or(mandatory_deadline);
    let deadline_failure = admitted.bound_failure(&deadline_exceeded(), &state.query.signer);
    let response_policy_failure = admitted.bound_failure(&policy_rejected(), &state.query.signer);

    // Preserve released V1 precedence and policy observability byte-for-byte:
    // V1 plan authorization historically ran before replay reservation.
    if request_format == RemoteRequestFormat::V1 {
        let policy_request = V2PolicyRequest::new(admitted.plan(), admitted.invocation());
        match await_before_deadline(
            deadline,
            state
                .pipeline
                .authorize_v2_request(&policy_request, &mut policy_context),
        )
        .await
        {
            Some(Ok(())) => {}
            Some(Err(_)) => {
                return finish_v2_failure(
                    &state,
                    deadline,
                    &policy_context,
                    "query_remote_policy_rejected",
                    response_policy_failure.clone(),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
            None => {
                return finish_v2_failure(
                    &state,
                    deadline,
                    &policy_context,
                    "transaction_deadline_exceeded",
                    deadline_failure.clone(),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
        }
    }

    // V2 reserves the request nonce before plan-level policy callbacks.
    // Transport authentication already ran before the body was collected, but
    // a replay must not invoke request-specific application hooks or construct
    // any live schema/provider/model host state.
    let retain_until = admitted.replay_until();
    match await_before_deadline(
        deadline,
        state.query.replay_store.reserve(
            state.query.advertisement.executor().epoch(),
            admitted.nonce(),
            retain_until,
        ),
    )
    .await
    {
        Some(Ok(())) => {}
        Some(Err(error)) => {
            let diagnostic = match error {
                ReplayStoreError::Replayed => replayed(),
                ReplayStoreError::Capacity => replay_capacity(),
                ReplayStoreError::Unavailable => replay_unavailable(),
            };
            let code = diagnostic.code().as_str().to_owned();
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                &code,
                admitted.bound_failure(&diagnostic, &state.query.signer),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
        None => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
    }

    if request_format == RemoteRequestFormat::V2 {
        let policy_request = V2PolicyRequest::new(admitted.plan(), admitted.invocation());
        match await_before_deadline(
            deadline,
            state
                .pipeline
                .authorize_v2_request(&policy_request, &mut policy_context),
        )
        .await
        {
            Some(Ok(())) => {}
            Some(Err(_)) => {
                return finish_v2_failure(
                    &state,
                    deadline,
                    &policy_context,
                    "query_remote_policy_rejected",
                    response_policy_failure.clone(),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
            None => {
                return finish_v2_failure(
                    &state,
                    deadline,
                    &policy_context,
                    "transaction_deadline_exceeded",
                    deadline_failure.clone(),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
        }
    }

    // A unique, authenticated request may force one bounded schema export, but
    // never a provider transaction unless current live authority has been
    // proved.
    match await_before_deadline(deadline, state.query.verify_live_authority()).await {
        Some(Ok(())) => {}
        Some(Err(diagnostic)) => {
            let code = diagnostic.code().as_str().to_owned();
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                &code,
                admitted.bound_failure(&diagnostic, &state.query.signer),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
        None => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
    }

    let rebuild_permit =
        match await_before_deadline(deadline, acquire_live_authority_rebuild_permit()).await {
            Some(Ok(permit)) => permit,
            Some(Err(_)) => {
                let diagnostic = diagnostic(
                    DiagnosticCategory::Integrity,
                    "query_remote_live_schema_unavailable",
                    "the executor could not reserve live-schema verification capacity",
                );
                let code = diagnostic.code().as_str().to_owned();
                return finish_v2_failure(
                    &state,
                    deadline,
                    &policy_context,
                    &code,
                    admitted.bound_failure(&diagnostic, &state.query.signer),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
            None => {
                return finish_v2_failure(
                    &state,
                    deadline,
                    &policy_context,
                    "transaction_deadline_exceeded",
                    deadline_failure.clone(),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
        };

    let fence_timeout = deadline
        .saturating_duration_since(Instant::now())
        .min(MAX_QUERY_V2_SCHEMA_FENCE_DURATION);
    if fence_timeout.is_zero() {
        return finish_v2_failure(
            &state,
            deadline,
            &policy_context,
            "transaction_deadline_exceeded",
            deadline_failure.clone(),
            response_policy_failure,
            deadline_failure,
        )
        .await;
    }
    let open = state
        .query
        .database
        .schema_fenced_read_transaction(fence_timeout);
    let opened = match await_before_deadline(deadline, open).await {
        Some(result) => result,
        None => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
    };
    let (mut transaction, fenced_schema) = match opened {
        Ok(fenced) => fenced,
        Err(_) => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "query_remote_provider_unavailable",
                admitted.bound_failure(&unavailable(), &state.query.signer),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
    };

    // The backend captured this export while the bounded schema-exclusion
    // guard was held, so it is the transaction's exact schema authority.
    match await_before_deadline(
        deadline,
        state
            .query
            .verify_live_authority_export_with_permit(fenced_schema, rebuild_permit),
    )
    .await
    {
        Some(Ok(())) => {}
        Some(Err(diagnostic)) => {
            let code = diagnostic.code().as_str().to_owned();
            return finish_v2_failure_after_transaction_close(
                &state,
                &mut transaction,
                deadline,
                &policy_context,
                &code,
                admitted.bound_failure(&diagnostic, &state.query.signer),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
        None => {
            return finish_v2_failure_after_transaction_close(
                &state,
                &mut transaction,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
    }
    if matches!(state.query.control_policy, V2QueryControlPolicy::Managed) {
        let control = verify_managed_query_control_scope(
            &mut transaction,
            state.query.delta_context.scope_id(),
            Some(deadline),
            &state.query.ceilings.answer.cancellation,
        );
        match await_before_deadline(deadline, control).await {
            Some(Ok(())) => {}
            Some(Err(diagnostic)) => {
                let code = diagnostic.code().as_str().to_owned();
                return finish_v2_failure_after_transaction_close(
                    &state,
                    &mut transaction,
                    deadline,
                    &policy_context,
                    &code,
                    admitted.bound_failure(&diagnostic, &state.query.signer),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
            None => {
                return finish_v2_failure_after_transaction_close(
                    &state,
                    &mut transaction,
                    deadline,
                    &policy_context,
                    "transaction_deadline_exceeded",
                    deadline_failure.clone(),
                    response_policy_failure,
                    deadline_failure,
                )
                .await;
            }
        }
    }
    let reply_expectation = admitted.reply_expectation();
    let execute =
        execute_admitted_remote_request_versioned(admitted, &mut transaction, &state.query.signer);
    let bytes = match await_before_deadline(deadline, execute).await {
        Some(bytes) => bytes,
        None => {
            return finish_v2_failure_after_transaction_close(
                &state,
                &mut transaction,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                response_policy_failure,
                deadline_failure,
            )
            .await;
        }
    };

    let (bytes, success, code) = match reply_expectation.audit(
        &bytes,
        &state.query.advertisement_fingerprint,
        state.query.advertisement.reply_key(),
    ) {
        Ok(audited) => (bytes, audited.success(), audited.code().to_owned()),
        Err(_) => {
            let diagnostic = internal_response_invalid();
            (
                reply_expectation.bound_internal_failure(
                    &diagnostic,
                    &state.query.advertisement_fingerprint,
                    &state.query.signer,
                ),
                false,
                diagnostic.code().as_str().to_owned(),
            )
        }
    };
    if close_v2_transaction(&mut transaction).await.is_err() {
        tracing::warn!(
            primary_code = %code,
            cleanup_code = "query_remote_transaction_close_failed",
            "remote query transaction cleanup failed"
        );
    }
    let outcome = V2PolicyOutcome::new(success, &code, bytes.len());
    match await_before_deadline(
        response_audit_deadline(deadline),
        state.pipeline.finish_v2_request(&outcome, &policy_context),
    )
    .await
    {
        Some(Ok(())) => {}
        Some(Err(_)) => return envelope_response(response_policy_failure),
        None => return envelope_response(deadline_failure),
    }
    envelope_response(bytes)
}

async fn finish_v2_failure(
    state: &V2RouterState,
    deadline: Instant,
    context: &RequestContext,
    code: &str,
    primary: Vec<u8>,
    policy_rejection: Vec<u8>,
    deadline_rejection: Vec<u8>,
) -> Response {
    let outcome = V2PolicyOutcome::new(false, code, primary.len());
    match await_before_deadline(
        response_audit_deadline(deadline),
        state.pipeline.finish_v2_request(&outcome, context),
    )
    .await
    {
        Some(Ok(())) => envelope_response(primary),
        Some(Err(_)) => envelope_response(policy_rejection),
        None => envelope_response(deadline_rejection),
    }
}

#[allow(clippy::too_many_arguments)]
async fn finish_v2_failure_after_transaction_close(
    state: &V2RouterState,
    transaction: &mut Transaction,
    deadline: Instant,
    context: &RequestContext,
    code: &str,
    primary: Vec<u8>,
    policy_rejection: Vec<u8>,
    deadline_rejection: Vec<u8>,
) -> Response {
    if close_v2_transaction(transaction).await.is_err() {
        tracing::warn!(
            primary_code = code,
            cleanup_code = "query_remote_transaction_close_failed",
            "remote query failed and transaction cleanup also failed"
        );
    }
    finish_v2_failure(
        state,
        deadline,
        context,
        code,
        primary,
        policy_rejection,
        deadline_rejection,
    )
    .await
}

fn response_audit_deadline(request_deadline: Instant) -> Instant {
    Instant::now()
        .checked_add(RESPONSE_AUDIT_GRACE)
        .unwrap_or(request_deadline)
}

#[cfg(test)]
fn bound_internal_response_failure(
    state: &V2QueryState,
    nonce: &str,
    request: &type_bridge_contract::query_remote::RemoteRequestFingerprint,
) -> (Vec<u8>, bool, String) {
    let diagnostic = internal_response_invalid();
    (
        RemoteQueryFailure::bound(nonce.to_owned(), request, &diagnostic)
            .encode_signed_or_fallback(&state.advertisement_fingerprint, &state.signer),
        false,
        diagnostic.code().as_str().to_owned(),
    )
}

fn v2_request_metadata(
    headers: &HeaderMap,
    endpoint: &'static str,
) -> HashMap<String, serde_json::Value> {
    // Authentication and tenant policies commonly consume transport
    // credentials rather than query-AST fields. Keep them outside the canonical
    // plan envelope, but expose every valid textual HTTP header to the same
    // interceptor context as V1 body metadata. The built-in audit interceptor
    // records only its fixed allowlist and never serializes this map.
    let mut values = serde_json::Map::new();
    for name in headers.keys() {
        let entries = headers
            .get_all(name)
            .iter()
            .filter_map(|value| value.to_str().ok().map(str::to_owned))
            .map(serde_json::Value::String)
            .collect::<Vec<_>>();
        if !entries.is_empty() {
            values.insert(name.as_str().to_owned(), serde_json::Value::Array(entries));
        }
    }
    HashMap::from([
        ("transport".to_owned(), serde_json::json!("v2")),
        ("v2_endpoint".to_owned(), serde_json::json!(endpoint)),
        ("query_format".to_owned(), serde_json::json!("typed-plan")),
        ("http_headers".to_owned(), serde_json::Value::Object(values)),
    ])
}

async fn handle_v2_capabilities(
    State(state): State<Arc<V2RouterState>>,
    request: Request,
) -> Response {
    let Some(deadline) = Instant::now().checked_add(state.query.execution_timeout) else {
        return envelope_response(state.query.unbound_failure(&deadline_exceeded()));
    };

    // Discovery is cheap only after the configured V2 transport policy has
    // authenticated/rate-limited it. In particular, rejected discovery never
    // reaches the live schema exporter below.
    let metadata = v2_request_metadata(request.headers(), "capabilities");
    let mut policy_context = match state.pipeline.v2_request_context(metadata) {
        Ok(context) => context,
        Err(_) => {
            return envelope_response(state.query.unbound_failure(&policy_rejected()));
        }
    };
    let policy_failure = state.query.unbound_failure(&policy_rejected());
    let deadline_failure = state.query.unbound_failure(&deadline_exceeded());
    match await_before_deadline(
        deadline,
        state.pipeline.begin_v2_request(&mut policy_context),
    )
    .await
    {
        Some(Ok(())) => {}
        Some(Err(_)) => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "query_remote_policy_rejected",
                policy_failure.clone(),
                policy_failure,
                deadline_failure,
            )
            .await;
        }
        None => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                policy_failure,
                deadline_failure,
            )
            .await;
        }
    }

    // Discovery proves that the advertised executor still has the configured
    // live schema/profile authority, but does not open an execution
    // transaction. Exact schema fencing and managed-control admission belong
    // to startup and to the query transaction that consumes them.
    match await_before_deadline(deadline, state.query.verify_live_authority()).await {
        Some(Ok(())) => {}
        Some(Err(diagnostic)) => {
            let code = diagnostic.code().as_str().to_owned();
            let failure = state.query.unbound_failure(&diagnostic);
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                &code,
                failure,
                policy_failure,
                deadline_failure,
            )
            .await;
        }
        None => {
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                "transaction_deadline_exceeded",
                deadline_failure.clone(),
                policy_failure,
                deadline_failure,
            )
            .await;
        }
    }
    let bytes = match state.query.advertisement.encode() {
        Ok(bytes) => bytes,
        Err(diagnostic) => {
            let code = diagnostic.code().as_str().to_owned();
            let failure = state.query.unbound_failure(&diagnostic);
            return finish_v2_failure(
                &state,
                deadline,
                &policy_context,
                &code,
                failure,
                policy_failure,
                deadline_failure,
            )
            .await;
        }
    };
    let outcome = V2PolicyOutcome::new(true, "ok", bytes.len());
    match await_before_deadline(
        response_audit_deadline(deadline),
        state.pipeline.finish_v2_request(&outcome, &policy_context),
    )
    .await
    {
        Some(Ok(())) => envelope_response(bytes),
        Some(Err(_)) => envelope_response(policy_failure),
        None => envelope_response(deadline_failure),
    }
}

fn envelope_response(bytes: Vec<u8>) -> Response {
    ([(CONTENT_TYPE, "application/json")], bytes).into_response()
}

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

fn unavailable() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::Integrity,
        "query_remote_provider_unavailable",
        "the executor could not open a read transaction",
    )
}

fn policy_rejected() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::Integrity,
        "query_remote_policy_rejected",
        "the executor policy rejected the request",
    )
}

fn replayed() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::Integrity,
        "query_remote_replay",
        "the request nonce was already consumed",
    )
}

fn replay_capacity() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::ResourceLimit,
        "query_remote_replay_capacity",
        "the replay registry cannot admit another request",
    )
}

fn replay_unavailable() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::Integrity,
        "query_remote_replay_unavailable",
        "the replay registry could not prove nonce uniqueness",
    )
}

fn deadline_exceeded() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::ResourceLimit,
        "transaction_deadline_exceeded",
        "provider transaction deadline expired",
    )
}

fn body_oversized() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::ResourceLimit,
        "query_remote_envelope_oversized",
        "remote request envelope exceeds the transport byte ceiling",
    )
}

fn internal_response_invalid() -> Diagnostic {
    diagnostic(
        DiagnosticCategory::Integrity,
        "query_remote_internal_response_invalid",
        "the executor produced invalid bound response evidence",
    )
}

#[cfg(test)]
mod tests {
    use std::collections::VecDeque;
    use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

    use axum::body::Body;
    use axum::http::StatusCode;
    use http_body_util::BodyExt;
    use tower::ServiceExt;
    use type_bridge_contract::capability::CapabilityId;
    use type_bridge_contract::codec::FormatVersion;
    use type_bridge_contract::fingerprint::SemanticProfileId;
    use type_bridge_contract::id::{AttributeId, TypeId, TypeKind};
    use type_bridge_contract::limits::{
        MAX_CANONICAL_COLLECTION_LEN, MAX_REMOTE_ENVELOPE_BYTES, StructuralLimits,
    };
    use type_bridge_contract::managed_scope::ManagedScopeId;
    use type_bridge_contract::migration_assertion::{AssertionBinding, BindingId, QueryVariable};
    use type_bridge_contract::query_plan::{
        DocumentField, DocumentSource, QueryInvocation, QueryOperation, QueryOutput, QueryPattern,
        QueryPlan, QueryPlanFingerprint, ReadStage,
    };
    use type_bridge_contract::query_plan::{
        query_plan_capability_vocabulary, query_plan_v2_capability_vocabulary,
    };
    use type_bridge_contract::query_remote::{
        RemoteLimits, RemoteOutcome, RemoteOutcomeShape, RemoteQueryResponse,
        RemoteReplyDecodeLimits, RemoteRequestFingerprint,
    };
    use type_bridge_contract::query_remote_v2::{
        RemoteLimitsV2, RemoteQueryRequestV2, RemoteReplyDecodeLimitsV2, RemoteReplyV2,
        RemoteRequestFingerprintV2, decode_remote_reply_v2, decode_signed_remote_failure_v2,
    };
    use type_bridge_contract::schema::{
        OwnsFact, OwnsFactId, SchemaFact, SourceSpan, SourcedSchemaFact, TypeFact, ValueFact,
        ValueFactId,
    };
    use type_bridge_contract::value::ValueTypeTag;
    use type_bridge_orm::error::OrmError;
    use type_bridge_orm::query_v2_remote::{encode_remote_request, encode_remote_request_v2};
    use type_bridge_orm::session::backend::{
        BoxFuture, DriverBackend, QueryResult, SchemaFencedReadTransaction, TransactionOps, TxType,
    };
    use type_bridge_query::validate_query_plan;
    use type_bridge_schema::{managed_schema_state, resolve};

    use crate::interceptor::{InterceptError, Interceptor};
    use crate::pipeline::{PipelineBuilder, QueryPipeline};
    use crate::test_helpers::MockExecutor;

    use super::*;

    const CURRENT_SCHEMA: &str = "define\nattribute transport-name, value string;\nentity transport-person, owns transport-name;";
    const STALE_SCHEMA: &str = "define\nattribute transport-name, value string;\nentity transport-person, owns transport-name;\nentity transport-unexpected;";
    const TEST_NONCE: &str = "server-route-nonce-000001";

    #[derive(Default)]
    struct BackendMetrics {
        fail_close: AtomicBool,
        fail_query: AtomicBool,
        schema_exports: AtomicUsize,
        transaction_opens: AtomicUsize,
        transaction_closes: AtomicUsize,
        query_executions: AtomicUsize,
    }

    struct CountingBackend {
        fallback_schema: String,
        metrics: Arc<BackendMetrics>,
        observations: Mutex<VecDeque<String>>,
        server_version: type_bridge_core_lib::version::Version,
    }

    impl CountingBackend {
        fn new(
            observations: impl IntoIterator<Item = &'static str>,
            metrics: Arc<BackendMetrics>,
        ) -> Self {
            Self {
                fallback_schema: CURRENT_SCHEMA.to_owned(),
                metrics,
                observations: Mutex::new(
                    observations
                        .into_iter()
                        .map(str::to_owned)
                        .collect::<VecDeque<_>>(),
                ),
                server_version: type_bridge_core_lib::version::Version::new(3, 12, 1),
            }
        }

        fn with_server_version(
            mut self,
            server_version: type_bridge_core_lib::version::Version,
        ) -> Self {
            self.server_version = server_version;
            self
        }
    }

    impl DriverBackend for CountingBackend {
        fn open_transaction(
            &self,
            _database: &str,
            _tx_type: TxType,
        ) -> BoxFuture<'_, Result<Box<dyn TransactionOps>, OrmError>> {
            self.metrics
                .transaction_opens
                .fetch_add(1, Ordering::SeqCst);
            let transaction = CountingTransaction {
                metrics: Arc::clone(&self.metrics),
            };
            Box::pin(async move { Ok(Box::new(transaction) as Box<dyn TransactionOps>) })
        }

        fn open_schema_fenced_read_transaction(
            &self,
            database: &str,
            _timeout: Duration,
        ) -> BoxFuture<'_, Result<SchemaFencedReadTransaction, OrmError>> {
            let database = database.to_owned();
            Box::pin(async move {
                let transaction = self.open_transaction(&database, TxType::Write).await?;
                let schema_text = self.schema_text(&database).await?;
                Ok(SchemaFencedReadTransaction::new(transaction, schema_text))
            })
        }

        fn is_open(&self) -> bool {
            true
        }

        fn server_version(&self) -> Option<type_bridge_core_lib::version::Version> {
            Some(self.server_version)
        }

        fn schema_text(&self, _database: &str) -> BoxFuture<'_, Result<String, OrmError>> {
            self.metrics.schema_exports.fetch_add(1, Ordering::SeqCst);
            let schema = self
                .observations
                .lock()
                .expect("schema observation lock")
                .pop_front()
                .unwrap_or_else(|| self.fallback_schema.clone());
            Box::pin(async move { Ok(schema) })
        }
    }

    struct CountingTransaction {
        metrics: Arc<BackendMetrics>,
    }

    impl TransactionOps for CountingTransaction {
        fn query(&mut self, _typeql: &str) -> BoxFuture<'_, Result<QueryResult, OrmError>> {
            self.metrics.query_executions.fetch_add(1, Ordering::SeqCst);
            let fail = self.metrics.fail_query.load(Ordering::SeqCst);
            Box::pin(async move {
                if fail {
                    Err(OrmError::QueryExecution("masked query fixture".into()))
                } else {
                    Ok(QueryResult::Rows(Vec::new()))
                }
            })
        }

        fn commit(&mut self) -> BoxFuture<'_, Result<(), OrmError>> {
            Box::pin(async { Ok(()) })
        }

        fn rollback(&mut self) -> BoxFuture<'_, Result<(), OrmError>> {
            Box::pin(async { Ok(()) })
        }

        fn close(&mut self) -> BoxFuture<'_, Result<(), OrmError>> {
            self.metrics
                .transaction_closes
                .fetch_add(1, Ordering::SeqCst);
            let fail = self.metrics.fail_close.load(Ordering::SeqCst);
            Box::pin(async move {
                if fail {
                    Err(OrmError::Transaction("masked close fixture".into()))
                } else {
                    Ok(())
                }
            })
        }
    }

    #[derive(Default)]
    struct PolicyMetrics {
        requests: AtomicUsize,
        responses: AtomicUsize,
        transports: AtomicUsize,
        outcomes: Mutex<Vec<(String, usize)>>,
    }

    struct CountingV2Policy {
        metrics: Arc<PolicyMetrics>,
        reject_request: bool,
        reject_transport: bool,
    }

    impl Interceptor for CountingV2Policy {
        fn name(&self) -> &str {
            "counting-v2-policy"
        }

        fn on_request<'a>(
            &'a self,
            clauses: Vec<type_bridge_core_lib::ast::Clause>,
            _ctx: &'a mut RequestContext,
        ) -> Pin<
            Box<
                dyn Future<Output = Result<Vec<type_bridge_core_lib::ast::Clause>, InterceptError>>
                    + Send
                    + 'a,
            >,
        > {
            Box::pin(async move { Ok(clauses) })
        }

        fn supports_v2(&self) -> bool {
            true
        }

        fn on_v2_transport<'a>(
            &'a self,
            _ctx: &'a mut RequestContext,
        ) -> Pin<Box<dyn Future<Output = Result<(), InterceptError>> + Send + 'a>> {
            Box::pin(async move {
                self.metrics.transports.fetch_add(1, Ordering::SeqCst);
                if self.reject_transport {
                    return Err(InterceptError::AccessDenied {
                        reason: "test policy rejection".to_owned(),
                    });
                }
                Ok(())
            })
        }

        fn on_v2_request<'a>(
            &'a self,
            _request: &'a V2PolicyRequest<'a>,
            _ctx: &'a mut RequestContext,
        ) -> Pin<Box<dyn Future<Output = Result<(), InterceptError>> + Send + 'a>> {
            Box::pin(async move {
                self.metrics.requests.fetch_add(1, Ordering::SeqCst);
                if self.reject_request {
                    return Err(InterceptError::AccessDenied {
                        reason: "test request-policy rejection".to_owned(),
                    });
                }
                Ok(())
            })
        }

        fn on_v2_response<'a>(
            &'a self,
            outcome: &'a V2PolicyOutcome<'a>,
            _ctx: &'a RequestContext,
        ) -> Pin<Box<dyn Future<Output = Result<(), InterceptError>> + Send + 'a>> {
            Box::pin(async move {
                self.metrics.responses.fetch_add(1, Ordering::SeqCst);
                self.metrics
                    .outcomes
                    .lock()
                    .expect("policy outcome lock")
                    .push((outcome.code().to_owned(), outcome.response_bytes()));
                Ok(())
            })
        }
    }

    struct AuthorityFixture {
        declared: DeclaredSchema,
        delta_context: ManagedDeltaContext,
        managed: ManagedSchemaState,
        resolved: ResolvedSchema,
    }

    fn authority_fixture(include_unexpected: bool) -> AuthorityFixture {
        let person = TypeId::new(TypeKind::Entity, "transport-person").expect("type");
        let name = AttributeId::new("transport-name").expect("attribute");
        let mut facts = vec![
            SchemaFact::Type(TypeFact::new(person.clone()).expect("type fact")),
            SchemaFact::Type(
                TypeFact::new(TypeId::new(TypeKind::Attribute, "transport-name").expect("type"))
                    .expect("type fact"),
            ),
            SchemaFact::Value(ValueFact::new(
                ValueFactId::new(name.clone()),
                ValueTypeTag::String,
            )),
            SchemaFact::Owns(OwnsFact::new(
                OwnsFactId::new(person, name).expect("owns fact ID"),
            )),
        ];
        if include_unexpected {
            facts.push(SchemaFact::Type(
                TypeFact::new(TypeId::new(TypeKind::Entity, "transport-unexpected").expect("type"))
                    .expect("type fact"),
            ));
        }
        let sourced = facts.into_iter().enumerate().map(|(index, fact)| {
            let byte = u64::try_from(index).expect("source byte");
            let line = u32::try_from(index + 1).expect("source line");
            SourcedSchemaFact::new(
                fact,
                SourceSpan::new(
                    DocumentId::new("server-v2-route-fixture.typeql").expect("document"),
                    byte,
                    byte + 1,
                    line,
                    1,
                    line,
                    2,
                )
                .expect("source span"),
            )
        });
        let declared = DeclaredSchema::from_facts(FormatVersion::V1, CapabilitySet::new(), sourced)
            .expect("declared schema");
        let profile = SemanticProfileId::new("typedb-3.12.1/v1").expect("profile");
        let resolved = resolve(&declared, &profile).expect("resolved schema");
        let delta_context = ManagedDeltaContext::new(
            ManagedScopeId::new("server-v2-route").expect("scope"),
            profile,
            CapabilitySet::new(),
        );
        let managed = managed_schema_state(&declared, &delta_context).expect("managed schema");
        AuthorityFixture {
            declared,
            delta_context,
            managed,
            resolved,
        }
    }

    fn validated_request(
        authority: &AuthorityFixture,
        advertisement: &RemoteCapabilities,
        nonce: &str,
    ) -> (Vec<u8>, QueryPlanFingerprint, RemoteRequestFingerprint) {
        validated_request_for(
            authority,
            advertisement,
            nonce,
            QueryOperation::Rows,
            QueryOutput::Rows {
                columns: vec![BindingId::new(0).expect("binding id")],
            },
            RemoteLimits {
                deadline_ms: Some(5_000),
                max_bytes: 1 << 20,
                max_items: 100,
                max_collection_members: 1 << 16,
            },
        )
    }

    fn validated_request_for(
        authority: &AuthorityFixture,
        advertisement: &RemoteCapabilities,
        nonce: &str,
        operation: QueryOperation,
        output: QueryOutput,
        limits: RemoteLimits,
    ) -> (Vec<u8>, QueryPlanFingerprint, RemoteRequestFingerprint) {
        let mut bindings = vec![AssertionBinding::new(
            BindingId::new(0).expect("binding id"),
            QueryVariable::new("person").expect("binding variable"),
        )];
        let mut patterns = vec![QueryPattern::Isa {
            binding: BindingId::new(0).expect("binding id"),
            include_subtypes: false,
            type_id: TypeId::new(TypeKind::Entity, "transport-person").expect("type"),
        }];
        if matches!(output, QueryOutput::Documents { .. }) {
            bindings.push(AssertionBinding::new(
                BindingId::new(1).expect("binding id"),
                QueryVariable::new("name").expect("binding variable"),
            ));
            patterns.push(QueryPattern::Has {
                attribute: BindingId::new(1).expect("binding id"),
                attribute_id: AttributeId::new("transport-name").expect("attribute"),
                owner: BindingId::new(0).expect("binding id"),
            });
        }
        let plan = QueryPlan::new(
            bindings,
            Vec::new(),
            vec![ReadStage::Match { patterns }],
            output,
            authority.managed.managed_semantic_schema().clone(),
        )
        .expect("query plan");
        let context =
            MigrationAssertionValidationContext::new(&authority.resolved, &authority.managed);
        let validated = validate_query_plan(&plan, &context, StructuralLimits::CANONICAL)
            .expect("validated query");
        let invocation =
            QueryInvocation::new(&plan, operation, Vec::new()).expect("query invocation");
        let request = encode_remote_request(&validated, &invocation, advertisement, limits, nonce)
            .expect("remote request");
        let plan_fingerprint = QueryPlanFingerprint::compute(&plan).expect("plan fingerprint");
        let request_fingerprint =
            RemoteRequestFingerprint::compute(&request).expect("request fingerprint");
        (request, plan_fingerprint, request_fingerprint)
    }

    fn validated_request_v2(
        authority: &AuthorityFixture,
        advertisement: &RemoteCapabilities,
        nonce: &str,
    ) -> (
        Vec<u8>,
        QueryPlan,
        RemoteQueryRequestV2,
        RemoteRequestFingerprintV2,
        RemoteLimitsV2,
    ) {
        let plan = QueryPlan::new_v2(
            vec![AssertionBinding::new(
                BindingId::new(0).expect("binding id"),
                QueryVariable::new("person").expect("binding variable"),
            )],
            Vec::new(),
            vec![ReadStage::Match {
                patterns: vec![QueryPattern::Isa {
                    binding: BindingId::new(0).expect("binding id"),
                    include_subtypes: false,
                    type_id: TypeId::new(TypeKind::Entity, "transport-person").expect("type"),
                }],
            }],
            QueryOutput::Rows {
                columns: vec![BindingId::new(0).expect("binding id")],
            },
            authority.managed.managed_semantic_schema().clone(),
        )
        .expect("V2 query plan");
        let context =
            MigrationAssertionValidationContext::new(&authority.resolved, &authority.managed);
        let validated = validate_query_plan(&plan, &context, StructuralLimits::CANONICAL)
            .expect("validated V2 query");
        let invocation =
            QueryInvocation::new(&plan, QueryOperation::Rows, Vec::new()).expect("V2 invocation");
        let limits = RemoteLimitsV2 {
            deadline_ms: Some(5_000),
            max_bytes: 1 << 20,
            max_items: 100,
            max_collection_members: 1 << 16,
            max_graph_nodes: 0,
            max_attribute_values: 0,
            max_role_players: 0,
        };
        let request =
            encode_remote_request_v2(&validated, &invocation, advertisement, limits, nonce)
                .expect("V2 remote request");
        let envelope = RemoteQueryRequestV2::decode(&request).expect("V2 request envelope");
        let fingerprint =
            RemoteRequestFingerprintV2::compute(&request).expect("V2 request fingerprint");
        (request, plan, envelope, fingerprint, limits)
    }

    struct RouteFixture {
        advertisement: RemoteCapabilities,
        metrics: Arc<BackendMetrics>,
        nonce: &'static str,
        plan_fingerprint: QueryPlanFingerprint,
        query: Arc<V2QueryState>,
        request: Vec<u8>,
        request_fingerprint: RemoteRequestFingerprint,
        router: Router,
    }

    fn default_pipeline() -> QueryPipeline {
        PipelineBuilder::new(MockExecutor::new())
            .with_default_database("server-v2-route")
            .build()
            .expect("query pipeline")
    }

    fn pipeline_with_policy(metrics: Arc<PolicyMetrics>, reject_transport: bool) -> QueryPipeline {
        PipelineBuilder::new(MockExecutor::new())
            .with_default_database("server-v2-route")
            .with_interceptor(CountingV2Policy {
                metrics,
                reject_request: false,
                reject_transport,
            })
            .build()
            .expect("query pipeline")
    }

    fn pipeline_with_rejecting_request_policy(metrics: Arc<PolicyMetrics>) -> QueryPipeline {
        PipelineBuilder::new(MockExecutor::new())
            .with_default_database("server-v2-route")
            .with_interceptor(CountingV2Policy {
                metrics,
                reject_request: true,
                reject_transport: false,
            })
            .build()
            .expect("query pipeline")
    }

    fn route_fixture(
        observations: impl IntoIterator<Item = &'static str>,
        advertised: CapabilitySet,
        pipeline: QueryPipeline,
    ) -> RouteFixture {
        route_fixture_with_replay_store(
            observations,
            advertised,
            pipeline,
            Arc::new(InMemoryReplayStore::new(DEFAULT_REPLAY_CAPACITY)),
        )
    }

    fn route_fixture_with_replay_store(
        observations: impl IntoIterator<Item = &'static str>,
        advertised: CapabilitySet,
        pipeline: QueryPipeline,
        replay_store: Arc<dyn RemoteReplayStore>,
    ) -> RouteFixture {
        let authority = authority_fixture(false);
        let metrics = Arc::new(BackendMetrics::default());
        let database = Database::with_backend(
            Box::new(CountingBackend::new(observations, Arc::clone(&metrics))),
            "server-v2-route",
        );
        let query = Arc::new(
            V2QueryState::new_query_only(
                advertised,
                QueryV2AnswerLimits::default(),
                database,
                authority.declared,
                authority.delta_context,
                authority.managed,
                authority.resolved,
            )
            .expect("executor advertisement is canonical")
            .with_replay_store(replay_store),
        );
        let advertisement = query.advertisement.clone();
        let authority = authority_fixture(false);
        let (request, plan_fingerprint, request_fingerprint) =
            validated_request(&authority, &advertisement, TEST_NONCE);
        let router = create_router_with_v2(Arc::new(pipeline), Arc::clone(&query));
        RouteFixture {
            advertisement,
            metrics,
            nonce: TEST_NONCE,
            plan_fingerprint,
            query,
            request,
            request_fingerprint,
            router,
        }
    }

    #[test]
    fn supported_non_baseline_patch_uses_the_frozen_authority_profile() {
        let authority = authority_fixture(false);
        let metrics = Arc::new(BackendMetrics::default());
        let backend = CountingBackend::new([], metrics)
            .with_server_version(type_bridge_core_lib::version::Version::new(3, 12, 99));
        let database = Database::with_backend(Box::new(backend), "server-v2-profile-patch");
        let state = V2QueryState::new_query_only(
            CapabilitySet::new(),
            QueryV2AnswerLimits::default(),
            database,
            authority.declared,
            authority.delta_context,
            authority.managed,
            authority.resolved,
        )
        .expect("V2 state");

        state
            .verify_database_profile()
            .expect("supported patch uses the frozen minor-line profile");
    }

    async fn post_query(router: &Router, body: Vec<u8>) -> Vec<u8> {
        let response = router
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/v2/query")
                    .header("content-type", "application/json")
                    .body(Body::from(body))
                    .expect("request"),
            )
            .await
            .expect("route response");
        assert_eq!(response.status(), StatusCode::OK);
        response
            .into_body()
            .collect()
            .await
            .expect("response body")
            .to_bytes()
            .to_vec()
    }

    struct V1RouteSnapshot {
        method: &'static str,
        path: &'static str,
        request_body: &'static [u8],
        status: StatusCode,
        response_body: &'static [u8],
    }

    async fn assert_v1_route_snapshot(router: &Router, snapshot: V1RouteSnapshot) {
        let mut request = Request::builder()
            .method(snapshot.method)
            .uri(snapshot.path);
        if !snapshot.request_body.is_empty() {
            request = request.header("content-type", "application/json");
        }
        let response = router
            .clone()
            .oneshot(
                request
                    .body(Body::from(snapshot.request_body))
                    .expect("V1 snapshot request"),
            )
            .await
            .expect("V1 snapshot response");
        assert_eq!(
            response.status(),
            snapshot.status,
            "{} {} status",
            snapshot.method,
            snapshot.path,
        );
        assert_eq!(
            response.headers().len(),
            2,
            "{} {} released header set",
            snapshot.method,
            snapshot.path,
        );
        assert_eq!(
            response
                .headers()
                .get("content-type")
                .and_then(|value| value.to_str().ok()),
            Some("application/json"),
            "{} {} content type",
            snapshot.method,
            snapshot.path,
        );
        let expected_content_length = snapshot.response_body.len().to_string();
        assert_eq!(
            response
                .headers()
                .get("content-length")
                .and_then(|value| value.to_str().ok()),
            Some(expected_content_length.as_str()),
            "{} {} content length",
            snapshot.method,
            snapshot.path,
        );
        let body = response
            .into_body()
            .collect()
            .await
            .expect("V1 snapshot body")
            .to_bytes();
        assert_eq!(
            body.as_ref(),
            snapshot.response_body,
            "{} {} released body",
            snapshot.method,
            snapshot.path,
        );
    }

    #[tokio::test]
    async fn merged_router_retains_every_released_v1_route_snapshot() {
        const QUERY_FAILURE: &[u8] = br#"{"status":"error","error":{"code":"QUERY_EXECUTION_ERROR","message":"Query execution error: Unknown transaction type: v1-wire-probe"}}"#;
        const SCHEMA_FAILURE: &[u8] = br#"{"status":"error","error":{"code":"SCHEMA_ERROR","message":"Schema error: No schema loaded"}}"#;
        let pipeline = PipelineBuilder::new(MockExecutor::failing(
            "Unknown transaction type: v1-wire-probe",
        ))
        .with_default_database("server-v2-route")
        .build()
        .expect("V1 snapshot pipeline");
        let fixture = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_v2_capability_vocabulary(),
            pipeline,
        );
        let snapshots = [
            V1RouteSnapshot {
                method: "GET",
                path: "/health",
                request_body: b"",
                status: StatusCode::OK,
                response_body:
                    br#"{"status":"ok","version":"1.5.11","typedb_connected":true}"#,
            },
            V1RouteSnapshot {
                method: "POST",
                path: "/query",
                request_body: br#"{"transaction_type":"read","clauses":[]}"#,
                status: StatusCode::BAD_REQUEST,
                response_body: QUERY_FAILURE,
            },
            V1RouteSnapshot {
                method: "POST",
                path: "/query/raw",
                request_body: br#"{"transaction_type":"read","query":"match $p isa person; fetch { \"person\": { $p.* } };"}"#,
                status: StatusCode::BAD_REQUEST,
                response_body: QUERY_FAILURE,
            },
            V1RouteSnapshot {
                method: "POST",
                path: "/query/validate",
                request_body: br#"{"clauses":[]}"#,
                status: StatusCode::INTERNAL_SERVER_ERROR,
                response_body: SCHEMA_FAILURE,
            },
            V1RouteSnapshot {
                method: "GET",
                path: "/schema",
                request_body: b"",
                status: StatusCode::INTERNAL_SERVER_ERROR,
                response_body: SCHEMA_FAILURE,
            },
        ];

        for snapshot in snapshots {
            assert_v1_route_snapshot(&fixture.router, snapshot).await;
        }
    }

    fn failure_code(query: &V2QueryState, bytes: &[u8]) -> String {
        type_bridge_contract::query_remote::decode_signed_remote_failure(
            bytes,
            &query.advertisement_fingerprint,
            query.advertisement.reply_key(),
            u64::try_from(type_bridge_contract::limits::MAX_REMOTE_ENVELOPE_BYTES)
                .expect("wire ceiling"),
            &Ed25519RemoteReplyVerifier,
        )
        .expect("failure envelope")
        .diagnostic()
        .expect("failure diagnostic")
        .code()
        .as_str()
        .to_owned()
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn blocking_preflight_admission_has_a_fixed_concurrency_bound_and_releases() {
        let slots = new_v2_preflight_slots();
        let permits = (0..V2_PREFLIGHT_CONCURRENCY)
            .map(|_| {
                Arc::clone(&slots)
                    .try_acquire_owned()
                    .expect("configured preflight slot")
            })
            .collect::<Vec<_>>();
        assert!(
            Arc::clone(&slots).try_acquire_owned().is_err(),
            "a fifth blocking preflight must wait rather than detach unbounded CPU work",
        );
        drop(permits);
        let released = Arc::clone(&slots)
            .try_acquire_owned()
            .expect("released blocking preflight slot");
        drop(released);

        let permit = Arc::clone(&slots)
            .acquire_owned()
            .await
            .expect("blocking-task permit");
        tokio::task::spawn_blocking(move || {
            let _permit = permit;
        })
        .await
        .expect("blocking preflight completion");
        assert_eq!(
            slots.available_permits(),
            V2_PREFLIGHT_CONCURRENCY,
            "the blocking task must release its owned permit on completion",
        );
    }

    #[tokio::test]
    async fn one_route_dispatches_v1_and_v2_without_downgrade_and_replays_before_host_work() {
        let policy_metrics = Arc::new(PolicyMetrics::default());
        let fixture = route_fixture(
            [CURRENT_SCHEMA; 4],
            query_plan_capability_vocabulary(),
            pipeline_with_policy(Arc::clone(&policy_metrics), false),
        );

        let v1 = post_query(&fixture.router, fixture.request.clone()).await;
        assert!(matches!(
            decode_remote_reply(
                &v1,
                fixture.nonce,
                &fixture.plan_fingerprint,
                &fixture.request_fingerprint,
                &fixture.query.advertisement_fingerprint,
                fixture.advertisement.reply_key(),
                RemoteReplyDecodeLimits {
                    shape: RemoteOutcomeShape::Rows { width: 1 },
                    max_bytes: 1 << 20,
                    max_items: 100,
                    max_collection_members: 1 << 16,
                },
                &Ed25519RemoteReplyVerifier,
            )
            .expect("V1 reply"),
            RemoteReply::Response(_)
        ));

        let authority = authority_fixture(false);
        let v2_nonce = "server-v2-dispatch-nonce-0001";
        let (request, plan, envelope, fingerprint, limits) =
            validated_request_v2(&authority, &fixture.advertisement, v2_nonce);
        let v2 = post_query(&fixture.router, request.clone()).await;
        assert!(matches!(
            decode_remote_reply_v2(
                &v2,
                &envelope,
                &plan.fingerprint().expect("V2 plan fingerprint"),
                &fingerprint,
                &fixture.query.advertisement_fingerprint,
                fixture.advertisement.reply_key(),
                RemoteReplyDecodeLimitsV2 {
                    max_bytes: limits.max_bytes,
                    max_items: limits.max_items,
                    max_collection_members: limits.max_collection_members,
                    max_graph_nodes: limits.max_graph_nodes,
                    max_attribute_values: limits.max_attribute_values,
                    max_role_players: limits.max_role_players,
                },
                &Ed25519RemoteReplyVerifier,
            )
            .expect("V2 reply"),
            RemoteReplyV2::Response(_)
        ));
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 2);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 2);
        assert_eq!(policy_metrics.requests.load(Ordering::SeqCst), 2);
        let exports_before_replay = fixture.metrics.schema_exports.load(Ordering::SeqCst);

        let replay = post_query(&fixture.router, request).await;
        let failure = match decode_remote_reply_v2(
            &replay,
            &envelope,
            &plan.fingerprint().expect("V2 plan fingerprint"),
            &fingerprint,
            &fixture.query.advertisement_fingerprint,
            fixture.advertisement.reply_key(),
            RemoteReplyDecodeLimitsV2 {
                max_bytes: limits.max_bytes,
                max_items: limits.max_items,
                max_collection_members: limits.max_collection_members,
                max_graph_nodes: limits.max_graph_nodes,
                max_attribute_values: limits.max_attribute_values,
                max_role_players: limits.max_role_players,
            },
            &Ed25519RemoteReplyVerifier,
        )
        .expect("request-bound V2 replay failure")
        {
            RemoteReplyV2::Failure(failure) => failure,
            RemoteReplyV2::Response(_) => panic!("replayed V2 request cannot succeed"),
        };
        assert_eq!(
            failure
                .diagnostic()
                .expect("complete V2 diagnostic")
                .code()
                .as_str(),
            "query_remote_replay"
        );
        assert_eq!(
            fixture.metrics.schema_exports.load(Ordering::SeqCst),
            exports_before_replay,
            "replay rejects before live authority reconstruction"
        );
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 2);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 2);
        assert_eq!(
            policy_metrics.requests.load(Ordering::SeqCst),
            2,
            "V2 replay rejects before request-specific application policy"
        );
    }

    #[tokio::test]
    async fn declared_oversized_body_rejects_before_collection_and_preflight() {
        let fixture = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let response = fixture
            .router
            .clone()
            .oneshot(
                Request::builder()
                    .method("POST")
                    .uri("/v2/query")
                    .header(CONTENT_TYPE, "application/json")
                    .header(CONTENT_LENGTH, V2_BODY_LIMIT_BYTES + 1)
                    .body(Body::empty())
                    .expect("oversized request declaration"),
            )
            .await
            .expect("route response");
        assert_eq!(response.status(), StatusCode::OK);
        let body = response
            .into_body()
            .collect()
            .await
            .expect("response body")
            .to_bytes();
        assert_eq!(
            failure_code(&fixture.query, &body),
            "query_remote_envelope_oversized",
        );
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 0);
        assert_eq!(fixture.metrics.schema_exports.load(Ordering::SeqCst), 0);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn hostile_maximal_body_dispatches_from_its_prefix_without_host_construction() {
        let fixture = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_v2_capability_vocabulary(),
            default_pipeline(),
        );
        let authority = authority_fixture(false);
        let (request, _, _, _, _) = validated_request_v2(
            &authority,
            &fixture.advertisement,
            "server-v2-hostile-prefix-0001",
        );
        let format_field = br#","format":"typebridge.query-remote-request/v2","#;
        let format_end = request
            .windows(format_field.len())
            .position(|window| window == format_field)
            .map(|offset| offset + format_field.len())
            .expect("canonical V2 format field is in the bounded prefix");
        let mut hostile = request[..format_end].to_vec();
        hostile.resize(MAX_REMOTE_ENVELOPE_BYTES, b'[');

        let response = post_query(&fixture.router, hostile).await;
        let failure = decode_signed_remote_failure_v2(
            &response,
            &fixture.query.advertisement_fingerprint,
            fixture.advertisement.reply_key(),
            u64::try_from(MAX_REMOTE_ENVELOPE_BYTES).expect("wire ceiling"),
            &Ed25519RemoteReplyVerifier,
        )
        .expect("the bounded prefix selects a signed V2 pre-request failure");
        assert_eq!(
            failure
                .diagnostic()
                .expect("complete V2 diagnostic")
                .code()
                .as_str(),
            "query_remote_v2_format_missing",
        );
        assert_eq!(fixture.metrics.schema_exports.load(Ordering::SeqCst), 0);
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 0);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn response_wire_floors_reject_before_host_work_and_admit_exact_boundaries() {
        let fixture = route_fixture(
            [CURRENT_SCHEMA; 8],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let authority = authority_fixture(false);
        let shapes = vec![
            (
                "rows",
                QueryOperation::Rows,
                QueryOutput::Rows {
                    columns: vec![BindingId::new(0).expect("binding id")],
                },
                RemoteOutcome::Rows { rows: Vec::new() },
                RemoteOutcomeShape::Rows { width: 1 },
            ),
            (
                "documents",
                QueryOperation::Rows,
                QueryOutput::Documents {
                    fields: vec![DocumentField::new(
                        QueryVariable::new("person").expect("document key"),
                        DocumentSource::Binding {
                            binding: BindingId::new(1).expect("binding id"),
                        },
                    )],
                },
                RemoteOutcome::Documents {
                    documents: Vec::new(),
                },
                RemoteOutcomeShape::Documents { width: 1 },
            ),
            (
                "count",
                QueryOperation::Count,
                QueryOutput::Rows {
                    columns: vec![BindingId::new(0).expect("binding id")],
                },
                RemoteOutcome::Count { value: 0 },
                RemoteOutcomeShape::Count,
            ),
            (
                "exists",
                QueryOperation::Exists,
                QueryOutput::Rows {
                    columns: vec![BindingId::new(0).expect("binding id")],
                },
                RemoteOutcome::Exists { value: false },
                RemoteOutcomeShape::Exists,
            ),
        ];
        let limits = |max_bytes| RemoteLimits {
            deadline_ms: Some(30_000),
            max_bytes,
            // A zero-item answer makes the empty/zero/false outcomes above the
            // exact smallest admissible successes for all four shapes.
            max_items: 0,
            max_collection_members: 0,
        };

        for (name, operation, output, outcome, response_shape) in &shapes {
            let probe_nonce = format!("server-wire-floor-{name}-probe-0001");
            let (_, plan, request) = validated_request_for(
                &authority,
                &fixture.advertisement,
                &probe_nonce,
                *operation,
                output.clone(),
                limits(1 << 20),
            );
            let floor = u64::try_from(
                RemoteQueryResponse::new(&probe_nonce, &plan, &request, outcome.clone())
                    .expect("minimal response")
                    .signed_encoded_len(
                        &fixture.query.advertisement_fingerprint,
                        fixture.advertisement.reply_key(),
                    )
                    .expect("minimal signed response length"),
            )
            .expect("wire length");

            for (suffix, max_bytes) in [("zero", 0), ("below", floor - 1)] {
                let nonce = format!("server-wire-floor-{name}-{suffix}-0001");
                let (request_bytes, plan, request) = validated_request_for(
                    &authority,
                    &fixture.advertisement,
                    &nonce,
                    *operation,
                    output.clone(),
                    limits(max_bytes),
                );
                let body = post_query(&fixture.router, request_bytes).await;
                let failure = match decode_remote_reply(
                    &body,
                    &nonce,
                    &plan,
                    &request,
                    &fixture.query.advertisement_fingerprint,
                    fixture.advertisement.reply_key(),
                    RemoteReplyDecodeLimits {
                        shape: *response_shape,
                        max_bytes,
                        max_items: 0,
                        max_collection_members: 0,
                    },
                    &Ed25519RemoteReplyVerifier,
                )
                .expect("request-bound failure remains decodable at the success budget")
                {
                    RemoteReply::Failure(failure) => failure,
                    RemoteReply::Response(_) => panic!("undersized request cannot succeed"),
                };
                assert_eq!(
                    failure
                        .diagnostic()
                        .expect("failure diagnostic")
                        .code()
                        .as_str(),
                    "query_remote_response_oversized",
                    "{name} at {suffix} budget",
                );
            }
        }
        assert_eq!(fixture.metrics.schema_exports.load(Ordering::SeqCst), 0);
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 0);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 0);

        for (name, operation, output, outcome, _) in shapes {
            let probe_nonce = format!("server-wire-floor-{name}-probe-0002");
            let (_, plan, request) = validated_request_for(
                &authority,
                &fixture.advertisement,
                &probe_nonce,
                operation,
                output.clone(),
                limits(1 << 20),
            );
            let floor = u64::try_from(
                RemoteQueryResponse::new(&probe_nonce, &plan, &request, outcome.clone())
                    .expect("minimal response")
                    .signed_encoded_len(
                        &fixture.query.advertisement_fingerprint,
                        fixture.advertisement.reply_key(),
                    )
                    .expect("minimal signed response length"),
            )
            .expect("wire length");
            let nonce = format!("server-wire-floor-{name}-exact-0001");
            let (request_bytes, plan, request) = validated_request_for(
                &authority,
                &fixture.advertisement,
                &nonce,
                operation,
                output,
                limits(floor),
            );
            let expected = RemoteQueryResponse::new(&nonce, &plan, &request, outcome)
                .expect("exact response")
                .encode_signed(
                    &fixture.query.advertisement_fingerprint,
                    &fixture.query.signer,
                )
                .expect("exact signed response");
            assert_eq!(u64::try_from(expected.len()).expect("wire length"), floor);
            assert_eq!(post_query(&fixture.router, request_bytes).await, expected);
        }
        assert_eq!(fixture.metrics.schema_exports.load(Ordering::SeqCst), 8);
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 4);
        assert_eq!(fixture.metrics.transaction_closes.load(Ordering::SeqCst), 4);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 4);
    }

    #[tokio::test]
    async fn rejected_query_shapes_never_open_a_data_transaction() {
        let malformed = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let body = post_query(&malformed.router, b"{".to_vec()).await;
        assert_eq!(
            failure_code(&malformed.query, &body),
            "malformed_canonical_json"
        );
        assert_eq!(
            malformed.metrics.transaction_opens.load(Ordering::SeqCst),
            0,
        );
        assert_eq!(malformed.metrics.schema_exports.load(Ordering::SeqCst), 0,);

        let forged_owner = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let foreign_signer = RemoteReplySigningKey::from_secret_bytes([0x91; 32]);
        let foreign_advertisement = RemoteCapabilities::new(
            query_plan_capability_vocabulary(),
            RemoteExecutorBinding::new("foreign-executor", "foreign-executor-epoch-0001")
                .expect("foreign executor binding"),
            foreign_signer.public_key(),
        );
        let authority = authority_fixture(false);
        let (foreign_request, _, _) = validated_request(
            &authority,
            &foreign_advertisement,
            "server-foreign-owner-nonce-01",
        );
        let body = post_query(&forged_owner.router, foreign_request).await;
        assert_eq!(
            failure_code(&forged_owner.query, &body),
            "query_remote_executor_mismatch",
        );
        assert_eq!(
            forged_owner
                .metrics
                .transaction_opens
                .load(Ordering::SeqCst),
            0,
        );
        assert_eq!(
            forged_owner.metrics.schema_exports.load(Ordering::SeqCst),
            0,
        );

        let forged_v2 = route_fixture_with_replay_store(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
            Arc::new(InMemoryReplayStore::new(0)),
        );
        let foreign_signer = RemoteReplySigningKey::from_secret_bytes([0x92; 32]);
        let foreign_advertisement = RemoteCapabilities::new(
            forged_v2.advertisement.capabilities().clone(),
            RemoteExecutorBinding::new("foreign-v2-executor", "foreign-v2-executor-epoch-0001")
                .expect("foreign V2 executor binding"),
            foreign_signer.public_key(),
        );
        let authority = authority_fixture(false);
        let (foreign_request, _, _, _, _) = validated_request_v2(
            &authority,
            &foreign_advertisement,
            "server-foreign-v2-owner-nonce-0001",
        );
        let body = post_query(&forged_v2.router, foreign_request).await;
        let signed: serde_json::Value =
            serde_json::from_slice(&body).expect("signed foreign-owner V2 failure");
        assert_eq!(
            signed["payload"]["code"],
            serde_json::json!("query_remote_v2_advertisement_mismatch"),
            "foreign V2 ownership rejects before the zero-capacity replay store"
        );
        assert_eq!(forged_v2.metrics.schema_exports.load(Ordering::SeqCst), 0);
        assert_eq!(
            forged_v2.metrics.transaction_opens.load(Ordering::SeqCst),
            0
        );
        assert_eq!(forged_v2.metrics.query_executions.load(Ordering::SeqCst), 0);

        let unsupported = route_fixture([CURRENT_SCHEMA], CapabilitySet::new(), default_pipeline());
        let body = post_query(&unsupported.router, unsupported.request.clone()).await;
        assert_eq!(
            failure_code(&unsupported.query, &body),
            "query_remote_capability_unsupported"
        );
        assert_eq!(
            unsupported.metrics.transaction_opens.load(Ordering::SeqCst),
            0,
        );
        assert_eq!(unsupported.metrics.schema_exports.load(Ordering::SeqCst), 0,);

        let invalid = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let request = String::from_utf8(invalid.request.clone()).expect("request JSON");
        let request = request.replace("\"rows\":[]", "\"rows\":[[]]");
        assert_ne!(request.as_bytes(), invalid.request.as_slice());
        let body = post_query(&invalid.router, request.into_bytes()).await;
        assert_eq!(
            failure_code(&invalid.query, &body),
            "query_invocation_unexpected_inputs"
        );
        assert_eq!(invalid.metrics.transaction_opens.load(Ordering::SeqCst), 0,);
        assert_eq!(invalid.metrics.schema_exports.load(Ordering::SeqCst), 0);

        let stale_plan = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let stale_authority = authority_fixture(true);
        let (request, _, _) = validated_request(
            &stale_authority,
            &stale_plan.advertisement,
            "server-stale-plan-nonce-0001",
        );
        let body = post_query(&stale_plan.router, request).await;
        assert_eq!(
            failure_code(&stale_plan.query, &body),
            "query_plan_managed_semantic_mismatch",
        );
        assert_eq!(
            stale_plan.metrics.transaction_opens.load(Ordering::SeqCst),
            0,
        );
        assert_eq!(stale_plan.metrics.schema_exports.load(Ordering::SeqCst), 0,);

        let stale_live = route_fixture(
            [STALE_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let body = post_query(&stale_live.router, stale_live.request.clone()).await;
        assert_eq!(
            failure_code(&stale_live.query, &body),
            "query_remote_stale_schema"
        );
        assert_eq!(
            stale_live.metrics.transaction_opens.load(Ordering::SeqCst),
            0,
        );
        assert_eq!(stale_live.metrics.schema_exports.load(Ordering::SeqCst), 1,);
        assert_eq!(
            stale_live.metrics.query_executions.load(Ordering::SeqCst),
            0,
        );
    }

    #[tokio::test]
    async fn replay_is_rejected_before_live_schema_or_data_transaction_work() {
        let policy_metrics = Arc::new(PolicyMetrics::default());
        let fixture = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            pipeline_with_policy(Arc::clone(&policy_metrics), false),
        );
        fixture
            .query
            .replay_store
            .reserve(
                fixture.advertisement.executor().epoch(),
                fixture.nonce,
                Instant::now() + Duration::from_secs(60),
            )
            .await
            .expect("pre-reserve nonce");
        let body = post_query(&fixture.router, fixture.request.clone()).await;
        assert_eq!(failure_code(&fixture.query, &body), "query_remote_replay");
        assert_eq!(fixture.metrics.schema_exports.load(Ordering::SeqCst), 0);
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 0,);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 0,);
        assert_eq!(
            policy_metrics.requests.load(Ordering::SeqCst),
            1,
            "released V1 plan-policy-before-replay precedence remains unchanged"
        );
        assert_eq!(
            policy_metrics.transports.load(Ordering::SeqCst),
            1,
            "pre-body transport authentication still protects the endpoint"
        );

        let rejecting_metrics = Arc::new(PolicyMetrics::default());
        let rejecting = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            pipeline_with_rejecting_request_policy(Arc::clone(&rejecting_metrics)),
        );
        rejecting
            .query
            .replay_store
            .reserve(
                rejecting.advertisement.executor().epoch(),
                rejecting.nonce,
                Instant::now() + Duration::from_secs(60),
            )
            .await
            .expect("pre-reserve rejecting V1 nonce");
        let expected = RemoteQueryFailure::bound(
            rejecting.nonce.to_owned(),
            &rejecting.request_fingerprint,
            &policy_rejected(),
        )
        .encode_signed_or_fallback(
            &rejecting.query.advertisement_fingerprint,
            &rejecting.query.signer,
        );
        let body = post_query(&rejecting.router, rejecting.request.clone()).await;
        assert_eq!(
            body, expected,
            "released V1 policy rejection bytes retain precedence over replay"
        );
        assert_eq!(rejecting_metrics.requests.load(Ordering::SeqCst), 1);
        assert_eq!(rejecting.metrics.schema_exports.load(Ordering::SeqCst), 0);
        assert_eq!(
            rejecting.metrics.transaction_opens.load(Ordering::SeqCst),
            0
        );
    }

    #[tokio::test]
    async fn authority_is_rechecked_across_one_data_snapshot_before_execution() {
        let stable = route_fixture(
            [CURRENT_SCHEMA, CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let body = post_query(&stable.router, stable.request.clone()).await;
        assert!(matches!(
            decode_remote_reply(
                &body,
                stable.nonce,
                &stable.plan_fingerprint,
                &stable.request_fingerprint,
                &stable.query.advertisement_fingerprint,
                stable.advertisement.reply_key(),
                type_bridge_contract::query_remote::RemoteReplyDecodeLimits {
                    shape: type_bridge_contract::query_remote::RemoteOutcomeShape::Rows {
                        width: 1,
                    },
                    max_bytes: 1 << 20,
                    max_items: 100,
                    max_collection_members: 1 << 16,
                },
                &Ed25519RemoteReplyVerifier,
            )
            .expect("bound successful reply"),
            RemoteReply::Response(_),
        ));
        assert_eq!(stable.metrics.schema_exports.load(Ordering::SeqCst), 2);
        assert_eq!(stable.metrics.transaction_opens.load(Ordering::SeqCst), 1);
        assert_eq!(stable.metrics.transaction_closes.load(Ordering::SeqCst), 1);
        assert_eq!(stable.metrics.query_executions.load(Ordering::SeqCst), 1);

        let changed = route_fixture(
            [CURRENT_SCHEMA, STALE_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        changed.metrics.fail_close.store(true, Ordering::SeqCst);
        let body = post_query(&changed.router, changed.request.clone()).await;
        let failure = match decode_remote_reply(
            &body,
            changed.nonce,
            &changed.plan_fingerprint,
            &changed.request_fingerprint,
            &changed.query.advertisement_fingerprint,
            changed.advertisement.reply_key(),
            type_bridge_contract::query_remote::RemoteReplyDecodeLimits {
                shape: type_bridge_contract::query_remote::RemoteOutcomeShape::Rows { width: 1 },
                max_bytes: 1 << 20,
                max_items: 100,
                max_collection_members: 1 << 16,
            },
            &Ed25519RemoteReplyVerifier,
        )
        .expect("bound stale reply")
        {
            RemoteReply::Failure(failure) => failure,
            RemoteReply::Response(_) => panic!("stale post-open authority executed"),
        };
        assert_eq!(
            failure
                .diagnostic()
                .expect("stale diagnostic")
                .code()
                .as_str(),
            "query_remote_stale_schema",
        );
        assert_eq!(changed.metrics.schema_exports.load(Ordering::SeqCst), 2);
        assert_eq!(changed.metrics.transaction_opens.load(Ordering::SeqCst), 1);
        assert_eq!(changed.metrics.transaction_closes.load(Ordering::SeqCst), 1);
        assert_eq!(changed.metrics.query_executions.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn close_failure_is_warning_only_and_preserves_the_primary_success_reply() {
        let fixture = route_fixture(
            [CURRENT_SCHEMA, CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        fixture.metrics.fail_close.store(true, Ordering::SeqCst);
        let body = post_query(&fixture.router, fixture.request.clone()).await;
        assert!(matches!(
            decode_remote_reply(
                &body,
                fixture.nonce,
                &fixture.plan_fingerprint,
                &fixture.request_fingerprint,
                &fixture.query.advertisement_fingerprint,
                fixture.advertisement.reply_key(),
                type_bridge_contract::query_remote::RemoteReplyDecodeLimits {
                    shape: type_bridge_contract::query_remote::RemoteOutcomeShape::Rows {
                        width: 1,
                    },
                    max_bytes: 1 << 20,
                    max_items: 100,
                    max_collection_members: 1 << 16,
                },
                &Ed25519RemoteReplyVerifier,
            )
            .expect("close failure must not replace the bound primary reply"),
            RemoteReply::Response(_),
        ));
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 1);
        assert_eq!(fixture.metrics.transaction_closes.load(Ordering::SeqCst), 1);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn provider_failure_closes_once_and_close_failure_preserves_exact_bound_bytes() {
        let fixture = route_fixture(
            [CURRENT_SCHEMA, CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        fixture.metrics.fail_query.store(true, Ordering::SeqCst);
        fixture.metrics.fail_close.store(true, Ordering::SeqCst);
        let body = post_query(&fixture.router, fixture.request.clone()).await;
        let expected = RemoteQueryFailure::bound(
            fixture.nonce.to_owned(),
            &fixture.request_fingerprint,
            &diagnostic(
                DiagnosticCategory::Integrity,
                "query_remote_provider_failed",
                "the executor provider call failed",
            ),
        )
        .encode_signed_or_fallback(
            &fixture.query.advertisement_fingerprint,
            &fixture.query.signer,
        );
        assert_eq!(body, expected);
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 1);
        assert_eq!(fixture.metrics.transaction_closes.load(Ordering::SeqCst), 1);
        assert_eq!(fixture.metrics.query_executions.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn capability_discovery_runs_v2_transport_policy_and_audit_before_export() {
        let policy_metrics = Arc::new(PolicyMetrics::default());
        let model_vocabulary = query_plan_v2_capability_vocabulary();
        let fixture = route_fixture(
            [CURRENT_SCHEMA],
            model_vocabulary.clone(),
            pipeline_with_policy(Arc::clone(&policy_metrics), false),
        );
        assert!(
            model_vocabulary
                .iter()
                .all(|capability| fixture.advertisement.capabilities().contains(capability)),
            "the production model executor advertises the complete V2 plan vocabulary",
        );
        let expected = fixture.advertisement.encode().expect("advertisement bytes");
        let response = fixture
            .router
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/v2/capabilities")
                    .header("authorization", "Bearer test")
                    .body(Body::empty())
                    .expect("request"),
            )
            .await
            .expect("capability response");
        assert_eq!(response.status(), StatusCode::OK);
        let body = response
            .into_body()
            .collect()
            .await
            .expect("capability body")
            .to_bytes();
        assert_eq!(body.as_ref(), expected.as_slice());
        assert_eq!(policy_metrics.transports.load(Ordering::SeqCst), 1);
        assert_eq!(
            policy_metrics.requests.load(Ordering::SeqCst),
            0,
            "discovery has no query plan to pass to plan authorization",
        );
        assert_eq!(policy_metrics.responses.load(Ordering::SeqCst), 1);
        assert_eq!(
            policy_metrics
                .outcomes
                .lock()
                .expect("policy outcomes")
                .as_slice(),
            [("ok".to_owned(), expected.len())],
        );
        assert_eq!(fixture.metrics.schema_exports.load(Ordering::SeqCst), 1);
        assert_eq!(fixture.metrics.transaction_opens.load(Ordering::SeqCst), 0);

        let rejected_policy = Arc::new(PolicyMetrics::default());
        let rejected = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            pipeline_with_policy(Arc::clone(&rejected_policy), true),
        );
        let response = rejected
            .router
            .clone()
            .oneshot(
                Request::builder()
                    .uri("/v2/capabilities")
                    .body(Body::empty())
                    .expect("request"),
            )
            .await
            .expect("capability rejection");
        let body = response
            .into_body()
            .collect()
            .await
            .expect("rejection body")
            .to_bytes();
        assert_eq!(
            failure_code(&rejected.query, &body),
            "query_remote_policy_rejected"
        );
        assert_eq!(rejected_policy.transports.load(Ordering::SeqCst), 1);
        assert_eq!(rejected_policy.responses.load(Ordering::SeqCst), 1);
        assert_eq!(rejected.metrics.schema_exports.load(Ordering::SeqCst), 0);
        assert_eq!(rejected.metrics.transaction_opens.load(Ordering::SeqCst), 0);
    }

    #[tokio::test]
    async fn one_absolute_deadline_bounds_every_async_transport_stage() {
        let deadline = Instant::now() + Duration::from_millis(10);
        let started = Instant::now();
        let result = await_before_deadline(deadline, std::future::pending::<()>()).await;
        assert!(result.is_none());
        assert!(
            started.elapsed() < Duration::from_secs(1),
            "a pending policy/provider stage must not outlive the round-trip deadline"
        );
        assert_eq!(
            await_before_deadline(Instant::now() + Duration::from_secs(1), async { 7_u8 }).await,
            Some(7),
        );
    }

    #[tokio::test]
    async fn replay_store_is_atomic_and_global_within_an_executor_epoch() {
        let store = Arc::new(InMemoryReplayStore::new(32));
        let expiry = Instant::now() + Duration::from_secs(60);
        let mut tasks = Vec::new();
        for _ in 0..16 {
            let store = Arc::clone(&store);
            tasks.push(tokio::spawn(async move {
                store
                    .reserve("executor-epoch-a", "same-nonce", expiry)
                    .await
            }));
        }
        let mut admitted = 0;
        let mut replayed = 0;
        for task in tasks {
            match task.await.expect("reservation task") {
                Ok(()) => admitted += 1,
                Err(ReplayStoreError::Replayed) => replayed += 1,
                other => panic!("unexpected reservation result: {other:?}"),
            }
        }
        assert_eq!(admitted, 1);
        assert_eq!(replayed, 15);
        assert_eq!(
            store
                .reserve("executor-epoch-a", "same-nonce", expiry)
                .await,
            Err(ReplayStoreError::Replayed),
            "different authenticated clients cannot replay one captured request",
        );
        store
            .reserve("executor-epoch-b", "same-nonce", expiry)
            .await
            .expect("a separately advertised epoch has its own nonce namespace");
    }

    #[test]
    fn standalone_restart_rotates_executor_identity_and_epoch() {
        let first = standalone_executor_binding();
        let restarted = standalone_executor_binding();
        assert_ne!(first.identity(), restarted.identity());
        assert_ne!(first.epoch(), restarted.epoch());

        let capabilities = CapabilitySet::new();
        let first_signer = RemoteReplySigningKey::from_secret_bytes([0x31; 32]);
        let restarted_signer = RemoteReplySigningKey::from_secret_bytes([0x32; 32]);
        let first = RemoteCapabilities::new(capabilities.clone(), first, first_signer.public_key())
            .fingerprint()
            .expect("first advertisement fingerprint");
        let restarted =
            RemoteCapabilities::new(capabilities, restarted, restarted_signer.public_key())
                .fingerprint()
                .expect("restarted advertisement fingerprint");
        assert_ne!(first, restarted);
    }

    #[test]
    fn oversized_executor_capability_set_is_a_typed_construction_error() {
        let authority = authority_fixture(false);
        let metrics = Arc::new(BackendMetrics::default());
        let database = Database::with_backend(
            Box::new(CountingBackend::new([], metrics)),
            "server-v2-capability-limit",
        );
        let mut advertised = CapabilitySet::new();
        for index in 0..=MAX_CANONICAL_COLLECTION_LEN {
            advertised.insert(
                CapabilityId::new(format!("query.generated-{index}"))
                    .expect("generated capability ID"),
            );
        }

        let error = match V2QueryState::new_query_only(
            advertised,
            QueryV2AnswerLimits::default(),
            database,
            authority.declared,
            authority.delta_context,
            authority.managed,
            authority.resolved,
        ) {
            Ok(_) => panic!("oversized advertisement must not construct state"),
            Err(error) => error,
        };
        assert_eq!(error.code().as_str(), "canonical_collection_too_large");
    }

    #[test]
    fn malformed_internal_reply_is_bound_failure_never_success() {
        let fixture = route_fixture(
            [CURRENT_SCHEMA],
            query_plan_capability_vocabulary(),
            default_pipeline(),
        );
        let request = type_bridge_contract::query_remote::RemoteRequestFingerprint::compute(
            b"canonical-request-fixture",
        )
        .expect("request fingerprint");
        let nonce = "internal-reply-nonce-000001";
        let (bytes, success, code) =
            bound_internal_response_failure(&fixture.query, nonce, &request);
        assert!(!success);
        assert_eq!(code, "query_remote_internal_response_invalid");
        let failure = type_bridge_contract::query_remote::decode_signed_remote_failure(
            &bytes,
            &fixture.query.advertisement_fingerprint,
            fixture.advertisement.reply_key(),
            u64::try_from(type_bridge_contract::limits::MAX_REMOTE_ENVELOPE_BYTES)
                .expect("wire ceiling"),
            &Ed25519RemoteReplyVerifier,
        )
        .expect("bound failure");
        failure
            .verify_binding(nonce, &request)
            .expect("internal failure binding");
        assert_eq!(
            failure.diagnostic().expect("diagnostic").code().as_str(),
            "query_remote_internal_response_invalid",
        );
    }

    #[test]
    fn response_audit_grace_is_small_and_independent_of_request_deadline() {
        let started = Instant::now();
        let audit = response_audit_deadline(started + Duration::from_secs(30));
        let grace = audit.duration_since(started);
        assert!(grace >= RESPONSE_AUDIT_GRACE);
        assert!(grace < Duration::from_secs(1));
    }

    #[tokio::test]
    async fn replay_store_expires_entries_and_fails_closed_at_capacity() {
        let store = InMemoryReplayStore::new(1);
        store
            .reserve("executor-epoch", "expired", Instant::now())
            .await
            .expect("first reservation");
        store
            .reserve(
                "executor-epoch",
                "replacement",
                Instant::now() + Duration::from_secs(60),
            )
            .await
            .expect("expired entries are removed");
        assert_eq!(
            store
                .reserve(
                    "executor-epoch",
                    "another",
                    Instant::now() + Duration::from_secs(60),
                )
                .await,
            Err(ReplayStoreError::Capacity),
        );
    }

    #[tokio::test]
    async fn replay_store_ignores_a_stale_expiry_index_entry() {
        let store = InMemoryReplayStore::new(2);
        let key = ("executor-epoch".to_owned(), "still-live".to_owned());
        let live_until = Instant::now() + Duration::from_secs(60);
        {
            let mut reservations = store.reservations.lock().expect("replay store lock");
            reservations.by_key.insert(key.clone(), live_until);
            reservations
                .by_expiry
                .push(Reverse((Instant::now(), key.clone())));
            reservations.by_expiry.push(Reverse((live_until, key)));
        }

        store
            .reserve(
                "executor-epoch",
                "other",
                Instant::now() + Duration::from_secs(60),
            )
            .await
            .expect("stale expiry metadata does not consume capacity");
        assert_eq!(
            store
                .reserve("executor-epoch", "still-live", live_until)
                .await,
            Err(ReplayStoreError::Replayed),
            "an obsolete expiry record must not remove the current reservation",
        );
    }

    #[test]
    fn request_policy_metadata_retains_textual_transport_credentials() {
        let mut headers = HeaderMap::new();
        headers.append("authorization", "Bearer first".parse().unwrap());
        headers.append("authorization", "Bearer second".parse().unwrap());
        let metadata = v2_request_metadata(&headers, "query");
        assert_eq!(metadata["transport"], "v2");
        assert_eq!(metadata["v2_endpoint"], "query");
        assert_eq!(
            metadata["http_headers"]["authorization"],
            serde_json::json!(["Bearer first", "Bearer second"]),
        );
    }
}