mxc-sdk 1.0.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
//! TCP bridge to the guest agent.
//!
//! Establishes four outbound TCP connections to the guest agent and provides
//! the bridge for relaying control/stdin/stdout/stderr.

use std::net::SocketAddr;
use std::time::Duration;

use anyhow::{Context, Result};
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
use tokio::net::TcpStream;

use crate::windows_sandbox_common::auth::{self, ChannelRole, Nonce};
use crate::windows_sandbox_common::sandbox_protocol::{
    decode_message, encode_message, validate_preamble, ControlMessage, DecodeResult, ExecRequest,
    PREAMBLE_LEN,
};

use crate::windows_sandbox_lifecycle::ipc_exec::{self, ExecStart, FrameKind};

const STREAMS_READY_TIMEOUT_SECS: Duration = Duration::from_secs(60);

const RECONNECT_TIMEOUT_SECS: Duration = Duration::from_secs(30);

const IPC_WRITE_TIMEOUT_SECS: Duration = Duration::from_secs(30);

const POST_EXIT_DRAIN_SECS: Duration = Duration::from_secs(15);

const PREAMBLE_HANDSHAKE_TIMEOUT_SECS: Duration = Duration::from_secs(10);

/// Per-stream cap on captured guest output in the one-shot protocol. The guest
/// is untrusted, so unbounded buffering lets a runaway script OOM the host.
const MAX_CAPTURED_OUTPUT_BYTES: usize = 64 * 1024 * 1024;

/// Grace added to the guest's own `timeout_ms` to form the host watchdog
/// deadline: covers guest process teardown and final output drain. If the guest
/// freezes and never reports, the host stops waiting after `timeout_ms + grace`.
pub const HOST_WATCHDOG_GRACE: Duration = Duration::from_secs(30);

/// Host watchdog deadline for one guest execution. `None` for an infinite guest
/// budget (`u32::MAX`, the normalized "no timeout"); otherwise `timeout_ms +
/// grace`, saturating against overflow.
pub fn host_watchdog_deadline(timeout_ms: u32, grace: Duration) -> Option<Duration> {
    if timeout_ms == u32::MAX {
        None
    } else {
        Some(Duration::from_millis(u64::from(timeout_ms)).saturating_add(grace))
    }
}

/// Four TCP connections to the guest agent.
pub struct GuestConnection {
    pub control: TcpStream,
    pub stdin_stream: TcpStream,
    pub stdout_stream: TcpStream,
    pub stderr_stream: TcpStream,
}

/// Result of executing a script on the guest agent.
pub struct ExecResult {
    /// Process exit code (negative values indicate error/timeout).
    pub exit_code: i32,
    /// Optional error message from the agent.
    pub error_message: String,
    /// Captured stdout bytes from the child process.
    pub stdout: Vec<u8>,
    /// Captured stderr bytes from the child process.
    pub stderr: Vec<u8>,
    /// Control bytes read beyond the exit frame.
    pub control_residual: Vec<u8>,
}

/// Connect to the guest agent and wait for `Ready`.
///
/// Each connection begins with the launch nonce and a [`ChannelRole`] byte.
pub async fn connect_to_guest(
    addr: SocketAddr,
    timeout: std::time::Duration,
    nonce: &Nonce,
) -> Result<GuestConnection> {
    let connect = |role: ChannelRole| async move {
        let label = role.label();
        let mut stream = tokio::time::timeout(timeout, TcpStream::connect(addr))
            .await
            .with_context(|| format!("timeout connecting {} to {}", label, addr))?
            .with_context(|| format!("connect {} to {}", label, addr))?;
        tokio::time::timeout(timeout, auth::write_nonce(&mut stream, nonce, role))
            .await
            .with_context(|| format!("timeout writing nonce on {} to {}", label, addr))?
            .with_context(|| format!("write nonce on {} to {}", label, addr))?;
        Ok::<TcpStream, anyhow::Error>(stream)
    };

    let control = connect(ChannelRole::Control).await?;
    let stdin_stream = connect(ChannelRole::Stdin).await?;
    let stdout_stream = connect(ChannelRole::Stdout).await?;
    let stderr_stream = connect(ChannelRole::Stderr).await?;

    eprintln!("[daemon] 4 TCP connections established to {}", addr);

    let mut conn = GuestConnection {
        control,
        stdin_stream,
        stdout_stream,
        stderr_stream,
    };
    let handshake_timeout = timeout.min(PREAMBLE_HANDSHAKE_TIMEOUT_SECS);
    read_and_validate_preamble(&mut conn.control, handshake_timeout).await?;
    wait_for_ready(&mut conn.control, timeout).await?;

    Ok(conn)
}

/// Read the fixed 8-byte control preamble and validate magic + version.
async fn read_and_validate_preamble(
    control: &mut TcpStream,
    timeout: std::time::Duration,
) -> Result<()> {
    let mut preamble = [0u8; PREAMBLE_LEN];
    tokio::time::timeout(timeout, control.read_exact(&mut preamble))
        .await
        .context("timeout reading guest preamble")?
        .context("read guest preamble")?;
    let version = validate_preamble(&preamble).map_err(|e| {
        anyhow::anyhow!(
            "guest protocol handshake failed: {} (refusing connection)",
            e
        )
    })?;
    eprintln!("[daemon] guest protocol handshake ok (version {})", version);
    Ok(())
}

/// Read from the control channel until a `Ready` message arrives.
async fn wait_for_ready(control: &mut TcpStream, timeout: std::time::Duration) -> Result<()> {
    let mut buf = Vec::with_capacity(256);
    let mut tmp = [0u8; 256];
    let deadline = tokio::time::Instant::now() + timeout;

    loop {
        let remaining = deadline
            .checked_duration_since(tokio::time::Instant::now())
            .unwrap_or(Duration::ZERO);
        let n = tokio::time::timeout(remaining, control.read(&mut tmp))
            .await
            .context("timeout waiting for Ready")?
            .context("read control for Ready")?;
        if n == 0 {
            anyhow::bail!("control connection closed before Ready");
        }
        buf.extend_from_slice(&tmp[..n]);

        match decode_message(&buf).context("decode Ready")? {
            DecodeResult::Message {
                message: ControlMessage::Ready,
                consumed,
            } => {
                buf.drain(..consumed);
                eprintln!("[daemon] received Ready from guest agent");
                return Ok(());
            }
            DecodeResult::Message { message, .. } => {
                anyhow::bail!("expected Ready, got {:?}", message);
            }
            DecodeResult::Incomplete => {
                continue;
            }
        }
    }
}

