mxc-sdk 1.2.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Piped execution: the frame consumer never waits for a caller's output pipe.

use std::io::{self, Read, Write};
use std::net::{Shutdown, TcpStream};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{mpsc, Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use crate::mxc_common::mxc_error::MxcError;
use crate::mxc_common::sandbox_process::StreamCloser;
use crate::mxc_common::state_aware_backend::{ExecHandle, ExecOutcome, PipeHandle};

use super::super::service::with_transport_stack;
use super::frame::{encode_frame, FrameAccumulator, FrameType, MAX_FRAME_PAYLOAD};
use super::payload::{parse_error, parse_exit, parse_started, ExecStartPayload};
use super::stream_buffer::{
    bounded_stream_pair, StreamCanceller, StreamOverflow, StreamReader, StreamWriter,
};
use super::transport::{connect_exec, ExecConnection, UpgradeTarget};

/// Pending bytes per output, excluding one pump chunk and the native pipe.
pub(crate) const MAX_PENDING_BYTES: usize = 8 * 1024 * 1024;
const POLL: Duration = Duration::from_millis(5);

#[cfg(windows)]
trait CancelPipe: Send + Sync {
    fn cancel(&self);
}

#[cfg(windows)]
struct PipeRef<T>(Arc<T>);

#[cfg(windows)]
impl<T: std::os::windows::io::AsRawHandle + Send + Sync> CancelPipe for PipeRef<T> {
    fn cancel(&self) {
        use windows::Win32::Foundation::HANDLE;
        use windows::Win32::System::IO::CancelIoEx;
        // SAFETY: this Arc owns the exact handle used for the pending I/O.
        unsafe {
            let _ = CancelIoEx(HANDLE(self.0.as_raw_handle()), None);
        }
    }
}

fn failure(code: &str, message: impl Into<String>) -> MxcError {
    MxcError::backend_error(format!("w365_agents {code}: {}", message.into())).with_details(
        serde_json::json!({
            "code": code,
            "possiblySucceeded": true,
            "outcomeUnknown": true
        }),
    )
}

struct WorkerStop {
    cancelled: AtomicBool,
    finished: AtomicBool,
    #[cfg(windows)]
    pipe: Mutex<Option<Box<dyn CancelPipe>>>,
}

impl WorkerStop {
    #[cfg(windows)]
    fn for_pipe<T: std::os::windows::io::AsRawHandle + Send + Sync + 'static>(
        pipe: &Arc<T>,
    ) -> Arc<Self> {
        Arc::new(Self {
            cancelled: AtomicBool::new(false),
            finished: AtomicBool::new(false),
            pipe: Mutex::new(Some(Box::new(PipeRef(Arc::clone(pipe))))),
        })
    }

    #[cfg(unix)]
    fn for_pipe<T>(_pipe: &Arc<T>) -> Arc<Self> {
        Arc::new(Self {
            cancelled: AtomicBool::new(false),
            finished: AtomicBool::new(false),
        })
    }

    fn cancelled(&self) -> bool {
        self.cancelled.load(Ordering::Acquire)
    }

    fn cancel(&self) {
        self.cancelled.store(true, Ordering::Release);
        #[cfg(windows)]
        {
            // Retry across the flag-check -> kernel-I/O window, as for the
            // common cancellable reader. The Arc keeps the exact handle alive.
            while !self.finished.load(Ordering::Acquire) {
                if let Some(pipe) = self.pipe.lock().unwrap_or_else(|e| e.into_inner()).as_ref() {
                    pipe.cancel();
                }
                thread::sleep(POLL);
            }
        }
    }
}

struct Worker {
    join: JoinHandle<()>,
    stop: Arc<WorkerStop>,
}

