asupersync 0.3.4

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

use crate::atp::path::PathCandidateId;
use crate::net::atp::protocol::frames::{Frame, FrameError, FrameType, ProtocolVersion};
use crate::net::atp::protocol::transcript::{SessionTranscript, TranscriptHash};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::BTreeSet;
use std::fmt;

/// Stable ATP peer identity, normally `sha256(public_key)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct PeerId([u8; 32]);

impl PeerId {
    const PUBLIC_KEY_DOMAIN: &'static [u8] = b"ATP-PEER-PUBLIC-KEY-V1\0";

    /// Construct a peer id from an already-hashed public identity.
    #[must_use]
    pub const fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Derive the canonical peer id from public key material.
    pub fn from_public_key(public_key: &[u8]) -> Result<Self, PeerIdentityError> {
        if public_key.is_empty() {
            return Err(PeerIdentityError::EmptyPublicKey);
        }
        if public_key.iter().all(|byte| *byte == 0) {
            return Err(PeerIdentityError::AllZeroPublicKey);
        }

        let mut hasher = Sha256::new();
        hasher.update(Self::PUBLIC_KEY_DOMAIN);
        hasher.update((public_key.len() as u64).to_be_bytes());
        hasher.update(public_key);
        Ok(Self(hasher.finalize().into()))
    }

    /// Deterministically derive a test/local peer id from a label.
    #[must_use]
    pub fn from_label(label: &str) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(b"ATP-PEER-ID-V1\x00");
        hasher.update(label.as_bytes());
        Self(hasher.finalize().into())
    }

    /// Deterministically derive a peer id for unit tests.
    #[cfg(test)]
    #[must_use]
    pub fn test(id: u64) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(b"ATP-PEER-ID-TEST-V1\x00");
        hasher.update(id.to_be_bytes());
        Self(hasher.finalize().into())
    }

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

    /// Redacted hex prefix suitable for logs and proof artifacts.
    #[must_use]
    pub fn redacted(self) -> String {
        hex::encode(&self.0[..8])
    }
}

/// Invalid public key material for ATP peer identity derivation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PeerIdentityError {
    /// Public key material was empty.
    EmptyPublicKey,
    /// Public key material was all zero bytes.
    AllZeroPublicKey,
}

impl fmt::Display for PeerIdentityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::EmptyPublicKey => f.write_str("empty public key material"),
            Self::AllZeroPublicKey => f.write_str("all-zero public key material"),
        }
    }
}

impl std::error::Error for PeerIdentityError {}

/// Per-transfer nonce bound into the transcript and replay cache.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct TransferNonce([u8; 32]);

impl TransferNonce {
    /// Construct a transfer nonce from caller-provided entropy.
    #[must_use]
    pub const fn new(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Deterministically derive a nonce for tests and lab replay fixtures.
    #[must_use]
    pub fn from_seed(seed: &str) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(b"ATP-TRANSFER-NONCE-V1\x00");
        hasher.update(seed.as_bytes());
        Self(hasher.finalize().into())
    }

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

    /// Whether the nonce is all zero and therefore invalid on the wire.
    #[must_use]
    pub fn is_zero(self) -> bool {
        self.0.iter().all(|byte| *byte == 0)
    }
}

/// Deterministic ATP session identifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SessionId([u8; 32]);

impl SessionId {
    /// Borrow the session-id bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Redacted hex prefix suitable for human diagnostics.
    #[must_use]
    pub fn redacted(self) -> String {
        hex::encode(&self.0[..8])
    }
}

/// ATP trace id carried in logs and proof artifacts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SessionTraceId(u64);

impl SessionTraceId {
    /// Construct a session trace id from a stable numeric value.
    #[must_use]
    pub const fn new(raw: u64) -> Self {
        Self(raw)
    }

    /// Return the raw trace id.
    #[must_use]
    pub const fn get(self) -> u64 {
        self.0
    }
}

/// Where the negotiated ATP session will move data.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum SessionContextKind {
    /// Direct peer-to-peer path.
    Direct,
    /// Online ATP relay path.
    Relay,
    /// Store-and-forward encrypted mailbox path.
    Mailbox,
    /// Multi-source verified transfer.
    Swarm,
}

impl SessionContextKind {
    /// Feature that must be selected for this context, if any.
    #[must_use]
    pub const fn required_feature(self) -> Option<AtpFeature> {
        match self {
            Self::Direct => None,
            Self::Relay => Some(AtpFeature::Relay),
            Self::Mailbox => Some(AtpFeature::Mailbox),
            Self::Swarm => Some(AtpFeature::Swarm),
        }
    }

    /// Stable machine-readable context code.
    #[must_use]
    pub const fn code(self) -> &'static str {
        match self {
            Self::Direct => "direct",
            Self::Relay => "relay",
            Self::Mailbox => "mailbox",
            Self::Swarm => "swarm",
        }
    }
}

/// ATP feature negotiated before object bytes move.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AtpFeature {
    /// RaptorQ or other repair-symbol coordination.
    Repair,
    /// QUIC DATAGRAM or datagram-like relay side channel.
    Datagrams,
    /// Compression plan negotiation.
    Compression,
    /// Explicit encryption policy negotiation.
    EncryptionPolicy,
    /// Multi-source verified transfer.
    Swarm,
    /// Store-and-forward encrypted mailbox delivery.
    Mailbox,
    /// ATP relay path.
    Relay,
    /// H3 adapter mode.
    H3Adapter,
    /// Browser/WebTransport adapter mode.
    WebTransportAdapter,
    /// MASQUE/CONNECT-UDP-style adapter mode.
    MasqueAdapter,
    /// Completion proof bundle exchange.
    ProofBundles,
    /// Crash-safe resume transcript/idempotency support.
    Resume,
}

impl AtpFeature {
    /// Every known ATP feature in canonical order.
    pub const ALL: [Self; 12] = [
        Self::Repair,
        Self::Datagrams,
        Self::Compression,
        Self::EncryptionPolicy,
        Self::Swarm,
        Self::Mailbox,
        Self::Relay,
        Self::H3Adapter,
        Self::WebTransportAdapter,
        Self::MasqueAdapter,
        Self::ProofBundles,
        Self::Resume,
    ];

    /// Stable machine-readable feature code.
    #[must_use]
    pub const fn code(self) -> &'static str {
        match self {
            Self::Repair => "repair",
            Self::Datagrams => "datagrams",
            Self::Compression => "compression",
            Self::EncryptionPolicy => "encryption_policy",
            Self::Swarm => "swarm",
            Self::Mailbox => "mailbox",
            Self::Relay => "relay",
            Self::H3Adapter => "h3_adapter",
            Self::WebTransportAdapter => "webtransport_adapter",
            Self::MasqueAdapter => "masque_adapter",
            Self::ProofBundles => "proof_bundles",
            Self::Resume => "resume",
        }
    }

    /// Stable reason code emitted when this optional feature is offered but not
    /// selected.
    #[must_use]
    pub const fn downgrade_reason_code(self) -> &'static str {
        match self {
            Self::H3Adapter => "h3_adapter_not_supported_by_peer",
            Self::WebTransportAdapter => "webtransport_adapter_not_supported_by_peer",
            Self::MasqueAdapter => "masque_adapter_not_supported_by_peer",
            Self::Datagrams => "datagrams_not_supported_by_selected_adapter",
            Self::Compression => "compression_not_supported_by_peer_policy",
            Self::Relay => "relay_not_supported_by_peer_policy",
            Self::Mailbox => "mailbox_not_supported_by_peer_policy",
            Self::Swarm => "swarm_not_supported_by_peer_policy",
            Self::Repair => "repair_not_supported_by_peer_policy",
            Self::ProofBundles => "proof_bundles_not_supported_by_peer_policy",
            Self::Resume => "resume_not_supported_by_peer_policy",
            Self::EncryptionPolicy => "encryption_policy_required",
        }
    }
}

/// ATP adapter families whose parity and downgrade behavior are tracked.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum AtpAdapterKind {
    /// Native ATP over Asupersync-owned QUIC.
    NativeQuic,
    /// ATP framed over HTTP/3 request/stream semantics.
    H3,
    /// Browser-facing WebTransport adapter.
    WebTransport,
    /// MASQUE CONNECT-UDP enterprise-egress adapter.
    MasqueConnectUdp,
    /// Hostile-network TCP/TLS 443 relay fallback.
    TcpTls443Fallback,
}

impl AtpAdapterKind {
    /// Stable adapter code for diagnostics, docs, and proof summaries.
    #[must_use]
    pub const fn code(self) -> &'static str {
        match self {
            Self::NativeQuic => "native_quic",
            Self::H3 => "h3_adapter",
            Self::WebTransport => "webtransport_adapter",
            Self::MasqueConnectUdp => "masque_connect_udp",
            Self::TcpTls443Fallback => "tcp_tls_443_fallback",
        }
    }

    /// Feature bit that advertises this adapter during session negotiation.
    #[must_use]
    pub const fn negotiated_feature(self) -> Option<AtpFeature> {
        match self {
            Self::NativeQuic | Self::TcpTls443Fallback => None,
            Self::H3 => Some(AtpFeature::H3Adapter),
            Self::WebTransport => Some(AtpFeature::WebTransportAdapter),
            Self::MasqueConnectUdp => Some(AtpFeature::MasqueAdapter),
        }
    }
}

/// One checked row in the ATP adapter parity matrix.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AtpAdapterParity {
    /// Adapter family covered by the row.
    pub adapter: AtpAdapterKind,
    /// Features that may be selected for this adapter without downgrade.
    pub supported_features: &'static [AtpFeature],
    /// Features that must fail closed or downgrade explicitly for this adapter.
    pub unsupported_features: &'static [AtpFeature],
    /// Stable reason emitted when the adapter itself cannot satisfy a requested
    /// capability.
    pub adapter_downgrade_reason: &'static str,
    /// Stable proof summary label for CLI and audit artifacts.
    pub proof_summary_label: &'static str,
}