/// Send an EXEC request to the guest and relay stdin/stdout/stderr.
pub async fn execute_on_guest(
    conn: &mut GuestConnection,
    exec_id: &str,
    script_code: &str,
    working_directory: &str,
    timeout_ms: u32,
    host_stdin: &[u8],
) -> Result<ExecResult> {
    execute_on_guest_with_grace(
        conn,
        exec_id,
        script_code,
        working_directory,
        timeout_ms,
        host_stdin,
        HOST_WATCHDOG_GRACE,
    )
    .await
}

/// [`execute_on_guest`] with an explicit host-watchdog grace period, for tests.
async fn execute_on_guest_with_grace(
    conn: &mut GuestConnection,
    exec_id: &str,
    script_code: &str,
    working_directory: &str,
    timeout_ms: u32,
    host_stdin: &[u8],
    grace: Duration,
) -> Result<ExecResult> {
    let exec_msg = ControlMessage::Exec(ExecRequest {
        exec_id: exec_id.to_string(),
        script_code: script_code.to_string(),
        working_directory: working_directory.to_string(),
        timeout_ms,
    });
    let frame = encode_message(&exec_msg).context("encode EXEC")?;
    conn.control.write_all(&frame).await.context("send EXEC")?;

    // Pump stdin concurrently with output to avoid filling opposing TCP windows.
    // BrokenPipe is non-fatal because commands may exit without consuming stdin.
    let stdin_task = {
        let stream = &mut conn.stdin_stream;
        async move {
            if !host_stdin.is_empty() {
                if let Err(e) = stream.write_all(host_stdin).await {
                    return Some(anyhow::Error::new(e).context("write stdin to guest"));
                }
            }
            // Shut down the write half to signal EOF.
            stream
                .shutdown()
                .await
                .err()
                .map(|e| anyhow::Error::new(e).context("shutdown stdin"))
        }
    };

    let stdout_task = drain_capped_stream(&mut conn.stdout_stream, "stdout");
    let stderr_task = drain_capped_stream(&mut conn.stderr_stream, "stderr");

    // Wait for the EXIT notification on the control channel.
    // Returns (exit_code, error_message, residual_bytes) where residual
    // is anything read past the EXIT frame.
    let exit_task = async {
        let mut buf = Vec::with_capacity(256);
        let mut tmp = [0u8; 4096];
        loop {
            let bytes_read = conn.control.read(&mut tmp).await.context("read control")?;
            if bytes_read == 0 {
                anyhow::bail!("control closed before EXIT");
            }
            buf.extend_from_slice(&tmp[..bytes_read]);

            // Drain all complete messages before reading more data.
            loop {
                match decode_message(&buf).context("decode control")? {
                    DecodeResult::Message {
                        message: ControlMessage::Exit(exit),
                        consumed,
                    } => {
                        buf.drain(..consumed);
                        return Ok((exit.exit_code, exit.error_message, buf));
                    }
                    DecodeResult::Message {
                        message: ControlMessage::Pong,
                        consumed,
                    } => {
                        buf.drain(..consumed);
                        // Continue inner loop to decode the next message.
                    }
                    DecodeResult::Message { message, .. } => {
                        anyhow::bail!("unexpected control message: {:?}", message);
                    }
                    DecodeResult::Incomplete => break,
                }
            }
        }
    };

    // Concurrently pump stdin/stdout/stderr and the exit notification, bounded
    // by a host watchdog so a frozen guest (or one that never closes its data
    // streams) cannot hang the host indefinitely.
    let joined = async { tokio::join!(stdin_task, stdout_task, stderr_task, exit_task) };
    let (
        stdin_err,
        (stdout, stdout_truncated, stdout_err),
        (stderr, stderr_truncated, stderr_err),
        exit_result,
    ) = match host_watchdog_deadline(timeout_ms, grace) {
        Some(deadline) => match tokio::time::timeout(deadline, joined).await {
            Ok(joined) => joined,
            Err(_) => {
                eprintln!(
                    "[daemon] WARNING: guest did not report completion within the host watchdog \
                     ({timeout_ms}ms guest timeout + {}s grace); the sandbox may be frozen",
                    grace.as_secs()
                );
                return Ok(ExecResult {
                    exit_code: -1,
                    error_message: format!(
                        "guest did not report completion within the host watchdog ({timeout_ms}ms \
                         guest timeout + {}s grace); the sandbox may be frozen",
                        grace.as_secs()
                    ),
                    stdout: Vec::new(),
                    stderr: Vec::new(),
                    control_residual: Vec::new(),
                });
            }
        },
        None => joined.await,
    };

    if let Some(e) = stdin_err {
        eprintln!("[daemon] WARNING: forwarding stdin to guest failed (continuing): {e:#}");
    }

    let (exit_code, mut error_message, control_residual) = exit_result?;

    // A read error on stdout/stderr means the captured output may be truncated.
    // Do NOT present a truncated run as a clean success: if the guest reported
    // exit 0 but a data channel errored mid-stream, surface a transport error
    // (preserving the bytes already captured) instead of silently returning
    // empty/partial output with exit 0. A non-zero exit already signals
    // failure, so the bytes are returned as-is in that case.
    if exit_code == 0 {
        if let Some(e) = stdout_err.or(stderr_err) {
            return Ok(ExecResult {
                exit_code: -1,
                error_message: format!(
                    "transport error draining guest output (captured {} stdout / {} stderr \
                     byte(s) before the error): {e}",
                    stdout.len(),
                    stderr.len()
                ),
                stdout,
                stderr,
                control_residual,
            });
        }
    }

    // Never present a capped run as a clean, complete capture.
    if stdout_truncated || stderr_truncated {
        let which = match (stdout_truncated, stderr_truncated) {
            (true, true) => "stdout and stderr",
            (true, false) => "stdout",
            (false, true) => "stderr",
            (false, false) => unreachable!(),
        };
        eprintln!(
            "[daemon] WARNING: guest {which} exceeded the {MAX_CAPTURED_OUTPUT_BYTES}-byte capture \
             cap and was truncated; use the state-aware backend (which streams) for large output."
        );
        let note = format!(
            "guest {which} exceeded the {MAX_CAPTURED_OUTPUT_BYTES}-byte capture cap and was \
             truncated"
        );
        error_message = if error_message.is_empty() {
            note
        } else {
            format!("{error_message}; {note}")
        };
    }

    Ok(ExecResult {
        exit_code,
        error_message,
        stdout,
        stderr,
        control_residual,
    })
}

