murmer 0.3.0

A distributed actor framework for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
pub mod certs;
pub mod config;
pub mod discovery;
pub mod error;
pub mod framing;
pub mod membership;
pub mod remote;
pub mod sync;
pub mod transport;

use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::{broadcast, mpsc, oneshot};
use tokio_util::sync::CancellationToken;

use crate::instrument;
use crate::{
    Actor, DispatchRequest, Endpoint, Listing, OpType, ReceptionKey, Receptionist,
    ReceptionistConfig, RemoteDispatch, RemoteInvocation,
};

use config::{ClusterConfig, NodeClass, NodeIdentity};
use error::ClusterError;
use framing::ControlMessage;
use membership::{ClusterEvent, ClusterMembership, FocaRuntime, TimerEvent, spawn_timer_manager};
use sync::{SpawnRegistry, TypeRegistry};
use transport::{ConnectionEvent, Transport};

// =============================================================================
// NODE REGISTRY — tracks node class and metadata from handshakes
// =============================================================================

/// Stores class and metadata for each connected node, populated during handshake.
///
/// This information is not part of the SWIM protocol — it comes from the QUIC
/// handshake and is only available for nodes we've directly connected to.
#[derive(Debug, Clone)]
pub struct NodeRegistryEntry {
    pub class: NodeClass,
    pub metadata: HashMap<String, String>,
    /// True only when the peer used `Transport::connect_only()` — a pure Edge
    /// client that doesn't host actors and shouldn't participate in SWIM.
    ///
    /// Distinct from `class`: a node may have `class = NodeClass::Edge` while
    /// still being a full server-mode cluster member.
    pub is_edge_client: bool,
}

/// Thread-safe registry of node capabilities, populated during connection setup.
#[derive(Debug, Clone, Default)]
pub struct NodeRegistry {
    nodes: Arc<std::sync::RwLock<HashMap<String, NodeRegistryEntry>>>,
}

impl NodeRegistry {
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a node's class, metadata, and edge-client flag (called during handle_new_connection).
    pub fn insert(
        &self,
        node_id: &str,
        class: NodeClass,
        metadata: HashMap<String, String>,
        is_edge_client: bool,
    ) {
        self.nodes.write().unwrap().insert(
            node_id.to_string(),
            NodeRegistryEntry {
                class,
                metadata,
                is_edge_client,
            },
        );
    }

    /// Look up a node's class and metadata.
    pub fn get(&self, node_id: &str) -> Option<NodeRegistryEntry> {
        self.nodes.read().unwrap().get(node_id).cloned()
    }

    /// Remove a node entry (called when a node departs/fails).
    pub fn remove(&self, node_id: &str) {
        self.nodes.write().unwrap().remove(node_id);
    }
}

// =============================================================================
// CLUSTER SYSTEM — the main entry point for clustered actor systems
// =============================================================================

/// A clustered actor system. Manages local actors, QUIC transport, SWIM
/// membership, mDNS/seed discovery, and OpLog-based registry replication.
pub struct ClusterSystem {
    receptionist: Receptionist,
    transport: Arc<Transport>,
    #[allow(dead_code)]
    config: ClusterConfig,
    identity: NodeIdentity,
    event_tx: broadcast::Sender<ClusterEvent>,
    #[allow(dead_code)]
    type_registry: Arc<TypeRegistry>,
    spawn_registry: Arc<SpawnRegistry>,
    node_registry: NodeRegistry,
    shutdown: CancellationToken,
}

impl ClusterSystem {
    /// Start a new clustered actor system.
    ///
    /// This binds the QUIC transport, starts discovery, initializes SWIM
    /// membership, and spawns the main event loop.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let config = ClusterConfig::builder()
    ///     .listen("127.0.0.1:0".parse()?)
    ///     .cookie("secret")
    ///     .build()?;
    /// let system = ClusterSystem::start(config, TypeRegistry::from_auto(), SpawnRegistry::new()).await?;
    /// let ep = system.start_actor("counter/0", Counter, CounterState { count: 0 });
    /// ```
    pub async fn start(
        config: ClusterConfig,
        type_registry: TypeRegistry,
        spawn_registry: SpawnRegistry,
    ) -> Result<Self, ClusterError> {
        let identity = config.identity.clone();
        let shutdown = CancellationToken::new();
        let (event_tx, _) = broadcast::channel(256);

        // Receptionist with this node's identity
        let receptionist = Receptionist::with_config(ReceptionistConfig {
            node_id: identity.node_id_string(),
            origin_addr: format!("{}:{}", identity.host, identity.port),
            ..Default::default()
        });

        // Type manifest from the registry
        let type_manifest = type_registry.known_types();

        // Bind QUIC transport with tuned parameters
        let (transport, incoming_rx, connection_events_rx) = Transport::bind(
            identity.clone(),
            config.cookie.clone(),
            type_manifest,
            config.node_class.clone(),
            config.node_metadata.clone(),
            config.transport.clone(),
            shutdown.clone(),
        )
        .await?;

        // Start discovery
        let discovery_rx =
            discovery::start_discovery(&identity, &config.discovery, shutdown.clone());

        // Initialize SWIM membership
        let membership = ClusterMembership::new(identity.clone(), event_tx.clone());

        let type_registry = Arc::new(type_registry);
        let spawn_registry = Arc::new(spawn_registry);
        let node_registry = NodeRegistry::new();

        // Spawn the main event loop
        let system = Self {
            receptionist: receptionist.clone(),
            transport: Arc::clone(&transport),
            config: config.clone(),
            identity: identity.clone(),
            event_tx: event_tx.clone(),
            type_registry: Arc::clone(&type_registry),
            spawn_registry: Arc::clone(&spawn_registry),
            node_registry: node_registry.clone(),
            shutdown: shutdown.clone(),
        };

        spawn_event_loop(
            receptionist,
            transport,
            membership,
            identity,
            event_tx,
            type_registry,
            spawn_registry,
            node_registry,
            incoming_rx,
            discovery_rx,
            connection_events_rx,
            shutdown,
        );

        tracing::info!("ClusterSystem started: {}", system.identity);

        Ok(system)
    }

    /// Start a local actor and register it with the receptionist.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let ep = cluster.start_actor("worker/0", Worker, WorkerState::default());
    /// ep.send(DoWork { task: "process".into() }).await?;
    /// ```
    pub fn start_actor<A>(&self, label: &str, actor: A, state: A::State) -> Endpoint<A>
    where
        A: Actor + RemoteDispatch + 'static,
    {
        self.receptionist.start(label, actor, state)
    }

