coralstack-cmd-ipc 0.3.0

Inter-Process Communication library for running typed Commands across processes and services
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
//! Conformance harness for the cmd-ipc protocol.
//!
//! Loads the shared vectors under `spec/conformance/` and drives them
//! against the Rust [`CommandRegistry`]. The same vectors are executed
//! by the TypeScript harness at `ts/packages/cmd-ipc/tests/conformance/` —
//! passing both is the contract.

use std::collections::{HashMap, VecDeque};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use coralstack_cmd_ipc::{
    ChannelError, CommandChannel, CommandError, CommandRegistry, Config, DynCommand, DynEvent,
    Message,
};
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures::channel::oneshot;
use futures::executor::{block_on, ThreadPool};
use futures::future::{BoxFuture, Shared};
use futures::task::SpawnExt;
use futures::{FutureExt, StreamExt};
use serde_json::{json, Value};

// ---------- spec-path discovery ----------

fn spec_dir() -> PathBuf {
    // CARGO_MANIFEST_DIR = rust/crates/cmd-ipc. spec/ is three levels up.
    let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    let mut dir = manifest.clone();
    for _ in 0..6 {
        let candidate = dir.join("spec");
        if candidate.join("conformance").is_dir() {
            return candidate;
        }
        if !dir.pop() {
            break;
        }
    }
    panic!(
        "could not locate spec/ directory above {}",
        manifest.display()
    );
}

fn list_vectors(dir: &Path) -> Vec<PathBuf> {
    let mut out: Vec<PathBuf> = fs::read_dir(dir)
        .unwrap_or_else(|e| panic!("read_dir {}: {e}", dir.display()))
        .filter_map(|e| e.ok().map(|e| e.path()))
        .filter(|p| p.extension().map(|e| e == "json").unwrap_or(false))
        .collect();
    out.sort();
    out
}

fn read_json(path: &Path) -> Value {
    let text = fs::read_to_string(path).unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
    serde_json::from_str(&text).unwrap_or_else(|e| panic!("parse {}: {e}", path.display()))
}

// ---------- matchers ----------

type CaptureBag = HashMap<String, Value>;

/// Match `actual` against `expected`, recognising `$match`, `$capture`,
/// `$ref`, and `$unordered` patterns. Returns `Err` with a diagnostic path
/// on mismatch; mutates `captures` on `$capture` success.
fn match_value(
    expected: &Value,
    actual: &Value,
    captures: &mut CaptureBag,
    path: &str,
) -> Result<(), String> {
    // Pattern objects.
    if let Some(obj) = expected.as_object() {
        if let Some(kind) = obj.get("$match") {
            return match kind.as_str() {
                Some("uuid") => {
                    let s = actual
                        .as_str()
                        .ok_or_else(|| format!("{path}: expected UUID string, got {actual}"))?;
                    if uuid::Uuid::parse_str(s).is_ok() {
                        Ok(())
                    } else {
                        Err(format!("{path}: expected UUID, got {s:?}"))
                    }
                }
                Some("any-string") => {
                    let s = actual.as_str().ok_or_else(|| {
                        format!("{path}: expected non-empty string, got {actual}")
                    })?;
                    if s.is_empty() {
                        Err(format!("{path}: expected non-empty string"))
                    } else {
                        Ok(())
                    }
                }
                other => Err(format!("{path}: unknown $match kind {other:?}")),
            };
        }
        if let Some(name) = obj.get("$capture").and_then(Value::as_str) {
            captures.insert(name.to_string(), actual.clone());
            return Ok(());
        }
        if let Some(name) = obj.get("$ref").and_then(Value::as_str) {
            let prev = captures
                .get(name)
                .ok_or_else(|| format!("{path}: $ref to unknown capture {name:?}"))?;
            if prev == actual {
                return Ok(());
            }
            return Err(format!(
                "{path}: $ref {name}: expected {prev}, got {actual}"
            ));
        }
        if let Some(Value::Array(items)) = obj.get("$unordered") {
            let actual_arr = actual
                .as_array()
                .ok_or_else(|| format!("{path}: $unordered needs array actual"))?;
            if items.len() != actual_arr.len() {
                return Err(format!(
                    "{path}: $unordered length mismatch: expected {}, got {}",
                    items.len(),
                    actual_arr.len()
                ));
            }
            let mut remaining: Vec<&Value> = actual_arr.iter().collect();
            for (i, exp) in items.iter().enumerate() {
                let mut found = None;
                for (j, cand) in remaining.iter().enumerate() {
                    let mut snapshot = captures.clone();
                    if match_value(exp, cand, &mut snapshot, &format!("{path}[{i}]")).is_ok() {
                        *captures = snapshot;
                        found = Some(j);
                        break;
                    }
                }
                match found {
                    Some(j) => {
                        remaining.remove(j);
                    }
                    None => {
                        return Err(format!(
                            "{path}: $unordered: no actual element matches expected[{i}] = {exp}"
                        ))
                    }
                }
            }
            return Ok(());
        }

        // Plain object — every key in expected must match; extra actual keys allowed.
        let actual_obj = actual
            .as_object()
            .ok_or_else(|| format!("{path}: expected object, got {actual}"))?;
        for (k, v) in obj {
            let a = actual_obj
                .get(k)
                .ok_or_else(|| format!("{path}.{k}: missing key"))?;
            match_value(v, a, captures, &format!("{path}.{k}"))?;
        }
        return Ok(());
    }

    if let Some(arr) = expected.as_array() {
        let actual_arr = actual
            .as_array()
            .ok_or_else(|| format!("{path}: expected array, got {actual}"))?;
        if arr.len() != actual_arr.len() {
            return Err(format!(
                "{path}: array length mismatch: expected {}, got {}",
                arr.len(),
                actual_arr.len()
            ));
        }
        for (i, (e, a)) in arr.iter().zip(actual_arr.iter()).enumerate() {
            match_value(e, a, captures, &format!("{path}[{i}]"))?;
        }
        return Ok(());
    }

    if expected == actual {
        Ok(())
    } else {
        Err(format!("{path}: expected {expected}, got {actual}"))
    }
}