/// Drain one guest data stream into a buffer capped at
/// [`MAX_CAPTURED_OUTPUT_BYTES`], returning the bytes, a truncation flag, and
/// any transport error. Reads continue to EOF past the cap (excess discarded).
async fn drain_capped_stream<R>(
    stream: &mut R,
    label: &str,
) -> (Vec<u8>, bool, Option<anyhow::Error>)
where
    R: tokio::io::AsyncRead + Unpin,
{
    drain_capped_stream_with_cap(stream, label, MAX_CAPTURED_OUTPUT_BYTES).await
}

/// [`drain_capped_stream`] with an explicit cap for testing.
async fn drain_capped_stream_with_cap<R>(
    stream: &mut R,
    label: &str,
    cap: usize,
) -> (Vec<u8>, bool, Option<anyhow::Error>)
where
    R: tokio::io::AsyncRead + Unpin,
{
    let mut buf = Vec::new();
    let mut tmp = [0u8; 8192];
    let mut seen: u64 = 0;
    let mut truncated = false;
    loop {
        let r = stream.read(&mut tmp).await;
        match classify_data_read(r, seen, label) {
            Ok(Some(n)) => {
                seen += n as u64;
                if buf.len() < cap {
                    let take = (cap - buf.len()).min(n);
                    buf.extend_from_slice(&tmp[..take]);
                    if take < n {
                        truncated = true;
                    }
                } else {
                    truncated = true;
                }
            }
            Ok(None) => break,
            Err(e) => return (buf, truncated, Some(e)),
        }
    }
    (buf, truncated, None)
}

/// Wait for `StreamsReady`, then reconnect stdin/stdout/stderr.
pub async fn reconnect_data_streams(
    conn: &mut GuestConnection,
    addr: SocketAddr,
    control_residual: Vec<u8>,
    nonce: &Nonce,
) -> Result<()> {
    let mut buf = control_residual;
    let mut tmp = [0u8; 256];
    let timeout = STREAMS_READY_TIMEOUT_SECS;
    let deadline = tokio::time::Instant::now() + timeout;

    loop {
        loop {
            match decode_message(&buf).context("decode StreamsReady")? {
                DecodeResult::Message {
                    message: ControlMessage::StreamsReady,
                    consumed,
                } => {
                    buf.drain(..consumed);
                    eprintln!("[daemon] received StreamsReady, reconnecting data streams");

                    let connect_timeout = RECONNECT_TIMEOUT_SECS;
                    let connect = |role: ChannelRole| {
                        let target = addr;
                        async move {
                            let label = role.label();
                            let mut stream =
                                tokio::time::timeout(connect_timeout, TcpStream::connect(target))
                                    .await
                                    .with_context(|| {
                                        format!("timeout reconnecting {} to {}", label, target)
                                    })?
                                    .with_context(|| {
                                        format!("reconnect {} to {}", label, target)
                                    })?;
                            tokio::time::timeout(
                                connect_timeout,
                                auth::write_nonce(&mut stream, nonce, role),
                            )
                            .await
                            .with_context(|| format!("timeout writing reconnect nonce on {label}"))?
                            .with_context(|| {
                                format!("write reconnect nonce on {label} to {target}")
                            })?;
                            Ok::<TcpStream, anyhow::Error>(stream)
                        }
                    };

                    let (stdin_stream, stdout_stream, stderr_stream) = tokio::try_join!(
                        connect(ChannelRole::Stdin),
                        connect(ChannelRole::Stdout),
                        connect(ChannelRole::Stderr),
                    )?;
                    conn.stdin_stream = stdin_stream;
                    conn.stdout_stream = stdout_stream;
                    conn.stderr_stream = stderr_stream;

                    eprintln!("[daemon] data streams reconnected to {}", addr);
                    return Ok(());
                }
                DecodeResult::Message {
                    message: ControlMessage::Pong,
                    consumed,
                } => {
                    buf.drain(..consumed);
                    continue;
                }
                DecodeResult::Message { message, consumed } => {
                    eprintln!(
                        "[daemon] skipping unexpected message while waiting for StreamsReady: {:?}",
                        message
                    );
                    buf.drain(..consumed);
                    continue;
                }
                DecodeResult::Incomplete => break,
            }
        }

        // Need more data from the control channel.
        let remaining = deadline
            .checked_duration_since(tokio::time::Instant::now())
            .unwrap_or(Duration::ZERO);
        let bytes_read = tokio::time::timeout(remaining, conn.control.read(&mut tmp))
            .await
            .context("timeout waiting for StreamsReady")?
            .context("read control for StreamsReady")?;
        if bytes_read == 0 {
            anyhow::bail!("control closed before StreamsReady");
        }
        buf.extend_from_slice(&tmp[..bytes_read]);
    }
}

