ringo-agent 0.2.6

Run a SIP user agent as its own process, driven over a framed stdio protocol
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
//! The wire protocol between the parent and a `ringo-flow agent` worker process.
//!
//! Length-prefixed frames (`[kind][len][payload]`, see [`write_frame`]) so JSON
//! control and raw binary audio share one stream per direction:
//!   * **parent → child** (worker stdin): control frames carrying [`ToWorker`]
//!     (a [`Command`] or an id-correlated [`Query`]), plus raw audio frames (TTS
//!     PCM played into the call after `StartTxAudio`).
//!   * **child → parent** (worker stdout): control frames carrying [`FromWorker`]
//!     (events, query replies, header pushes, readiness), plus raw audio frames
//!     (received-call PCM after `StartRxAudio`, preceded by `RxAudioStarted`).
//!
//! The worker's **stderr** carries logs / SIP traces and is never part of the
//! protocol, so the framed stream stays clean.

use ringo_core::account::{Account, BackendOptions};
use ringo_core::event::{AppEvent, InviteHeaders, MediaStats};
use serde::{Deserialize, Serialize};
use std::io::{self, Read, Write};

// ── wire framing ──────────────────────────────────────────────────────────--
//
// The wire is length-prefixed frames (not line-delimited), so raw binary audio
// and JSON control can share one stream without escaping: `[kind:u8][len:u32
// LE][payload]`. A control frame's payload is JSON (a `ToWorker`/`FromWorker`);
// an audio frame's payload is raw mono s16le PCM.

/// Frame kind: JSON control message.
pub(crate) const FRAME_CONTROL: u8 = 0;
/// Frame kind: raw mono s16le PCM audio.
pub(crate) const FRAME_AUDIO: u8 = 1;

/// Reject absurd frame lengths from a corrupt stream (far above any real frame).
const MAX_FRAME_LEN: usize = 16 * 1024 * 1024;

/// Write one frame: `[kind][len][payload]`, flushed.
pub(crate) fn write_frame<W: Write>(w: &mut W, kind: u8, payload: &[u8]) -> io::Result<()> {
    if payload.len() > MAX_FRAME_LEN {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "frame too large",
        ));
    }
    w.write_all(&[kind])?;
    w.write_all(&(payload.len() as u32).to_le_bytes())?;
    w.write_all(payload)?;
    w.flush()
}

/// Read one frame; `Ok(None)` only on a *clean* EOF at a frame boundary (no
/// bytes of a new frame seen). EOF partway through the header or payload is a
/// torn frame and surfaces as an error, not a clean close.
pub(crate) fn read_frame<R: Read>(r: &mut R) -> io::Result<Option<(u8, Vec<u8>)>> {
    let mut hdr = [0u8; 5];
    // Read the header byte-by-byte so we can tell "nothing arrived" (clean EOF)
    // from "EOF mid-header" (torn frame). read_exact can't distinguish them.
    let mut filled = 0;
    while filled < hdr.len() {
        match r.read(&mut hdr[filled..]) {
            Ok(0) if filled == 0 => return Ok(None), // clean frame-boundary EOF
            Ok(0) => {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "EOF in frame header",
                ));
            }
            Ok(n) => filled += n,
            Err(e) if e.kind() == io::ErrorKind::Interrupted => continue,
            Err(e) => return Err(e),
        }
    }
    let kind = hdr[0];
    let len = u32::from_le_bytes([hdr[1], hdr[2], hdr[3], hdr[4]]) as usize;
    if len > MAX_FRAME_LEN {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "frame too large",
        ));
    }
    let mut payload = vec![0u8; len];
    r.read_exact(&mut payload)?;
    Ok(Some((kind, payload)))
}

/// Mono s16 samples → little-endian audio-frame bytes.
pub(crate) fn pcm_to_bytes(samples: &[i16]) -> Vec<u8> {
    let mut v = Vec::with_capacity(samples.len() * 2);
    for &s in samples {
        v.extend_from_slice(&s.to_le_bytes());
    }
    v
}