/// Walk a value replacing `{ "$ref": "name" }` with the captured value.
/// Used on inbound messages so vectors can reference IDs emitted earlier.
fn resolve_refs(value: &Value, captures: &CaptureBag) -> Result<Value, String> {
    match value {
        Value::Object(obj) => {
            if let Some(name) = obj.get("$ref").and_then(Value::as_str) {
                return captures
                    .get(name)
                    .cloned()
                    .ok_or_else(|| format!("inbound $ref to unknown capture {name:?}"));
            }
            let mut out = serde_json::Map::new();
            for (k, v) in obj {
                out.insert(k.clone(), resolve_refs(v, captures)?);
            }
            Ok(Value::Object(out))
        }
        Value::Array(items) => {
            let mut out = Vec::with_capacity(items.len());
            for item in items {
                out.push(resolve_refs(item, captures)?);
            }
            Ok(Value::Array(out))
        }
        other => Ok(other.clone()),
    }
}

// ---------- MockChannel ----------

/// Single-ended test double: `send()` (called by the registry) pushes to
/// an outbound queue the harness can drain; `recv()` reads from an inbound
/// queue the harness feeds via `deliver()`.
struct MockChannel {
    id: String,
    outbound: Mutex<VecDeque<Message>>,
    inbound_tx: UnboundedSender<Message>,
    inbound_rx: futures::lock::Mutex<Option<UnboundedReceiver<Message>>>,
    close_tx: Mutex<Option<oneshot::Sender<()>>>,
    close_rx: Shared<oneshot::Receiver<()>>,
    closed: AtomicBool,
}

impl MockChannel {
    fn new(id: &str) -> Arc<Self> {
        let (tx, rx) = unbounded();
        let (ctx, crx) = oneshot::channel();
        Arc::new(Self {
            id: id.to_string(),
            outbound: Mutex::new(VecDeque::new()),
            inbound_tx: tx,
            inbound_rx: futures::lock::Mutex::new(Some(rx)),
            close_tx: Mutex::new(Some(ctx)),
            close_rx: crx.shared(),
            closed: AtomicBool::new(false),
        })
    }