/// Outcome of a streamed execution on the guest.
pub struct StreamExecOutcome {
    /// Control-channel bytes read past the `Exit` frame (may contain the
    /// guest's `StreamsReady`). Hand this to [`reconnect_data_streams`].
    pub control_residual: Vec<u8>,
    /// `false` if the IPC writer (the exec-phase client) errored mid-stream and
    /// was abandoned. The guest execution still ran to completion and the guest
    /// connection is left in a clean, reusable state; only the client relay was
    /// lost.
    pub ipc_alive: bool,
    /// The child's exit code, as reported by the guest `Exit` message. The
    /// caller writes the terminal exit frame after restoring/releasing the
    /// single-flight guest slot.
    pub exit_code: i32,
    /// The guest's error message accompanying the exit (empty on success).
    pub error_message: String,
    /// `true` if the host watchdog fired before the guest reported completion.
    /// When set, `exit_code`/`error_message` are unspecified and the caller
    /// synthesises the terminal frame; the guest slot must be marked unusable.
    pub timed_out: bool,
}

/// Run one guest execution and stream stdout/stderr to the IPC client.
///
/// The terminal exit frame is written separately after the guest slot is
/// restored for reuse.
///
/// `deadline`, when `Some`, bounds the whole execution: it is a host watchdog
/// for a frozen-but-alive guest. It is honoured **only at frame boundaries**
/// (between `select!` iterations), never mid-frame, so a partially-written data
/// frame can never be followed by the terminal frame. On expiry the loop breaks
/// and the returned outcome has `timed_out == true`.
pub async fn stream_exec_on_guest<R, W>(
    conn: &mut GuestConnection,
    exec_id: &str,
    req: &ExecStart,
    ipc_reader: &mut R,
    ipc: &mut W,
    deadline: Option<tokio::time::Instant>,
) -> Result<StreamExecOutcome>
where
    R: tokio::io::AsyncRead + Unpin,
    W: AsyncWrite + Unpin,
{
    let exec_msg = ControlMessage::Exec(ExecRequest {
        exec_id: exec_id.to_string(),
        script_code: req.script_code.clone(),
        working_directory: req.working_directory.clone(),
        timeout_ms: req.timeout_ms,
    });
    let frame = encode_message(&exec_msg).context("encode EXEC")?;
    conn.control.write_all(&frame).await.context("send EXEC")?;

    let control = &mut conn.control;
    let stdout = &mut conn.stdout_stream;
    let stderr = &mut conn.stderr_stream;
    let stdin_out = &mut conn.stdin_stream;

    let mut ipc_alive = true;
    let mut stdout_done = false;
    let mut stderr_done = false;
    let mut stdin_in_done = false;
    let mut stdin_out_closed = false;
    let mut stdout_seen: u64 = 0;
    let mut stderr_seen: u64 = 0;
    let mut exit: Option<(i32, String)> = None;
    let mut timed_out = false;
    let mut post_exit_deadline: Option<tokio::time::Instant> = None;
    let mut ctrl_buf: Vec<u8> = Vec::with_capacity(256);
    let mut so = [0u8; 8192];
    let mut se = [0u8; 8192];
    let mut cb = [0u8; 4096];

    while !(stdout_done && stderr_done && exit.is_some()) {
        tokio::select! {
            r = stdout.read(&mut so), if !stdout_done => {
                match classify_data_read(r, stdout_seen, "stdout")? {
                    None => {
                        stdout_done = true;
                    }
                    Some(n) => {
                        stdout_seen += n as u64;
                        if ipc_alive
                            && !write_data_frame_split(ipc, FrameKind::Stdout, &so[..n]).await
                        {
                            ipc_alive = false;
                        }
                    }
                }
            }
            r = stderr.read(&mut se), if !stderr_done => {
                match classify_data_read(r, stderr_seen, "stderr")? {
                    None => {
                        stderr_done = true;
                    }
                    Some(n) => {
                        stderr_seen += n as u64;
                        if ipc_alive
                            && !write_data_frame_split(ipc, FrameKind::Stderr, &se[..n]).await
                        {
                            ipc_alive = false;
                        }
                    }
                }
            }
            r = ipc_exec::read_frame_async(ipc_reader), if !stdin_in_done => {
                match r {
                    Ok(None) => {
                        stdin_in_done = true;
                        if !stdin_out_closed {
                            let _ = stdin_out.shutdown().await;
                            stdin_out_closed = true;
                        }
                    }
                    Ok(Some(frame)) if frame.kind == Some(FrameKind::Stdin) => {
                        if !frame.payload.is_empty() && !stdin_out_closed {
                            if let Err(e) = stdin_out.write_all(&frame.payload).await {
                                eprintln!(
                                    "[daemon] guest stdin write failed: {e}; closing stdin and \
                                     continuing exec"
                                );
                                let _ = stdin_out.shutdown().await;
                                stdin_out_closed = true;
                            }
                        }
                    }
                    Ok(Some(frame)) => {
                        eprintln!(
                            "[daemon] ignoring unexpected IPC frame kind {} during exec",
                            frame.raw_kind
                        );
                    }
                    Err(e) => {
                        eprintln!("[daemon] IPC stdin reader errored: {e}; closing guest stdin");
                        stdin_in_done = true;
                        if !stdin_out_closed {
                            let _ = stdin_out.shutdown().await;
                            stdin_out_closed = true;
                        }
                    }
                }
            }
            r = control.read(&mut cb), if exit.is_none() => {
                let n = r.context("read guest control")?;
                if n == 0 {
                    anyhow::bail!("guest control closed before Exit");
                }
                ctrl_buf.extend_from_slice(&cb[..n]);
                loop {
                    match decode_message(&ctrl_buf).context("decode control")? {
                        DecodeResult::Message {
                            message: ControlMessage::Exit(e),
                            consumed,
                        } => {
                            ctrl_buf.drain(..consumed);
                            exit = Some((e.exit_code, e.error_message));
                            post_exit_deadline =
                                Some(tokio::time::Instant::now() + POST_EXIT_DRAIN_SECS);
                            if !stdin_out_closed {
                                let _ = stdin_out.shutdown().await;
                                stdin_out_closed = true;
                            }
                            stdin_in_done = true;
                            break;
                        }
                        DecodeResult::Message {
                            message: ControlMessage::Pong,
                            consumed,
                        } => {
                            ctrl_buf.drain(..consumed);
                        }
                        DecodeResult::Message { message, consumed } => {
                            eprintln!("[daemon] unexpected control during exec: {message:?}");
                            ctrl_buf.drain(..consumed);
                        }
                        DecodeResult::Incomplete => break,
                    }
                }
            }
            _ = async {
                match post_exit_deadline {
                    Some(d) => tokio::time::sleep_until(d).await,
                    None => std::future::pending::<()>().await,
                }
            }, if exit.is_some() && !(stdout_done && stderr_done) => {
                eprintln!(
                    "[daemon] post-exit drain timed out after {:?}; abandoning stdout/stderr (possible leaked guest descendant holding the pipes)",
                    POST_EXIT_DRAIN_SECS
                );
                stdout_done = true;
                stderr_done = true;
            }
            // Host watchdog. Observed only here, between whole-frame relays, so
            // it never interrupts a `write_data_frame_split` mid-frame. `None`
            // (infinite budget) parks on `pending` and never fires.
            _ = async {
                match deadline {
                    Some(d) => tokio::time::sleep_until(d).await,
                    None => std::future::pending::<()>().await,
                }
            } => {
                eprintln!(
                    "[daemon] exec {exec_id}: host watchdog fired; abandoning frozen guest"
                );
                timed_out = true;
                break;
            }
        }
    }

    if timed_out {
        // The guest is untrustworthy; the caller marks the slot unusable and
        // synthesises the terminal frame. Break happened at a frame boundary,
        // so the IPC stream carries no partial data frame.
        return Ok(StreamExecOutcome {
            control_residual: ctrl_buf,
            ipc_alive,
            exit_code: -1,
            error_message: String::new(),
            timed_out: true,
        });
    }

    let (exit_code, error_message) = match exit {
        Some(e) => e,
        None => anyhow::bail!("internal: exec loop ended without an exit notification"),
    };

    Ok(StreamExecOutcome {
        control_residual: ctrl_buf,
        ipc_alive,
        exit_code,
        error_message,
        timed_out: false,
    })
}