    /// Look up an actor by label (local or remote).
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// if let Some(counter) = cluster.lookup::<Counter>("counter/0") {
    ///     let count = counter.send(GetCount).await?;
    /// }
    /// ```
    pub fn lookup<A: Actor + 'static>(&self, label: &str) -> Option<Endpoint<A>> {
        self.receptionist.lookup(label)
    }

    /// Get a listing (subscription) for a reception key.
    pub fn listing<A: Actor + RemoteDispatch + 'static>(
        &self,
        key: &ReceptionKey<A>,
    ) -> Listing<A> {
        self.receptionist.listing(key.clone())
    }

    /// Subscribe to cluster events.
    ///
    /// # Examples
    ///
    /// ```rust,ignore
    /// let mut events = cluster.subscribe_events();
    /// tokio::spawn(async move {
    ///     while let Ok(event) = events.recv().await {
    ///         match event {
    ///             ClusterEvent::NodeJoined(id) => println!("Node joined: {id}"),
    ///             ClusterEvent::NodeLeft(id) => println!("Node left: {id}"),
    ///             _ => {}
    ///         }
    ///     }
    /// });
    /// ```
    pub fn subscribe_events(&self) -> broadcast::Receiver<ClusterEvent> {
        self.event_tx.subscribe()
    }

    /// Get this node's identity.
    pub fn identity(&self) -> &NodeIdentity {
        &self.identity
    }

    /// Get this node's class.
    pub fn node_class(&self) -> &NodeClass {
        &self.config.node_class
    }

    /// Get this node's metadata.
    pub fn node_metadata(&self) -> &std::collections::HashMap<String, String> {
        &self.config.node_metadata
    }

    /// Get a reference to the receptionist.
    pub fn receptionist(&self) -> &Receptionist {
        &self.receptionist
    }

    /// Get a reference to the spawn registry.
    pub fn spawn_registry(&self) -> &Arc<SpawnRegistry> {
        &self.spawn_registry
    }

    /// Access the node registry (class and metadata for connected nodes).
    pub fn node_registry(&self) -> &NodeRegistry {
        &self.node_registry
    }

    /// Access the underlying transport (for orchestration integration).
    pub fn transport(&self) -> &Arc<Transport> {
        &self.transport
    }

    /// Get the actual bound address (useful when binding to port 0).
    pub fn local_addr(&self) -> std::net::SocketAddr {
        self.transport.local_addr()
    }

    /// Shut down the cluster system gracefully.
    ///
    /// Broadcasts a `Departure` message to all connected peers so they can
    /// prune this node's actors immediately instead of waiting for SWIM timeout.
    pub async fn shutdown(&self) {
        // Broadcast departure to all connected peers
        let departure = ControlMessage::Departure(self.identity.clone());
        self.transport.broadcast_control(&departure).await;

        // Small grace period for message delivery
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Cancel the event loop
        self.shutdown.cancel();
    }
}

// =============================================================================
// EVENT LOOP — the main select! loop driving the cluster
// =============================================================================

