meerkat-comms 0.8.3

Inter-agent communication for Meerkat
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
//! Multi-identity host acceptor for the mob member host daemon (D1).
//!
//! One TCP listener serves signed envelopes for EVERY identity registered on
//! this host (the host's own identity in phase 2; materialized member
//! identities from phase 3), demultiplexing per envelope by `envelope.to`.
//! Acks are signed by the ADDRESSED identity's keypair — the sender's router
//! accepts an ack only when `ack.from` equals the envelope's original `to`.
//!
//! Peer auth on this path is enforced by construction: neither
//! [`HostAcceptorConfig`] nor the demux entry point carries a flag that
//! could disable signature verification (D1 — mandatory, not configurable).
//!
//! The optional pairing branch is host-scoped ceremony transport (§7.2 step
//! 2): it proves possession of the host pairing secret and returns the
//! daemon-supplied host binding descriptor verbatim. Unlike member pairing
//! it installs NO trust and persists NOTHING (DEC-P2-3) — supervisor trust
//! arrives machine-gated at `HostBindAccepted`.

use std::any::Any;
use std::collections::HashMap;
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
use std::sync::Arc;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};

use parking_lot::{Mutex, RwLock};
use tokio::io::BufReader;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{Semaphore, oneshot, watch};
use tokio::task::JoinSet;
use uuid::Uuid;

use crate::classify::declared_tcp_port;
use crate::identity::{Keypair, PubKey};
use crate::inbox::InboxSender;
use crate::io_task::handle_tcp_connection_demux;
use crate::runtime::comms_runtime::{
    PAIRING_CHALLENGE_KIND, PAIRING_COMPLETE_KIND, PAIRING_VERSION, PairingClientMessage,
    PairingIdentityKind, comms_pairing_password_proof, constant_time_str_eq, parse_peer_address,
    read_pairing_message, validate_pairing_secret, write_pairing_json,
};

fn validate_advertised_tcp_endpoint(
    endpoint: &meerkat_core::comms::PeerAddress,
) -> Result<(), &'static str> {
    if declared_tcp_port(endpoint).is_none() {
        return Err("expected tcp://host:nonzero-port");
    }
    let authority = endpoint.endpoint();
    let host = if let Some(bracketed) = authority.strip_prefix('[') {
        let closing_bracket = bracketed
            .find(']')
            .ok_or("expected tcp://host:nonzero-port")?;
        let host = &bracketed[..closing_bracket];
        host.parse::<Ipv6Addr>()
            .map_err(|_| "bracketed advertised TCP hosts must be IPv6 addresses")?;
        host
    } else {
        authority
            .rsplit_once(':')
            .map(|(host, _)| host)
            .ok_or("expected tcp://host:nonzero-port")?
    };
    let wildcard_ip = host.parse::<IpAddr>().is_ok_and(|address| match address {
        IpAddr::V4(address) => address.is_unspecified(),
        IpAddr::V6(address) => {
            address.is_unspecified()
                || address
                    .to_ipv4_mapped()
                    .is_some_and(|mapped| mapped.is_unspecified())
        }
    });
    if host == "*" || wildcard_ip {
        return Err("advertised TCP host must be dialable, not a wildcard address");
    }
    Ok(())
}

/// Registration material one comms runtime hands to a host-acceptor
/// composer: the runtime's identity pubkey, the keypair the acceptor signs
/// transport acks with AS that identity, and the inbox sender inbound
/// envelopes are delivered through.
///
/// Produced by the concrete runtime behind the opaque
/// `CoreCommsRuntime::host_acceptor_registration_payload()` seam; consumed
/// by composers that hold this crate concretely (mob host daemon, the
/// controlling-side reverse-lane acceptor).
pub struct HostAcceptorRegistrationMaterial {
    pub pubkey: PubKey,
    pub keypair: Arc<Keypair>,
    pub inbox_sender: InboxSender,
}

/// Host-local projection of the daemon's registered inbound identities.
///
/// Installed/removed ONLY under the installed owner (machine-effect-driven
/// calls from the mob host actor); never self-populated, never seeded from
/// config or disk — the trust-store no-seed rule applies to this registry
/// too.
///
/// Comms cannot name the generated mob-side machine types (dependency
/// direction), so the comms-side gate is owner-token identity (Arc-ptr
/// equality, the `install_generated_mob_trust_owner` mechanics) and the
/// typed `from_transition` witness wraps the calls mob-side — the same
/// two-layer split the trust seam uses.
pub struct HostAcceptorIdentityRegistry {
    state: RwLock<HostAcceptorIdentityRegistryState>,
}

struct HostAcceptorIdentityRegistryState {
    identities: HashMap<PubKey, RegisteredAcceptorIdentity>,
    owner: Option<Arc<dyn Any + Send + Sync>>,
    reservation: Option<ReservedAcceptorIdentity>,
    replacement_reservation: Option<ReservedAcceptorIdentityReplacement>,
}

struct ReservedAcceptorIdentity {
    token: Arc<()>,
    owner: Arc<dyn Any + Send + Sync>,
    pubkey: PubKey,
    keypair: Arc<Keypair>,
}

struct ReservedAcceptorIdentityReplacement {
    token: Arc<()>,
    owner: Arc<dyn Any + Send + Sync>,
    current_pubkey: PubKey,
    current_sender: InboxSender,
    replacement_pubkey: PubKey,
    replacement_keypair: Arc<Keypair>,
}

/// Reversible reservation of the once-only registry owner and one identity.
///
/// The reservation prevents competing owners/identities without making the
/// identity resolvable by the acceptor. A composing daemon can therefore
/// publish its descriptor between [`HostAcceptorIdentityRegistry::reserve_identity`]
/// and [`Self::commit`]. Dropping the reservation rolls the exact private
/// owner/identity claim back.
#[must_use = "a host acceptor identity reservation must be committed or rolled back"]
pub struct HostAcceptorIdentityReservation {
    registry: Arc<HostAcceptorIdentityRegistry>,
    owner: Arc<dyn Any + Send + Sync>,
    token: Arc<()>,
    installed_owner: bool,
    committed: bool,
}

/// Prepared exact replacement of one registered acceptor identity.
///
/// Reserving validates the installed owner, the exact current inbox
/// generation, and the replacement key without changing the live route.
/// Dropping the reservation is therefore inert. [`Self::commit`] performs the
/// old-remove/new-install mutation under one registry write lock and has no
/// fallible path after an enclosing transaction has published related state.
#[must_use = "a host acceptor identity replacement must be committed or dropped"]
pub struct HostAcceptorIdentityReplacementReservation {
    registry: Arc<HostAcceptorIdentityRegistry>,
    owner: Arc<dyn Any + Send + Sync>,
    token: Arc<()>,
    committed: bool,
}

impl HostAcceptorIdentityReservation {
    /// Atomically make the reserved identity resolvable. Every fallible
    /// invariant was checked while reserving, and the reservation excludes a
    /// competing insert, so this commit has no error path after descriptor
    /// publication.
    pub fn commit(mut self, inbox_sender: InboxSender) {
        let mut state = self.registry.state.write();
        let reservation_is_exact = state.reservation.as_ref().is_some_and(|reservation| {
            Arc::ptr_eq(&reservation.token, &self.token)
                && Arc::ptr_eq(&reservation.owner, &self.owner)
        });
        if !reservation_is_exact {
            // This can only mean in-process authority corruption: the write
            // lock excludes races and the reservation API exposes no token
            // mutation. Do not continue with a different identity.
            std::process::abort();
        }
        let Some(reservation) = state.reservation.take() else {
            std::process::abort();
        };
        if state.identities.contains_key(&reservation.pubkey) {
            // Reservation excludes every other registry mutation. Reaching
            // this branch is likewise an impossible authority violation.
            std::process::abort();
        }
        state.identities.insert(
            reservation.pubkey,
            RegisteredAcceptorIdentity {
                keypair: reservation.keypair,
                inbox_sender,
            },
        );
        self.committed = true;
    }
}

impl Drop for HostAcceptorIdentityReservation {
    fn drop(&mut self) {
        if self.committed {
            return;
        }
        let mut state = self.registry.state.write();
        let owns_reservation = state
            .reservation
            .as_ref()
            .is_some_and(|reservation| Arc::ptr_eq(&reservation.token, &self.token));
        if owns_reservation {
            state.reservation = None;
        }
        if self.installed_owner
            && owns_reservation
            && state.identities.is_empty()
            && state
                .owner
                .as_ref()
                .is_some_and(|owner| Arc::ptr_eq(owner, &self.owner))
        {
            state.owner = None;
        }
    }
}

impl HostAcceptorIdentityReplacementReservation {
    /// Atomically fence the exact current identity and install its replacement.
    pub fn commit(mut self, replacement_sender: InboxSender) {
        let mut state = self.registry.state.write();
        let reservation_is_exact =
            state
                .replacement_reservation
                .as_ref()
                .is_some_and(|reservation| {
                    Arc::ptr_eq(&reservation.token, &self.token)
                        && Arc::ptr_eq(&reservation.owner, &self.owner)
                });
        if !reservation_is_exact {
            std::process::abort();
        }
        let Some(reservation) = state.replacement_reservation.take() else {
            std::process::abort();
        };
        let current_is_exact = state
            .identities
            .get(&reservation.current_pubkey)
            .is_some_and(|identity| {
                identity
                    .inbox_sender
                    .same_inbox(&reservation.current_sender)
            });
        if !current_is_exact
            || (reservation.replacement_pubkey != reservation.current_pubkey
                && state
                    .identities
                    .contains_key(&reservation.replacement_pubkey))
        {
            // A reservation excludes every registry mutation. Reaching this
            // branch means the in-process ownership invariant was corrupted.
            std::process::abort();
        }
        state.identities.remove(&reservation.current_pubkey);
        state.identities.insert(
            reservation.replacement_pubkey,
            RegisteredAcceptorIdentity {
                keypair: reservation.replacement_keypair,
                inbox_sender: replacement_sender,
            },
        );
        self.committed = true;
    }
}