/// Little-endian audio-frame bytes → mono s16 samples (trailing odd byte ignored).
pub(crate) fn bytes_to_pcm(bytes: &[u8]) -> Vec<i16> {
    bytes
        .chunks_exact(2)
        .map(|c| i16::from_le_bytes([c[0], c[1]]))
        .collect()
}

// ── handshake ─────────────────────────────────────────────────────────────--

/// Wire-protocol version. Parent and worker are normally the same binary, but a
/// stale worker (old binary on `$PATH`, a half-finished upgrade) would otherwise
/// desync silently on the framed stream. The worker rejects a mismatching
/// version at the handshake instead. Bump on any incompatible wire change.
pub(crate) const PROTO_VERSION: u32 = 2;

/// The first frame the parent sends: the protocol version plus the agent config.
/// Versioning lives here, on the wire envelope, so the public [`AgentConfig`]
/// stays a plain user-facing struct.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct Handshake {
    /// Sender's [`PROTO_VERSION`]; the worker bails if it doesn't match its own.
    pub proto_version: u32,
    /// The agent configuration to spawn.
    pub config: AgentConfig,
}

/// The full agent configuration the parent sends as the worker's first stdin
/// line (before any command/query). Carrying it on stdin rather than argv keeps
/// credentials out of the process table. The `Account`/`BackendOptions` are
/// embedded verbatim (they are `Serialize`/`Deserialize` in ringo-core), so the
/// worker rebuilds the *exact* account the in-process path would — adding a
/// field to either struct can't silently drift out of sync.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AgentConfig {
    /// Label for the agent (used in logs / per-agent log file names).
    pub name: String,
    /// The SIP account to register.
    pub account: Account,
    /// Backend options (audio driver, timeouts, recording, …).
    pub options: BackendOptions,
}

// ── parent → child ──────────────────────────────────────────────────────────

/// A fire-and-forget phone action.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "cmd", rename_all = "snake_case")]
pub enum Command {
    Register {
        aor: String,
        regint: u32,
    },
    Dial {
        number: String,
    },
    Hangup,
    HangupAll,
    Accept,
    Hold,
    Resume,
    Mute,
    Dtmf {
        digit: char,
    },
    SwitchLine {
        line: usize,
    },
    Transfer {
        uri: String,
    },
    AttendedTransferStart {
        uri: String,
    },
    AttendedTransferExec,
    AttendedTransferAbort,
    AddHeader {
        key: String,
        value: String,
    },
    RmHeader {
        key: String,
    },
    SetAudioSource {
        spec: String,
    },
    /// Arm or disarm full-call recording for this UA (see
    /// `ringo_core::set_recording`): arming clears the previous recording.
    SetRecording {
        on: bool,
    },
    ArmInviteResponse {
        scode: u16,
        reason: String,
        headers: Vec<String>,
    },
    DisarmInviteResponse,
    /// Switch the agent's audio source to live-streamed PCM at `rate` Hz; raw
    /// audio frames (kind=1) on stdin are then played into the call (TTS).
    StartTxAudio {
        rate: u32,
    },
    /// Begin streaming the agent's received audio: the worker emits an
    /// `RxAudioStarted` control message then raw audio frames (kind=1) on stdout.
    StartRxAudio,
    /// Graceful shutdown request (also triggered by stdin EOF).
    Shutdown,
}

/// A request that expects exactly one [`Reply`] with the matching `id`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "query", rename_all = "snake_case")]
pub enum Query {
    /// RTP media stats for the active/last call.
    MediaStats,
    /// DTMF digits received so far.
    ReceivedDtmf,
    /// Write the agent's captured sent/received audio as WAV files named
    /// `<prefix>-sent.wav` / `<prefix>-recv.wav`; returns what was written
    /// (paths, side, size, and whether the retention cap truncated it).
    SaveAudio { prefix: String },
    /// Number of active calls (for teardown's BYE-flush wait).
    CallCount,
}

