murmer 0.4.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
use std::collections::HashMap;

use bytes::{Buf, BufMut, BytesMut};
use serde::{Deserialize, Serialize};

use super::config::{NodeClass, NodeIdentity};
use crate::{Op, VersionVector};

/// Maximum frame size (4 MB).
const MAX_FRAME_SIZE: usize = 4 * 1024 * 1024;

/// Per-read scratch buffer size for the frame-read loops (handshake, control,
/// response, and actor streams). One place so a tuning change is a single edit
/// rather than a hunt across every stream reader.
pub const FRAME_READ_BUF: usize = 8192;

// =============================================================================
// CONTROL MESSAGES — sent on stream 0 (long-lived, bidirectional)
// =============================================================================

/// Messages exchanged on the control stream (stream 0) of each node connection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ControlMessage {
    /// First message on a new connection — mutual authentication.
    Handshake(HandshakePayload),
    /// Opaque SWIM protocol bytes (foca).
    Swim(Vec<u8>),
    /// OpLog delta: "here are ops you haven't seen".
    RegistrySync(Vec<Op>),
    /// Request peer's delta: "send me ops I haven't seen".
    RegistrySyncRequest(VersionVector),
    /// Keepalive ping.
    Ping,
    /// Keepalive pong.
    Pong,
    /// Graceful departure — "I'm leaving cleanly, don't wait for SWIM timeout".
    Departure(NodeIdentity),
    /// Coordinator instructs a node to spawn an actor locally.
    SpawnActor(SpawnRequest),
    /// Node confirms successful actor spawn.
    SpawnAckOk { request_id: u64, label: String },
    /// Node reports actor spawn failure.
    SpawnAckErr { request_id: u64, error: String },
    /// Coordinator instructs the current owner node to stop a cluster singleton
    /// so ownership can hand off. `generation` is the packed `(term, seq)` the
    /// owner was granted, echoed back in the ack so a superseded owner's late
    /// ack can be ignored.
    StopSingleton { label: String, generation: u64 },
    /// Owner node confirms the singleton has fully stopped (its `DeregisterGuard`
    /// fired). This is the cross-node await-stopped barrier — the analog of a
    /// local drain — that must complete before the new owner is started.
    SingletonStoppedAck {
        label: String,
        stopped_generation: u64,
    },
}

/// A request to spawn an actor on a remote node.
///
/// Sent by the Coordinator over the control stream. The receiving node
/// looks up the `actor_type_name` in its `SpawnRegistry`, deserializes
/// the state bytes, and calls `receptionist.start()`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpawnRequest {
    /// Unique identifier for correlating ack responses.
    pub request_id: u64,
    /// Label to register the actor under.
    pub label: String,
    /// Key into the SpawnRegistry — identifies the actor type to instantiate.
    pub actor_type_name: String,
    /// Serialized initial state (via MigratableActor).
    pub initial_state: Vec<u8>,
    /// Packed `(term, seq)` placement generation when this spawn is for a fenced
    /// cluster singleton, threaded opaquely to the spawning node so it can stamp
    /// the actor's boot config. `None` for ordinary (non-singleton) spawns.
    ///
    /// Note: `#[serde(default)]` is for forward-friendliness with self-describing
    /// formats; the wire codec is bincode (positional), and all cluster nodes run
    /// the same `PROTOCOL_VERSION`, so genuinely-older byte streams are never
    /// decoded here — `SpawnRequest` is wire-only and never persisted.
    #[serde(default)]
    pub singleton_generation: Option<u64>,
}

/// The handshake payload exchanged when two nodes first connect.
///
/// In addition to authentication (cookie) and capability negotiation
/// (type_manifest, protocol_version), this carries the node's class and
/// metadata so the orchestrator can make placement decisions immediately
/// after a node joins the cluster.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HandshakePayload {
    pub identity: NodeIdentity,
    pub cookie: String,
    pub type_manifest: Vec<String>,
    pub protocol_version: u32,
    /// This node's role in the cluster (Worker, Coordinator, Edge, etc.).
    pub node_class: NodeClass,
    /// Arbitrary key-value metadata describing node capabilities.
    /// Examples: `"region" = "us-west"`, `"gpu" = "true"`, `"rack" = "A3"`.
    pub node_metadata: HashMap<String, String>,
    /// Whether this node is a pure Edge client (connected via `Transport::connect_only`).
    ///
    /// True Edge clients do not host actors, skip SWIM membership, and receive
    /// only public actor ops. Defaults to `false` so old nodes deserialize correctly.
    ///
    /// Distinct from `NodeClass::Edge`: a node can have `node_class = Edge` while
    /// still being a full cluster member (server mode). This flag is only set when
    /// using `Transport::connect_only()`.
    #[serde(default)]
    pub is_edge_client: bool,
}