impl AtpAdapterParity {
    /// Whether this adapter row supports the feature directly.
    #[must_use]
    pub fn supports(self, feature: AtpFeature) -> bool {
        self.supported_features.contains(&feature)
    }

    /// Whether this adapter row explicitly rejects or downgrades the feature.
    #[must_use]
    pub fn downgrades(self, feature: AtpFeature) -> bool {
        self.unsupported_features.contains(&feature)
    }
}

/// Checked ATP adapter parity matrix used by docs, tests, and proof summaries.
pub const ATP_ADAPTER_PARITY_MATRIX: [AtpAdapterParity; 5] = [
    AtpAdapterParity {
        adapter: AtpAdapterKind::NativeQuic,
        supported_features: &[
            AtpFeature::EncryptionPolicy,
            AtpFeature::ProofBundles,
            AtpFeature::Resume,
            AtpFeature::Repair,
            AtpFeature::Datagrams,
            AtpFeature::Compression,
            AtpFeature::Swarm,
            AtpFeature::Mailbox,
            AtpFeature::Relay,
        ],
        unsupported_features: &[
            AtpFeature::H3Adapter,
            AtpFeature::WebTransportAdapter,
            AtpFeature::MasqueAdapter,
        ],
        adapter_downgrade_reason: "native_quic_requires_no_compat_adapter",
        proof_summary_label: "native_quic_full_atp",
    },
    AtpAdapterParity {
        adapter: AtpAdapterKind::H3,
        supported_features: &[
            AtpFeature::EncryptionPolicy,
            AtpFeature::ProofBundles,
            AtpFeature::Resume,
            AtpFeature::Repair,
            AtpFeature::Compression,
            AtpFeature::H3Adapter,
        ],
        unsupported_features: &[
            AtpFeature::Datagrams,
            AtpFeature::WebTransportAdapter,
            AtpFeature::MasqueAdapter,
            AtpFeature::Swarm,
            AtpFeature::Mailbox,
        ],
        adapter_downgrade_reason: "h3_adapter_lacks_native_datagram_and_swarm_parity",
        proof_summary_label: "h3_adapter_stream",
    },
    AtpAdapterParity {
        adapter: AtpAdapterKind::WebTransport,
        supported_features: &[
            AtpFeature::EncryptionPolicy,
            AtpFeature::ProofBundles,
            AtpFeature::Resume,
            AtpFeature::Repair,
            AtpFeature::Datagrams,
            AtpFeature::WebTransportAdapter,
        ],
        unsupported_features: &[
            AtpFeature::H3Adapter,
            AtpFeature::MasqueAdapter,
            AtpFeature::Mailbox,
            AtpFeature::Swarm,
        ],
        adapter_downgrade_reason: "webtransport_adapter_browser_policy_limited",
        proof_summary_label: "webtransport_adapter_browser",
    },
    AtpAdapterParity {
        adapter: AtpAdapterKind::MasqueConnectUdp,
        supported_features: &[
            AtpFeature::EncryptionPolicy,
            AtpFeature::ProofBundles,
            AtpFeature::Resume,
            AtpFeature::Repair,
            AtpFeature::Datagrams,
            AtpFeature::Relay,
            AtpFeature::MasqueAdapter,
        ],
        unsupported_features: &[
            AtpFeature::H3Adapter,
            AtpFeature::WebTransportAdapter,
            AtpFeature::Mailbox,
            AtpFeature::Swarm,
        ],
        adapter_downgrade_reason: "masque_connect_udp_requires_proxy_authority",
        proof_summary_label: "masque_connect_udp_proxy",
    },
    AtpAdapterParity {
        adapter: AtpAdapterKind::TcpTls443Fallback,
        supported_features: &[
            AtpFeature::EncryptionPolicy,
            AtpFeature::ProofBundles,
            AtpFeature::Resume,
            AtpFeature::Repair,
            AtpFeature::Compression,
            AtpFeature::Relay,
        ],
        unsupported_features: &[
            AtpFeature::Datagrams,
            AtpFeature::H3Adapter,
            AtpFeature::WebTransportAdapter,
            AtpFeature::MasqueAdapter,
            AtpFeature::Mailbox,
            AtpFeature::Swarm,
        ],
        adapter_downgrade_reason: "tcp_tls_443_fallback_lacks_datagrams",
        proof_summary_label: "tcp_tls_443_fallback_relay",
    },
];

/// Deterministic set of negotiated ATP features.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FeatureSet {
    features: BTreeSet<AtpFeature>,
}

impl FeatureSet {
    /// Construct an empty feature set.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Construct a feature set from a slice.
    #[must_use]
    pub fn from_slice(features: &[AtpFeature]) -> Self {
        features.iter().copied().collect()
    }

    /// Add a feature.
    pub fn insert(&mut self, feature: AtpFeature) {
        self.features.insert(feature);
    }

    /// Whether a feature is present.
    #[must_use]
    pub fn contains(&self, feature: AtpFeature) -> bool {
        self.features.contains(&feature)
    }

    /// Iterate over features in canonical order.
    pub fn iter(&self) -> impl Iterator<Item = AtpFeature> + '_ {
        self.features.iter().copied()
    }

    /// Select the deterministic intersection with another set.
    #[must_use]
    pub fn intersection(&self, other: &Self) -> Self {
        self.features
            .intersection(&other.features)
            .copied()
            .collect()
    }

    /// Whether this set is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.features.is_empty()
    }
}

impl FromIterator<AtpFeature> for FeatureSet {
    fn from_iter<T: IntoIterator<Item = AtpFeature>>(iter: T) -> Self {
        Self {
            features: iter.into_iter().collect(),
        }
    }
}

/// Downgrade warning emitted when an optional offered feature is not selected.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DowngradeWarning {
    /// Feature that was offered but not selected.
    pub feature: AtpFeature,
    /// Stable reason code.
    pub reason_code: &'static str,
}

/// Capability action authorized by a grant.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum CapabilityAction {
    /// Read an object or object graph.
    Read,
    /// Write or upload object data.
    Write,
    /// Receive an incoming transfer.
    Receive,
    /// Share a capability or transfer offer.
    Share,
    /// Use a relay path.
    Relay,
    /// Seed data for peer-assisted transfer.
    Seed,
    /// Use encrypted mailbox storage.
    Mailbox,
    /// Delegate a narrower grant.
    Delegate,
    /// Invite another peer into a transfer/session.
    Invite,
}

impl CapabilityAction {
    /// Stable action code for logs and transcripts.
    #[must_use]
    pub const fn code(self) -> &'static str {
        match self {
            Self::Read => "read",
            Self::Write => "write",
            Self::Receive => "receive",
            Self::Share => "share",
            Self::Relay => "relay",
            Self::Seed => "seed",
            Self::Mailbox => "mailbox",
            Self::Delegate => "delegate",
            Self::Invite => "invite",
        }
    }
}

/// Stable capability grant id.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct CapabilityGrantId([u8; 16]);

impl CapabilityGrantId {
    /// Construct from caller-provided bytes.
    #[must_use]
    pub const fn new(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }

    /// Deterministically derive a grant id for tests/lab fixtures.
    #[must_use]
    pub fn from_label(label: &str) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(b"ATP-CAPABILITY-GRANT-ID-V1\x00");
        hasher.update(label.as_bytes());
        let digest: [u8; 32] = hasher.finalize().into();
        let mut id = [0u8; 16];
        id.copy_from_slice(&digest[..16]);
        Self(id)
    }

    /// Borrow the grant-id bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 16] {
        &self.0
    }
}

/// Path/object/context restrictions carried by a capability grant.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CapabilityScope {
    /// Whether every ATP path candidate id is allowed.
    pub allow_any_path: bool,
    /// Explicitly allowed path candidate ids.
    pub allowed_path_ids: BTreeSet<PathCandidateId>,
    /// Human/path prefixes for CLI/daemon policy surfaces.
    pub allowed_path_prefixes: BTreeSet<String>,
    /// Whether every trusted relay peer may satisfy a relay context.
    pub allow_any_relay_peer: bool,
    /// Explicit relay peers allowed by this grant.
    pub allowed_relay_peers: BTreeSet<PeerId>,
    /// Whether every manifest/object root is allowed.
    pub allow_any_manifest_root: bool,
    /// Explicitly allowed manifest roots.
    pub allowed_manifest_roots: BTreeSet<[u8; 32]>,
    /// Allowed ATP contexts.
    pub allowed_contexts: BTreeSet<SessionContextKind>,
}

impl CapabilityScope {
    /// Scope with no path/object/context restriction.
    #[must_use]
    pub fn unrestricted() -> Self {
        Self {
            allow_any_path: true,
            allowed_path_ids: BTreeSet::new(),
            allowed_path_prefixes: BTreeSet::new(),
            allow_any_relay_peer: true,
            allowed_relay_peers: BTreeSet::new(),
            allow_any_manifest_root: true,
            allowed_manifest_roots: BTreeSet::new(),
            allowed_contexts: SessionContextKind::ALL.into_iter().collect(),
        }
    }

    /// Scope for one context.
    #[must_use]
    pub fn for_context(context: SessionContextKind) -> Self {
        Self {
            allowed_contexts: std::iter::once(context).collect(),
            ..Self::unrestricted()
        }
    }

    /// Restrict to a single path candidate id.
    #[must_use]
    pub fn with_path_id(mut self, path_id: PathCandidateId) -> Self {
        self.allow_any_path = false;
        self.allowed_path_ids.insert(path_id);
        self
    }

    /// Restrict to a single trusted relay peer.
    #[must_use]
    pub fn with_relay_peer(mut self, relay_peer: PeerId) -> Self {
        self.allow_any_relay_peer = false;
        self.allowed_relay_peers.insert(relay_peer);
        self
    }