    fn deliver(&self, msg: Message) {
        let _ = self.inbound_tx.unbounded_send(msg);
    }

    fn take_outbound(&self) -> Option<Message> {
        self.outbound.lock().unwrap().pop_front()
    }

    fn outbound_len(&self) -> usize {
        self.outbound.lock().unwrap().len()
    }

    fn outbound_snapshot(&self) -> Vec<Message> {
        self.outbound.lock().unwrap().iter().cloned().collect()
    }

    fn drain_list_commands_request(&self) {
        self.outbound
            .lock()
            .unwrap()
            .retain(|m| !matches!(m, Message::ListCommandsRequest { .. }));
    }
}

impl CommandChannel for MockChannel {
    fn id(&self) -> &str {
        &self.id
    }

    fn start(&self) -> BoxFuture<'_, Result<(), ChannelError>> {
        Box::pin(async { Ok(()) })
    }

    fn close(&self) -> BoxFuture<'_, ()> {
        Box::pin(async move {
            self.closed.store(true, Ordering::SeqCst);
            if let Some(tx) = self.close_tx.lock().unwrap().take() {
                let _ = tx.send(());
            }
        })
    }

    fn send(&self, msg: Message) -> Result<(), ChannelError> {
        self.outbound.lock().unwrap().push_back(msg);
        Ok(())
    }

    fn recv(&self) -> BoxFuture<'_, Option<Message>> {
        Box::pin(async move {
            if self.closed.load(Ordering::SeqCst) {
                return None;
            }
            let mut guard = self.inbound_rx.lock().await;
            let rx = guard.as_mut()?;
            let close_fut = self.close_rx.clone();
            futures::select_biased! {
                msg = rx.next().fuse() => msg,
                _ = close_fut.fuse() => None,
            }
        })
    }
}

// ---------- encoding vectors ----------

fn run_encoding_vector(file: &Path) -> Result<(), String> {
    let vector = read_json(file);
    let description = vector["description"].as_str().unwrap_or("");
    let message_json = &vector["message"];
    let schema_file = vector["schema"].as_str().ok_or("missing `schema` field")?;
    let canonical_json = vector["json"].as_str().ok_or("missing `json` field")?;

    // 1. Schema validity via typed deserialization through Message.
    let parsed: Message = serde_json::from_value(message_json.clone())
        .map_err(|e| format!("schema validation failed: {e}"))?;

    // 1b. The parsed variant must match the schema filename.
    let expected_type = schema_file_to_type(schema_file)
        .ok_or_else(|| format!("unknown schema reference: {schema_file}"))?;
    let actual_type = message_json["type"].as_str().unwrap_or("");
    if actual_type != expected_type {
        return Err(format!(
            "vector claims schema {schema_file:?} but message.type is {actual_type:?}"
        ));
    }

    // 2. JSON decode matches message.
    let decoded: Value = serde_json::from_str(canonical_json)
        .map_err(|e| format!("JSON parse of canonical string failed: {e}"))?;
    if decoded != *message_json {
        return Err(format!(
            "JSON.parse(json) != message:\n  parsed : {decoded}\n  message: {message_json}"
        ));
    }

    // 3. JSON round-trip via Message (stronger than raw Value round-trip —
    //    exercises serde impls).
    let reserialized =
        serde_json::to_value(&parsed).map_err(|e| format!("serialize roundtrip failed: {e}"))?;
    if reserialized != *message_json {
        return Err(format!(
            "JSON round-trip mismatch:\n  re-serialized: {reserialized}\n  expected     : {message_json}"
        ));
    }

    let _ = description;
    Ok(())
}

fn schema_file_to_type(schema: &str) -> Option<&'static str> {
    Some(match schema {
        "register.command.request.json" => "register.command.request",
        "register.command.response.json" => "register.command.response",
        "list.commands.request.json" => "list.commands.request",
        "list.commands.response.json" => "list.commands.response",
        "execute.command.request.json" => "execute.command.request",
        "execute.command.response.json" => "execute.command.response",
        "event.json" => "event",
        _ => return None,
    })
}

// ---------- behavior vectors ----------

struct ListenerTrace {
    invocations: u32,
    last_payload: Option<Value>,
}

