type-bridge-contract 2.0.1

Versioned binding-neutral contract primitives for type-bridge
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
//! Versioned fail-closed wire envelopes for remote query execution.
//!
//! One validated plan/result contract serves direct and server execution:
//! the request carries the exact canonical plan bytes plus the invocation
//! (operation, input rows, caller budgets), the exact executor advertisement,
//! an absolute bounded expiry, and a caller nonce; the response binds that
//! nonce and the whole request fingerprint so replayed or foreign evidence is
//! rejected before any host object is constructed. Envelope formats are
//! versioned independently of the plan format.

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

use crate::codec::{
    from_canonical_json_with_limits, to_canonical_json, to_canonical_json_with_limits,
};
use crate::diagnostic::{Diagnostic, DiagnosticCategory, DiagnosticCode};
use crate::fingerprint::{
    CanonicalizationVersion, Fingerprint, FingerprintDigest, FingerprintDomain,
};
use crate::id::TypeId;
use crate::limits::{
    MAX_CANONICAL_COLLECTION_LEN, MAX_REMOTE_ENVELOPE_BYTES, REMOTE_ENVELOPE_CODEC_LIMITS,
    REMOTE_REQUEST_CODEC_LIMITS,
};
use crate::query_plan::{
    InputRow, QueryInvocation, QueryOperation, QueryPlan, QueryPlanFingerprint, decode_query_plan,
};
use crate::value::CanonicalValue;

/// The exact wire discriminator for first-format remote requests.
pub const QUERY_REMOTE_REQUEST_FORMAT_V1: &str = "typebridge.query-remote-request/v1";
/// The exact wire discriminator for first-format remote responses.
pub const QUERY_REMOTE_RESPONSE_FORMAT_V1: &str = "typebridge.query-remote-response/v1";
/// The exact wire discriminator for first-format remote failures.
pub const QUERY_REMOTE_FAILURE_FORMAT_V1: &str = "typebridge.query-remote-failure/v1";
/// The authenticated outer reply format used for both successes and failures.
pub const QUERY_REMOTE_SIGNED_REPLY_FORMAT_V1: &str = "typebridge.query-remote-signed-reply/v1";
/// Domain separating Ed25519 reply signatures from every other signed value.
pub const QUERY_REMOTE_REPLY_SIGNATURE_DOMAIN: &str = "typebridge.query.remote-reply-signature/v1";
/// Domain separating deterministic reply-signing key identifiers.
pub const QUERY_REMOTE_REPLY_KEY_ID_DOMAIN: &str = "typebridge.query.remote-reply-key-id/v1";

const NONCE_MIN_BYTES: usize = 16;
const NONCE_MAX_BYTES: usize = 128;
const EXECUTOR_COMPONENT_MIN_BYTES: usize = 16;
const EXECUTOR_COMPONENT_MAX_BYTES: usize = 128;
/// Default lifetime for a remote request with no explicit caller deadline.
///
/// This matches the standalone executor's mandatory execution timeout and
/// keeps one omitted field from occupying replay capacity for hours.
pub const DEFAULT_REMOTE_DEADLINE_MS: u64 = 30 * 1_000;
/// Longest caller deadline admitted by the first remote format: five minutes.
pub const MAX_REMOTE_DEADLINE_MS: u64 = 5 * 60 * 1_000;
/// Maximum positive client/server wall-clock skew admitted by remote preflight.
///
/// The request lifetime is still bounded to [`MAX_REMOTE_DEADLINE_MS`]
/// between its fingerprint-bound preparation and expiry timestamps. This allowance only
/// prevents a client clock up to one minute ahead from being rejected as a
/// forged future request.
pub const MAX_REMOTE_CLOCK_SKEW_MS: u64 = 60 * 1_000;

/// Caller execution budgets carried with one remote invocation.
///
/// Budgets tighten provider and session ceilings; they never raise them.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteLimits {
    /// Optional wall-clock deadline in milliseconds.
    pub deadline_ms: Option<u64>,
    /// Maximum successful typed-response bytes the caller accepts.
    ///
    /// Authenticated structured failures are control-plane evidence bounded by
    /// [`MAX_REMOTE_ENVELOPE_BYTES`], not by this success-data budget. This lets
    /// an executor report that even the smallest success cannot fit when the
    /// caller deliberately supplies a zero or otherwise tiny budget.
    pub max_bytes: u64,
    /// Maximum answer items the caller accepts.
    pub max_items: u64,
    /// Maximum aggregate list members the caller accepts across documents.
    pub max_collection_members: u64,
}

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
enum RemoteOperation {
    Rows,
    Count,
    Exists,
}

impl RemoteOperation {
    const fn from_operation(operation: QueryOperation) -> Self {
        match operation {
            QueryOperation::Rows => Self::Rows,
            QueryOperation::Count => Self::Count,
            QueryOperation::Exists => Self::Exists,
        }
    }

    const fn operation(self) -> QueryOperation {
        match self {
            Self::Rows => QueryOperation::Rows,
            Self::Count => QueryOperation::Count,
            Self::Exists => QueryOperation::Exists,
        }
    }
}

/// One complete remote invocation of a reusable validated plan.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteQueryRequest {
    // Pre-release wire ledger: before any 2.0.0 artifact shipped, /v1 gained
    // an exact capability-advertisement fingerprint and absolute bounded
    // preparation/expiry timestamps. Relative deadline_ms remains the caller
    // API, but it is resolved once during preparation; executors never restart
    // or replay the full relative window. Before any 2.0.0 artifact shipped,
    // the omitted-deadline lifetime became 30 seconds, the explicit ceiling
    // became five minutes, and decode began requiring the absolute timestamps
    // to equal that fingerprint-bound declared/default lifetime exactly.
    advertisement: String,
    expires_at_unix_ms: u64,
    format: String,
    limits: RemoteLimits,
    nonce: String,
    operation: RemoteOperation,
    // Pre-release wire ledger: the /v1 request embedded the plan as one
    // JSON string until 2.0.0 shipped, which capped remote plans at the
    // 1 MiB per-string ceiling instead of the 16 MiB document limit the
    // plan contract states. The plan now embeds as the canonical JSON
    // object itself, so local and remote share one plan size limit.
    plan: serde_json::Value,
    prepared_at_unix_ms: u64,
    rows: Vec<Vec<Option<CanonicalValue>>>,
}