impl Drop for HostAcceptorIdentityReplacementReservation {
    fn drop(&mut self) {
        if self.committed {
            return;
        }
        let mut state = self.registry.state.write();
        if state
            .replacement_reservation
            .as_ref()
            .is_some_and(|reservation| Arc::ptr_eq(&reservation.token, &self.token))
        {
            state.replacement_reservation = None;
        }
    }
}

struct RegisteredAcceptorIdentity {
    keypair: Arc<Keypair>,
    inbox_sender: InboxSender,
}

impl HostAcceptorIdentityRegistry {
    pub fn new() -> Self {
        Self {
            state: RwLock::new(HostAcceptorIdentityRegistryState {
                identities: HashMap::new(),
                owner: None,
                reservation: None,
                replacement_reservation: None,
            }),
        }
    }

    fn reservation_in_progress(state: &HostAcceptorIdentityRegistryState) -> bool {
        state.reservation.is_some() || state.replacement_reservation.is_some()
    }

    /// Reserve the once-only owner plus one inbound identity without exposing
    /// the identity to the acceptor yet.
    ///
    /// This is the startup transaction seam for descriptor-publishing
    /// daemons: all registry conflicts are reported before external
    /// publication, descriptor failure rolls back by dropping the returned
    /// reservation, and `commit` is infallible.
    pub fn reserve_identity(
        self: &Arc<Self>,
        owner: Arc<dyn Any + Send + Sync>,
        pubkey: PubKey,
        keypair: Arc<Keypair>,
    ) -> Result<HostAcceptorIdentityReservation, HostAcceptorError> {
        if keypair.public_key() != pubkey {
            return Err(HostAcceptorError::IdentityKeypairMismatch {
                pubkey: pubkey.to_pubkey_string(),
            });
        }
        let mut state = self.state.write();
        if Self::reservation_in_progress(&state) {
            return Err(HostAcceptorError::RegistryReservationInProgress);
        }
        let installed_owner = match state.owner.as_ref() {
            Some(existing) if Arc::ptr_eq(existing, &owner) => false,
            Some(_) => return Err(HostAcceptorError::OwnerAlreadyInstalled),
            None => {
                state.owner = Some(Arc::clone(&owner));
                true
            }
        };
        if state.identities.contains_key(&pubkey) {
            return Err(HostAcceptorError::IdentityAlreadyRegistered {
                pubkey: pubkey.to_pubkey_string(),
            });
        }
        let token = Arc::new(());
        state.reservation = Some(ReservedAcceptorIdentity {
            token: Arc::clone(&token),
            owner: Arc::clone(&owner),
            pubkey,
            keypair,
        });
        drop(state);
        Ok(HostAcceptorIdentityReservation {
            registry: Arc::clone(self),
            owner,
            token,
            installed_owner,
            committed: false,
        })
    }

    /// Reserve an exact live-identity replacement without changing routing.
    ///
    /// The current sender is the generation witness: participant names and
    /// signing keys may be reused, while an inbox is minted once per runtime.
    /// All conflicts are checked before the reservation is returned. While it
    /// is live, ordinary registry mutations are refused; `resolve` continues to
    /// expose only the current identity until commit.
    pub fn reserve_identity_replacement(
        self: &Arc<Self>,
        owner: Arc<dyn Any + Send + Sync>,
        current_pubkey: PubKey,
        current_sender: &InboxSender,
        replacement_pubkey: PubKey,
        replacement_keypair: Arc<Keypair>,
    ) -> Result<HostAcceptorIdentityReplacementReservation, HostAcceptorError> {
        if replacement_keypair.public_key() != replacement_pubkey {
            return Err(HostAcceptorError::IdentityKeypairMismatch {
                pubkey: replacement_pubkey.to_pubkey_string(),
            });
        }
        let mut state = self.state.write();
        if Self::reservation_in_progress(&state) {
            return Err(HostAcceptorError::RegistryReservationInProgress);
        }
        Self::validate_owner_state(&state, &owner)?;
        if !state
            .identities
            .get(&current_pubkey)
            .is_some_and(|identity| identity.inbox_sender.same_inbox(current_sender))
        {
            return Err(HostAcceptorError::ExpectedIdentityGenerationNotCurrent {
                pubkey: current_pubkey.to_pubkey_string(),
            });
        }
        if replacement_pubkey != current_pubkey
            && state.identities.contains_key(&replacement_pubkey)
        {
            return Err(HostAcceptorError::IdentityAlreadyRegistered {
                pubkey: replacement_pubkey.to_pubkey_string(),
            });
        }
        let token = Arc::new(());
        state.replacement_reservation = Some(ReservedAcceptorIdentityReplacement {
            token: Arc::clone(&token),
            owner: Arc::clone(&owner),
            current_pubkey,
            current_sender: current_sender.clone(),
            replacement_pubkey,
            replacement_keypair,
        });
        drop(state);
        Ok(HostAcceptorIdentityReplacementReservation {
            registry: Arc::clone(self),
            owner,
            token,
            committed: false,
        })
    }

    /// Once-only owner install: re-installing the SAME owner (Arc-ptr
    /// equality) is idempotent; rebinding to a different owner is refused.
    pub fn install_owner(
        &self,
        owner: Arc<dyn Any + Send + Sync>,
    ) -> Result<(), HostAcceptorError> {
        let mut state = self.state.write();
        if Self::reservation_in_progress(&state) {
            return Err(HostAcceptorError::RegistryReservationInProgress);
        }
        if let Some(existing) = state.owner.as_ref() {
            if Arc::ptr_eq(existing, &owner) {
                return Ok(());
            }
            return Err(HostAcceptorError::OwnerAlreadyInstalled);
        }
        state.owner = Some(owner);
        Ok(())
    }

    /// Register an inbound identity under the installed owner.
    ///
    /// `keypair` must be the signing keypair for `pubkey` — a mismatch would
    /// silently break ack verification sender-side, so it is a typed error
    /// here.
    pub fn register_identity(
        &self,
        owner: &Arc<dyn Any + Send + Sync>,
        pubkey: PubKey,
        keypair: Arc<Keypair>,
        inbox_sender: InboxSender,
    ) -> Result<(), HostAcceptorError> {
        if keypair.public_key() != pubkey {
            return Err(HostAcceptorError::IdentityKeypairMismatch {
                pubkey: pubkey.to_pubkey_string(),
            });
        }
        let mut state = self.state.write();
        if Self::reservation_in_progress(&state) {
            return Err(HostAcceptorError::RegistryReservationInProgress);
        }
        Self::validate_owner_state(&state, owner)?;
        if state.identities.contains_key(&pubkey) {
            return Err(HostAcceptorError::IdentityAlreadyRegistered {
                pubkey: pubkey.to_pubkey_string(),
            });
        }
        state.identities.insert(
            pubkey,
            RegisteredAcceptorIdentity {
                keypair,
                inbox_sender,
            },
        );
        Ok(())
    }

    /// Remove an identity under the installed owner. Returns whether the
    /// identity was present.
    pub fn remove_identity(
        &self,
        owner: &Arc<dyn Any + Send + Sync>,
        pubkey: &PubKey,
    ) -> Result<bool, HostAcceptorError> {
        let mut state = self.state.write();
        if Self::reservation_in_progress(&state) {
            return Err(HostAcceptorError::RegistryReservationInProgress);
        }
        Self::validate_owner_state(&state, owner)?;
        Ok(state.identities.remove(pubkey).is_some())
    }

    /// Resolve an inbound `envelope.to` to the addressed identity's ack
    /// keypair and inbox. `None` covers both never-registered and
    /// registered-then-removed identities — the demux path maps it to the
    /// typed misaddressed reject.
    pub(crate) fn resolve(&self, to: &PubKey) -> Option<(Arc<Keypair>, InboxSender)> {
        let state = self.state.read();
        let entry = state.identities.get(to)?;
        Some((entry.keypair.clone(), entry.inbox_sender.clone()))
    }

    fn validate_owner_state(
        state: &HostAcceptorIdentityRegistryState,
        owner: &Arc<dyn Any + Send + Sync>,
    ) -> Result<(), HostAcceptorError> {
        match state.owner.as_ref() {
            Some(installed) if Arc::ptr_eq(installed, owner) => Ok(()),
            Some(_) => Err(HostAcceptorError::OwnerMismatch),
            None => Err(HostAcceptorError::OwnerNotInstalled),
        }
    }
}

impl Default for HostAcceptorIdentityRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Configuration for the host acceptor.
///
/// Note the deliberate ABSENCE of any peer-auth field: signature
/// verification on the demux path is hardwired on (D1). The acceptor never
/// consults `CoreCommsConfig.require_peer_auth`.
pub struct HostAcceptorConfig {
    pub listen_tcp: SocketAddr,
    /// REQUIRED for non-loopback binds (tightens the member listener's
    /// wildcard-only rule: any non-loopback bind must state what it
    /// advertises). An explicit value must be
    /// `tcp://<host>:<nonzero-port>`; loopback binds default to
    /// `tcp://<local_addr>`.
    pub advertise_address: Option<String>,
    pub registry: Arc<HostAcceptorIdentityRegistry>,
    pub pairing: Option<HostPairingConfig>,
    pub bounds: HostAcceptorBounds,
}

/// Host-scoped pairing ceremony configuration (§7.2 step 2).
///
/// Deliberately holds NO router, trust-store, or persistence handles: the
/// host pairing branch cannot install trust or write snapshots by
/// construction (DEC-P2-3).
pub struct HostPairingConfig {
    /// Pairing secret; minimum 32 chars, validated at spawn.
    pub password: String,
    /// Challenge/proof target identity — the HOST keypair (§7.2).
    pub host_keypair: Arc<Keypair>,
    /// Host participant name carried in the pairing challenge/complete
    /// messages.
    pub participant_name: String,
    /// Pre-serialized `WireHostBindingDescriptor` JSON supplied by the
    /// daemon (comms stays contracts-free); returned verbatim as the
    /// PAIRING_COMPLETE `binding` object. Carries the CURRENT one-time
    /// token — the daemon refreshes this slot on token re-mint (DEC-P2-4).
    pub descriptor_json: watch::Receiver<String>,
}

