Skip to content

fix(box): forward the shell alias's stdin to the command - #140

Closed
ridhimrastogi wants to merge 4 commits into
strands-agents:mainfrom
ridhimrastogi:fix/alias-forwards-stdin
Closed

ridhimrastogi wants to merge 4 commits into
strands-agents:mainfrom
ridhimrastogi:fix/alias-forwards-stdin

Conversation

@ridhimrastogi

@ridhimrastogi ridhimrastogi commented Oct 21, 2026 •

Copy link
Copy Markdown

Description

Problem

A command run through the box shell alias gets an empty stdin:

printf 'hello' | zsh -c 'read LINE; printf "[%s]" "$LINE"'   # [] inside a box, [hello] outside

The broker accepts stdin as Input frames, but the Shell and Python alias sent Open, Call, and InputEof with nothing between them. The hook decision tells operators to rewrite each harness hook as <box_dir>/bin/zsh -c "<command>". A harness gives a hook its payload as JSON on stdin, so a rewritten hook gets no payload. See #120.

Fix

The alias now sends its stdin as Input frames, then InputEof. One pump_stdin does this for every alias, in one of three modes:

  • Shell Call (Grace): forwards stdin. A terminal gets InputEof at once. A stdin that gives no byte and no end-of-file within 200 ms (STDIN_GRACE) is treated as empty. Without the grace, a command that pre-reads stdin, such as the lua builtin, waits until its deadline when a harness holds stdin open and writes nothing.
  • MCP server (Stream): no grace, because a quiet MCP client must not get an end-of-file. The pump starts after the broker answers McpReady. The alias stays up until its client closes stdin, and it drains the socket while it waits. Before, a blocking tokio::io::stdin read did this by accident. A refused server still ends the alias at once.
  • Python Call (Closed): reads no stdin and sends InputEof at once. Monty has no input(), and reading stdin would take bytes from the commands after it, for example in while read f; do python3 …; done < list.

In the broker:

  • program.rs: unread stdin is bounded at 1 MiB per Program (MAX_BUFFERED_INPUT_BYTES), counted in bytes. Before, the bound was 16 chunks of any size. A chunk releases its bytes when the reader takes it. Past the bound, the broker still answers Denied { FlowControl }.
  • host.rs: a Python Program accepts Input and InputEof and drops them. Signal is still refused by name.

The stdin bullet in crates/box/AGENTS.md and one sentence of the hook decision describe the new behavior.

Interface

No frame, PROTOCOL_VERSION, alias name, or socket derivation changes. Two observable behaviors change: a Shell Call is now followed by Input frames, and a Python Program drops Input instead of refusing it. This needs a maintainer call under the interface freeze.

Known limits

This is an interim fix. #120 stays open for a credit frame.

  • Only Strands Shell's own commands, such as read, cat, jq, and lua, get the stdin. A program that runs in a tool leaf, such as a hook script, still starts with an empty stdin. Most real hooks are scripts, so this does not fix them yet.
  • A writer that is silent for more than 200 ms before its first byte loses its input.
  • When a harness holds stdin open and silent, each Shell call waits up to 200 ms more.
  • More than 1 MiB of stdin that the command does not read exits 125. A slow writer of chunks under 4 KiB can reach the 256-slot channel bound below 1 MiB.
  • A pipe that writes some bytes and then stays open and silent still holds a command that pre-reads stdin.
  • The alias reads its stdin even when the command does not. An agent that runs zsh -c without redirecting stdin has its own stdin read by the alias.

Related Issues

Refs #120 (interim fix; the issue stays open for the credit-frame fix)

Type of Change

Bug fix

Testing

Test What it proves
box_cardinality.rs::e5a_stdin_reaches_a_shell_command A piped value reaches zsh -c 'read …'. The old experiment asserted that nothing arrived.
box_shell.rs::a_silent_open_stdin_does_not_hold_a_command_that_pre_reads_it A stdin held open and silent does not hold lua -e to its deadline.
box_shell.rs::read_stdin_releases_its_bytes_from_the_broker_budget 2 MiB reaches wc -c, which is past the 1 MiB budget.
box_shell.rs::a_python_call_leaves_stdin_to_the_commands_after_it A Python Call leaves stdin for the next command.
host.rs::unread_stdin_is_held_up_to_the_byte_budget A Program that reads nothing holds 1 MiB, and the next chunk gets Denied { FlowControl }.
host.rs::a_python_program_drops_input_and_refuses_signal_by_name A Python Program drops Input and InputEof, refuses Signal, and still runs a Call.
runtime_mcp_start.rs::a_refused_server_ends_its_alias_while_the_client_keeps_writing A refused MCP server ends its alias while the client still writes. After the merge, main refuses the server in the answer to Open, before the pump starts, so this test also passes without the fix. No test now covers a Denied frame during a session.
runtime_mcp_policy_staging.rs::one_failure_reaps_all_live_leader_and_follower_groups_before_exchange_close (existing) The MCP alias stays up after its server fails.