impl RemoteQueryRequest {
    /// Bind one plan invocation and caller budgets into a request envelope.
    pub fn new(
        plan: &QueryPlan,
        invocation: &QueryInvocation,
        advertisement: &RemoteCapabilities,
        limits: RemoteLimits,
        nonce: impl Into<String>,
        prepared_at_unix_ms: u64,
    ) -> Result<Self, Diagnostic> {
        if !invocation.binds(plan)? {
            return Err(envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_invocation_plan_mismatch",
                "invocation does not bind the exact plan fingerprint",
            ));
        }
        let nonce = nonce.into();
        check_nonce(&nonce)?;
        validate_remote_limits(limits)?;
        let expires_at_unix_ms = prepared_at_unix_ms
            .checked_add(limits.deadline_ms.unwrap_or(DEFAULT_REMOTE_DEADLINE_MS))
            .ok_or_else(remote_time_invalid)?;
        validate_remote_time_shape(prepared_at_unix_ms, expires_at_unix_ms, limits)?;
        let advertisement = advertisement.fingerprint()?.digest_hex();
        // Canonical bytes prove the plan encodes within contract limits;
        // the parsed tree of those exact bytes is what the envelope embeds.
        let plan_value = serde_json::from_slice::<serde_json::Value>(&plan.canonical_bytes()?)
            .map_err(|_| {
                envelope_failure(
                    DiagnosticCategory::Integrity,
                    "query_remote_plan_unencodable",
                    "the plan cannot be embedded as canonical JSON",
                )
            })?;
        Ok(Self {
            advertisement,
            expires_at_unix_ms,
            format: QUERY_REMOTE_REQUEST_FORMAT_V1.to_owned(),
            limits,
            nonce,
            operation: RemoteOperation::from_operation(invocation.operation()),
            plan: plan_value,
            prepared_at_unix_ms,
            rows: invocation
                .inputs()
                .iter()
                .map(|row| row.values().to_vec())
                .collect(),
        })
    }

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

    /// Decode one request envelope, rejecting unknown fields and formats.
    pub fn decode(bytes: &[u8]) -> Result<Self, Diagnostic> {
        let request = from_canonical_json_with_limits::<Self>(bytes, REMOTE_REQUEST_CODEC_LIMITS)?;
        if request.format != QUERY_REMOTE_REQUEST_FORMAT_V1 {
            return Err(envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_format_unsupported",
                "remote request wire format is unsupported",
            ));
        }
        check_nonce(&request.nonce)?;
        validate_remote_limits(request.limits)?;
        FingerprintDigest::from_hex(&request.advertisement).map_err(|_| {
            envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_advertisement_invalid",
                "remote request carries a malformed advertisement fingerprint",
            )
        })?;
        validate_remote_time_shape(
            request.prepared_at_unix_ms,
            request.expires_at_unix_ms,
            request.limits,
        )?;
        if request.encode()? != bytes {
            return Err(envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_request_wire_mismatch",
                "remote request bytes normalize after trusted reconstruction",
            ));
        }
        Ok(request)
    }

    /// Rebuild the trusted plan from the embedded canonical document.
    ///
    /// The embedded tree re-encodes to exact canonical bytes and runs the
    /// full plan wire decoder, so every structural plan check applies to
    /// remote plans exactly as it does to local ones.
    pub fn plan(&self) -> Result<QueryPlan, Diagnostic> {
        decode_query_plan(&to_canonical_json(&self.plan)?)
    }

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

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

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

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

    /// Validate this request's absolute lifetime at one executor clock sample.
    ///
    /// Clients at most [`MAX_REMOTE_CLOCK_SKEW_MS`] ahead are accepted. The
    /// expiry itself is exclusive: a request at or beyond it is rejected.
    /// The returned duration reaches the absolute expiry and is the replay
    /// retention horizon. It can exceed the declared execution duration by
    /// the admitted positive clock skew; use [`Self::remaining_execution_ms`]
    /// to bound execution itself.
    pub fn remaining_lifetime_ms(&self, now_unix_ms: u64) -> Result<u64, Diagnostic> {
        validate_remote_time_shape(
            self.prepared_at_unix_ms,
            self.expires_at_unix_ms,
            self.limits,
        )?;
        if self.prepared_at_unix_ms > now_unix_ms.saturating_add(MAX_REMOTE_CLOCK_SKEW_MS) {
            return Err(envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_time_future",
                "remote request preparation time exceeds the allowed clock skew",
            ));
        }
        self.expires_at_unix_ms
            .checked_sub(now_unix_ms)
            .filter(|remaining| *remaining > 0)
            .ok_or_else(|| {
                envelope_failure(
                    DiagnosticCategory::ResourceLimit,
                    "query_remote_request_expired",
                    "remote request absolute expiry has elapsed",
                )
            })
    }

    /// Return the remaining execution duration without granting clock skew.
    ///
    /// Positive skew can place absolute expiry later than `now + declared
    /// duration`. Execution therefore uses the smaller of the absolute
    /// remaining horizon and the fingerprint-bound declared lifetime, while
    /// replay retention continues through the full absolute horizon.
    pub fn remaining_execution_ms(&self, now_unix_ms: u64) -> Result<u64, Diagnostic> {
        let absolute_remaining = self.remaining_lifetime_ms(now_unix_ms)?;
        let declared_lifetime = self
            .expires_at_unix_ms
            .checked_sub(self.prepared_at_unix_ms)
            .ok_or_else(remote_time_invalid)?;
        Ok(absolute_remaining.min(declared_lifetime))
    }

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

/// One typed value of a remote result row.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RemoteValue {
    /// An entity or relation reference.
    Thing {
        /// The provider instance identity.
        iid: String,
        /// The validated runtime type.
        type_id: TypeId,
    },
    /// An attribute instance with its parsed canonical value.
    Attribute {
        /// The validated runtime attribute type.
        type_id: TypeId,
        /// The exact typed scalar value.
        value: CanonicalValue,
    },
    /// A pure typed value.
    Value {
        /// The exact typed scalar value.
        value: CanonicalValue,
    },
    /// An explicit absence in an optional column.
    Absent,
}

/// One typed field value of a remote fetched document.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RemoteFieldValue {
    /// One exact typed scalar.
    Scalar {
        /// The exact typed scalar value.
        value: CanonicalValue,
    },
    /// An explicit absence in an optional scalar field.
    Absent,
    /// A typed list of attribute values.
    List {
        /// The exact typed list elements.
        values: Vec<CanonicalValue>,
    },
}

/// The typed terminal outcome of one remote invocation.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum RemoteOutcome {
    /// Evidence-validated projected rows in provider order.
    Rows {
        /// Positional row values per validated output column.
        rows: Vec<Vec<RemoteValue>>,
    },
    /// Evidence-validated fetched documents in provider order.
    Documents {
        /// Positional field values per validated document column.
        documents: Vec<Vec<RemoteFieldValue>>,
    },
    /// The exact number of returned answers.
    Count {
        /// The counted answers.
        value: u64,
    },
    /// Whether at least one answer exists.
    Exists {
        /// The existence verdict.
        value: bool,
    },
}

/// Fingerprint domain for whole remote request envelopes.
pub const QUERY_REMOTE_REQUEST_FINGERPRINT_DOMAIN: &str = "typebridge.query.remote-request";
/// Canonicalization identifier for whole remote request envelopes.
pub const QUERY_REMOTE_REQUEST_CANONICALIZATION: &str = "typebridge.query-remote-request/v1";

/// The canonical fingerprint of one complete request envelope.
///
/// Covers every request field — plan bytes, operation, input rows, limits,
/// and nonce — so evidence carrying it is bound to exactly one invocation,
/// never merely to a plan/nonce pair whose rows may differ.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RemoteRequestFingerprint(Fingerprint);

impl RemoteRequestFingerprint {
    /// Compute the fingerprint of exact request envelope bytes.
    pub fn compute(request_bytes: &[u8]) -> Result<Self, Diagnostic> {
        Ok(Self(Fingerprint::compute(
            FingerprintDomain::new(QUERY_REMOTE_REQUEST_FINGERPRINT_DOMAIN)?,
            CanonicalizationVersion::new(QUERY_REMOTE_REQUEST_CANONICALIZATION)?,
            None,
            request_bytes,
        )))
    }

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

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

/// Exact Ed25519 public key trusted to authenticate replies from one executor.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct RemoteSigningPublicKey([u8; 32]);

impl RemoteSigningPublicKey {
    /// Construct a public key from its exact Ed25519 bytes.
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Borrow the exact Ed25519 public-key bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Encode the key as fixed-width lowercase hexadecimal wire text.
    #[must_use]
    pub fn to_hex(self) -> String {
        encode_hex(&self.0)
    }

    fn from_hex(value: &str) -> Result<Self, ()> {
        decode_fixed_hex(value).map(Self)
    }
}

impl std::fmt::Debug for RemoteSigningPublicKey {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_tuple("RemoteSigningPublicKey")
            .field(&self.to_hex())
            .finish()
    }
}

impl Serialize for RemoteSigningPublicKey {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_hex())
    }
}

impl<'de> Deserialize<'de> for RemoteSigningPublicKey {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::from_hex(&value).map_err(|()| serde::de::Error::custom("invalid remote signing key"))
    }
}

/// Deterministic identity of one exact remote reply-signing public key.
///
/// The identifier is domain-separated from every other digest and is carried
/// alongside the key in capability advertisements and signed outer replies.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct RemoteSigningKeyId([u8; 32]);

impl RemoteSigningKeyId {
    /// Derive the identifier for one exact Ed25519 public key.
    #[must_use]
    pub fn for_public_key(key: RemoteSigningPublicKey) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(QUERY_REMOTE_REPLY_KEY_ID_DOMAIN.as_bytes());
        hasher.update([0]);
        hasher.update(key.as_bytes());
        Self(hasher.finalize().into())
    }

    /// Borrow the exact key-identifier bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Encode the identifier as fixed-width lowercase hexadecimal wire text.
    #[must_use]
    pub fn to_hex(self) -> String {
        encode_hex(&self.0)
    }

    fn from_hex(value: &str) -> Result<Self, ()> {
        decode_fixed_hex(value).map(Self)
    }
}

