byteflow-actors 0.5.1

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
//! Built-in demo chunks assembled with [`crate::ChunkBuilder`].
//!
//! Use these as runnable specs of the messaging contract (and as regression
//! tests). Prefer copying a sample over inventing hop register layouts from
//! scratch.
//!
//! | Sample | Shows |
//! |--------|--------|
//! | [`add_forty_two`] | Scalar VM path (no natives) |
//! | [`ping_pong`] | Cap spawn + Atomic Hop round-trip |
//! | [`atomic_request_reply`] | Tagged REQ/REP + `print` |
//! | [`selective_receive`] | `ReceiveMatch` FIFO skip |
//! | [`ask_reply`] | `Ask` RPC hop |
//! | [`forged_sender_send`] / [`forged_sender_ask`] | S1: forged `make_msg` sender dies |
//! | [`boom`] | Immediate trap (supervisor demos) |
//!
//! Hop samples require [`crate::std_native_table`].

use crate::{emit_native1_from, emit_native_n, Chunk, ChunkBuilder, Opcode};

/// Native indices (must match [`crate::std_native_map`]).
///
/// Hard-coded here so the sample stays self-contained without looking up
/// the map at assembly time ÔÇö the stability test in `natives` guards drift.
const N_PRINT: u32 = 0;
const N_MAKE_MSG: u32 = 2;
const N_MSG_SENDER: u32 = 3;
const N_MSG_REQUEST_ID: u32 = 4;
const N_MSG_TAG: u32 = 5;
const N_MSG_PAYLOAD: u32 = 6;
const N_MSG_REPLY_CAP: u32 = 7;

/// Protocol tags for Atomic Hop samples.
///
/// Opaque to the VM; only this sample (and its clients) interpret them.
pub const TAG_REQ: i32 = 1;
pub const TAG_REP: i32 = 2;
pub const TAG_PING: i32 = 10;
pub const TAG_PONG: i32 = 11;
/// Decoy hop for [`selective_receive`] ÔÇö must be skipped by `ReceiveMatch`.
pub const TAG_JUNK: i32 = 99;

/// `r0 = 41 + 1; return r0` ÔÇö the 60-second sanity chunk.
pub fn add_forty_two() -> Chunk {
    let mut b = ChunkBuilder::new("add-forty-two");
    b.begin_function("main", 0, 2);
    b.emit_load_imm(0, 41);
    b.emit_load_imm(1, 1);
    b.emit_binop(Opcode::Add, 0, 0, 1);
    b.emit_return(0);
    b.finish()
}

/// Two flows, one **Atomic Hop** round-trip: `main` sends a `Message` to
/// `pong`, `pong` replies with payload+1 via `msg_reply_cap`, `main` returns
/// that payload (`2`).
///
/// Requires [`crate::std_native_table`] (`make_msg` / `msg_*`).
///
/// Every `Send` carries exactly one [`crate::Value::Message`] and targets a
/// [`crate::Value::Cap`] ÔÇö bare ints / pids trap (Atomic Hop + FlowCap).
pub fn ping_pong() -> Chunk {
    let mut b = ChunkBuilder::new("ping-pong");

    // pong: receive Message, reply payload+1 via reply_cap
    // r0 = request Message
    // r1 = reply Cap
    // r2 = request_id
    // r3 = payload (+1), then make_msg result
    // r4..r7 = make_msg arg window
    let pong = b.begin_function("pong", 0, 8);
    b.emit_receive(0);
    emit_native1_from!(b, 1, 0, N_MSG_REPLY_CAP);
    emit_native1_from!(b, 2, 0, N_MSG_REQUEST_ID);
    emit_native1_from!(b, 3, 0, N_MSG_PAYLOAD);
    b.emit_load_imm(7, 1);
    b.emit_binop(Opcode::Add, 3, 3, 7);
    b.emit_self_pid(4);
    b.emit_move(5, 2);
    b.emit_load_imm(6, TAG_PONG);
    b.emit_move(7, 3);
    emit_native_n!(b, 4, N_MAKE_MSG, 4);
    b.emit_send(1, 4);
    b.emit_exit(4);

    // main: spawn pong (Cap), hop Message{tag=PING, payload=1}, return reply payload
    b.begin_function("main", 0, 8);
    b.emit_self_pid(1);
    b.emit_spawn(0, pong, 0);
    b.emit_move(2, 1);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_PING);
    b.emit_load_imm(5, 1);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    b.emit_send(0, 2);
    b.emit_receive(6);
    emit_native1_from!(b, 4, 6, N_MSG_PAYLOAD);
    b.emit_return(4);

    b.finish()
}