/// Normalised error shape for local-call results so we can assert on a
/// single `code` string that matches the wire-level error code enum used
/// in both languages (`not_found`, `internal_error`, `invalid_request`,
/// `timeout`, `channel_disconnected`, `duplicate_command`, …).
struct LocalCallErr {
    code: String,
    message: String,
}

fn command_error_to_local_err(e: &coralstack_cmd_ipc::CommandError) -> LocalCallErr {
    use coralstack_cmd_ipc::CommandError;
    let (code, message) = match e {
        CommandError::NotFound(_) => ("not_found", e.to_string()),
        CommandError::InvalidRequest { message, .. } => ("invalid_request", message.clone()),
        CommandError::Internal { message, .. } => ("internal_error", message.clone()),
        CommandError::Timeout => ("timeout", e.to_string()),
        CommandError::ChannelDisconnected => ("channel_disconnected", e.to_string()),
        CommandError::DuplicateCommand(_) => ("duplicate_command", e.to_string()),
        CommandError::InvalidMessage(_) => ("invalid_message", e.to_string()),
        CommandError::Serde(_) => ("internal_error", e.to_string()),
    };
    LocalCallErr {
        code: code.into(),
        message,
    }
}

/// Expression evaluator for `returns.$expr` — the same tiny grammar the TS
/// harness accepts (`request.<ident>` or numeric literal, combined with `+`,
/// `-`, `*`).
fn eval_expr(expr: &str, request: &Value) -> Value {
    let mut acc: Option<f64> = None;
    let mut op = '+';
    let tokens: Vec<&str> = expr
        .split(|c: char| c.is_whitespace())
        .filter(|t| !t.is_empty())
        .collect();
    for tok in tokens {
        match tok {
            "+" | "-" | "*" => op = tok.chars().next().unwrap(),
            _ => {
                let val: f64 = if let Some(rest) = tok.strip_prefix("request.") {
                    request
                        .get(rest)
                        .and_then(Value::as_f64)
                        .unwrap_or(f64::NAN)
                } else {
                    tok.parse::<f64>().unwrap_or(f64::NAN)
                };
                acc = Some(match acc {
                    None => val,
                    Some(a) => match op {
                        '+' => a + val,
                        '-' => a - val,
                        '*' => a * val,
                        _ => f64::NAN,
                    },
                });
            }
        }
    }
    // Represent whole-number results as integers to match TS JSON serialization.
    match acc {
        Some(n) if (n.fract() == 0.0) && (n.abs() < (i64::MAX as f64)) => json!(n as i64),
        Some(n) => json!(n),
        None => Value::Null,
    }
}

fn make_local_handler(
    spec: &Value,
) -> impl Fn(Value) -> BoxFuture<'static, Result<Value, CommandError>> + Send + Sync + 'static {
    let returns = spec.get("returns").cloned().unwrap_or(Value::Null);
    move |req: Value| {
        let returns = returns.clone();
        async move {
            let out = match &returns {
                Value::Object(o) if o.contains_key("$expr") => {
                    let expr = o.get("$expr").and_then(Value::as_str).unwrap_or("");
                    eval_expr(expr, &req)
                }
                other => other.clone(),
            };
            Ok(out)
        }
        .boxed()
    }
}

/// Poll for `cond` to become true, or `timeout` elapses.
fn wait_until<F: Fn() -> bool>(cond: F, timeout: Duration) -> bool {
    let start = Instant::now();
    while start.elapsed() < timeout {
        if cond() {
            return true;
        }
        std::thread::sleep(Duration::from_millis(5));
    }
    cond()
}