impl std::fmt::Debug for RemoteSigningKeyId {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_tuple("RemoteSigningKeyId")
            .field(&self.to_hex())
            .finish()
    }
}

impl Serialize for RemoteSigningKeyId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_hex())
    }
}

impl<'de> Deserialize<'de> for RemoteSigningKeyId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::from_hex(&value)
            .map_err(|()| serde::de::Error::custom("invalid remote signing key identifier"))
    }
}

/// Exact Ed25519 signature carried by the authenticated outer reply.
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct RemoteReplySignature([u8; 64]);

impl RemoteReplySignature {
    /// Construct a signature from its exact Ed25519 bytes.
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 64]) -> Self {
        Self(bytes)
    }

    /// Borrow the exact Ed25519 signature bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 64] {
        &self.0
    }

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

    fn from_hex(value: &str) -> Result<Self, ()> {
        decode_fixed_hex(value).map(Self)
    }
}

impl std::fmt::Debug for RemoteReplySignature {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("RemoteReplySignature(..)")
    }
}

/// Domain-separated digest signed for one canonical unsigned outer reply.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RemoteReplySigningDigest([u8; 32]);

impl RemoteReplySigningDigest {
    /// Borrow the digest bytes passed to the Ed25519 implementation.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }
}

/// Binding-neutral signing operation used by the contract wire encoder.
pub trait RemoteReplySigner {
    /// Return the public key paired with this signer.
    fn public_key(&self) -> RemoteSigningPublicKey;

    /// Sign one domain-separated canonical reply digest.
    fn sign(&self, digest: &RemoteReplySigningDigest) -> RemoteReplySignature;
}

/// Binding-neutral signature verifier used before any reply payload is decoded.
pub trait RemoteReplyVerifier {
    /// Verify one digest against the exact trusted public key.
    fn verify(
        &self,
        key: RemoteSigningPublicKey,
        digest: &RemoteReplySigningDigest,
        signature: &RemoteReplySignature,
    ) -> bool;
}

/// Expected authenticated success shape used for allocation-free budget scans.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RemoteOutcomeShape {
    /// Selected rows with this exact output width.
    Rows {
        /// Exact number of values in every selected row.
        width: usize,
    },
    /// Fetched documents with this exact output width.
    Documents {
        /// Exact number of fields in every fetched document.
        width: usize,
    },
    /// A scalar count.
    Count,
    /// A scalar existence verdict.
    Exists,
}

/// Caller limits checked before a typed remote outcome is allocated.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct RemoteReplyDecodeLimits {
    /// Expected success outcome shape.
    pub shape: RemoteOutcomeShape,
    /// Maximum authenticated successful outer-reply bytes accepted by the caller.
    ///
    /// Request-bound failure envelopes remain subject to the protocol hard
    /// ceiling so their typed diagnostic can be surfaced at any success budget.
    pub max_bytes: u64,
    /// Maximum rows, documents, count value, or positive existence item accepted by the caller.
    pub max_items: u64,
    /// Maximum aggregate list members accepted across fetched documents.
    pub max_collection_members: u64,
}

/// One successful remote execution bound to its request and plan.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteQueryResponse {
    format: String,
    nonce: String,
    outcome: RemoteOutcome,
    plan: String,
    // Pre-release wire ledger: the /v1 response gained the whole-request
    // binding before any 2.0.0 artifact shipped; no released bytes change.
    request: String,
}

impl RemoteQueryResponse {
    /// Bind one outcome to the request nonce, plan, and whole request.
    pub fn new(
        nonce: impl Into<String>,
        plan: &QueryPlanFingerprint,
        request: &RemoteRequestFingerprint,
        outcome: RemoteOutcome,
    ) -> Result<Self, Diagnostic> {
        let nonce = nonce.into();
        check_nonce(&nonce)?;
        Ok(Self {
            format: QUERY_REMOTE_RESPONSE_FORMAT_V1.to_owned(),
            nonce,
            outcome,
            plan: plan.as_fingerprint().digest().to_hex(),
            request: request.digest_hex(),
        })
    }

    /// Encode one authenticated outer reply using the exact trusted advertisement.
    pub fn encode_signed(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        signer: &impl RemoteReplySigner,
    ) -> Result<Vec<u8>, Diagnostic> {
        encode_signed_reply(&self.encode_payload()?, advertisement, signer)
    }

    /// Return the exact authenticated wire length without invoking a signer.
    pub fn signed_encoded_len(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        key: RemoteSigningPublicKey,
    ) -> Result<usize, Diagnostic> {
        Ok(signed_reply_encoded_len(
            &self.encode_payload()?,
            advertisement,
            key,
        ))
    }

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

    fn decode_bound(bytes: &[u8]) -> Result<Self, Diagnostic> {
        let response =
            from_canonical_json_with_limits::<Self>(bytes, REMOTE_ENVELOPE_CODEC_LIMITS)?;
        if response.format != QUERY_REMOTE_RESPONSE_FORMAT_V1 {
            return Err(envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_format_unsupported",
                "remote response wire format is unsupported",
            ));
        }
        if response.encode_payload()? != bytes {
            return Err(envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_response_wire_mismatch",
                "remote response bytes normalize after trusted reconstruction",
            ));
        }
        Ok(response)
    }

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

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

/// One structured remote failure bound to its request.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteQueryFailure {
    category: DiagnosticCategory,
    code: String,
    format: String,
    message: String,
    nonce: Option<String>,
    // Pre-release wire ledger: the /v1 failure gained the optional
    // whole-request binding before any 2.0.0 artifact shipped.
    request: Option<String>,
}

impl RemoteQueryFailure {
    /// Bind one structured diagnostic to the request nonce.
    #[must_use]
    pub fn new(nonce: Option<String>, diagnostic: &Diagnostic) -> Self {
        Self {
            category: diagnostic.category(),
            code: diagnostic.code().as_str().to_owned(),
            format: QUERY_REMOTE_FAILURE_FORMAT_V1.to_owned(),
            message: diagnostic.message().to_owned(),
            nonce,
            request: None,
        }
    }

    /// Bind one structured diagnostic to the nonce and the exact request.
    #[must_use]
    pub fn bound(
        nonce: impl Into<String>,
        request: &RemoteRequestFingerprint,
        diagnostic: &Diagnostic,
    ) -> Self {
        Self {
            category: diagnostic.category(),
            code: diagnostic.code().as_str().to_owned(),
            format: QUERY_REMOTE_FAILURE_FORMAT_V1.to_owned(),
            message: diagnostic.message().to_owned(),
            nonce: Some(nonce.into()),
            request: Some(request.digest_hex()),
        }
    }

    /// Verify this failure's binding to the request the caller sent.
    ///
    /// Both the nonce and whole-request digest are mandatory on a reply to a
    /// valid request. Pre-decode transport failures use a separate channel;
    /// an unbound envelope is never treated as request-correlated evidence.
    pub fn verify_binding(
        &self,
        expected_nonce: &str,
        expected_request: &RemoteRequestFingerprint,
    ) -> Result<(), Diagnostic> {
        verify_failure_binding(
            self.nonce.as_deref(),
            self.request.as_deref(),
            expected_nonce,
            expected_request,
        )
    }

    /// Encode one authenticated outer reply using the exact trusted advertisement.
    pub fn encode_signed(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        signer: &impl RemoteReplySigner,
    ) -> Result<Vec<u8>, Diagnostic> {
        encode_signed_reply(&self.encode_payload()?, advertisement, signer)
    }

    /// Encode an authenticated failure, replacing an unencodable diagnostic
    /// with a fixed bounded internal failure while preserving safe bindings.
    ///
    /// Server transports use this path so production failures can never
    /// collapse to empty or unsigned bytes merely because an upstream error
    /// message exceeded a codec ceiling.
    #[must_use]
    pub fn encode_signed_or_fallback(
        &self,
        advertisement: &RemoteCapabilitiesFingerprint,
        signer: &impl RemoteReplySigner,
    ) -> Vec<u8> {
        match self.encode_signed(advertisement, signer) {
            Ok(encoded) => encoded,
            Err(_) => encode_minimal_signed_failure(self, advertisement, signer),
        }
    }

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

    /// Decode an already authenticated failure payload.
    pub fn decode_payload(bytes: &[u8]) -> Result<Self, Diagnostic> {
        let failure = from_canonical_json_with_limits::<Self>(bytes, REMOTE_ENVELOPE_CODEC_LIMITS)?;
        if failure.format != QUERY_REMOTE_FAILURE_FORMAT_V1 {
            return Err(envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_format_unsupported",
                "remote failure wire format is unsupported",
            ));
        }
        Ok(failure)
    }

    /// Rebuild the structured diagnostic.
    pub fn diagnostic(&self) -> Result<Diagnostic, Diagnostic> {
        let code = DiagnosticCode::new(self.code.clone()).map_err(|_| {
            envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_code_invalid",
                "remote failure carries a malformed diagnostic code",
            )
        })?;
        Ok(Diagnostic::new(self.category, code, self.message.clone()))
    }

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

/// The exact wire discriminator for first-format capability advertisements.
pub const QUERY_REMOTE_CAPABILITIES_FORMAT_V1: &str = "typebridge.query-remote-capabilities/v1";
/// Fingerprint domain for exact executor capability advertisements.
pub const QUERY_REMOTE_CAPABILITIES_FINGERPRINT_DOMAIN: &str =
    "typebridge.query.remote-capabilities";
/// Canonicalization identifier for capability-advertisement fingerprints.
pub const QUERY_REMOTE_CAPABILITIES_CANONICALIZATION: &str =
    "typebridge.query-remote-capabilities/v1";

/// One logical executor identity and one concrete process/shared-store epoch.
///
/// Standalone executors generate a fresh pair at startup. A multi-instance
/// deployment may share a pair only together with a globally atomic replay
/// store; otherwise advertisements differ and cross-instance requests fail
/// closed at preflight.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteExecutorBinding {
    epoch: String,
    identity: String,
}

impl RemoteExecutorBinding {
    /// Construct one validated executor identity/epoch pair.
    pub fn new(identity: impl Into<String>, epoch: impl Into<String>) -> Result<Self, Diagnostic> {
        let binding = Self {
            epoch: epoch.into(),
            identity: identity.into(),
        };
        validate_executor_component(&binding.identity)?;
        validate_executor_component(&binding.epoch)?;
        Ok(binding)
    }

    /// Return the logical executor identity.
    #[must_use]
    pub fn identity(&self) -> &str {
        &self.identity
    }

    /// Return the concrete process/shared-store epoch.
    #[must_use]
    pub fn epoch(&self) -> &str {
        &self.epoch
    }
}

/// The canonical fingerprint of one exact capability advertisement.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RemoteCapabilitiesFingerprint(Fingerprint);

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

    /// Return the fixed-width lowercase advertisement digest.
    #[must_use]
    pub fn digest_hex(&self) -> String {
        self.0.digest().to_hex()
    }
}

/// One executor capability advertisement for pre-flight negotiation.
///
/// A client checks its plan's required capabilities against this set and
/// refuses to send unsupported plans; the executor re-checks on receipt.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RemoteCapabilities {
    capabilities: crate::capability::CapabilitySet,
    // Pre-release wire ledger: /v1 gained an executor identity and epoch
    // before 2.0.0 shipped. Requests fingerprint these exact bytes so a
    // restart or a different standalone instance cannot accept captured
    // requests prepared for an earlier executor incarnation.
    executor: RemoteExecutorBinding,
    format: String,
    // This key is an explicit caller trust input. Its inclusion in the exact
    // advertisement fingerprint binds every prepared request to one signer.
    reply_key: RemoteSigningPublicKey,
    // The domain-separated identity is redundant by design: decoding verifies
    // it against `reply_key`, while signed replies bind both exact values.
    reply_key_id: RemoteSigningKeyId,
}

impl RemoteCapabilities {
    /// Advertise one executor capability set.
    #[must_use]
    pub fn new(
        capabilities: crate::capability::CapabilitySet,
        executor: RemoteExecutorBinding,
        reply_key: RemoteSigningPublicKey,
    ) -> Self {
        let reply_key_id = RemoteSigningKeyId::for_public_key(reply_key);
        Self {
            capabilities,
            executor,
            format: QUERY_REMOTE_CAPABILITIES_FORMAT_V1.to_owned(),
            reply_key,
            reply_key_id,
        }
    }

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

    /// Decode one advertisement, rejecting unknown fields and formats.
    pub fn decode(bytes: &[u8]) -> Result<Self, Diagnostic> {
        let advertisement =
            from_canonical_json_with_limits::<Self>(bytes, REMOTE_ENVELOPE_CODEC_LIMITS)?;
        if advertisement.format != QUERY_REMOTE_CAPABILITIES_FORMAT_V1 {
            return Err(envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_format_unsupported",
                "remote capability wire format is unsupported",
            ));
        }
        validate_executor_component(advertisement.executor.identity())?;
        validate_executor_component(advertisement.executor.epoch())?;
        if advertisement.reply_key_id != RemoteSigningKeyId::for_public_key(advertisement.reply_key)
        {
            return Err(remote_signature_invalid());
        }
        if advertisement.encode()? != bytes {
            return Err(envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_capabilities_wire_mismatch",
                "remote capability bytes normalize after trusted reconstruction",
            ));
        }
        Ok(advertisement)
    }

    /// Return the advertised capability set.
    #[must_use]
    pub const fn capabilities(&self) -> &crate::capability::CapabilitySet {
        &self.capabilities
    }

    /// Return this advertisement's executor identity and epoch.
    #[must_use]
    pub const fn executor(&self) -> &RemoteExecutorBinding {
        &self.executor
    }

    /// Return the exact public key trusted to authenticate executor replies.
    #[must_use]
    pub const fn reply_key(&self) -> RemoteSigningPublicKey {
        self.reply_key
    }

    /// Return the deterministic identity of the advertised reply key.
    #[must_use]
    pub const fn reply_key_id(&self) -> RemoteSigningKeyId {
        self.reply_key_id
    }

    /// Fingerprint the exact canonical advertisement, including its epoch.
    pub fn fingerprint(&self) -> Result<RemoteCapabilitiesFingerprint, Diagnostic> {
        Ok(RemoteCapabilitiesFingerprint(Fingerprint::compute(
            FingerprintDomain::new(QUERY_REMOTE_CAPABILITIES_FINGERPRINT_DOMAIN)?,
            CanonicalizationVersion::new(QUERY_REMOTE_CAPABILITIES_CANONICALIZATION)?,
            None,
            &self.encode()?,
        )))
    }
}

/// One decoded remote reply: a typed response or a request-bound failure.
#[derive(Clone, Debug, PartialEq)]
pub enum RemoteReply {
    /// A typed successful response, fully bound to the request.
    Response(RemoteQueryResponse),
    /// A structured failure whose request bindings were verified.
    Failure(RemoteQueryFailure),
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SignedRemoteReplyPeek<'wire> {
    #[serde(borrow)]
    advertisement: &'wire str,
    #[serde(borrow)]
    format: &'wire str,
    #[serde(borrow)]
    key: &'wire str,
    #[serde(borrow)]
    key_id: &'wire str,
    #[serde(borrow)]
    payload: &'wire serde_json::value::RawValue,
    #[serde(borrow)]
    signature: &'wire str,
}

/// Borrowed correlation fields parsed before any reply outcome is materialized.
///
/// Canonical replies borrow all four strings directly from the input. Escaped
/// spellings are non-canonical and fail this precheck without allocating;
/// ignored outcome values are traversed without constructing a
/// `serde_json::Value` or a [`RemoteOutcome`].
#[derive(Deserialize)]
struct RemoteReplyBindingPeek<'wire> {
    #[serde(borrow)]
    format: &'wire str,
    #[serde(borrow)]
    nonce: Option<&'wire str>,
    #[serde(borrow)]
    plan: Option<&'wire str>,
    #[serde(borrow)]
    request: Option<&'wire str>,
}