/// Atomic request-reply with [`crate::Value::Message`] (one envelope per hop).
///
/// Requires [`crate::std_native_table`] (`make_msg` / `msg_*` / `print`).
///
/// # Why "Atomic Hop"
///
/// Correlation (`request_id`) and reply routing (`sender`) travel in a
/// **single** mailbox value. Classic actor runtimes often allow any scalar
/// on `Send`; Byteflow rejects that ÔÇö every hop is a typed envelope.
///
/// # Protocol
///
/// 1. `main` spawns `server`, builds
///    `Message { id=1, tag=REQ, payload=41 }` (sender placeholder ignored)
/// 2. `server` receives, logs via `print`, replies
///    `tag=REP, payload=42` via `msg_reply_cap` (echoing `request_id`)
/// 3. `main` returns the reply payload `42`
///
/// # Register discipline (`CallNative` clobbers `r[a]`)
///
/// `CallNative ra, , nc` reads args from `r[a..a+nc]` and writes the
/// result into `r[a]`. Keeping the original `Message` in `r0` therefore
/// means every unpack is `Move ri, r0` then `CallNative ri, msg_*, 1`.
/// `make_msg` needs four **contiguous** arg registers; the server rearranges
/// into `r4..r7` before the call.
pub fn atomic_request_reply() -> Chunk {
    let mut b = ChunkBuilder::new("atomic-request-reply");

    // --- server -----------------------------------------------------------
    // r0  = request Message (never overwritten until Exit)
    // r1  = reply Cap (from msg_reply_cap)
    // r2  = request_id
    // r3  = tag
    // r4  = payload, then make_msg result (reply Message)
    // r5  = self Cap, then make_msg arg slot
    // r6  = TAG_REP for make_msg
    // r7  = scratch (print copy, +1 imm, payload for make_msg)
    let server = b.begin_function("server", 0, 8);
    b.emit_receive(0);
    emit_native1_from!(b, 7, 0, N_PRINT);
    emit_native1_from!(b, 1, 0, N_MSG_REPLY_CAP);
    emit_native1_from!(b, 2, 0, N_MSG_REQUEST_ID);
    emit_native1_from!(b, 3, 0, N_MSG_TAG);
    emit_native1_from!(b, 4, 0, N_MSG_PAYLOAD);
    b.emit_load_imm(7, 1);
    b.emit_binop(Opcode::Add, 4, 4, 7);
    b.emit_self_pid(5);
    b.emit_move(7, 4);
    b.emit_move(4, 5);
    b.emit_move(5, 2);
    b.emit_load_imm(6, TAG_REP);
    emit_native_n!(b, 4, N_MAKE_MSG, 4);
    emit_native1_from!(b, 7, 4, N_PRINT);
    b.emit_send(1, 4);
    b.emit_exit(4);

    // --- main -------------------------------------------------------------
    // r0 = server Cap
    // r1 = self Cap
    // r2..r5 = make_msg(self, 1, TAG_REQ, 41)  r2 becomes the request Message
    // r6 = reply Message
    // r7 = scratch for print / unpack
    b.begin_function("main", 0, 8);
    b.emit_self_pid(1);
    b.emit_spawn(0, server, 0);
    b.emit_move(2, 1);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_REQ);
    b.emit_load_imm(5, 41);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    emit_native1_from!(b, 7, 2, N_PRINT);
    b.emit_send(0, 2);
    b.emit_receive(6);
    emit_native1_from!(b, 7, 6, N_PRINT);
    emit_native1_from!(b, 4, 6, N_MSG_PAYLOAD);
    b.emit_return(4);

    b.finish()
}

