arachne-node 0.1.0

Iroh-based peer connectivity and bounded data transport for Arachne.
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
//! Direct-peer live pub/sub assembled over the portable routing module.
//!
//! Experimental: callers supply VERIFIED policies and peer address hints. No
//! invitation verification, group encryption, retained delivery or identity
//! storage yet. This module must not be presented as secure group management.
//! Traffic is direct QUIC/TLS, bound to the authenticated peer's identity; no
//! forwarding through other endpoints. Only locally installed policies authorize.
use std::{
    collections::{BTreeMap, VecDeque},
    net::SocketAddr,
    sync::{
        Arc, Mutex as StdMutex,
        atomic::{AtomicBool, Ordering},
    },
    time::Duration,
};

mod budget;
mod connections;
mod control;
mod overlay;
pub mod resources;
mod wire;
pub use budget::{CapacityCounts, ConnectionBudget};
pub use control::{ControlClient, ControlRequest, ControlTiming, InquiryResponder, MAX_CONTROL_REPLY, Timing};

use connections::Connections;
use arachne_routing::RoutingTable;
pub use arachne_routing::{PeerId, Permissions, Topic, WorkspaceId};
use futures_util::{StreamExt, stream};
use serde::{Deserialize, Serialize};
use tokio::{
    sync::{Mutex, mpsc, watch},
    task::JoinHandle,
};

const ALPN: &[u8] = b"data-fabric/pubsub-experiment/1";
const MAX_PAYLOAD: usize = 16 * 1024;
const MAX_RECIPIENTS: usize = 64;
const MAX_FRAME: usize = 128 * 1024;
const TIMEOUT: Duration = Duration::from_secs(5);
const CONNECTION_IDLE: Duration = Duration::from_secs(60);
const CRITICAL_QUEUE: usize = 256;
const CURRENT_QUEUE: usize = 64;
const BULK_QUEUE: usize = 64;

/// Address discovery and path policy; never workspace authority.
#[derive(Clone, Copy, Debug)]
pub enum NetworkProfile {
    Direct,
    Lan,
    Nearby,
    Wan,
    RelayOnly,
    WanOnly,
}

impl NetworkProfile {
    fn settings(self) -> (Option<&'static str>, bool, bool, bool) {
        match self {
            Self::Direct => (None, false, false, true),
            Self::Lan => (Some("data-fabric"), false, false, true),
            Self::Nearby => (Some("arachne-nearby"), false, false, true),
            Self::Wan => (Some("data-fabric"), true, false, false),
            Self::RelayOnly => (None, true, true, false),
            Self::WanOnly => (None, true, false, false),
        }
    }
}

/// Caller-supplied relay transport settings for a controlled qualification or
/// an operator-managed relay deployment.
#[derive(Clone, Debug)]
pub struct RelayOptions {
    pub(crate) map: iroh::RelayMap,
    pub(crate) tls: iroh::tls::CaTlsConfig,
}

