byteflow-actors 0.9.21

Embeddable flow runtime: M:N scheduler, Atomic Hop (Message+Cap), supervisor — use byteflow::
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
//! Outbound hops for another node.
//!
//! Byteflow does not open sockets and does not speak a cluster protocol.
//! The host owns I/O. Bytecode [`crate::Opcode::SendRemote`] stamps the
//! sender here and queues the hop on [`DistributionBridge`]; the embedder
//! drains that queue and delivers it however it wants.
//!
//! Payload `Cap` tokens do not leave the node. A live cap, including one
//! nested in a list or in another hop's `reply_cap`, fails the send before
//! a send-quota token is spent. A nested envelope whose `sender` is still
//! `0` is stamped with this flow. `reply_cap` on the outbound hop itself
//! is [`crate::CapId::NONE`] — there is no local flow to mint a reply
//! address for.

use std::collections::VecDeque;
use std::sync::{Arc, Condvar, Mutex};

use crate::bytecode::{CapId, Message, Value};

use super::error::RuntimeError;
use super::process::FlowId;
use super::sync_lock;

/// How many outbound hops a bridge holds before `SendRemote` fails the flow.
const MAX_OUTBOUND: usize = 1024;

/// One authenticated hop waiting for the host to deliver it off-node.
#[derive(Clone, Debug)]
pub struct OutboundHop {
    pub sender: FlowId,
    /// Host-defined node number. `0` is not special.
    pub node: u64,
    pub name: Arc<str>,
    pub message: Message,
}

/// Queue the host drains. Share one [`Arc`] via [`crate::RuntimeConfig::distribution`].
#[derive(Debug)]
pub struct DistributionBridge {
    queue: Mutex<VecDeque<OutboundHop>>,
    ready: Condvar,
}

impl DistributionBridge {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            queue: Mutex::new(VecDeque::new()),
            ready: Condvar::new(),
        })
    }

    pub(crate) fn push(&self, hop: OutboundHop) -> Result<(), String> {
        let mut queue =
            sync_lock::lock(&self.queue, "DistributionBridge::push").map_err(|e| e.to_string())?;
        if queue.len() >= MAX_OUTBOUND {
            return Err("distribution bridge full".into());
        }
        queue.push_back(hop);
        self.ready.notify_one();
        Ok(())
    }

    /// Next hop, if the host has not fallen behind.
    pub fn try_recv(&self) -> Result<Option<OutboundHop>, RuntimeError> {
        let mut queue = sync_lock::lock(&self.queue, "DistributionBridge::try_recv")?;
        Ok(queue.pop_front())
    }

    /// Hops waiting for the host to encode and send (`SendRemote` backpressure).
    pub fn outbound_pending(&self) -> Result<usize, RuntimeError> {
        let queue = sync_lock::lock(&self.queue, "DistributionBridge::outbound_pending")?;
        Ok(queue.len())
    }

    /// Block until a hop arrives or `timeout` elapses.
    pub fn recv_timeout(
        &self,
        timeout: std::time::Duration,
    ) -> Result<Option<OutboundHop>, RuntimeError> {
        let mut queue = sync_lock::lock(&self.queue, "DistributionBridge::recv_timeout")?;
        let start = std::time::Instant::now();
        loop {
            if let Some(hop) = queue.pop_front() {
                return Ok(Some(hop));
            }
            let left = match timeout.checked_sub(start.elapsed()) {
                Some(left) => left,
                None => return Ok(None),
            };
            let (guard, status) =
                sync_lock::wait_timeout(&self.ready, queue, left, "DistributionBridge::wait")?;
            queue = guard;
            if status.timed_out() && queue.is_empty() {
                return Ok(None);
            }
        }
    }
}

/// Sliding-window replay filter for [`crate::Runtime::deliver_frame`].
#[derive(Debug)]
pub struct RemoteFrameDedup {
    capacity: usize,
    seen: Mutex<VecDeque<(u64, u64)>>,
}

impl RemoteFrameDedup {
    pub fn new(capacity: usize) -> Arc<Self> {
        Arc::new(RemoteFrameDedup {
            capacity: capacity.max(1),
            seen: Mutex::new(VecDeque::new()),
        })
    }

    /// `true` when this `(sender, request_id)` was already accepted in the window.
    pub fn is_duplicate(&self, sender: u64, request_id: u64) -> Result<bool, RuntimeError> {
        let mut queue = sync_lock::lock(&self.seen, "RemoteFrameDedup::is_duplicate")?;
        for &(s, id) in queue.iter() {
            if s == sender && id == request_id {
                return Ok(true);
            }
        }
        queue.push_back((sender, request_id));
        while queue.len() > self.capacity {
            queue.pop_front();
        }
        Ok(false)
    }
}

/// Stamp `sender` and clear `reply_cap`. Does not mint a local reply Cap.
pub(crate) fn stamp(message: Message, sender: FlowId) -> Message {
    message.authenticate(sender.as_u64(), CapId::NONE)
}

/// `SendRemote` effect. `message` must already be a hop.
pub(crate) fn deliver(
    shared: &super::runtime::Shared,
    flow: &mut super::process::Flow,
    node: u64,
    name: Arc<str>,
    message: Value,
) -> Result<(), String> {
    if !flow.authority.rights().contains(crate::CapRights::SEND) {
        return Err("send_remote denied: flow lacks SEND right".into());
    }
    let Some(bridge) = shared.distribution.as_ref() else {
        return Err("send_remote: no distribution bridge".into());
    };
    let Some(mut msg) = message.as_message().cloned() else {
        return Err("send_remote: value is not a hop".into());
    };
    if !msg.payload.within_nesting_limit() {
        return Err("send_remote: payload too nested".into());
    }
    if msg.payload.contains_live_cap(0) {
        return Err("send_remote: payload contains a local capability".into());
    }
    if let Err(e) = flow.quota.check_send() {
        return Err(e.to_string());
    }
    if msg.request_id == 0 {
        msg.request_id = flow.vm.fresh_request_id();
    }
    let Some(payload) = msg
        .payload
        .stamp_unset_envelopes(0, flow.id.as_u64(), &mut || flow.vm.fresh_request_id())
    else {
        return Err("send_remote: payload too nested".into());
    };
    let stamped = stamp(msg.with_payload(payload), flow.id);
    bridge.push(OutboundHop {
        sender: flow.id,
        node,
        name,
        message: stamped,
    })
}

const FRAME_MAGIC: [u8; 4] = *b"BFH1";
const FRAME_VERSION: u8 = 1;
const MAX_FRAME_NAME: usize = 256;

/// Why a remote hop frame was refused.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RemoteFrameError {
    BadMagic,
    Truncated,
    BadVersion,
    BadUtf8,
    /// A `Cap` or `Pid` was on the wire, or a nested hop still had a reply cap.
    LiveCapability,
    /// Sender `0` is the local host. A remote frame must name another id.
    SenderZero,
    /// The frame header and the envelope name different senders.
    SenderMismatch,
    Name,
    Unsupported,
    NoSuchName,
    Mailbox(super::mailbox::MailboxFullReason),
    Unavailable,
}

impl std::fmt::Display for RemoteFrameError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BadMagic => f.write_str("remote frame magic"),
            Self::Truncated => f.write_str("remote frame truncated"),
            Self::BadVersion => f.write_str("remote frame version"),
            Self::BadUtf8 => f.write_str("remote frame name"),
            Self::LiveCapability => f.write_str("remote frame carried a local capability"),
            Self::SenderZero => f.write_str("remote frame sender is the local host"),
            Self::SenderMismatch => f.write_str("remote frame sender does not match the envelope"),
            Self::Name => f.write_str("remote frame name"),
            Self::Unsupported => f.write_str("remote frame value"),
            Self::NoSuchName => f.write_str("remote frame name is not registered"),
            Self::Mailbox(reason) => write!(f, "remote frame mailbox ({reason})"),
            Self::Unavailable => f.write_str("remote frame unavailable"),
        }
    }
}

impl std::error::Error for RemoteFrameError {}

/// Bytes the host can write on its own socket. No capability leaves the node.
pub fn encode_frame(hop: &OutboundHop) -> Result<Vec<u8>, RemoteFrameError> {
    if hop.sender.is_host() {
        return Err(RemoteFrameError::SenderZero);
    }
    if hop.name.is_empty() || hop.name.len() > MAX_FRAME_NAME {
        return Err(RemoteFrameError::Name);
    }
    if hop.message.sender != hop.sender.as_u64() {
        return Err(frame_sender_error(hop.message.sender));
    }
    screen_message(&hop.message, 0)?;
    let mut out = Vec::with_capacity(32 + hop.name.len());
    out.extend_from_slice(&FRAME_MAGIC);
    out.push(FRAME_VERSION);
    out.extend_from_slice(&hop.node.to_le_bytes());
    out.extend_from_slice(&(hop.name.len() as u16).to_le_bytes());
    out.extend_from_slice(hop.name.as_bytes());
    out.extend_from_slice(&hop.sender.as_u64().to_le_bytes());
    out.extend_from_slice(&crate::bytecode::encode_value(&Value::Message(
        hop.message.clone(),
    )));
    Ok(out)
}

/// Inverse of [`encode_frame`]. The host still decides which bytes are trusted.
pub fn decode_frame(bytes: &[u8]) -> Result<OutboundHop, RemoteFrameError> {
    let mut pos = 0usize;
    let magic: [u8; 4] = take(bytes, &mut pos)?;
    if magic != FRAME_MAGIC {
        return Err(RemoteFrameError::BadMagic);
    }
    let version: [u8; 1] = take(bytes, &mut pos)?;
    if version[0] != FRAME_VERSION {
        return Err(RemoteFrameError::BadVersion);
    }
    let node = u64::from_le_bytes(take(bytes, &mut pos)?);
    let name_len = u16::from_le_bytes(take(bytes, &mut pos)?) as usize;
    if name_len == 0 || name_len > MAX_FRAME_NAME {
        return Err(RemoteFrameError::Name);
    }
    let name_bytes = take_slice(bytes, &mut pos, name_len)?;
    let name = std::str::from_utf8(name_bytes).map_err(|_| RemoteFrameError::BadUtf8)?;
    let sender = u64::from_le_bytes(take(bytes, &mut pos)?);
    if sender == 0 {
        return Err(RemoteFrameError::SenderZero);
    }
    let (value, consumed) =
        crate::bytecode::decode_value(&bytes[pos..]).map_err(frame_format_error)?;
    if consumed != bytes.len() - pos {
        return Err(RemoteFrameError::Unsupported);
    }
    let Value::Message(message) = value else {
        return Err(RemoteFrameError::Unsupported);
    };
    if message.sender != sender {
        return Err(frame_sender_error(message.sender));
    }
    screen_message(&message, 0)?;
    Ok(OutboundHop {
        sender: FlowId(sender),
        node,
        name: Arc::from(name),
        message,
    })
}

fn frame_sender_error(message_sender: u64) -> RemoteFrameError {
    if message_sender == 0 {
        RemoteFrameError::SenderZero
    } else {
        RemoteFrameError::SenderMismatch
    }
}

fn take<const N: usize>(data: &[u8], pos: &mut usize) -> Result<[u8; N], RemoteFrameError> {
    let slice = take_slice(data, pos, N)?;
    let mut out = [0u8; N];
    out.copy_from_slice(slice);
    Ok(out)
}

fn take_slice<'a>(data: &'a [u8], pos: &mut usize, n: usize) -> Result<&'a [u8], RemoteFrameError> {
    let end = pos.checked_add(n).ok_or(RemoteFrameError::Truncated)?;
    if end > data.len() {
        return Err(RemoteFrameError::Truncated);
    }
    let slice = &data[*pos..end];
    *pos = end;
    Ok(slice)
}

fn frame_format_error(err: crate::bytecode::FormatError) -> RemoteFrameError {
    match err {
        crate::bytecode::FormatError::Truncated => RemoteFrameError::Truncated,
        crate::bytecode::FormatError::BadUtf8 => RemoteFrameError::BadUtf8,
        crate::bytecode::FormatError::UnknownValueTag(4 | 6) => RemoteFrameError::LiveCapability,
        _ => RemoteFrameError::Unsupported,
    }
}