/// Write the terminal exit frame to the IPC client.
///
/// Write the final exit frame after the guest slot is reusable.
pub async fn write_exit_frame<W>(ipc: &mut W, exit_code: i32, error_message: &str) -> Result<bool>
where
    W: AsyncWrite + Unpin,
{
    let frame =
        ipc_exec::encode_exit_frame(exit_code, error_message).context("encode exit frame")?;
    let ok = write_ipc(ipc, &frame).await
        && matches!(
            tokio::time::timeout(IPC_WRITE_TIMEOUT_SECS, ipc.flush()).await,
            Ok(Ok(()))
        );
    Ok(ok)
}

/// Classify a data-stream (`stdout`/`stderr`) read result.
fn classify_data_read(r: std::io::Result<usize>, seen: u64, label: &str) -> Result<Option<usize>> {
    use std::io::ErrorKind;
    match r {
        Ok(0) => Ok(None),
        Ok(n) => Ok(Some(n)),
        Err(e)
            if seen == 0
                && matches!(
                    e.kind(),
                    ErrorKind::ConnectionReset
                        | ErrorKind::ConnectionAborted
                        | ErrorKind::BrokenPipe
                ) =>
        {
            eprintln!(
                "[daemon] guest {label} reset with no data ({:?}); treating as EOF",
                e.kind()
            );
            Ok(None)
        }
        Err(e) => {
            Err(anyhow::Error::new(e).context(format!("read guest {label} after {seen} bytes")))
        }
    }
}

/// Write a frame to the IPC client with a bounded timeout.
async fn write_ipc<W>(ipc: &mut W, frame: &[u8]) -> bool
where
    W: AsyncWrite + Unpin,
{
    matches!(
        tokio::time::timeout(IPC_WRITE_TIMEOUT_SECS, ipc.write_all(frame),).await,
        Ok(Ok(()))
    )
}