/// Decode one reply envelope of either kind and verify its request binding.
///
/// Success and failure envelopes share one entry point so every caller —
/// including the Python and Node bindings — decodes both outcomes with the
/// same nonce and whole-request correlation checks.
#[expect(
    clippy::too_many_arguments,
    reason = "the trust-boundary API keeps every expected binding and verifier explicit"
)]
pub fn decode_remote_reply(
    bytes: &[u8],
    expected_nonce: &str,
    expected_plan: &QueryPlanFingerprint,
    expected_request: &RemoteRequestFingerprint,
    expected_advertisement: &RemoteCapabilitiesFingerprint,
    trusted_key: RemoteSigningPublicKey,
    limits: RemoteReplyDecodeLimits,
    verifier: &impl RemoteReplyVerifier,
) -> Result<RemoteReply, Diagnostic> {
    // Every reply is capped before JSON parsing or signature work. The caller
    // budget applies only to successful data: applying it before authenticating
    // the reply kind would make a valid request-bound failure undecodable at a
    // tiny budget, including the failure explaining that no success can fit.
    preflight_remote_reply_size(bytes, u64::MAX)?;
    let payload = verify_signed_reply(bytes, expected_advertisement, trusted_key, verifier)?;
    let peek = peek_remote_reply_binding(payload)?;
    if peek.format == QUERY_REMOTE_RESPONSE_FORMAT_V1 {
        verify_response_binding(
            peek.nonce,
            peek.plan,
            peek.request,
            expected_nonce,
            expected_plan,
            expected_request,
        )?;
        preflight_remote_reply_size(bytes, limits.max_bytes)?;
        preflight_remote_response_shape(payload, limits)?;
        return Ok(RemoteReply::Response(RemoteQueryResponse::decode_bound(
            payload,
        )?));
    }
    if peek.format == QUERY_REMOTE_FAILURE_FORMAT_V1 {
        verify_failure_binding(peek.nonce, peek.request, expected_nonce, expected_request)?;
        let failure = RemoteQueryFailure::decode_payload(payload)?;
        failure.verify_binding(expected_nonce, expected_request)?;
        return Ok(RemoteReply::Failure(failure));
    }
    Err(envelope_failure(
        DiagnosticCategory::InvalidContract,
        "query_remote_format_unsupported",
        "remote reply wire format is unsupported",
    ))
}

/// Authenticate and decode an uncorrelated remote failure.
///
/// This is reserved for transport failures that occur before a request can be
/// decoded and fingerprinted. Request-correlated clients must use
/// [`decode_remote_reply`] so nonce, plan, and whole-request bindings are
/// mandatory.
pub fn decode_signed_remote_failure(
    bytes: &[u8],
    expected_advertisement: &RemoteCapabilitiesFingerprint,
    trusted_key: RemoteSigningPublicKey,
    max_bytes: u64,
    verifier: &impl RemoteReplyVerifier,
) -> Result<RemoteQueryFailure, Diagnostic> {
    preflight_remote_reply_size(bytes, max_bytes)?;
    let payload = verify_signed_reply(bytes, expected_advertisement, trusted_key, verifier)?;
    RemoteQueryFailure::decode_payload(payload)
}

fn preflight_remote_reply_size(bytes: &[u8], caller_max_bytes: u64) -> Result<(), Diagnostic> {
    let wire_max_bytes = u64::try_from(MAX_REMOTE_ENVELOPE_BYTES).unwrap_or(u64::MAX);
    let effective_max_bytes = caller_max_bytes.min(wire_max_bytes);
    if u64::try_from(bytes.len()).unwrap_or(u64::MAX) <= effective_max_bytes {
        return Ok(());
    }
    if caller_max_bytes < wire_max_bytes {
        return Err(remote_response_oversized());
    }
    Err(remote_envelope_too_large())
}

/// Shared authenticated-outer-envelope byte preflight for additive payload
/// versions. This does not inspect or reconstruct a version-specific payload.
pub(crate) fn preflight_signed_reply_size(
    bytes: &[u8],
    caller_max_bytes: u64,
) -> Result<(), Diagnostic> {
    preflight_remote_reply_size(bytes, caller_max_bytes)
}

fn verify_signed_reply<'wire>(
    bytes: &'wire [u8],
    expected_advertisement: &RemoteCapabilitiesFingerprint,
    trusted_key: RemoteSigningPublicKey,
    verifier: &impl RemoteReplyVerifier,
) -> Result<&'wire [u8], Diagnostic> {
    let outer = serde_json::from_slice::<SignedRemoteReplyPeek<'wire>>(bytes).map_err(|_| {
        envelope_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_reply_malformed",
            "remote reply is not a signed JSON envelope",
        )
    })?;
    let expected_advertisement = expected_advertisement.digest_hex();
    let expected_key = trusted_key.to_hex();
    let expected_key_id = RemoteSigningKeyId::for_public_key(trusted_key).to_hex();
    let signature =
        RemoteReplySignature::from_hex(outer.signature).map_err(|()| remote_signature_invalid())?;
    if outer.advertisement != expected_advertisement
        || outer.key != expected_key
        || outer.key_id != expected_key_id
        || outer.signature != signature.to_hex()
    {
        return Err(remote_signature_invalid());
    }
    let digest = remote_reply_signing_digest(
        outer.advertisement,
        outer.format,
        outer.key,
        outer.key_id,
        outer.payload.get().as_bytes(),
    );
    if !verifier.verify(trusted_key, &digest, &signature) {
        return Err(remote_signature_invalid());
    }
    if outer.format != QUERY_REMOTE_SIGNED_REPLY_FORMAT_V1 {
        return Err(envelope_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_format_unsupported",
            "signed remote reply wire format is unsupported",
        ));
    }
    let canonical = canonical_signed_reply(
        outer.advertisement,
        outer.format,
        outer.key,
        outer.key_id,
        outer.payload.get().as_bytes(),
        outer.signature,
    );
    if canonical != bytes {
        return Err(envelope_failure(
            DiagnosticCategory::InvalidContract,
            "non_canonical_json",
            "input is valid JSON but not the canonical encoding",
        ));
    }
    Ok(outer.payload.get().as_bytes())
}

/// Authenticate the unchanged signed outer envelope for an additive payload
/// version. Payload dispatch remains the caller's version-specific boundary.
pub(crate) fn verify_signed_reply_payload<'wire>(
    bytes: &'wire [u8],
    expected_advertisement: &RemoteCapabilitiesFingerprint,
    trusted_key: RemoteSigningPublicKey,
    verifier: &impl RemoteReplyVerifier,
) -> Result<&'wire [u8], Diagnostic> {
    verify_signed_reply(bytes, expected_advertisement, trusted_key, verifier)
}

fn encode_signed_reply(
    payload: &[u8],
    advertisement: &RemoteCapabilitiesFingerprint,
    signer: &impl RemoteReplySigner,
) -> Result<Vec<u8>, Diagnostic> {
    let encoded = encode_signed_reply_unchecked(payload, advertisement, signer);
    if encoded.len() > MAX_REMOTE_ENVELOPE_BYTES {
        return Err(envelope_failure(
            DiagnosticCategory::ResourceLimit,
            "query_remote_envelope_too_large",
            "remote reply exceeds the envelope byte ceiling",
        ));
    }
    Ok(encoded)
}

/// Encode additive versioned payload bytes in the unchanged signed outer
/// envelope.
pub(crate) fn encode_signed_reply_payload(
    payload: &[u8],
    advertisement: &RemoteCapabilitiesFingerprint,
    signer: &impl RemoteReplySigner,
) -> Result<Vec<u8>, Diagnostic> {
    encode_signed_reply(payload, advertisement, signer)
}

/// Encode already bounded, canonical additive payload bytes.
///
/// This is used only by the fixed internal-failure fallback after the normal
/// checked encoder has failed. Version-specific modules must construct the
/// payload from static text plus previously validated ASCII bindings.
pub(crate) fn encode_signed_reply_payload_unchecked(
    payload: &[u8],
    advertisement: &RemoteCapabilitiesFingerprint,
    signer: &impl RemoteReplySigner,
) -> Vec<u8> {
    encode_signed_reply_unchecked(payload, advertisement, signer)
}

fn encode_signed_reply_unchecked(
    payload: &[u8],
    advertisement: &RemoteCapabilitiesFingerprint,
    signer: &impl RemoteReplySigner,
) -> Vec<u8> {
    let advertisement = advertisement.digest_hex();
    let key = signer.public_key().to_hex();
    let key_id = RemoteSigningKeyId::for_public_key(signer.public_key()).to_hex();
    let digest = remote_reply_signing_digest(
        &advertisement,
        QUERY_REMOTE_SIGNED_REPLY_FORMAT_V1,
        &key,
        &key_id,
        payload,
    );
    let signature = signer.sign(&digest).to_hex();
    canonical_signed_reply(
        &advertisement,
        QUERY_REMOTE_SIGNED_REPLY_FORMAT_V1,
        &key,
        &key_id,
        payload,
        &signature,
    )
}