    /// Restrict to a manifest root.
    #[must_use]
    pub fn with_manifest_root(mut self, manifest_root: [u8; 32]) -> Self {
        self.allow_any_manifest_root = false;
        self.allowed_manifest_roots.insert(manifest_root);
        self
    }

    fn allows_request(&self, hello: &ClientHello) -> Result<(), SessionError> {
        if !self.allowed_contexts.contains(&hello.context) {
            return Err(SessionError::ContextDenied(hello.context));
        }

        if !self.allow_any_path {
            let path_id = hello
                .path_id
                .ok_or(SessionError::PathScopeDenied { path_id: None })?;
            if !self.allowed_path_ids.contains(&path_id) {
                return Err(SessionError::PathScopeDenied {
                    path_id: Some(path_id),
                });
            }
        }

        if matches!(hello.context, SessionContextKind::Relay) && !self.allow_any_relay_peer {
            let relay_peer = hello
                .relay_peer
                .ok_or(SessionError::RelayScopeDenied { relay_peer: None })?;
            if !self.allowed_relay_peers.contains(&relay_peer) {
                return Err(SessionError::RelayScopeDenied {
                    relay_peer: Some(relay_peer),
                });
            }
        }

        if !self.allow_any_manifest_root {
            let manifest_root = hello.manifest_root.ok_or(SessionError::ObjectScopeDenied {
                manifest_root: None,
            })?;
            if !self.allowed_manifest_roots.contains(&manifest_root) {
                return Err(SessionError::ObjectScopeDenied {
                    manifest_root: Some(manifest_root),
                });
            }
        }

        Ok(())
    }
}

impl SessionContextKind {
    /// Every session context in canonical order.
    pub const ALL: [Self; 4] = [Self::Direct, Self::Relay, Self::Mailbox, Self::Swarm];
}

/// Capability grant supplied during negotiation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CapabilityGrant {
    /// Grant id.
    pub id: CapabilityGrantId,
    /// Peer that issued the grant.
    pub issuer: PeerId,
    /// Peer that may exercise the grant.
    pub subject: PeerId,
    /// Allowed actions.
    pub actions: BTreeSet<CapabilityAction>,
    /// Scope restrictions.
    pub scope: CapabilityScope,
    /// Earliest valid timestamp in microseconds.
    pub valid_from_micros: u64,
    /// Expiry timestamp in microseconds, if bounded.
    pub expires_at_micros: Option<u64>,
    /// Revoked grants fail closed.
    pub revoked: bool,
    /// Remaining delegation depth.
    pub delegation_depth: u8,
    /// Whether invite-style delegation is allowed.
    pub invite_scope: bool,
}

impl CapabilityGrant {
    /// Construct a grant.
    #[must_use]
    pub fn new(
        id: CapabilityGrantId,
        issuer: PeerId,
        subject: PeerId,
        actions: impl IntoIterator<Item = CapabilityAction>,
        scope: CapabilityScope,
    ) -> Self {
        Self {
            id,
            issuer,
            subject,
            actions: actions.into_iter().collect(),
            scope,
            valid_from_micros: 0,
            expires_at_micros: None,
            revoked: false,
            delegation_depth: 0,
            invite_scope: false,
        }
    }

    /// Set the validity window.
    #[must_use]
    pub const fn with_validity(mut self, valid_from_micros: u64, expires_at_micros: u64) -> Self {
        self.valid_from_micros = valid_from_micros;
        self.expires_at_micros = Some(expires_at_micros);
        self
    }

    /// Mark the grant revoked.
    #[must_use]
    pub const fn revoked(mut self) -> Self {
        self.revoked = true;
        self
    }

    /// Allow bounded delegation.
    #[must_use]
    pub const fn with_delegation(mut self, depth: u8, invite_scope: bool) -> Self {
        self.delegation_depth = depth;
        self.invite_scope = invite_scope;
        self
    }

    fn validate_for(
        &self,
        hello: &ClientHello,
        action: CapabilityAction,
        policy: &SessionPolicy,
    ) -> Result<(), SessionError> {
        if self.revoked {
            return Err(SessionError::GrantRevoked(self.id));
        }
        if self.valid_from_micros > policy.now_micros {
            return Err(SessionError::GrantNotYetValid(self.id));
        }
        if self
            .expires_at_micros
            .is_some_and(|expires_at| expires_at <= policy.now_micros)
        {
            return Err(SessionError::GrantExpired(self.id));
        }
        if self.subject != hello.initiator {
            return Err(SessionError::PeerConfusion);
        }
        if !policy.trusted_grant_issuers.contains(&self.issuer) {
            return Err(SessionError::UntrustedGrantIssuer(self.issuer));
        }
        if !self.actions.contains(&action) {
            return Err(SessionError::MissingGrantAction(action));
        }
        if matches!(action, CapabilityAction::Delegate) && self.delegation_depth == 0 {
            return Err(SessionError::DelegationDenied(self.id));
        }
        if matches!(action, CapabilityAction::Invite) && !self.invite_scope {
            return Err(SessionError::InviteDenied(self.id));
        }
        self.scope.allows_request(hello)
    }
}

/// Policy applied by the accepting peer before it sends `ServerHello`.
#[derive(Debug, Clone)]
pub struct SessionPolicy {
    /// Local accepting peer.
    pub local_peer: PeerId,
    /// Supported protocol versions.
    pub supported_versions: BTreeSet<ProtocolVersion>,
    /// Supported optional features.
    pub supported_features: FeatureSet,
    /// Required features that must be offered and selected.
    pub required_features: FeatureSet,
    /// Required capability actions for this acceptor.
    pub required_actions: BTreeSet<CapabilityAction>,
    /// Contexts this peer is willing to negotiate.
    pub accepted_contexts: BTreeSet<SessionContextKind>,
    /// Trusted grant issuers.
    pub trusted_grant_issuers: BTreeSet<PeerId>,
    /// Relay peers whose identity may be used for relay sessions.
    pub trusted_relays: BTreeSet<PeerId>,
    /// Replay cache for transfer nonces.
    pub seen_nonces: BTreeSet<TransferNonce>,
    /// Policy clock in microseconds.
    pub now_micros: u64,
    /// Whether the session must be bound to a known manifest root.
    pub require_manifest_binding: bool,
}

impl SessionPolicy {
    /// Construct a conservative policy for a local peer.
    #[must_use]
    pub fn new(local_peer: PeerId, now_micros: u64) -> Self {
        Self {
            local_peer,
            supported_versions: std::iter::once(ProtocolVersion::CURRENT).collect(),
            supported_features: FeatureSet::from_slice(&[
                AtpFeature::EncryptionPolicy,
                AtpFeature::ProofBundles,
                AtpFeature::Resume,
            ]),
            required_features: FeatureSet::from_slice(&[AtpFeature::EncryptionPolicy]),
            required_actions: BTreeSet::new(),
            accepted_contexts: SessionContextKind::ALL.into_iter().collect(),
            trusted_grant_issuers: std::iter::once(local_peer).collect(),
            trusted_relays: BTreeSet::new(),
            seen_nonces: BTreeSet::new(),
            now_micros,
            require_manifest_binding: false,
        }
    }

    /// Add supported features.
    #[must_use]
    pub fn with_supported_features(mut self, features: &[AtpFeature]) -> Self {
        self.supported_features = FeatureSet::from_slice(features);
        self
    }

    /// Add required feature policy.
    #[must_use]
    pub fn with_required_features(mut self, features: &[AtpFeature]) -> Self {
        self.required_features = FeatureSet::from_slice(features);
        self
    }

    /// Add required capability actions.
    #[must_use]
    pub fn with_required_actions(mut self, actions: &[CapabilityAction]) -> Self {
        self.required_actions = actions.iter().copied().collect();
        self
    }

    /// Restrict accepted contexts.
    #[must_use]
    pub fn with_accepted_contexts(mut self, contexts: &[SessionContextKind]) -> Self {
        self.accepted_contexts = contexts.iter().copied().collect();
        self
    }

    /// Trust relay peers for relay-context negotiation.
    #[must_use]
    pub fn with_trusted_relays(mut self, relays: &[PeerId]) -> Self {
        self.trusted_relays = relays.iter().copied().collect();
        self
    }

    /// Mark a nonce as already seen.
    #[must_use]
    pub fn with_seen_nonce(mut self, nonce: TransferNonce) -> Self {
        self.seen_nonces.insert(nonce);
        self
    }

    /// Require a manifest root binding.
    #[must_use]
    pub const fn require_manifest_binding(mut self) -> Self {
        self.require_manifest_binding = true;
        self
    }
}

/// Client hello fields bound into the transcript.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientHello {
    /// Initiating peer.
    pub initiator: PeerId,
    /// Expected accepting peer.
    pub responder: PeerId,
    /// Transfer nonce.
    pub nonce: TransferNonce,
    /// Requested protocol version.
    pub version: ProtocolVersion,
    /// Optional object/manifest root known at session setup time.
    pub manifest_root: Option<[u8; 32]>,
    /// Optional path candidate id.
    pub path_id: Option<PathCandidateId>,
    /// Relay peer identity required for relay contexts.
    pub relay_peer: Option<PeerId>,
    /// Negotiation context.
    pub context: SessionContextKind,
    /// Offered feature set.
    pub offered_features: FeatureSet,
    /// Capability grants presented by the initiator.
    pub grants: Vec<CapabilityGrant>,
    /// Actions requested by the initiator.
    pub requested_actions: BTreeSet<CapabilityAction>,
    /// Trace id for diagnostics.
    pub trace_id: SessionTraceId,
}

