Repository navigation
fix: keep a subshell's stderr when its output is captured - #233
Conversation
3bdf70f to
757c123
Compare
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The fix is correct and mirrors what execute_item already does for subshells (src/exec.rs:2213-2216). It now forwards sub.captured_stderr on both branches, which includes the depth-limit message. I found no blocking issues.
One sibling path still drops stderr. It's pre-existing and not a blocker; details below.
✅ What I verified
- Head
757c123,cargo test --release --test shell_integration: 1781 passed. - On base (
origin/mainexec.rs plus the PR's tests), 4 of the 5 new tests fail.subshell_stderr_plainpasses on base, which is fine because it guards the path that already worked. - I ran about 20 probes through
Shell::run, comparing base and PR and checking against Bash. These behave correctly:$( (…) 2>/dev/null )is silent,$( (…) 2>&1 )merges into stdout,2>&1 >/dev/nullkeeps only stderr, and(( … ) 2>/dev/null)nested stays silent. Order is preserved in$( echo a >&2; (echo b >&2); echo c >&2 ), which prints a b c. Function,ifandforbodies,| catinside the substitution, and& waiteach show stderr exactly once. - The CLI behaves the same as on base.
strands-shell -c 'x=$( (echo e>&2) )'still drops stderr, as the PR description says, and nothing extra leaks out later. - CI shows green re-runs. The cancelled runs were superseded.
- Probe log:
s3://…/strands-agents/shell/pr/233/probes.txt
Appendix: non-blocking (1)
- Pre-existing:
x=$( (cmd) <file )andx=$( { cmd; } <file )still drop stderr. The stdin-redirect branch ofCompoundRedirectinrun_capturing_status(src/exec.rs:2053-2066) forkssuband never forwardssub.captured_stderr. #235 doesn't touch this branch. The obvious one-line fix is not enough on its own. I tried it, and it makes(…) </dev/null 2>/dev/nullprint stderr, because that branch skips the stdout/stderr redirects entirely whenever a stdin redirect is present. On base,x=$( (echo o) </dev/null >/tmp/f )writesointoxinstead of/tmp/f. The fix needs to apply both kinds of redirect, so it's better as its own follow-up under #207 than folded into this PR.
|
I reproduced the sibling path through MCP: adding That stdin branch is unchanged from the base. It applies only the input redirects and skips the output redirects, so forwarding its captured stderr alone would bypass a requested |
Description
When a subshell runs with its output captured, its stderr is thrown away. An agent using the library or MCP can therefore see a command finish without the error that explains what went wrong.
The captured subshell now passes its stderr back to the shell that ran it, including through nested subshells and stdout redirection. The group-redirection fix in #206 is already merged, so stderr suppression and merging work with this change too. CLI command substitution still drops stderr even without a subshell; that remains a separate fix. Captured compound commands with stdin redirects also retain an existing bug that drops stderr and ignores output redirects; fixing that path requires handling the redirects together.
I checked captured substitutions, nested subshells and redirected streams through seven MCP probes, and compared six scoped CLI cases against Bash. Those checks and the full local Rust suite, doc tests, formatting, clippy and docs checks pass; the separate CLI substitution limitation remains reproducible.
Related Issues
Part of #207
Documentation PR
None.
Type of Change
Bug fix
Testing
The full local Rust suite and doc tests pass, and formatting, clippy and docs checks pass. Seven MCP probes and six scoped CLI comparisons against Bash pass. I didn't run the Python or Node suites locally since neither binding changed.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.