Skip to content

fix: keep a subshell's stderr when its output is captured - #233

Merged
ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:fix/subshell-captured-stderr
Nov 12, 2026
Merged

ferdingler merged 1 commit into
strands-agents:mainfrom
ferdingler:fix/subshell-captured-stderr

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

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.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

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
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

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

@ferdingler
ferdingler force-pushed the fix/subshell-captured-stderr branch from 3bdf70f to 757c123 Compare November 12, 2026 23:17
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 23:17
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 23:17
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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/main exec.rs plus the PR's tests), 4 of the 5 new tests fail. subshell_stderr_plain passes 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/null keeps 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, if and for bodies, | cat inside the substitution, and & wait each 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 ) and x=$( { cmd; } <file ) still drop stderr. The stdin-redirect branch of CompoundRedirect in run_capturing_status (src/exec.rs:2053-2066) forks sub and never forwards sub.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/null print 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 ) writes o into x instead 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.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

I reproduced the sibling path through MCP: adding </dev/null makes both the subshell and brace-group substitutions lose stderr, and combining it with stdout redirection puts o in the substitution instead of the file. The stdout-only control still writes the file correctly.

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 2>/dev/null. I agree this needs a separate fix under #207 that handles the redirects together; I’m keeping the approved subshell change focused here. The CLI substitution limitation is also still documented.

@ferdingler
ferdingler merged commit d21a4e3 into strands-agents:main Nov 12, 2026
33 of 62 checks passed
@ferdingler
ferdingler deleted the fix/subshell-captured-stderr branch November 12, 2026 23:51
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.

2 participants