#[allow(clippy::too_many_arguments)]
fn spawn_event_loop(
    receptionist: Receptionist,
    transport: Arc<Transport>,
    mut membership: ClusterMembership,
    identity: NodeIdentity,
    event_tx: broadcast::Sender<ClusterEvent>,
    type_registry: Arc<TypeRegistry>,
    spawn_registry: Arc<SpawnRegistry>,
    node_registry: NodeRegistry,
    mut incoming_rx: mpsc::UnboundedReceiver<transport::IncomingConnection>,
    mut discovery_rx: mpsc::UnboundedReceiver<discovery::DiscoveryEvent>,
    mut conn_events: mpsc::UnboundedReceiver<transport::ConnectionEvent>,
    shutdown: CancellationToken,
) {
    // Channels for the foca runtime
    let (swim_tx, mut swim_rx) = mpsc::unbounded_channel::<(NodeIdentity, Vec<u8>)>();
    let (timer_tx, mut timer_rx) = mpsc::unbounded_channel::<TimerEvent>();
    let (control_in_tx, mut control_in_rx) = mpsc::unbounded_channel::<(String, ControlMessage)>();

    // Bridge for outbound connections: discovery spawns connect(), sends the
    // resulting IncomingConnection here so the event loop can set up stream
    // acceptance, foca membership, and initial sync — same as for inbound.
    let (connected_tx, mut connected_rx) =
        mpsc::unbounded_channel::<transport::IncomingConnection>();

    // Subscribe to cluster events for NodeLeft pruning
    let mut cluster_event_rx = event_tx.subscribe();

    // Spawn centralized timer manager — replaces per-timer tokio::spawn
    let timer_cmd_tx = spawn_timer_manager(timer_tx);

    let mut runtime = FocaRuntime::new(identity.clone(), event_tx.clone(), swim_tx, timer_cmd_tx);

    // Periodic sync interval
    let mut sync_interval = tokio::time::interval(Duration::from_secs(5));

    // Track addresses we're currently connecting to, to avoid duplicates
    let connecting = Arc::new(tokio::sync::Mutex::new(std::collections::HashSet::<
        SocketAddr,
    >::new()));

    tokio::spawn(async move {
        // Helper closure factored into an inline fn below to handle a new
        // connection identically regardless of inbound vs outbound origin.

        loop {
            tokio::select! {
                // ── Incoming handshaked connections (accepted by accept_loop) ─
                Some(incoming) = incoming_rx.recv() => {
                    handle_new_connection(
                        incoming,
                        &mut membership,
                        &mut runtime,
                        &control_in_tx,
                        &shutdown,
                        &receptionist,
                        &transport,
                        &node_registry,
                    ).await;
                }

                // ── Outbound connections (initiated by discovery) ────────
                Some(incoming) = connected_rx.recv() => {
                    handle_new_connection(
                        incoming,
                        &mut membership,
                        &mut runtime,
                        &control_in_tx,
                        &shutdown,
                        &receptionist,
                        &transport,
                        &node_registry,
                    ).await;
                }

                // ── Discovery events ────────────────────────────────────
                Some(event) = discovery_rx.recv() => {
                    match event {
                        discovery::DiscoveryEvent::PeerDiscovered(addr) => {
                            let transport = Arc::clone(&transport);
                            let connecting = Arc::clone(&connecting);
                            let mut lock = connecting.lock().await;
                            if lock.contains(&addr) {
                                continue;
                            }
                            lock.insert(addr);
                            drop(lock);

                            let connecting2 = Arc::clone(&connecting);
                            let connected_tx = connected_tx.clone();
                            tokio::spawn(async move {
                                match transport.connect(addr).await {
                                    Ok(ic) => {
                                        tracing::info!("Connected to discovered peer: {addr}");
                                        // Feed back into event loop for stream-accept handling
                                        let _ = connected_tx.send(ic);
                                    }
                                    Err(e) => {
                                        tracing::debug!("Failed to connect to {addr}: {e}");
                                    }
                                }
                                connecting2.lock().await.remove(&addr);
                            });
                        }
                    }
                }

                // ── Foca timer events ───────────────────────────────────
                Some(timer_event) = timer_rx.recv() => {
                    let _ = membership.foca.handle_timer(
                        timer_event.timer,
                        &mut runtime,
                    );
                }

                // ── Outbound SWIM data ──────────────────────────────────
                Some((target, data)) = swim_rx.recv() => {
                    let node_id = target.node_id_string();
                    let msg = ControlMessage::Swim(data);
                    if let Err(e) = transport.send_control(&node_id, msg).await {
                        tracing::trace!("Failed to send SWIM to {node_id}: {e}");
                    }
                }

                // ── Incoming control messages ───────────────────────────
                Some((node_id, msg)) = control_in_rx.recv() => {
                    match msg {
                        ControlMessage::Swim(data) => {
                            let _ = membership.foca.handle_data(
                                &data,
                                &mut runtime,
                            );
                        }
                        ControlMessage::RegistrySync(ops) => {
                            sync::apply_remote_ops(
                                ops.clone(),
                                &receptionist,
                                &type_registry,
                                &node_id,
                                &event_tx,
                                &transport,
                            );

                            // Eager connect: if ops mention nodes we're not
                            // connected to, initiate a connection immediately.
                            let connected = transport.connected_nodes().await;
                            for op in &ops {
                                if let OpType::Register { origin_addr, .. } = &op.op_type {
                                    // Skip ops from our own node
                                    if op.node_id == receptionist.node_id() {
                                        continue;
                                    }
                                    // Skip if already connected to this node
                                    if connected.contains(&op.node_id) {
                                        continue;
                                    }
                                    if let Ok(addr) = origin_addr.parse::<SocketAddr>() {
                                        let mut lock = connecting.lock().await;
                                        if lock.contains(&addr) {
                                            continue;
                                        }
                                        lock.insert(addr);
                                        drop(lock);

                                        let transport_clone = Arc::clone(&transport);
                                        let connecting_clone = Arc::clone(&connecting);
                                        let connected_tx = connected_tx.clone();
                                        tokio::spawn(async move {
                                            match transport_clone.connect(addr).await {
                                                Ok(ic) => {
                                                    tracing::info!(
                                                        "Eager connect to {addr} succeeded"
                                                    );
                                                    let _ = connected_tx.send(ic);
                                                }
                                                Err(e) => {
                                                    tracing::debug!(
                                                        "Eager connect to {addr} failed: {e}"
                                                    );
                                                }
                                            }
                                            connecting_clone.lock().await.remove(&addr);
                                        });
                                    }
                                }
                            }
                        }
                        ControlMessage::RegistrySyncRequest(peer_vv) => {
                            let is_edge = node_registry
                                .get(&node_id)
                                .map(|e| e.is_edge_client)
                                .unwrap_or(false);
                            if is_edge {
                                sync::send_public_sync_to_peer(
                                    &transport,
                                    &receptionist,
                                    &node_id,
                                    &peer_vv,
                                ).await;
                            } else {
                                sync::send_sync_to_peer(
                                    &transport,
                                    &receptionist,
                                    &node_id,
                                    &peer_vv,
                                ).await;
                            }
                        }
                        ControlMessage::Ping => {
                            let _ = transport.send_control(
                                &node_id,
                                ControlMessage::Pong,
                            ).await;
                        }
                        ControlMessage::Pong => {
                            tracing::trace!("Pong from {node_id}");
                        }
                        ControlMessage::Departure(ref identity) => {
                            let departing_id = identity.node_id_string();
                            tracing::info!("Node {departing_id} departing gracefully");
                            instrument::cluster_node_left();
                            let _ = event_tx.send(ClusterEvent::NodeLeft(identity.clone()));
                            receptionist.prune_node(&departing_id);
                            transport.remove_connection(&departing_id).await;
                            let _ = event_tx.send(ClusterEvent::NodePruned(identity.clone()));
                        }
                        ControlMessage::SpawnActor(ref request) => {
                            tracing::info!(
                                "SpawnActor request from {node_id}: label={}, type={}",
                                request.label, request.actor_type_name
                            );
                            match spawn_registry.spawn(
                                receptionist.clone(),
                                &request.label,
                                &request.actor_type_name,
                                &request.initial_state,
                            ).await {
                                Ok(()) => {
                                    tracing::info!(
                                        "Spawned {} (type: {}) successfully",
                                        request.label, request.actor_type_name
                                    );
                                    let _ = transport.send_control(
                                        &node_id,
                                        ControlMessage::SpawnAckOk {
                                            request_id: request.request_id,
                                            label: request.label.clone(),
                                        },
                                    ).await;
                                }
                                Err(e) => {
                                    tracing::warn!(
                                        "Failed to spawn {}: {e}",
                                        request.label
                                    );
                                    let _ = transport.send_control(
                                        &node_id,
                                        ControlMessage::SpawnAckErr {
                                            request_id: request.request_id,
                                            error: e.to_string(),
                                        },
                                    ).await;
                                }
                            }
                        }
                        ControlMessage::SpawnAckOk { request_id, ref label } => {
                            tracing::info!(
                                "SpawnAckOk from {node_id}: request_id={request_id}, label={label}"
                            );
                            let _ = event_tx.send(ClusterEvent::SpawnAckOk {
                                request_id,
                                label: label.clone(),
                            });
                        }
                        ControlMessage::SpawnAckErr { request_id, ref error } => {
                            tracing::warn!(
                                "SpawnAckErr from {node_id}: request_id={request_id}, error={error}"
                            );
                            let _ = event_tx.send(ClusterEvent::SpawnAckErr {
                                request_id,
                                error: error.clone(),
                            });
                        }
                        ControlMessage::StopSingleton { ref label, generation } => {
                            // Cross-node drain: the Coordinator asked this node to
                            // stop the singleton instance it owns. Signal the local
                            // stop and ack. The successor is placed with a strictly
                            // higher generation, so a briefly-lingering old instance
                            // is fenced on its next write.
                            tracing::info!(
                                "StopSingleton from {node_id} for {label} (gen={generation}) — stopping local instance"
                            );
                            receptionist.stop(label);
                            let _ = transport
                                .send_control(
                                    &node_id,
                                    ControlMessage::SingletonStoppedAck {
                                        label: label.clone(),
                                        stopped_generation: generation,
                                    },
                                )
                                .await;
                        }
                        ControlMessage::SingletonStoppedAck { ref label, stopped_generation } => {
                            tracing::info!(
                                "SingletonStoppedAck from {node_id}: label={label}, gen={stopped_generation}"
                            );
                            let _ = event_tx.send(ClusterEvent::SingletonStopped {
                                label: label.clone(),
                                stopped_generation,
                            });
                        }
                        ControlMessage::Handshake(_) => {
                            tracing::warn!("Unexpected handshake from {node_id}");
                        }
                    }
                }

                // ── Cluster events (NodeLeft/NodeFailed pruning) ─────
                Ok(cluster_event) = cluster_event_rx.recv() => {
                    match cluster_event {
                        ClusterEvent::NodeLeft(ref identity) | ClusterEvent::NodeFailed(ref identity) => {
                            let node_id = identity.node_id_string();
                            tracing::info!("Node departed: {node_id} — pruning actors");
                            receptionist.prune_node(&node_id);
                            transport.remove_connection(&node_id).await;
                            node_registry.remove(&node_id);
                            let _ = event_tx.send(ClusterEvent::NodePruned(identity.clone()));
                        }
                        _ => {}
                    }
                }

                // ── Connection events (disconnect handling) ────────────
                Some(event) = conn_events.recv() => {
                    if let ConnectionEvent::Disconnected(ref disconnected_node_id) = event {
                        let was_edge = node_registry
                            .get(disconnected_node_id)
                            .map(|e| e.is_edge_client)
                            .unwrap_or(false);

                        if was_edge {
                            // Silent cleanup — Edge clients are not cluster members.
                            // No SWIM events, no actor pruning, no cluster alarms.
                            node_registry.remove(disconnected_node_id);
                            tracing::debug!("Edge client disconnected: {disconnected_node_id}");
                        } else {
                            tracing::info!(
                                "Connection lost to {disconnected_node_id}, pruning actors"
                            );
                            receptionist.prune_node(disconnected_node_id);
                        }
                    }
                }

                // ── Periodic registry sync ──────────────────────────────
                _ = sync_interval.tick() => {
                    sync::periodic_sync(&transport, &receptionist, &node_registry).await;
                }

                // ── Shutdown ────────────────────────────────────────────
                _ = shutdown.cancelled() => {
                    tracing::info!("ClusterSystem event loop shutting down");
                    break;
                }
            }
        }
    });
}