/// Selective Atomic Hop: server waits for `TAG_REQ` while a `TAG_JUNK` hop
/// sits ahead in the mailbox (FIFO skip, not drop).
///
/// Requires [`crate::std_native_table`].
///
/// 1. `main` sends junk (`tag=TAG_JUNK`), then request (`tag=TAG_REQ`, payload=41)
/// 2. `server` does `ReceiveMatchImm TAG_REQ` ÔÇö must see payload 41, not junk
/// 3. replies `TAG_REP` / 42; then classic `Receive` drains the leftover junk
/// 4. `main` returns reply payload `42`
pub fn selective_receive() -> Chunk {
    let mut b = ChunkBuilder::new("selective-receive");

    // server: match TAG_REQ, reply 42 via reply_cap, then drain junk
    let server = b.begin_function("server", 0, 8);
    b.emit_receive_match_imm(0, TAG_REQ as u16);
    emit_native1_from!(b, 1, 0, N_MSG_REPLY_CAP);
    emit_native1_from!(b, 2, 0, N_MSG_REQUEST_ID);
    emit_native1_from!(b, 4, 0, N_MSG_PAYLOAD);
    b.emit_load_imm(7, 1);
    b.emit_binop(Opcode::Add, 4, 4, 7);
    b.emit_self_pid(5);
    b.emit_move(7, 4);
    b.emit_move(4, 5);
    b.emit_move(5, 2);
    b.emit_load_imm(6, TAG_REP);
    emit_native_n!(b, 4, N_MAKE_MSG, 4);
    b.emit_send(1, 4);
    // leftover TAG_JUNK must still be waiting
    b.emit_receive(0);
    emit_native1_from!(b, 3, 0, N_MSG_TAG);
    b.emit_load_imm(7, TAG_JUNK);
    b.emit_binop(Opcode::Eq, 3, 3, 7);
    // Branch jumps when falsy: not-equal  trap
    let trap_lbl = b.new_label();
    b.emit_branch(3, trap_lbl);
    b.emit_exit(4);
    b.bind_label(trap_lbl);
    b.emit_trap(2);

    b.begin_function("main", 0, 8);
    b.emit_self_pid(1);
    b.emit_spawn(0, server, 0);
    // junk hop first
    b.emit_move(2, 1);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_JUNK);
    b.emit_load_imm(5, 0);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    b.emit_send(0, 2);
    // real request
    b.emit_move(2, 1);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_REQ);
    b.emit_load_imm(5, 41);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    b.emit_send(0, 2);
    b.emit_receive_match_imm(6, TAG_REP as u16);
    emit_native1_from!(b, 4, 6, N_MSG_PAYLOAD);
    b.emit_return(4);

    b.finish()
}

/// Atomic request/reply via [`Opcode::Ask`] (RPC hop).
///
/// Requires [`crate::std_native_table`].
///
/// 1. `main` builds `Message { id=1, tag=REQ, payload=41 }` and `Ask`s the server Cap
/// 2. `server` `ReceiveMatchImm TAG_REQ`, replies via `msg_reply_cap` with
///    `TAG_REP` / payload 42 and the same `request_id`
/// 3. `Ask` resumes with the reply; `main` returns payload `42`
pub fn ask_reply() -> Chunk {
    let mut b = ChunkBuilder::new("ask-reply");

    let server = b.begin_function("server", 0, 8);
    b.emit_receive_match_imm(0, TAG_REQ as u16);
    emit_native1_from!(b, 1, 0, N_MSG_REPLY_CAP);
    emit_native1_from!(b, 2, 0, N_MSG_REQUEST_ID);
    emit_native1_from!(b, 4, 0, N_MSG_PAYLOAD);
    b.emit_load_imm(7, 1);
    b.emit_binop(Opcode::Add, 4, 4, 7);
    b.emit_self_pid(5);
    b.emit_move(7, 4);
    b.emit_move(4, 5);
    b.emit_move(5, 2);
    b.emit_load_imm(6, TAG_REP);
    emit_native_n!(b, 4, N_MAKE_MSG, 4);
    b.emit_send(1, 4);
    b.emit_exit(4);

    // main: Ask r6, r0 (server Cap), r2 (request Message)
    b.begin_function("main", 0, 8);
    b.emit_self_pid(1);
    b.emit_spawn(0, server, 0);
    b.emit_move(2, 1);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_REQ);
    b.emit_load_imm(5, 41);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    b.emit_ask(6, 0, 2);
    emit_native1_from!(b, 4, 6, N_MSG_PAYLOAD);
    b.emit_return(4);

    b.finish()
}