impl ClientHello {
    /// Construct a client hello.
    #[must_use]
    pub fn new(
        initiator: PeerId,
        responder: PeerId,
        nonce: TransferNonce,
        context: SessionContextKind,
        trace_id: SessionTraceId,
    ) -> Self {
        Self {
            initiator,
            responder,
            nonce,
            version: ProtocolVersion::CURRENT,
            manifest_root: None,
            path_id: None,
            relay_peer: None,
            context,
            offered_features: FeatureSet::from_slice(&[AtpFeature::EncryptionPolicy]),
            grants: Vec::new(),
            requested_actions: BTreeSet::new(),
            trace_id,
        }
    }

    /// Attach offered features.
    #[must_use]
    pub fn with_features(mut self, features: &[AtpFeature]) -> Self {
        self.offered_features = FeatureSet::from_slice(features);
        self
    }

    /// Attach a manifest root.
    #[must_use]
    pub const fn with_manifest_root(mut self, manifest_root: [u8; 32]) -> Self {
        self.manifest_root = Some(manifest_root);
        self
    }

    /// Attach a path candidate id.
    #[must_use]
    pub const fn with_path_id(mut self, path_id: PathCandidateId) -> Self {
        self.path_id = Some(path_id);
        self
    }

    /// Attach the relay peer identity for relay-context negotiation.
    #[must_use]
    pub const fn with_relay_peer(mut self, relay_peer: PeerId) -> Self {
        self.relay_peer = Some(relay_peer);
        self
    }

    /// Present grants to the acceptor.
    #[must_use]
    pub fn with_grants(mut self, grants: Vec<CapabilityGrant>) -> Self {
        self.grants = grants;
        self
    }

    /// Request capability actions.
    #[must_use]
    pub fn with_requested_actions(mut self, actions: &[CapabilityAction]) -> Self {
        self.requested_actions = actions.iter().copied().collect();
        self
    }

    /// Convert to a canonical ATP frame.
    pub fn to_frame(&self) -> Result<Frame, SessionError> {
        Frame::new(
            self.version,
            FrameType::Handshake,
            self.to_canonical_bytes(),
        )
        .map_err(SessionError::Frame)
    }

    fn to_canonical_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::new();
        put_peer_id(&mut bytes, self.initiator);
        put_peer_id(&mut bytes, self.responder);
        bytes.extend_from_slice(self.nonce.as_bytes());
        bytes.extend_from_slice(&self.version.0.to_be_bytes());
        put_optional_hash(&mut bytes, self.manifest_root);
        put_optional_u64(&mut bytes, self.path_id.map(PathCandidateId::get));
        put_optional_peer_id(&mut bytes, self.relay_peer);
        bytes.push(context_code(self.context));
        put_features(&mut bytes, &self.offered_features);
        put_actions(&mut bytes, &self.requested_actions);
        bytes.extend_from_slice(&self.trace_id.get().to_be_bytes());
        put_grants(&mut bytes, &self.grants);
        bytes
    }
}

/// Server hello fields bound into the transcript.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServerHello {
    /// Negotiated session id.
    pub session_id: SessionId,
    /// Accepting peer.
    pub acceptor: PeerId,
    /// Initiating peer.
    pub initiator: PeerId,
    /// Transfer nonce from the client hello.
    pub nonce: TransferNonce,
    /// Accepted protocol version.
    pub version: ProtocolVersion,
    /// Negotiation context.
    pub context: SessionContextKind,
    /// Selected features.
    pub selected_features: FeatureSet,
    /// Non-fatal downgrade warnings.
    pub downgrade_warnings: Vec<DowngradeWarning>,
    /// Grants that authorized the requested actions.
    pub accepted_grants: Vec<CapabilityGrantId>,
    /// Trace id carried from the client hello.
    pub trace_id: SessionTraceId,
}

impl ServerHello {
    /// Convert to a canonical ATP frame.
    pub fn to_frame(&self) -> Result<Frame, SessionError> {
        Frame::new(
            self.version,
            FrameType::HandshakeAck,
            self.to_canonical_bytes(),
        )
        .map_err(SessionError::Frame)
    }

    fn to_canonical_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::new();
        bytes.extend_from_slice(self.session_id.as_bytes());
        put_peer_id(&mut bytes, self.acceptor);
        put_peer_id(&mut bytes, self.initiator);
        bytes.extend_from_slice(self.nonce.as_bytes());
        bytes.extend_from_slice(&self.version.0.to_be_bytes());
        bytes.push(context_code(self.context));
        put_features(&mut bytes, &self.selected_features);
        put_u32(&mut bytes, self.downgrade_warnings.len());
        for warning in &self.downgrade_warnings {
            bytes.push(feature_code(warning.feature));
            put_str(&mut bytes, warning.reason_code);
        }
        put_u32(&mut bytes, self.accepted_grants.len());
        for grant_id in &self.accepted_grants {
            bytes.extend_from_slice(grant_id.as_bytes());
        }
        bytes.extend_from_slice(&self.trace_id.get().to_be_bytes());
        bytes
    }
}

/// Terminal negotiated session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NegotiatedSession {
    /// Session id.
    pub session_id: SessionId,
    /// Local peer for the state machine that produced this value.
    pub local_peer: PeerId,
    /// Remote peer.
    pub remote_peer: PeerId,
    /// Transfer nonce.
    pub nonce: TransferNonce,
    /// Protocol version.
    pub version: ProtocolVersion,
    /// Selected context.
    pub context: SessionContextKind,
    /// Selected features.
    pub selected_features: FeatureSet,
    /// Accepted grants.
    pub accepted_grants: Vec<CapabilityGrantId>,
    /// Transcript hash after hello and ack.
    pub transcript_hash: TranscriptHash,
    /// Trace id.
    pub trace_id: SessionTraceId,
}

/// User-facing proof/log artifact for session negotiation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionProofArtifact {
    /// Local peer id, redacted.
    pub local_peer: String,
    /// Remote peer id, redacted.
    pub remote_peer: String,
    /// Session id, if negotiation reached that point.
    pub session_id: Option<String>,
    /// Transfer nonce.
    pub transfer_nonce: TransferNonce,
    /// Selected feature codes.
    pub selected_features: Vec<&'static str>,
    /// Rejected feature/grant/path/object reason, if any.
    pub rejected_reason: Option<String>,
    /// Redacted transcript hash.
    pub transcript_hash: String,
    /// Cx/session trace id.
    pub trace_id: SessionTraceId,
}

impl SessionProofArtifact {
    fn accepted(
        local_peer: PeerId,
        remote_peer: PeerId,
        session_id: SessionId,
        nonce: TransferNonce,
        selected_features: &FeatureSet,
        transcript_hash: TranscriptHash,
        trace_id: SessionTraceId,
    ) -> Self {
        Self {
            local_peer: local_peer.redacted(),
            remote_peer: remote_peer.redacted(),
            session_id: Some(session_id.redacted()),
            transfer_nonce: nonce,
            selected_features: selected_features.iter().map(AtpFeature::code).collect(),
            rejected_reason: None,
            transcript_hash: redact_transcript_hash(transcript_hash),
            trace_id,
        }
    }

    fn rejected(
        local_peer: PeerId,
        remote_peer: PeerId,
        nonce: TransferNonce,
        reason: &SessionError,
        transcript_hash: TranscriptHash,
        trace_id: SessionTraceId,
    ) -> Self {
        Self {
            local_peer: local_peer.redacted(),
            remote_peer: remote_peer.redacted(),
            session_id: None,
            transfer_nonce: nonce,
            selected_features: Vec::new(),
            rejected_reason: Some(reason.code().to_string()),
            transcript_hash: redact_transcript_hash(transcript_hash),
            trace_id,
        }
    }
}

/// Role of a session state machine.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SessionRole {
    /// Initiating peer.
    Client,
    /// Accepting peer.
    Server,
}

/// Deterministic session negotiation state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SessionNegotiationState {
    /// No frame has been processed.
    Idle,
    /// Client hello has been sent by the initiator.
    ClientHelloSent,
    /// Server hello has been sent by the acceptor.
    ServerHelloSent,
    /// Session is fully established.
    Established(SessionId),
    /// Session failed closed.
    Rejected(String),
    /// Session is closed.
    Closed,
}

/// ATP session negotiation state machine.
#[derive(Debug, Clone)]
pub struct SessionNegotiator {
    role: SessionRole,
    local_peer: PeerId,
    state: SessionNegotiationState,
    transcript: SessionTranscript,
}

impl SessionNegotiator {
    /// Construct a client-side session negotiator.
    #[must_use]
    pub fn client(local_peer: PeerId) -> Self {
        Self::new(SessionRole::Client, local_peer)
    }

    /// Construct a server-side session negotiator.
    #[must_use]
    pub fn server(local_peer: PeerId) -> Self {
        Self::new(SessionRole::Server, local_peer)
    }

    fn new(role: SessionRole, local_peer: PeerId) -> Self {
        Self {
            role,
            local_peer,
            state: SessionNegotiationState::Idle,
            transcript: SessionTranscript::new(),
        }
    }

    /// Current state.
    #[must_use]
    pub const fn state(&self) -> &SessionNegotiationState {
        &self.state
    }

    /// Start client negotiation and produce the handshake frame.
    pub fn start_client_hello(&mut self, hello: &ClientHello) -> Result<Frame, SessionError> {
        self.expect_role(SessionRole::Client)?;
        self.expect_state(&SessionNegotiationState::Idle)?;
        if hello.initiator != self.local_peer {
            return self.reject(SessionError::PeerConfusion);
        }
        validate_nonce(hello.nonce)?;
        let frame = hello.to_frame()?;
        self.transcript.add_frame(&frame);
        self.state = SessionNegotiationState::ClientHelloSent;
        Ok(frame)
    }