impl Worker {
    fn spawn(
        name: &str,
        stop: Arc<WorkerStop>,
        work: impl FnOnce(Arc<WorkerStop>) + Send + 'static,
    ) -> io::Result<Self> {
        let worker_stop = Arc::clone(&stop);
        let (ready_tx, ready_rx) = mpsc::sync_channel(1);
        let join = thread::Builder::new().name(name.into()).spawn(move || {
            struct Finished(Arc<WorkerStop>);
            impl Drop for Finished {
                fn drop(&mut self) {
                    #[cfg(windows)]
                    self.0.pipe.lock().unwrap_or_else(|e| e.into_inner()).take();
                    self.0.finished.store(true, Ordering::Release);
                }
            }
            let _finished = Finished(Arc::clone(&worker_stop));
            #[cfg(unix)]
            {
                // A library host need not ignore SIGPIPE. Only this worker's
                // mask changes, so closing a caller's pipe cannot kill it.
                let mut signals = nix::sys::signal::SigSet::empty();
                signals.add(nix::sys::signal::Signal::SIGPIPE);
                if let Err(error) = signals.thread_block() {
                    let _ = ready_tx.send(Err(io::Error::from(error)));
                    return;
                }
            }
            let _ = ready_tx.send(Ok(()));
            work(worker_stop);
        })?;
        if let Err(error) = ready_rx
            .recv()
            .unwrap_or_else(|_| Err(io::Error::other("W365 pipe worker did not initialize")))
        {
            let _ = join.join();
            return Err(error);
        }
        Ok(Self { join, stop })
    }
}

struct Control {
    socket: TcpStream,
    cancelled: Arc<AtomicBool>,
    terminal: AtomicBool,
    outputs: [StreamCanceller; 2],
    workers: Vec<Arc<WorkerStop>>,
    relay: Mutex<Option<JoinHandle<()>>>,
}

impl Control {
    fn stop(&self) {
        self.cancelled.store(true, Ordering::Release);
        let _ = self.socket.shutdown(Shutdown::Both);
        for output in &self.outputs {
            output.close();
        }
        for worker in &self.workers {
            worker.cancel();
        }
        if let Some(relay) = self.relay.lock().unwrap_or_else(|e| e.into_inner()).take() {
            let _ = relay.join();
        }
    }
}

impl Drop for Control {
    fn drop(&mut self) {
        if !self.terminal.load(Ordering::Acquire) {
            self.stop();
        }
        // Capture releases its process before joining readers. After completion
        // the supervisor owns the pumps until caller-owned pipes drain or close.
    }
}

#[cfg(windows)]
fn raw(pipe: &impl std::os::windows::io::AsRawHandle) -> PipeHandle {
    windows::Win32::Foundation::HANDLE(pipe.as_raw_handle())
}

#[cfg(unix)]
fn raw(pipe: &impl std::os::fd::AsRawFd) -> PipeHandle {
    pipe.as_raw_fd()
}

#[cfg(windows)]
fn prepare_pipe_io(_pipe: &impl std::os::windows::io::AsRawHandle) -> io::Result<()> {
    Ok(())
}

#[cfg(unix)]
fn prepare_pipe_io(pipe: &impl std::os::fd::AsRawFd) -> io::Result<()> {
    let fd = pipe.as_raw_fd();
    // SAFETY: the borrowed descriptor remains owned by the worker.
    let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
    if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
        return Err(io::Error::last_os_error());
    }
    Ok(())
}

#[cfg(windows)]
fn wait_for_output(_pipe: &impl std::os::windows::io::AsRawHandle) -> io::Result<()> {
    thread::sleep(POLL);
    Ok(())
}

#[cfg(unix)]
fn wait_for_output(pipe: &impl std::os::fd::AsRawFd) -> io::Result<()> {
    let mut descriptor = libc::pollfd {
        fd: pipe.as_raw_fd(),
        events: libc::POLLOUT,
        revents: 0,
    };
    // Wake as soon as the caller drains space, but bound the wait for cancellation.
    // SAFETY: descriptor is initialized and its fd remains owned by this worker.
    let result = unsafe { libc::poll(&mut descriptor, 1, POLL.as_millis() as i32) };
    if result < 0 {
        let error = io::Error::last_os_error();
        if error.kind() != io::ErrorKind::Interrupted {
            return Err(error);
        }
    } else if descriptor.revents & libc::POLLNVAL != 0 {
        return Err(io::Error::from_raw_os_error(libc::EBADF));
    }
    Ok(())
}