This branch merged main, last at 61ade09. The one conflict was stream_mcp, where main added the McpReady and McpUnmatched routing. The stdin pump now starts after McpReady, so after McpUnmatched the ordinary program gets all of stdin. Gates on the merge ran on Linux x86_64, in a rust:1.98.1-bookworm container with EXPERIMENTAL_X86_SUPPORT=TRUE:

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)

  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

  • Build, just fmt-check, and just clippy: clean.

  • cargo test --workspace --locked --all-features --no-fail-fast, on 23c4c7a: 5469 passed, 12 failed, 20 ignored. Nine are the sandbox and file-permission tests that fail the same way on main in that container. Three in mcp_alias_fallback.rs failed with Text file busy while the suite ran in parallel, and passed when that file ran alone (9 passed).

  • Not run: the tests that start a box through the namespace launcher, which include the five box tests above and the MCP lifetime test. On x86_64 they return early and report ok. CI runs them on Ubuntu ARM64 and macOS.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

// `InputEof` immediately after the Call, because this alias forwards no input: a harness
// invoking `zsh -c` gives the command nothing on stdin, and without the EOF the Program's stdin
for body in [
let reads_stdin = forwards_stdin(&interpreter);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do the same for python too, so every interpreter gets open, call, input…, input_eof and the alias has no special case here. the broker side needs a small change for that, since it refuses input/input_eof for python today (only harmless because python runs inline and sends exit first), it can accept and drop them. python can't actually use the input yet, monty 1.0.0 has no input() or sys.stdin, but at least the wire sequence is the same for all three.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done on the broker side: a Python Program now takes Input and InputEof and drops them. Signal is still refused by name (host.rs::a_python_program_drops_input_and_refuses_signal_by_name).

On the alias side I kept one difference, and I'd like your call on it. When the Python alias forwarded stdin, it consumed bytes that Monty can't use. Those bytes were then gone for the commands after it:

printf 'a\nb\n' | { python3 -c 'print(1)'; read L; echo "[$L]"; }   # [a] before, [] when Python forwards stdin

That breaks while read f; do python3 …; done < list. So a Python Call now uses StdinMode::Closed: it sends Open, Call, InputEof, which is the zero-Input case of the same sequence. There's no separate frame path, only a mode on the one pump. box_shell.rs::a_python_call_leaves_stdin_to_the_commands_after_it pins it. Once Monty can read stdin, it's a one-line switch to Grace. If you'd rather have the full stream now anyway, tell me and I'll flip it.

@ferdingler

ferdingler commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Thanks for this! Quick question. Most hooks I've seen, and pretty much all plugin hooks, are a script file like ~/.claude/hooks/check.sh, not an inline command. That means those run as a tool in their own leaf box, and that leaf always starts with an empty stdin. So even with this change, the JSON payload would still not reach a script hook. Am I reading that right? @priyasingh2111 should have context about this.

Happy to approve because the change here makes sense, but I wonder if its actually going to solve anything for hooks.

@ridhimrastogi

Copy link
Copy Markdown
Author

Thanks for this! Quick question. Most hooks I've seen, and pretty much all plugin hooks, are a script file like ~/.claude/hooks/check.sh, not an inline command. That means those run as a tool in their own leaf box, and that leaf always starts with an empty stdin. So even with this change, the JSON payload would still not reach a script hook. Am I reading that right? @priyasingh2111 should have context about this.

Happy to approve because the change here makes sense, but I wonder if its actually going to solve anything for hooks.

You're right, good catch. This PR only fixes inline hooks. Script hooks like ./hook.sh still get an empty stdin, because they run in a tool leaf that starts with /dev/null.
So on its own this doesn't fix most real hooks yet. It's the first step: it gets stdin from the agent into the shell. The second step is passing that stdin into the tool leaf instead of /dev/null. I'll write that up on #120 as a follow-up.

Unrelated: the failing macOS check is a flaky test-integ egress test. It's failed the same way on main and on a few other branches, so it should pass on a re-run.

The shell alias sent `Open`, `Call`, and `InputEof` with no frames between them. A command run
as `zsh -c "<command>"` therefore read an empty stdin, although the broker accepts in-band
`Input` for a Shell Program. docs/design/decisions.md#a-harness-hook-runs-only-in-exec-form tells an
operator to rewrite each harness hook to that form, and a hook gets its payload as JSON on stdin,
so every rewritten hook lost its payload. Issue strands-agents#120.

- For a Shell Call, the alias sends its stdin as `Input` frames and then `InputEof`. It relays
  output at the same time, and it keeps the socket's write half open until the Program exits.
  The `Input` writes have no timeout, because a cancelled write leaves part of a frame on the
  socket. A terminal stdin gets `InputEof` at once.
- A stdin that yields no byte and no end-of-file within `STDIN_GRACE` (200 ms) is treated as
  empty. A harness can hold the alias's stdin open and write nothing, and the `lua` builtin
  pre-reads stdin, so without the grace each such `lua` call blocks to its deadline.
- A Python Call keeps `Open`, `Call`, `InputEof`, because the broker refuses `Input` for a Python
  Program.
- The broker holds at most `MAX_BUFFERED_INPUT_BYTES` (1 MiB) of unread stdin per Program, counted
  in bytes. Before, the bound was 16 chunks of any size. Each queued chunk releases its bytes when
  the reader takes it, so the real hold is the budget plus one partly read chunk. The channel
  depth is 256, so chunks under 4 KiB reach that bound first. Past either bound the broker still
  answers `Denied { FlowControl }`.
- No frame, no `PROTOCOL_VERSION`, no alias name, and no socket derivation changes. The frame
  sequence that the alias sends changes, which crates/box/AGENTS.md and the decision entry now
  state.

Rejected: a credit frame for flow control. It is the complete fix, but it changes the protocol,
so it is left for issue strands-agents#120.

- `box_cardinality.rs::e5a_stdin_reaches_a_shell_command`: the inverted experiment. A piped
  value reaches `zsh -c 'read LINE …'`. It fails with the old alias (`read=[]`).
- `box_shell.rs::a_silent_open_stdin_does_not_hold_a_command_that_pre_reads_it`: a silent open
  pipe does not hold `lua -e`. It fails with the grace removed (`waited 8s`).
- `box_shell.rs::read_stdin_releases_its_bytes_from_the_broker_budget`: 2 MiB reaches `wc -c`.
  It fails when the chunk does not release its bytes.
- `host.rs::unread_stdin_is_held_up_to_the_byte_budget`: a Program that reads nothing holds the
  budget, and the next chunk gets `Denied { FlowControl }`.
- `strands-box-sock-alias::only_a_shell_call_forwards_stdin`: Python gets no stdin. It fails when
  Python forwards stdin.
- `cargo test -p strands-box --all-features` on macOS arm64: 956 passed, 0 failed, 6 ignored.
  `cargo fmt --check` is clean. `cargo clippy` has one owned warning, in `run/hosted.rs`, which
  this change does not touch.

Not run: Linux. Still broken, and accepted: a writer silent for more than 200 ms before its first
byte loses its input; more than 1 MiB of stdin that the command does not read exits 125; a pipe
that writes some bytes and then stays open and silent still holds a command that pre-reads stdin;
the alias reads its stdin even when the command does not, so an agent that runs `zsh -c` without
redirecting stdin has its own stdin read by the alias; a slow writer of chunks under 4 KiB can
reach the channel bound below 1 MiB.
@ridhimrastogi
ridhimrastogi force-pushed the fix/alias-forwards-stdin branch from 223d6e4 to 7d94c3e Compare November 10, 2026 18:43
@ferdingler

Copy link
Copy Markdown
Collaborator

Thanks for this one, it got the ball rolling on hook stdin. #397 landed the same fix: the Shell alias forwards piped stdin, a terminal gets EOF right away, Python reads none, and the broker caps what it holds. The one thing it doesn't do is the 200 ms grace for an open but silent stdin, and that was deliberate: a command that reads stdin waits like bash does instead of losing input from a slow writer. Closing as superseded by #397.

@ferdingler ferdingler closed this Nov 13, 2026
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.

4 participants