    /// Accept a client hello, select features, and produce a server hello frame.
    pub fn accept_client_hello(
        &mut self,
        hello: &ClientHello,
        policy: &mut SessionPolicy,
    ) -> Result<(ServerHello, Frame, SessionProofArtifact), SessionError> {
        self.expect_role(SessionRole::Server)?;
        self.expect_state(&SessionNegotiationState::Idle)?;
        if policy.local_peer != self.local_peer {
            return self.reject(SessionError::PeerConfusion);
        }
        if hello.responder != self.local_peer {
            return self.reject(SessionError::PeerConfusion);
        }

        let client_frame = hello.to_frame()?;
        self.transcript.add_frame(&client_frame);

        match build_server_hello(hello, policy) {
            Ok(server_hello) => {
                let server_frame = server_hello.to_frame()?;
                self.transcript.add_frame(&server_frame);
                self.state = SessionNegotiationState::ServerHelloSent;
                let transcript_hash = self.transcript.current_hash();
                let proof = SessionProofArtifact::accepted(
                    self.local_peer,
                    hello.initiator,
                    server_hello.session_id,
                    hello.nonce,
                    &server_hello.selected_features,
                    transcript_hash,
                    hello.trace_id,
                );
                Ok((server_hello, server_frame, proof))
            }
            Err(error) => {
                let proof = SessionProofArtifact::rejected(
                    self.local_peer,
                    hello.initiator,
                    hello.nonce,
                    &error,
                    self.transcript.current_hash(),
                    hello.trace_id,
                );
                self.state = SessionNegotiationState::Rejected(error.code().to_string());
                Err(error.with_proof(proof))
            }
        }
    }

    /// Finish client negotiation after receiving a server hello.
    pub fn finish_client(
        &mut self,
        hello: &ClientHello,
        server_hello: &ServerHello,
        policy: &SessionPolicy,
    ) -> Result<(NegotiatedSession, SessionProofArtifact), SessionError> {
        self.expect_role(SessionRole::Client)?;
        self.expect_state(&SessionNegotiationState::ClientHelloSent)?;
        if hello.initiator != self.local_peer || server_hello.initiator != self.local_peer {
            return self.reject(SessionError::PeerConfusion);
        }
        validate_server_hello(hello, server_hello, policy)?;
        let server_frame = server_hello.to_frame()?;
        self.transcript.add_frame(&server_frame);
        self.state = SessionNegotiationState::Established(server_hello.session_id);
        let transcript_hash = self.transcript.current_hash();
        let session = NegotiatedSession {
            session_id: server_hello.session_id,
            local_peer: self.local_peer,
            remote_peer: server_hello.acceptor,
            nonce: hello.nonce,
            version: server_hello.version,
            context: server_hello.context,
            selected_features: server_hello.selected_features.clone(),
            accepted_grants: server_hello.accepted_grants.clone(),
            transcript_hash,
            trace_id: hello.trace_id,
        };
        let proof = SessionProofArtifact::accepted(
            self.local_peer,
            server_hello.acceptor,
            session.session_id,
            session.nonce,
            &session.selected_features,
            session.transcript_hash,
            session.trace_id,
        );
        Ok((session, proof))
    }

    fn expect_role(&self, expected: SessionRole) -> Result<(), SessionError> {
        if self.role == expected {
            Ok(())
        } else {
            Err(SessionError::InvalidRole {
                expected,
                actual: self.role,
            })
        }
    }

    fn expect_state(&self, expected: &SessionNegotiationState) -> Result<(), SessionError> {
        if &self.state == expected {
            Ok(())
        } else {
            Err(SessionError::InvalidTransition {
                from: format!("{:?}", self.state),
                expected: format!("{expected:?}"),
            })
        }
    }

    fn reject<T>(&mut self, error: SessionError) -> Result<T, SessionError> {
        self.state = SessionNegotiationState::Rejected(error.code().to_string());
        Err(error)
    }
}

/// Session negotiation errors.
#[derive(Debug, thiserror::Error)]
pub enum SessionError {
    /// Underlying frame error.
    #[error("frame error: {0}")]
    Frame(#[from] FrameError),
    /// State transition was not legal.
    #[error("invalid transition from {from}; expected {expected}")]
    InvalidTransition {
        /// Actual state.
        from: String,
        /// Expected state.
        expected: String,
    },
    /// Method called on the wrong negotiator role.
    #[error("invalid role: expected {expected:?}, actual {actual:?}")]
    InvalidRole {
        /// Expected role.
        expected: SessionRole,
        /// Actual role.
        actual: SessionRole,
    },
    /// Peer ids do not match the expected initiator/responder/grant subject.
    #[error("peer confusion")]
    PeerConfusion,
    /// Nonce is all zero.
    #[error("zero transfer nonce")]
    ZeroNonce,
    /// Nonce already appeared in the replay cache.
    #[error("replayed transfer nonce")]
    ReplayedNonce,
    /// Protocol version is not supported.
    #[error("unsupported protocol version {0}")]
    UnsupportedVersion(u32),
    /// Context is denied by policy or grant scope.
    #[error("context denied: {0:?}")]
    ContextDenied(SessionContextKind),
    /// Manifest root is required but absent.
    #[error("manifest root required")]
    ManifestRootRequired,
    /// Required feature was not offered or selected.
    #[error("missing required feature: {0:?}")]
    MissingRequiredFeature(AtpFeature),
    /// Feature selected by peer was not offered by the client.
    #[error("feature confusion: {0:?}")]
    FeatureConfusion(AtpFeature),
    /// No grant authorized a required action.
    #[error("missing grant action: {0:?}")]
    MissingGrantAction(CapabilityAction),
    /// Grant issuer was not trusted by the acceptor.
    #[error("untrusted grant issuer: {0:?}")]
    UntrustedGrantIssuer(PeerId),
    /// Grant is not yet valid.
    #[error("grant is not yet valid")]
    GrantNotYetValid(CapabilityGrantId),
    /// Grant expired.
    #[error("grant expired")]
    GrantExpired(CapabilityGrantId),
    /// Grant was revoked.
    #[error("grant revoked")]
    GrantRevoked(CapabilityGrantId),
    /// Grant cannot delegate.
    #[error("delegation denied")]
    DelegationDenied(CapabilityGrantId),
    /// Grant cannot invite.
    #[error("invite denied")]
    InviteDenied(CapabilityGrantId),
    /// Path restrictions rejected the request.
    #[error("path scope denied")]
    PathScopeDenied {
        /// Rejected path id.
        path_id: Option<PathCandidateId>,
    },
    /// Relay context did not name a relay peer.
    #[error("relay identity required")]
    MissingRelayIdentity,
    /// Non-relay context carried a relay peer.
    #[error("unexpected relay identity")]
    UnexpectedRelayIdentity,
    /// Relay peer is not trusted by policy.
    #[error("untrusted relay identity: {0:?}")]
    UntrustedRelayIdentity(PeerId),
    /// Relay restrictions rejected the request.
    #[error("relay scope denied")]
    RelayScopeDenied {
        /// Rejected relay peer.
        relay_peer: Option<PeerId>,
    },
    /// Object restrictions rejected the request.
    #[error("object scope denied")]
    ObjectScopeDenied {
        /// Rejected manifest root.
        manifest_root: Option<[u8; 32]>,
    },
    /// Server reply did not derive the expected session id.
    #[error("session id mismatch")]
    SessionIdMismatch,
    /// Error annotated with a proof artifact.
    #[error("{source}")]
    WithProof {
        /// Original error.
        source: Box<SessionError>,
        /// Rejection proof.
        proof: SessionProofArtifact,
    },
}

impl SessionError {
    /// Stable machine-readable error code.
    #[must_use]
    pub const fn code(&self) -> &'static str {
        match self {
            Self::Frame(_) => "frame_error",
            Self::InvalidTransition { .. } => "invalid_transition",
            Self::InvalidRole { .. } => "invalid_role",
            Self::PeerConfusion => "peer_confusion",
            Self::ZeroNonce => "zero_nonce",
            Self::ReplayedNonce => "replayed_nonce",
            Self::UnsupportedVersion(_) => "unsupported_version",
            Self::ContextDenied(_) => "context_denied",
            Self::ManifestRootRequired => "manifest_root_required",
            Self::MissingRequiredFeature(_) => "missing_required_feature",
            Self::FeatureConfusion(_) => "feature_confusion",
            Self::MissingGrantAction(_) => "missing_grant_action",
            Self::UntrustedGrantIssuer(_) => "untrusted_grant_issuer",
            Self::GrantNotYetValid(_) => "grant_not_yet_valid",
            Self::GrantExpired(_) => "grant_expired",
            Self::GrantRevoked(_) => "grant_revoked",
            Self::DelegationDenied(_) => "delegation_denied",
            Self::InviteDenied(_) => "invite_denied",
            Self::PathScopeDenied { .. } => "path_scope_denied",
            Self::MissingRelayIdentity => "missing_relay_identity",
            Self::UnexpectedRelayIdentity => "unexpected_relay_identity",
            Self::UntrustedRelayIdentity(_) => "untrusted_relay_identity",
            Self::RelayScopeDenied { .. } => "relay_scope_denied",
            Self::ObjectScopeDenied { .. } => "object_scope_denied",
            Self::SessionIdMismatch => "session_id_mismatch",
            Self::WithProof { source, .. } => source.code(),
        }
    }

    fn with_proof(self, proof: SessionProofArtifact) -> Self {
        Self::WithProof {
            source: Box::new(self),
            proof,
        }
    }

    /// Borrow the attached proof if this error carries one.
    #[must_use]
    pub const fn proof(&self) -> Option<&SessionProofArtifact> {
        match self {
            Self::WithProof { proof, .. } => Some(proof),
            _ => None,
        }
    }
}