fn output_worker(
    pipe: io::PipeWriter,
    mut source: StreamReader,
    fault: Arc<Mutex<Option<io::Error>>>,
) -> io::Result<Worker> {
    prepare_pipe_io(&pipe)?;
    let pipe = Arc::new(pipe);
    let stop = WorkerStop::for_pipe(&pipe);
    Worker::spawn("w365-output", stop, move |stop| {
        let mut pipe = &*pipe;
        let mut bytes = [0; MAX_FRAME_PAYLOAD];
        let result = (|| {
            while !stop.cancelled() {
                let count = source.read(&mut bytes)?;
                if count == 0 {
                    break;
                }
                let mut pending = &bytes[..count];
                while !pending.is_empty() && !stop.cancelled() {
                    match pipe.write(pending) {
                        Ok(0) => return Err(io::ErrorKind::WriteZero.into()),
                        Ok(count) => pending = &pending[count..],
                        Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
                            wait_for_output(pipe)?
                        }
                        Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
                        Err(error) => return Err(error),
                    }
                }
            }
            Ok(())
        })();
        if let Err(error) = result {
            // Closing a caller-owned reader intentionally abandons its output.
            if !stop.cancelled() && error.kind() != io::ErrorKind::BrokenPipe {
                *fault.lock().unwrap_or_else(|e| e.into_inner()) = Some(error);
            }
        }
    })
}

enum Input {
    Bytes(Vec<u8>),
    Eof,
    Error(io::Error),
}

fn input_worker(pipe: io::PipeReader, tx: mpsc::SyncSender<Input>) -> io::Result<Worker> {
    prepare_pipe_io(&pipe)?;
    let pipe = Arc::new(pipe);
    let stop = WorkerStop::for_pipe(&pipe);
    Worker::spawn("w365-input", stop, move |stop| {
        let mut pipe = &*pipe;
        let mut bytes = [0; MAX_FRAME_PAYLOAD];
        while !stop.cancelled() {
            let message = match pipe.read(&mut bytes) {
                Ok(0) => Input::Eof,
                Ok(count) => Input::Bytes(bytes[..count].to_vec()),
                Err(error) if error.kind() == io::ErrorKind::WouldBlock => {
                    thread::sleep(POLL);
                    continue;
                }
                Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
                Err(error) => Input::Error(error),
            };
            let terminal = !matches!(message, Input::Bytes(_));
            let mut pending = message;
            loop {
                if stop.cancelled() {
                    return;
                }
                match tx.try_send(pending) {
                    Ok(()) => break,
                    Err(mpsc::TrySendError::Disconnected(_)) => return,
                    Err(mpsc::TrySendError::Full(message)) => {
                        pending = message;
                        thread::sleep(POLL);
                    }
                }
            }
            if terminal {
                return;
            }
        }
    })
}