fn run_behavior_vector(file: &Path, pool: &ThreadPool) -> Result<(), String> {
    let vector = read_json(file);
    let setup = &vector["setup"];
    let registry_cfg = &setup["registry"];
    let peers = setup["peers"].as_array().cloned().unwrap_or_default();
    let steps = vector["steps"].as_array().cloned().unwrap_or_default();

    let registry_id = registry_cfg["id"].as_str().unwrap_or("main").to_string();
    let router_channel = registry_cfg
        .get("routerChannel")
        .and_then(Value::as_str)
        .map(String::from);

    let registry = CommandRegistry::new(Config {
        id: Some(registry_id),
        router_channel,
        request_ttl: Duration::from_secs(5),
        event_ttl: Duration::from_secs(5),
        max_in_flight_per_channel: 256,
    });

    // Pre-register local commands.
    if let Some(cmds) = registry_cfg.get("localCommands").and_then(Value::as_array) {
        for cmd in cmds {
            let id = cmd["id"].as_str().unwrap_or("").to_string();
            let handler = make_local_handler(cmd);
            block_on(registry.register_command(DynCommand::new(id.clone(), handler)))
                .map_err(|e| format!("registering {id}: {e}"))?;
        }
    }

    // Attach local event listeners.
    let mut listener_traces: HashMap<String, Arc<Mutex<ListenerTrace>>> = HashMap::new();
    if let Some(arr) = registry_cfg
        .get("localEventListeners")
        .and_then(Value::as_array)
    {
        for eid in arr {
            let eid = eid.as_str().unwrap_or("").to_string();
            let trace = Arc::new(Mutex::new(ListenerTrace {
                invocations: 0,
                last_payload: None,
            }));
            listener_traces.insert(eid.clone(), trace.clone());
            let trace_cb = trace.clone();
            // Keep the listener live for the lifetime of the test — we
            // deliberately ignore the unsubscribe closure.
            let _keep = registry.on_dyn(&eid, move |payload| {
                let mut t = trace_cb.lock().unwrap();
                t.invocations += 1;
                t.last_payload = Some(payload);
            });
        }
    }

    // Register mock channels for each peer; spawn driver futures.
    let mut channels: HashMap<String, Arc<MockChannel>> = HashMap::new();
    for peer in &peers {
        let ch_id = peer["channelId"].as_str().unwrap_or("").to_string();
        let ch = MockChannel::new(&ch_id);
        let driver = block_on(registry.register_channel(ch.clone()))
            .map_err(|e| format!("register_channel({ch_id}): {e}"))?;
        pool.spawn(driver)
            .map_err(|e| format!("spawn driver: {e}"))?;
        // Drain the auto-sent list.commands.request emitted by register_channel.
        // Give the driver a moment to observe it first.
        let _ = wait_until(|| ch.outbound_len() > 0, Duration::from_millis(100));
        ch.drain_list_commands_request();
        channels.insert(ch_id, ch);
    }

    let mut captures: CaptureBag = HashMap::new();
    let mut pending_result: Option<oneshot::Receiver<Result<Value, LocalCallErr>>> = None;

    for (i, step) in steps.iter().enumerate() {
        let direction = step["direction"].as_str().unwrap_or("");
        let tag = format!("step[{i}] ({direction})");

        match direction {
            "inbound" => {
                let ch_id = step["from"].as_str().unwrap_or("");
                let ch = channels
                    .get(ch_id)
                    .ok_or_else(|| format!("{tag}: unknown channel {ch_id:?}"))?;
                let resolved = resolve_refs(&step["message"], &captures)?;
                let msg: Message = serde_json::from_value(resolved)
                    .map_err(|e| format!("{tag}: inbound deserialize: {e}"))?;
                ch.deliver(msg);
                std::thread::sleep(Duration::from_millis(15));
            }
            "outbound" => {
                let ch_id = step["to"].as_str().unwrap_or("");
                let ch = channels
                    .get(ch_id)
                    .ok_or_else(|| format!("{tag}: unknown channel {ch_id:?}"))?;
                let ch_poll = ch.clone();
                if !wait_until(|| ch_poll.outbound_len() > 0, Duration::from_millis(1000)) {
                    return Err(format!("{tag}: expected outbound on {ch_id}, got none"));
                }
                let actual = ch.take_outbound().unwrap();
                let actual_json = serde_json::to_value(&actual).unwrap();
                match_value(
                    &step["expected"],
                    &actual_json,
                    &mut captures,
                    &format!("$[{i}]"),
                )?;
            }
            "assert-no-outbound" => {
                let ch_id = step["to"].as_str().unwrap_or("");
                let ch = channels
                    .get(ch_id)
                    .ok_or_else(|| format!("{tag}: unknown channel {ch_id:?}"))?;
                std::thread::sleep(Duration::from_millis(30));
                if ch.outbound_len() != 0 {
                    return Err(format!(
                        "{tag}: expected no outbound, got {:?}",
                        ch.outbound_snapshot()
                    ));
                }
            }
            "local-call" => {
                let trigger = &step["trigger"];
                if let Some(args) = trigger.get("executeCommand").and_then(Value::as_array) {
                    let cmd = args
                        .first()
                        .and_then(Value::as_str)
                        .unwrap_or("")
                        .to_string();
                    let req = args.get(1).cloned().unwrap_or(Value::Null);
                    let (tx, rx) = oneshot::channel::<Result<Value, LocalCallErr>>();
                    let registry_clone = registry.clone();
                    pool.spawn(async move {
                        let result = registry_clone.execute_dyn(&cmd, req).await;
                        let _ = tx.send(result.map_err(|e| command_error_to_local_err(&e)));
                    })
                    .map_err(|e| format!("{tag}: spawn: {e}"))?;
                    pending_result = Some(rx);
                } else if let Some(args) = trigger.get("emitEvent").and_then(Value::as_array) {
                    let eid = args
                        .first()
                        .and_then(Value::as_str)
                        .unwrap_or("")
                        .to_string();
                    let payload = args.get(1).cloned().unwrap_or(Value::Null);
                    registry
                        .emit(DynEvent::new(eid, payload))
                        .map_err(|e| format!("{tag}: emit: {e}"))?;
                } else if let Some(args) = trigger.get("registerCommand").and_then(Value::as_array)
                {
                    let id = args
                        .first()
                        .and_then(Value::as_str)
                        .unwrap_or("")
                        .to_string();
                    let (tx, rx) = oneshot::channel::<Result<Value, LocalCallErr>>();
                    let registry_clone = registry.clone();
                    pool.spawn(async move {
                        let result = registry_clone
                            .register_command(DynCommand::new(id, |_v: Value| async move {
                                Ok::<Value, CommandError>(Value::Null)
                            }))
                            .await;
                        let _ = tx.send(
                            result
                                .map(|_| Value::Null)
                                .map_err(|e| command_error_to_local_err(&e)),
                        );
                    })
                    .map_err(|e| format!("{tag}: spawn: {e}"))?;
                    pending_result = Some(rx);
                } else if trigger.get("listCommands").is_some() {
                    let commands = registry.list_commands();
                    let value = serde_json::to_value(
                        commands
                            .into_iter()
                            .map(|c| serde_json::json!({ "id": c.id }))
                            .collect::<Vec<_>>(),
                    )
                    .unwrap();
                    let (tx, rx) = oneshot::channel::<Result<Value, LocalCallErr>>();
                    let _ = tx.send(Ok(value));
                    pending_result = Some(rx);
                } else {
                    return Err(format!("{tag}: unknown trigger"));
                }
                std::thread::sleep(Duration::from_millis(15));
            }
            "local-result" => {
                let rx = pending_result
                    .take()
                    .ok_or_else(|| format!("{tag}: no pending local-call"))?;
                let result = block_on(async {
                    futures::select_biased! {
                        r = rx.fuse() => r.unwrap_or_else(|_| Err(LocalCallErr { code: "canceled".into(), message: "canceled".into() })),
                        _ = sleep_future(Duration::from_secs(2)).fuse() => Err(LocalCallErr { code: "timeout".into(), message: "timeout waiting for local-result".into() }),
                    }
                });
                if let Some(expected_err) = step.get("expectedError") {
                    match result {
                        Ok(v) => {
                            return Err(format!("{tag}: expected error but got ok value {v}"));
                        }
                        Err(e) => {
                            if let Some(code) = expected_err.get("code").and_then(Value::as_str) {
                                if e.code != code {
                                    return Err(format!(
                                        "{tag}: expected error code {code:?}, got {:?} ({})",
                                        e.code, e.message
                                    ));
                                }
                            }
                            if let Some(msg_pat) = expected_err.get("message") {
                                match_value(
                                    msg_pat,
                                    &Value::String(e.message.clone()),
                                    &mut captures,
                                    &format!("$[{i}].message"),
                                )?;
                            }
                        }
                    }
                } else {
                    let value = result.map_err(|e| {
                        format!("{tag}: local-call rejected: {} ({})", e.code, e.message)
                    })?;
                    if let Some(expected) = step.get("expected") {
                        match_value(expected, &value, &mut captures, &format!("$[{i}].result"))?;
                    }
                }
            }
            "close-channel" => {
                let ch_id = step["channel"].as_str().unwrap_or("");
                let ch = channels
                    .get(ch_id)
                    .ok_or_else(|| format!("{tag}: unknown channel {ch_id:?}"))?
                    .clone();
                block_on(ch.close());
                // Give the driver task time to observe EOF and run
                // handle_channel_close so remote commands are purged.
                std::thread::sleep(Duration::from_millis(60));
            }
            "assert-local-listener" => {
                let eid = step["eventId"].as_str().unwrap_or("");
                let trace = listener_traces
                    .get(eid)
                    .ok_or_else(|| format!("{tag}: no listener registered for {eid:?} — add it to setup.registry.localEventListeners"))?;
                std::thread::sleep(Duration::from_millis(15));
                let expected = step["invocations"].as_u64().unwrap_or(0) as u32;
                let t = trace.lock().unwrap();
                if t.invocations != expected {
                    return Err(format!(
                        "{tag}: expected {expected} invocations of {eid:?}, got {}",
                        t.invocations
                    ));
                }
                if let Some(last) = step.get("lastPayload") {
                    if !last.is_null() {
                        let actual = t.last_payload.clone().unwrap_or(Value::Null);
                        match_value(last, &actual, &mut captures, &format!("$[{i}].lastPayload"))?;
                    }
                }
            }
            other => return Err(format!("{tag}: unknown direction {other:?}")),
        }
    }

    // No unasserted outbound unless last step was assert-no-outbound.
    let last_dir = steps
        .last()
        .and_then(|s| s["direction"].as_str())
        .unwrap_or("");
    if last_dir != "assert-no-outbound" {
        for (id, ch) in &channels {
            std::thread::sleep(Duration::from_millis(10));
            if ch.outbound_len() > 0 {
                return Err(format!(
                    "unasserted outbound messages on channel {id:?}: {:?}",
                    ch.outbound_snapshot()
                ));
            }
        }
    }

    block_on(registry.dispose());
    Ok(())
}