/// Pre-auth resource bounds for the host acceptor (§21.2), config-fed from
/// `[mob_host]` with safe defaults.
#[derive(Debug, Clone, Copy)]
pub struct HostAcceptorBounds {
    /// Concurrent-connection cap; connections over the cap are dropped at
    /// the accept loop before any read.
    pub max_connections: usize,
    /// Bounds the complete pre-auth exchange per connection, from the
    /// classify peek through either the single signed envelope or the whole
    /// pairing challenge/proof exchange. This is one wall-clock budget, not
    /// a fresh timeout for each handshake phase.
    pub read_deadline: Duration,
    /// Pairing-branch rate limit, token bucket keyed by source IP.
    pub pairing_rate: PairingRateLimit,
}

pub const DEFAULT_HOST_ACCEPTOR_MAX_CONNECTIONS: usize = 64;
pub const DEFAULT_HOST_ACCEPTOR_READ_DEADLINE: Duration = Duration::from_secs(10);

impl Default for HostAcceptorBounds {
    fn default() -> Self {
        Self {
            max_connections: DEFAULT_HOST_ACCEPTOR_MAX_CONNECTIONS,
            read_deadline: DEFAULT_HOST_ACCEPTOR_READ_DEADLINE,
            pairing_rate: PairingRateLimit::default(),
        }
    }
}

/// Token-bucket pairing rate limit per source IP: bursts up to
/// `max_attempts`, refilling at `max_attempts` per `window`.
#[derive(Debug, Clone, Copy)]
pub struct PairingRateLimit {
    pub max_attempts: u32,
    pub window: Duration,
}

impl Default for PairingRateLimit {
    fn default() -> Self {
        Self {
            max_attempts: 10,
            window: Duration::from_secs(60),
        }
    }
}

/// Bound on tracked source-IP buckets. An entry idle for a full window has
/// necessarily refilled to capacity, so pruning it is lossless.
const PAIRING_RATE_MAX_TRACKED_SOURCES: usize = 1024;
const HOST_ACCEPTOR_ACCEPT_RETRY_DELAY: Duration = Duration::from_millis(100);

struct PairingRateLimiter {
    limit: PairingRateLimit,
    buckets: Mutex<HashMap<IpAddr, TokenBucket>>,
}

struct TokenBucket {
    tokens: f64,
    last_refill: Instant,
}

impl PairingRateLimiter {
    fn new(limit: PairingRateLimit) -> Self {
        Self {
            limit,
            buckets: Mutex::new(HashMap::new()),
        }
    }

    fn try_admit(&self, source: IpAddr) -> bool {
        if self.limit.max_attempts == 0 {
            return false;
        }
        let capacity = f64::from(self.limit.max_attempts);
        let now = Instant::now();
        let mut buckets = self.buckets.lock();
        if buckets.len() >= PAIRING_RATE_MAX_TRACKED_SOURCES && !buckets.contains_key(&source) {
            buckets.retain(|_, bucket| now.duration_since(bucket.last_refill) < self.limit.window);
            if buckets.len() >= PAIRING_RATE_MAX_TRACKED_SOURCES {
                // Every tracked source is currently active: refuse rather
                // than grow without bound (pre-auth fail-closed posture).
                return false;
            }
        }
        let bucket = buckets.entry(source).or_insert(TokenBucket {
            tokens: capacity,
            last_refill: now,
        });
        let window_secs = self.limit.window.as_secs_f64();
        if window_secs > 0.0 {
            let elapsed = now.duration_since(bucket.last_refill).as_secs_f64();
            bucket.tokens = (bucket.tokens + elapsed * capacity / window_secs).min(capacity);
        }
        bucket.last_refill = now;
        if bucket.tokens >= 1.0 {
            bucket.tokens -= 1.0;
            true
        } else {
            false
        }
    }
}

/// Typed host acceptor faults.
#[derive(Debug, thiserror::Error)]
pub enum HostAcceptorError {
    #[error("host acceptor registry is already bound to a different owner")]
    OwnerAlreadyInstalled,
    #[error("host acceptor registry has no installed owner")]
    OwnerNotInstalled,
    #[error("host acceptor registry mutation requires the installed owner")]
    OwnerMismatch,
    #[error("host acceptor registry has an identity reservation in progress")]
    RegistryReservationInProgress,
    #[error("identity {pubkey} is already registered on the host acceptor")]
    IdentityAlreadyRegistered { pubkey: String },
    #[error("expected current host acceptor identity generation {pubkey} is no longer installed")]
    ExpectedIdentityGenerationNotCurrent { pubkey: String },
    #[error("registered identity pubkey {pubkey} does not match the supplied keypair")]
    IdentityKeypairMismatch { pubkey: String },
    #[error("invalid host pairing secret: {reason}")]
    InvalidPairingSecret { reason: String },
    #[error(
        "host pairing may listen only on a loopback address; use the 0600 descriptor out-of-band or tunnel a loopback listener (got {addr})"
    )]
    PairingRequiresLoopback { addr: SocketAddr },
    #[error("invalid host acceptor advertise address '{address}': {reason}")]
    InvalidAdvertiseAddress { address: String, reason: String },
    #[error(
        "host acceptor bound to non-loopback address {addr} requires an explicit advertise address"
    )]
    AdvertiseAddressRequired { addr: SocketAddr },
    #[error("host acceptor bind failed: {0}")]
    Bind(#[from] std::io::Error),
}

/// Running host acceptor: the bound listener plus its accept-loop task.
pub struct HostAcceptorHandle {
    local_addr: SocketAddr,
    advertised_address: String,
    task: Option<tokio::task::JoinHandle<()>>,
    shutdown_tx: Option<oneshot::Sender<()>>,
    overflow_dropped: Arc<AtomicU64>,
    pairing_refused: Arc<AtomicU64>,
}

impl HostAcceptorHandle {
    /// Real bound address (ephemeral port resolved).
    pub fn local_addr(&self) -> SocketAddr {
        self.local_addr
    }

    /// The address peers should dial: the configured advertise address, else
    /// `tcp://<local_addr>` for loopback binds.
    pub fn advertised_address(&self) -> &str {
        &self.advertised_address
    }

    /// Connections dropped at the accept loop because the concurrent
    /// connection cap was reached.
    pub fn overflow_dropped(&self) -> u64 {
        self.overflow_dropped.load(Ordering::Relaxed)
    }

    /// Pairing attempts refused by the per-source rate limit.
    pub fn pairing_refused(&self) -> u64 {
        self.pairing_refused.load(Ordering::Relaxed)
    }

    /// Stop accepting new connections and wait for every accepted
    /// connection task to finish under its single pre-auth deadline. No
    /// connection task outlives successful shutdown.
    pub async fn shutdown(mut self) {
        if let Some(shutdown_tx) = self.shutdown_tx.take() {
            let _ = shutdown_tx.send(());
        }
        if let Some(task) = self.task.take() {
            let _ = task.await;
        }
    }
}

impl Drop for HostAcceptorHandle {
    fn drop(&mut self) {
        if let Some(shutdown_tx) = self.shutdown_tx.take() {
            let _ = shutdown_tx.send(());
        }
        if let Some(task) = self.task.as_ref() {
            // Drop cannot await the bounded connection drain. Aborting the
            // owner task drops its JoinSet, which aborts every owned child;
            // explicit `shutdown` remains the orderly join path.
            task.abort();
        }
    }
}