/// Handles a newly established connection (inbound or outbound):
/// spawns the control stream reader, announces to foca, spawns the
/// stream acceptor for subsequent bi-streams, and requests initial sync.
#[allow(clippy::too_many_arguments)]
async fn handle_new_connection(
    incoming: transport::IncomingConnection,
    membership: &mut ClusterMembership,
    runtime: &mut FocaRuntime,
    control_in_tx: &mpsc::UnboundedSender<(String, ControlMessage)>,
    shutdown: &CancellationToken,
    receptionist: &Receptionist,
    transport: &Arc<Transport>,
    node_registry: &NodeRegistry,
) {
    let node_id = incoming.remote_identity.node_id_string();
    let is_edge_client = incoming.is_edge_client;

    if is_edge_client {
        tracing::info!("Edge client connected: {node_id}");
    } else {
        tracing::info!("New peer connected: {node_id}");
    }

    // Store node class, metadata, and edge-client flag from handshake
    node_registry.insert(
        &node_id,
        incoming.node_class.clone(),
        incoming.node_metadata.clone(),
        is_edge_client,
    );

    // Only add full cluster members to SWIM gossip — Edge clients are not peers
    if !is_edge_client {
        let _ = membership.foca.apply_many(
            std::iter::once(foca::Member::alive(incoming.remote_identity.clone())),
            false,
            runtime,
        );
    }

    // Spawn control stream reader — reads ongoing control messages from
    // the handshake stream (which survived read_handshake).
    tokio::spawn(transport::run_control_stream_reader(
        incoming.control_recv,
        control_in_tx.clone(),
        node_id.clone(),
        shutdown.clone(),
    ));

    // Spawn a task to accept additional bi streams from this connection.
    // New streams are actor streams or control continuations.
    let control_tx = control_in_tx.clone();
    let shutdown_clone = shutdown.clone();
    let conn = incoming.connection.clone();
    let nid = node_id.clone();
    let receptionist_for_streams = receptionist.clone();
    tokio::spawn(async move {
        loop {
            tokio::select! {
                result = conn.accept_bi() => {
                    match result {
                        Ok((send, recv)) => {
                            let receptionist_clone = receptionist_for_streams.clone();
                            let ctrl_tx = control_tx.clone();
                            let nid2 = nid.clone();
                            tokio::spawn(async move {
                                handle_incoming_stream(
                                    receptionist_clone,
                                    send,
                                    recv,
                                    ctrl_tx,
                                    nid2,
                                ).await;
                            });
                        }
                        Err(e) => {
                            tracing::debug!("Peer {nid} stopped accepting streams: {e}");
                            break;
                        }
                    }
                }
                _ = shutdown_clone.cancelled() => break,
            }
        }
    });

    // Only request bidirectional sync for full cluster members.
    // Edge clients send their own RegistrySyncRequest on connect.
    if !is_edge_client {
        sync::request_sync_from_peer(transport, receptionist, &node_id).await;
    }
}

// =============================================================================
// STREAM DISPATCH — determines if an incoming stream is control or actor
// =============================================================================

/// Reads the first frame from an incoming bidirectional stream to determine
/// whether it's a control message or an actor stream (StreamInit).
///
/// We use a simple heuristic: try to decode as both. StreamInit is small and
/// distinctive, while ControlMessages have different structure.
async fn handle_incoming_stream(
    receptionist: Receptionist,
    send: quinn::SendStream,
    mut recv: quinn::RecvStream,
    control_tx: mpsc::UnboundedSender<(String, ControlMessage)>,
    node_id: String,
) {
    let mut codec = framing::FrameCodec::new();
    let mut buf = vec![0u8; 8192];

    // Read until we get the first frame
    let first_frame = loop {
        match recv.read(&mut buf).await {
            Ok(Some(n)) => {
                codec.push_data(&buf[..n]);
                if let Ok(Some(frame)) = codec.next_frame() {
                    break frame;
                }
            }
            Ok(None) | Err(_) => return,
        }
    };

    // Try StreamInit first (actor stream)
    if let Ok(init) = framing::decode_message::<framing::StreamInit>(&first_frame) {
        tracing::debug!("Incoming actor stream for: {}", init.actor_label);
        // Reconstruct the state: the first frame was StreamInit, now we need
        // to continue reading invocations. We'll create a wrapper that
        // delegates to handle_actor_stream but the StreamInit is already consumed.
        handle_actor_stream_after_init(receptionist, send, recv, codec, &init.actor_label).await;
        return;
    }

    // Otherwise, treat as control message
    if let Ok(msg) = framing::decode_message::<ControlMessage>(&first_frame) {
        let _ = control_tx.send((node_id.clone(), msg));
        // Continue reading control messages from this stream
        transport::run_control_stream_reader(recv, control_tx, node_id, CancellationToken::new())
            .await;
    } else {
        tracing::warn!("Could not decode first frame from {node_id}");
    }
}