/// Current protocol version.
pub const PROTOCOL_VERSION: u32 = 1;

// =============================================================================
// ACTOR STREAM MESSAGES — sent on per-actor streams (lazy, bidirectional)
// =============================================================================

/// First message on a new actor stream — tells the receiver which actor
/// this stream is for.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StreamInit {
    pub actor_label: String,
}

// =============================================================================
// FRAME CODEC — stateful length-prefix codec for reading from QUIC streams
// =============================================================================

/// A stateful length-prefix codec. Accumulates bytes from a QUIC stream and
/// yields complete frames.
///
/// Wire format: `[u32 LE length][payload bytes]`
///
/// The length prefix indicates the size of the payload (not including the
/// 4-byte length field itself).
pub struct FrameCodec {
    buffer: BytesMut,
    expected_length: Option<usize>,
}

impl FrameCodec {
    pub fn new() -> Self {
        Self {
            buffer: BytesMut::new(),
            expected_length: None,
        }
    }

    /// Push raw bytes into the codec's internal buffer.
    pub fn push_data(&mut self, data: &[u8]) {
        self.buffer.extend_from_slice(data);
    }

    /// Try to extract the next complete frame. Returns `None` if more data
    /// is needed, or an error if the frame exceeds the size limit.
    pub fn next_frame(&mut self) -> Result<Option<Vec<u8>>, std::io::Error> {
        // Read length prefix if we don't have it yet
        if self.expected_length.is_none() {
            if self.buffer.len() < 4 {
                return Ok(None);
            }
            let length = u32::from_le_bytes([
                self.buffer[0],
                self.buffer[1],
                self.buffer[2],
                self.buffer[3],
            ]) as usize;

            if length > MAX_FRAME_SIZE {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("frame too large: {length} bytes (max {MAX_FRAME_SIZE})"),
                ));
            }
            self.expected_length = Some(length);
        }

        let expected = self.expected_length.unwrap();

        // Wait until we have the full payload
        if self.buffer.len() < 4 + expected {
            return Ok(None);
        }

        // Consume the length prefix + payload
        self.buffer.advance(4);
        let payload = self.buffer.split_to(expected).to_vec();
        self.expected_length = None;

        Ok(Some(payload))
    }

    /// Encode a payload into a length-prefixed frame.
    pub fn encode_frame(data: &[u8]) -> Vec<u8> {
        let mut buf = BytesMut::with_capacity(4 + data.len());
        buf.put_u32_le(data.len() as u32);
        buf.put_slice(data);
        buf.to_vec()
    }
}

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

// =============================================================================
// SERDE HELPERS — encode/decode typed messages to/from frames
// =============================================================================

/// Serialize a value to a length-prefixed frame using bincode.
pub fn encode_message<T: Serialize>(msg: &T) -> Result<Vec<u8>, String> {
    let payload = bincode::serde::encode_to_vec(msg, bincode::config::standard())
        .map_err(|e| e.to_string())?;
    Ok(FrameCodec::encode_frame(&payload))
}

/// Deserialize a value from a raw payload (no length prefix — already stripped).
pub fn decode_message<T: for<'de> Deserialize<'de>>(data: &[u8]) -> Result<T, String> {
    let (val, _): (T, _) = bincode::serde::decode_from_slice(data, bincode::config::standard())
        .map_err(|e| e.to_string())?;
    Ok(val)
}