/// Write a data frame without allocating a combined header+payload buffer.
async fn write_data_frame_split<W>(ipc: &mut W, kind: FrameKind, payload: &[u8]) -> bool
where
    W: AsyncWrite + Unpin,
{
    let header = ipc_exec::frame_header(kind, payload.len());
    let combined = async {
        ipc.write_all(&header).await?;
        ipc.write_all(payload).await?;
        Ok::<(), std::io::Error>(())
    };
    matches!(
        tokio::time::timeout(IPC_WRITE_TIMEOUT_SECS, combined).await,
        Ok(Ok(()))
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::windows_sandbox_common::auth::generate_nonce;
    use crate::windows_sandbox_common::sandbox_protocol::{encode_message, encode_preamble};
    use std::io::{Error, ErrorKind};
    use std::net::SocketAddr;
    use tokio::net::TcpListener;
    use tokio::sync::oneshot;

    #[test]
    fn classify_data_read_clean_eof_is_none() {
        assert_eq!(classify_data_read(Ok(0), 0, "stdout").unwrap(), None);
        // A clean FIN is EOF even after bytes were observed.
        assert_eq!(classify_data_read(Ok(0), 42, "stdout").unwrap(), None);
    }

    #[test]
    fn classify_data_read_some_bytes_passes_through() {
        assert_eq!(classify_data_read(Ok(7), 0, "stderr").unwrap(), Some(7));
        assert_eq!(classify_data_read(Ok(3), 100, "stderr").unwrap(), Some(3));
    }

    #[test]
    fn classify_data_read_reset_with_no_data_is_eof() {
        // The narrow defensive case: a reset-class error before any byte was
        // seen is treated as EOF (RST racing ahead of FIN on the zero-output
        // path).
        for kind in [
            ErrorKind::ConnectionReset,
            ErrorKind::ConnectionAborted,
            ErrorKind::BrokenPipe,
        ] {
            let r = Err(Error::new(kind, "reset"));
            assert_eq!(
                classify_data_read(r, 0, "stdout").unwrap(),
                None,
                "reset kind {kind:?} with no bytes should be EOF"
            );
        }
    }

    #[test]
    fn classify_data_read_reset_after_bytes_is_error() {
        // Once any byte was relayed, a reset must be a hard error so we never
        // silently truncate output.
        let r = Err(Error::new(ErrorKind::ConnectionReset, "reset"));
        assert!(classify_data_read(r, 1, "stdout").is_err());
    }

    #[test]
    fn classify_data_read_other_error_is_error_regardless_of_seen() {
        let r = Err(Error::new(ErrorKind::NotConnected, "nope"));
        assert!(classify_data_read(r, 0, "stderr").is_err());
        let r = Err(Error::new(ErrorKind::NotConnected, "nope"));
        assert!(classify_data_read(r, 5, "stderr").is_err());
    }

    #[tokio::test]
    async fn drain_capped_stream_captures_full_output_under_cap() {
        let payload = vec![b'x'; 1000];
        let mut src: &[u8] = &payload;
        let (buf, truncated, err) =
            drain_capped_stream_with_cap(&mut src, "stdout", 64 * 1024).await;
        assert!(err.is_none());
        assert!(!truncated);
        assert_eq!(buf, payload);
    }

    #[tokio::test]
    async fn drain_capped_stream_truncates_output_over_cap() {
        let cap = 4096usize;
        let payload = vec![b'y'; cap * 8];
        let mut src: &[u8] = &payload;
        let (buf, truncated, err) = drain_capped_stream_with_cap(&mut src, "stdout", cap).await;
        assert!(err.is_none());
        assert!(truncated);
        assert_eq!(buf.len(), cap);
        assert!(buf.iter().all(|&b| b == b'y'));
    }

    #[tokio::test]
    async fn drain_capped_stream_truncates_exactly_at_boundary() {
        let cap = 8192usize;
        let exact = vec![b'z'; cap];
        let mut exact_src: &[u8] = &exact;
        let (buf, truncated, _) = drain_capped_stream_with_cap(&mut exact_src, "stderr", cap).await;
        assert_eq!(buf.len(), cap);
        assert!(!truncated);

        let over = vec![b'z'; cap + 1];
        let mut over_src: &[u8] = &over;
        let (buf, truncated, _) = drain_capped_stream_with_cap(&mut over_src, "stderr", cap).await;
        assert_eq!(buf.len(), cap);
        assert!(truncated);
    }

    #[tokio::test]
    async fn write_exit_frame_emits_single_decodable_exit_frame() {
        // The terminal exit frame is written by the caller (after releasing
        // the single-flight slot). Verify the extracted helper
        // emits exactly one decodable exit frame and reports the client alive.
        let mut buf: Vec<u8> = Vec::new();
        let alive = write_exit_frame(&mut buf, 7, "boom").await.unwrap();
        assert!(alive, "an in-memory writer never errors");

        let mut cur = std::io::Cursor::new(buf);
        let frame = ipc_exec::read_frame(&mut cur)
            .unwrap()
            .expect("one exit frame");
        assert_eq!(frame.kind, Some(FrameKind::Exit));
        let exit: ipc_exec::ExecExit = serde_json::from_slice(&frame.payload).unwrap();
        assert_eq!(exit.exit_code, 7);
        assert_eq!(exit.error_message, "boom");
        assert!(
            ipc_exec::read_frame(&mut cur).unwrap().is_none(),
            "no trailing frames after the terminal exit frame"
        );
    }

    #[test]
    fn host_watchdog_deadline_infinite_is_none() {
        // u32::MAX = "no timeout": no watchdog.
        assert_eq!(
            host_watchdog_deadline(u32::MAX, Duration::from_secs(30)),
            None
        );
    }

    #[test]
    fn host_watchdog_deadline_finite_adds_grace() {
        assert_eq!(
            host_watchdog_deadline(5_000, Duration::from_secs(30)),
            Some(Duration::from_millis(5_000) + Duration::from_secs(30))
        );
    }

    #[tokio::test]
    async fn host_watchdog_fires_when_guest_never_reports_exit() {
        // The fake guest handshakes but never sends EXIT nor closes its data
        // streams; without the watchdog this would block forever. `_fake` keeps
        // the sockets open for the call.
        let nonce = generate_nonce().expect("generate nonce");
        let (addr, fake_rx) = spawn_fake_guest(nonce.clone()).await;
        let mut conn = connect_to_guest(addr, Duration::from_secs(5), &nonce)
            .await
            .expect("connect_to_guest must succeed");
        let _fake = fake_rx
            .await
            .expect("fake-guest oneshot")
            .expect("fake-guest accept");

        let result = execute_on_guest_with_grace(
            &mut conn,
            "exec-frozen",
            "sleep forever",
            "",
            200,
            b"",
            Duration::from_millis(100),
        )
        .await
        .expect("execute_on_guest returns Ok with a watchdog result");

        assert_eq!(result.exit_code, -1);
        assert!(
            result.error_message.contains("host watchdog"),
            "unexpected watchdog message: {}",
            result.error_message
        );
    }

    #[tokio::test]
    async fn stream_exec_watchdog_fires_at_frame_boundary_without_partial_frame() {
        // Regression: the streaming watchdog is honoured *inside* the relay loop
        // at frame boundaries, so a timeout can never truncate a data frame and
        // leave the client's decoder desynced. The guest emits output then goes
        // silent (never Exit); the watchdog must fire, and everything written to
        // the IPC client must be complete, decodable frames with no residue.
        let nonce = generate_nonce().expect("generate nonce");
        let (addr, fake_rx) = spawn_fake_guest(nonce.clone()).await;
        let mut conn = connect_to_guest(addr, Duration::from_secs(5), &nonce)
            .await
            .expect("connect_to_guest must succeed");
        // Hold `fake` for the whole call so the control channel stays open: a
        // closed control would end the loop with an error, not a timeout.
        let mut fake = fake_rx
            .await
            .expect("fake-guest oneshot")
            .expect("fake-guest accept");

        // Pre-load guest stdout; the relay drains it before parking on control.
        fake.stdout.write_all(b"helloworld").await.unwrap();

        let req = ExecStart {
            script_code: "sleep".to_string(),
            working_directory: String::new(),
            timeout_ms: 60_000,
        };
        let mut ipc_reader: &[u8] = b"";
        let mut ipc_writer: Vec<u8> = Vec::new();
        let deadline = Some(tokio::time::Instant::now() + Duration::from_millis(150));

        // The 5s guard turns a watchdog-never-fires regression into a failure
        // instead of a hang; the real watchdog fires at 150ms.
        let outcome = tokio::time::timeout(
            Duration::from_secs(5),
            stream_exec_on_guest(
                &mut conn,
                "exec-wd",
                &req,
                &mut ipc_reader,
                &mut ipc_writer,
                deadline,
            ),
        )
        .await
        .expect("watchdog must return within the guard window")
        .expect("stream returns Ok");
        drop(fake);

        assert!(outcome.timed_out, "watchdog must report a timeout");

        let mut cur = std::io::Cursor::new(ipc_writer);
        let mut payload = Vec::new();
        while let Some(frame) = ipc_exec::read_frame(&mut cur).unwrap() {
            assert_eq!(
                frame.kind,
                Some(FrameKind::Stdout),
                "only stdout frames expected before the (caller-written) terminal frame"
            );
            payload.extend_from_slice(&frame.payload);
        }
        assert_eq!(
            cur.position() as usize,
            cur.get_ref().len(),
            "no trailing partial frame may remain in the IPC stream"
        );
        assert_eq!(payload, b"helloworld");
    }

    // In-process guest for bridge and reconnect integration tests.

    struct FakeGuestSide {
        control: tokio::net::TcpStream,
        stdin: tokio::net::TcpStream,
        stdout: tokio::net::TcpStream,
        stderr: tokio::net::TcpStream,
        #[allow(dead_code)]
        listener: TcpListener,
        #[allow(dead_code)]
        addr: SocketAddr,
    }

    /// Spawn an authenticated fake guest that pairs sockets by declared role.
    async fn spawn_fake_guest(
        nonce: crate::windows_sandbox_common::auth::Nonce,
    ) -> (
        SocketAddr,
        oneshot::Receiver<Result<FakeGuestSide, std::io::Error>>,
    ) {
        use crate::windows_sandbox_common::auth::ChannelRole;
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let (tx, rx) = oneshot::channel();
        tokio::spawn(async move {
            let result: Result<FakeGuestSide, std::io::Error> = async {
                let accept_one = || async {
                    let (mut s, _) = listener.accept().await?;
                    let mut buf = [0u8; crate::windows_sandbox_common::auth::NONCE_LEN_IN_BYTES];
                    s.read_exact(&mut buf).await?;
                    let got = crate::windows_sandbox_common::auth::Nonce::from_bytes(&buf)
                        .expect("read_exact filled the nonce buffer");
                    if !nonce.constant_time_eq(&got) {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            "bad nonce on fake-guest accept",
                        ));
                    }
                    let mut role_buf = [0u8; 1];
                    s.read_exact(&mut role_buf).await?;
                    let role = ChannelRole::from_wire(role_buf[0]).ok_or_else(|| {
                        std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            format!("unknown channel role 0x{:02x}", role_buf[0]),
                        )
                    })?;
                    Ok::<(tokio::net::TcpStream, ChannelRole), std::io::Error>((s, role))
                };
                let mut control: Option<tokio::net::TcpStream> = None;
                let mut stdin_stream: Option<tokio::net::TcpStream> = None;
                let mut stdout_stream: Option<tokio::net::TcpStream> = None;
                let mut stderr_stream: Option<tokio::net::TcpStream> = None;
                for _ in 0..4 {
                    let (s, role) = accept_one().await?;
                    let slot = match role {
                        ChannelRole::Control => &mut control,
                        ChannelRole::Stdin => &mut stdin_stream,
                        ChannelRole::Stdout => &mut stdout_stream,
                        ChannelRole::Stderr => &mut stderr_stream,
                    };
                    if slot.is_some() {
                        return Err(std::io::Error::new(
                            std::io::ErrorKind::InvalidData,
                            format!("duplicate role {:?} on fake-guest accept", role),
                        ));
                    }
                    *slot = Some(s);
                }
                let mut control = control.expect("control role declared");
                let stdin = stdin_stream.expect("stdin role declared");
                let stdout = stdout_stream.expect("stdout role declared");
                let stderr = stderr_stream.expect("stderr role declared");
                control.write_all(&encode_preamble()).await?;
                let ready = encode_message(&ControlMessage::Ready).expect("encode Ready");
                control.write_all(&ready).await?;
                Ok(FakeGuestSide {
                    control,
                    stdin,
                    stdout,
                    stderr,
                    listener,
                    addr,
                })
            }
            .await;
            let _ = tx.send(result);
        });
        (addr, rx)
    }

    #[tokio::test]
    async fn stream_exec_relays_stdout_stderr_exit_to_ipc_client() {
        let nonce = generate_nonce().expect("generate nonce");
        let (addr, fake_rx) = spawn_fake_guest(nonce.clone()).await;
        let mut conn = connect_to_guest(addr, Duration::from_secs(5), &nonce)
            .await
            .expect("connect_to_guest must succeed");
        let mut fake = fake_rx
            .await
            .expect("fake-guest oneshot")
            .expect("fake-guest accept");

        let mut ipc_reader: &[u8] = b"";
        let mut ipc_writer: Vec<u8> = Vec::new();
        let req = ExecStart {
            script_code: "echo hi".to_string(),
            working_directory: String::new(),
            timeout_ms: 5_000,
        };

        let fake_side = async {
            let mut buf = [0u8; 4096];
            let _ = fake.control.read(&mut buf).await;
            fake.stdout.write_all(b"hi").await.unwrap();
            fake.stdout.shutdown().await.ok();
            fake.stderr.write_all(b"warn").await.unwrap();
            fake.stderr.shutdown().await.ok();
            let exit = encode_message(&ControlMessage::Exit(
                crate::windows_sandbox_common::sandbox_protocol::ExitNotification {
                    exec_id: "exec-test".to_string(),
                    exit_code: 0,
                    error_message: String::new(),
                },
            ))
            .unwrap();
            fake.control.write_all(&exit).await.unwrap();
            fake
        };

        let host_side = stream_exec_on_guest(
            &mut conn,
            "exec-test",
            &req,
            &mut ipc_reader,
            &mut ipc_writer,
            None,
        );

        let (_fake_back, outcome) = tokio::join!(fake_side, host_side);
        let outcome = outcome.expect("stream_exec_on_guest");
        assert_eq!(outcome.exit_code, 0);
        assert_eq!(outcome.error_message, "");
        assert!(outcome.ipc_alive);

        let mut cur = std::io::Cursor::new(ipc_writer);
        let mut stdout_seen = Vec::new();
        let mut stderr_seen = Vec::new();
        while let Some(frame) = ipc_exec::read_frame(&mut cur).unwrap() {
            match frame.kind {
                Some(FrameKind::Stdout) => stdout_seen.extend_from_slice(&frame.payload),
                Some(FrameKind::Stderr) => stderr_seen.extend_from_slice(&frame.payload),
                other => panic!("unexpected frame kind during stream test: {other:?}"),
            }
        }
        assert_eq!(stdout_seen, b"hi");
        assert_eq!(stderr_seen, b"warn");
    }

    #[tokio::test]
    async fn stream_exec_forwards_frame_stdin_to_guest() {
        let nonce = generate_nonce().expect("generate nonce");
        let (addr, fake_rx) = spawn_fake_guest(nonce.clone()).await;
        let mut conn = connect_to_guest(addr, Duration::from_secs(5), &nonce)
            .await
            .unwrap();
        let mut fake = fake_rx.await.unwrap().unwrap();

        let (mut ipc_writer_side, mut ipc_reader_side) = tokio::io::duplex(4096);
        let stdin_frame = ipc_exec::encode_frame(FrameKind::Stdin, b"input data");
        ipc_writer_side.write_all(&stdin_frame).await.unwrap();
        drop(ipc_writer_side); // EOF on the IPC reader

        let mut ipc_out: Vec<u8> = Vec::new();
        let req = ExecStart {
            script_code: "cat".to_string(),
            working_directory: String::new(),
            timeout_ms: 5_000,
        };

        let fake_side = async {
            let mut buf = [0u8; 4096];
            let _ = fake.control.read(&mut buf).await;
            // Read whatever lands on fake stdin (forwarded by daemon).
            let mut stdin_recv = Vec::new();
            // Bound the read so a regression where stdin never closes does
            // not hang the test forever.
            let _ = tokio::time::timeout(
                Duration::from_secs(2),
                fake.stdin.read_to_end(&mut stdin_recv),
            )
            .await;
            // Close stdout/stderr cleanly so the host's exec drain exits.
            fake.stdout.shutdown().await.ok();
            fake.stderr.shutdown().await.ok();
            let exit = encode_message(&ControlMessage::Exit(
                crate::windows_sandbox_common::sandbox_protocol::ExitNotification {
                    exec_id: "exec-stdin".to_string(),
                    exit_code: 0,
                    error_message: String::new(),
                },
            ))
            .unwrap();
            fake.control.write_all(&exit).await.unwrap();
            stdin_recv
        };

        let host_side = stream_exec_on_guest(
            &mut conn,
            "exec-stdin",
            &req,
            &mut ipc_reader_side,
            &mut ipc_out,
            None,
        );

        let (stdin_recv, outcome) = tokio::join!(fake_side, host_side);
        outcome.expect("stream_exec_on_guest");
        assert_eq!(
            stdin_recv, b"input data",
            "guest stdin must receive forwarded bytes"
        );
    }

    #[tokio::test]
    async fn stream_exec_survives_ipc_client_disconnect_mid_stream() {
        // If the IPC client (wxc-exec) disconnects mid-stream the guest
        // execution must still run to completion and the connection must
        // stay in a clean reusable state. ipc_alive should be false but
        // exit_code populated normally. Review robustness contract on
        // stream_exec_on_guest.
        let nonce = generate_nonce().expect("generate nonce");
        let (addr, fake_rx) = spawn_fake_guest(nonce.clone()).await;
        let mut conn = connect_to_guest(addr, Duration::from_secs(5), &nonce)
            .await
            .unwrap();
        let mut fake = fake_rx.await.unwrap().unwrap();

        let (ipc_client_side, ipc_server_side) = tokio::io::duplex(64);
        // Daemon writes flow to `ipc_server_side`'s write half (which the
        // client would read). When we drop ipc_client_side, the daemon's
        // write returns EOF.
        let (server_read, mut server_write) = tokio::io::split(ipc_server_side);
        let mut empty_reader: &[u8] = b"";

        let req = ExecStart {
            script_code: "long-running".to_string(),
            working_directory: String::new(),
            timeout_ms: 5_000,
        };

        let fake_side = async {
            let mut buf = [0u8; 4096];
            let _ = fake.control.read(&mut buf).await;
            // Spew enough stdout that the daemon's write to the (now-
            // disconnected) IPC writer fails. 64 KiB exceeds the duplex
            // buffer many times over.
            for _ in 0..16 {
                fake.stdout.write_all(&[b'x'; 4096]).await.ok();
            }
            fake.stdout.shutdown().await.ok();
            fake.stderr.shutdown().await.ok();
            let exit = encode_message(&ControlMessage::Exit(
                crate::windows_sandbox_common::sandbox_protocol::ExitNotification {
                    exec_id: "exec-disco".to_string(),
                    exit_code: 0,
                    error_message: String::new(),
                },
            ))
            .unwrap();
            fake.control.write_all(&exit).await.unwrap();
        };

        drop(ipc_client_side);
        drop(server_read);

        let host_side = stream_exec_on_guest(
            &mut conn,
            "exec-disco",
            &req,
            &mut empty_reader,
            &mut server_write,
            None,
        );

        let (_fake, outcome) = tokio::join!(fake_side, host_side);
        let outcome = outcome.expect("stream_exec_on_guest must still complete");
        assert!(
            !outcome.ipc_alive,
            "ipc_alive must be false after client disconnect"
        );
        assert_eq!(outcome.exit_code, 0);
    }
}