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
//! Router for Meerkat comms - high-level send API.

#[cfg(not(target_arch = "wasm32"))]
use futures::{SinkExt, StreamExt};
use parking_lot::RwLock;
#[cfg(all(not(unix), not(target_arch = "wasm32")))]
use std::io::ErrorKind;
use std::sync::Arc;
#[cfg(not(target_arch = "wasm32"))]
use std::time::Duration;
use thiserror::Error;
#[cfg(not(target_arch = "wasm32"))]
use tokio::io::{AsyncRead, AsyncWrite};
#[cfg(not(target_arch = "wasm32"))]
use tokio::net::TcpStream;
#[cfg(unix)]
use tokio::net::UnixStream;
#[cfg(not(target_arch = "wasm32"))]
use tokio_util::codec::Framed;
use uuid::Uuid;

use crate::identity::Keypair;
use crate::inbox::InboxSender;
use crate::inproc::{InprocRegistry, InprocSendError};
#[cfg(not(target_arch = "wasm32"))]
use crate::transport::codec::{EnvelopeFrame, TransportCodec};
use crate::transport::{PeerAddr, TransportError};
use crate::trust::{TrustEntry, TrustError, TrustStore, TrustedPeersView};
use crate::types::{Envelope, MessageKind};
use meerkat_core::comms::{
    GeneratedCommsTrustAuthoritySourceKind, PeerId, SendTaintOverride, SenderContentTaint,
};

type TrustSourceSet = std::collections::BTreeSet<GeneratedCommsTrustAuthoritySourceKind>;
type PeerIdSet = std::collections::BTreeSet<PeerId>;
type TrustSourceMap = std::collections::BTreeMap<PeerId, TrustSourceSet>;
type TrustDescriptorMap = std::collections::BTreeMap<
    PeerId,
    std::collections::BTreeMap<GeneratedCommsTrustAuthoritySourceKind, TrustEntry>,
>;

/// Derive the canonical [`PeerId`] for a signing [`crate::identity::PubKey`].
///
/// Thin wrapper over [`crate::identity::PubKey::to_peer_id`] — kept for
/// callers that prefer the free-function form. The UUIDv5 namespace lives
/// with [`crate::identity::PubKey`] so it travels with the type being
/// hashed.
pub fn peer_id_from_pubkey(pubkey: &crate::identity::PubKey) -> PeerId {
    pubkey.to_peer_id()
}

pub const DEFAULT_ACK_TIMEOUT_SECS: u64 = 30;
pub const DEFAULT_MAX_MESSAGE_BYTES: u32 = crate::transport::MAX_PAYLOAD_SIZE;
#[cfg(not(target_arch = "wasm32"))]
const DECLARED_REPLY_OPERATION_TIMEOUT: Duration = Duration::from_secs(2);

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct CommsConfig {
    pub ack_timeout_secs: u64,
    pub max_message_bytes: u32,
}