pub(crate) fn run(target: UpgradeTarget, start: ExecStartPayload) -> Result<ExecHandle, MxcError> {
    let start_frame = start.encode()?;
    let (stdout_reader, stdout_pipe) = io::pipe().map_err(setup_error)?;
    let (stderr_reader, stderr_pipe) = io::pipe().map_err(setup_error)?;
    let (stdin_pipe, stdin_writer) = io::pipe().map_err(setup_error)?;
    let (out, out_source, out_overflow) = bounded_stream_pair(MAX_PENDING_BYTES);
    let (err, err_source, err_overflow) = bounded_stream_pair(MAX_PENDING_BYTES);
    let outputs = [out_source.canceller(), err_source.canceller()];
    let output_fault = Arc::new(Mutex::new(None));
    let (tx, rx) = mpsc::sync_channel(1);
    let mut workers = Vec::new();
    let setup = (|| {
        workers.push(output_worker(
            stdout_pipe,
            out_source,
            Arc::clone(&output_fault),
        )?);
        workers.push(output_worker(
            stderr_pipe,
            err_source,
            Arc::clone(&output_fault),
        )?);
        workers.push(input_worker(stdin_pipe, tx)?);
        Ok::<(), io::Error>(())
    })();
    if let Err(error) = setup {
        for output in &outputs {
            output.close();
        }
        for worker in workers {
            worker.stop.cancel();
            let _ = worker.join.join();
        }
        return Err(setup_error(error));
    }

    // No remote process has been admitted before pipes and workers are ready.
    let connected = connect_exec(&target);
    let mut stream = match connected {
        Ok(stream) => stream,
        Err(error) => {
            for output in &outputs {
                output.close();
            }
            for worker in workers {
                worker.stop.cancel();
                let _ = worker.join.join();
            }
            return Err(error);
        }
    };
    let socket = match stream.socket().try_clone() {
        Ok(socket) => socket,
        Err(error) => {
            for output in &outputs {
                output.close();
            }
            for worker in workers {
                worker.stop.cancel();
                let _ = worker.join.join();
            }
            return Err(setup_error(error));
        }
    };
    let control = Arc::new(Control {
        socket,
        cancelled: Arc::new(AtomicBool::new(false)),
        terminal: AtomicBool::new(false),
        outputs,
        workers: workers
            .iter()
            .map(|worker| Arc::clone(&worker.stop))
            .collect(),
        relay: Mutex::new(None),
    });
    // A failed post-upgrade send is an unknown execution outcome, never an exit.
    if let Err(error) = with_transport_stack(|| {
        stream
            .write_all(&start_frame)
            .and_then(|()| stream.flush())
            .map_err(|error| failure("exec_transport", format!("send EXEC_START: {error}")))
    }) {
        control.stop();
        for worker in workers {
            let _ = worker.join.join();
        }
        return Err(error);
    }
    let (done_tx, done_rx) = mpsc::sync_channel(1);
    let supervisor_control = Arc::downgrade(&control);
    let cancellation = Arc::clone(&control.cancelled);
    let worker_slot = Arc::new(Mutex::new(Some(workers)));
    let relay_workers = Arc::clone(&worker_slot);
    let spawned = thread::Builder::new()
        .name("w365-relay".into())
        .spawn(move || {
            let Some(workers) = relay_workers
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .take()
            else {
                let _ = done_tx.send(Err(failure(
                    "exec_worker",
                    "W365 workers were already consumed",
                )));
                return;
            };
            let result = consume(
                &mut stream,
                rx,
                &out,
                &err,
                &cancellation,
                &output_fault,
                start.client_budget,
            )
            .and_then(|code| truncated(code, &out_overflow, &err_overflow));
            let _ = stream.socket().shutdown(Shutdown::Both);
            drop(stream);
            out.close();
            err.close();
            if let Some(input) = workers.last() {
                input.stop.cancel();
            }
            if let Some(control) = supervisor_control.upgrade() {
                control.terminal.store(true, Ordering::Release);
            }
            let _ = done_tx.send(result);
            // Completion is independent of output backpressure, but these workers
            // remain owned until each caller-owned pipe drains or closes.
            for worker in workers {
                let _ = worker.join.join();
            }
        });
    let relay = match spawned {
        Ok(relay) => relay,
        Err(error) => {
            control.stop();
            if let Some(workers) = worker_slot.lock().unwrap_or_else(|e| e.into_inner()).take() {
                for worker in workers {
                    let _ = worker.join.join();
                }
            }
            return Err(failure(
                "exec_worker",
                format!("start W365 relay worker: {error}"),
            ));
        }
    };
    *control.relay.lock().unwrap_or_else(|e| e.into_inner()) = Some(relay);

    let stdout = raw(&stdout_reader);
    let stderr = raw(&stderr_reader);
    let stdin = raw(&stdin_writer);
    let input = Arc::new(Mutex::new(Some(stdin_writer)));
    let closer = Arc::clone(&input);
    let wait_control = Arc::clone(&control);
    Ok(ExecHandle {
        stdout,
        stderr,
        stdin,
        stdin_closer: Some(Box::new(move || {
            closer.lock().unwrap_or_else(|e| e.into_inner()).take();
        })),
        waiter: Box::new(move || {
            // Adapters duplicate the output handles before starting this waiter.
            // Retaining these originals would hide caller-closed output streams.
            drop((stdout_reader, stderr_reader));
            let _owners = (input, wait_control);
            done_rx
                .recv()
                .map_err(|_| failure("exec_worker", "W365 relay ended without an outcome"))?
        }),
        terminator: Box::new(move || {
            // Terminal errors still leave a queued prefix for caller-owned readers.
            if !control.terminal.load(Ordering::Acquire) {
                control.stop();
            }
            Err(failure(
                "remote_termination_unsupported",
                "W365 relay closed locally; remote process termination is not confirmed. Release the reservation explicitly.",
            ))
        }),
    })
}