fn build_server_hello(
    hello: &ClientHello,
    policy: &mut SessionPolicy,
) -> Result<ServerHello, SessionError> {
    validate_client_hello(hello, policy)?;
    let (selected_features, downgrade_warnings) = select_features(hello, policy)?;
    let accepted_grants = authorize_actions(hello, policy)?;
    reserve_client_nonce(hello, policy)?;
    let session_id = derive_session_id(hello, &selected_features);

    Ok(ServerHello {
        session_id,
        acceptor: policy.local_peer,
        initiator: hello.initiator,
        nonce: hello.nonce,
        version: hello.version,
        context: hello.context,
        selected_features,
        downgrade_warnings,
        accepted_grants,
        trace_id: hello.trace_id,
    })
}

fn validate_client_hello(hello: &ClientHello, policy: &SessionPolicy) -> Result<(), SessionError> {
    validate_nonce(hello.nonce)?;
    if !policy.supported_versions.contains(&hello.version) {
        return Err(SessionError::UnsupportedVersion(hello.version.0));
    }
    if policy.seen_nonces.contains(&hello.nonce) {
        return Err(SessionError::ReplayedNonce);
    }
    if !policy.accepted_contexts.contains(&hello.context) {
        return Err(SessionError::ContextDenied(hello.context));
    }
    validate_relay_identity(hello, policy)?;
    if policy.require_manifest_binding && hello.manifest_root.is_none() {
        return Err(SessionError::ManifestRootRequired);
    }
    Ok(())
}

fn validate_relay_identity(
    hello: &ClientHello,
    policy: &SessionPolicy,
) -> Result<(), SessionError> {
    match (hello.context, hello.relay_peer) {
        (SessionContextKind::Relay, Some(relay_peer)) => {
            if relay_peer == hello.initiator || relay_peer == hello.responder {
                return Err(SessionError::PeerConfusion);
            }
            if policy.trusted_relays.contains(&relay_peer) {
                Ok(())
            } else {
                Err(SessionError::UntrustedRelayIdentity(relay_peer))
            }
        }
        (SessionContextKind::Relay, None) => Err(SessionError::MissingRelayIdentity),
        (_, Some(_)) => Err(SessionError::UnexpectedRelayIdentity),
        (_, None) => Ok(()),
    }
}

fn reserve_client_nonce(
    hello: &ClientHello,
    policy: &mut SessionPolicy,
) -> Result<(), SessionError> {
    if policy.seen_nonces.insert(hello.nonce) {
        Ok(())
    } else {
        Err(SessionError::ReplayedNonce)
    }
}

fn validate_nonce(nonce: TransferNonce) -> Result<(), SessionError> {
    if nonce.is_zero() {
        Err(SessionError::ZeroNonce)
    } else {
        Ok(())
    }
}

fn select_features(
    hello: &ClientHello,
    policy: &SessionPolicy,
) -> Result<(FeatureSet, Vec<DowngradeWarning>), SessionError> {
    let selected = hello
        .offered_features
        .intersection(&policy.supported_features);
    for feature in policy.required_features.iter() {
        if !selected.contains(feature) {
            return Err(SessionError::MissingRequiredFeature(feature));
        }
    }
    if let Some(required) = hello.context.required_feature() {
        if !selected.contains(required) {
            return Err(SessionError::MissingRequiredFeature(required));
        }
    }

    let downgrade_warnings = hello
        .offered_features
        .iter()
        .filter(|feature| !selected.contains(*feature))
        .map(|feature| DowngradeWarning {
            feature,
            reason_code: feature.downgrade_reason_code(),
        })
        .collect();

    Ok((selected, downgrade_warnings))
}

fn authorize_actions(
    hello: &ClientHello,
    policy: &SessionPolicy,
) -> Result<Vec<CapabilityGrantId>, SessionError> {
    let mut required = policy.required_actions.clone();
    required.extend(hello.requested_actions.iter().copied());

    let mut accepted = BTreeSet::new();
    for action in required {
        let grant = hello
            .grants
            .iter()
            .find(|grant| grant.validate_for(hello, action, policy).is_ok())
            .ok_or(SessionError::MissingGrantAction(action))?;
        grant.validate_for(hello, action, policy)?;
        accepted.insert(grant.id);
    }
    Ok(accepted.into_iter().collect())
}

fn validate_server_hello(
    hello: &ClientHello,
    server_hello: &ServerHello,
    policy: &SessionPolicy,
) -> Result<(), SessionError> {
    if server_hello.acceptor != hello.responder {
        return Err(SessionError::PeerConfusion);
    }
    if server_hello.initiator != hello.initiator {
        return Err(SessionError::PeerConfusion);
    }
    if server_hello.nonce != hello.nonce {
        return Err(SessionError::PeerConfusion);
    }
    if server_hello.context != hello.context {
        return Err(SessionError::PeerConfusion);
    }
    validate_relay_identity(hello, policy)?;
    if !policy.supported_versions.contains(&server_hello.version) {
        return Err(SessionError::UnsupportedVersion(server_hello.version.0));
    }
    for feature in server_hello.selected_features.iter() {
        if !hello.offered_features.contains(feature) {
            return Err(SessionError::FeatureConfusion(feature));
        }
    }
    for feature in policy.required_features.iter() {
        if !server_hello.selected_features.contains(feature) {
            return Err(SessionError::MissingRequiredFeature(feature));
        }
    }
    if derive_session_id(hello, &server_hello.selected_features) != server_hello.session_id {
        return Err(SessionError::SessionIdMismatch);
    }
    Ok(())
}

fn derive_session_id(hello: &ClientHello, selected_features: &FeatureSet) -> SessionId {
    let mut hasher = Sha256::new();
    hasher.update(b"ATP-SESSION-ID-V1\x00");
    hasher.update(hello.initiator.as_bytes());
    hasher.update(hello.responder.as_bytes());
    hasher.update(hello.nonce.as_bytes());
    hasher.update(hello.version.0.to_be_bytes());
    hasher.update([context_code(hello.context)]);
    if let Some(manifest_root) = hello.manifest_root {
        hasher.update([1]);
        hasher.update(manifest_root);
    } else {
        hasher.update([0]);
    }
    if let Some(path_id) = hello.path_id {
        hasher.update([1]);
        hasher.update(path_id.get().to_be_bytes());
    } else {
        hasher.update([0]);
    }
    if let Some(relay_peer) = hello.relay_peer {
        hasher.update([1]);
        hasher.update(relay_peer.as_bytes());
    } else {
        hasher.update([0]);
    }
    for feature in selected_features.iter() {
        hasher.update([feature_code(feature)]);
    }
    SessionId(hasher.finalize().into())
}

fn redact_transcript_hash(hash: TranscriptHash) -> String {
    hex::encode(&hash.as_bytes()[..12])
}

fn put_peer_id(bytes: &mut Vec<u8>, peer_id: PeerId) {
    bytes.extend_from_slice(peer_id.as_bytes());
}

fn put_optional_hash(bytes: &mut Vec<u8>, hash: Option<[u8; 32]>) {
    match hash {
        Some(hash) => {
            bytes.push(1);
            bytes.extend_from_slice(&hash);
        }
        None => bytes.push(0),
    }
}

fn put_optional_u64(bytes: &mut Vec<u8>, value: Option<u64>) {
    match value {
        Some(value) => {
            bytes.push(1);
            bytes.extend_from_slice(&value.to_be_bytes());
        }
        None => bytes.push(0),
    }
}

fn put_features(bytes: &mut Vec<u8>, features: &FeatureSet) {
    let features = features.iter().collect::<Vec<_>>();
    put_u32(bytes, features.len());
    for feature in features {
        bytes.push(feature_code(feature));
    }
}

fn put_actions(bytes: &mut Vec<u8>, actions: &BTreeSet<CapabilityAction>) {
    put_u32(bytes, actions.len());
    for action in actions {
        bytes.push(action_code(*action));
    }
}

fn put_grants(bytes: &mut Vec<u8>, grants: &[CapabilityGrant]) {
    put_u32(bytes, grants.len());
    for grant in grants {
        bytes.extend_from_slice(grant.id.as_bytes());
        put_peer_id(bytes, grant.issuer);
        put_peer_id(bytes, grant.subject);
        put_actions(bytes, &grant.actions);
        bytes.extend_from_slice(&grant.valid_from_micros.to_be_bytes());
        put_optional_u64(bytes, grant.expires_at_micros);
        bytes.push(u8::from(grant.revoked));
        bytes.push(grant.delegation_depth);
        bytes.push(u8::from(grant.invite_scope));
        put_scope(bytes, &grant.scope);
    }
}

fn put_scope(bytes: &mut Vec<u8>, scope: &CapabilityScope) {
    bytes.push(u8::from(scope.allow_any_path));
    put_u32(bytes, scope.allowed_path_ids.len());
    for path_id in &scope.allowed_path_ids {
        bytes.extend_from_slice(&path_id.get().to_be_bytes());
    }
    put_u32(bytes, scope.allowed_path_prefixes.len());
    for prefix in &scope.allowed_path_prefixes {
        put_str(bytes, prefix);
    }
    bytes.push(u8::from(scope.allow_any_relay_peer));
    put_u32(bytes, scope.allowed_relay_peers.len());
    for relay_peer in &scope.allowed_relay_peers {
        put_peer_id(bytes, *relay_peer);
    }
    bytes.push(u8::from(scope.allow_any_manifest_root));
    put_u32(bytes, scope.allowed_manifest_roots.len());
    for root in &scope.allowed_manifest_roots {
        bytes.extend_from_slice(root);
    }
    put_u32(bytes, scope.allowed_contexts.len());
    for context in &scope.allowed_contexts {
        bytes.push(context_code(*context));
    }
}

fn put_str(bytes: &mut Vec<u8>, value: &str) {
    put_u32(bytes, value.len());
    bytes.extend_from_slice(value.as_bytes());
}

fn put_optional_peer_id(bytes: &mut Vec<u8>, value: Option<PeerId>) {
    match value {
        Some(peer_id) => {
            bytes.push(1);
            put_peer_id(bytes, peer_id);
        }
        None => bytes.push(0),
    }
}