impl RelayOptions {
    pub fn new(map: iroh::RelayMap, tls: iroh::tls::CaTlsConfig) -> Self {
        Self { map, tls }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("routing rejected: {0:?}")]
    Routing(#[from] arachne_routing::Error),
    #[error("transport: {0}")]
    Transport(String),
    #[error("unknown peer address")]
    MissingPeer,
    #[error("control request not sent: {0}")]
    ControlNotSent(&'static str),
    #[error("message exceeds limit")]
    TooLarge,
    #[error("peer rejected operation")]
    Rejected,
    #[error("operation deadline exceeded during {0}; admission outcome may be unknown")]
    Timeout(&'static str),
    #[error("operation cancelled because the local session closed")]
    Cancelled,
    #[error("invalid frame")]
    InvalidFrame,
    #[error("consumer queue full or closed")]
    Backpressure,
    #[error("peer has no announced subscription to this topic")]
    NotSubscribed,
}
type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Message {
    pub workspace: WorkspaceId,
    pub revision: u64,
    pub sender: PeerId,
    /// Authenticated endpoint that handed this frame to us. Equal to `sender`
    /// for direct traffic; forwarding code must never treat it as the author.
    pub received_from: PeerId,
    pub topic: Topic,
    pub payload: Vec<u8>,
    /// Nonempty only for a direct publication. These are workspace member IDs;
    /// endpoint selection is authenticated transport metadata, not application identity.
    pub recipients: Vec<[u8; 32]>,
    pub delivery: DeliveryClass,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
/// Payload-neutral queue behavior declared by the publishing adapter.
pub enum DeliveryClass {
    #[default]
    Critical,
    Current {
        replacement_key: [u8; 32],
    },
    Bulk,
}

type CurrentKey = (
    WorkspaceId,
    u64,
    String,
    PeerId,
    PeerId,
    [u8; 32],
    Vec<[u8; 32]>,
);

#[derive(Default)]
struct DeliveryState {
    critical: VecDeque<Message>,
    current: BTreeMap<CurrentKey, Message>,
    bulk: VecDeque<Message>,
    critical_run: u8,
    bulk_turn: bool,
}

#[derive(Clone, Default)]
struct DeliveryQueue {
    state: Arc<StdMutex<DeliveryState>>,
    changed: Arc<tokio::sync::Notify>,
    closed: Arc<AtomicBool>,
}

impl DeliveryQueue {
    fn push(&self, message: Message) -> Result<()> {
        if self.closed.load(Ordering::Acquire) {
            return Err(Error::Backpressure);
        }
        let mut state = self.state.lock().unwrap();
        match message.delivery {
            DeliveryClass::Critical if state.critical.len() < CRITICAL_QUEUE => {
                state.critical.push_back(message)
            }
            DeliveryClass::Current { replacement_key } => {
                let key = (
                    message.workspace,
                    message.revision,
                    message.topic.as_str().into(),
                    message.sender,
                    message.received_from,
                    replacement_key,
                    message.recipients.clone(),
                );
                if state.current.len() == CURRENT_QUEUE && !state.current.contains_key(&key) {
                    return Err(Error::Backpressure);
                }
                state.current.insert(key, message);
            }
            DeliveryClass::Bulk if state.bulk.len() < BULK_QUEUE => state.bulk.push_back(message),
            _ => return Err(Error::Backpressure),
        }
        drop(state);
        self.changed.notify_one();
        Ok(())
    }

    fn pop(&self) -> Option<Message> {
        let mut state = self.state.lock().unwrap();
        if state.critical_run < 8
            && let Some(message) = state.critical.pop_front()
        {
            state.critical_run += 1;
            return Some(message);
        }
        let lower = if state.bulk_turn {
            state
                .bulk
                .pop_front()
                .or_else(|| state.current.pop_first().map(|(_, message)| message))
        } else {
            state
                .current
                .pop_first()
                .map(|(_, message)| message)
                .or_else(|| state.bulk.pop_front())
        };
        if lower.is_some() {
            state.critical_run = 0;
            state.bulk_turn = !state.bulk_turn;
            return lower;
        }
        state.critical.pop_front()
    }

    fn close(&self) {
        self.closed.store(true, Ordering::Release);
        self.changed.notify_waiters();
    }
}

pub struct MessageReceiver {
    queue: DeliveryQueue,
}

impl MessageReceiver {
    /// Snapshot of all delivery classes, without consuming the next message.
    pub fn is_empty(&self) -> bool {
        let state = self.queue.state.lock().unwrap();
        state.critical.is_empty() && state.current.is_empty() && state.bulk.is_empty()
    }

    pub fn try_recv(&mut self) -> std::result::Result<Message, mpsc::error::TryRecvError> {
        self.queue.pop().ok_or_else(|| {
            if self.queue.closed.load(Ordering::Acquire) {
                mpsc::error::TryRecvError::Disconnected
            } else {
                mpsc::error::TryRecvError::Empty
            }
        })
    }

    pub async fn recv(&mut self) -> Option<Message> {
        loop {
            let notify = self.queue.changed.clone();
            let changed = notify.notified();
            match self.try_recv() {
                Ok(message) => return Some(message),
                Err(mpsc::error::TryRecvError::Disconnected) => return None,
                Err(mpsc::error::TryRecvError::Empty) => changed.await,
            }
        }
    }
}

/// Acceptance of an operation. Publications enter consumer queues; this is not
/// a delivery/read receipt. Subscription operations update interest. A timeout
/// may have occurred after admission; retry semantics are not implemented yet.
#[derive(Debug, Default)]
pub struct AdmissionReport {
    pub admitted: Vec<PeerId>,
    pub failed: Vec<(PeerId, Error)>,
    /// A bounded live overlay accepted this publication locally. This is not a
    /// remote admission, delivery, retention or read receipt.
    pub queued: bool,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
struct Frame {
    workspace: WorkspaceId,
    revision: u64,
    #[serde(deserialize_with = "wire::topic")]
    topic: String,
    delivery: DeliveryClass,
    operation: Operation,
}

#[derive(Serialize, Deserialize)]
enum Operation {
    Subscribe,
    Unsubscribe,
    Publish(#[serde(with = "wire::payload")] Vec<u8>),
    DirectPublish {
        #[serde(with = "wire::payload")]
        payload: Vec<u8>,
        #[serde(deserialize_with = "wire::recipients")]
        recipients: Vec<[u8; 32]>,
    },
}

/// Local endpoint counters. These include transport overhead and retransmission,
/// not application delivery receipts. They reset when the endpoint is opened.
#[derive(Debug, serde::Serialize)]
pub struct TransportMetrics {
    pub received_bytes: u64,
    pub sent_bytes: u64,
    pub receive_queue: usize,
    pub paths: Vec<PeerPath>,
    pub paths_limited: bool,
}

#[derive(Debug, serde::Serialize)]
pub struct PeerPath {
    pub endpoint: PeerId,
    pub route: &'static str,
    pub rtt_ms: u64,
}

pub struct Node {
    controls: mpsc::Receiver<ControlRequest>,
    control_inbox: control::ControlInbox,
    control_signal: Arc<tokio::sync::Notify>,
    control_cancel: watch::Sender<bool>,
    deferred_controls: std::collections::VecDeque<ControlRequest>,
    connections: Connections,
    routing: Arc<Mutex<RoutingTable>>,
    resources: resources::ResourceTransfers,
    events: DeliveryQueue,
    overlays: Arc<Mutex<BTreeMap<WorkspaceId, Arc<overlay::Overlay>>>>,
    membership: overlay::MembershipInbox,
    listener: JoinHandle<()>,
}

impl Node {
    pub fn transport_metrics(&self) -> TransportMetrics {
        let mut metrics = self.connections.metrics();
        let queue = self.events.state.lock().unwrap();
        metrics.receive_queue = queue.critical.len() + queue.current.len() + queue.bulk.len();
        metrics
    }

    /// Bind a directly reachable local address. Public discovery, relays and
    /// port mapping are disabled. Fresh identity on every bind in this version.
    pub async fn bind(address: SocketAddr) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            None,
            NetworkProfile::Direct,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Bind using a caller-owned, securely generated and stored 32-byte endpoint
    /// credential. This restores transport identity only, never workspace authority.
    /// The caller must prevent concurrent endpoint instances using this credential.
    pub async fn bind_with_identity(
        address: SocketAddr,
        secret: &[u8; 32],
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            Some(secret),
            NetworkProfile::Direct,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Opt in to local-network address advertisement/lookup. This publishes the
    /// endpoint key and addresses, never workspace names or membership authority.
    /// Public lookup services and relay transports remain disabled.
    pub async fn bind_lan(address: SocketAddr) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            None,
            NetworkProfile::Lan,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Opt in to LAN lookup while retaining a workspace-scoped endpoint identity.
    /// Advertises this endpoint key and addresses, never membership authority.
    pub async fn bind_lan_with_identity(
        address: SocketAddr,
        secret: &[u8; 32],
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            Some(secret),
            NetworkProfile::Lan,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Local device-level invitation discovery, isolated from workspace endpoint discovery.
    pub async fn bind_nearby_with_identity(
        address: SocketAddr,
        secret: &[u8; 32],
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            Some(secret),
            NetworkProfile::Nearby,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Opt in to Iroh's public Pkarr address lookup and relay network while
    /// retaining LAN lookup. These services provide routes, never membership.
    pub async fn bind_wan_with_identity(
        address: SocketAddr,
        secret: &[u8; 32],
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            Some(secret),
            NetworkProfile::Wan,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Diagnostic WAN profile that forces authenticated traffic through Iroh
    /// relays while leaving the device's Wi-Fi connection intact.
    pub async fn bind_relay_with_identity(
        address: SocketAddr,
        secret: &[u8; 32],
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            Some(secret),
            NetworkProfile::RelayOnly,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Diagnostic WAN profile that uses only public endpoint lookup and ignores
    /// LAN discovery and saved IP hints while retaining direct Iroh paths.
    pub async fn bind_wan_only_with_identity(
        address: SocketAddr,
        secret: &[u8; 32],
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile(
            address,
            Some(secret),
            NetworkProfile::WanOnly,
            ConnectionBudget::default(),
        )
        .await
    }

    /// Bind an independently identified endpoint using host-owned shared device
    /// capacity. Clone the same budget for each workspace and discovery endpoint.
    pub async fn bind_with_profile(
        address: SocketAddr,
        secret: Option<&[u8; 32]>,
        profile: NetworkProfile,
        budget: ConnectionBudget,
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile_and_relays(address, secret, profile, budget, None).await
    }

    /// Bind with a caller-supplied relay map and TLS trust configuration.
    ///
    /// This is intended for controlled relay qualification and operator-managed
    /// relay deployments. The caller must select `NetworkProfile::RelayOnly`
    /// when direct paths must be excluded.
    pub async fn bind_with_profile_and_relay(
        address: SocketAddr,
        secret: Option<&[u8; 32]>,
        profile: NetworkProfile,
        budget: ConnectionBudget,
        relay: RelayOptions,
    ) -> Result<(Self, MessageReceiver)> {
        Self::bind_with_profile_and_relays(address, secret, profile, budget, Some(relay)).await
    }

    async fn bind_with_profile_and_relays(
        address: SocketAddr,
        secret: Option<&[u8; 32]>,
        profile: NetworkProfile,
        budget: ConnectionBudget,
        relay: Option<RelayOptions>,
    ) -> Result<(Self, MessageReceiver)> {
        let connections = Connections::bind(
            address,
            profile,
            secret.map(iroh::SecretKey::from_bytes),
            budget.clone(),
            vec![ALPN.to_vec(), control::ALPN.to_vec()],
            relay,
        )
        .await?;
        let routing = Arc::new(Mutex::new(RoutingTable::default()));
        let resources = resources::ResourceTransfers::new(connections.clone(), routing.clone());
        let events = DeliveryQueue::default();
        let receiver = MessageReceiver {
            queue: events.clone(),
        };
        let (control_inbox, controls, control_signal) = control::ControlInbox::new(512);
        let (control_cancel, _) = watch::channel(false);
        let node_control_inbox = control_inbox.clone();
        let overlays: Arc<Mutex<BTreeMap<WorkspaceId, Arc<overlay::Overlay>>>> =
            Arc::new(Mutex::new(BTreeMap::new()));
        let accepted_connections = connections.clone();
        let shared = routing.clone();
        let output = events.clone();
        let accepted_overlays = overlays.clone();
        let accepted_resources = resources.clone();
        let listener = tokio::spawn(async move {
            // Handshakes release their slot before application work. Slow data
            // peers cannot occupy the slots needed to reach the control budget.
            let handshake_capacity = budget.handshakes;
            let [data_capacity, control_capacity] = budget.exchanges;
            let mut workers = tokio::task::JoinSet::new();
            while let Some(incoming) = accepted_connections.accept().await {
                while workers.try_join_next().is_some() {}
                let Ok(permit) = handshake_capacity.clone().try_acquire_owned() else {
                    incoming.refuse();
                    continue;
                };
                let control_inbox = control_inbox.clone();
                let control_capacity = control_capacity.clone();
                let data_capacity = data_capacity.clone();
                let routing = shared.clone();
                let overlays = accepted_overlays.clone();
                let connections = accepted_connections.clone();
                let output = output.clone();
                let resources = accepted_resources.clone();
                workers.spawn(async move {
                    let mut stage = "accept connection";
                    let mut remote = None;
                    let mut connection_id = None;
                    let mut observed_connection = None;
                    let result = async {
                        let observed_address = incoming.remote_addr();
                        let connection = tokio::time::timeout(TIMEOUT, incoming).await
                            .map_err(|_| Error::Timeout("accept connection"))?.map_err(transport)?;
                        drop(permit);
                        observed_connection = Some(connection.clone());
                        let sender = *connection.remote_id().as_bytes();
                        remote = Some(connection.remote_id());
                        connection_id = Some(connection.stable_id());
                        let is_control = connection.alpn() == control::ALPN;
                        let overlay = {
                            let overlays = overlays.lock().await;
                            overlays
                                .values()
                                .find(|overlay| overlay.alpn.as_slice() == connection.alpn())
                                .map(|overlay| {
                                    (
                                        overlay.workspace,
                                        overlay.revision(),
                                        overlay.gossip.clone(),
                                    )
                                })
                        };
                        if let Some((workspace, revision, gossip)) = overlay {
                            routing
                                .lock()
                                .await
                                .authorizes_endpoint(workspace, revision, sender)?;
                            stage = "accept gossip";
                            tokio::time::timeout(control::CONTROL_TIMEOUT, gossip.handle_connection(connection))
                                .await.map_err(|_| Error::Timeout("accept gossip"))?.map_err(transport)?;
                            return Ok(());
                        }
                        // A removed overlay's already-negotiated ALPN must never
                        // be interpreted as the direct-frame schema.
                        if !is_control && connection.alpn() != ALPN {
                            return Err(Error::InvalidFrame);
                        }
                        // An idle exchange link may give way when the device is full.
                        let connection_key = connection.stable_id();
                        connections.mark_evictable(connection_key);
                        let (capacity, budget) = if is_control {
                            (control_capacity, control::CONTROL_TIMEOUT)
                        } else {
                            (data_capacity, TIMEOUT)
                        };
                        let address = match observed_address {
                            iroh::endpoint::IncomingAddr::Ip(address) => Some(address),
                            _ => None,
                        };
                        stage = "receive exchanges";
                        tracing::info!(target: "data_fabric_transport", ?remote, ?connection_id, paths = ?connection.paths(), "TRANSPORT_RECEIVE_CONNECTED");
                        let connection = &connection;
                        let connections = &connections;
                        let routing = &routing;
                        let output = &output;
                        let control_inbox = &control_inbox;
                        let resources = &resources;
                        let mut exchanges = stream::FuturesUnordered::new();
                        let idle = tokio::time::sleep(CONNECTION_IDLE);
                        tokio::pin!(idle);
                        loop {
                            tokio::select! {
                                streams = connection.accept_bi() => {
                                    let Ok((mut send, mut recv)) = streams else { return Ok(()); };
                                    let Ok(permit) = capacity.clone().try_acquire_owned() else {
                                        let _ = send.reset(1u8.into());
                                        let _ = recv.stop(1u8.into());
                                        continue;
                                    };
                                    idle.as_mut().reset(tokio::time::Instant::now() + CONNECTION_IDLE);
                                    let guard = connections.exchange(connection_key);
                                    exchanges.push(async move {
                                        let _permit = permit;
                                        let _guard = guard;
                                        let result = async {
                                            if !is_control {
                                                let mut kind = [0; 1];
                                                tokio::time::timeout(TIMEOUT, recv.read_exact(&mut kind)).await
                                                    .map_err(|_| Error::Timeout("stream kind"))?.map_err(transport)?;
                                                match kind[0] {
                                                    resources::STREAM_KIND => return resources.serve(connection, &mut send, &mut recv).await,
                                                    0 => (),
                                                    _ => return Err(Error::InvalidFrame),
                                                }
                                            }
                                            tokio::time::timeout(budget, async {
                                              if is_control {
                                                control::receive(connection, control_inbox, address, (&mut send, &mut recv)).await
                                              } else {
                                                receive_frame(connection, connections, routing, output, address, (&mut send, &mut recv)).await
                                              }
                                            }).await.map_err(|_| Error::Timeout("receive exchange"))?
                                        }.await;
                                        // Dropping a QUIC SendStream finishes it. An
                                        // abandoned/expired exchange must reset instead
                                        // of looking like a successful empty reply.
                                        if result.is_err() {
                                            let _ = send.reset(1u8.into());
                                            let _ = recv.stop(1u8.into());
                                        }
                                        tracing::info!(target: "data_fabric_transport", ?remote, ?connection_id, is_control, ?result, "TRANSPORT_EXCHANGE_END");
                                    });
                                }
                                _ = exchanges.next(), if !exchanges.is_empty() => {
                                    idle.as_mut().reset(tokio::time::Instant::now() + CONNECTION_IDLE);
                                }
                                _ = &mut idle, if exchanges.is_empty() => {
                                    connection.close(0u8.into(), b"idle");
                                    return Ok(());
                                }
                            }
                        }
                    }
                    .await;
                    tracing::info!(target: "data_fabric_transport", ?remote, ?connection_id, stage, ?result, "TRANSPORT_RECEIVE_END");
                    if result.is_err() {
                        tracing::info!(target: "data_fabric_transport", ?remote, ?connection_id,
                            stats = ?observed_connection.as_ref().map(|connection| connection.stats()),
                            "TRANSPORT_RECEIVE_STATS");
                    }
                });
            }
        });
        let control_signal_for_membership = control_signal.clone();
        Ok((
            Self {
                controls,
                control_inbox: node_control_inbox,
                control_signal,
                control_cancel,
                deferred_controls: std::collections::VecDeque::new(),
                connections,
                routing,
                resources,
                events,
                overlays,
                membership: overlay::MembershipInbox::new(control_signal_for_membership),
                listener,
            },
            receiver,
        ))
    }

    pub fn id(&self) -> PeerId {
        self.connections.id()
    }

    /// A session-local cancellation latch for outbound control exchanges.
    pub fn control_cancellation(&self) -> watch::Sender<bool> {
        self.control_cancel.clone()
    }

    /// Next membership step received by gossip, as (workspace, opaque bytes).
    /// Delivered without a policy revision check (ADR 0008): the caller must
    /// verify the step itself against its own saved state before using it.
    pub fn poll_membership_gossip(&self) -> Option<(WorkspaceId, Vec<u8>)> {
        self.membership.pop()
    }

    /// Wait for one queued membership envelope for `workspace`.
    /// The caller owns the deadline; no polling interval is introduced here.
    pub async fn wait_for_membership_gossip(&self, workspace: WorkspaceId) -> Option<Vec<u8>> {
        loop {
            let notified = self.control_signal.notified();
            if let Some(payload) = self.membership.pop_for(workspace) {
                return Some(payload);
            }
            notified.await;
        }
    }

    /// Broadcast a committed membership step on the workspace overlay's
    /// reserved membership topic. Owned so the host can spawn it. Ok(false)
    /// means no overlay or no remote member to send to.
    pub fn broadcast_membership(
        &self,
        workspace: WorkspaceId,
        payload: Vec<u8>,
    ) -> impl std::future::Future<Output = Result<bool>> + Send + 'static {
        let overlays = self.overlays.clone();
        let sender = self.id();
        async move {
            let Some(overlay) = overlays.lock().await.get(&workspace).cloned() else {
                return Ok(false);
            };
            overlay.broadcast_membership(sender, payload).await
        }
    }
    pub fn address(&self) -> SocketAddr {
        self.connections.address()
    }

    /// Wait until the endpoint has an active relay connection.
    ///
    /// This is only needed by callers that explicitly selected a relay-only
    /// profile. It is event-driven and does not add a polling delay or fallback.
    pub async fn wait_online(&self) {
        self.connections.wait_online().await;
    }

    pub fn resources(&self) -> resources::ResourceTransfers {
        self.resources.clone()
    }

    /// Android cannot detect host network changes from native code. The Java
    /// connectivity callback must forward them so Iroh can rebind its sockets.
    pub async fn network_change(&self) {
        self.connections.network_change().await;
    }

    pub async fn add_address_hint(&self, peer: PeerId, address: SocketAddr) -> Result<()> {
        self.connections.add_address_hint(peer, address).await?;
        let overlays = self
            .overlays
            .lock()
            .await
            .values()
            .cloned()
            .collect::<Vec<_>>();
        for overlay in overlays {
            let authorized = self
                .routing
                .lock()
                .await
                .authorizes_endpoint(overlay.workspace, overlay.revision(), peer)
                .is_ok();
            if authorized && peer != self.id() {
                if overlay.is_joined() {
                    overlay.join_peer(peer).await?;
                } else {
                    tracing::info!(target: "data_fabric_transport", peer = %iroh::EndpointId::from_bytes(&peer).map(|p| p.fmt_short().to_string()).unwrap_or_default(), "GOSSIP_OVERLAY_REBUILD_ON_HINT");
                    let peers = self
                        .routing
                        .lock()
                        .await
                        .authorized_endpoints(overlay.workspace, overlay.revision())?;
                    let replacement = overlay::Overlay::prepare(
                        &self.connections,
                        overlay.workspace,
                        overlay.revision(),
                        peers,
                        self.routing.clone(),
                        self.events.clone(),
                        self.membership.clone(),
                    )
                    .await?;
                    self.overlays
                        .lock()
                        .await
                        .insert(overlay.workspace, Arc::new(replacement));
                }
            }
        }
        Ok(())
    }

    /// Remember an authenticated return path without synchronously changing
    /// any workspace gossip overlay. Control-plane observations use this
    /// narrow path so a presence reply cannot wait on peer discovery.
    pub async fn remember_observed(&self, peer: PeerId, address: SocketAddr) {
        self.connections.remember_observed(peer, address).await;
    }

    /// A cached route is only a hint. Callers must separately authorize any
    /// workspace metadata they share with it.
    pub async fn address_hint(&self, peer: PeerId) -> Option<SocketAddr> {
        self.connections.address_hint(peer).await
    }

    /// Endpoints passively observed on the application-named local mDNS service.
    /// These are route hints only and carry no workspace or human identity claim.
    pub async fn nearby_peers(&self) -> Vec<PeerId> {
        self.connections.nearby_peers(true).await
    }

    /// Return a bounded batch for workspace advertisement queries.
    pub async fn nearby_workspace_peers(&self) -> Vec<PeerId> {
        self.connections.nearby_peers(false).await
    }

    /// Trusted application seam. Never invoke with an unverified network policy.
    /// The caller resolves member/device bindings into endpoint permissions; this
    /// module neither establishes those bindings nor verifies admin authority.
    pub async fn install_verified_policy(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        endpoint_permissions: BTreeMap<PeerId, Permissions>,
    ) -> Result<()> {
        let replace_overlay = self.overlays.lock().await.contains_key(&workspace);
        let peers = endpoint_permissions.keys().copied().collect::<Vec<_>>();
        self.routing.lock().await.install_verified_policy(
            workspace,
            revision,
            endpoint_permissions,
        )?;
        self.resources.policy_changed();
        if replace_overlay {
            if !peers.contains(&self.id()) {
                self.connections
                    .authorize_gossip(overlay::alpn(workspace), Vec::new())
                    .await;
                self.overlays.lock().await.remove(&workspace);
                return Ok(());
            }
            self.connections
                .authorize_gossip(overlay::alpn(workspace), peers.clone())
                .await;
            // An epoch that only adds members keeps the swarm: rebuilding it on
            // every admission dropped all neighbors mid-broadcast (ADR 0008).
            let existing = self.overlays.lock().await.get(&workspace).cloned();
            if existing.is_some_and(|overlay| overlay.advance(revision, &peers)) {
                return Ok(());
            }
            let candidate = overlay::Overlay::prepare(
                &self.connections,
                workspace,
                revision,
                peers,
                self.routing.clone(),
                self.events.clone(),
                self.membership.clone(),
            )
            .await?;
            self.overlays
                .lock()
                .await
                .insert(workspace, Arc::new(candidate));
        }
        Ok(())
    }

    /// Enable one bounded workspace-wide live overlay after installing a verified
    /// policy. Direct-recipient publications keep their acknowledged path.
    pub async fn enable_gossip(&self, workspace: WorkspaceId, revision: u64) -> Result<()> {
        if self
            .overlays
            .lock()
            .await
            .get(&workspace)
            .is_some_and(|overlay| overlay.revision() == revision)
        {
            return Ok(());
        }
        let peers = self
            .routing
            .lock()
            .await
            .authorized_endpoints(workspace, revision)?;
        self.connections
            .authorize_gossip(overlay::alpn(workspace), peers.clone())
            .await;
        let candidate = overlay::Overlay::prepare(
            &self.connections,
            workspace,
            revision,
            peers,
            self.routing.clone(),
            self.events.clone(),
            self.membership.clone(),
        )
        .await?;
        self.overlays
            .lock()
            .await
            .insert(workspace, Arc::new(candidate));
        Ok(())
    }

    /// Read the current routing policy under the same lock used for data routing.
    /// The callback must be bounded, synchronous and must not reenter this node.
    /// Owned results may leave the lock; policy references cannot escape it.
    pub async fn with_routing_policy<T>(&self, inspect: impl FnOnce(&RoutingTable) -> T) -> T {
        let routing = self.routing.lock().await;
        inspect(&routing)
    }

    /// Current maintained live neighbors for a workspace overlay. Zero means
    /// no overlay or no connected neighbor; it is not a workspace member count.
    pub async fn live_neighbor_count(&self, workspace: WorkspaceId) -> usize {
        self.overlays
            .lock()
            .await
            .get(&workspace)
            .map_or(0, |overlay| overlay.neighbor_count())
    }

    /// Authenticated direct neighbors in the bounded workspace Gossip view.
    /// These are advisory retrieval candidates, not proof that history exists.
    pub async fn live_neighbors(&self, workspace: WorkspaceId) -> Vec<PeerId> {
        self.overlays
            .lock()
            .await
            .get(&workspace)
            .map_or_else(Vec::new, |overlay| overlay.neighbors())
    }

    /// Wait for the native Gossip overlay to observe at least one neighbor.
    /// The caller owns the deadline; no polling interval is introduced here.
    pub async fn wait_for_gossip_neighbor(&self, workspace: WorkspaceId) -> bool {
        let Some(overlay) = self.overlays.lock().await.get(&workspace).cloned() else {
            return false;
        };
        overlay.wait_for_neighbor().await
    }

    pub async fn subscribe(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
    ) -> Result<AdmissionReport> {
        self.interest(workspace, revision, topic, true).await
    }

    pub async fn unsubscribe(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
    ) -> Result<AdmissionReport> {
        self.interest(workspace, revision, topic, false).await
    }

    async fn interest(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
        subscribe: bool,
    ) -> Result<AdmissionReport> {
        self.prepare_interest(workspace, revision, topic, subscribe)
            .await?
            .await
    }

    /// Apply local interest before returning an owned remote announcement.
    /// Hosts may schedule this future without holding their session lock. They
    /// must serialize announcements; dropping one does not undo local interest.
    pub async fn prepare_interest(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
        subscribe: bool,
    ) -> Result<impl std::future::Future<Output = Result<AdmissionReport>> + Send + 'static> {
        let mut peers = {
            let mut routing = self.routing.lock().await;
            let peers = routing.publishers(workspace, revision, self.id(), &topic)?;
            if subscribe {
                routing.subscribe(workspace, revision, self.id(), topic.clone())?;
            } else {
                routing.unsubscribe(workspace, revision, self.id(), &topic)?;
            }
            peers
        };
        let local = self.id();
        let admitted_local = peers.contains(&local);
        peers.retain(|peer| *peer != local);
        // Local interest is already installed. A delayed self-announcement must
        // never overwrite a more recent local unsubscribe.
        let remote = self.fanout(
            peers,
            Frame {
                workspace,
                revision,
                topic: topic.as_str().into(),
                delivery: DeliveryClass::Critical,
                operation: if subscribe {
                    Operation::Subscribe
                } else {
                    Operation::Unsubscribe
                },
            },
        );
        Ok(async move {
            let mut report = remote.await?;
            if admitted_local {
                report.admitted.push(local);
                report.admitted.sort_unstable();
            }
            Ok(report)
        })
    }

    pub async fn publish(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
        payload: Vec<u8>,
    ) -> Result<AdmissionReport> {
        self.publish_with_class(workspace, revision, topic, DeliveryClass::Critical, payload)
            .await
    }

    pub async fn publish_with_class(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
        delivery: DeliveryClass,
        payload: Vec<u8>,
    ) -> Result<AdmissionReport> {
        if payload.len() > MAX_PAYLOAD {
            return Err(Error::TooLarge);
        }
        let overlay = self.overlays.lock().await.get(&workspace).cloned();
        if let Some(overlay) = overlay {
            if overlay.revision() != revision {
                return Err(Error::Routing(arachne_routing::Error::WrongPolicyRevision));
            }
            let recipients =
                self.routing
                    .lock()
                    .await
                    .recipients(workspace, revision, self.id(), &topic)?;
            let mut report = AdmissionReport::default();
            if recipients.contains(&self.id()) {
                apply(
                    &self.routing,
                    &self.events,
                    self.id(),
                    self.id(),
                    self.id(),
                    Frame {
                        workspace,
                        revision,
                        topic: topic.as_str().into(),
                        delivery,
                        operation: Operation::Publish(payload.clone()),
                    },
                )
                .await?;
                report.admitted.push(self.id());
            }
            report.queued = overlay
                .broadcast(self.id(), &topic, delivery, payload)
                .await?;
            return Ok(report);
        }
        let peers = self
            .routing
            .lock()
            .await
            .recipients(workspace, revision, self.id(), &topic)?;
        self.fanout(
            peers,
            Frame {
                workspace,
                revision,
                topic: topic.as_str().into(),
                delivery,
                operation: Operation::Publish(payload),
            },
        )
        .await
    }

    /// Send to authorized subscribers within the resolved audience. Each selected
    /// endpoint without announced interest gets a non-admission result.
    pub async fn publish_to(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
        endpoints: Vec<PeerId>,
        recipients: Vec<[u8; 32]>,
        payload: Vec<u8>,
    ) -> Result<AdmissionReport> {
        self.publish_to_with_class(
            workspace,
            revision,
            topic,
            endpoints,
            recipients,
            DeliveryClass::Critical,
            payload,
        )
        .await
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn publish_to_with_class(
        &self,
        workspace: WorkspaceId,
        revision: u64,
        topic: Topic,
        endpoints: Vec<PeerId>,
        recipients: Vec<[u8; 32]>,
        delivery: DeliveryClass,
        payload: Vec<u8>,
    ) -> Result<AdmissionReport> {
        if payload.len() > MAX_PAYLOAD
            || recipients.is_empty()
            || recipients.len() > MAX_RECIPIENTS
            || recipients.windows(2).any(|pair| pair[0] >= pair[1])
        {
            return Err(Error::Rejected);
        }
        let peers = self.routing.lock().await.direct_recipients(
            workspace,
            revision,
            self.id(),
            &topic,
            &endpoints,
        )?;
        let skipped: Vec<_> = endpoints
            .into_iter()
            .filter(|peer| peers.binary_search(peer).is_err())
            .collect();
        let mut report = self
            .fanout(
                peers,
                Frame {
                    workspace,
                    revision,
                    topic: topic.as_str().into(),
                    delivery,
                    operation: Operation::DirectPublish {
                        payload,
                        recipients,
                    },
                },
            )
            .await?;
        report
            .failed
            .extend(skipped.into_iter().map(|peer| (peer, Error::NotSubscribed)));
        report.failed.sort_unstable_by_key(|(peer, _)| *peer);
        Ok(report)
    }

    fn fanout(
        &self,
        peers: Vec<PeerId>,
        frame: Frame,
    ) -> impl std::future::Future<Output = Result<AdmissionReport>> + Send + 'static {
        let connections = self.connections.clone();
        let routing = self.routing.clone();
        let events = self.events.clone();
        let local = self.id();
        async move {
            let bytes = wire::encode(&frame)?;
            let mut report = AdmissionReport::default();
            // ponytail: 16 concurrent operations per fanout; connection reuse and a
            // sparse overlay still need measurement before large-workspace claims.
            let mut pending = stream::iter(peers.into_iter().map(|peer| {
                let frame = &frame;
                let bytes = &bytes;
                let connections = &connections;
                let routing = &routing;
                let events = &events;
                async move {
                    let result = if peer == local {
                        match wire::decode(bytes) {
                            Ok(copy) => apply(routing, events, local, local, local, copy).await,
                            Err(_) => Err(Error::InvalidFrame),
                        }
                    } else {
                        send_frame(connections, routing, peer, frame, bytes).await
                    };
                    (peer, result)
                }
            }))
            .buffer_unordered(16);
            while let Some((peer, result)) = pending.next().await {
                match result {
                    Ok(()) => report.admitted.push(peer),
                    Err(error) => report.failed.push((peer, error)),
                }
            }
            // Preserve the public report's deterministic peer order.
            report.admitted.sort_unstable();
            report.failed.sort_unstable_by_key(|(peer, _)| *peer);
            Ok(report)
        }
    }

    pub async fn close(mut self) {
        self.resources.close().await;
        self.overlays.lock().await.clear();
        self.connections.close().await;
        self.listener.abort();
        let _ = (&mut self.listener).await;

    }
}

async fn receive_frame(
    connection: &iroh::endpoint::Connection,
    connections: &Connections,
    routing: &Mutex<RoutingTable>,
    output: &DeliveryQueue,
    address: Option<SocketAddr>,
    (send, recv): (
        &mut iroh::endpoint::SendStream,
        &mut iroh::endpoint::RecvStream,
    ),
) -> Result<()> {
    let sender = *connection.remote_id().as_bytes();
    let bytes = recv.read_to_end(MAX_FRAME).await.map_err(transport)?;
    tracing::info!(target: "data_fabric_transport", remote = %connection.remote_id(), connection_id = connection.stable_id(), bytes = bytes.len(), "TRANSPORT_FRAME_RECEIVED");
    let outcome = match wire::decode::<Frame>(&bytes) {
        Ok(frame) => apply(routing, output, connections.id(), sender, sender, frame).await,
        Err(_) => Err(Error::InvalidFrame),
    };
    // Reusing transport identity never caches workspace authority. Remember an
    // observed direct path only after this particular frame is authorized.
    if outcome.is_ok()
        && let Some(address) = address
    {
        connections.remember_observed(sender, address).await;
    }
    tracing::info!(target: "data_fabric_transport", remote = %connection.remote_id(), connection_id = connection.stable_id(), ?outcome, "TRANSPORT_ADMISSION");
    send.write_all(&[u8::from(outcome.is_ok())])
        .await
        .map_err(transport)?;
    send.finish().map_err(transport)?;
    Ok(())
}

async fn send_frame(
    connections: &Connections,
    routing: &Mutex<RoutingTable>,
    peer: PeerId,
    frame: &Frame,
    bytes: &[u8],
) -> Result<()> {
    let key = iroh::PublicKey::from_bytes(&peer).map_err(transport)?;
    let mut stage = "connect";
    let mut observed_connection = None;
    tracing::info!(target: "data_fabric_transport", remote = %key, bytes = bytes.len(), "TRANSPORT_SEND_BEGIN");
    let result = tokio::time::timeout(TIMEOUT, async {
        let connection = connections.connect(peer, ALPN).await?;
        observed_connection = Some(connection.clone());
        tracing::info!(target: "data_fabric_transport", remote = %key, connection_id = connection.stable_id(), paths = ?connection.paths(), "TRANSPORT_SEND_CONNECTED");
        stage = "open stream";
        let (mut send, mut recv) = connection.open_bi().await.map_err(transport)?;
        // Connection establishment can outlive a policy change. Recheck at
        // outbound admission; already-admitted bytes may remain in flight.
        stage = "recheck authorization";
        let topic = Topic::new(frame.topic.clone())?;
        let routing = routing.lock().await;
        let allowed = match &frame.operation {
            Operation::Publish(_) => {
                routing.recipients(frame.workspace, frame.revision, connections.id(), &topic)?
            }
            Operation::DirectPublish { .. } => routing.direct_recipients(
                frame.workspace,
                frame.revision,
                connections.id(),
                &topic,
                &[peer],
            )?,
            _ => {
                routing.publishers(frame.workspace, frame.revision, connections.id(), &topic)?
            }
        };
        if !allowed.contains(&peer) {
            return Err(Error::Routing(arachne_routing::Error::Denied));
        }
        drop(routing);
        stage = "write frame";
        send.write_all(&[0]).await.map_err(transport)?;
        send.write_all(bytes).await.map_err(transport)?;
        send.finish().map_err(transport)?;
        stage = "read acknowledgment";
        let ack = recv.read_to_end(1).await.map_err(transport)?;
        if ack == [1] {
            Ok(())
        } else {
            Err(Error::Rejected)
        }
    })
    .await
    .map_err(|_| Error::Timeout(stage))
    .and_then(|result| result);
    tracing::info!(target: "data_fabric_transport", remote = %key, stage, ?result, "TRANSPORT_SEND_END");
    if result.is_err() {
        tracing::info!(target: "data_fabric_transport", remote = %key,
            stats = ?observed_connection.as_ref().map(|connection| connection.stats()),
            "TRANSPORT_SEND_STATS");
    }
    result
}

impl Drop for Node {
    fn drop(&mut self) {
        self.resources.stop();
        self.events.close();
        self.listener.abort();
    }
}

fn transport(error: impl std::fmt::Display) -> Error {
    Error::Transport(error.to_string())
}

async fn apply(
    routing: &Mutex<RoutingTable>,
    events: &DeliveryQueue,
    local: PeerId,
    sender: PeerId,
    received_from: PeerId,
    frame: Frame,
) -> Result<()> {
    let topic = Topic::new(frame.topic)?;
    let delivery = frame.delivery;
    let mut routing = routing.lock().await;
    match frame.operation {
        Operation::Subscribe => {
            routing.subscribe(frame.workspace, frame.revision, sender, topic)?
        }
        Operation::Unsubscribe => {
            routing.unsubscribe(frame.workspace, frame.revision, sender, &topic)?
        }
        Operation::Publish(payload) => {
            if payload.len() > MAX_PAYLOAD {
                return Err(Error::TooLarge);
            }
            let recipients = routing.recipients(frame.workspace, frame.revision, sender, &topic)?;
            if !recipients.contains(&local) {
                return Err(Error::Rejected);
            }
            events.push(Message {
                workspace: frame.workspace,
                revision: frame.revision,
                sender,
                received_from,
                topic,
                payload,
                recipients: Vec::new(),
                delivery,
            })?;
        }
        Operation::DirectPublish {
            payload,
            recipients,
        } => {
            if payload.len() > MAX_PAYLOAD
                || recipients.is_empty()
                || recipients.len() > MAX_RECIPIENTS
                || recipients.windows(2).any(|pair| pair[0] >= pair[1])
            {
                return Err(Error::Rejected);
            }
            let recipient_endpoints = routing.direct_recipients(
                frame.workspace,
                frame.revision,
                sender,
                &topic,
                &[local],
            )?;
            if recipient_endpoints != [local] {
                return Err(Error::Rejected);
            }
            events.push(Message {
                workspace: frame.workspace,
                revision: frame.revision,
                sender,
                received_from,
                topic,
                payload,
                recipients,
                delivery,
            })?;
        }
    }
    Ok(())
}

#[tokio::test]
async fn receiver_checks_local_interest_even_if_sender_routes_a_direct_frame() {
    let routing = Mutex::new(RoutingTable::default());
    let events = DeliveryQueue::default();
    let mut received = MessageReceiver {
        queue: events.clone(),
    };
    let topic = Topic::new("streams/opaque").unwrap();
    routing
        .lock()
        .await
        .install_verified_policy(
            [9; 32],
            1,
            BTreeMap::from([
                ([1; 32], Permissions::AllTopics),
                ([2; 32], Permissions::AllTopics),
            ]),
        )
        .unwrap();
    let frame = || Frame {
        workspace: [9; 32],
        revision: 1,
        topic: topic.as_str().into(),
        delivery: DeliveryClass::Critical,
        operation: Operation::DirectPublish {
            payload: vec![0, 255],
            recipients: vec![[3; 32]],
        },
    };
    assert!(
        apply(&routing, &events, [2; 32], [1; 32], [1; 32], frame())
            .await
            .is_err(),
        "receiver admitted a direct frame without local interest"
    );
    routing
        .lock()
        .await
        .subscribe([9; 32], 1, [2; 32], topic.clone())
        .unwrap();
    apply(&routing, &events, [2; 32], [1; 32], [1; 32], frame())
        .await
        .unwrap();
    assert_eq!(received.recv().await.unwrap().payload, vec![0, 255]);
    routing
        .lock()
        .await
        .unsubscribe([9; 32], 1, [2; 32], &topic)
        .unwrap();
    assert!(
        apply(&routing, &events, [2; 32], [1; 32], [1; 32], frame())
            .await
            .is_err(),
        "stale sender interest defeated local unsubscribe"
    );
    assert!(received.try_recv().is_err());
}