/// Security regression sample: forged `make_msg` sender must not survive `Send`.
///
/// # What this proves
///
/// Invariant **S1** from `docs/security.md`: structural Atomic Hop typing
/// alone cannot stop a module from writing `sender = 999` into a
/// [`crate::Message`]. The scheduler overwrites that field on bytecode
/// `Send`, so the serverÔÇÖs `msg_sender` / echoed payload reflects the **real**
/// client flow id.
///
/// # Protocol
///
/// 1. `main` builds a request with forged sender `999` and `Send`s it.
/// 2. `server` reads the delivered hop, puts authenticated `msg_sender` into
///    the reply `payload`, and answers.
/// 3. `main` returns that payload as `Int`.
///
/// Unit tests assert the returned id is not `999` (and is a plausible live
/// flow id). Requires [`crate::std_native_table`].
pub fn forged_sender_send() -> Chunk {
    let mut b = ChunkBuilder::new("forged-sender-send");

    let server = b.begin_function("server", 0, 8);
    b.emit_receive(0);
    emit_native1_from!(b, 1, 0, N_MSG_REPLY_CAP);
    emit_native1_from!(b, 2, 0, N_MSG_REQUEST_ID);
    emit_native1_from!(b, 3, 0, N_MSG_SENDER);
    // reply: payload = authenticated sender FlowId
    b.emit_self_pid(4);
    b.emit_move(5, 2);
    b.emit_load_imm(6, TAG_REP);
    b.emit_move(7, 3);
    emit_native_n!(b, 4, N_MAKE_MSG, 4);
    b.emit_send(1, 4);
    b.emit_exit(4);

    b.begin_function("main", 0, 8);
    b.emit_self_pid(1);
    b.emit_spawn(0, server, 0);
    // Deliberately forge sender = 999
    b.emit_load_imm(2, 999);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_REQ);
    b.emit_load_imm(5, 0);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    b.emit_send(0, 2);
    b.emit_receive(6);
    emit_native1_from!(b, 4, 6, N_MSG_PAYLOAD);
    b.emit_return(4);

    b.finish()
}

/// Same security property as [`forged_sender_send`], via [`Opcode::Ask`].
///
/// Covers the request half of **S1** on the RPC path: a forged
/// `make_msg.sender` is stamped away before the server observes the hop.
/// Reply authenticity (**S2**, `sender == target`) is covered separately by
/// mailbox unit tests (`ask_requires_reply_from_target`).
pub fn forged_sender_ask() -> Chunk {
    let mut b = ChunkBuilder::new("forged-sender-ask");

    let server = b.begin_function("server", 0, 8);
    b.emit_receive_match_imm(0, TAG_REQ as u16);
    emit_native1_from!(b, 1, 0, N_MSG_REPLY_CAP);
    emit_native1_from!(b, 2, 0, N_MSG_REQUEST_ID);
    emit_native1_from!(b, 3, 0, N_MSG_SENDER);
    b.emit_self_pid(4);
    b.emit_move(5, 2);
    b.emit_load_imm(6, TAG_REP);
    b.emit_move(7, 3);
    emit_native_n!(b, 4, N_MAKE_MSG, 4);
    b.emit_send(1, 4);
    b.emit_exit(4);

    b.begin_function("main", 0, 8);
    b.emit_self_pid(1);
    b.emit_spawn(0, server, 0);
    b.emit_load_imm(2, 999);
    b.emit_load_imm(3, 1);
    b.emit_load_imm(4, TAG_REQ);
    b.emit_load_imm(5, 0);
    emit_native_n!(b, 2, N_MAKE_MSG, 4);
    b.emit_ask(6, 0, 2);
    emit_native1_from!(b, 4, 6, N_MSG_PAYLOAD);
    b.emit_return(4);

    b.finish()
}