/// Bind the host acceptor and start its accept loop.
///
/// The loop mirrors the member TCP listener: bind, resolve the real
/// ephemeral `local_addr`, then one task per accepted connection — with the
/// §21.2 pre-auth bounds applied before any envelope is read.
pub async fn spawn_host_acceptor(
    config: HostAcceptorConfig,
) -> Result<HostAcceptorHandle, HostAcceptorError> {
    let HostAcceptorConfig {
        listen_tcp,
        advertise_address,
        registry,
        pairing,
        bounds,
    } = config;
    // Pairing returns the one-time host binding descriptor. The signed comms
    // transport is deliberately plaintext, so exposing this branch on a
    // remotely reachable listener would disclose the bootstrap token to a
    // passive on-path observer. Remote mob traffic remains supported on this
    // acceptor; only descriptor transfer is loopback/tunnel scoped.
    if pairing.is_some() && !listen_tcp.ip().is_loopback() {
        return Err(HostAcceptorError::PairingRequiresLoopback { addr: listen_tcp });
    }
    if let Some(pairing) = pairing.as_ref() {
        validate_pairing_secret(&pairing.password).map_err(|err| {
            HostAcceptorError::InvalidPairingSecret {
                reason: err.to_string(),
            }
        })?;
    }
    // Validate the explicit route before binding. This acceptor serves TCP
    // only, so publishing any other transport (or an unusable port) would
    // create a healthy-looking daemon whose signed binding descriptor cannot
    // route back to it.
    let advertise_address = advertise_address
        .map(|address| {
            let endpoint = parse_peer_address(&address).map_err(|reason| {
                HostAcceptorError::InvalidAdvertiseAddress {
                    address: address.clone(),
                    reason,
                }
            })?;
            if let Err(reason) = validate_advertised_tcp_endpoint(&endpoint) {
                return Err(HostAcceptorError::InvalidAdvertiseAddress {
                    address,
                    reason: reason.to_string(),
                });
            }
            Ok(address)
        })
        .transpose()?;
    let listener = TcpListener::bind(listen_tcp).await?;
    let local_addr = listener.local_addr()?;
    let advertised_address = match advertise_address {
        Some(address) => address,
        None => {
            if !local_addr.ip().is_loopback() {
                return Err(HostAcceptorError::AdvertiseAddressRequired { addr: local_addr });
            }
            format!("tcp://{local_addr}")
        }
    };

    let semaphore = Arc::new(Semaphore::new(bounds.max_connections));
    let overflow_dropped = Arc::new(AtomicU64::new(0));
    let pairing_refused = Arc::new(AtomicU64::new(0));
    let rate_limiter = Arc::new(PairingRateLimiter::new(bounds.pairing_rate));
    let pairing = Arc::new(pairing);
    let read_deadline = bounds.read_deadline;

    let loop_advertised = advertised_address.clone();
    let loop_overflow = overflow_dropped.clone();
    let loop_pairing_refused = pairing_refused.clone();
    let (shutdown_tx, mut shutdown_rx) = oneshot::channel();
    let task = tokio::spawn(async move {
        let mut connections = JoinSet::new();
        loop {
            let accepted = tokio::select! {
                biased;
                _ = &mut shutdown_rx => break,
                joined = connections.join_next(), if !connections.is_empty() => {
                    if let Some(Err(error)) = joined {
                        tracing::warn!(%error, "mob host acceptor connection task failed");
                    }
                    continue;
                }
                accepted = listener.accept() => accepted,
            };
            let (stream, source) = match accepted {
                Ok(accepted) => accepted,
                Err(error) => {
                    // Tokio listeners may surface transient accept errors.
                    // Exiting here would leave the daemon and its published
                    // binding descriptor alive while ingress had silently
                    // stopped. Retry under a small backoff so a persistent
                    // descriptor never claims a dead listener after one
                    // recoverable kernel error.
                    tracing::error!(
                        %error,
                        retry_delay_ms = HOST_ACCEPTOR_ACCEPT_RETRY_DELAY.as_millis(),
                        "mob host acceptor failed to accept a connection; retrying"
                    );
                    tokio::select! {
                        biased;
                        _ = &mut shutdown_rx => break,
                        () = tokio::time::sleep(HOST_ACCEPTOR_ACCEPT_RETRY_DELAY) => {}
                    }
                    continue;
                }
            };
            // Connection cap is enforced here, before any read, so an
            // over-cap connection costs nothing beyond the accept.
            let Ok(permit) = semaphore.clone().try_acquire_owned() else {
                loop_overflow.fetch_add(1, Ordering::Relaxed);
                tracing::warn!(
                    %source,
                    "host acceptor connection cap reached; dropping connection"
                );
                continue;
            };
            let registry = registry.clone();
            let pairing = pairing.clone();
            let rate_limiter = rate_limiter.clone();
            let advertised = loop_advertised.clone();
            let pairing_refused = loop_pairing_refused.clone();
            connections.spawn(async move {
                let _permit = permit;
                if let Err(err) = handle_host_connection(
                    stream,
                    source,
                    &registry,
                    pairing.as_ref().as_ref(),
                    &rate_limiter,
                    &pairing_refused,
                    read_deadline,
                    &advertised,
                )
                .await
                {
                    tracing::debug!(
                        %source,
                        error = %err,
                        "host acceptor connection ended with error"
                    );
                }
            });
        }

        // Stop kernel admission immediately. Keeping the listener alive
        // while draining children would leave its backlog connectable even
        // though no task polls `accept` anymore.
        drop(listener);
        // The listener is no longer polled. Retain ownership of accepted
        // tasks and wait for their already-running, deadline-bounded
        // exchanges so shutdown cannot return with a live pairing or demux
        // task still holding transport/state references.
        while let Some(joined) = connections.join_next().await {
            if let Err(error) = joined {
                tracing::warn!(%error, "mob host acceptor connection task failed during shutdown");
            }
        }
    });

    Ok(HostAcceptorHandle {
        local_addr,
        advertised_address,
        task: Some(task),
        shutdown_tx: Some(shutdown_tx),
        overflow_dropped,
        pairing_refused,
    })
}

#[allow(clippy::too_many_arguments)]
async fn handle_host_connection(
    stream: TcpStream,
    source: SocketAddr,
    registry: &HostAcceptorIdentityRegistry,
    pairing: Option<&HostPairingConfig>,
    rate_limiter: &PairingRateLimiter,
    pairing_refused: &AtomicU64,
    read_deadline: Duration,
    advertised_address: &str,
) -> Result<(), std::io::Error> {
    match tokio::time::timeout(
        read_deadline,
        handle_host_connection_within_deadline(
            stream,
            source,
            registry,
            pairing,
            rate_limiter,
            pairing_refused,
            advertised_address,
        ),
    )
    .await
    {
        Ok(outcome) => outcome,
        Err(_) => Err(std::io::Error::new(
            std::io::ErrorKind::TimedOut,
            "host acceptor read deadline elapsed",
        )),
    }
}

#[allow(clippy::too_many_arguments)]
async fn handle_host_connection_within_deadline(
    stream: TcpStream,
    source: SocketAddr,
    registry: &HostAcceptorIdentityRegistry,
    pairing: Option<&HostPairingConfig>,
    rate_limiter: &PairingRateLimiter,
    pairing_refused: &AtomicU64,
    advertised_address: &str,
) -> Result<(), std::io::Error> {
    // First-byte `{` sniff, exactly as the member listener classifies:
    // signed CBOR envelope frames never start with `{`.
    let mut first = [0_u8; 1];
    let is_pairing = pairing.is_some() && stream.peek(&mut first).await? == 1 && first[0] == b'{';
    if is_pairing && let Some(pairing) = pairing {
        // Pre-auth pairing rate limit: refused before the challenge is
        // issued, so an abusive source never advances the handshake.
        if !rate_limiter.try_admit(source.ip()) {
            pairing_refused.fetch_add(1, Ordering::Relaxed);
            tracing::warn!(%source, "host pairing rate limit exceeded; refusing attempt");
            return Err(std::io::Error::new(
                std::io::ErrorKind::PermissionDenied,
                "host pairing rate limit exceeded",
            ));
        }
        return handle_host_pairing_connection(stream, pairing, advertised_address).await;
    }

    // Peer auth is hardwired on (no parameter exists). The caller's single
    // outer timeout also covers classification and the full pairing branch.
    handle_tcp_connection_demux(stream, source, registry)
        .await
        .map_err(|err| std::io::Error::other(err.to_string()))
}

