Repository navigation
feat(shell): forward a Shell alias's stdin to its command - #397
Conversation
## Problem
A command run through the `zsh`, `bash`, or `sh` alias gets empty stdin.
A harness hook reads its event JSON on stdin, so no hook can read its
input. The hosted Shell also loses stdin in `$(...)` and `( ... )`,
reports status 0 for `{ false; } > f`, and ignores `!` before a compound
command, so a guard such as `if ! (cmd) > f` takes the wrong branch.
## Solution
The Shell alias classifies fd 0 with `fstat`. It streams a pipe, a
regular file, or a socket as `Input` frames after the `Call`, then sends
`InputEof`. It returns at `Exit`, so a stdin that its writer keeps open
holds no command. Over 64 KiB, or on a read error, it signals the Call
and exits 125. No frame type or protocol field changes. The Python and
MCP aliases and tool leaves do not change.
The broker refuses an `Input` over one chunk before it decodes it. It
caps stdin per Program and per connection. A refusal signals the
running Call and refuses each later Call on that Program.
In the vendored Shell: a substitution and a subshell borrow the
caller's stdin; a redirected group keeps its status; `!` inverts a
compound (`parser::Item::Negated`); `set -e` exempts an inverted
command; and `lua` reads stdin only when the script names it.
## Tests
- Alias: `a_finite_pipe_is_read_byte_for_byte`,
`oversized_stdin_is_refused_and_stdin_at_the_limit_is_not`,
`a_socket_backed_stdin_is_read`,
`a_read_error_is_refused_rather_than_sent_as_empty_input`,
`stdin_is_classified_by_its_descriptor`,
`the_alias_streams_its_stdin_after_the_call`,
`a_stdin_held_open_does_not_hold_a_command_that_finishes`,
`oversized_stdin_interrupts_the_call_and_fails`.
- Broker (`host.rs`): `a_command_gets_its_full_stdin_before_the_call`,
`input_sent_after_the_call_reaches_the_running_command`,
`a_command_that_never_reads_its_stdin_finishes`,
`oversized_input_is_refused_at_the_broker_and_the_call_does_not_run`,
`an_overlong_input_frame_is_refused_and_the_call_does_not_run`,
`a_refused_input_stops_the_running_call_before_it_acts`,
`a_connections_input_is_capped_across_its_programs`,
`a_command_evaluated_from_stdin_is_decided_by_policy`,
`a_file_read_named_on_stdin_needs_its_own_permit`.
- Shell: the redirected-group, `negation_*`, stdin-borrow, lua, and
errexit tests in `tests/shell_integration.rs`. Each fails without
its change.
- Gates on x86_64 Linux: fmt, owned clippy 0, vendored clippy
unchanged, shell suite green. Box lib failures equal main's x86
baseline; `modern_cancelled_large_ids_reach_the_byte_limit...` is a
timing flake that passed 2 of 3 reruns.
- Not run here: the containment suite. macOS runs are in the PR.
|
Assessment: Request Changes One blocking problem. When the alias refuses oversized stdin or fails to read it, it still sends EOF, so a running command can finish on a truncated prefix of its input. That breaks the guarantee this PR writes into DetailsAssumptions and rules checked that held
Streaming after the Call, rather than collecting stdin to EOF first, is the right call for held-open harness stdin. |
|
Assessment: Request Changes Reviewed at head Checked and held
Once the stdin hand-back is fixed, the four Shell fixes and the streaming design look solid. |
… Call without EOF
|
Assessment: Request Changes I reviewed the changes from Checked and held at c651eab
|
|
Assessment: Request Changes Reviewed at head Checked and held
The stdin hand-back fix is thorough, and the new regression table test pins it well. |
|
Assessment: Request Changes Reviewed at head Checked and held at 716eab4
|
|
Assessment: Request Changes Reviewed at head
Checked and held
|
|
Assessment: Approve once the open threads from other reviewers are resolved. No new findings at
Checked and held at d12f6a1
|
A cancelled Call left the compound that starts a pipeline running with the caller's stdin. An abort guard now stops it. The alias also fails when a stdin write fails, the connection limit test checks the cut, and the shell page no longer promises that a writer never gets a broken pipe.
|
Assessment: Approve. 6b4e451 makes one real Shell fix, and I left one test-coverage comment on it (non-blocking). Checked at 6b4e451 and held
|
|
Assessment: Approve. 71ca11c adds the Shell-level test I asked for, and it now pins the abort guard from 6b4e451. I have no new findings. Checked at 71ca11c and held
|
…against the upstream Shell The vendored Shell files that #397 changed are deleted here. #397's Shell tests move to crates/shell/tests/main_shell_semantics.rs and the broker tests, and its two Lua stdin tests move to the broker. The tests for #397's Shell fixes fail until the upstream Shell fixes land.
Description
What / why. A command run through the
zsh,bash, orshalias received empty stdin. A harness hook (for example, a Claude Code hook in exec form,{"command":"bash","args":["/path/hook.sh"]}) gets its event JSON on stdin, so no hook could read its input. This PR forwards finite stdin to the hosted Shell. It also fixes three Shell defects that a hook script hits:$(cat)and( ... )read no stdin;{ false; } > freported 0;!before a compound was ignored;Before and after.
On
main, the alias sends the command text but not its stdin, so a hook reads nothing:flowchart LR subgraph cage["Agent sandbox"] H["Claude Code hook: bash hook.sh, event JSON on stdin"] --> A["bash alias"] end subgraph tcb["Trusted process"] B["Broker"] --> S["Shell runs hook.sh"] end A -->|"command text only"| B S -->|"input=$(cat) reads empty"| X["hook cannot see the event"]With this PR, the alias streams stdin after the Call, and the broker caps it:
flowchart LR subgraph cage["Agent sandbox"] H["Claude Code hook: bash hook.sh, event JSON on stdin"] --> A["bash alias"] end subgraph tcb["Trusted process"] B["Broker: caps at limits.shell.max_stdin (64 KiB default)"] --> S["Shell runs hook.sh"] end A -->|"Call, then Input frames, then InputEof"| B S -->|"input=$(cat) reads the event"| P["fs:* and shell:* decisions as before"] S -->|"exit 2"| K["Claude Code blocks the tool call"]How.
[limits.shell] max_stdin(a positive byte count, 64 KiB by default;0is a load error because the cap bounds the stdin the trusted process holds), and per connection at that × the programs per connection. Unread stdin waits in one buffer of at mostmax_stdinbytes, however many frames brought it, and reaches the Shell in frames of at most one 48 KiB chunk. Past the cap it feeds exactlymax_stdinbytes, discards the rest one frame at a time, withholds EOF, sends no Denied, and does not stop the Call: a command that ignores its input succeeds, and a reader past the cap waits for the Shell deadline and fails, with a stderr note that its input was cut.crates/shell/UPSTREAM.md):$(...)and( ... )read the caller's stdin;{ ...; } > fkeeps its status;!inverts a compound. Lua reads outside stdin as empty, as onmain.New primitive or interface? No new frame types or fields, and
PROTOCOL_VERSIONis unchanged. One additive box.toml key,[limits.shell] max_stdin, approved by the maintainer; a record that sets it is stamped record version 33, so an older build refuses the box instead of resuming it with the default cap. Two behaviors change and can be observed: Shell aliases now forward stdin, and the broker caps it.Security change? No new authority. stdin is data. A command that turns it into code (
eval "$(cat)") or into a path (cat "$(cat)") is still decided by policy (a_command_evaluated_from_stdin_is_decided_by_policy,a_file_read_named_on_stdin_needs_its_own_permit). Every read stays in the contained alias; there is no host/dev/stdinmapping. The alias still refuses no-args and-s.Scope and costs:
Stdio::null(), and the Python and MCP aliases are unchanged.max_stdinlimit applies only to stdin piped into the alias from outside the Shell (hooks, or programs that startbashwith input). Pipes inside a command are not affected.shell.timeout = false, a reader past the cap waits until its caller stops it, like any blocked read.strands-box-sock-alias.rs(the Python aliases forward a Shell command when a tool replacespython3) and the external-command branch incrates/shell/src/exec.rs. A trial merge of feat(box): let a tool replace python3 so a project's own Python runs #389 onto this branch conflicts in both files. Whichever PR lands second resolves them. The two changes touch different branches of the alias.Related Issues
None filed. This is part of supporting harness hooks in a box.
Type of Change
New feature (plus three Shell bug fixes)
Testing
cargo test -p strands-shell --all-features: 2018 passed, 0 failed (x86_64 Linux and macOS).cargo test -p strands-box --all-featureson x86_64 Linux: lib failures equal main's x86 baseline (Linux containment there is ARM64 only). The MCP exchange testmodern_cancelled_large_ids_reach_the_byte_limit_before_the_count_limitis a timing flake: it passed 2 of 3 reruns.cargo fmt --all --check. Clippy shows 0 warnings incrates/box, and the count incrates/shellis unchanged (17).cargo test -p strands-shell --all-features: 2018 passed, 0 failed.hosttests (60) pass.box_cardinality:e5a_stdin_reaches_a_shell_commandpasses. The piped value arrives, which inverts the old experiment.e2b_a_non_yielding_request_does_not_delay_a_concurrent_oneis a load flake that also fails on main under the full suite (1 of 2 runs on main; 3 of 3 alone on this branch; the suite passes with--test-threads=1).e5a, inverted since.input=$(cat)read the event, and exit 2 blocked the tool call.{ false; } > f; echo $?printed1, andif ! { false; } > ftook the then-branch.cat > big.json.stdin is more than 65536 bytes; the command received only the first 65536.max_stdin = 262144: the reader got the whole event (200,793 bytes,tool_input.content100,000), with no cut note, and the run took 4 s.max_stdinyet. This run used a local Boxy build that passes the key through; it is not part of any PR.Checklist
docs/user/shell.md,docs/design/decisions.md,crates/box/AGENTS.md,crates/shell/UPSTREAM.md)By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.