/// Handle an actor stream where StreamInit has already been consumed.
async fn handle_actor_stream_after_init(
    receptionist: Receptionist,
    mut send: quinn::SendStream,
    mut recv: quinn::RecvStream,
    mut codec: framing::FrameCodec,
    actor_label: &str,
) {
    let dispatch_tx = match receptionist.get_dispatch_sender(actor_label) {
        Some(tx) => tx,
        None => {
            tracing::warn!("Actor stream for unknown actor: {actor_label}");
            let frame =
                framing::encode_response_frame(0, &Err(format!("actor not found: {actor_label}")));
            let _ = send.write_all(&frame).await;
            return;
        }
    };

    let mut buf = vec![0u8; 8192];

    // Process any frames already buffered in the codec from the initial read
    while let Ok(Some(frame)) = codec.next_frame() {
        if !dispatch_and_respond(&dispatch_tx, &mut send, &frame, actor_label).await {
            return;
        }
    }

    // Continue reading
    loop {
        match recv.read(&mut buf).await {
            Ok(Some(n)) => {
                codec.push_data(&buf[..n]);
                while let Ok(Some(frame)) = codec.next_frame() {
                    if !dispatch_and_respond(&dispatch_tx, &mut send, &frame, actor_label).await {
                        return;
                    }
                }
            }
            Ok(None) => break,
            Err(e) => {
                tracing::warn!("Actor stream read error for {actor_label}: {e}");
                break;
            }
        }
    }
}

/// Decode an invocation frame (lean wire format), dispatch it, await the
/// response, and write it back. Returns false if the stream should be closed.
async fn dispatch_and_respond(
    dispatch_tx: &mpsc::UnboundedSender<DispatchRequest>,
    send: &mut quinn::SendStream,
    frame: &[u8],
    actor_label: &str,
) -> bool {
    let decoded = match framing::decode_invocation_frame(frame) {
        Ok(d) => d,
        Err(e) => {
            tracing::warn!("Failed to decode invocation for {actor_label}: {e}");
            return true; // skip this frame but keep stream open
        }
    };

    let (resp_tx, resp_rx) = oneshot::channel();
    let request = DispatchRequest {
        invocation: RemoteInvocation {
            call_id: decoded.call_id,
            actor_label: actor_label.to_string(),
            message_type: decoded.message_type.to_string(),
            payload: decoded.payload.to_vec(),
        },
        respond_to: resp_tx,
    };

    if dispatch_tx.send(request).is_err() {
        tracing::warn!("Dispatch channel closed for {actor_label}");
        return false;
    }

    match resp_rx.await {
        Ok(response) => {
            let frame = framing::encode_response_frame(response.call_id, &response.result);
            if let Err(e) = send.write_all(&frame).await {
                tracing::warn!("Failed to send response for {actor_label}: {e}");
                return false;
            }
        }
        Err(_) => {
            tracing::warn!("Response channel dropped for {actor_label}");
            // Keep stream open — actor may have been restarted
        }
    }

    true
}