/// Host-scoped pairing handshake (§7.2 step 2), modeled on the member
/// pairing recipe with two deliberate differences (DEC-P2-3): the
/// challenge/proof target identity is the HOST keypair, and completing the
/// handshake writes NOTHING — no `add_trusted_peer_for_source`, no trust
/// snapshot persistence. It hands out the daemon-supplied binding
/// descriptor and stops.
async fn handle_host_pairing_connection(
    stream: TcpStream,
    pairing: &HostPairingConfig,
    advertised_address: &str,
) -> Result<(), std::io::Error> {
    let mut reader = BufReader::new(stream);
    let mut line = String::new();
    let PairingClientMessage::Hello { version } =
        read_pairing_message(&mut reader, &mut line).await?
    else {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "expected meerkat pairing hello",
        ));
    };
    if version != PAIRING_VERSION {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "unsupported meerkat pairing version",
        ));
    }

    let challenge = Uuid::new_v4().to_string();
    write_pairing_json(
        reader.get_mut(),
        serde_json::json!({
            "kind": PAIRING_CHALLENGE_KIND,
            "version": PAIRING_VERSION,
            "challenge": challenge,
            "target": {
                "name": pairing.participant_name,
                "address": advertised_address,
                "identity": {
                    "kind": "ed25519_public_key",
                    "public_key": pairing.host_keypair.public_key().to_pubkey_string(),
                }
            }
        }),
    )
    .await?;

    let PairingClientMessage::Proof {
        caller,
        password_proof,
    } = read_pairing_message(&mut reader, &mut line).await?
    else {
        return Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            "expected meerkat pairing proof",
        ));
    };
    let PairingIdentityKind::Ed25519PublicKey = caller.identity.kind;
    let expected = comms_pairing_password_proof(
        &pairing.password,
        &challenge,
        &caller.identity.public_key.raw,
        &caller.address.0,
    );
    if !constant_time_str_eq(&password_proof, &expected) {
        return Err(std::io::Error::new(
            std::io::ErrorKind::PermissionDenied,
            "invalid pairing password proof",
        ));
    }

    // The descriptor is an opaque pass-through: the daemon owns the typed
    // `WireHostBindingDescriptor`; comms embeds the current serialized form
    // verbatim (the watch slot carries the CURRENT one-time token).
    let descriptor_json = pairing.descriptor_json.borrow().clone();
    let descriptor: serde_json::Value = serde_json::from_str(&descriptor_json).map_err(|err| {
        std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("host binding descriptor is not valid JSON: {err}"),
        )
    })?;
    write_pairing_json(
        reader.get_mut(),
        serde_json::json!({
            "kind": PAIRING_COMPLETE_KIND,
            "version": PAIRING_VERSION,
            "binding": descriptor,
            "comms_name": pairing.participant_name,
        }),
    )
    .await
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;
    use crate::classify::test_support;
    use crate::identity::Signature;
    use crate::inbox::Inbox;
    use crate::router::{CommsConfig, Router, SendError};
    use crate::transport::codec::TransportCodec;
    use crate::trust::{TrustEntry, TrustStore};
    use crate::types::{Envelope, InboxItem, MessageKind};
    use futures::StreamExt;
    use meerkat_core::comms::{
        GeneratedCommsTrustAuthoritySourceKind, PeerAddress, PeerDeliveryOutcome, PeerId, PeerName,
    };
    use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
    use tokio_util::codec::FramedRead;

    struct MemberFixture {
        keypair: Keypair,
        trusted: Arc<parking_lot::RwLock<TrustStore>>,
        inbox: Inbox,
        inbox_sender: InboxSender,
    }

    fn member_trusting(sender_pubkey: &PubKey, require_peer_auth: bool) -> MemberFixture {
        let keypair = Keypair::generate();
        let mut store = TrustStore::new();
        store
            .insert(TrustEntry {
                peer_id: sender_pubkey.to_peer_id(),
                name: PeerName::new("sender").expect("valid peer name"),
                pubkey: *sender_pubkey,
                address: PeerAddress::parse("tcp://127.0.0.1:1").expect("valid peer address"),
                meta: crate::PeerMeta::default(),
            })
            .expect("sender trust insert");
        let trusted = Arc::new(parking_lot::RwLock::new(store));
        let (inbox, inbox_sender) = Inbox::new_classified(
            test_support::classification_context_shared(trusted.clone(), require_peer_auth),
        );
        MemberFixture {
            keypair,
            trusted,
            inbox,
            inbox_sender,
        }
    }

    fn registry_with_owner() -> (
        Arc<HostAcceptorIdentityRegistry>,
        Arc<dyn Any + Send + Sync>,
    ) {
        let registry = Arc::new(HostAcceptorIdentityRegistry::new());
        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry
            .install_owner(owner.clone())
            .expect("owner install");
        (registry, owner)
    }

    fn register_member(
        registry: &HostAcceptorIdentityRegistry,
        owner: &Arc<dyn Any + Send + Sync>,
        member: &MemberFixture,
    ) {
        registry
            .register_identity(
                owner,
                member.keypair.public_key(),
                Arc::new(member.keypair.clone()),
                member.inbox_sender.clone(),
            )
            .expect("identity registration");
    }

    async fn spawn_loopback_acceptor(
        registry: Arc<HostAcceptorIdentityRegistry>,
        pairing: Option<HostPairingConfig>,
        bounds: HostAcceptorBounds,
    ) -> HostAcceptorHandle {
        spawn_host_acceptor(HostAcceptorConfig {
            listen_tcp: "127.0.0.1:0".parse().expect("loopback socket addr"),
            advertise_address: None,
            registry,
            pairing,
            bounds,
        })
        .await
        .expect("host acceptor spawn")
    }

    /// Sender-side router whose ack validation (`ack.from == sent_to`) is
    /// the per-member ack-signing assertion.
    fn sender_router(sender_keypair: &Keypair) -> (Router, Inbox) {
        let (inbox, inbox_sender) = Inbox::new();
        let router = Router::new(
            sender_keypair.clone(),
            CommsConfig::default(),
            inbox_sender,
            true,
        );
        (router, inbox)
    }

    fn trust_recipient(router: &Router, name: &str, pubkey: &PubKey, address: &str) -> PeerId {
        let peer_id = pubkey.to_peer_id();
        router
            .add_trusted_peer_for_source(
                TrustEntry {
                    peer_id,
                    name: PeerName::new(name).expect("valid peer name"),
                    pubkey: *pubkey,
                    address: PeerAddress::parse(address).expect("valid peer address"),
                    meta: crate::PeerMeta::default(),
                },
                GeneratedCommsTrustAuthoritySourceKind::MobMachineExternalPeerTrustWiring,
                false,
            )
            .expect("recipient trust install");
        peer_id
    }

    fn test_message() -> MessageKind {
        MessageKind::Message {
            content_taint: None,
            blocks: None,
            body: "hello".to_string(),
            handling_mode: None,
            objective_id: None,
        }
    }

    fn envelope_frame_bytes(envelope: &Envelope) -> Vec<u8> {
        let mut payload = Vec::new();
        ciborium::into_writer(envelope, &mut payload).unwrap();
        let mut bytes = Vec::new();
        bytes.extend_from_slice(&(payload.len() as u32).to_be_bytes());
        bytes.extend_from_slice(&payload);
        bytes
    }

    // ---- registry owner gate ----

    #[test]
    fn registry_owner_install_is_once_only_and_idempotent() {
        let registry = HostAcceptorIdentityRegistry::new();
        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry.install_owner(owner.clone()).unwrap();
        // Same Arc: idempotent.
        registry.install_owner(owner.clone()).unwrap();
        // Different owner: refused.
        let other: Arc<dyn Any + Send + Sync> = Arc::new(());
        assert!(matches!(
            registry.install_owner(other),
            Err(HostAcceptorError::OwnerAlreadyInstalled)
        ));
    }

    #[test]
    fn registry_identity_reservation_is_hidden_reversible_and_exactly_committed() {
        let registry = Arc::new(HostAcceptorIdentityRegistry::new());
        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        let keypair = Arc::new(Keypair::generate());
        let reservation = registry
            .reserve_identity(
                Arc::clone(&owner),
                keypair.public_key(),
                Arc::clone(&keypair),
            )
            .expect("reserve host identity");
        assert!(
            registry.resolve(&keypair.public_key()).is_none(),
            "reserved identity must remain invisible to the acceptor"
        );
        assert!(matches!(
            registry.install_owner(Arc::clone(&owner)),
            Err(HostAcceptorError::RegistryReservationInProgress)
        ));
        drop(reservation);

        let replacement_owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry
            .install_owner(replacement_owner)
            .expect("dropped reservation restores an unowned registry");

        let committed_registry = Arc::new(HostAcceptorIdentityRegistry::new());
        let committed_owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        let committed_keypair = Arc::new(Keypair::generate());
        let (_inbox, committed_inbox_sender) = Inbox::new();
        committed_registry
            .reserve_identity(
                committed_owner,
                committed_keypair.public_key(),
                Arc::clone(&committed_keypair),
            )
            .expect("reserve committed host identity")
            .commit(committed_inbox_sender);
        assert!(
            committed_registry
                .resolve(&committed_keypair.public_key())
                .is_some(),
            "reservation commit makes the exact identity resolvable"
        );
    }

    #[test]
    fn identity_replacement_reservation_is_inert_until_atomic_commit() {
        let registry = Arc::new(HostAcceptorIdentityRegistry::new());
        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry
            .install_owner(Arc::clone(&owner))
            .expect("owner install");
        let current = Arc::new(Keypair::generate());
        let replacement = Arc::new(Keypair::generate());
        let (_current_inbox, current_sender) = Inbox::new();
        let (_replacement_inbox, replacement_sender) = Inbox::new();
        registry
            .register_identity(
                &owner,
                current.public_key(),
                Arc::clone(&current),
                current_sender.clone(),
            )
            .expect("current identity registration");

        let prepared = registry
            .reserve_identity_replacement(
                Arc::clone(&owner),
                current.public_key(),
                &current_sender,
                replacement.public_key(),
                Arc::clone(&replacement),
            )
            .expect("prepare exact replacement");
        let still_current = registry
            .resolve(&current.public_key())
            .expect("current route remains sole route while prepared");
        assert!(still_current.1.same_inbox(&current_sender));
        assert!(registry.resolve(&replacement.public_key()).is_none());
        drop(prepared);

        let after_drop = registry
            .resolve(&current.public_key())
            .expect("dropping preparation leaves current route intact");
        assert!(after_drop.1.same_inbox(&current_sender));
        assert!(registry.resolve(&replacement.public_key()).is_none());

        registry
            .reserve_identity_replacement(
                Arc::clone(&owner),
                current.public_key(),
                &current_sender,
                replacement.public_key(),
                Arc::clone(&replacement),
            )
            .expect("prepare committed replacement")
            .commit(replacement_sender.clone());
        assert!(registry.resolve(&current.public_key()).is_none());
        let committed = registry
            .resolve(&replacement.public_key())
            .expect("replacement route becomes visible at commit");
        assert!(committed.1.same_inbox(&replacement_sender));
    }

    #[test]
    fn identity_replacement_rejects_stale_generation_without_mutation() {
        let registry = Arc::new(HostAcceptorIdentityRegistry::new());
        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry
            .install_owner(Arc::clone(&owner))
            .expect("owner install");
        let current = Arc::new(Keypair::generate());
        let replacement = Arc::new(Keypair::generate());
        let (_current_inbox, current_sender) = Inbox::new();
        let (_stale_inbox, stale_sender) = Inbox::new();
        registry
            .register_identity(
                &owner,
                current.public_key(),
                Arc::clone(&current),
                current_sender.clone(),
            )
            .expect("current identity registration");

        assert!(matches!(
            registry.reserve_identity_replacement(
                Arc::clone(&owner),
                current.public_key(),
                &stale_sender,
                replacement.public_key(),
                replacement,
            ),
            Err(HostAcceptorError::ExpectedIdentityGenerationNotCurrent { .. })
        ));
        let retained = registry
            .resolve(&current.public_key())
            .expect("stale reservation must retain current route");
        assert!(retained.1.same_inbox(&current_sender));
    }

    #[test]
    fn registry_mutations_require_installed_owner() {
        let registry = HostAcceptorIdentityRegistry::new();
        let keypair = Keypair::generate();
        let (_inbox, inbox_sender) = Inbox::new();
        let stranger: Arc<dyn Any + Send + Sync> = Arc::new(());

        // No owner installed yet: register/remove fail closed.
        assert!(matches!(
            registry.register_identity(
                &stranger,
                keypair.public_key(),
                Arc::new(keypair.clone()),
                inbox_sender.clone(),
            ),
            Err(HostAcceptorError::OwnerNotInstalled)
        ));
        assert!(matches!(
            registry.remove_identity(&stranger, &keypair.public_key()),
            Err(HostAcceptorError::OwnerNotInstalled)
        ));

        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry.install_owner(owner.clone()).unwrap();

        // Wrong owner: refused, registry never mutated.
        assert!(matches!(
            registry.register_identity(
                &stranger,
                keypair.public_key(),
                Arc::new(keypair.clone()),
                inbox_sender.clone(),
            ),
            Err(HostAcceptorError::OwnerMismatch)
        ));
        assert!(registry.resolve(&keypair.public_key()).is_none());

        registry
            .register_identity(
                &owner,
                keypair.public_key(),
                Arc::new(keypair.clone()),
                inbox_sender,
            )
            .unwrap();
        assert!(registry.resolve(&keypair.public_key()).is_some());
    }

    #[test]
    fn registry_rejects_duplicate_and_mismatched_identities() {
        let registry = HostAcceptorIdentityRegistry::new();
        let owner: Arc<dyn Any + Send + Sync> = Arc::new(());
        registry.install_owner(owner.clone()).unwrap();
        let keypair = Keypair::generate();
        let other_keypair = Keypair::generate();
        let (_inbox, inbox_sender) = Inbox::new();

        // A keypair that cannot sign for the claimed pubkey is a typed error.
        assert!(matches!(
            registry.register_identity(
                &owner,
                keypair.public_key(),
                Arc::new(other_keypair),
                inbox_sender.clone(),
            ),
            Err(HostAcceptorError::IdentityKeypairMismatch { .. })
        ));

        registry
            .register_identity(
                &owner,
                keypair.public_key(),
                Arc::new(keypair.clone()),
                inbox_sender.clone(),
            )
            .unwrap();
        assert!(matches!(
            registry.register_identity(
                &owner,
                keypair.public_key(),
                Arc::new(keypair),
                inbox_sender,
            ),
            Err(HostAcceptorError::IdentityAlreadyRegistered { .. })
        ));
    }

    // ---- spawn validation ----

    #[tokio::test]
    async fn spawn_rejects_short_pairing_secret() {
        let (registry, _owner) = registry_with_owner();
        let (_tx, rx) = watch::channel(String::from("{}"));
        let result = spawn_host_acceptor(HostAcceptorConfig {
            listen_tcp: "127.0.0.1:0".parse().unwrap(),
            advertise_address: None,
            registry,
            pairing: Some(HostPairingConfig {
                password: "short".to_string(),
                host_keypair: Arc::new(Keypair::generate()),
                participant_name: "host".to_string(),
                descriptor_json: rx,
            }),
            bounds: HostAcceptorBounds::default(),
        })
        .await;
        assert!(matches!(
            result,
            Err(HostAcceptorError::InvalidPairingSecret { .. })
        ));
    }

    #[tokio::test]
    async fn spawn_rejects_pairing_on_a_non_loopback_listener_before_bind() {
        let (registry, _owner) = registry_with_owner();
        let (_tx, rx) = watch::channel(String::from("{}"));
        let addr: SocketAddr = "0.0.0.0:0".parse().expect("wildcard socket address");
        let result = spawn_host_acceptor(HostAcceptorConfig {
            listen_tcp: addr,
            advertise_address: Some("tcp://203.0.113.10:7801".to_string()),
            registry,
            pairing: Some(HostPairingConfig {
                password: "a-very-long-host-pairing-secret-0123456789".to_string(),
                host_keypair: Arc::new(Keypair::generate()),
                participant_name: "host".to_string(),
                descriptor_json: rx,
            }),
            bounds: HostAcceptorBounds::default(),
        })
        .await;
        assert!(matches!(
            result,
            Err(HostAcceptorError::PairingRequiresLoopback { addr: rejected })
                if rejected == addr
        ));
    }

    #[tokio::test]
    async fn spawn_requires_advertise_address_for_non_loopback_bind() {
        let (registry, _owner) = registry_with_owner();
        let result = spawn_host_acceptor(HostAcceptorConfig {
            listen_tcp: "0.0.0.0:0".parse().unwrap(),
            advertise_address: None,
            registry,
            pairing: None,
            bounds: HostAcceptorBounds::default(),
        })
        .await;
        assert!(matches!(
            result,
            Err(HostAcceptorError::AdvertiseAddressRequired { .. })
        ));
    }

    #[tokio::test]
    async fn spawn_rejects_unparseable_advertise_address() {
        let (registry, _owner) = registry_with_owner();
        let result = spawn_host_acceptor(HostAcceptorConfig {
            listen_tcp: "127.0.0.1:0".parse().unwrap(),
            advertise_address: Some("not a peer address".to_string()),
            registry,
            pairing: None,
            bounds: HostAcceptorBounds::default(),
        })
        .await;
        assert!(matches!(
            result,
            Err(HostAcceptorError::InvalidAdvertiseAddress { .. })
        ));
    }

    #[tokio::test]
    async fn spawn_rejects_non_tcp_or_unusable_tcp_advertise_address() {
        for address in [
            "inproc://host",
            "uds:///tmp/host.sock",
            "tcp://host:0",
            "tcp://0.0.0.0:7801",
            "tcp://[::]:7801",
            "tcp://[::ffff:0.0.0.0]:7801",
            "tcp://*:7801",
            "tcp://[not-an-ip]:7801",
            "tcp://[127.0.0.1]:7801",
            "tcp://host",
            "tcp://host:not-a-port",
            "tcp://host:65536",
            "tcp://:7801",
            "tcp://2001:db8::1:7801",
        ] {
            let (registry, _owner) = registry_with_owner();
            let result = spawn_host_acceptor(HostAcceptorConfig {
                listen_tcp: "127.0.0.1:0".parse().unwrap(),
                advertise_address: Some(address.to_string()),
                registry,
                pairing: None,
                bounds: HostAcceptorBounds::default(),
            })
            .await;
            assert!(
                matches!(
                    result,
                    Err(HostAcceptorError::InvalidAdvertiseAddress { .. })
                ),
                "invalid TCP advertise route {address} must fail before the acceptor starts"
            );
        }
    }

    #[tokio::test]
    async fn spawn_accepts_dns_and_bracketed_ipv6_tcp_advertise_addresses() {
        for address in ["tcp://worker.example:7801", "tcp://[2001:db8::1]:7801"] {
            let (registry, _owner) = registry_with_owner();
            let handle = spawn_host_acceptor(HostAcceptorConfig {
                listen_tcp: "127.0.0.1:0".parse().unwrap(),
                advertise_address: Some(address.to_string()),
                registry,
                pairing: None,
                bounds: HostAcceptorBounds::default(),
            })
            .await
            .expect("valid advertised TCP route should not require DNS resolution at startup");
            assert_eq!(handle.advertised_address(), address);
            handle.shutdown().await;
        }
    }

    // ---- §11 demux rows over real loopback TCP ----

    /// Two identities on one acceptor: an envelope to A lands in A's inbox
    /// with `delivery == Acked` — the router's `ack.from == sent_to` validation
    /// IS the per-member ack-signing assertion — and the same holds for B.
    #[tokio::test]
    async fn acceptor_demuxes_by_to_and_acks_per_member() {
        let sender_keypair = Keypair::generate();
        let mut member_a = member_trusting(&sender_keypair.public_key(), true);
        let mut member_b = member_trusting(&sender_keypair.public_key(), true);
        let (registry, owner) = registry_with_owner();
        register_member(&registry, &owner, &member_a);
        register_member(&registry, &owner, &member_b);
        let handle = spawn_loopback_acceptor(registry, None, HostAcceptorBounds::default()).await;
        let address = handle.advertised_address().to_string();

        let (router, _sender_inbox) = sender_router(&sender_keypair);
        let peer_a = trust_recipient(
            &router,
            "member-a",
            &member_a.keypair.public_key(),
            &address,
        );
        let peer_b = trust_recipient(
            &router,
            "member-b",
            &member_b.keypair.public_key(),
            &address,
        );

        let outcome_a = router
            .send(peer_a, test_message())
            .await
            .expect("send to A");
        assert!(
            matches!(outcome_a.delivery, PeerDeliveryOutcome::Acked),
            "ack for A must be signed by A's keypair (ack.from == sent_to)"
        );
        let items_a = member_a.inbox.try_drain_classified();
        assert_eq!(items_a.len(), 1, "envelope to A lands in A's inbox");
        match &items_a[0].item {
            InboxItem::External { envelope } => {
                assert_eq!(envelope.to, member_a.keypair.public_key());
            }
            _ => panic!("expected External"),
        }
        assert!(
            member_b.inbox.try_drain_classified().is_empty(),
            "B must not see A's envelope"
        );

        let outcome_b = router
            .send(peer_b, test_message())
            .await
            .expect("send to B");
        assert!(
            matches!(outcome_b.delivery, PeerDeliveryOutcome::Acked),
            "ack for B must be signed by B's keypair (ack.from == sent_to)"
        );
        let items_b = member_b.inbox.try_drain_classified();
        assert_eq!(items_b.len(), 1, "envelope to B lands in B's inbox");
        assert!(member_a.inbox.try_drain_classified().is_empty());

        handle.shutdown().await;
    }

    /// An envelope addressed to an unregistered identity is refused with no
    /// ack — the sender observes `PeerOffline`.
    #[tokio::test]
    async fn acceptor_send_to_unregistered_identity_reports_peer_offline() {
        let sender_keypair = Keypair::generate();
        let mut member_a = member_trusting(&sender_keypair.public_key(), true);
        let stranger = Keypair::generate();
        let (registry, owner) = registry_with_owner();
        register_member(&registry, &owner, &member_a);
        let handle = spawn_loopback_acceptor(registry, None, HostAcceptorBounds::default()).await;
        let address = handle.advertised_address().to_string();

        let (router, _sender_inbox) = sender_router(&sender_keypair);
        let peer_stranger = trust_recipient(&router, "stranger", &stranger.public_key(), &address);

        let outcome = router.send(peer_stranger, test_message()).await;
        assert!(
            matches!(outcome, Err(SendError::PeerOffline)),
            "misaddressed envelope must surface sender-side as PeerOffline, got {outcome:?}"
        );
        assert!(member_a.inbox.try_drain_classified().is_empty());

        handle.shutdown().await;
    }

    /// Register → remove → send is rejected exactly like never-registered.
    #[tokio::test]
    async fn acceptor_rejects_registered_then_removed_identity() {
        let sender_keypair = Keypair::generate();
        let mut member_a = member_trusting(&sender_keypair.public_key(), true);
        let (registry, owner) = registry_with_owner();
        register_member(&registry, &owner, &member_a);
        let handle =
            spawn_loopback_acceptor(registry.clone(), None, HostAcceptorBounds::default()).await;
        let address = handle.advertised_address().to_string();

        let (router, _sender_inbox) = sender_router(&sender_keypair);
        let peer_a = trust_recipient(
            &router,
            "member-a",
            &member_a.keypair.public_key(),
            &address,
        );

        let before = router
            .send(peer_a, test_message())
            .await
            .expect("send while registered");
        assert!(matches!(before.delivery, PeerDeliveryOutcome::Acked));
        assert_eq!(member_a.inbox.try_drain_classified().len(), 1);

        assert!(
            registry
                .remove_identity(&owner, &member_a.keypair.public_key())
                .expect("owner removal")
        );

        let after = router.send(peer_a, test_message()).await;
        assert!(
            matches!(after, Err(SendError::PeerOffline)),
            "removed identity must reject like never-registered, got {after:?}"
        );
        assert!(member_a.inbox.try_drain_classified().is_empty());

        handle.shutdown().await;
    }

    /// D1 type-level pin: `HostAcceptorConfig` has no peer-auth field to
    /// consult, so even an identity whose own inbox context was built from a
    /// `require_peer_auth = false` configuration still rejects unsigned
    /// envelopes at the acceptor's hardwired signature gate.
    #[tokio::test]
    async fn acceptor_rejects_unsigned_envelope_and_has_no_auth_knob() {
        let sender_keypair = Keypair::generate();
        let mut member_a = member_trusting(&sender_keypair.public_key(), false);
        let (registry, owner) = registry_with_owner();
        register_member(&registry, &owner, &member_a);
        let handle = spawn_loopback_acceptor(registry, None, HostAcceptorBounds::default()).await;

        let envelope = Envelope {
            id: Uuid::new_v4(),
            from: sender_keypair.public_key(),
            to: member_a.keypair.public_key(),
            kind: test_message(),
            sig: Signature::new([0u8; 64]), // unsigned
        };
        let bytes = envelope_frame_bytes(&envelope);

        let mut stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        stream.write_all(&bytes).await.expect("write frame");
        let mut buf = [0u8; 64];
        let read = tokio::time::timeout(Duration::from_secs(5), stream.read(&mut buf))
            .await
            .expect("read completes before deadline")
            .expect("read");
        assert_eq!(
            read, 0,
            "connection must close with no ack for an unsigned envelope"
        );
        assert!(member_a.inbox.try_drain_classified().is_empty());

        handle.shutdown().await;
    }

    /// §11 byte-compat row at the acceptor: hand-encoded frame bytes (u32-BE
    /// length prefix + ciborium CBOR) drive the demux path end to end; the
    /// signature verifies, delivery happens, and the ack decodes with the
    /// addressed member as signer.
    #[tokio::test]
    async fn acceptor_accepts_hand_encoded_envelope_bytes() {
        let sender_keypair = Keypair::generate();
        let mut member_a = member_trusting(&sender_keypair.public_key(), true);
        let (registry, owner) = registry_with_owner();
        register_member(&registry, &owner, &member_a);
        let handle = spawn_loopback_acceptor(registry, None, HostAcceptorBounds::default()).await;

        let mut envelope = Envelope {
            id: Uuid::new_v4(),
            from: sender_keypair.public_key(),
            to: member_a.keypair.public_key(),
            kind: test_message(),
            sig: Signature::new([0u8; 64]),
        };
        envelope.sign(&sender_keypair);
        let original_id = envelope.id;
        let bytes = envelope_frame_bytes(&envelope);

        let mut stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        stream.write_all(&bytes).await.expect("write frame");
        let mut framed = FramedRead::new(
            &mut stream,
            TransportCodec::new(crate::transport::MAX_PAYLOAD_SIZE),
        );
        let ack = match tokio::time::timeout(Duration::from_secs(5), framed.next()).await {
            Ok(Some(Ok(frame))) => frame.envelope,
            other => panic!("expected ack frame, got {other:?}"),
        };
        match ack.kind {
            MessageKind::Ack { in_reply_to } => assert_eq!(in_reply_to, original_id),
            _ => panic!("expected Ack"),
        }
        assert_eq!(ack.from, member_a.keypair.public_key());
        assert!(ack.verify());

        let items = member_a.inbox.try_drain_classified();
        assert_eq!(items.len(), 1);
        match &items[0].item {
            InboxItem::External { envelope } => assert_eq!(envelope.id, original_id),
            _ => panic!("expected External"),
        }

        handle.shutdown().await;
    }

    // ---- pre-auth bounds (§21.2) ----

    #[tokio::test]
    async fn acceptor_connection_cap_drops_overflow() {
        let (registry, _owner) = registry_with_owner();
        let bounds = HostAcceptorBounds {
            max_connections: 1,
            read_deadline: Duration::from_secs(30),
            pairing_rate: PairingRateLimit::default(),
        };
        let handle = spawn_loopback_acceptor(registry, None, bounds).await;

        // First connection stalls, holding the single permit.
        let _held = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect held");
        // Second connection must be dropped at the accept loop.
        let mut overflow = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect overflow");
        let mut buf = [0u8; 8];
        let read = tokio::time::timeout(Duration::from_secs(5), overflow.read(&mut buf))
            .await
            .expect("overflow connection must be closed promptly")
            .expect("read");
        assert_eq!(read, 0, "over-cap connection must be dropped without data");
        assert_eq!(handle.overflow_dropped(), 1);

        handle.shutdown().await;
    }

    #[tokio::test]
    async fn acceptor_read_deadline_closes_stalled_connection() {
        let (registry, _owner) = registry_with_owner();
        let bounds = HostAcceptorBounds {
            max_connections: 4,
            read_deadline: Duration::from_millis(200),
            pairing_rate: PairingRateLimit::default(),
        };
        let handle = spawn_loopback_acceptor(registry, None, bounds).await;

        // Open and stall: send nothing.
        let mut stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        let mut buf = [0u8; 8];
        let read = tokio::time::timeout(Duration::from_secs(5), stream.read(&mut buf))
            .await
            .expect("stalled connection must be closed by the read deadline")
            .expect("read");
        assert_eq!(read, 0, "stalled connection must be closed with no data");

        handle.shutdown().await;
    }

    #[tokio::test]
    async fn host_pairing_entire_handshake_obeys_acceptor_read_deadline() {
        let (pairing, _tx, _host_keypair, _password) =
            pairing_fixture(r#"{"kind":"host","bootstrap_token":"tok-1"}"#);
        let (registry, _owner) = registry_with_owner();
        let bounds = HostAcceptorBounds {
            max_connections: 4,
            read_deadline: Duration::from_millis(200),
            pairing_rate: PairingRateLimit::default(),
        };
        let handle = spawn_loopback_acceptor(registry, Some(pairing), bounds).await;

        let stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        let mut reader = tokio::io::BufReader::new(stream);
        reader
            .get_mut()
            .write_all(b"{\"kind\":\"meerkat_pairing_hello\",\"version\":1}\n")
            .await
            .expect("write hello");
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("read challenge");
        let challenge: serde_json::Value = serde_json::from_str(&line).expect("challenge JSON");
        assert_eq!(challenge["kind"], PAIRING_CHALLENGE_KIND);

        // Stall before proof. This used to bypass the configured 200ms
        // acceptor budget and inherit read_pairing_message's independent
        // ten-second timeout. The one outer connection deadline must close
        // the socket promptly instead.
        let mut byte = [0_u8; 1];
        let read = tokio::time::timeout(Duration::from_secs(1), reader.read(&mut byte))
            .await
            .expect("the configured host acceptor deadline must close pairing");
        match read {
            Ok(0) => {}
            Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => {}
            other => panic!("stalled pairing must close without data, got {other:?}"),
        }

        handle.shutdown().await;
    }

    #[tokio::test]
    async fn host_acceptor_shutdown_joins_accepted_pairing_tasks() {
        let (pairing, _tx, _host_keypair, _password) =
            pairing_fixture(r#"{"kind":"host","bootstrap_token":"tok-1"}"#);
        let (registry, _owner) = registry_with_owner();
        let bounds = HostAcceptorBounds {
            max_connections: 4,
            read_deadline: Duration::from_millis(200),
            pairing_rate: PairingRateLimit::default(),
        };
        let handle = spawn_loopback_acceptor(registry, Some(pairing), bounds).await;

        let stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        let mut reader = tokio::io::BufReader::new(stream);
        reader
            .get_mut()
            .write_all(b"{\"kind\":\"meerkat_pairing_hello\",\"version\":1}\n")
            .await
            .expect("write hello");
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("read challenge");

        tokio::time::timeout(Duration::from_secs(1), handle.shutdown())
            .await
            .expect("shutdown must join the deadline-bounded child task");

        // Successful shutdown is an ownership boundary: no detached pairing
        // task may retain the accepted socket after it returns.
        let mut byte = [0_u8; 1];
        let read = tokio::time::timeout(Duration::from_millis(50), reader.read(&mut byte))
            .await
            .expect("joined child must already have released its socket");
        match read {
            Ok(0) => {}
            Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => {}
            other => panic!("shutdown must leave no live child transport, got {other:?}"),
        }
    }

    // ---- host pairing branch (DEC-P2-3 / DEC-P2-4) ----

    fn pairing_fixture(
        descriptor: &str,
    ) -> (
        HostPairingConfig,
        watch::Sender<String>,
        Arc<Keypair>,
        String,
    ) {
        let password = "a-very-long-host-pairing-secret-0123456789".to_string();
        let host_keypair = Arc::new(Keypair::generate());
        let (tx, rx) = watch::channel(descriptor.to_string());
        (
            HostPairingConfig {
                password: password.clone(),
                host_keypair: host_keypair.clone(),
                participant_name: "mob-host".to_string(),
                descriptor_json: rx,
            },
            tx,
            host_keypair,
            password,
        )
    }

    async fn pairing_handshake(
        addr: SocketAddr,
        password: &str,
    ) -> (serde_json::Value, serde_json::Value) {
        let caller_keypair = Keypair::generate();
        let caller_pubkey = caller_keypair.public_key().to_pubkey_string();
        let caller_address = "tcp://127.0.0.1:9";
        let stream = TcpStream::connect(addr).await.expect("connect");
        let mut reader = tokio::io::BufReader::new(stream);
        reader
            .get_mut()
            .write_all(b"{\"kind\":\"meerkat_pairing_hello\",\"version\":1}\n")
            .await
            .expect("write hello");
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("read challenge");
        let challenge_msg: serde_json::Value = serde_json::from_str(&line).expect("challenge JSON");
        let challenge = challenge_msg["challenge"].as_str().expect("challenge str");
        let proof =
            comms_pairing_password_proof(password, challenge, &caller_pubkey, caller_address);
        let proof_msg = serde_json::json!({
            "kind": "meerkat_pairing_proof",
            "caller": {
                "name": "caller",
                "address": caller_address,
                "identity": {
                    "kind": "ed25519_public_key",
                    "public_key": caller_pubkey,
                }
            },
            "password_proof": proof,
        });
        let mut proof_line = serde_json::to_vec(&proof_msg).expect("proof encode");
        proof_line.push(b'\n');
        reader
            .get_mut()
            .write_all(&proof_line)
            .await
            .expect("write proof");
        line.clear();
        reader.read_line(&mut line).await.expect("read complete");
        let complete: serde_json::Value = serde_json::from_str(&line).expect("complete JSON");
        (challenge_msg, complete)
    }

    /// DEC-P2-3 regression pin: host pairing proves possession of the HOST
    /// secret (the challenge targets the HOST identity), returns the
    /// daemon-supplied descriptor verbatim, and writes NOTHING — the
    /// registered identity's trust view is untouched and the registry is
    /// never mutated. Type-level backstop: `HostPairingConfig` holds no
    /// router/trust-store/persistence handles at all.
    #[tokio::test]
    async fn host_pairing_returns_descriptor_and_installs_no_trust() {
        let descriptor =
            r#"{"kind":"host","address":"tcp://127.0.0.1:4400","bootstrap_token":"tok-1"}"#;
        let (pairing, _tx, host_keypair, password) = pairing_fixture(descriptor);
        let sender_keypair = Keypair::generate();
        let member_a = member_trusting(&sender_keypair.public_key(), true);
        let (registry, owner) = registry_with_owner();
        register_member(&registry, &owner, &member_a);
        let trusted_before = member_a.trusted.read().clone();
        let handle = spawn_loopback_acceptor(
            registry.clone(),
            Some(pairing),
            HostAcceptorBounds::default(),
        )
        .await;

        let (challenge_msg, complete) = pairing_handshake(handle.local_addr(), &password).await;

        // Challenge targets the HOST identity at the advertised address.
        assert_eq!(
            challenge_msg["target"]["identity"]["public_key"],
            serde_json::Value::String(host_keypair.public_key().to_pubkey_string()),
        );
        assert_eq!(
            challenge_msg["target"]["address"],
            serde_json::Value::String(handle.advertised_address().to_string()),
        );

        assert_eq!(complete["kind"], "meerkat_pairing_complete");
        assert_eq!(
            complete["binding"],
            serde_json::from_str::<serde_json::Value>(descriptor).unwrap(),
            "the binding object must be the daemon-supplied descriptor verbatim"
        );
        assert_eq!(complete["comms_name"], "mob-host");

        // Zero trust writes: the member's trust view is unchanged.
        let trusted_after = member_a.trusted.read().clone();
        assert_eq!(
            trusted_before, trusted_after,
            "host pairing must not install trust"
        );
        assert_eq!(
            trusted_after.len(),
            1,
            "only the pre-existing sender entry remains"
        );
        // Registry unchanged: still exactly the one registered identity.
        assert!(registry.resolve(&member_a.keypair.public_key()).is_some());

        handle.shutdown().await;
    }

    /// DEC-P2-4: the descriptor watch slot serves the CURRENT value — after
    /// a re-mint pushes a new serialized descriptor, the next pairing
    /// returns the new one.
    #[tokio::test]
    async fn host_pairing_serves_reminted_descriptor_from_watch() {
        let first = r#"{"kind":"host","bootstrap_token":"tok-1"}"#;
        let second = r#"{"kind":"host","bootstrap_token":"tok-2"}"#;
        let (pairing, tx, _host_keypair, password) = pairing_fixture(first);
        let (registry, _owner) = registry_with_owner();
        let handle =
            spawn_loopback_acceptor(registry, Some(pairing), HostAcceptorBounds::default()).await;

        let (_, complete_one) = pairing_handshake(handle.local_addr(), &password).await;
        assert_eq!(complete_one["binding"]["bootstrap_token"], "tok-1");

        tx.send(second.to_string())
            .expect("descriptor re-mint push");

        let (_, complete_two) = pairing_handshake(handle.local_addr(), &password).await;
        assert_eq!(
            complete_two["binding"]["bootstrap_token"], "tok-2",
            "pairing must serve the re-minted descriptor"
        );

        handle.shutdown().await;
    }

    #[tokio::test]
    async fn host_pairing_wrong_password_is_refused() {
        let (pairing, _tx, _host_keypair, _password) =
            pairing_fixture(r#"{"kind":"host","bootstrap_token":"tok-1"}"#);
        let (registry, _owner) = registry_with_owner();
        let handle =
            spawn_loopback_acceptor(registry, Some(pairing), HostAcceptorBounds::default()).await;

        let caller_keypair = Keypair::generate();
        let caller_pubkey = caller_keypair.public_key().to_pubkey_string();
        let stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        let mut reader = tokio::io::BufReader::new(stream);
        reader
            .get_mut()
            .write_all(b"{\"kind\":\"meerkat_pairing_hello\",\"version\":1}\n")
            .await
            .expect("write hello");
        let mut line = String::new();
        reader.read_line(&mut line).await.expect("read challenge");
        let challenge_msg: serde_json::Value = serde_json::from_str(&line).expect("challenge JSON");
        let challenge = challenge_msg["challenge"].as_str().expect("challenge str");
        let proof = comms_pairing_password_proof(
            "the-wrong-password-entirely-0123456789012345",
            challenge,
            &caller_pubkey,
            "tcp://127.0.0.1:9",
        );
        let proof_msg = serde_json::json!({
            "kind": "meerkat_pairing_proof",
            "caller": {
                "name": "caller",
                "address": "tcp://127.0.0.1:9",
                "identity": { "kind": "ed25519_public_key", "public_key": caller_pubkey },
            },
            "password_proof": proof,
        });
        let mut proof_line = serde_json::to_vec(&proof_msg).expect("proof encode");
        proof_line.push(b'\n');
        reader
            .get_mut()
            .write_all(&proof_line)
            .await
            .expect("write proof");
        line.clear();
        let read = reader.read_line(&mut line).await.expect("read refusal");
        assert_eq!(
            read, 0,
            "wrong password proof must close with no pairing-complete"
        );

        handle.shutdown().await;
    }

    /// Pairing attempts beyond the per-source budget are refused BEFORE the
    /// challenge is issued.
    #[tokio::test]
    async fn host_pairing_rate_limit_refuses_before_challenge() {
        let (pairing, _tx, _host_keypair, _password) =
            pairing_fixture(r#"{"kind":"host","bootstrap_token":"tok-1"}"#);
        let (registry, _owner) = registry_with_owner();
        let bounds = HostAcceptorBounds {
            max_connections: 8,
            read_deadline: Duration::from_secs(10),
            pairing_rate: PairingRateLimit {
                max_attempts: 2,
                window: Duration::from_secs(60),
            },
        };
        let handle = spawn_loopback_acceptor(registry, Some(pairing), bounds).await;

        for attempt in 0..2 {
            let stream = TcpStream::connect(handle.local_addr())
                .await
                .expect("connect");
            let mut reader = tokio::io::BufReader::new(stream);
            reader
                .get_mut()
                .write_all(b"{\"kind\":\"meerkat_pairing_hello\",\"version\":1}\n")
                .await
                .expect("write hello");
            let mut line = String::new();
            let read = reader.read_line(&mut line).await.expect("read challenge");
            assert!(
                read > 0,
                "attempt {attempt} within budget must receive a challenge"
            );
            let challenge_msg: serde_json::Value =
                serde_json::from_str(&line).expect("challenge JSON");
            assert_eq!(challenge_msg["kind"], "meerkat_pairing_challenge");
        }

        // Third attempt from the same source: refused before the challenge.
        let stream = TcpStream::connect(handle.local_addr())
            .await
            .expect("connect");
        let mut reader = tokio::io::BufReader::new(stream);
        reader
            .get_mut()
            .write_all(b"{\"kind\":\"meerkat_pairing_hello\",\"version\":1}\n")
            .await
            .expect("write hello");
        let mut line = String::new();
        let read = tokio::time::timeout(Duration::from_secs(5), reader.read_line(&mut line))
            .await
            .expect("refused attempt must close promptly");
        // A refusal closes the socket without a challenge. The close may
        // surface as EOF or, on platforms that RST a socket closed with
        // unread inbound data (macOS), as ConnectionReset — both are the
        // refused shape; a challenge line is the failure.
        match read {
            Ok(0) => {}
            Ok(n) => {
                panic!("over-budget pairing attempt must be refused, got {n}-byte reply: {line}")
            }
            Err(error) if error.kind() == std::io::ErrorKind::ConnectionReset => {}
            Err(error) => panic!("unexpected read error shape: {error}"),
        }
        assert_eq!(handle.pairing_refused(), 1);

        handle.shutdown().await;
    }

    #[test]
    fn pairing_rate_limiter_zero_budget_refuses_everything() {
        let limiter = PairingRateLimiter::new(PairingRateLimit {
            max_attempts: 0,
            window: Duration::from_secs(60),
        });
        assert!(!limiter.try_admit("127.0.0.1".parse().unwrap()));
    }

    #[test]
    fn pairing_rate_limiter_is_per_source() {
        let limiter = PairingRateLimiter::new(PairingRateLimit {
            max_attempts: 1,
            window: Duration::from_secs(60),
        });
        let a: IpAddr = "127.0.0.1".parse().unwrap();
        let b: IpAddr = "127.0.0.2".parse().unwrap();
        assert!(limiter.try_admit(a));
        assert!(!limiter.try_admit(a), "source A exhausted its budget");
        assert!(limiter.try_admit(b), "source B has an independent budget");
    }
}