Repository navigation
fix(box): forward the shell alias's stdin to the command - #140
ridhimrastogi wants to merge 4 commits into
Conversation
| // `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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 stdinThat 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.
|
Thanks for this! Quick question. Most hooks I've seen, and pretty much all plugin hooks, are a script file like 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. 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. |
aa1b8e4 to
3efacb5
Compare
5bf849b to
223d6e4
Compare
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.
… (review feedback)
223d6e4 to
7d94c3e
Compare
|
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. |
Description
Problem
A command run through the box shell alias gets an empty stdin:
The broker accepts stdin as
Inputframes, but the Shell and Python alias sentOpen,Call, andInputEofwith 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
Inputframes, thenInputEof. Onepump_stdindoes this for every alias, in one of three modes:Grace): forwards stdin. A terminal getsInputEofat 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 theluabuiltin, waits until its deadline when a harness holds stdin open and writes nothing.Stream): no grace, because a quiet MCP client must not get an end-of-file. The pump starts after the broker answersMcpReady. The alias stays up until its client closes stdin, and it drains the socket while it waits. Before, a blockingtokio::io::stdinread did this by accident. A refused server still ends the alias at once.Closed): reads no stdin and sendsInputEofat once. Monty has noinput(), and reading stdin would take bytes from the commands after it, for example inwhile 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 answersDenied { FlowControl }.host.rs: a Python Program acceptsInputandInputEofand drops them.Signalis still refused by name.The stdin bullet in
crates/box/AGENTS.mdand 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 ShellCallis now followed byInputframes, and a Python Program dropsInputinstead 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.
read,cat,jq, andlua, 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.zsh -cwithout 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
box_cardinality.rs::e5a_stdin_reaches_a_shell_commandzsh -c 'read …'. The old experiment asserted that nothing arrived.box_shell.rs::a_silent_open_stdin_does_not_hold_a_command_that_pre_reads_itlua -eto its deadline.box_shell.rs::read_stdin_releases_its_bytes_from_the_broker_budgetwc -c, which is past the 1 MiB budget.box_shell.rs::a_python_call_leaves_stdin_to_the_commands_after_ithost.rs::unread_stdin_is_held_up_to_the_byte_budgetDenied { FlowControl }.host.rs::a_python_program_drops_input_and_refuses_signal_by_nameInputandInputEof, refusesSignal, and still runs a Call.runtime_mcp_start.rs::a_refused_server_ends_its_alias_while_the_client_keeps_writingmainrefuses the server in the answer toOpen, before the pump starts, so this test also passes without the fix. No test now covers aDeniedframe during a session.runtime_mcp_policy_staging.rs::one_failure_reaps_all_live_leader_and_follower_groups_before_exchange_close(existing)This branch merged
main, last at61ade09. The one conflict wasstream_mcp, wheremainadded theMcpReadyandMcpUnmatchedrouting. The stdin pump now starts afterMcpReady, so afterMcpUnmatchedthe ordinary program gets all of stdin. Gates on the merge ran on Linux x86_64, in arust:1.98.1-bookwormcontainer withEXPERIMENTAL_X86_SUPPORT=TRUE:I ran the relevant suites (
just check, orcargo test --workspace --all-features)If I touched Rust, I ran
cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningsBuild,
just fmt-check, andjust clippy: clean.cargo test --workspace --locked --all-features --no-fail-fast, on23c4c7a: 5469 passed, 12 failed, 20 ignored. Nine are the sandbox and file-permission tests that fail the same way onmainin that container. Three inmcp_alias_fallback.rsfailed withText file busywhile 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
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.