// =============================================================================
// INTEGRATION TESTS
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        Actor, ActorContext, DispatchError, Handler, Message, RemoteDispatch, RemoteMessage,
        ResponseRegistry, Router, RoutingStrategy,
    };
    use config::{ClusterConfig, ClusterConfigBuilder, Discovery};
    use serde::{Deserialize, Serialize};
    use std::time::Duration;
    use sync::TypeRegistry;

    // ── Test actor (manual impls, no proc-macro dependency) ──────────

    #[derive(Debug)]
    struct TestCounter;

    struct TestCounterState {
        count: i64,
    }

    impl Actor for TestCounter {
        type State = TestCounterState;
    }

    // Messages

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct Increment {
        amount: i64,
    }
    impl Message for Increment {
        type Result = i64;
    }
    impl RemoteMessage for Increment {
        const TYPE_ID: &'static str = "test::Increment";
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct GetCount;
    impl Message for GetCount {
        type Result = i64;
    }
    impl RemoteMessage for GetCount {
        const TYPE_ID: &'static str = "test::GetCount";
    }

    // Handler impls

    impl Handler<Increment> for TestCounter {
        fn handle(
            &mut self,
            _ctx: &ActorContext<Self>,
            state: &mut TestCounterState,
            msg: Increment,
        ) -> i64 {
            state.count += msg.amount;
            state.count
        }
    }

    impl Handler<GetCount> for TestCounter {
        fn handle(
            &mut self,
            _ctx: &ActorContext<Self>,
            state: &mut TestCounterState,
            _msg: GetCount,
        ) -> i64 {
            state.count
        }
    }

    impl RemoteDispatch for TestCounter {
        async fn dispatch_remote<'a>(
            &'a mut self,
            ctx: &'a ActorContext<Self>,
            state: &'a mut TestCounterState,
            message_type: &'a str,
            payload: &'a [u8],
        ) -> Result<Vec<u8>, DispatchError> {
            match message_type {
                "test::Increment" => {
                    let (msg, _): (Increment, _) =
                        bincode::serde::decode_from_slice(payload, bincode::config::standard())
                            .map_err(|e| DispatchError::DeserializeFailed(e.to_string()))?;
                    let result = <Self as Handler<Increment>>::handle(self, ctx, state, msg);
                    bincode::serde::encode_to_vec(result, bincode::config::standard())
                        .map_err(|e| DispatchError::SerializeFailed(e.to_string()))
                }
                "test::GetCount" => {
                    let (msg, _): (GetCount, _) =
                        bincode::serde::decode_from_slice(payload, bincode::config::standard())
                            .map_err(|e| DispatchError::DeserializeFailed(e.to_string()))?;
                    let result = <Self as Handler<GetCount>>::handle(self, ctx, state, msg);
                    bincode::serde::encode_to_vec(result, bincode::config::standard())
                        .map_err(|e| DispatchError::SerializeFailed(e.to_string()))
                }
                other => Err(DispatchError::UnknownMessageType(other.to_string())),
            }
        }
    }

    // ── Helpers ──────────────────────────────────────────────────────

    fn test_type_registry() -> TypeRegistry {
        let mut reg = TypeRegistry::new();
        reg.register(
            "murmer::cluster::tests::TestCounter",
            Box::new(
                |receptionist: &crate::Receptionist,
                 label: &str,
                 wire_tx: mpsc::UnboundedSender<crate::RemoteInvocation>,
                 response_registry: ResponseRegistry,
                 node_id: &str,
                 visibility: crate::receptionist::Visibility| {
                    receptionist.register_remote_from_node::<TestCounter>(
                        label,
                        wire_tx,
                        response_registry,
                        node_id,
                        visibility,
                    );
                },
            ),
        );
        reg
    }

    fn test_config(name: &str) -> ClusterConfig {
        ClusterConfigBuilder::new()
            .name(name)
            .listen("127.0.0.1:0".parse::<std::net::SocketAddr>().unwrap())
            .cookie("test-cookie")
            .discovery(Discovery::None)
            .build()
            .unwrap()
    }

    fn test_config_with_seed(name: &str, seed: std::net::SocketAddr) -> ClusterConfig {
        ClusterConfigBuilder::new()
            .name(name)
            .listen("127.0.0.1:0".parse::<std::net::SocketAddr>().unwrap())
            .cookie("test-cookie")
            .seed_nodes([seed])
            .build()
            .unwrap()
    }

    /// Install rustls ring crypto provider (idempotent).
    fn init_crypto() {
        let _ = rustls::crypto::ring::default_provider().install_default();
    }

    /// Polls a closure until it returns true, with a timeout.
    async fn poll_until<F, Fut>(timeout: Duration, mut f: F)
    where
        F: FnMut() -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let deadline = tokio::time::Instant::now() + timeout;
        loop {
            if f().await {
                return;
            }
            if tokio::time::Instant::now() >= deadline {
                panic!("poll_until timed out after {timeout:?}");
            }
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
    }

    // ── Tests ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_two_node_handshake() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        let config_a = test_config("node-a");

        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        // Node B uses A as a seed
        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        // Wait for the connection to establish (discovery fires, connect happens)
        let transport_a = &system_a.transport;
        poll_until(Duration::from_secs(5), || async {
            !transport_a.connected_nodes().await.is_empty()
        })
        .await;

        // Both should see each other
        let a_peers = system_a.transport.connected_nodes().await;
        let b_peers = system_b.transport.connected_nodes().await;
        assert!(!a_peers.is_empty(), "A should have peers");
        assert!(!b_peers.is_empty(), "B should have peers");

        system_a.shutdown().await;
        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_end_to_end_remote_messaging() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        // Start node A with a counter actor
        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        let local_ep =
            system_a.start_actor("counter/main", TestCounter, TestCounterState { count: 0 });

        // Verify local works
        let result = local_ep.send(Increment { amount: 5 }).await.unwrap();
        assert_eq!(result, 5);

        // Start node B with A as seed
        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        // Wait for registry sync to propagate the actor
        poll_until(Duration::from_secs(10), || {
            let receptionist = system_b.receptionist().clone();
            async move { receptionist.lookup::<TestCounter>("counter/main").is_some() }
        })
        .await;

        // Look up the remote actor from node B and send a message
        let remote_ep = system_b
            .lookup::<TestCounter>("counter/main")
            .expect("remote actor should be discoverable");

        let result = remote_ep.send(GetCount).await.unwrap();
        assert_eq!(result, 5, "remote GetCount should return 5");

        let result = remote_ep.send(Increment { amount: 10 }).await.unwrap();
        assert_eq!(result, 15, "remote Increment should return 15");

        system_a.shutdown().await;
        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_bidirectional_messaging() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        // Node A has counter/a, Node B has counter/b
        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        system_a.start_actor("counter/a", TestCounter, TestCounterState { count: 100 });

        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        system_b.start_actor("counter/b", TestCounter, TestCounterState { count: 200 });

        // Wait for both actors to be visible from both sides
        poll_until(Duration::from_secs(10), || {
            let ra = system_a.receptionist().clone();
            let rb = system_b.receptionist().clone();
            async move {
                ra.lookup::<TestCounter>("counter/b").is_some()
                    && rb.lookup::<TestCounter>("counter/a").is_some()
            }
        })
        .await;

        // Node A sends to node B's actor
        let ep_b_from_a = system_a
            .lookup::<TestCounter>("counter/b")
            .expect("B's actor visible from A");
        let result = ep_b_from_a.send(GetCount).await.unwrap();
        assert_eq!(result, 200);

        // Node B sends to node A's actor
        let ep_a_from_b = system_b
            .lookup::<TestCounter>("counter/a")
            .expect("A's actor visible from B");
        let result = ep_a_from_b.send(GetCount).await.unwrap();
        assert_eq!(result, 100);

        system_a.shutdown().await;
        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_node_failure_cleanup() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        system_a.start_actor(
            "counter/ephemeral",
            TestCounter,
            TestCounterState { count: 42 },
        );

        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        // Wait for the actor to be visible from B
        poll_until(Duration::from_secs(10), || {
            let r = system_b.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/ephemeral").is_some() }
        })
        .await;

        assert!(
            system_b
                .lookup::<TestCounter>("counter/ephemeral")
                .is_some(),
            "actor should be visible before shutdown"
        );

        // Shut down node A
        system_a.shutdown().await;
        // Give it a moment for the connection to drop
        tokio::time::sleep(Duration::from_millis(500)).await;

        // Manually prune (in production SWIM would detect this, but in tests
        // the SWIM protocol may not trigger fast enough)
        let a_node_id = system_a.identity().node_id_string();
        system_b.receptionist().prune_node(&a_node_id);

        // The actor should be gone from B's receptionist
        assert!(
            system_b
                .lookup::<TestCounter>("counter/ephemeral")
                .is_none(),
            "actor should be pruned after node departure"
        );

        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_graceful_departure() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        system_a.start_actor(
            "counter/depart",
            TestCounter,
            TestCounterState { count: 77 },
        );

        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        // Wait for the actor to be visible from B
        poll_until(Duration::from_secs(10), || {
            let r = system_b.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/depart").is_some() }
        })
        .await;

        assert!(
            system_b.lookup::<TestCounter>("counter/depart").is_some(),
            "actor should be visible before departure"
        );

        // Graceful shutdown of node A — should broadcast Departure
        system_a.shutdown().await;

        // Node B should prune A's actors quickly (no SWIM timeout needed)
        poll_until(Duration::from_secs(5), || {
            let r = system_b.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/depart").is_none() }
        })
        .await;

        assert!(
            system_b.lookup::<TestCounter>("counter/depart").is_none(),
            "actor should be pruned after graceful departure"
        );

        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_rejoin_after_departure() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        system_a.start_actor(
            "counter/rejoin",
            TestCounter,
            TestCounterState { count: 10 },
        );

        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_b = system_b.local_addr();

        // Wait for A's actor to be visible from B
        poll_until(Duration::from_secs(10), || {
            let r = system_b.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/rejoin").is_some() }
        })
        .await;

        // Graceful departure of A
        system_a.shutdown().await;

        // Wait for B to prune
        poll_until(Duration::from_secs(5), || {
            let r = system_b.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/rejoin").is_none() }
        })
        .await;

        // Now start a NEW node A and connect it to B (rejoin)
        let config_a2 = test_config_with_seed("node-a-2", addr_b);
        let system_a2 = ClusterSystem::start(config_a2, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        system_a2.start_actor(
            "counter/rejoined",
            TestCounter,
            TestCounterState { count: 20 },
        );

        // Wait for B to see the new actor from the rejoined node
        poll_until(Duration::from_secs(10), || {
            let r = system_b.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/rejoined").is_some() }
        })
        .await;

        // Verify the new actor is accessible
        let ep = system_b
            .lookup::<TestCounter>("counter/rejoined")
            .expect("rejoined actor should be discoverable");
        let count = ep.send(GetCount).await.unwrap();
        assert_eq!(count, 20, "rejoined actor should have correct state");

        system_a2.shutdown().await;
        system_b.shutdown().await;
    }

    // ── RefReceiver actor (for ActorRef-over-QUIC test) ─────────────

    #[derive(Debug)]
    struct RefReceiver;

    struct RefReceiverState;

    impl Actor for RefReceiver {
        type State = RefReceiverState;
    }

    #[derive(Debug, Clone, Serialize, Deserialize)]
    struct SendRef {
        label: String,
        node_id: String,
    }

    impl Message for SendRef {
        type Result = String;
    }

    impl RemoteMessage for SendRef {
        const TYPE_ID: &'static str = "test::SendRef";
    }

    impl Handler<SendRef> for RefReceiver {
        fn handle(
            &mut self,
            ctx: &ActorContext<Self>,
            _state: &mut RefReceiverState,
            msg: SendRef,
        ) -> String {
            // Try to resolve the actor by looking up the label in the receptionist
            match ctx.receptionist().lookup::<TestCounter>(&msg.label) {
                Some(_) => msg.label,
                None => "not found".to_string(),
            }
        }
    }

    impl RemoteDispatch for RefReceiver {
        async fn dispatch_remote<'a>(
            &'a mut self,
            ctx: &'a ActorContext<Self>,
            state: &'a mut RefReceiverState,
            message_type: &'a str,
            payload: &'a [u8],
        ) -> Result<Vec<u8>, DispatchError> {
            match message_type {
                "test::SendRef" => {
                    let (msg, _): (SendRef, _) =
                        bincode::serde::decode_from_slice(payload, bincode::config::standard())
                            .map_err(|e| DispatchError::DeserializeFailed(e.to_string()))?;
                    let result = <Self as Handler<SendRef>>::handle(self, ctx, state, msg);
                    bincode::serde::encode_to_vec(result, bincode::config::standard())
                        .map_err(|e| DispatchError::SerializeFailed(e.to_string()))
                }
                other => Err(DispatchError::UnknownMessageType(other.to_string())),
            }
        }
    }

    /// Type registry that includes both TestCounter and RefReceiver.
    fn extended_type_registry() -> TypeRegistry {
        let mut reg = test_type_registry();
        reg.register(
            "murmer::cluster::tests::RefReceiver",
            Box::new(
                |receptionist: &crate::Receptionist,
                 label: &str,
                 wire_tx: mpsc::UnboundedSender<crate::RemoteInvocation>,
                 response_registry: ResponseRegistry,
                 node_id: &str,
                 visibility: crate::receptionist::Visibility| {
                    receptionist.register_remote_from_node::<RefReceiver>(
                        label,
                        wire_tx,
                        response_registry,
                        node_id,
                        visibility,
                    );
                },
            ),
        );
        reg
    }

    // ── New integration tests ───────────────────────────────────────

    #[tokio::test]
    async fn test_router_with_remote_endpoints() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        // Node A: start 2 TestCounter actors
        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        system_a.start_actor("counter/r1", TestCounter, TestCounterState { count: 0 });
        system_a.start_actor("counter/r2", TestCounter, TestCounterState { count: 0 });

        // Node B: connect to A as seed
        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        // Wait for both actors to appear on B via poll_until
        poll_until(Duration::from_secs(10), || {
            let r = system_b.receptionist().clone();
            async move {
                r.lookup::<TestCounter>("counter/r1").is_some()
                    && r.lookup::<TestCounter>("counter/r2").is_some()
            }
        })
        .await;

        // On B: look up both remote endpoints
        let ep1 = system_b
            .lookup::<TestCounter>("counter/r1")
            .expect("counter/r1 should be visible from B");
        let ep2 = system_b
            .lookup::<TestCounter>("counter/r2")
            .expect("counter/r2 should be visible from B");

        // Create Router with RoundRobin strategy
        let router = Router::new(vec![ep1, ep2], RoutingStrategy::RoundRobin);

        // Send 4 Increment { amount: 1 } through the router
        // RoundRobin: msg 0 → ep1, msg 1 → ep2, msg 2 → ep1, msg 3 → ep2
        for _ in 0..4 {
            router
                .send(Increment { amount: 1 })
                .await
                .expect("router send should succeed");
        }

        // Verify via GetCount: each should have count 2
        let ep1_check = system_b
            .lookup::<TestCounter>("counter/r1")
            .expect("counter/r1 still visible");
        let ep2_check = system_b
            .lookup::<TestCounter>("counter/r2")
            .expect("counter/r2 still visible");

        let count1 = ep1_check.send(GetCount).await.unwrap();
        let count2 = ep2_check.send(GetCount).await.unwrap();
        assert_eq!(count1, 2, "counter/r1 should have count 2");
        assert_eq!(count2, 2, "counter/r2 should have count 2");

        system_a.shutdown().await;
        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_actor_ref_passed_over_quic() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        // Node A: start "counter/ref-target" (TestCounter, count=0)
        let config_a = test_config("node-a");
        let system_a =
            ClusterSystem::start(config_a, extended_type_registry(), SpawnRegistry::new())
                .await
                .unwrap();
        let addr_a = system_a.local_addr();
        let node_a_id = system_a.identity().node_id_string();

        system_a.start_actor(
            "counter/ref-target",
            TestCounter,
            TestCounterState { count: 0 },
        );

        // Node B: start "ref-receiver/main" (RefReceiver)
        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b =
            ClusterSystem::start(config_b, extended_type_registry(), SpawnRegistry::new())
                .await
                .unwrap();

        system_b.start_actor("ref-receiver/main", RefReceiver, RefReceiverState);

        // Wait for cross-sync (both actors visible on both nodes)
        poll_until(Duration::from_secs(10), || {
            let ra = system_a.receptionist().clone();
            let rb = system_b.receptionist().clone();
            async move {
                ra.lookup::<RefReceiver>("ref-receiver/main").is_some()
                    && rb.lookup::<TestCounter>("counter/ref-target").is_some()
            }
        })
        .await;

        // From node A: look up "ref-receiver/main" (remote endpoint on B)
        let ref_receiver_ep = system_a
            .lookup::<RefReceiver>("ref-receiver/main")
            .expect("ref-receiver/main should be visible from A");

        // Send SendRef — RefReceiver on B will try to resolve "counter/ref-target"
        // in its local receptionist (which should have it via sync)
        let result = ref_receiver_ep
            .send(SendRef {
                label: "counter/ref-target".to_string(),
                node_id: node_a_id,
            })
            .await
            .unwrap();

        assert_eq!(
            result, "counter/ref-target",
            "RefReceiver should resolve the actor ref via its receptionist"
        );

        system_a.shutdown().await;
        system_b.shutdown().await;
    }

    #[tokio::test]
    async fn test_three_node_linear_chain() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        // Topology: A <--> B <--> C (linear chain).
        // This tests that:
        // 1. B can see actors on A (direct peer)
        // 2. C can see actors on B (direct peer)
        // 3. C can discover A's actors transitively (B re-records A's ops in its oplog)
        //
        // NOTE: C's remote endpoint for A's actor is set up with B as the
        // relay node. Since B doesn't have A's actor locally, the QUIC stream
        // from C→B for that actor will fail. This is a known limitation:
        // intermediate routing is not yet implemented. We verify discovery only.

        // Node A: start "counter/on-a"
        let config_a = test_config("node-a");
        let system_a = ClusterSystem::start(config_a, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_a = system_a.local_addr();

        system_a.start_actor("counter/on-a", TestCounter, TestCounterState { count: 10 });

        // Node B: seed = A, start "counter/on-b"
        let config_b = test_config_with_seed("node-b", addr_a);
        let system_b = ClusterSystem::start(config_b, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();
        let addr_b = system_b.local_addr();

        system_b.start_actor("counter/on-b", TestCounter, TestCounterState { count: 20 });

        // Wait for A-B bidirectional sync
        poll_until(Duration::from_secs(10), || {
            let ra = system_a.receptionist().clone();
            let rb = system_b.receptionist().clone();
            async move {
                ra.lookup::<TestCounter>("counter/on-b").is_some()
                    && rb.lookup::<TestCounter>("counter/on-a").is_some()
            }
        })
        .await;

        // B can message A (direct peer)
        let ep_a_from_b = system_b
            .lookup::<TestCounter>("counter/on-a")
            .expect("A's actor visible from B");
        let count = ep_a_from_b.send(GetCount).await.unwrap();
        assert_eq!(count, 10, "B reads A's counter directly");

        // A can message B (direct peer)
        let ep_b_from_a = system_a
            .lookup::<TestCounter>("counter/on-b")
            .expect("B's actor visible from A");
        let count = ep_b_from_a.send(GetCount).await.unwrap();
        assert_eq!(count, 20, "A reads B's counter directly");

        // Node C: seed = B only (no direct connection to A)
        let config_c = test_config_with_seed("node-c", addr_b);
        let system_c = ClusterSystem::start(config_c, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        // C can see B's own actor quickly (direct peer)
        poll_until(Duration::from_secs(10), || {
            let r = system_c.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/on-b").is_some() }
        })
        .await;

        // C should be able to message B (direct peer)
        let ep_b_from_c = system_c
            .lookup::<TestCounter>("counter/on-b")
            .expect("B's actor visible from C");
        let count = ep_b_from_c.send(GetCount).await.unwrap();
        assert_eq!(count, 20, "C reads B's counter directly");

        // C discovers A's actor transitively through B (B re-records A's registration
        // in its own oplog via register_remote_from_node). This may take a sync cycle.
        poll_until(Duration::from_secs(15), || {
            let r = system_c.receptionist().clone();
            async move { r.lookup::<TestCounter>("counter/on-a").is_some() }
        })
        .await;

        // Verify transitive discovery: C's receptionist knows about A's actor
        assert!(
            system_c.lookup::<TestCounter>("counter/on-a").is_some(),
            "counter/on-a should be discovered on C via transitive sync through B"
        );

        // NOTE: Sending from C to A's actor through the remote endpoint will
        // fail because the endpoint routes through B's QUIC connection, and
        // B doesn't have "counter/on-a" as a local actor (it's a remote entry).
        // This is expected behavior — intermediate message routing (C→B→A) is
        // a future feature. For now, we verify that discovery propagates transitively.

        system_a.shutdown().await;
        system_b.shutdown().await;
        system_c.shutdown().await;
    }

    #[tokio::test]
    async fn test_concurrent_send_stress() {
        let _ = tracing_subscriber::fmt().with_test_writer().try_init();
        init_crypto();

        // This test uses the cluster infrastructure for a single node to
        // stress-test the mailbox concurrency safety with many concurrent senders.
        let config = test_config("stress-node");
        let system = ClusterSystem::start(config, test_type_registry(), SpawnRegistry::new())
            .await
            .unwrap();

        let ep = system.start_actor("counter/stress", TestCounter, TestCounterState { count: 0 });

        // Spawn 50 tokio tasks, each sending 20 Increment { amount: 1 }
        let mut handles = Vec::new();
        for _ in 0..50 {
            let ep = ep.clone();
            handles.push(tokio::spawn(async move {
                for _ in 0..20 {
                    ep.send(Increment { amount: 1 }).await.unwrap();
                }
            }));
        }

        // Wait for all tasks to complete
        for handle in handles {
            handle.await.unwrap();
        }

        // Query GetCount — must equal 1000
        let count = ep.send(GetCount).await.unwrap();
        assert_eq!(
            count, 1000,
            "50 tasks * 20 increments = 1000 total increments"
        );

        system.shutdown().await;
    }
}