fn encode_minimal_signed_failure(
    original: &RemoteQueryFailure,
    advertisement: &RemoteCapabilitiesFingerprint,
    signer: &impl RemoteReplySigner,
) -> Vec<u8> {
    const PREFIX: &[u8] = b"{\"category\":\"integrity\",\"code\":\"query_remote_internal_failure\",\"format\":\"typebridge.query-remote-failure/v1\",\"message\":\"executor could not encode the original remote failure\",\"nonce\":";
    let nonce = original
        .nonce
        .as_deref()
        .filter(|nonce| check_nonce(nonce).is_ok());
    let request = original.request.as_deref().filter(|request| {
        request.len() == 64
            && request
                .bytes()
                .all(|byte| byte.is_ascii_digit() || matches!(byte, b'a'..=b'f'))
    });
    let mut payload = Vec::with_capacity(PREFIX.len() + 256);
    payload.extend_from_slice(PREFIX);
    append_optional_safe_ascii(&mut payload, nonce);
    payload.extend_from_slice(b",\"request\":");
    append_optional_safe_ascii(&mut payload, request);
    payload.push(b'}');
    encode_signed_reply_unchecked(&payload, advertisement, signer)
}

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

fn signed_reply_encoded_len(
    payload: &[u8],
    advertisement: &RemoteCapabilitiesFingerprint,
    key: RemoteSigningPublicKey,
) -> usize {
    canonical_signed_reply(
        &advertisement.digest_hex(),
        QUERY_REMOTE_SIGNED_REPLY_FORMAT_V1,
        &key.to_hex(),
        &RemoteSigningKeyId::for_public_key(key).to_hex(),
        payload,
        &encode_hex(&[0_u8; 64]),
    )
    .len()
}

/// Return the exact unchanged signed-envelope length for additive payload
/// bytes without constructing a signature.
pub(crate) fn signed_reply_payload_encoded_len(
    payload: &[u8],
    advertisement: &RemoteCapabilitiesFingerprint,
    key: RemoteSigningPublicKey,
) -> usize {
    signed_reply_encoded_len(payload, advertisement, key)
}

fn remote_reply_signing_digest(
    advertisement: &str,
    format: &str,
    key: &str,
    key_id: &str,
    payload: &[u8],
) -> RemoteReplySigningDigest {
    let prefix = canonical_signed_reply_prefix(advertisement, format, key, key_id);
    let mut hasher = Sha256::new();
    hasher.update(QUERY_REMOTE_REPLY_SIGNATURE_DOMAIN.as_bytes());
    hasher.update([0]);
    hasher.update(&prefix);
    hasher.update(payload);
    hasher.update(b"}");
    RemoteReplySigningDigest(hasher.finalize().into())
}

fn canonical_signed_reply_prefix(
    advertisement: &str,
    format: &str,
    key: &str,
    key_id: &str,
) -> Vec<u8> {
    format!(
        "{{\"advertisement\":\"{advertisement}\",\"format\":\"{format}\",\"key\":\"{key}\",\"key_id\":\"{key_id}\",\"payload\":"
    )
    .into_bytes()
}

fn canonical_signed_reply(
    advertisement: &str,
    format: &str,
    key: &str,
    key_id: &str,
    payload: &[u8],
    signature: &str,
) -> Vec<u8> {
    let prefix = canonical_signed_reply_prefix(advertisement, format, key, key_id);
    let suffix = format!(",\"signature\":\"{signature}\"}}");
    let mut encoded = Vec::with_capacity(prefix.len() + payload.len() + suffix.len());
    encoded.extend_from_slice(&prefix);
    encoded.extend_from_slice(payload);
    encoded.extend_from_slice(suffix.as_bytes());
    encoded
}

/// Stable rejection for an unauthenticated or foreign remote reply.
#[must_use]
pub fn remote_signature_invalid() -> Diagnostic {
    envelope_failure(
        DiagnosticCategory::Integrity,
        "query_remote_signature_invalid",
        "remote reply signature or trusted executor binding is invalid",
    )
}

fn remote_response_oversized() -> Diagnostic {
    envelope_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_response_oversized",
        "response envelope exceeds the caller byte budget",
    )
}

fn remote_envelope_too_large() -> Diagnostic {
    envelope_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_envelope_too_large",
        "remote reply exceeds the envelope byte ceiling",
    )
}

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

#[derive(Clone, Copy)]
enum SequenceScanKind {
    Rows,
    Documents,
}

#[derive(Clone, Copy)]
enum ShapeLimitExceeded {
    Items,
    Width,
    Members,
    Outcome,
    Evidence,
}

#[derive(Default)]
struct ShapeScanState {
    items: u64,
    members: u64,
    exceeded: Option<ShapeLimitExceeded>,
}

fn preflight_remote_response_shape(
    payload: &[u8],
    limits: RemoteReplyDecodeLimits,
) -> Result<(), Diagnostic> {
    let response =
        serde_json::from_slice::<RemoteResponseOutcomePeek<'_>>(payload).map_err(|_| {
            envelope_failure(
                DiagnosticCategory::InvalidContract,
                "query_remote_reply_malformed",
                "remote response payload is malformed",
            )
        })?;
    let mut state = ShapeScanState::default();
    let mut deserializer = serde_json::Deserializer::from_slice(response.outcome.get().as_bytes());
    let result = match limits.shape {
        RemoteOutcomeShape::Rows { width } => OutcomeShapeSeed {
            field: "rows",
            kind: SequenceScanKind::Rows,
            width,
            max_items: limits
                .max_items
                .min(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).unwrap_or(u64::MAX)),
            max_members: limits
                .max_collection_members
                .min(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).unwrap_or(u64::MAX)),
            state: &mut state,
        }
        .deserialize(&mut deserializer),
        RemoteOutcomeShape::Documents { width } => OutcomeShapeSeed {
            field: "documents",
            kind: SequenceScanKind::Documents,
            width,
            max_items: limits
                .max_items
                .min(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).unwrap_or(u64::MAX)),
            max_members: limits
                .max_collection_members
                .min(u64::try_from(MAX_CANONICAL_COLLECTION_LEN).unwrap_or(u64::MAX)),
            state: &mut state,
        }
        .deserialize(&mut deserializer),
        RemoteOutcomeShape::Count => ScalarOutcomeSeed {
            kind: ScalarScanKind::Count,
            max_items: limits.max_items,
            state: &mut state,
        }
        .deserialize(&mut deserializer),
        RemoteOutcomeShape::Exists => ScalarOutcomeSeed {
            kind: ScalarScanKind::Exists,
            max_items: limits.max_items,
            state: &mut state,
        }
        .deserialize(&mut deserializer),
    };
    if let Some(exceeded) = state.exceeded {
        return Err(match exceeded {
            ShapeLimitExceeded::Items => envelope_failure(
                DiagnosticCategory::ResourceLimit,
                "query_remote_response_oversized",
                "response rows, documents, or scalar evidence exceed the caller item budget",
            ),
            ShapeLimitExceeded::Width => envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_evidence_mismatch",
                "response evidence does not conform to the validated output schema",
            ),
            ShapeLimitExceeded::Members => envelope_failure(
                DiagnosticCategory::ResourceLimit,
                "query_v2_document_member_limit",
                "document lists exceed the aggregate member ceiling",
            ),
            ShapeLimitExceeded::Outcome => envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_outcome_mismatch",
                "response outcome kind does not match the invoked operation",
            ),
            ShapeLimitExceeded::Evidence => envelope_failure(
                DiagnosticCategory::Integrity,
                "query_remote_evidence_mismatch",
                "response evidence does not conform to the validated output schema",
            ),
        });
    }
    result.map_err(|_| {
        envelope_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_reply_malformed",
            "remote response outcome is malformed",
        )
    })
}

#[derive(Clone, Copy)]
enum ScalarScanKind {
    Count,
    Exists,
}

impl ScalarScanKind {
    const fn wire_name(self) -> &'static str {
        match self {
            Self::Count => "count",
            Self::Exists => "exists",
        }
    }
}