fn setup_error(error: io::Error) -> MxcError {
    MxcError::backend_error(format!("set up W365 execution pipes: {error}"))
}

fn truncated(
    code: i32,
    stdout: &StreamOverflow,
    stderr: &StreamOverflow,
) -> Result<ExecOutcome, MxcError> {
    if stdout.has_overflowed() || stderr.has_overflowed() {
        return Err(failure(
            "output_truncated",
            "W365 execution output exceeded the pending-byte limit; output was truncated",
        )
        .with_details(serde_json::json!({
            "code": "output_truncated", "exitCode": code,
            "stdoutTruncated": stdout.has_overflowed(),
            "stderrTruncated": stderr.has_overflowed(),
            "pendingByteLimit": MAX_PENDING_BYTES
        })));
    }
    Ok(ExecOutcome::Exited(code))
}

struct PendingInput {
    bytes: Vec<u8>,
    written: usize,
}

fn consume(
    stream: &mut ExecConnection,
    input: mpsc::Receiver<Input>,
    out: &StreamWriter,
    err: &StreamWriter,
    cancelled: &AtomicBool,
    output_fault: &Mutex<Option<io::Error>>,
    budget: Option<Duration>,
) -> Result<i32, MxcError> {
    stream.enable_duplex().map_err(|error| {
        failure(
            "exec_transport",
            format!("configure W365 duplex transport: {error}"),
        )
    })?;
    let mut accumulator = FrameAccumulator::new();
    let mut bytes = [0; 16 * 1024];
    let mut stdin_open = true;
    let mut started = false;
    let mut pending: Option<PendingInput> = None;
    let clock_start = Instant::now();
    loop {
        if let Some(budget) = budget {
            let elapsed = clock_start.elapsed();
            #[cfg(feature = "test-support")]
            let elapsed = match std::env::var_os("MXC_W365_TEST_EXEC_ELAPSED_MS") {
                None => elapsed,
                Some(value) => elapsed.saturating_add(Duration::from_millis(
                    value
                        .to_str()
                        .and_then(|value| value.parse::<u64>().ok())
                        .ok_or_else(|| {
                            failure("fixture_configuration", "invalid W365 fixture elapsed time")
                        })?,
                )),
            };
            if elapsed >= budget {
                return Err(failure(
                    "exec_client_timeout",
                    "local execution budget expired without Exit; remote outcome is unknown",
                ));
            }
        }
        if let Some(error) = output_fault
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .take()
        {
            return Err(failure(
                "output_io",
                format!("write W365 output pipe: {error}"),
            ));
        }
        if cancelled.load(Ordering::Acquire) {
            return Err(failure(
                "exec_disposed",
                "W365 execution relay was closed locally; remote outcome is unknown",
            ));
        }
        let mut progressed = false;
        match stream.read(&mut bytes) {
            Ok(0) => {
                return Err(failure(
                    "exec_transport",
                    "W365 transport closed before EXIT; execution outcome is unknown",
                ))
            }
            Ok(count) => {
                accumulator.extend(&bytes[..count]);
                progressed = true;
            }
            Err(error)
                if matches!(
                    error.kind(),
                    io::ErrorKind::TimedOut
                        | io::ErrorKind::WouldBlock
                        | io::ErrorKind::Interrupted
                ) => {}
            Err(error) => {
                return Err(failure(
                    "exec_transport",
                    format!("read W365 execution: {error}"),
                ))
            }
        }
        while let Some(frame) = accumulator
            .next_frame()
            .map_err(|error| failure("exec_protocol", error.message))?
        {
            match frame.kind {
                Some(FrameType::Started) if !started => {
                    parse_started(&frame.payload)
                        .map_err(|error| failure("exec_protocol", error.message))?;
                    started = true;
                }
                Some(FrameType::Stdout) if started => out.write(&frame.payload),
                Some(FrameType::Stderr) if started => err.write(&frame.payload),
                Some(FrameType::Exit) if started => {
                    return parse_exit(&frame.payload)
                        .map(|exit| exit.exit_code)
                        .map_err(|error| failure("exec_protocol", error.message))
                }
                Some(FrameType::Error) => {
                    let error = parse_error(&frame.payload)
                        .map_err(|error| failure("exec_protocol", error.message))?;
                    return Err(failure(
                        "remote_exec_error",
                        format!("W365 remote execution failed: {}", error.message),
                    )
                    .with_details(serde_json::json!({
                        "code":"remote_exec_error", "remoteCode":error.code,
                        "possiblySucceeded":true, "outcomeUnknown":true
                    })));
                }
                _ => {
                    return Err(failure(
                        "exec_protocol",
                        format!("unexpected W365 frame type {}", frame.raw_kind),
                    ))
                }
            }
        }
        // Read terminal frames before sending more input: a process may exit
        // without consuming stdin. One input chunk per read keeps EXIT fair.
        progressed |= stream
            .flush_outgoing()
            .map_err(|error| failure("exec_transport", format!("send W365 stdin: {error}")))?;
        if pending.is_none() && !stream.has_pending_output() && stdin_open && started {
            let frame = match input.try_recv() {
                Ok(Input::Bytes(bytes)) => Some(encode_frame(FrameType::Stdin, &bytes)),
                Ok(Input::Eof) | Err(mpsc::TryRecvError::Disconnected) => {
                    stdin_open = false;
                    Some(encode_frame(FrameType::StdinClose, b""))
                }
                Ok(Input::Error(error)) => {
                    return Err(failure(
                        "stdin_io",
                        format!("read W365 stdin pipe: {error}"),
                    ))
                }
                Err(mpsc::TryRecvError::Empty) => None,
            };
            pending = frame.map(|bytes| PendingInput { bytes, written: 0 });
        }
        if let Some(frame) = &mut pending {
            if frame.written < frame.bytes.len() && !stream.has_pending_output() {
                match stream.write(&frame.bytes[frame.written..]) {
                    Ok(0) => {
                        return Err(failure(
                            "exec_transport",
                            "send W365 stdin: transport accepted no bytes",
                        ))
                    }
                    Ok(count) => {
                        frame.written += count;
                        progressed = true;
                    }
                    Err(error)
                        if matches!(
                            error.kind(),
                            io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted
                        ) => {}
                    Err(error) => {
                        return Err(failure(
                            "exec_transport",
                            format!("send W365 stdin: {error}"),
                        ))
                    }
                }
            }
            progressed |= stream
                .flush_outgoing()
                .map_err(|error| failure("exec_transport", format!("send W365 stdin: {error}")))?;
            if frame.written == frame.bytes.len() {
                match stream.flush() {
                    Ok(()) => pending = None,
                    Err(error)
                        if matches!(
                            error.kind(),
                            io::ErrorKind::WouldBlock | io::ErrorKind::Interrupted
                        ) => {}
                    Err(error) => {
                        return Err(failure(
                            "exec_transport",
                            format!("flush W365 stdin: {error}"),
                        ))
                    }
                }
            }
        }
        if !progressed {
            thread::sleep(POLL);
        }
    }
}