/// A [`Query`] tagged with a correlation id.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryEnvelope {
    pub id: u64,
    #[serde(flatten)]
    pub query: Query,
}

/// Anything the parent writes to the worker's stdin. Untagged — disjoint by the
/// distinguishing key `cmd` (Command) vs `query` (QueryEnvelope). Do NOT add a
/// variant whose payload reuses either key.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToWorker {
    Cmd(Command),
    Query(QueryEnvelope),
}

// ── child → parent ──────────────────────────────────────────────────────────

/// RTP media stats, mirroring [`MediaStats`] (which carries no serde derives).
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct WireMediaStats {
    pub rtt_ms: f64,
    pub jitter_ms: f64,
    pub rx_lost: i32,
    pub packet_loss_pct: f64,
    pub mos: f64,
}

impl From<MediaStats> for WireMediaStats {
    fn from(s: MediaStats) -> Self {
        Self {
            rtt_ms: s.rtt_ms,
            jitter_ms: s.jitter_ms,
            rx_lost: s.rx_lost,
            packet_loss_pct: s.packet_loss_pct,
            mos: s.mos,
        }
    }
}

impl From<WireMediaStats> for MediaStats {
    fn from(s: WireMediaStats) -> Self {
        Self {
            rtt_ms: s.rtt_ms,
            jitter_ms: s.jitter_ms,
            rx_lost: s.rx_lost,
            packet_loss_pct: s.packet_loss_pct,
            mos: s.mos,
        }
    }
}

/// One WAV written by a `SaveAudio` query: which side of the call it holds,
/// how much audio it contains, and whether the recording hit the retention
/// cap (only the newest `ringo_core::RECORDING_MAX_SECS` seconds are kept).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WireSavedAudio {
    pub path: String,
    /// `"sent"` or `"recv"`.
    pub kind: String,
    /// Captured mono samples at `srate`.
    pub samples: usize,
    /// Sample rate in Hz.
    pub srate: u32,
    /// Whether the recording reached the retention cap.
    pub truncated: bool,
}

/// The payload of a [`Reply`], one variant per [`Query`]. Externally tagged
/// (e.g. `{"call_count":2}`) so newtype variants over primitives/sequences
/// round-trip — internal tagging can't carry those.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReplyResult {
    MediaStats(Option<WireMediaStats>),
    Dtmf(String),
    SavedAudio(Vec<WireSavedAudio>),
    CallCount(u32),
}

/// A response to a [`Query`], correlated by `id` (e.g.
/// `{"reply":7,"result":{"call_count":2}}`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Reply {
    pub reply: u64,
    pub result: ReplyResult,
}

/// A push of received INVITE headers (keyed by SIP Call-ID).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Headers {
    pub headers: InviteHeaders,
}

/// Readiness handshake: the worker emits this as its FIRST stdout message once
/// the backend is up, so [`crate::client::ProcessClient::spawn`] can surface a
/// bad config / failed backend at spawn time instead of as a later query timeout.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ready {
    pub ready: bool,
}

/// Sent once after `StartRxAudio`, before the raw RX audio frames, so the parent
/// knows the sample rate of the mono s16 frames that follow.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RxAudioStarted {
    pub rx_audio_rate: u32,
}

/// A control message the worker writes to its stdout (the `control`-kind frame
/// payload). Untagged — the variants are disjoint by their distinguishing key:
/// `ready` / `event` / `reply` / `headers` / `rx_audio_rate`. Do NOT add a
/// variant whose payload reuses one of those keys.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum FromWorker {
    Ready(Ready),
    Event(WireEvent),
    Reply(Reply),
    Headers(Headers),
    RxAudioStarted(RxAudioStarted),
}