fn put_u32(bytes: &mut Vec<u8>, value: usize) {
    bytes.extend_from_slice(&(value as u32).to_be_bytes());
}

fn context_code(context: SessionContextKind) -> u8 {
    match context {
        SessionContextKind::Direct => 0,
        SessionContextKind::Relay => 1,
        SessionContextKind::Mailbox => 2,
        SessionContextKind::Swarm => 3,
    }
}

fn feature_code(feature: AtpFeature) -> u8 {
    match feature {
        AtpFeature::Repair => 0,
        AtpFeature::Datagrams => 1,
        AtpFeature::Compression => 2,
        AtpFeature::EncryptionPolicy => 3,
        AtpFeature::Swarm => 4,
        AtpFeature::Mailbox => 5,
        AtpFeature::Relay => 6,
        AtpFeature::H3Adapter => 7,
        AtpFeature::WebTransportAdapter => 8,
        AtpFeature::MasqueAdapter => 9,
        AtpFeature::ProofBundles => 10,
        AtpFeature::Resume => 11,
    }
}

fn action_code(action: CapabilityAction) -> u8 {
    match action {
        CapabilityAction::Read => 0,
        CapabilityAction::Write => 1,
        CapabilityAction::Receive => 2,
        CapabilityAction::Share => 3,
        CapabilityAction::Relay => 4,
        CapabilityAction::Seed => 5,
        CapabilityAction::Mailbox => 6,
        CapabilityAction::Delegate => 7,
        CapabilityAction::Invite => 8,
    }
}