struct ScalarOutcomeSeed<'scan> {
    kind: ScalarScanKind,
    max_items: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ScalarOutcomeVisitor { seed: self })
    }
}

struct ScalarOutcomeVisitor<'scan> {
    seed: ScalarOutcomeSeed<'scan>,
}

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

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("an exact count or exists remote outcome object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut found_kind = false;
        let mut found_value = false;
        while let Some(key) = map.next_key::<&str>()? {
            if key == "kind" {
                if found_kind {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                    return Err(serde::de::Error::custom(
                        "remote scalar outcome kind is duplicated",
                    ));
                }
                found_kind = true;
                if map.next_value::<&str>()? != self.seed.kind.wire_name() {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                    return Err(serde::de::Error::custom(
                        "remote scalar outcome kind does not match the expected operation",
                    ));
                }
            } else if key == "value" {
                if found_value {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                    return Err(serde::de::Error::custom(
                        "remote scalar outcome value is duplicated",
                    ));
                }
                found_value = true;
                let value = match self.seed.kind {
                    ScalarScanKind::Count => map.next_value::<u64>().map(|value| {
                        if value > self.seed.max_items {
                            self.seed.state.exceeded = Some(ShapeLimitExceeded::Items);
                        }
                    }),
                    ScalarScanKind::Exists => map.next_value::<bool>().map(|value| {
                        if value && self.seed.max_items == 0 {
                            self.seed.state.exceeded = Some(ShapeLimitExceeded::Items);
                        }
                    }),
                };
                if value.is_err() {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Evidence);
                }
                value?;
            } else {
                self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                return Err(serde::de::Error::custom(
                    "remote scalar outcome carries an unexpected field",
                ));
            }
        }
        if found_kind && found_value {
            Ok(())
        } else {
            self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
            Err(serde::de::Error::custom(
                "remote scalar outcome does not carry its exact fields",
            ))
        }
    }
}

struct OutcomeShapeSeed<'scan> {
    field: &'static str,
    kind: SequenceScanKind,
    width: usize,
    max_items: u64,
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(OutcomeShapeVisitor { seed: self })
    }
}

struct OutcomeShapeVisitor<'scan> {
    seed: OutcomeShapeSeed<'scan>,
}

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

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

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut found = false;
        let mut found_kind = false;
        while let Some(key) = map.next_key::<&str>()? {
            if key == self.seed.field {
                if found {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                    return Err(serde::de::Error::custom(
                        "remote outcome field is duplicated",
                    ));
                }
                found = true;
                map.next_value_seed(OutcomeSequenceSeed {
                    kind: self.seed.kind,
                    width: self.seed.width,
                    max_items: self.seed.max_items,
                    max_members: self.seed.max_members,
                    state: self.seed.state,
                })?;
            } else if key == "kind" {
                if found_kind {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                    return Err(serde::de::Error::custom(
                        "remote outcome kind is duplicated",
                    ));
                }
                found_kind = true;
                if map.next_value::<&str>()? != self.seed.field {
                    self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                    return Err(serde::de::Error::custom(
                        "remote outcome kind does not match the expected shape",
                    ));
                }
            } else {
                self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
                return Err(serde::de::Error::custom(
                    "remote outcome carries an unexpected field",
                ));
            }
        }
        if found && found_kind {
            Ok(())
        } else {
            self.seed.state.exceeded = Some(ShapeLimitExceeded::Outcome);
            Err(serde::de::Error::custom(
                "remote outcome does not carry the expected field",
            ))
        }
    }
}

struct OutcomeSequenceSeed<'scan> {
    kind: SequenceScanKind,
    width: usize,
    max_items: u64,
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(OutcomeSequenceVisitor { seed: self })
    }
}

struct OutcomeSequenceVisitor<'scan> {
    seed: OutcomeSequenceSeed<'scan>,
}

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

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("a remote row or document sequence")
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        loop {
            let next = sequence.next_element_seed(OutputItemSeed {
                kind: self.seed.kind,
                width: self.seed.width,
                max_items: self.seed.max_items,
                max_members: self.seed.max_members,
                state: self.seed.state,
            })?;
            if next.is_none() {
                return Ok(());
            }
        }
    }
}

struct OutputItemSeed<'scan> {
    kind: SequenceScanKind,
    width: usize,
    max_items: u64,
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        self.state.items = self.state.items.saturating_add(1);
        if self.state.items > self.max_items {
            self.state.exceeded = Some(ShapeLimitExceeded::Items);
            return Err(serde::de::Error::custom("remote item budget exceeded"));
        }
        deserializer.deserialize_seq(OutputItemVisitor { seed: self })
    }
}

struct OutputItemVisitor<'scan> {
    seed: OutputItemSeed<'scan>,
}

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

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("one positional remote output item")
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        let mut width = 0_usize;
        loop {
            let next = sequence.next_element_seed(OutputFieldSeed {
                kind: self.seed.kind,
                position: &mut width,
                max_width: self.seed.width,
                max_members: self.seed.max_members,
                state: self.seed.state,
            })?;
            if next.is_none() {
                if width == self.seed.width {
                    return Ok(());
                }
                self.seed.state.exceeded = Some(ShapeLimitExceeded::Width);
                return Err(serde::de::Error::custom(
                    "remote output width does not match the validated shape",
                ));
            }
        }
    }
}

struct OutputFieldSeed<'scan> {
    kind: SequenceScanKind,
    position: &'scan mut usize,
    max_width: usize,
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        *self.position = self.position.saturating_add(1);
        if *self.position > self.max_width {
            self.state.exceeded = Some(ShapeLimitExceeded::Width);
            return Err(serde::de::Error::custom("remote output width exceeded"));
        }
        match self.kind {
            SequenceScanKind::Rows => IgnoredAny::deserialize(deserializer).map(|_| ()),
            SequenceScanKind::Documents => deserializer.deserialize_map(DocumentFieldVisitor {
                max_members: self.max_members,
                state: self.state,
            }),
        }
    }
}

struct DocumentFieldVisitor<'scan> {
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("one remote document field object")
    }

    fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
    where
        M: MapAccess<'de>,
    {
        let mut kind = None;
        let mut value = false;
        let mut values = false;
        while let Some(key) = map.next_key::<&str>()? {
            if key == "kind" {
                if kind.is_some() {
                    self.state.exceeded = Some(ShapeLimitExceeded::Evidence);
                    return Err(serde::de::Error::custom(
                        "remote document field kind is duplicated",
                    ));
                }
                kind = Some(map.next_value::<&str>()?);
            } else if key == "value" {
                if value {
                    self.state.exceeded = Some(ShapeLimitExceeded::Evidence);
                    return Err(serde::de::Error::custom(
                        "remote document scalar value is duplicated",
                    ));
                }
                value = true;
                map.next_value::<IgnoredAny>()?;
            } else if key == "values" {
                if values {
                    self.state.exceeded = Some(ShapeLimitExceeded::Evidence);
                    return Err(serde::de::Error::custom(
                        "remote document list values are duplicated",
                    ));
                }
                values = true;
                map.next_value_seed(MemberSequenceSeed {
                    max_members: self.max_members,
                    state: self.state,
                })?;
            } else {
                self.state.exceeded = Some(ShapeLimitExceeded::Evidence);
                return Err(serde::de::Error::custom(
                    "remote document field carries an unexpected member",
                ));
            }
        }
        let exact = match kind {
            Some("absent") => !value && !values,
            Some("scalar") => value && !values,
            Some("list") => !value && values,
            Some(_) | None => false,
        };
        if exact {
            Ok(())
        } else {
            self.state.exceeded = Some(ShapeLimitExceeded::Evidence);
            Err(serde::de::Error::custom(
                "remote document field does not match its declared kind",
            ))
        }
    }
}

struct MemberSequenceSeed<'scan> {
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(MemberSequenceVisitor { seed: self })
    }
}

struct MemberSequenceVisitor<'scan> {
    seed: MemberSequenceSeed<'scan>,
}

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

    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter.write_str("a remote document member sequence")
    }

    fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        loop {
            let next = sequence.next_element_seed(MemberSeed {
                max_members: self.seed.max_members,
                state: self.seed.state,
            })?;
            if next.is_none() {
                return Ok(());
            }
        }
    }
}