/// A serializable mirror of [`AppEvent`] (which lives in `ringo-core` and
/// carries no serde derives).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum WireEvent {
    Registering {
        account: String,
    },
    RegisterOk {
        account: String,
    },
    RegisterFailed {
        reason: String,
    },
    Unregistered {
        account: String,
    },
    CallIncoming {
        call_id: String,
        number: String,
        display_name: Option<String>,
    },
    CallOutgoing {
        call_id: String,
        number: String,
    },
    CallRinging {
        call_id: String,
    },
    CallEstablished {
        call_id: String,
    },
    CallClosed {
        call_id: String,
        reason: String,
        error: bool,
    },
    CallDeflected {
        from: String,
        display_name: Option<String>,
        target: String,
    },
    CallHold {
        call_id: String,
    },
    CallResume {
        call_id: String,
    },
    CallTransferFailed {
        call_id: String,
    },
    VoicemailStatus {
        waiting: bool,
        new_count: u32,
    },
    Response {
        ok: bool,
        data: String,
    },
    Unknown {
        class: String,
        type_: String,
    },
    BackendConnectFailed {
        reason: String,
    },
}

impl From<&AppEvent> for WireEvent {
    fn from(e: &AppEvent) -> Self {
        match e {
            AppEvent::Registering { account } => Self::Registering {
                account: account.clone(),
            },
            AppEvent::RegisterOk { account } => Self::RegisterOk {
                account: account.clone(),
            },
            AppEvent::RegisterFailed { reason } => Self::RegisterFailed {
                reason: reason.clone(),
            },
            AppEvent::Unregistered { account } => Self::Unregistered {
                account: account.clone(),
            },
            AppEvent::CallIncoming {
                call_id,
                number,
                display_name,
            } => Self::CallIncoming {
                call_id: call_id.clone(),
                number: number.clone(),
                display_name: display_name.clone(),
            },
            AppEvent::CallOutgoing { call_id, number } => Self::CallOutgoing {
                call_id: call_id.clone(),
                number: number.clone(),
            },
            AppEvent::CallRinging { call_id } => Self::CallRinging {
                call_id: call_id.clone(),
            },
            AppEvent::CallEstablished { call_id } => Self::CallEstablished {
                call_id: call_id.clone(),
            },
            AppEvent::CallClosed {
                call_id,
                reason,
                error,
            } => Self::CallClosed {
                call_id: call_id.clone(),
                reason: reason.clone(),
                error: *error,
            },
            AppEvent::CallHold { call_id } => Self::CallHold {
                call_id: call_id.clone(),
            },
            AppEvent::CallResume { call_id } => Self::CallResume {
                call_id: call_id.clone(),
            },
            AppEvent::CallTransferFailed { call_id } => Self::CallTransferFailed {
                call_id: call_id.clone(),
            },
            AppEvent::CallDeflected {
                from,
                display_name,
                target,
            } => Self::CallDeflected {
                from: from.clone(),
                display_name: display_name.clone(),
                target: target.clone(),
            },
            AppEvent::VoicemailStatus { waiting, new_count } => Self::VoicemailStatus {
                waiting: *waiting,
                new_count: *new_count,
            },
            AppEvent::Response { ok, data } => Self::Response {
                ok: *ok,
                data: data.clone(),
            },
            AppEvent::Unknown { class, type_ } => Self::Unknown {
                class: class.clone(),
                type_: type_.clone(),
            },
            AppEvent::BackendConnectFailed { reason } => Self::BackendConnectFailed {
                reason: reason.clone(),
            },
        }
    }
}