fn sleep_future(d: Duration) -> impl std::future::Future<Output = ()> {
    let (tx, rx) = oneshot::channel();
    std::thread::spawn(move || {
        std::thread::sleep(d);
        let _ = tx.send(());
    });
    async move {
        let _ = rx.await;
    }
}

// ---------- top-level tests ----------

fn run_suite<F: Fn(&Path) -> Result<(), String>>(
    label: &str,
    files: Vec<PathBuf>,
    runner: F,
) -> Vec<(String, String)> {
    let mut failures = Vec::new();
    for file in files {
        let name = file
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("<?>")
            .to_string();
        match runner(&file) {
            Ok(()) => eprintln!("{label} {name}"),
            Err(e) => {
                eprintln!("{label} {name}\n      {e}");
                failures.push((format!("{label} {name}"), e));
            }
        }
    }
    failures
}

#[test]
fn encoding_vectors() {
    let dir = spec_dir().join("conformance").join("encoding");
    let files = list_vectors(&dir);
    assert!(
        !files.is_empty(),
        "no encoding vectors in {}",
        dir.display()
    );
    let failures = run_suite("encoding", files, run_encoding_vector);
    if !failures.is_empty() {
        panic!("{} encoding vector(s) failed", failures.len());
    }
}

#[test]
fn behavior_vectors() {
    let dir = spec_dir().join("conformance").join("behavior");
    let files = list_vectors(&dir);
    assert!(
        !files.is_empty(),
        "no behavior vectors in {}",
        dir.display()
    );
    let pool = ThreadPool::new().expect("ThreadPool");
    let failures = run_suite("behavior", files, |f| run_behavior_vector(f, &pool));
    if !failures.is_empty() {
        panic!("{} behavior vector(s) failed", failures.len());
    }
}