// =============================================================================
// LEAN WIRE FORMAT — actor stream invocations and responses
// =============================================================================
//
// These functions encode/decode actor stream messages using a custom binary
// format that writes the payload bytes directly into the frame, avoiding the
// double-serialization that occurs when using `encode_message(&RemoteInvocation)`.
//
// With bincode, encoding a `RemoteInvocation { payload: Vec<u8> }` re-encodes
// the already-serialized payload bytes (length prefix + full copy). The lean
// format writes the raw payload bytes directly after the header fields.
//
// Additionally, the lean format omits `actor_label` from every frame — the
// StreamInit message already establishes which actor the stream targets, so
// repeating it per-message is redundant.
//
// Wire format for invocations:
//   [u32 LE frame_length]            — handled by FrameCodec
//   [u64 LE call_id]                 — response correlation ID
//   [u16 LE message_type_len]        — length of the message type string
//   [message_type bytes]             — UTF-8 TYPE_ID (e.g., "counter::Increment")
//   [payload bytes until end]        — raw serialized message (no length prefix)
//
// Wire format for responses:
//   [u32 LE frame_length]            — handled by FrameCodec
//   [u64 LE call_id]                 — matches the invocation's call_id
//   [u8 status]                      — 1 = Ok, 0 = Err
//   [body bytes until end]           — raw result payload (Ok) or UTF-8 error string (Err)

/// Encode an actor stream invocation to a length-prefixed frame.
///
/// Writes the payload bytes directly — no double-serialization.
pub fn encode_invocation_frame(call_id: u64, message_type: &str, payload: &[u8]) -> Vec<u8> {
    let type_bytes = message_type.as_bytes();
    let body_len = 8 + 2 + type_bytes.len() + payload.len();
    let mut buf = Vec::with_capacity(4 + body_len);
    buf.extend_from_slice(&(body_len as u32).to_le_bytes());
    buf.extend_from_slice(&call_id.to_le_bytes());
    buf.extend_from_slice(&(type_bytes.len() as u16).to_le_bytes());
    buf.extend_from_slice(type_bytes);
    buf.extend_from_slice(payload);
    buf
}

/// Decoded invocation from a frame payload.
pub struct DecodedInvocation<'a> {
    pub call_id: u64,
    pub message_type: &'a str,
    pub payload: &'a [u8],
}

/// Decode an actor stream invocation from a frame payload (length prefix already stripped).
///
/// Returns borrowed slices into the frame data — zero-copy for the payload.
pub fn decode_invocation_frame(data: &[u8]) -> Result<DecodedInvocation<'_>, String> {
    if data.len() < 10 {
        return Err("invocation frame too short".into());
    }
    let call_id = u64::from_le_bytes(data[0..8].try_into().unwrap());
    let type_len = u16::from_le_bytes(data[8..10].try_into().unwrap()) as usize;
    if data.len() < 10 + type_len {
        return Err(format!(
            "invocation frame too short for message type (need {}, have {})",
            10 + type_len,
            data.len()
        ));
    }
    let message_type = std::str::from_utf8(&data[10..10 + type_len])
        .map_err(|e| format!("invalid message type UTF-8: {e}"))?;
    let payload = &data[10 + type_len..];
    Ok(DecodedInvocation {
        call_id,
        message_type,
        payload,
    })
}

/// Encode an actor stream response to a length-prefixed frame.
///
/// Writes the result bytes directly — no double-serialization.
pub fn encode_response_frame(call_id: u64, result: &Result<Vec<u8>, String>) -> Vec<u8> {
    let (status, body): (u8, &[u8]) = match result {
        Ok(payload) => (1, payload.as_slice()),
        Err(error) => (0, error.as_bytes()),
    };
    let body_len = 8 + 1 + body.len();
    let mut buf = Vec::with_capacity(4 + body_len);
    buf.extend_from_slice(&(body_len as u32).to_le_bytes());
    buf.extend_from_slice(&call_id.to_le_bytes());
    buf.push(status);
    buf.extend_from_slice(body);
    buf
}

/// Decoded response from a frame payload.
pub struct DecodedResponse<'a> {
    pub call_id: u64,
    pub result: Result<&'a [u8], String>,
}