impl Default for CommsConfig {
    fn default() -> Self {
        Self {
            ack_timeout_secs: DEFAULT_ACK_TIMEOUT_SECS,
            max_message_bytes: DEFAULT_MAX_MESSAGE_BYTES,
        }
    }
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum SendError {
    /// The destination [`PeerId`] does not resolve to any trusted-peer
    /// entry. Carries the typed [`PeerId`] so callers can distinguish it
    /// from a display-name mismatch.
    #[error("Peer not found: {0}")]
    PeerNotFound(PeerId),
    #[error("Peer offline (no ack received)")]
    PeerOffline,
    #[error("Transport error: {0}")]
    Transport(#[from] TransportError),
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
    /// The peer admitted the transport layer but rejected our envelope at
    /// its ingress admission gate. Semantically distinct from `PeerOffline`
    /// — transport worked, policy refused. Carries the typed `DropReason`
    /// so callers can render e.g. `untrusted_sender` rather than masquerade
    /// as "peer unreachable".
    #[error("Peer dropped envelope at admission: {reason:?}")]
    AdmissionDropped { reason: crate::inbox::DropReason },
}

/// Outcome of a successful router send.
///
/// Carries the strongest delivery fact the transport can truthfully prove:
/// a verified peer ACK on stream transports, direct receiver-inbox admission
/// on the wait-based inproc lane, or only a queued transport write.
#[derive(Debug, Clone, Copy)]
pub struct SendOutcome {
    pub envelope_id: Uuid,
    pub delivery: meerkat_core::comms::PeerDeliveryOutcome,
}

/// A one-shot callback endpoint for responses to peers outside the trust
/// store.
///
/// A supervisor-bridge responder may need to deliver a typed `Rejected` reply
/// to an authenticated sender it has never trusted. When that request carries
/// an admissible callback capability, the responder stages it bound to the
/// AUTHENTICATED ingress signer key:
///
/// * `pubkey` MUST be the verified envelope signer from the ingress fact,
///   never a payload-claimed identity — the reply is addressed to the key
///   that proved itself, so a spoofed payload identity cannot redirect the
///   response to a third party's key.
/// * On the correlated signed-request path, `address` is reconstructed from
///   the kernel-observed TCP source IP plus the signed declared port. The
///   sender-selected host is never retained. Legacy uncorrelated callers may
///   use only machine-authorized stored endpoint truth, never the current
///   untrusted request payload.
///
/// Entries are consumed on first use and ONLY for [`MessageKind::Response`]
/// envelopes — this is a reply channel, not a general send capability. The
/// legacy uncorrelated path is consulted only when trust resolution misses;
/// an exact correlated route may override a stale trusted address for its one
/// matching response.
#[derive(Debug, Clone)]
pub struct DeclaredReplyEndpoint {
    /// Authenticated signer key of the request being answered.
    pub pubkey: crate::identity::PubKey,
    /// Callback-only reply address, e.g. `tcp://observed-source:port`.
    pub address: String,
}

#[cfg(not(target_arch = "wasm32"))]
fn declared_reply_timeout_error(address: &str) -> std::io::Error {
    std::io::Error::new(
        std::io::ErrorKind::TimedOut,
        format!(
            "declared reply endpoint operation timed out after {}s: {address}",
            DECLARED_REPLY_OPERATION_TIMEOUT.as_secs()
        ),
    )
}

#[cfg(not(target_arch = "wasm32"))]
async fn run_declared_reply_with_timeout<T, F>(address: &str, operation: F) -> Result<T, SendError>
where
    F: std::future::Future<Output = Result<T, SendError>>,
{
    tokio::time::timeout(DECLARED_REPLY_OPERATION_TIMEOUT, operation)
        .await
        .map_err(|_| SendError::Io(declared_reply_timeout_error(address)))?
}

/// Apply the callback-only deadline at the socket-routing choke point.
/// Durable trusted routes deliberately retain their transport's normal
/// lifetime; a sender-declared callback can never impose an unbounded connect,
/// write, or ACK wait on the responder.
#[cfg(not(target_arch = "wasm32"))]
async fn run_socket_route_operation<T, F>(
    address: &str,
    is_declared_reply: bool,
    operation: F,
) -> Result<T, SendError>
where
    F: std::future::Future<Output = Result<T, SendError>>,
{
    if is_declared_reply {
        run_declared_reply_with_timeout(address, operation).await
    } else {
        operation.await
    }
}

#[inline]
fn map_inproc_send_error(err: InprocSendError, dest: PeerId) -> SendError {
    match err {
        InprocSendError::PeerNotFound(_) => SendError::PeerNotFound(dest),
        InprocSendError::InboxClosed | InprocSendError::InboxFull => SendError::PeerOffline,
        // Preserve the typed ingress-drop reason all the way through to
        // REST/RPC/MCP payloads. Do NOT collapse into `PeerOffline` — the
        // transport worked, the receiver's admission policy rejected us.
        InprocSendError::IngressDropped(reason) => SendError::AdmissionDropped { reason },
    }
}

#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
pub struct Router {
    keypair: Arc<Keypair>,
    /// Single source of truth for trusted peers, keyed by canonical
    /// [`PeerId`]. Shared internally by the Router, CommsRuntime, and
    /// IngressClassificationContext. Uses `parking_lot::RwLock` so ingress
    /// classification can read synchronously, but the mutable lock is not
    /// exposed outside this crate.
    trusted_peers: Arc<RwLock<TrustStore>>,
    /// Mechanical projection of which generated source currently owns each
    /// trust row. Admission uses the union; generated removals are scoped to
    /// the source that authorized them.
    trusted_peer_sources: Arc<RwLock<TrustSourceMap>>,
    /// Source-owned descriptors for generated trust rows. The public
    /// `trusted_peers` set remains the send/admission union, but source-scoped
    /// snapshots read this map so one generated owner cannot rewrite another
    /// owner's descriptor projection.
    trusted_peer_descriptors_by_source: Arc<RwLock<TrustDescriptorMap>>,
    /// Source-attributed directory visibility for private (control-plane)
    /// trust edges. A peer is private iff some generated source authorized it
    /// privately. This is the single visibility authority: `is_private`,
    /// `is_private_peer_id`, and directory resolution derive from it, so
    /// rebuilding the Router from the generated trust-authority snapshot
    /// reproduces identical `comms.peers` visibility with no separate flat
    /// side-map to repopulate. Private peers are still admitted AND
    /// send-resolvable, but `resolve_peer_directory()` filters them out of the
    /// `comms.peers` REST/RPC/MCP surface (e.g. the supervisor bridge in
    /// session-backed mob members).
    private_peer_sources: Arc<RwLock<TrustSourceMap>>,
    /// Host-owned outbound content-taint declaration. Stamped onto every
    /// content-bearing envelope at the single outbound choke point
    /// ([`Router::send_with_id`]) unless a per-send override says otherwise.
    /// Host-set config, not machine state: installed via
    /// [`Router::set_outbound_content_taint`].
    outbound_content_taint: RwLock<Option<SenderContentTaint>>,
    /// Legacy uncorrelated one-shot reply endpoints for responses to peers the
    /// trust store does not know (see [`DeclaredReplyEndpoint`]). Only
    /// machine-authorized stored endpoint truth may enter this map. It grants
    /// no admission or general sendability and is consumed by the first
    /// [`MessageKind::Response`] send to that peer id.
    staged_reply_endpoints: RwLock<std::collections::BTreeMap<PeerId, DeclaredReplyEndpoint>>,
    /// Authenticated one-shot reply endpoints keyed by the exact destination
    /// identity and request correlation id. Unlike the legacy uncorrelated
    /// staging map above, an exact correlated endpoint overrides a durable
    /// trusted route for that one Response only. This lets a peer rotate its
    /// socket endpoint without granting a general routing capability or
    /// misrouting concurrent responses for the same identity.
    staged_correlated_reply_endpoints:
        RwLock<std::collections::BTreeMap<(PeerId, Uuid), DeclaredReplyEndpoint>>,
    config: CommsConfig,
    require_peer_auth: bool,
    inbox_sender: InboxSender,
    inproc_namespace: Option<String>,
}

impl Router {
    /// Construct a router with an empty live trust projection.
    ///
    /// Live trust rows are installed only by generated machine/composition
    /// mutation authority.
    pub fn new(
        keypair: Keypair,
        config: CommsConfig,
        inbox_sender: InboxSender,
        require_peer_auth: bool,
    ) -> Self {
        Self::with_shared_peers(
            keypair,
            Arc::new(RwLock::new(TrustStore::new())),
            config,
            inbox_sender,
            require_peer_auth,
        )
    }

    pub(crate) fn with_shared_peers(
        keypair: Keypair,
        trusted_peers: Arc<RwLock<TrustStore>>,
        config: CommsConfig,
        inbox_sender: InboxSender,
        require_peer_auth: bool,
    ) -> Self {
        *trusted_peers.write() = TrustStore::new();
        Self {
            keypair: Arc::new(keypair),
            trusted_peers,
            trusted_peer_sources: Arc::new(RwLock::new(std::collections::BTreeMap::new())),
            trusted_peer_descriptors_by_source: Arc::new(RwLock::new(
                std::collections::BTreeMap::new(),
            )),
            private_peer_sources: Arc::new(RwLock::new(std::collections::BTreeMap::new())),
            outbound_content_taint: RwLock::new(None),
            staged_reply_endpoints: RwLock::new(std::collections::BTreeMap::new()),
            staged_correlated_reply_endpoints: RwLock::new(std::collections::BTreeMap::new()),
            config,
            require_peer_auth,
            inbox_sender,
            inproc_namespace: None,
        }
    }

    /// Install (or clear) the host-owned outbound content-taint declaration.
    ///
    /// `Some(taint)` stamps every subsequent content-bearing send with the
    /// declaration; `None` sends no declaration. Per-send overrides on the
    /// comms command surface take precedence via [`SendTaintOverride`].
    pub fn set_outbound_content_taint(&self, taint: Option<SenderContentTaint>) {
        *self.outbound_content_taint.write() = taint;
    }

    /// The currently-installed host-owned outbound content-taint declaration.
    pub fn outbound_content_taint(&self) -> Option<SenderContentTaint> {
        *self.outbound_content_taint.read()
    }

    /// Returns `true` if the peer is currently marked private.
    pub fn is_private(&self, pubkey: &crate::identity::PubKey) -> bool {
        self.is_private_peer_id(&peer_id_from_pubkey(pubkey))
    }

    /// Snapshot of the peer IDs that are currently directory-private, derived
    /// from the source-attributed `private_peer_sources` authority (a peer is
    /// private iff some generated source authorized it privately).
    pub(crate) fn private_peer_ids(&self) -> PeerIdSet {
        self.private_peer_sources
            .read()
            .iter()
            .filter(|(_, sources)| !sources.is_empty())
            .map(|(peer_id, _)| *peer_id)
            .collect()
    }

    pub(crate) fn has_trust_source(
        &self,
        peer_id: &PeerId,
        source_kind: GeneratedCommsTrustAuthoritySourceKind,
    ) -> bool {
        self.trusted_peer_sources
            .read()
            .get(peer_id)
            .is_some_and(|sources| sources.contains(&source_kind))
    }

    pub(crate) fn trusted_peers_for_source(
        &self,
        source_kind: GeneratedCommsTrustAuthoritySourceKind,
    ) -> Vec<(PeerId, TrustEntry)> {
        self.trusted_peer_descriptors_by_source
            .read()
            .iter()
            .filter_map(|(peer_id, descriptors)| {
                descriptors
                    .get(&source_kind)
                    .cloned()
                    .map(|peer| (*peer_id, peer))
            })
            .collect()
    }

    /// Scope in-process routing to a namespace.
    pub fn with_inproc_namespace(mut self, namespace: Option<String>) -> Self {
        self.inproc_namespace = namespace;
        self
    }

    pub fn keypair_arc(&self) -> Arc<Keypair> {
        self.keypair.clone()
    }

    pub fn trusted_peers_view(&self) -> TrustedPeersView {
        TrustedPeersView::new(self.trusted_peers.clone())
    }

    pub fn trusted_peers_snapshot(&self) -> TrustStore {
        self.trusted_peers.read().clone()
    }
    pub fn inbox_sender(&self) -> &InboxSender {
        &self.inbox_sender
    }

    pub fn has_peers(&self) -> bool {
        self.trusted_peers.read().has_peers()
    }

    /// Apply a generated trust-projection add to the router's mechanical
    /// trust store. Semantic trust authority lives at the machine seam that
    /// called into `CommsRuntime::apply_trust_mutation`.
    pub(crate) fn add_trusted_peer_for_source(
        &self,
        entry: TrustEntry,
        source_kind: GeneratedCommsTrustAuthoritySourceKind,
        private: bool,
    ) -> Result<bool, TrustError> {
        let peer_id = entry.peer_id;
        let mut trusted_peer_sources = self.trusted_peer_sources.write();
        let mut descriptors_by_source = self.trusted_peer_descriptors_by_source.write();
        let mut private_peer_sources = self.private_peer_sources.write();

        if let Some(existing) = descriptors_by_source
            .get(&peer_id)
            .and_then(|descriptors| descriptors.get(&source_kind))
        {
            let source_private = private_peer_sources
                .get(&peer_id)
                .is_some_and(|sources| sources.contains(&source_kind));
            // Trust material is the routing identity (pubkey + address);
            // `name` and discovery `meta` are display-only (a peer-only
            // member's name is derived heuristically per projection source —
            // an inproc address embeds the comms name, a non-inproc address
            // falls back to a synthetic backend-peer label — so it can differ
            // for one peer). Only a divergent pubkey or address is a genuine
            // re-add of different routing material.
            let same_routing_material =
                existing.pubkey == entry.pubkey && existing.address == entry.address;
            if same_routing_material && source_private == private {
                return Ok(false);
            }
            return Err(TrustError::ConflictingGeneratedTrustSource {
                peer_id,
                source_kind,
            });
        }
        if trusted_peer_sources
            .get(&peer_id)
            .is_some_and(|sources| sources.contains(&source_kind))
        {
            return Err(TrustError::ConflictingGeneratedTrustSource {
                peer_id,
                source_kind,
            });
        }

        self.trusted_peers.write().upsert(entry.clone())?;
        descriptors_by_source
            .entry(peer_id)
            .or_default()
            .insert(source_kind, entry);
        let created = trusted_peer_sources
            .entry(peer_id)
            .or_default()
            .insert(source_kind);
        if private {
            private_peer_sources
                .entry(peer_id)
                .or_default()
                .insert(source_kind);
        }
        Ok(created)
    }

    /// Apply a generated trust-projection removal to the router's mechanical
    /// trust store. Semantic trust authority lives at the machine seam that
    /// called into `CommsRuntime::apply_trust_mutation`.
    pub(crate) fn remove_trusted_peer_for_source(
        &self,
        peer_id: &PeerId,
        source_kind: GeneratedCommsTrustAuthoritySourceKind,
    ) -> bool {
        let source_removed = {
            let mut sources = self.trusted_peer_sources.write();
            let Some(peer_sources) = sources.get_mut(peer_id) else {
                return false;
            };
            if !peer_sources.remove(&source_kind) {
                return false;
            }
            if peer_sources.is_empty() {
                sources.remove(peer_id);
            }
            true
        };
        if !source_removed {
            return false;
        }
        let remaining_descriptor = {
            let mut descriptors_by_source = self.trusted_peer_descriptors_by_source.write();
            let Some(descriptors) = descriptors_by_source.get_mut(peer_id) else {
                return false;
            };
            descriptors.remove(&source_kind);
            let remaining = descriptors.values().next().cloned();
            if descriptors.is_empty() {
                descriptors_by_source.remove(peer_id);
            }
            remaining
        };
        {
            let mut private_sources = self.private_peer_sources.write();
            if let Some(peer_sources) = private_sources.get_mut(peer_id) {
                peer_sources.remove(&source_kind);
                if peer_sources.is_empty() {
                    private_sources.remove(peer_id);
                }
            }
        }
        if let Some(peer) = remaining_descriptor {
            // Another generated source still owns this trust row; the union
            // projection becomes that source's descriptor.
            return self.trusted_peers.write().upsert(peer).is_ok();
        }
        if self.trusted_peer_sources.read().contains_key(peer_id) {
            return true;
        }
        if self.trusted_peers.write().remove(peer_id).is_none() {
            return false;
        }
        self.private_peer_sources.write().remove(peer_id);
        true
    }

    /// Directory-private iff some generated source authorized the peer
    /// privately. Derived from the source-attributed `private_peer_sources`
    /// authority — there is no separate flat side-map to consult.
    pub(crate) fn is_private_peer_id(&self, peer_id: &PeerId) -> bool {
        self.private_peer_sources
            .read()
            .get(peer_id)
            .is_some_and(|sources| !sources.is_empty())
    }

    fn trusted_peer_by_peer_id(&self, peer_id: &PeerId) -> Option<TrustEntry> {
        self.trusted_peers.read().get(peer_id).cloned()
    }

    /// Stage a one-shot [`DeclaredReplyEndpoint`] for `dest`.
    ///
    /// Restaging the same peer id replaces the prior machine-authorized repair
    /// endpoint. Raw Request metadata and decoded payload/sender addresses
    /// must never call this legacy uncorrelated seam. Trust-store entries
    /// always win at send time; a staged entry for a peer that is (or later
    /// becomes) trusted is simply never consulted.
    pub fn stage_reply_endpoint(&self, dest: PeerId, endpoint: DeclaredReplyEndpoint) {
        self.staged_reply_endpoints.write().insert(dest, endpoint);
    }

    fn take_staged_reply_endpoint(&self, dest: &PeerId) -> Option<DeclaredReplyEndpoint> {
        self.staged_reply_endpoints.write().remove(dest)
    }

    /// Stage a one-shot endpoint for the exact `(dest, in_reply_to)` Response.
    ///
    /// Restaging the same correlation replaces only that entry. Other
    /// correlations for the same peer remain independent, which is required
    /// when requests overlap during a socket rotation.
    pub(crate) fn stage_correlated_reply_endpoint(
        &self,
        dest: PeerId,
        in_reply_to: Uuid,
        endpoint: DeclaredReplyEndpoint,
    ) {
        self.staged_correlated_reply_endpoints
            .write()
            .insert((dest, in_reply_to), endpoint);
    }

    fn take_staged_correlated_reply_endpoint(
        &self,
        dest: &PeerId,
        in_reply_to: Uuid,
    ) -> Option<DeclaredReplyEndpoint> {
        self.staged_correlated_reply_endpoints
            .write()
            .remove(&(*dest, in_reply_to))
    }

    /// Idempotently remove one exact correlated endpoint without sending.
    /// Returns whether an entry existed so focused callers/tests can observe
    /// cleanup while the cross-crate capability keeps idempotent `Ok(())`
    /// semantics.
    pub(crate) fn unstage_correlated_reply_endpoint(
        &self,
        dest: PeerId,
        in_reply_to: Uuid,
    ) -> bool {
        self.staged_correlated_reply_endpoints
            .write()
            .remove(&(dest, in_reply_to))
            .is_some()
    }

    /// Remove every staged callback for a terminal interaction correlation.
    ///
    /// The machine cleanup effect carries the correlation but not the remote
    /// destination, so cleanup must match the second key component across
    /// peers. The map is bounded by active inbound requests and this path runs
    /// only at interaction termination.
    pub(crate) fn unstage_correlated_reply_endpoints_for_interaction(
        &self,
        in_reply_to: Uuid,
    ) -> usize {
        let mut endpoints = self.staged_correlated_reply_endpoints.write();
        let before = endpoints.len();
        endpoints.retain(|(_, correlation), _| *correlation != in_reply_to);
        before - endpoints.len()
    }

    #[cfg(not(target_arch = "wasm32"))]
    async fn send_on_stream<S>(
        &self,
        stream: &mut S,
        envelope: Envelope,
        wait_for_ack: bool,
    ) -> Result<SendOutcome, SendError>
    where
        S: AsyncRead + AsyncWrite + Unpin,
    {
        let sent_id = envelope.id;
        let sent_to = envelope.to;

        let mut framed = Framed::new(stream, TransportCodec::new(self.config.max_message_bytes));
        framed.send(EnvelopeFrame::from_envelope(envelope)).await?;
        if wait_for_ack {
            match tokio::time::timeout(
                Duration::from_secs(self.config.ack_timeout_secs),
                framed.next(),
            )
            .await
            {
                Ok(Some(Ok(frame))) => {
                    // Validate ACK: signature, sender, recipient, and in_reply_to
                    if let MessageKind::Ack { in_reply_to } = frame.envelope.kind {
                        if self.require_peer_auth {
                            if !frame.envelope.verify() {
                                return Err(SendError::PeerOffline);
                            }
                            if frame.envelope.from != sent_to {
                                return Err(SendError::PeerOffline);
                            }
                            // Verify ACK is addressed to us (prevents misrouted/injected ACKs)
                            if frame.envelope.to != self.keypair.public_key() {
                                return Err(SendError::PeerOffline);
                            }
                        } else if frame.envelope.to != self.keypair.public_key() {
                            return Err(SendError::PeerOffline);
                        }
                        if in_reply_to != sent_id {
                            return Err(SendError::PeerOffline);
                        }
                        // A peer ACK was received and verified: this is the only
                        // path that may report `PeerDeliveryOutcome::Acked`.
                        Ok(SendOutcome {
                            envelope_id: sent_id,
                            delivery: meerkat_core::comms::PeerDeliveryOutcome::Acked,
                        })
                    } else {
                        Err(SendError::PeerOffline)
                    }
                }
                _ => Err(SendError::PeerOffline),
            }
        } else {
            // No ACK was awaited (Ack/Response kinds), so we cannot claim one
            // was received.
            Ok(SendOutcome {
                envelope_id: sent_id,
                delivery: meerkat_core::comms::PeerDeliveryOutcome::Queued,
            })
        }
    }

    /// Deliver an inproc envelope, honoring namespace isolation when it has
    /// been opted into.
    ///
    /// `CoreCommsConfig.inproc_namespace` is the namespace-isolation OPT-IN.
    /// When it is configured (`Some(ns)`) the namespace is the delivery
    /// authority: the destination is resolved exactly once, *inside* that
    /// namespace, and that resolution is the delivery target — or we fail
    /// closed with [`SendError::PeerNotFound`] rather than crossing namespaces
    /// (#242 — no cross-namespace delivery). Resolution is not re-derived from
    /// the global registry after the namespace check (a second any-namespace
    /// lookup would open a re-registration window where delivery crosses the
    /// boundary the check just enforced). When it is NOT configured (`None`)
    /// no isolation was promised, so none is imposed — the legacy
    /// any-namespace resolution stands, failing closed on ambiguity. Coercing
    /// an absent namespace to a synthetic `""` namespace here would wrongly
    /// partition default-namespace peers that legitimately share a process
    /// (e.g. an in-process mob member and its runtime bridge), so the typed
    /// `None` is respected as "unconstrained", not as a distinct namespace.
    async fn send_inproc_in_namespace(
        &self,
        peer: &TrustEntry,
        envelope: Envelope,
        dest: PeerId,
    ) -> Result<SendOutcome, SendError> {
        let registry = InprocRegistry::global();
        let envelope_id = match self.inproc_namespace.as_deref() {
            Some(namespace) => registry
                .send_to_pubkey_in_namespace_with_id_wait(
                    namespace,
                    &self.keypair,
                    &peer.pubkey,
                    envelope.id,
                    envelope.kind,
                    self.require_peer_auth,
                )
                .await
                .map_err(|err| map_inproc_send_error(err, dest))?,
            None => registry
                .send_to_pubkey_any_namespace_with_id_wait(
                    &self.keypair,
                    &peer.pubkey,
                    envelope.id,
                    envelope.kind,
                    self.require_peer_auth,
                )
                .await
                .map_err(|err| map_inproc_send_error(err, dest))?,
        };
        // The wait-based inproc handoff returns only after the receiver's
        // trust-gated inbox admitted the envelope (every drop maps to a typed
        // error above). This proves direct handoff, not a cryptographic peer
        // ACK, so retain that distinction in the typed delivery outcome.
        Ok(SendOutcome {
            envelope_id,
            delivery: meerkat_core::comms::PeerDeliveryOutcome::HandedOff,
        })
    }

    /// Canonical send: routing identity is a [`PeerId`], never a [`PeerName`].
    ///
    /// Wave-B V5: `PeerName` is display metadata; it is not safe as a routing
    /// key (duplicate names are legal). Callers that hold only a name must
    /// resolve it to a `PeerId` via [`crate::trust::TrustStore::resolve_name`]
    /// at the boundary and handle the typed
    /// [`TrustResolveError::Ambiguous`](crate::trust::TrustResolveError::Ambiguous)
    /// case explicitly — the router will not guess.
    pub async fn send(&self, dest: PeerId, kind: MessageKind) -> Result<SendOutcome, SendError> {
        self.send_with_id(dest, Uuid::new_v4(), kind, None).await
    }

    /// Canonical send with a caller-supplied envelope id.
    ///
    /// Used by the correlated request/response path, which reserves a
    /// stream key before the envelope goes out so replies can correlate
    /// via `in_reply_to` without an extra local id map.
    ///
    /// `content_taint` is the per-send tri-state taint override: `None`
    /// inherits the runtime-level declaration installed via
    /// [`Router::set_outbound_content_taint`]; `Undeclared` strips the
    /// declaration; `Declare(taint)` stamps exactly `taint`. The resolved
    /// declaration is stamped here — the single outbound choke point — onto
    /// content-bearing kinds, INSIDE the signed region, before signing.
    ///
    /// The returned [`SendOutcome`] carries the strongest typed delivery fact
    /// proved by the selected transport; only a verified stream ACK yields
    /// [`meerkat_core::comms::PeerDeliveryOutcome::Acked`].
    pub async fn send_with_id(
        &self,
        dest: PeerId,
        envelope_id: Uuid,
        kind: MessageKind,
        content_taint: Option<SendTaintOverride>,
    ) -> Result<SendOutcome, SendError> {
        // An exact correlated endpoint is the authority for precisely one
        // Response and therefore takes precedence over a potentially stale
        // durable route. For every other send, durable trust remains the
        // authority; the legacy uncorrelated endpoint is consulted only when
        // trust misses, preserving its existing trust-store-wins contract.
        enum ResolvedDest {
            Trusted(TrustEntry),
            Declared(DeclaredReplyEndpoint),
        }
        let correlated = match &kind {
            MessageKind::Response { in_reply_to, .. } => {
                self.take_staged_correlated_reply_endpoint(&dest, *in_reply_to)
            }
            _ => None,
        };
        let dest_entry = match correlated {
            Some(endpoint) => ResolvedDest::Declared(endpoint),
            None => match self.trusted_peer_by_peer_id(&dest) {
                Some(peer) => ResolvedDest::Trusted(peer),
                None => {
                    let staged = if matches!(kind, MessageKind::Response { .. }) {
                        self.take_staged_reply_endpoint(&dest)
                    } else {
                        None
                    };
                    match staged {
                        Some(endpoint) => ResolvedDest::Declared(endpoint),
                        None => return Err(SendError::PeerNotFound(dest)),
                    }
                }
            },
        };
        let (addr, to_pubkey) = match &dest_entry {
            ResolvedDest::Trusted(peer) => {
                (PeerAddr::parse(&peer.address.to_string())?, peer.pubkey)
            }
            ResolvedDest::Declared(endpoint) => {
                (PeerAddr::parse(&endpoint.address)?, endpoint.pubkey)
            }
        };
        let resolved_taint = match content_taint {
            // Absent override = inherit. A declaration the caller already
            // constructed INTO the kind wins over the runtime-level
            // declaration — the stamping choke point must never silently
            // degrade an explicit Some(..) to the unset runtime default.
            None => kind
                .content_taint()
                .or_else(|| self.outbound_content_taint()),
            Some(SendTaintOverride::Declare(taint)) => Some(taint),
            Some(SendTaintOverride::Undeclared) => None,
        };
        let kind = kind.with_content_taint(resolved_taint);
        let mut envelope = Envelope {
            id: envelope_id,
            from: self.keypair.public_key(),
            to: to_pubkey,
            kind,
            sig: crate::identity::Signature::new([0u8; 64]),
        };
        if self.require_peer_auth {
            envelope.sign(&self.keypair);
        }

        #[cfg(not(target_arch = "wasm32"))]
        {
            let wait_for_ack = should_wait_for_ack(&envelope.kind);
            let is_declared_reply = matches!(&dest_entry, ResolvedDest::Declared(_));
            match addr {
                #[cfg(unix)]
                PeerAddr::Uds(path) => {
                    let display = path.display().to_string();
                    run_socket_route_operation(&display, is_declared_reply, async {
                        let mut stream = UnixStream::connect(&path).await?;
                        self.send_on_stream(&mut stream, envelope, wait_for_ack)
                            .await
                    })
                    .await
                }
                #[cfg(not(unix))]
                PeerAddr::Uds(_path) => Err(std::io::Error::new(
                    ErrorKind::Unsupported,
                    "unix domain sockets are not supported on this platform",
                )
                .into()),
                PeerAddr::Tcp(addr_str) => {
                    run_socket_route_operation(&addr_str, is_declared_reply, async {
                        let mut stream = TcpStream::connect(&addr_str).await?;
                        self.send_on_stream(&mut stream, envelope, wait_for_ack)
                            .await
                    })
                    .await
                }
                PeerAddr::Inproc(_) => match &dest_entry {
                    ResolvedDest::Trusted(peer) => {
                        self.send_inproc_in_namespace(peer, envelope, dest).await
                    }
                    ResolvedDest::Declared(_) => {
                        Err(SendError::Transport(TransportError::InvalidAddress(
                            "declared reply endpoints must be socket addresses (tcp:// or uds://)"
                                .to_string(),
                        )))
                    }
                },
            }
        }

        #[cfg(target_arch = "wasm32")]
        {
            match addr {
                PeerAddr::Tcp(_) => Err(SendError::Transport(TransportError::InvalidAddress(
                    "TCP transport is not available on wasm32".to_string(),
                ))),
                PeerAddr::Inproc(_) => match &dest_entry {
                    ResolvedDest::Trusted(peer) => {
                        self.send_inproc_in_namespace(peer, envelope, dest).await
                    }
                    ResolvedDest::Declared(_) => {
                        Err(SendError::Transport(TransportError::InvalidAddress(
                            "declared reply endpoints must be socket addresses (tcp:// or uds://)"
                                .to_string(),
                        )))
                    }
                },
            }
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
fn should_wait_for_ack(kind: &MessageKind) -> bool {
    !matches!(kind, MessageKind::Ack { .. } | MessageKind::Response { .. })
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::identity::PubKey;
    use crate::inbox::Inbox;
    use crate::peer_meta::PeerMeta;

    fn test_router() -> Router {
        let (_inbox, inbox_sender) = Inbox::new();
        Router::new(
            Keypair::generate(),
            CommsConfig::default(),
            inbox_sender,
            true,
        )
    }

    fn peer(name: &str, pubkey: [u8; 32], addr: &str) -> TrustEntry {
        let pubkey = PubKey::new(pubkey);
        TrustEntry {
            peer_id: pubkey.to_peer_id(),
            name: meerkat_core::comms::PeerName::new(name).expect("valid peer name"),
            pubkey,
            address: meerkat_core::comms::PeerAddress::parse(addr).expect("valid peer address"),
            meta: PeerMeta::default(),
        }
    }

    /// ROW #268 gate: the outcome reflects verified delivery-path authority.
    /// A stream send that receives and verifies a peer ACK reports
    /// [`PeerDeliveryOutcome::Acked`]; sends without receiver acknowledgement
    /// report the weaker typed `Queued` or `HandedOff` outcome.
    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn send_outcome_acked_reflects_verified_ack() {
        use crate::transport::codec::{EnvelopeFrame, TransportCodec};
        use crate::types::MessageKind;
        use futures::{SinkExt, StreamExt};
        use tokio_util::codec::Framed;

        let router_kp = Keypair::generate();
        let router_pubkey = router_kp.public_key();
        let (_inbox, inbox_sender) = Inbox::new();
        let router = Router::new(router_kp, CommsConfig::default(), inbox_sender, true);

        let peer_kp = Keypair::generate();
        let peer_pubkey = peer_kp.public_key();

        // Outgoing Message envelope addressed to the peer, signed by the router.
        let mut envelope = Envelope {
            id: Uuid::new_v4(),
            from: router_pubkey,
            to: peer_pubkey,
            kind: MessageKind::Message {
                objective_id: None,
                body: "needs ack".to_string(),
                blocks: None,
                content_taint: None,
                handling_mode: None,
            },
            sig: crate::identity::Signature::new([0u8; 64]),
        };
        envelope.sign(&router.keypair);
        let sent_id = envelope.id;

        let (mut client, server) = tokio::io::duplex(4096);

        // Peer side: read the framed message, then reply with a verified ACK.
        let peer_task = tokio::spawn(async move {
            let mut framed = Framed::new(server, TransportCodec::new(DEFAULT_MAX_MESSAGE_BYTES));
            let incoming = framed
                .next()
                .await
                .expect("peer receives a frame")
                .expect("frame decodes");
            let mut ack = Envelope {
                id: Uuid::new_v4(),
                from: peer_pubkey,
                to: router_pubkey,
                kind: MessageKind::Ack {
                    in_reply_to: incoming.envelope.id,
                },
                sig: crate::identity::Signature::new([0u8; 64]),
            };
            ack.sign(&peer_kp);
            framed
                .send(EnvelopeFrame::from_envelope(ack))
                .await
                .expect("peer sends ack");
        });

        let outcome = router
            .send_on_stream(&mut client, envelope, true)
            .await
            .expect("send_on_stream succeeds with a verified ack");
        peer_task.await.expect("peer task completes");

        assert_eq!(outcome.envelope_id, sent_id);
        assert_eq!(
            outcome.delivery,
            meerkat_core::comms::PeerDeliveryOutcome::Acked
        );

        // A kind that does not await an ACK (Response) proves only a write.
        let (mut client2, _server2) = tokio::io::duplex(4096);
        let mut response_env = Envelope {
            id: Uuid::new_v4(),
            from: router_pubkey,
            to: peer_pubkey,
            kind: MessageKind::Response {
                objective_id: None,
                in_reply_to: Uuid::new_v4(),
                status: crate::types::Status::Completed,
                result: serde_json::json!({"ok": true}),
                blocks: None,
                content_taint: None,
                handling_mode: None,
            },
            sig: crate::identity::Signature::new([0u8; 64]),
        };
        response_env.sign(&router.keypair);
        let no_ack = router
            .send_on_stream(&mut client2, response_env, false)
            .await
            .expect("non-ack-bearing send succeeds");
        assert_eq!(
            no_ack.delivery,
            meerkat_core::comms::PeerDeliveryOutcome::Queued
        );
    }

    /// ROW #242 gate: a trusted inproc send from a runtime in namespace A to a
    /// peer registered ONLY in namespace B must NOT be delivered — the
    /// configured `inproc_namespace` is the delivery authority, not a
    /// cross-namespace pubkey lookup.
    #[tokio::test]
    async fn inproc_send_does_not_cross_namespace() {
        use crate::classify::test_support;

        // Use the process-global registry, but isolate by unique namespaces and
        // a fresh keypair rather than `clear()` so we never disturb peers other
        // tests registered concurrently (the registry is process-global).
        let registry = InprocRegistry::global();
        let suffix = Uuid::new_v4().simple().to_string();
        let namespace_a = format!("realm-a-{suffix}");
        let namespace_b = format!("realm-b-{suffix}");

        // Receiver lives only in namespace B.
        let receiver_kp = Keypair::generate();
        let receiver_pubkey = receiver_kp.public_key();
        let receiver_pubkey_bytes = *receiver_pubkey.as_bytes();
        let (mut receiver_inbox, receiver_sender) = Inbox::new_classified(
            test_support::classification_context(TrustStore::new(), false),
        );
        registry.register_with_meta_in_namespace(
            &namespace_b,
            "receiver",
            receiver_pubkey,
            receiver_sender,
            PeerMeta::default(),
        );

        // Router is scoped to namespace "realm-a" and trusts the receiver.
        let (_inbox, inbox_sender) = Inbox::new();
        let router = Router::new(
            Keypair::generate(),
            CommsConfig::default(),
            inbox_sender,
            false,
        )
        .with_inproc_namespace(Some(namespace_a.clone()));

        let peer_id = PeerId::from_ed25519_pubkey(&receiver_pubkey_bytes);
        router
            .add_trusted_peer_for_source(
                peer("receiver", receiver_pubkey_bytes, "inproc://receiver"),
                GeneratedCommsTrustAuthoritySourceKind::MeerkatMachinePeerProjection,
                false,
            )
            .expect("trust add");

        // Trusted send resolves the peer (trust is namespace-agnostic) but the
        // delivery authority is the router's namespace ("realm-a"), where the
        // receiver is NOT registered. Must fail closed, not deliver.
        let result = router
            .send(
                peer_id,
                MessageKind::Message {
                    objective_id: None,
                    body: "cross-namespace must not deliver".to_string(),
                    blocks: None,
                    content_taint: None,
                    handling_mode: None,
                },
            )
            .await;
        assert!(
            matches!(result, Err(SendError::PeerNotFound(_))),
            "send to a peer registered only in another namespace must fail closed, got {result:?}"
        );
        assert!(
            receiver_inbox.try_drain_classified().is_empty(),
            "envelope must not be delivered across namespaces"
        );

        // Clean up only our own registration; never `clear()` the shared
        // process-global registry.
        registry.unregister_in_namespace(&namespace_b, &receiver_pubkey);
    }

    /// Single-resolution invariant: when the router is namespace-scoped, the
    /// destination is resolved exactly once *inside* that namespace and that
    /// resolution is the delivery target. A peer whose pubkey is live in OUR
    /// namespace AND in a foreign namespace must receive the envelope on the
    /// in-namespace inbox (and never on the foreign one). The former
    /// precheck-then-re-resolve shape failed closed on this ambiguity and
    /// opened a re-registration window between check and delivery.
    #[tokio::test]
    async fn inproc_send_delivers_to_own_namespace_when_pubkey_live_elsewhere_too() {
        use crate::classify::test_support;

        let registry = InprocRegistry::global();
        let suffix = Uuid::new_v4().simple().to_string();
        let namespace_a = format!("realm-a-{suffix}");
        let namespace_b = format!("realm-b-{suffix}");

        // ONE keypair registered in BOTH namespaces with distinct inboxes.
        let receiver_kp = Keypair::generate();
        let receiver_pubkey = receiver_kp.public_key();
        let receiver_pubkey_bytes = *receiver_pubkey.as_bytes();
        let (mut inbox_a, sender_a) = Inbox::new_classified(test_support::classification_context(
            TrustStore::new(),
            false,
        ));
        let (mut inbox_b, sender_b) = Inbox::new_classified(test_support::classification_context(
            TrustStore::new(),
            false,
        ));
        registry.register_with_meta_in_namespace(
            &namespace_a,
            "receiver-a",
            receiver_pubkey,
            sender_a,
            PeerMeta::default(),
        );
        registry.register_with_meta_in_namespace(
            &namespace_b,
            "receiver-b",
            receiver_pubkey,
            sender_b,
            PeerMeta::default(),
        );

        let (_inbox, inbox_sender) = Inbox::new();
        let router = Router::new(
            Keypair::generate(),
            CommsConfig::default(),
            inbox_sender,
            false,
        )
        .with_inproc_namespace(Some(namespace_a.clone()));

        let peer_id = PeerId::from_ed25519_pubkey(&receiver_pubkey_bytes);
        router
            .add_trusted_peer_for_source(
                peer("receiver", receiver_pubkey_bytes, "inproc://receiver"),
                GeneratedCommsTrustAuthoritySourceKind::MeerkatMachinePeerProjection,
                false,
            )
            .expect("trust add");

        let result = router
            .send(
                peer_id,
                MessageKind::Message {
                    objective_id: None,
                    body: "deliver in-namespace".to_string(),
                    blocks: None,
                    content_taint: None,
                    handling_mode: None,
                },
            )
            .await;
        assert!(
            result.is_ok(),
            "namespace-scoped send must deliver via the in-namespace resolution \
             even when the pubkey is also live elsewhere, got {result:?}"
        );
        assert!(
            !inbox_a.try_drain_classified().is_empty(),
            "envelope must arrive on the in-namespace inbox"
        );
        assert!(
            inbox_b.try_drain_classified().is_empty(),
            "envelope must never arrive on the foreign-namespace inbox"
        );

        registry.unregister_in_namespace(&namespace_a, &receiver_pubkey);
        registry.unregister_in_namespace(&namespace_b, &receiver_pubkey);
    }

    #[test]
    fn trust_re_add_ignores_display_name_but_rejects_divergent_address() {
        // TRUST-1: generated trust identity is the routing material (pubkey +
        // addr); `name` is display-only and legitimately differs across
        // projection sources for one peer (an inproc address embeds the comms
        // name; a non-inproc address falls back to a synthetic backend label).
        // A re-add under the SAME source with the same pubkey+addr but a
        // different name must be an idempotent no-op (Ok(false)); only a
        // divergent pubkey/addr is a genuine conflicting re-add.
        let router = test_router();
        let pubkey = [7u8; 32];
        let source = GeneratedCommsTrustAuthoritySourceKind::MeerkatMachinePeerProjection;

        assert!(
            router
                .add_trusted_peer_for_source(
                    peer("member-inproc-name", pubkey, "tcp://10.0.0.1:7001"),
                    source,
                    false,
                )
                .expect("initial add"),
            "first add of a new peer creates the trust row"
        );

        // Same identity, DIFFERENT display name -> idempotent no-op.
        assert!(
            !router
                .add_trusted_peer_for_source(
                    peer("synthetic-backend-label", pubkey, "tcp://10.0.0.1:7001"),
                    source,
                    false,
                )
                .expect("re-add under a different name must be accepted, not rejected"),
            "a name-only divergence is not a new trust row"
        );

        // Same pubkey, DIVERGENT addr -> genuine conflict.
        let conflict = router.add_trusted_peer_for_source(
            peer("member-inproc-name", pubkey, "tcp://10.9.9.9:7001"),
            source,
            false,
        );
        assert!(
            matches!(
                conflict,
                Err(TrustError::ConflictingGeneratedTrustSource { .. })
            ),
            "a divergent routing address must conflict, got {conflict:?}"
        );
    }

    /// ROW #82 gate: directory visibility is a property of the
    /// source-attributed trust authority, not a separate flat side-map.
    /// Rebuilding a fresh Router by replaying the same generated trust-authority
    /// adds reproduces identical `comms.peers` visibility (private peers stay
    /// filtered) with no separate `private_peer_ids` population step.
    #[test]
    fn private_visibility_derives_from_source_authority_and_survives_rebuild() {
        let public_pubkey = [11u8; 32];
        let private_pubkey = [22u8; 32];
        let public_id = PeerId::from_ed25519_pubkey(&public_pubkey);
        let private_id = PeerId::from_ed25519_pubkey(&private_pubkey);
        let source = GeneratedCommsTrustAuthoritySourceKind::MeerkatMachineSupervisorPublish;

        // Replay the generated trust authority into a fresh router: one public
        // edge, one private (control-plane) edge.
        let install = |router: &Router| {
            router
                .add_trusted_peer_for_source(
                    peer("public-peer", public_pubkey, "tcp://10.0.0.1:7001"),
                    source,
                    false,
                )
                .expect("public add");
            router
                .add_trusted_peer_for_source(
                    peer("private-bridge", private_pubkey, "tcp://10.0.0.2:7001"),
                    source,
                    true,
                )
                .expect("private add");
        };

        let original = test_router();
        install(&original);
        assert!(
            !original.is_private_peer_id(&public_id),
            "a public trust edge is not directory-private"
        );
        assert!(
            original.is_private_peer_id(&private_id),
            "a privately-authorized trust edge is directory-private"
        );
        assert!(original.is_private(&PubKey::new(private_pubkey)));
        assert_eq!(
            original.private_peer_ids(),
            std::collections::BTreeSet::from([private_id]),
            "the private snapshot is derived from the source-attributed authority"
        );

        // A fresh router replaying the SAME authority reproduces identical
        // visibility — there is no separate side-map that has to be repopulated.
        let rebuilt = test_router();
        install(&rebuilt);
        assert_eq!(rebuilt.private_peer_ids(), original.private_peer_ids());
        assert!(rebuilt.is_private_peer_id(&private_id));
        assert!(!rebuilt.is_private_peer_id(&public_id));
    }

    /// ROW #82 gate: privacy is multi-source. A peer authorized privately by
    /// two sources stays directory-private until the LAST private source is
    /// removed; removing one private source does not prematurely expose it.
    #[test]
    fn private_visibility_clears_only_when_last_private_source_removed() {
        let pubkey = [33u8; 32];
        let peer_id = PeerId::from_ed25519_pubkey(&pubkey);
        let source_a = GeneratedCommsTrustAuthoritySourceKind::MeerkatMachineSupervisorPublish;
        let source_b = GeneratedCommsTrustAuthoritySourceKind::MobMachineMemberTrustWiring;

        let router = test_router();
        router
            .add_trusted_peer_for_source(
                peer("dual-private", pubkey, "tcp://10.0.0.3:7001"),
                source_a,
                true,
            )
            .expect("private add A");
        router
            .add_trusted_peer_for_source(
                peer("dual-private", pubkey, "tcp://10.0.0.3:7001"),
                source_b,
                true,
            )
            .expect("private add B");
        assert!(router.is_private_peer_id(&peer_id));

        // Removing one private source must NOT expose the peer.
        assert!(router.remove_trusted_peer_for_source(&peer_id, source_a));
        assert!(
            router.is_private_peer_id(&peer_id),
            "peer stays private while another private source owns it"
        );

        // Removing the last private source clears visibility.
        assert!(router.remove_trusted_peer_for_source(&peer_id, source_b));
        assert!(
            !router.is_private_peer_id(&peer_id),
            "the last private source removal clears directory privacy"
        );
        assert!(router.private_peer_ids().is_empty());
    }

    /// Ask 5 gate: the router is the single outbound taint-stamping choke
    /// point. The runtime-level declaration is inherited when the per-send
    /// override is absent; `Undeclared` strips it; `Declare(taint)` sets it.
    #[tokio::test]
    async fn send_with_id_stamps_outbound_content_taint_declaration() {
        use crate::classify::test_support;
        use crate::inbox::Inbox;
        use crate::types::{InboxItem, MessageKind};
        use meerkat_core::comms::SenderContentTaint;

        let registry = InprocRegistry::global();
        let suffix = Uuid::new_v4().simple().to_string();
        let namespace = format!("taint-{suffix}");

        let receiver_kp = Keypair::generate();
        let receiver_pubkey = receiver_kp.public_key();
        let receiver_pubkey_bytes = *receiver_pubkey.as_bytes();
        let (mut receiver_inbox, receiver_sender) = Inbox::new_classified(
            test_support::classification_context(TrustStore::new(), false),
        );
        registry.register_with_meta_in_namespace(
            &namespace,
            "taint-receiver",
            receiver_pubkey,
            receiver_sender,
            PeerMeta::default(),
        );

        let (_inbox, inbox_sender) = Inbox::new();
        let router = Router::new(
            Keypair::generate(),
            CommsConfig::default(),
            inbox_sender,
            false,
        )
        .with_inproc_namespace(Some(namespace.clone()));
        let peer_id = PeerId::from_ed25519_pubkey(&receiver_pubkey_bytes);
        router
            .add_trusted_peer_for_source(
                peer(
                    "taint-receiver",
                    receiver_pubkey_bytes,
                    "inproc://taint-receiver",
                ),
                GeneratedCommsTrustAuthoritySourceKind::MeerkatMachinePeerProjection,
                false,
            )
            .expect("trust add");

        let message = |body: &str| MessageKind::Message {
            objective_id: None,
            body: body.to_string(),
            blocks: None,
            content_taint: None,
            handling_mode: None,
        };
        let mut received_taint = |expected_body: &str| {
            let entries = receiver_inbox.try_drain_classified();
            assert_eq!(entries.len(), 1, "expected exactly one delivered envelope");
            let InboxItem::External { envelope } = &entries[0].item else {
                panic!("expected external envelope");
            };
            let MessageKind::Message { body, .. } = &envelope.kind else {
                panic!("expected message kind");
            };
            assert_eq!(body, expected_body);
            envelope.kind.content_taint()
        };

        // No runtime declaration + absent override => no declaration.
        router
            .send_with_id(peer_id, Uuid::new_v4(), message("undeclared"), None)
            .await
            .expect("send");
        assert_eq!(received_taint("undeclared"), None);

        // Runtime declaration + absent override => inherit.
        router.set_outbound_content_taint(Some(SenderContentTaint::Tainted));
        router
            .send_with_id(peer_id, Uuid::new_v4(), message("inherited"), None)
            .await
            .expect("send");
        assert_eq!(
            received_taint("inherited"),
            Some(SenderContentTaint::Tainted)
        );

        // Runtime declaration + Undeclared override => strip.
        router
            .send_with_id(
                peer_id,
                Uuid::new_v4(),
                message("stripped"),
                Some(SendTaintOverride::Undeclared),
            )
            .await
            .expect("send");
        assert_eq!(received_taint("stripped"), None);

        // Declare override wins over the runtime declaration.
        router
            .send_with_id(
                peer_id,
                Uuid::new_v4(),
                message("declared"),
                Some(SendTaintOverride::Declare(SenderContentTaint::Clean)),
            )
            .await
            .expect("send");
        assert_eq!(received_taint("declared"), Some(SenderContentTaint::Clean));

        registry.unregister_in_namespace(&namespace, &receiver_pubkey);
    }

    /// One connection's framed envelope read off a raw loopback listener —
    /// the receiver side for declared-reply-endpoint delivery tests.
    #[cfg(not(target_arch = "wasm32"))]
    async fn accept_one_envelope(
        listener: tokio::net::TcpListener,
    ) -> tokio::task::JoinHandle<Envelope> {
        use crate::transport::codec::TransportCodec;
        use futures::StreamExt;
        use tokio_util::codec::Framed;
        tokio::spawn(async move {
            let (stream, _) = listener.accept().await.expect("accept reply connection");
            let mut framed = Framed::new(stream, TransportCodec::new(DEFAULT_MAX_MESSAGE_BYTES));
            framed
                .next()
                .await
                .expect("reply frame arrives")
                .expect("reply frame decodes")
                .envelope
        })
    }

    #[cfg(not(target_arch = "wasm32"))]
    async fn accept_envelopes(
        listener: tokio::net::TcpListener,
        count: usize,
    ) -> tokio::task::JoinHandle<Vec<Envelope>> {
        use crate::transport::codec::TransportCodec;
        use futures::StreamExt;
        tokio::spawn(async move {
            let mut envelopes = Vec::with_capacity(count);
            for _ in 0..count {
                let (stream, _) = listener.accept().await.expect("accept reply connection");
                let mut framed =
                    Framed::new(stream, TransportCodec::new(DEFAULT_MAX_MESSAGE_BYTES));
                envelopes.push(
                    framed
                        .next()
                        .await
                        .expect("reply frame arrives")
                        .expect("reply frame decodes")
                        .envelope,
                );
            }
            envelopes
        })
    }

    fn response_kind(marker: &str) -> MessageKind {
        response_kind_for(Uuid::new_v4(), marker)
    }

    fn response_kind_for(in_reply_to: Uuid, marker: &str) -> MessageKind {
        MessageKind::Response {
            in_reply_to,
            status: crate::types::Status::Failed,
            result: serde_json::json!({ "marker": marker }),
            blocks: None,
            content_taint: None,
            handling_mode: None,
            objective_id: None,
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test(start_paused = true)]
    async fn declared_reply_operation_is_bounded() {
        let error = run_declared_reply_with_timeout(
            "tcp://192.0.2.1:4311",
            std::future::pending::<Result<(), SendError>>(),
        )
        .await
        .expect_err("pending declared-route operation must time out");
        assert!(matches!(
            error,
            SendError::Io(error) if error.kind() == std::io::ErrorKind::TimedOut
        ));
    }

    /// The production socket-routing wrapper applies its deadline only to a
    /// declared callback. Advancing beyond that budget must not complete a
    /// pending operation selected as durable trusted routing.
    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test(start_paused = true)]
    async fn trusted_socket_route_operation_has_no_declared_reply_deadline() {
        let mut operation = Box::pin(run_socket_route_operation(
            "tcp://trusted-peer:4311",
            false,
            std::future::pending::<Result<(), SendError>>(),
        ));
        assert!(
            futures::poll!(operation.as_mut()).is_pending(),
            "trusted operation starts pending"
        );

        tokio::time::advance(DECLARED_REPLY_OPERATION_TIMEOUT + Duration::from_millis(1)).await;
        assert!(
            futures::poll!(operation.as_mut()).is_pending(),
            "trusted routes must not inherit the untrusted callback deadline"
        );
    }

    /// The declared-route deadline covers the frame write too, not just the
    /// socket connect. Use a deliberately tiny in-memory stream so the test
    /// does not depend on platform TCP autotuning or kernel buffer sizes.
    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test(start_paused = true)]
    async fn declared_socket_reply_frame_write_is_bounded() {
        let router = test_router();
        let recipient = Keypair::generate().public_key();
        let correlation = Uuid::new_v4();
        let (mut sender, _nonreading_receiver) = tokio::io::duplex(1024);
        let marker = "x".repeat(900 * 1024);
        let mut envelope = Envelope {
            id: Uuid::new_v4(),
            from: router.keypair.public_key(),
            to: recipient,
            kind: response_kind_for(correlation, &marker),
            sig: crate::identity::Signature::new([0u8; 64]),
        };
        envelope.sign(&router.keypair);

        let error = run_socket_route_operation("tcp://declared-callback", true, async {
            router.send_on_stream(&mut sender, envelope, false).await
        })
        .await
        .expect_err("non-reading declared callback must time out");
        assert!(matches!(
            error,
            SendError::Io(error) if error.kind() == std::io::ErrorKind::TimedOut
        ));
    }

    #[cfg(unix)]
    #[tokio::test]
    async fn declared_uds_reply_frame_write_is_bounded() {
        let router = test_router();
        let recipient = Keypair::generate().public_key();
        let dest = recipient.to_peer_id();
        let correlation = Uuid::new_v4();
        // macOS sockaddr_un paths are limited to 103 bytes and its per-user
        // TMPDIR is already long. Keep this fixture path short enough that the
        // test reaches the write-deadline behavior it is meant to exercise.
        let socket_path = std::path::PathBuf::from("/tmp")
            .join(format!("rkat-dr-{}.sock", Uuid::new_v4().simple()));
        let listener = tokio::net::UnixListener::bind(&socket_path).expect("bind UDS callback");
        router.stage_correlated_reply_endpoint(
            dest,
            correlation,
            DeclaredReplyEndpoint {
                pubkey: recipient,
                address: format!("uds://{}", socket_path.display()),
            },
        );
        let trap = tokio::spawn(async move {
            let (_nonreading_receiver, _) = listener.accept().await.expect("accept UDS callback");
            std::future::pending::<()>().await;
        });

        let marker = "x".repeat(900 * 1024);
        let error = router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(correlation, &marker),
                None,
            )
            .await
            .expect_err("non-reading correlated UDS callback must time out");
        trap.abort();
        let _ = std::fs::remove_file(&socket_path);
        assert!(matches!(
            error,
            SendError::Io(error) if error.kind() == std::io::ErrorKind::TimedOut
        ));
    }

    /// A staged [`DeclaredReplyEndpoint`] delivers a `Response` to a peer the
    /// trust store does not know, addressed to the staged (authenticated)
    /// key — and is consumed by that one send.
    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn staged_reply_endpoint_delivers_response_once_to_untrusted_peer() {
        let router = test_router();
        let sender_kp = Keypair::generate();
        let sender_pubkey = sender_kp.public_key();
        let dest = sender_pubkey.to_peer_id();

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind reply listener");
        let address = format!("tcp://{}", listener.local_addr().expect("listener addr"));
        let received = accept_one_envelope(listener).await;

        router.stage_reply_endpoint(
            dest,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address,
            },
        );
        let outcome = router
            .send_with_id(dest, Uuid::new_v4(), response_kind("reject"), None)
            .await
            .expect("staged endpoint must deliver the response");
        assert_eq!(
            outcome.delivery,
            meerkat_core::comms::PeerDeliveryOutcome::Queued,
            "Response sends never wait for an ack"
        );

        let envelope = received.await.expect("receiver task");
        assert_eq!(
            envelope.to, sender_pubkey,
            "the reply is addressed to the staged authenticated key"
        );
        assert!(
            matches!(envelope.kind, MessageKind::Response { .. }),
            "staged delivery carries the response kind"
        );

        // One-shot: the same destination misses the trust store again AND the
        // staged entry is gone.
        let second = router
            .send_with_id(dest, Uuid::new_v4(), response_kind("replay"), None)
            .await;
        assert!(
            matches!(second, Err(SendError::PeerNotFound(_))),
            "a consumed staged endpoint must not serve a second send: {second:?}"
        );
    }

    /// Staged endpoints are a REPLY channel: non-`Response` kinds never
    /// consult them (and must not consume them).
    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn staged_reply_endpoint_never_serves_non_response_kinds() {
        let router = test_router();
        let sender_kp = Keypair::generate();
        let sender_pubkey = sender_kp.public_key();
        let dest = sender_pubkey.to_peer_id();

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind reply listener");
        let address = format!("tcp://{}", listener.local_addr().expect("listener addr"));
        let received = accept_one_envelope(listener).await;

        router.stage_reply_endpoint(
            dest,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address,
            },
        );
        let message = router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                MessageKind::Message {
                    body: "not a reply".to_string(),
                    blocks: None,
                    content_taint: None,
                    handling_mode: None,
                    objective_id: None,
                },
                None,
            )
            .await;
        assert!(
            matches!(message, Err(SendError::PeerNotFound(_))),
            "a staged endpoint must not grant general sendability: {message:?}"
        );

        // The refused Message send did not consume the entry: the Response
        // still delivers.
        router
            .send_with_id(dest, Uuid::new_v4(), response_kind("still-staged"), None)
            .await
            .expect("the staged endpoint survives non-response sends");
        let envelope = received.await.expect("receiver task");
        assert_eq!(envelope.to, sender_pubkey);
    }