/// Immediate `Trap` ÔÇö used to show [`crate::Supervisor`] restart.
pub fn boom() -> Chunk {
    let mut b = ChunkBuilder::new("boom");
    b.begin_function("boom", 0, 1);
    b.emit_trap(1);
    b.finish()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        decode, encode, std_native_table, verify, FlowOutcome, Runtime, RuntimeConfig, Value,
    };

    fn tiny(chunk: Chunk) -> Runtime {
        Runtime::with_config(
            chunk,
            RuntimeConfig {
                workers: 1,
                quantum: 10_000,
            },
        )
        .expect("runtime")
    }

    fn tiny_natives(chunk: Chunk) -> Runtime {
        Runtime::with_natives_and_config(
            chunk,
            std_native_table(),
            RuntimeConfig {
                workers: 1,
                quantum: 10_000,
            },
        )
        .expect("runtime")
    }

    #[test]
    fn add_forty_two_joins_42() {
        let rt = tiny(add_forty_two());
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        rt.shutdown();
        assert!(matches!(outcome, FlowOutcome::Completed(Value::Int(42))));
    }

    #[test]
    fn ping_pong_joins_2() {
        let chunk = ping_pong();
        assert!(verify(&chunk).is_ok());
        let bytes = encode(&chunk);
        let chunk = decode(&bytes).expect("decode");
        let rt = tiny_natives(chunk);
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        let sent = rt.metrics().messages_sent;
        rt.shutdown();
        assert!(matches!(outcome, FlowOutcome::Completed(Value::Int(2))));
        assert!(sent >= 2);
    }

    #[test]
    fn atomic_request_reply_joins_42() {
        let chunk = atomic_request_reply();
        assert!(verify(&chunk).is_ok());
        let bytes = encode(&chunk);
        let chunk = decode(&bytes).expect("decode");
        let rt = tiny_natives(chunk);
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        let sent = rt.metrics().messages_sent;
        rt.shutdown();
        assert!(
            matches!(outcome, FlowOutcome::Completed(Value::Int(42))),
            "got {outcome:?}"
        );
        assert!(sent >= 1);
    }

    #[test]
    fn selective_receive_skips_junk_tag() {
        let chunk = selective_receive();
        assert!(verify(&chunk).is_ok());
        let bytes = encode(&chunk);
        let chunk = decode(&bytes).expect("decode");
        let rt = tiny_natives(chunk);
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        rt.shutdown();
        assert!(
            matches!(outcome, FlowOutcome::Completed(Value::Int(42))),
            "got {outcome:?}"
        );
    }

    #[test]
    fn ask_reply_joins_42() {
        let chunk = ask_reply();
        assert!(verify(&chunk).is_ok());
        let bytes = encode(&chunk);
        let chunk = decode(&bytes).expect("decode");
        let rt = tiny_natives(chunk);
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        let sent = rt.metrics().messages_sent;
        rt.shutdown();
        assert!(
            matches!(outcome, FlowOutcome::Completed(Value::Int(42))),
            "got {outcome:?}"
        );
        // request + reply
        assert!(sent >= 2);
    }

    #[test]
    fn send_overwrites_forged_sender() {
        // S1: make_msg(sender=999, ) + Send  receiver must not see 999.
        let chunk = forged_sender_send();
        assert!(verify(&chunk).is_ok());
        let rt = tiny_natives(chunk);
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        rt.shutdown();
        match outcome {
            FlowOutcome::Completed(Value::Int(n)) => {
                assert_ne!(n, 999, "forged make_msg sender must not survive Send");
                assert!(n >= 1, "authenticated sender must be a live flow id");
            }
            other => panic!("expected Completed(Int), got {other:?}"),
        }
    }

    #[test]
    fn ask_overwrites_forged_request_sender() {
        // S1 on the Ask request path (same forge, RPC hop).
        let chunk = forged_sender_ask();
        assert!(verify(&chunk).is_ok());
        let rt = tiny_natives(chunk);
        let idx = rt.function_index("main").expect("main");
        let outcome = rt.spawn(idx, &[]).expect("spawn").join();
        rt.shutdown();
        match outcome {
            FlowOutcome::Completed(Value::Int(n)) => {
                assert_ne!(n, 999, "forged make_msg sender must not survive Ask");
                assert!(n >= 1, "authenticated sender must be a live flow id");
            }
            other => panic!("expected Completed(Int), got {other:?}"),
        }
    }

    #[test]
    fn send_scalar_target_traps() {
        let mut b = ChunkBuilder::new("bad-cap-target");
        b.begin_function("main", 0, 6);
        b.emit_load_imm(0, 99); // Int ÔÇö not Cap
        b.emit_load_imm(1, 0);
        b.emit_load_imm(2, 1);
        b.emit_load_imm(3, TAG_PING);
        b.emit_load_imm(4, 1);
        emit_native_n!(b, 1, N_MAKE_MSG, 4);
        b.emit_send(0, 1);
        b.emit_return(1);
        let rt = tiny_natives(b.finish());
        let outcome = rt.spawn(0, &[]).expect("spawn").join();
        rt.shutdown();
        assert!(
            matches!(outcome, FlowOutcome::Failed(_)),
            "non-Cap Send target must fail, got {outcome:?}"
        );
    }

    #[test]
    fn send_scalar_is_not_an_atomic_hop() {
        let mut b = ChunkBuilder::new("bad-hop");
        b.begin_function("main", 0, 2);
        b.emit_self_pid(0);
        b.emit_load_imm(1, 99);
        b.emit_send(0, 1);
        b.emit_return(1);
        let rt = tiny(b.finish());
        let outcome = rt.spawn(0, &[]).expect("spawn").join();
        rt.shutdown();
        assert!(
            matches!(outcome, FlowOutcome::Failed(_)),
            "scalar Send must trap, got {outcome:?}"
        );
    }
}