/// Decode an actor stream response from a frame payload (length prefix already stripped).
///
/// The Ok payload is a borrowed slice — zero-copy.
pub fn decode_response_frame(data: &[u8]) -> Result<DecodedResponse<'_>, String> {
    if data.len() < 9 {
        return Err("response frame too short".into());
    }
    let call_id = u64::from_le_bytes(data[0..8].try_into().unwrap());
    let status = data[8];
    let body = &data[9..];
    let result = if status == 1 {
        Ok(body)
    } else {
        Err(String::from_utf8_lossy(body).into_owned())
    };
    Ok(DecodedResponse { call_id, result })
}

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

    #[test]
    fn test_frame_codec_roundtrip() {
        let original = b"hello, cluster!";
        let frame = FrameCodec::encode_frame(original);

        let mut codec = FrameCodec::new();
        codec.push_data(&frame);

        let decoded = codec.next_frame().unwrap().unwrap();
        assert_eq!(decoded, original);
    }

    #[test]
    fn test_frame_codec_incremental() {
        let original = b"incremental data";
        let frame = FrameCodec::encode_frame(original);

        let mut codec = FrameCodec::new();

        // Feed byte by byte
        for (i, byte) in frame.iter().enumerate() {
            codec.push_data(std::slice::from_ref(byte));
            let result = codec.next_frame().unwrap();
            if i < frame.len() - 1 {
                assert!(result.is_none(), "should not yield frame at byte {i}");
            } else {
                assert_eq!(result.unwrap(), original);
            }
        }
    }

    #[test]
    fn test_frame_codec_multiple_frames() {
        let msg1 = b"first";
        let msg2 = b"second";

        let mut wire = FrameCodec::encode_frame(msg1);
        wire.extend_from_slice(&FrameCodec::encode_frame(msg2));

        let mut codec = FrameCodec::new();
        codec.push_data(&wire);

        assert_eq!(codec.next_frame().unwrap().unwrap(), msg1);
        assert_eq!(codec.next_frame().unwrap().unwrap(), msg2);
        assert!(codec.next_frame().unwrap().is_none());
    }

    #[test]
    fn test_control_message_serde() {
        let msg = ControlMessage::Ping;
        let frame = encode_message(&msg).unwrap();

        // Strip the 4-byte length prefix
        let payload = &frame[4..];
        let decoded: ControlMessage = decode_message(payload).unwrap();

        assert!(matches!(decoded, ControlMessage::Ping));
    }

    #[test]
    fn test_spawn_request_roundtrip_with_and_without_generation() {
        // Same-version round-trip for both the singleton (Some) and ordinary
        // (None) spawn shapes. (bincode is positional, so this is not a
        // cross-version test — all nodes share PROTOCOL_VERSION.)
        for generation in [None, Some((1u64 << 32) | 7)] {
            let msg = ControlMessage::SpawnActor(SpawnRequest {
                request_id: 42,
                label: "catalog".into(),
                actor_type_name: "app::Catalog".into(),
                initial_state: vec![1, 2, 3],
                singleton_generation: generation,
            });
            let frame = encode_message(&msg).unwrap();
            let decoded: ControlMessage = decode_message(&frame[4..]).unwrap();
            match decoded {
                ControlMessage::SpawnActor(req) => {
                    assert_eq!(req.request_id, 42);
                    assert_eq!(req.singleton_generation, generation);
                }
                other => panic!("expected SpawnActor, got {other:?}"),
            }
        }
    }

    #[test]
    fn test_singleton_stop_and_ack_roundtrip() {
        let packed = (3u64 << 32) | 5;
        let stop = ControlMessage::StopSingleton {
            label: "catalog".into(),
            generation: packed,
        };
        let decoded: ControlMessage = decode_message(&encode_message(&stop).unwrap()[4..]).unwrap();
        assert!(
            matches!(decoded, ControlMessage::StopSingleton { label, generation } if label == "catalog" && generation == packed)
        );

        let ack = ControlMessage::SingletonStoppedAck {
            label: "catalog".into(),
            stopped_generation: packed,
        };
        let decoded: ControlMessage = decode_message(&encode_message(&ack).unwrap()[4..]).unwrap();
        assert!(
            matches!(decoded, ControlMessage::SingletonStoppedAck { label, stopped_generation } if label == "catalog" && stopped_generation == packed)
        );
    }

    #[test]
    fn test_frame_too_large() {
        let mut codec = FrameCodec::new();
        // Fake a frame header claiming 5MB
        let len = (5 * 1024 * 1024u32).to_le_bytes();
        codec.push_data(&len);
        codec.push_data(&[0u8; 10]);

        let result = codec.next_frame();
        assert!(result.is_err());
    }

    #[test]
    fn test_empty_data_returns_none() {
        let mut codec = FrameCodec::new();
        // Push 0 bytes — nothing to decode
        codec.push_data(&[]);
        assert!(codec.next_frame().unwrap().is_none());
    }

    #[test]
    fn test_truncated_length_prefix() {
        let mut codec = FrameCodec::new();

        // Only 1 byte — not enough for a 4-byte length prefix
        codec.push_data(&[0x02]);
        assert!(codec.next_frame().unwrap().is_none());

        // 3 bytes total — still not enough
        codec.push_data(&[0x00, 0x00]);
        assert!(codec.next_frame().unwrap().is_none());

        // Complete the 4-byte prefix: 2u32 LE = [0x02, 0x00, 0x00, 0x00]
        // We already pushed [0x02, 0x00, 0x00], push the last prefix byte
        codec.push_data(&[0x00]);

        // Now we have a valid length prefix saying 2 bytes of payload, but no payload yet
        assert!(codec.next_frame().unwrap().is_none());

        // Push the 2-byte payload
        codec.push_data(b"hi");
        assert_eq!(codec.next_frame().unwrap().unwrap(), b"hi");
    }

    #[test]
    fn test_partial_payload_returns_none() {
        let frame = FrameCodec::encode_frame(b"hello"); // 4 prefix + 5 payload = 9 bytes

        let mut codec = FrameCodec::new();

        // Push only first 7 bytes (prefix + 2 payload bytes)
        codec.push_data(&frame[..7]);
        assert!(codec.next_frame().unwrap().is_none());

        // Push remaining 2 bytes
        codec.push_data(&frame[7..]);
        assert_eq!(codec.next_frame().unwrap().unwrap(), b"hello");
    }

    #[test]
    fn test_garbage_after_valid_frame() {
        let valid_frame = FrameCodec::encode_frame(b"good");
        let garbage: &[u8] = &[0xff, 0xfe, 0x01];

        let mut codec = FrameCodec::new();
        codec.push_data(&valid_frame);
        codec.push_data(garbage);

        // First frame decodes successfully
        assert_eq!(codec.next_frame().unwrap().unwrap(), b"good");

        // Garbage is only 3 bytes — not enough for a length prefix, returns None
        assert!(codec.next_frame().unwrap().is_none());
    }

    #[test]
    fn test_frame_exactly_at_max_size() {
        let mut codec = FrameCodec::new();

        // Header claiming exactly MAX_FRAME_SIZE bytes
        let len = (MAX_FRAME_SIZE as u32).to_le_bytes();
        codec.push_data(&len);

        // Valid size, just waiting for payload — should not error
        assert!(codec.next_frame().unwrap().is_none());
    }

    #[test]
    fn test_frame_one_over_max_size() {
        let mut codec = FrameCodec::new();

        // Header claiming MAX_FRAME_SIZE + 1 bytes
        let len = ((MAX_FRAME_SIZE + 1) as u32).to_le_bytes();
        codec.push_data(&len);

        // Should error — exceeds limit
        assert!(codec.next_frame().is_err());
    }

    #[test]
    fn test_zero_length_frame() {
        // encode_frame with empty data should produce [0, 0, 0, 0]
        let frame = FrameCodec::encode_frame(b"");
        assert_eq!(frame, vec![0, 0, 0, 0]);

        let mut codec = FrameCodec::new();
        codec.push_data(&frame);

        // Empty payload is valid
        let result = codec.next_frame().unwrap().unwrap();
        assert_eq!(result, Vec::<u8>::new());
    }

    #[test]
    fn test_multiple_frames_with_partial_interleaved() {
        let frame1 = FrameCodec::encode_frame(b"alpha");
        let frame2 = FrameCodec::encode_frame(b"beta");

        let mut codec = FrameCodec::new();

        // Feed all of frame1 and first 3 bytes of frame2
        codec.push_data(&frame1);
        codec.push_data(&frame2[..3]);

        assert_eq!(codec.next_frame().unwrap().unwrap(), b"alpha");
        assert!(codec.next_frame().unwrap().is_none());

        // Feed remaining bytes of frame2
        codec.push_data(&frame2[3..]);
        assert_eq!(codec.next_frame().unwrap().unwrap(), b"beta");
    }

    #[test]
    fn test_encode_decode_control_message_departure() {
        let identity = NodeIdentity {
            name: "test-node".into(),
            endpoint_id: NodeIdentity::test_endpoint_id("test-node"),
            host: "127.0.0.1".into(),
            port: 9000,
            incarnation: 1,
        };
        let msg = ControlMessage::Departure(identity.clone());

        // Round-trip through encode_message / decode_message
        let frame = encode_message(&msg).unwrap();

        // decode_message expects payload without the 4-byte length prefix
        let payload = &frame[4..];
        let decoded: ControlMessage = decode_message(payload).unwrap();

        match decoded {
            ControlMessage::Departure(decoded_identity) => {
                assert_eq!(decoded_identity.name, identity.name);
                assert_eq!(decoded_identity.host, identity.host);
                assert_eq!(decoded_identity.port, identity.port);
                assert_eq!(decoded_identity.incarnation, identity.incarnation);
            }
            other => panic!("expected Departure, got {other:?}"),
        }
    }

    // ── Lean wire format tests ─────────────────────────────────────

    #[test]
    fn test_invocation_frame_roundtrip() {
        let call_id = 42u64;
        let message_type = "counter::Increment";
        let payload = b"some-serialized-message-bytes";

        let frame = encode_invocation_frame(call_id, message_type, payload);

        // Strip the 4-byte length prefix (FrameCodec would do this)
        let body = &frame[4..];
        let decoded = decode_invocation_frame(body).unwrap();

        assert_eq!(decoded.call_id, call_id);
        assert_eq!(decoded.message_type, message_type);
        assert_eq!(decoded.payload, payload);
    }

    #[test]
    fn test_invocation_frame_empty_payload() {
        let frame = encode_invocation_frame(1, "test::Empty", &[]);
        let body = &frame[4..];
        let decoded = decode_invocation_frame(body).unwrap();

        assert_eq!(decoded.call_id, 1);
        assert_eq!(decoded.message_type, "test::Empty");
        assert!(decoded.payload.is_empty());
    }

    #[test]
    fn test_invocation_frame_through_codec() {
        let frame = encode_invocation_frame(99, "actor::Ping", b"hello");

        let mut codec = FrameCodec::new();
        codec.push_data(&frame);

        let body = codec.next_frame().unwrap().unwrap();
        let decoded = decode_invocation_frame(&body).unwrap();

        assert_eq!(decoded.call_id, 99);
        assert_eq!(decoded.message_type, "actor::Ping");
        assert_eq!(decoded.payload, b"hello");
    }

    #[test]
    fn test_invocation_frame_too_short() {
        assert!(decode_invocation_frame(&[0u8; 5]).is_err());
    }

    #[test]
    fn test_response_frame_roundtrip_ok() {
        let call_id = 42u64;
        let result_bytes = vec![1, 2, 3, 4, 5];

        let frame = encode_response_frame(call_id, &Ok(result_bytes.clone()));
        let body = &frame[4..];
        let decoded = decode_response_frame(body).unwrap();

        assert_eq!(decoded.call_id, call_id);
        assert_eq!(decoded.result.unwrap(), &result_bytes[..]);
    }

    #[test]
    fn test_response_frame_roundtrip_err() {
        let call_id = 7u64;
        let error = "actor not found".to_string();

        let frame = encode_response_frame(call_id, &Err(error.clone()));
        let body = &frame[4..];
        let decoded = decode_response_frame(body).unwrap();

        assert_eq!(decoded.call_id, call_id);
        assert_eq!(decoded.result.unwrap_err(), error);
    }

    #[test]
    fn test_response_frame_through_codec() {
        let frame = encode_response_frame(55, &Ok(vec![10, 20, 30]));

        let mut codec = FrameCodec::new();
        codec.push_data(&frame);

        let body = codec.next_frame().unwrap().unwrap();
        let decoded = decode_response_frame(&body).unwrap();

        assert_eq!(decoded.call_id, 55);
        assert_eq!(decoded.result.unwrap(), &[10, 20, 30]);
    }

    #[test]
    fn test_response_frame_too_short() {
        assert!(decode_response_frame(&[0u8; 5]).is_err());
    }

    #[test]
    fn test_multiple_invocation_frames_through_codec() {
        let frame1 = encode_invocation_frame(1, "a::B", b"first");
        let frame2 = encode_invocation_frame(2, "c::D", b"second");

        let mut codec = FrameCodec::new();
        codec.push_data(&frame1);
        codec.push_data(&frame2);

        let body1 = codec.next_frame().unwrap().unwrap();
        let d1 = decode_invocation_frame(&body1).unwrap();
        assert_eq!(d1.call_id, 1);
        assert_eq!(d1.message_type, "a::B");
        assert_eq!(d1.payload, b"first");

        let body2 = codec.next_frame().unwrap().unwrap();
        let d2 = decode_invocation_frame(&body2).unwrap();
        assert_eq!(d2.call_id, 2);
        assert_eq!(d2.message_type, "c::D");
        assert_eq!(d2.payload, b"second");
    }
}