    /// Trust-store entries are the send authority: when the destination is
    /// trusted, the response dials the TRUSTED address and the staged entry
    /// is never consulted.
    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn trust_store_wins_over_staged_reply_endpoint() {
        let router = test_router();
        let sender_kp = Keypair::generate();
        let sender_pubkey = sender_kp.public_key();
        let dest = sender_pubkey.to_peer_id();

        let trusted_listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind trusted listener");
        let trusted_address = format!("tcp://{}", trusted_listener.local_addr().expect("addr"));
        let received = accept_one_envelope(trusted_listener).await;

        let staged_listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind staged listener");
        let staged_address = format!("tcp://{}", staged_listener.local_addr().expect("addr"));

        router
            .add_trusted_peer_for_source(
                peer("trusted-sender", sender_pubkey.0, &trusted_address),
                GeneratedCommsTrustAuthoritySourceKind::MeerkatMachinePeerProjection,
                false,
            )
            .expect("install trusted entry");
        router.stage_reply_endpoint(
            dest,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address: staged_address,
            },
        );

        router
            .send_with_id(dest, Uuid::new_v4(), response_kind("via-trust"), None)
            .await
            .expect("trusted response send");
        let envelope = received.await.expect("trusted receiver task");
        assert_eq!(
            envelope.to, sender_pubkey,
            "the trusted route serves the response; the staged endpoint is not consulted"
        );
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn correlated_reply_endpoints_are_exact_concurrent_and_one_shot() {
        let router = test_router();
        let sender_kp = Keypair::generate();
        let sender_pubkey = sender_kp.public_key();
        let dest = sender_pubkey.to_peer_id();
        let correlation_a = Uuid::new_v4();
        let correlation_b = Uuid::new_v4();

        let listener_a = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind reply listener A");
        let address_a = format!(
            "tcp://{}",
            listener_a.local_addr().expect("listener A addr")
        );
        let received_a = accept_one_envelope(listener_a).await;
        let listener_b = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind reply listener B");
        let address_b = format!(
            "tcp://{}",
            listener_b.local_addr().expect("listener B addr")
        );
        let received_b = accept_one_envelope(listener_b).await;

        router.stage_correlated_reply_endpoint(
            dest,
            correlation_a,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address: address_a,
            },
        );
        router.stage_correlated_reply_endpoint(
            dest,
            correlation_b,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address: address_b,
            },
        );

        // A non-Response cannot consume either entry.
        let non_response = router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                MessageKind::Message {
                    body: "not a response".to_string(),
                    blocks: None,
                    content_taint: None,
                    handling_mode: None,
                    objective_id: None,
                },
                None,
            )
            .await;
        assert!(matches!(non_response, Err(SendError::PeerNotFound(_))));

        // An unmatched correlation cannot consume either exact entry.
        let unmatched = router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(Uuid::new_v4(), "unmatched"),
                None,
            )
            .await;
        assert!(matches!(unmatched, Err(SendError::PeerNotFound(_))));

        let (sent_a, sent_b) = tokio::join!(
            router.send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(correlation_a, "a"),
                None,
            ),
            router.send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(correlation_b, "b"),
                None,
            )
        );
        sent_a.expect("correlation A must route independently");
        sent_b.expect("correlation B must route independently");
        let envelope_a = received_a.await.expect("receiver A task");
        let envelope_b = received_b.await.expect("receiver B task");
        assert!(matches!(
            envelope_a.kind,
            MessageKind::Response { in_reply_to, .. } if in_reply_to == correlation_a
        ));
        assert!(matches!(
            envelope_b.kind,
            MessageKind::Response { in_reply_to, .. } if in_reply_to == correlation_b
        ));

        for correlation in [correlation_a, correlation_b] {
            let replay = router
                .send_with_id(
                    dest,
                    Uuid::new_v4(),
                    response_kind_for(correlation, "replay"),
                    None,
                )
                .await;
            assert!(
                matches!(replay, Err(SendError::PeerNotFound(_))),
                "correlated endpoint must be one-shot: {replay:?}"
            );
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn exact_correlated_reply_overrides_trusted_route_only_for_its_response() {
        let router = test_router();
        let sender_kp = Keypair::generate();
        let sender_pubkey = sender_kp.public_key();
        let dest = sender_pubkey.to_peer_id();
        let correlation = Uuid::new_v4();
        let other_correlation = Uuid::new_v4();

        let trusted_listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind stale trusted listener");
        let trusted_address = format!(
            "tcp://{}",
            trusted_listener
                .local_addr()
                .expect("trusted listener addr")
        );
        let trusted_received = accept_envelopes(trusted_listener, 2).await;
        router
            .add_trusted_peer_for_source(
                peer("rotating-peer", sender_pubkey.0, &trusted_address),
                GeneratedCommsTrustAuthoritySourceKind::MeerkatMachinePeerProjection,
                false,
            )
            .expect("install durable route");

        let correlated_listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind correlated listener");
        let correlated_address = format!(
            "tcp://{}",
            correlated_listener
                .local_addr()
                .expect("correlated listener addr")
        );
        let correlated_received = accept_one_envelope(correlated_listener).await;
        router.stage_correlated_reply_endpoint(
            dest,
            correlation,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address: correlated_address,
            },
        );

        router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(correlation, "exact"),
                None,
            )
            .await
            .expect("exact correlation must bypass stale durable route");
        let exact = correlated_received.await.expect("correlated receiver task");
        assert!(matches!(
            exact.kind,
            MessageKind::Response { in_reply_to, .. } if in_reply_to == correlation
        ));

        router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(other_correlation, "durable"),
                None,
            )
            .await
            .expect("other correlation must retain durable trust routing");
        router
            .send_with_id(
                dest,
                Uuid::new_v4(),
                response_kind_for(correlation, "exact-replay"),
                None,
            )
            .await
            .expect("consumed exact endpoint must fall back to durable trust");
        let durable = trusted_received.await.expect("trusted receiver task");
        assert_eq!(durable.len(), 2);
        assert!(matches!(
            &durable[0].kind,
            MessageKind::Response { in_reply_to, .. } if *in_reply_to == other_correlation
        ));
        assert!(matches!(
            &durable[1].kind,
            MessageKind::Response { in_reply_to, .. } if *in_reply_to == correlation
        ));
    }

    #[test]
    fn correlated_reply_endpoint_cleanup_is_idempotent_and_exact() {
        let router = test_router();
        let sender_pubkey = Keypair::generate().public_key();
        let dest = sender_pubkey.to_peer_id();
        let correlation = Uuid::new_v4();
        router.stage_correlated_reply_endpoint(
            dest,
            correlation,
            DeclaredReplyEndpoint {
                pubkey: sender_pubkey,
                address: "tcp://127.0.0.1:4311".to_string(),
            },
        );
        assert!(!router.unstage_correlated_reply_endpoint(dest, Uuid::new_v4()));
        assert!(router.unstage_correlated_reply_endpoint(dest, correlation));
        assert!(!router.unstage_correlated_reply_endpoint(dest, correlation));
    }
}