impl From<WireEvent> for AppEvent {
    fn from(w: WireEvent) -> Self {
        match w {
            WireEvent::Registering { account } => AppEvent::Registering { account },
            WireEvent::RegisterOk { account } => AppEvent::RegisterOk { account },
            WireEvent::RegisterFailed { reason } => AppEvent::RegisterFailed { reason },
            WireEvent::Unregistered { account } => AppEvent::Unregistered { account },
            WireEvent::CallIncoming {
                call_id,
                number,
                display_name,
            } => AppEvent::CallIncoming {
                call_id,
                number,
                display_name,
            },
            WireEvent::CallOutgoing { call_id, number } => {
                AppEvent::CallOutgoing { call_id, number }
            }
            WireEvent::CallRinging { call_id } => AppEvent::CallRinging { call_id },
            WireEvent::CallEstablished { call_id } => AppEvent::CallEstablished { call_id },
            WireEvent::CallClosed {
                call_id,
                reason,
                error,
            } => AppEvent::CallClosed {
                call_id,
                reason,
                error,
            },
            WireEvent::CallHold { call_id } => AppEvent::CallHold { call_id },
            WireEvent::CallResume { call_id } => AppEvent::CallResume { call_id },
            WireEvent::CallTransferFailed { call_id } => AppEvent::CallTransferFailed { call_id },
            WireEvent::CallDeflected {
                from,
                display_name,
                target,
            } => AppEvent::CallDeflected {
                from,
                display_name,
                target,
            },
            WireEvent::VoicemailStatus { waiting, new_count } => {
                AppEvent::VoicemailStatus { waiting, new_count }
            }
            WireEvent::Response { ok, data } => AppEvent::Response { ok, data },
            WireEvent::Unknown { class, type_ } => AppEvent::Unknown { class, type_ },
            WireEvent::BackendConnectFailed { reason } => AppEvent::BackendConnectFailed { reason },
        }
    }
}

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

    #[test]
    fn command_roundtrips_as_to_worker() {
        let line = serde_json::to_string(&ToWorker::Cmd(Command::Dial {
            number: "sip:1@example.com".into(),
        }))
        .unwrap();
        assert_eq!(line, r#"{"cmd":"dial","number":"sip:1@example.com"}"#);
        assert!(matches!(
            serde_json::from_str::<ToWorker>(&line).unwrap(),
            ToWorker::Cmd(Command::Dial { .. })
        ));
    }

    #[test]
    fn query_envelope_roundtrips_as_to_worker() {
        let line = serde_json::to_string(&ToWorker::Query(QueryEnvelope {
            id: 7,
            query: Query::SaveAudio {
                prefix: "rec".into(),
            },
        }))
        .unwrap();
        let back: ToWorker = serde_json::from_str(&line).unwrap();
        match back {
            ToWorker::Query(QueryEnvelope {
                id,
                query: Query::SaveAudio { prefix },
            }) => {
                assert_eq!((id, prefix.as_str()), (7, "rec"));
            }
            _ => panic!("expected save_audio query"),
        }
    }

    #[test]
    fn event_and_reply_disambiguate_as_from_worker() {
        let ev = serde_json::to_string(&FromWorker::Event(WireEvent::RegisterOk {
            account: "a".into(),
        }))
        .unwrap();
        assert!(matches!(
            serde_json::from_str::<FromWorker>(&ev).unwrap(),
            FromWorker::Event(WireEvent::RegisterOk { .. })
        ));

        let rep = serde_json::to_string(&FromWorker::Reply(Reply {
            reply: 7,
            result: ReplyResult::CallCount(2),
        }))
        .unwrap();
        match serde_json::from_str::<FromWorker>(&rep).unwrap() {
            FromWorker::Reply(Reply {
                reply,
                result: ReplyResult::CallCount(n),
            }) => {
                assert_eq!((reply, n), (7, 2));
            }
            _ => panic!("expected call_count reply"),
        }
    }

    #[test]
    fn app_event_mirrors_both_ways() {
        let ev = AppEvent::CallIncoming {
            call_id: "abc".into(),
            number: "sip:1@x".into(),
            display_name: Some("Bob".into()),
        };
        let line = serde_json::to_string(&WireEvent::from(&ev)).unwrap();
        let back: AppEvent = serde_json::from_str::<WireEvent>(&line).unwrap().into();
        assert!(matches!(back, AppEvent::CallIncoming { .. }));
    }

    #[test]
    fn ready_disambiguates_from_an_event() {
        let ready = serde_json::to_string(&FromWorker::Ready(Ready { ready: true })).unwrap();
        assert!(matches!(
            serde_json::from_str::<FromWorker>(&ready).unwrap(),
            FromWorker::Ready(Ready { ready: true })
        ));
        // An event must NOT be mis-parsed as Ready (disjoint keys).
        let ev = serde_json::to_string(&FromWorker::Event(WireEvent::RegisterOk {
            account: "a".into(),
        }))
        .unwrap();
        assert!(matches!(
            serde_json::from_str::<FromWorker>(&ev).unwrap(),
            FromWorker::Event(_)
        ));
    }

    #[test]
    fn handshake_carries_version_and_config() {
        let mut cfg = AgentConfig {
            name: "A".into(),
            ..Default::default()
        };
        cfg.account.username = "alice".into();
        let hs = Handshake {
            proto_version: PROTO_VERSION,
            config: cfg,
        };
        let line = serde_json::to_string(&hs).unwrap();
        let back: Handshake = serde_json::from_str(&line).unwrap();
        assert_eq!(back.proto_version, PROTO_VERSION);
        assert_eq!(back.config.account.username, "alice");
    }

    #[test]
    fn agent_config_roundtrips_with_embedded_account() {
        let mut cfg = AgentConfig {
            name: "A".into(),
            ..Default::default()
        };
        cfg.account.username = "alice".into();
        cfg.account.catchall = true;
        cfg.options.audio_driver = Some("aubridge".into());
        let line = serde_json::to_string(&cfg).unwrap();
        let back: AgentConfig = serde_json::from_str(&line).unwrap();
        assert_eq!(back.name, "A");
        assert_eq!(back.account.username, "alice");
        assert!(back.account.catchall);
        assert_eq!(back.options.audio_driver.as_deref(), Some("aubridge"));
    }

    #[test]
    fn frames_roundtrip_control_then_audio() {
        let mut buf: Vec<u8> = Vec::new();
        write_frame(&mut buf, FRAME_CONTROL, b"{\"cmd\":\"accept\"}").unwrap();
        write_frame(&mut buf, FRAME_AUDIO, &pcm_to_bytes(&[1, -2, 3])).unwrap();
        let mut r = &buf[..];
        let (k1, p1) = read_frame(&mut r).unwrap().unwrap();
        assert_eq!(k1, FRAME_CONTROL);
        assert_eq!(p1, b"{\"cmd\":\"accept\"}");
        let (k2, p2) = read_frame(&mut r).unwrap().unwrap();
        assert_eq!(k2, FRAME_AUDIO);
        assert_eq!(bytes_to_pcm(&p2), vec![1, -2, 3]);
        assert!(read_frame(&mut r).unwrap().is_none()); // clean EOF
    }

    #[test]
    fn audio_frame_can_contain_newline_bytes() {
        // 0x0A (\n) in PCM must NOT break framing (the whole point of length-prefix).
        let samples = vec![0x0A0Ai16, 0x000A, -1];
        let mut buf = Vec::new();
        write_frame(&mut buf, FRAME_AUDIO, &pcm_to_bytes(&samples)).unwrap();
        let (_k, p) = read_frame(&mut &buf[..]).unwrap().unwrap();
        assert_eq!(bytes_to_pcm(&p), samples);
    }

    #[test]
    fn rx_audio_started_disambiguates_from_event_and_reply() {
        let started = serde_json::to_string(&FromWorker::RxAudioStarted(RxAudioStarted {
            rx_audio_rate: 8000,
        }))
        .unwrap();
        assert!(matches!(
            serde_json::from_str::<FromWorker>(&started).unwrap(),
            FromWorker::RxAudioStarted(RxAudioStarted {
                rx_audio_rate: 8000
            })
        ));
        // Neither an event nor a reply carries the `rx_audio_rate` key.
        let ev = serde_json::to_string(&FromWorker::Event(WireEvent::RegisterOk {
            account: "a".into(),
        }))
        .unwrap();
        let rep = serde_json::to_string(&FromWorker::Reply(Reply {
            reply: 1,
            result: ReplyResult::CallCount(0),
        }))
        .unwrap();
        assert!(matches!(
            serde_json::from_str::<FromWorker>(&ev).unwrap(),
            FromWorker::Event(_)
        ));
        assert!(matches!(
            serde_json::from_str::<FromWorker>(&rep).unwrap(),
            FromWorker::Reply(_)
        ));
    }

    #[test]
    fn read_frame_distinguishes_clean_eof_from_torn_header() {
        // Nothing buffered → clean frame-boundary EOF.
        assert!(read_frame(&mut &b""[..]).unwrap().is_none());
        // A partial header (2 of 5 bytes) is a torn frame, not a clean close.
        let err = read_frame(&mut &[FRAME_CONTROL, 0x01][..]).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::UnexpectedEof);
    }

    #[test]
    fn read_frame_rejects_oversized_length() {
        // Header claiming a payload above MAX_FRAME_LEN must be rejected, not allocated.
        let mut buf = vec![FRAME_AUDIO];
        buf.extend_from_slice(&(MAX_FRAME_LEN as u32 + 1).to_le_bytes());
        let err = read_frame(&mut &buf[..]).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    #[test]
    fn read_frame_errors_on_truncated_payload() {
        // Full header but a short payload (EOF mid-payload) is a torn frame.
        let mut buf = vec![FRAME_AUDIO];
        buf.extend_from_slice(&4u32.to_le_bytes()); // claims 4 bytes
        buf.extend_from_slice(&[1, 2]); // only 2 follow
        let err = read_frame(&mut &buf[..]).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::UnexpectedEof);
    }

    #[test]
    fn audio_commands_disambiguate_as_to_worker_cmd() {
        // The audio control commands must parse as Cmd, not Query (untagged split).
        for cmd in [
            Command::StartRxAudio,
            Command::StartTxAudio { rate: 8000 },
            Command::SetRecording { on: true },
        ] {
            let line = serde_json::to_string(&ToWorker::Cmd(cmd)).unwrap();
            assert!(
                matches!(
                    serde_json::from_str::<ToWorker>(&line).unwrap(),
                    ToWorker::Cmd(_)
                ),
                "expected Cmd for {line}"
            );
        }
    }

    #[test]
    fn set_recording_roundtrips() {
        let line =
            serde_json::to_string(&ToWorker::Cmd(Command::SetRecording { on: true })).unwrap();
        assert_eq!(line, r#"{"cmd":"set_recording","on":true}"#);
        match serde_json::from_str::<ToWorker>(&line).unwrap() {
            ToWorker::Cmd(Command::SetRecording { on }) => assert!(on),
            _ => panic!("expected set_recording"),
        }
    }

    #[test]
    fn saved_audio_reply_roundtrips() {
        let file = WireSavedAudio {
            path: "rec-recv.wav".into(),
            kind: "recv".into(),
            samples: 8000,
            srate: 8000,
            truncated: false,
        };
        let rep = serde_json::to_string(&FromWorker::Reply(Reply {
            reply: 7,
            result: ReplyResult::SavedAudio(vec![file]),
        }))
        .unwrap();
        match serde_json::from_str::<FromWorker>(&rep).unwrap() {
            FromWorker::Reply(Reply {
                reply,
                result: ReplyResult::SavedAudio(files),
            }) => {
                assert_eq!(reply, 7);
                assert_eq!(files.len(), 1);
                assert_eq!(files[0].path, "rec-recv.wav");
                assert_eq!(files[0].kind, "recv");
                assert_eq!(files[0].samples, 8000);
                assert_eq!(files[0].srate, 8000);
                assert!(!files[0].truncated);
            }
            _ => panic!("expected saved_audio reply"),
        }
    }
}