fn screen_message(message: &Message, depth: u32) -> Result<(), RemoteFrameError> {
    if depth > crate::bytecode::MAX_VALUE_DEPTH {
        return Err(RemoteFrameError::Unsupported);
    }
    if !message.reply_cap.is_none() {
        return Err(RemoteFrameError::LiveCapability);
    }
    screen_value(message.payload.as_ref(), depth + 1)
}

fn screen_value(value: &Value, depth: u32) -> Result<(), RemoteFrameError> {
    if depth > crate::bytecode::MAX_VALUE_DEPTH {
        return Err(RemoteFrameError::Unsupported);
    }
    match value {
        Value::Pid(_) | Value::Cap(_) => Err(RemoteFrameError::LiveCapability),
        Value::Message(message) => screen_message(message, depth),
        Value::List(items) => {
            if items.len() > crate::bytecode::MAX_LIST_LEN {
                return Err(RemoteFrameError::Unsupported);
            }
            items
                .iter()
                .try_for_each(|item| screen_value(item, depth + 1))
        }
        Value::Record(fields) => {
            if fields.len() > crate::bytecode::MAX_RECORD_LEN {
                return Err(RemoteFrameError::Unsupported);
            }
            fields
                .iter()
                .try_for_each(|field| screen_value(field, depth + 1))
        }
        Value::Map(entries) => {
            if entries.len() > crate::bytecode::MAX_MAP_LEN {
                return Err(RemoteFrameError::Unsupported);
            }
            entries
                .iter()
                .try_for_each(|(_, item)| screen_value(item, depth + 1))
        }
        Value::Unit
        | Value::Bool(_)
        | Value::Int(_)
        | Value::Float(_)
        | Value::Str(_)
        | Value::Bytes(_) => Ok(()),
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use crate::{FlowOutcome, Program, Runtime, RuntimeConfig, Value};

    use super::*;

    #[test]
    fn host_drains_a_remote_hop() -> Result<(), Box<dyn std::error::Error>> {
        let bridge = DistributionBridge::new();
        let mut program = Program::new("remote");
        program.function("main", 0, |f| {
            let node = f.load_i32(7);
            let name = f.load_str("svc");
            let payload = f.load_i32(41);
            let msg = f.hop_fresh(3, payload);
            f.send_remote(node, name, msg);
            f.return_(payload);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                distribution: Some(Arc::clone(&bridge)),
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let outcome = rt.spawn(0, &[])?.join();
        rt.shutdown();
        match outcome {
            FlowOutcome::Completed(Value::Int(41)) => {}
            other => return Err(format!("remote send failed: {other:?}").into()),
        }
        let hop = match bridge.recv_timeout(Duration::from_secs(1))? {
            Some(hop) => hop,
            None => return Err("bridge empty".into()),
        };
        if hop.node != 7 || hop.name.as_ref() != "svc" {
            return Err(format!("unexpected route {hop:?}").into());
        }
        if hop.message.reply_cap != CapId::NONE {
            return Err("remote hop minted a local reply cap".into());
        }
        if !matches!(hop.message.payload.as_ref(), Value::Int(41)) {
            return Err(format!("payload {:?}", hop.message.payload).into());
        }
        Ok(())
    }

    #[test]
    fn a_live_cap_in_the_payload_does_not_leave_the_node() -> Result<(), Box<dyn std::error::Error>>
    {
        let bridge = DistributionBridge::new();
        let mut program = Program::new("remote-cap");
        program.function("main", 0, |f| {
            let node = f.load_i32(7);
            let name = f.load_str("svc");
            let me = f.self_cap();
            let msg = f.hop_fresh(3, me);
            f.send_remote(node, name, msg);
            f.return_(node);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                distribution: Some(Arc::clone(&bridge)),
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let outcome = rt.spawn(0, &[])?.join();
        rt.shutdown();
        match outcome {
            FlowOutcome::Failed(reason) if reason.contains("local capability") => {}
            other => return Err(format!("expected a refused remote cap, got {other:?}").into()),
        }
        if bridge.try_recv()?.is_some() {
            return Err("bridge accepted a hop that carried a capability".into());
        }
        Ok(())
    }

    #[test]
    fn remote_nested_envelope_is_stamped_with_the_flow() -> Result<(), Box<dyn std::error::Error>> {
        let bridge = DistributionBridge::new();
        let mut program = Program::new("remote-nested");
        program.function("main", 0, |f| {
            let node = f.load_i32(7);
            let name = f.load_str("svc");
            let body = f.load_i32(1);
            let inner = f.hop_fresh(1, body);
            let outer = f.hop_fresh(2, inner);
            f.send_remote(node, name, outer);
            f.return_(node);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                distribution: Some(Arc::clone(&bridge)),
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let outcome = rt.spawn(0, &[])?.join();
        rt.shutdown();
        if !matches!(outcome, FlowOutcome::Completed(Value::Int(7))) {
            return Err(format!("remote nested send failed: {outcome:?}").into());
        }
        let hop = match bridge.recv_timeout(Duration::from_secs(1))? {
            Some(hop) => hop,
            None => return Err("bridge empty".into()),
        };
        let Some(inner) = hop.message.payload.as_message() else {
            return Err(format!("payload {:?}", hop.message.payload).into());
        };
        if inner.sender != hop.sender.as_u64() {
            return Err(format!("nested sender {} hop {}", inner.sender, hop.sender).into());
        }
        if !inner.reply_cap.is_none() {
            return Err("remote nested hop minted a local reply cap".into());
        }
        Ok(())
    }

    #[test]
    fn frame_keeps_the_declared_sender() -> Result<(), RemoteFrameError> {
        use crate::CapId;
        let hop = OutboundHop {
            sender: FlowId(9),
            node: 3,
            name: std::sync::Arc::from("svc"),
            message: Message::new(9, 4, 1, Value::Int(5)).authenticate(9, CapId::NONE),
        };
        let back = decode_frame(&encode_frame(&hop)?)?;
        if back.sender.as_u64() != 9 || back.node != 3 || back.message.tag != 1 {
            return Err(RemoteFrameError::Unsupported);
        }
        if !matches!(back.message.payload.as_ref(), Value::Int(5)) {
            return Err(RemoteFrameError::Unsupported);
        }
        Ok(())
    }

    #[test]
    fn frame_round_trips_a_record_without_a_cap() -> Result<(), RemoteFrameError> {
        use crate::CapId;
        let record = Value::try_record(vec![Value::Int(1), Value::Int(2)])
            .ok_or(RemoteFrameError::Unsupported)?;
        let hop = OutboundHop {
            sender: FlowId(9),
            node: 3,
            name: std::sync::Arc::from("svc"),
            message: Message::new(9, 4, 1, record).authenticate(9, CapId::NONE),
        };
        let back = decode_frame(&encode_frame(&hop)?)?;
        if back.sender.as_u64() != 9 || !back.message.reply_cap.is_none() {
            return Err(RemoteFrameError::Unsupported);
        }
        match back.message.payload.as_ref() {
            Value::Record(fields) if fields.len() == 2 => Ok(()),
            _ => Err(RemoteFrameError::Unsupported),
        }
    }

    #[test]
    fn frame_rejects_a_live_cap() {
        use crate::CapId;
        let hop = OutboundHop {
            sender: FlowId(9),
            node: 1,
            name: std::sync::Arc::from("svc"),
            message: Message::new(9, 1, 1, Value::Cap(CapId::from_raw(1)))
                .authenticate(9, CapId::NONE),
        };
        assert!(matches!(
            encode_frame(&hop),
            Err(RemoteFrameError::LiveCapability)
        ));
    }

    #[test]
    fn frame_rejects_a_list_past_the_cap() {
        let hop = OutboundHop {
            sender: FlowId(9),
            node: 1,
            name: std::sync::Arc::from("svc"),
            message: Message::new(
                9,
                1,
                1,
                Value::List(std::sync::Arc::from(vec![Value::Int(1); 257])),
            )
            .authenticate(9, crate::CapId::NONE),
        };
        assert!(matches!(
            encode_frame(&hop),
            Err(RemoteFrameError::Unsupported)
        ));
    }

    #[test]
    fn frame_accepts_a_list_at_the_cap() -> Result<(), RemoteFrameError> {
        let list =
            Value::try_list(vec![Value::Int(1); 256]).ok_or(RemoteFrameError::Unsupported)?;
        let hop = OutboundHop {
            sender: FlowId(9),
            node: 1,
            name: std::sync::Arc::from("svc"),
            message: Message::new(9, 1, 1, list).authenticate(9, crate::CapId::NONE),
        };
        encode_frame(&hop).map(|_| ())
    }

    #[test]
    fn remote_frame_dedup_detects_replay() -> Result<(), RuntimeError> {
        let dedup = RemoteFrameDedup::new(4);
        assert!(!dedup.is_duplicate(1, 100)?);
        assert!(dedup.is_duplicate(1, 100)?);
        assert!(!dedup.is_duplicate(1, 101)?);
        Ok(())
    }

    #[test]
    fn deliver_frame_lands_in_the_named_mailbox() -> Result<(), Box<dyn std::error::Error>> {
        use crate::CapId;
        let mut program = Program::new("inbox");
        program.function("main", 0, |f| {
            let msg = f.receive();
            let n = f.hop_payload(msg);
            f.return_(n);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let handle = rt.spawn(0, &[])?;
        let cap = rt.mint_cap(handle.id())?;
        rt.register_name("inbox", cap)?;
        let frame = encode_frame(&OutboundHop {
            sender: FlowId(44),
            node: 2,
            name: std::sync::Arc::from("inbox"),
            message: Message::new(44, 1, 7, Value::Int(8)).authenticate(44, CapId::NONE),
        })?;
        rt.deliver_frame(&frame).map_err(|err| format!("{err:?}"))?;
        let outcome = handle
            .join_timeout(Duration::from_secs(2))
            .ok_or("deliver frame timed out")?;
        rt.shutdown();
        match outcome {
            FlowOutcome::Completed(Value::Int(8)) => Ok(()),
            other => Err(format!("deliver frame: {other:?}").into()),
        }
    }

    #[test]
    fn receive_match_cmp_keeps_the_failing_int_queued() -> Result<(), Box<dyn std::error::Error>> {
        use crate::bytecode::IntCmp;
        use crate::bytecode::Message;
        let mut program = Program::new("guard");
        program.function("main", 0, |f| {
            let tag = f.load_i32(1);
            let bound = f.load_i32(10);
            let msg = f.receive_match_cmp(tag, bound, IntCmp::Lt);
            let n = f.hop_payload(msg);
            f.return_(n);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let handle = rt.spawn(0, &[])?;
        rt.send(
            handle.id(),
            Value::Message(Message::request(1, 1, Value::Int(20))),
        )?;
        rt.send(
            handle.id(),
            Value::Message(Message::request(2, 1, Value::Int(3))),
        )?;
        let outcome = handle
            .join_timeout(Duration::from_secs(2))
            .ok_or("guard timed out")?;
        rt.shutdown();
        match outcome {
            FlowOutcome::Completed(Value::Int(3)) => Ok(()),
            other => Err(format!("guard: {other:?}").into()),
        }
    }

    #[test]
    fn table_put_get_and_read_only_delegate() -> Result<(), Box<dyn std::error::Error>> {
        use crate::bytecode::CapRights;
        let mut program = Program::new("table");
        program.function("main", 0, |f| {
            let name = f.load_str("box");
            let table = f.table_open(name);
            let key = f.load_i32(1);
            let value = f.load_i32(42);
            f.table_put(table, key, value);
            let got = f.table_get(table, key);
            f.return_(got);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let handle = rt.spawn(0, &[])?;
        let outcome = handle
            .join_timeout(Duration::from_secs(2))
            .ok_or("table get timed out")?;
        rt.shutdown();
        if !matches!(outcome, FlowOutcome::Completed(Value::Int(42))) {
            return Err(format!("table get: {outcome:?}").into());
        }

        let mut program = Program::new("table-ro");
        program.function("main", 0, |f| {
            let name = f.load_str("box");
            let table = f.table_open(name);
            let key = f.load_i32(1);
            let value = f.load_i32(42);
            f.table_put(table, key, value);
            let read_only = f.delegate(table, CapRights::RECV);
            f.table_put(read_only, key, value);
            f.return_(value);
        });
        let rt = Runtime::with_natives_and_config(
            program.build(),
            crate::std_native_table(),
            RuntimeConfig {
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let handle = rt.spawn(0, &[])?;
        let outcome = handle
            .join_timeout(Duration::from_secs(2))
            .ok_or("read-only put timed out")?;
        rt.shutdown();
        match outcome {
            FlowOutcome::Failed(text) if text.contains("rights") => Ok(()),
            other => Err(format!("read-only put: {other:?}").into()),
        }
    }

    #[test]
    fn frame_rejects_a_sender_that_is_not_the_envelope() -> Result<(), RemoteFrameError> {
        use crate::CapId;
        let agreed = OutboundHop {
            sender: FlowId(9),
            node: 1,
            name: std::sync::Arc::from("svc"),
            message: Message::new(9, 1, 1, Value::Int(1)).authenticate(9, CapId::NONE),
        };
        let bytes = encode_frame(&agreed)?;
        let host = OutboundHop {
            sender: FlowId(9),
            node: 1,
            name: std::sync::Arc::from("svc"),
            message: Message::new(0, 1, 1, Value::Int(1)).authenticate(0, CapId::NONE),
        };
        if !matches!(encode_frame(&host), Err(RemoteFrameError::SenderZero)) {
            return Err(RemoteFrameError::Unsupported);
        }
        let other = OutboundHop {
            sender: FlowId(9),
            node: 1,
            name: std::sync::Arc::from("svc"),
            message: Message::new(4, 1, 1, Value::Int(1)).authenticate(4, CapId::NONE),
        };
        if !matches!(encode_frame(&other), Err(RemoteFrameError::SenderMismatch)) {
            return Err(RemoteFrameError::Unsupported);
        }
        // Header, then the `.bf` message tag, then the envelope sender.
        let envelope_at = 4 + 1 + 8 + 2 + agreed.name.len() + 8 + 1;
        let mut forged = bytes.clone();
        forged[envelope_at..envelope_at + 8].copy_from_slice(&0u64.to_le_bytes());
        if !matches!(decode_frame(&forged), Err(RemoteFrameError::SenderZero)) {
            return Err(RemoteFrameError::Unsupported);
        }
        forged[envelope_at..envelope_at + 8].copy_from_slice(&4u64.to_le_bytes());
        if !matches!(decode_frame(&forged), Err(RemoteFrameError::SenderMismatch)) {
            return Err(RemoteFrameError::Unsupported);
        }
        Ok(())
    }

    #[test]
    fn table_overwrite_at_the_row_cap_keeps_the_row() -> Result<(), Box<dyn std::error::Error>> {
        use crate::bytecode::builder::ChunkBuilder;
        let mut b = ChunkBuilder::new("full");
        b.begin_function("main", 0, 6);
        let name = b.const_(Value::str("box"));
        b.emit_load_const(0, name);
        b.emit_table_open(1, 0);
        b.emit_load_imm(2, 0);
        b.emit_load_imm(3, 0);
        let again = b.new_label();
        let done = b.new_label();
        b.bind_label(again);
        b.emit_table_put(1, 2, 3);
        b.emit_add_imm(2, 2, 1);
        b.emit_lt_imm(4, 2, 256);
        b.emit_branch(4, done);
        b.emit_jump(again);
        b.bind_label(done);
        b.emit_load_imm(2, 0);
        b.emit_load_imm(3, 7);
        b.emit_table_put(1, 2, 3);
        b.emit_table_get(5, 1, 2);
        b.emit_return(5);
        let rt = Runtime::with_natives_and_config(
            b.finish(),
            crate::std_native_table(),
            RuntimeConfig {
                workers: 1,
                ..RuntimeConfig::default()
            },
        )?;
        let handle = rt.spawn(0, &[])?;
        let outcome = handle
            .join_timeout(Duration::from_secs(2))
            .ok_or("table fill timed out")?;
        rt.shutdown();
        match outcome {
            FlowOutcome::Completed(Value::Int(7)) => Ok(()),
            other => Err(format!("overwrite at cap: {other:?}").into()),
        }
    }
}