struct MemberSeed<'scan> {
    max_members: u64,
    state: &'scan mut ShapeScanState,
}

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

    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: Deserializer<'de>,
    {
        self.state.members = self.state.members.saturating_add(1);
        if self.state.members > self.max_members {
            self.state.exceeded = Some(ShapeLimitExceeded::Members);
            return Err(serde::de::Error::custom(
                "remote document member budget exceeded",
            ));
        }
        IgnoredAny::deserialize(deserializer).map(|_| ())
    }
}

fn peek_remote_reply_binding(bytes: &[u8]) -> Result<RemoteReplyBindingPeek<'_>, Diagnostic> {
    if bytes.len() > MAX_REMOTE_ENVELOPE_BYTES {
        return Err(envelope_failure(
            DiagnosticCategory::ResourceLimit,
            "query_remote_envelope_too_large",
            "remote reply exceeds the envelope byte ceiling",
        ));
    }
    serde_json::from_slice(bytes).map_err(|_| {
        envelope_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_reply_malformed",
            "remote reply is not a JSON envelope with a format discriminator",
        )
    })
}

fn verify_response_binding(
    nonce: Option<&str>,
    plan: Option<&str>,
    request: Option<&str>,
    expected_nonce: &str,
    expected_plan: &QueryPlanFingerprint,
    expected_request: &RemoteRequestFingerprint,
) -> Result<(), Diagnostic> {
    if nonce != Some(expected_nonce) {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_nonce_mismatch",
            "response evidence does not echo the request nonce",
        ));
    }
    let Some(plan) = plan else {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_plan_mismatch",
            "response evidence does not bind the invoked plan",
        ));
    };
    let echoed = FingerprintDigest::from_hex(plan)?;
    if echoed != expected_plan.as_fingerprint().digest() {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_plan_mismatch",
            "response evidence does not bind the invoked plan",
        ));
    }
    let Some(request) = request else {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_request_mismatch",
            "response evidence does not bind the exact request envelope",
        ));
    };
    let echoed_request = FingerprintDigest::from_hex(request)?;
    if echoed_request != expected_request.as_fingerprint().digest() {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_request_mismatch",
            "response evidence does not bind the exact request envelope",
        ));
    }
    Ok(())
}

fn verify_failure_binding(
    nonce: Option<&str>,
    request: Option<&str>,
    expected_nonce: &str,
    expected_request: &RemoteRequestFingerprint,
) -> Result<(), Diagnostic> {
    let nonce = nonce.ok_or_else(|| {
        envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_failure_unbound",
            "failure evidence is not bound to the request nonce",
        )
    })?;
    if nonce != expected_nonce {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_nonce_mismatch",
            "failure evidence does not echo the request nonce",
        ));
    }
    let request = request.ok_or_else(|| {
        envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_failure_unbound",
            "failure evidence is not bound to the request envelope",
        )
    })?;
    let echoed = FingerprintDigest::from_hex(request)?;
    if echoed != expected_request.as_fingerprint().digest() {
        return Err(envelope_failure(
            DiagnosticCategory::Integrity,
            "query_remote_request_mismatch",
            "failure evidence does not bind the exact request envelope",
        ));
    }
    Ok(())
}

/// Convert one caller-supplied limit into the unsigned wire range.
///
/// Every binding funnels its limit arguments through this exact
/// conversion, so a negative or out-of-range budget fails with one
/// stable diagnostic in every language instead of silently saturating
/// to zero or dropping the budget entirely.
pub fn checked_remote_limit(value: i128) -> Result<u64, Diagnostic> {
    u64::try_from(value).map_err(|_| remote_limit_invalid())
}

/// The stable rejection every out-of-range limit argument maps to.
///
/// Exposed so bindings whose integer representation exceeds `i128`
/// (JavaScript `BigInt`) reject unrepresentable values with exactly
/// this diagnostic instead of inventing their own.
#[must_use]
pub fn remote_limit_invalid() -> Diagnostic {
    envelope_failure(
        DiagnosticCategory::InvalidContract,
        "query_remote_limit_invalid",
        "remote limits are unsigned 64-bit integers",
    )
}

/// Convert one optional caller-supplied deadline into the wire range.
///
/// A negative deadline is rejected — never silently mapped to "no
/// deadline", which would remove the bound instead of enforcing it.
pub fn checked_remote_deadline(value: Option<i128>) -> Result<Option<u64>, Diagnostic> {
    value
        .map(|value| {
            let value = checked_remote_limit(value)?;
            if value > MAX_REMOTE_DEADLINE_MS {
                return Err(remote_deadline_limit());
            }
            Ok(value)
        })
        .transpose()
}

fn validate_remote_limits(limits: RemoteLimits) -> Result<(), Diagnostic> {
    if limits
        .deadline_ms
        .is_some_and(|value| value > MAX_REMOTE_DEADLINE_MS)
    {
        return Err(remote_deadline_limit());
    }
    Ok(())
}

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

fn remote_time_invalid() -> Diagnostic {
    envelope_failure(
        DiagnosticCategory::InvalidContract,
        "query_remote_time_invalid",
        "remote request timestamps do not form a bounded absolute lifetime",
    )
}

/// Stable rejection for a deadline outside the supported monotonic range.
#[must_use]
pub fn remote_deadline_limit() -> Diagnostic {
    envelope_failure(
        DiagnosticCategory::ResourceLimit,
        "query_remote_deadline_limit",
        "remote deadline exceeds the maximum supported duration",
    )
}

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

fn validate_executor_component(value: &str) -> Result<(), Diagnostic> {
    let valid = (EXECUTOR_COMPONENT_MIN_BYTES..=EXECUTOR_COMPONENT_MAX_BYTES)
        .contains(&value.len())
        && value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.'));
    if valid {
        Ok(())
    } else {
        Err(envelope_failure(
            DiagnosticCategory::InvalidContract,
            "query_remote_executor_invalid",
            "executor identity and epoch are 16-128 safe ASCII bytes",
        ))
    }
}

fn encode_hex(bytes: &[u8]) -> String {
    const DIGITS: &[u8; 16] = b"0123456789abcdef";
    let mut encoded = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        encoded.push(char::from(DIGITS[usize::from(byte >> 4)]));
        encoded.push(char::from(DIGITS[usize::from(byte & 0x0f)]));
    }
    encoded
}

fn decode_fixed_hex<const N: usize>(value: &str) -> Result<[u8; N], ()> {
    if value.len() != N * 2 || !value.bytes().all(|byte| byte.is_ascii_hexdigit()) {
        return Err(());
    }
    let mut decoded = [0_u8; N];
    for (index, pair) in value.as_bytes().chunks_exact(2).enumerate() {
        decoded[index] = (hex_nibble(pair[0])? << 4) | hex_nibble(pair[1])?;
    }
    Ok(decoded)
}

fn hex_nibble(byte: u8) -> Result<u8, ()> {
    match byte {
        b'0'..=b'9' => Ok(byte - b'0'),
        b'a'..=b'f' => Ok(byte - b'a' + 10),
        b'A'..=b'F' => Ok(byte - b'A' + 10),
        _ => Err(()),
    }
}

fn envelope_failure(
    category: DiagnosticCategory,
    code: &'static str,
    message: &'static str,
) -> Diagnostic {
    Diagnostic::new(
        category,
        DiagnosticCode::new(code).expect("static remote envelope code"),
        message,
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn canonical_reply_binding_peek_borrows_correlation_strings() {
        let bytes = br#"{"format":"typebridge.query-remote-response/v1","nonce":"remote-nonce-0123456789abcdef","outcome":{"kind":"exists","value":true},"plan":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","request":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"}"#;
        let peek: RemoteReplyBindingPeek<'_> = serde_json::from_slice(bytes).expect("binding peek");

        assert!(bytes.as_ptr_range().contains(&peek.format.as_ptr()));
        assert!(
            peek.nonce
                .is_some_and(|value| bytes.as_ptr_range().contains(&value.as_ptr()))
        );
        assert!(
            peek.plan
                .is_some_and(|value| bytes.as_ptr_range().contains(&value.as_ptr()))
        );
        assert!(
            peek.request
                .is_some_and(|value| bytes.as_ptr_range().contains(&value.as_ptr()))
        );
    }
}