impl fmt::Display for PeerId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "peer:{}", self.redacted())
    }
}

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

    fn peers() -> (PeerId, PeerId) {
        (PeerId::from_label("alice"), PeerId::from_label("bob"))
    }

    fn relay_peer() -> PeerId {
        PeerId::from_label("relay-a")
    }

    fn alternate_relay_peer() -> PeerId {
        PeerId::from_label("relay-b")
    }

    #[test]
    fn peer_id_from_public_key_is_canonical_and_rejects_bad_material() {
        let public_key = b"ed25519:alice-device-public-key";
        let first = PeerId::from_public_key(public_key).unwrap();
        let second = PeerId::from_public_key(public_key).unwrap();

        assert_eq!(first, second);
        assert_ne!(first, PeerId::from_label("ed25519:alice-device-public-key"));
        assert_eq!(
            PeerId::from_public_key(&[]),
            Err(PeerIdentityError::EmptyPublicKey)
        );
        assert_eq!(
            PeerId::from_public_key(&[0; 32]),
            Err(PeerIdentityError::AllZeroPublicKey)
        );
    }

    fn manifest_root(byte: u8) -> [u8; 32] {
        [byte; 32]
    }

    fn grant_for(
        issuer: PeerId,
        subject: PeerId,
        actions: &[CapabilityAction],
        context: SessionContextKind,
    ) -> CapabilityGrant {
        CapabilityGrant::new(
            CapabilityGrantId::from_label("grant"),
            issuer,
            subject,
            actions.iter().copied(),
            if matches!(context, SessionContextKind::Relay) {
                CapabilityScope::for_context(context).with_relay_peer(relay_peer())
            } else {
                CapabilityScope::for_context(context)
            },
        )
    }

    fn hello_for(context: SessionContextKind) -> ClientHello {
        let (alice, bob) = peers();
        let hello = ClientHello::new(
            alice,
            bob,
            TransferNonce::from_seed(context.code()),
            context,
            SessionTraceId::new(42),
        )
        .with_features(&[
            AtpFeature::EncryptionPolicy,
            AtpFeature::ProofBundles,
            AtpFeature::Resume,
            AtpFeature::Relay,
            AtpFeature::Mailbox,
            AtpFeature::Swarm,
            AtpFeature::Repair,
            AtpFeature::Datagrams,
        ])
        .with_requested_actions(&[CapabilityAction::Write])
        .with_grants(vec![grant_for(
            bob,
            alice,
            &[CapabilityAction::Write],
            context,
        )]);
        if matches!(context, SessionContextKind::Relay) {
            hello.with_relay_peer(relay_peer())
        } else {
            hello
        }
    }

    fn policy_for(context: SessionContextKind) -> SessionPolicy {
        let (_alice, bob) = peers();
        let policy = SessionPolicy::new(bob, 100)
            .with_supported_features(&[
                AtpFeature::EncryptionPolicy,
                AtpFeature::ProofBundles,
                AtpFeature::Resume,
                AtpFeature::Relay,
                AtpFeature::Mailbox,
                AtpFeature::Swarm,
                AtpFeature::Repair,
                AtpFeature::Datagrams,
            ])
            .with_required_features(&[AtpFeature::EncryptionPolicy])
            .with_required_actions(&[CapabilityAction::Write])
            .with_accepted_contexts(&[context]);
        if matches!(context, SessionContextKind::Relay) {
            policy.with_trusted_relays(&[relay_peer()])
        } else {
            policy
        }
    }

    fn negotiate(
        hello: &ClientHello,
        policy: &mut SessionPolicy,
    ) -> Result<
        (
            NegotiatedSession,
            SessionProofArtifact,
            SessionProofArtifact,
        ),
        SessionError,
    > {
        let mut client = SessionNegotiator::client(hello.initiator);
        let mut server = SessionNegotiator::server(policy.local_peer);
        let client_frame = client.start_client_hello(hello)?;
        assert_eq!(client_frame.frame_type(), FrameType::Handshake);

        let (server_hello, server_frame, server_proof) =
            server.accept_client_hello(hello, policy)?;
        assert_eq!(server_frame.frame_type(), FrameType::HandshakeAck);

        let (session, client_proof) = client.finish_client(hello, &server_hello, policy)?;
        Ok((session, client_proof, server_proof))
    }

    #[test]
    fn direct_first_contact_pairing_establishes_session() {
        let hello = hello_for(SessionContextKind::Direct);
        let mut policy = policy_for(SessionContextKind::Direct);

        let (session, client_proof, server_proof) = negotiate(&hello, &mut policy).unwrap();

        assert_eq!(session.context, SessionContextKind::Direct);
        assert!(
            session
                .selected_features
                .contains(AtpFeature::EncryptionPolicy)
        );
        assert_eq!(session.accepted_grants.len(), 1);
        assert_eq!(client_proof.session_id, server_proof.session_id);
        assert_eq!(client_proof.rejected_reason, None);
        assert!(!client_proof.transcript_hash.is_empty());
    }

    #[test]
    fn relay_mailbox_and_swarm_contexts_require_matching_features() {
        for (context, feature, action) in [
            (
                SessionContextKind::Relay,
                AtpFeature::Relay,
                CapabilityAction::Relay,
            ),
            (
                SessionContextKind::Mailbox,
                AtpFeature::Mailbox,
                CapabilityAction::Mailbox,
            ),
            (
                SessionContextKind::Swarm,
                AtpFeature::Swarm,
                CapabilityAction::Seed,
            ),
        ] {
            let (alice, bob) = peers();
            let hello = ClientHello::new(
                alice,
                bob,
                TransferNonce::from_seed(context.code()),
                context,
                SessionTraceId::new(77),
            )
            .with_features(&[AtpFeature::EncryptionPolicy, feature])
            .with_requested_actions(&[action])
            .with_grants(vec![grant_for(bob, alice, &[action], context)]);
            let hello = if matches!(context, SessionContextKind::Relay) {
                hello.with_relay_peer(relay_peer())
            } else {
                hello
            };
            let policy = SessionPolicy::new(bob, 100)
                .with_supported_features(&[AtpFeature::EncryptionPolicy, feature])
                .with_required_features(&[AtpFeature::EncryptionPolicy])
                .with_required_actions(&[action])
                .with_accepted_contexts(&[context]);
            let mut policy = if matches!(context, SessionContextKind::Relay) {
                policy.with_trusted_relays(&[relay_peer()])
            } else {
                policy
            };

            let (session, _client_proof, _server_proof) = negotiate(&hello, &mut policy).unwrap();
            assert_eq!(session.context, context);
            assert!(session.selected_features.contains(feature));
        }
    }

    #[test]
    fn relay_context_requires_trusted_relay_identity() {
        let (alice, bob) = peers();
        let relay_grant = grant_for(
            bob,
            alice,
            &[CapabilityAction::Relay],
            SessionContextKind::Relay,
        );
        let base = ClientHello::new(
            alice,
            bob,
            TransferNonce::from_seed("relay-auth"),
            SessionContextKind::Relay,
            SessionTraceId::new(88),
        )
        .with_features(&[AtpFeature::EncryptionPolicy, AtpFeature::Relay])
        .with_requested_actions(&[CapabilityAction::Relay])
        .with_grants(vec![relay_grant]);

        let mut missing_policy = SessionPolicy::new(bob, 100)
            .with_supported_features(&[AtpFeature::EncryptionPolicy, AtpFeature::Relay])
            .with_required_features(&[AtpFeature::EncryptionPolicy])
            .with_required_actions(&[CapabilityAction::Relay])
            .with_accepted_contexts(&[SessionContextKind::Relay])
            .with_trusted_relays(&[relay_peer()]);
        let mut server = SessionNegotiator::server(bob);
        let error = server
            .accept_client_hello(&base, &mut missing_policy)
            .unwrap_err();
        assert_eq!(error.code(), "missing_relay_identity");

        let mut untrusted_policy = SessionPolicy::new(bob, 100)
            .with_supported_features(&[AtpFeature::EncryptionPolicy, AtpFeature::Relay])
            .with_required_features(&[AtpFeature::EncryptionPolicy])
            .with_required_actions(&[CapabilityAction::Relay])
            .with_accepted_contexts(&[SessionContextKind::Relay])
            .with_trusted_relays(&[relay_peer()]);
        let mut server = SessionNegotiator::server(bob);
        let error = server
            .accept_client_hello(
                &base.clone().with_relay_peer(alternate_relay_peer()),
                &mut untrusted_policy,
            )
            .unwrap_err();
        assert_eq!(error.code(), "untrusted_relay_identity");
    }

    #[test]
    fn relay_grant_scope_and_session_id_bind_relay_identity() {
        let (alice, bob) = peers();
        let grant = grant_for(
            bob,
            alice,
            &[CapabilityAction::Relay],
            SessionContextKind::Relay,
        );
        let hello = ClientHello::new(
            alice,
            bob,
            TransferNonce::from_seed("relay-scope"),
            SessionContextKind::Relay,
            SessionTraceId::new(89),
        )
        .with_features(&[AtpFeature::EncryptionPolicy, AtpFeature::Relay])
        .with_requested_actions(&[CapabilityAction::Relay])
        .with_relay_peer(alternate_relay_peer())
        .with_grants(vec![grant.clone()]);
        let policy = SessionPolicy::new(bob, 100)
            .with_supported_features(&[AtpFeature::EncryptionPolicy, AtpFeature::Relay])
            .with_required_features(&[AtpFeature::EncryptionPolicy])
            .with_required_actions(&[CapabilityAction::Relay])
            .with_accepted_contexts(&[SessionContextKind::Relay])
            .with_trusted_relays(&[relay_peer(), alternate_relay_peer()]);

        match grant
            .validate_for(&hello, CapabilityAction::Relay, &policy)
            .expect_err("relay grant scope")
        {
            SessionError::RelayScopeDenied {
                relay_peer: Some(rejected),
            } => assert_eq!(rejected, alternate_relay_peer()),
            other => panic!("unexpected error: {other:?}"),
        }

        let selected = FeatureSet::from_slice(&[AtpFeature::EncryptionPolicy, AtpFeature::Relay]);
        let relay_a_session =
            derive_session_id(&hello.clone().with_relay_peer(relay_peer()), &selected);
        let relay_b_session = derive_session_id(&hello, &selected);
        assert_ne!(relay_a_session, relay_b_session);
    }

    #[test]
    fn unsupported_optional_features_emit_downgrade_warnings() {
        let hello = hello_for(SessionContextKind::Direct).with_features(&[
            AtpFeature::EncryptionPolicy,
            AtpFeature::Repair,
            AtpFeature::Compression,
            AtpFeature::H3Adapter,
            AtpFeature::WebTransportAdapter,
        ]);
        let mut policy = policy_for(SessionContextKind::Direct)
            .with_supported_features(&[AtpFeature::EncryptionPolicy, AtpFeature::Repair]);
        let mut server = SessionNegotiator::server(policy.local_peer);

        let (server_hello, _frame, proof) =
            server.accept_client_hello(&hello, &mut policy).unwrap();

        assert!(server_hello.selected_features.contains(AtpFeature::Repair));
        assert!(
            !server_hello
                .selected_features
                .contains(AtpFeature::Compression)
        );
        let warned = server_hello
            .downgrade_warnings
            .iter()
            .map(|warning| warning.feature)
            .collect::<BTreeSet<_>>();
        assert!(warned.contains(&AtpFeature::Compression));
        assert!(warned.contains(&AtpFeature::H3Adapter));
        assert!(warned.contains(&AtpFeature::WebTransportAdapter));
        assert_eq!(proof.selected_features, vec!["repair", "encryption_policy"]);
    }

    #[test]
    fn missing_required_feature_fails_closed() {
        let hello = hello_for(SessionContextKind::Direct).with_features(&[AtpFeature::Repair]);
        let mut policy = policy_for(SessionContextKind::Direct);
        let mut server = SessionNegotiator::server(policy.local_peer);

        let error = server.accept_client_hello(&hello, &mut policy).unwrap_err();

        assert_eq!(error.code(), "missing_required_feature");
        assert_eq!(
            error
                .proof()
                .and_then(|proof| proof.rejected_reason.as_deref()),
            Some("missing_required_feature")
        );
    }

    #[test]
    fn expired_and_revoked_grants_are_rejected() {
        let (alice, bob) = peers();
        let base = ClientHello::new(
            alice,
            bob,
            TransferNonce::from_seed("expired"),
            SessionContextKind::Direct,
            SessionTraceId::new(1),
        )
        .with_features(&[AtpFeature::EncryptionPolicy])
        .with_requested_actions(&[CapabilityAction::Write]);
        let mut policy = policy_for(SessionContextKind::Direct);

        let expired = grant_for(
            bob,
            alice,
            &[CapabilityAction::Write],
            SessionContextKind::Direct,
        )
        .with_validity(0, 50);
        let mut server = SessionNegotiator::server(policy.local_peer);
        let error = server
            .accept_client_hello(&base.clone().with_grants(vec![expired]), &mut policy)
            .unwrap_err();
        assert_eq!(error.code(), "missing_grant_action");

        let revoked = grant_for(
            bob,
            alice,
            &[CapabilityAction::Write],
            SessionContextKind::Direct,
        )
        .revoked();
        let mut server = SessionNegotiator::server(policy.local_peer);
        let error = server
            .accept_client_hello(&base.with_grants(vec![revoked]), &mut policy)
            .unwrap_err();
        assert_eq!(error.code(), "missing_grant_action");
    }

    #[test]
    fn replayed_nonce_is_rejected_before_authorization() {
        let hello = hello_for(SessionContextKind::Direct);
        let mut policy = policy_for(SessionContextKind::Direct).with_seen_nonce(hello.nonce);
        let mut server = SessionNegotiator::server(policy.local_peer);

        let error = server.accept_client_hello(&hello, &mut policy).unwrap_err();

        assert_eq!(error.code(), "replayed_nonce");
    }

    #[test]
    fn successful_accept_records_nonce_for_future_replay_rejection() {
        let hello = hello_for(SessionContextKind::Direct);
        let mut policy = policy_for(SessionContextKind::Direct);
        let mut server = SessionNegotiator::server(policy.local_peer);

        server.accept_client_hello(&hello, &mut policy).unwrap();

        assert!(policy.seen_nonces.contains(&hello.nonce));

        let mut replay_server = SessionNegotiator::server(policy.local_peer);
        let error = replay_server
            .accept_client_hello(&hello, &mut policy)
            .unwrap_err();

        assert_eq!(error.code(), "replayed_nonce");
    }

    #[test]
    fn path_and_object_scope_escalation_is_rejected() {
        let (alice, bob) = peers();
        let allowed_path = PathCandidateId::new(7);
        let denied_path = PathCandidateId::new(8);
        let allowed_root = manifest_root(1);
        let denied_root = manifest_root(2);
        let scope = CapabilityScope::for_context(SessionContextKind::Direct)
            .with_path_id(allowed_path)
            .with_manifest_root(allowed_root);
        let grant = CapabilityGrant::new(
            CapabilityGrantId::from_label("scoped"),
            bob,
            alice,
            [CapabilityAction::Write],
            scope,
        );
        let mut policy = policy_for(SessionContextKind::Direct).require_manifest_binding();

        let path_escalation = ClientHello::new(
            alice,
            bob,
            TransferNonce::from_seed("path-escalation"),
            SessionContextKind::Direct,
            SessionTraceId::new(2),
        )
        .with_features(&[AtpFeature::EncryptionPolicy])
        .with_requested_actions(&[CapabilityAction::Write])
        .with_manifest_root(allowed_root)
        .with_path_id(denied_path)
        .with_grants(vec![grant.clone()]);
        let mut server = SessionNegotiator::server(policy.local_peer);
        let error = server
            .accept_client_hello(&path_escalation, &mut policy)
            .unwrap_err();
        assert_eq!(error.code(), "missing_grant_action");

        let object_escalation = ClientHello::new(
            alice,
            bob,
            TransferNonce::from_seed("object-escalation"),
            SessionContextKind::Direct,
            SessionTraceId::new(3),
        )
        .with_features(&[AtpFeature::EncryptionPolicy])
        .with_requested_actions(&[CapabilityAction::Write])
        .with_manifest_root(denied_root)
        .with_path_id(allowed_path)
        .with_grants(vec![grant]);
        let mut server = SessionNegotiator::server(policy.local_peer);
        let error = server
            .accept_client_hello(&object_escalation, &mut policy)
            .unwrap_err();
        assert_eq!(error.code(), "missing_grant_action");
    }

    #[test]
    fn invalid_transitions_fail_closed() {
        let hello = hello_for(SessionContextKind::Direct);
        let mut client = SessionNegotiator::client(hello.initiator);

        client.start_client_hello(&hello).unwrap();
        let error = client.start_client_hello(&hello).unwrap_err();

        assert_eq!(error.code(), "invalid_transition");
    }

    #[test]
    fn server_feature_confusion_is_rejected_by_client() {
        let hello =
            hello_for(SessionContextKind::Direct).with_features(&[AtpFeature::EncryptionPolicy]);
        let policy = policy_for(SessionContextKind::Direct);
        let mut client = SessionNegotiator::client(hello.initiator);
        client.start_client_hello(&hello).unwrap();

        let server_hello = ServerHello {
            session_id: derive_session_id(
                &hello,
                &FeatureSet::from_slice(&[AtpFeature::EncryptionPolicy, AtpFeature::Compression]),
            ),
            acceptor: hello.responder,
            initiator: hello.initiator,
            nonce: hello.nonce,
            version: ProtocolVersion::CURRENT,
            context: SessionContextKind::Direct,
            selected_features: FeatureSet::from_slice(&[
                AtpFeature::EncryptionPolicy,
                AtpFeature::Compression,
            ]),
            downgrade_warnings: Vec::new(),
            accepted_grants: vec![CapabilityGrantId::from_label("grant")],
            trace_id: hello.trace_id,
        };

        let error = client
            .finish_client(&hello, &server_hello, &policy)
            .unwrap_err();

        assert_eq!(error.code(), "feature_confusion");
    }
}