Skip to content

feat(cli): add exec --stream for non-PTY bidirectional streaming exec#922

Open
dijdzv wants to merge 7 commits into
superradcompany:mainfrom
dijdzv:feat/exec-stream
Open

feat(cli): add exec --stream for non-PTY bidirectional streaming exec#922
dijdzv wants to merge 7 commits into
superradcompany:mainfrom
dijdzv:feat/exec-stream

Conversation

@dijdzv

@dijdzv dijdzv commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

What

Adds a --stream flag to msb exec. It streams stdin/stdout bidirectionally
without buffering or a PTY, built on the existing streaming primitives
(Sandbox::exec_stream_with + ExecHandle::recv + ExecSink).

Diff is one file (crates/cli/lib/commands/exec.rs); the default exec path,
agentd, and the protocol are untouched.

Why

msb exec can't drive a long-lived guest process turn by turn (read turn N's
output, then send turn N+1's request):

  • the default path reads stdin to EOF up front and buffers all output until
    the process exits (collect()), so output never comes back mid-run;
  • -t/--tty allocates a PTY, which echoes input and translates CRLF —
    unsafe for line protocols like JSON lines.

--stream forwards host stdin incrementally and flushes guest stdout/stderr per
chunk, with no PTY. This lets a host driver run e.g. an agent CLI in stream-json
mode and read each turn before sending the next.

How

  • New --stream bool; skips the upfront read_to_end of stdin.
  • Calls exec_stream_with(... .stdin_pipe()).
  • A background task forwards host stdin → guest stdin incrementally; host EOF
    closes the guest's stdin.
  • Drains ExecEvents, writing + flushing Stdout/Stderr per chunk, exits with
    the child's code, and fails if the session ends without an Exited event
    (matching the buffered collect() path).

Notes

  • Builds on the streaming SDK API already present (added in feat(microsandbox): add exec, attach, and streaming execution (#387) #387).

  • Adds a CLI e2e test (crates/cli/tests/exec_stream.rs) that drives a guest
    turn by turn over the stream; it deadlocks under the buffered exec path, so
    it fails closed if streaming ever regresses. Gated behind #[msb_test] like
    the other KVM integration tests:

    cargo test -p microsandbox-cli --test exec_stream -- --ignored
    

dijdzv and others added 4 commits June 5, 2026 14:49
…g exec

orbit drives a long-lived guest agent process (claude --input-format
stream-json) turn by turn: it must read each turn's output before sending
the next request. The default `exec` path can't do this — it reads stdin to
EOF upfront and buffers all guest output until the process exits (collect()).
`ssh connect` streams stdout but doesn't forward piped stdin.

`--stream` uses the existing streaming primitives (exec_stream_with +
ExecHandle::recv + ExecSink) to forward host stdin incrementally and flush
guest stdout/stderr per chunk, without allocating a PTY (so the byte stream
is not echoed or CRLF-translated — safe for JSON lines). The default path is
untouched, so existing buffered exec callers are unaffected. agentd/protocol
unchanged (CLI-only patch).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`_ => {}` would silently drop a newly added ExecEvent variant. Matching
`Started`/`StdinError` by name (as the SDK's own `collect()` does) turns a
future variant into a compile error instead of a swallowed event.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…event

The recv loop seeded exit_code to 0 and fell through to Ok(()), so a session
that ended without an Exited event (agent/VM dropped mid-run) was reported as
success. The buffered `exec_with`/`collect()` path errors here. Track the code
as Option and bail on None so a dead session can't masquerade as exit 0.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The `--stream` flag had no test. The SDK primitives it wraps are covered by
`tests/stdin.rs`, but the CLI wiring — forward stdin incrementally, flush
output per chunk — wasn't. Add a `#[msb_test]` e2e that drives a guest awk
loop turn by turn over the streamed channel, sending each line only after
reading the previous reply. The buffered `exec` path reads stdin to EOF
before emitting output, so it deadlocks here; completing within the timeout
is the assertion (confirmed: dropping `--stream` makes it time out).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant