Skip to content

fix: keep stderr of a compound command piped into another command - #235

Merged
ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/compound-pipeline-stderr
Nov 13, 2026
Merged

ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/compound-pipeline-stderr

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Loops, conditionals, and command groups lost their error output when piped into another command. That hid diagnostics from the CLI, MCP, and library callers.

This change keeps error output while sending normal output down the pipe. It also reports a timeout once and preserves both pipefail and the rule that exiting a pipeline stage leaves the calling shell running.

I ran the full Rust checks and compared 26 CLI cases with Bash; output, error output, and status all matched. Tests also cover large diagnostics and capturing errors from command substitution through the library. The CLI's existing loss of error output inside command substitution remains separate.

Related Issues

Part of #207

Documentation PR

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • 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/compound-pipeline-stderr branch from d9aa8b3 to 36d18db Compare November 12, 2026 23:27
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 23:27
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 23:27
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@ferdingler

Copy link
Copy Markdown
Collaborator Author

I checked this against #222 as well. There is one overlap in the compound-pipeline path: when the second PR is rebased, it needs to keep the stage-exit reset from #222 alongside this PR's stderr forwarding.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Rebased over #222 and kept its pipeline-stage exit reset alongside the stderr forwarding here. A new regression covers a function exiting in the tail while the producer writes stderr: it fails if the reset is removed and passes with both changes. Full Rust checks and 26 Bash comparisons pass.

The later #223, #230, and #228 merges combine cleanly with this head. The overlap noted above is resolved.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@ferdingler
ferdingler force-pushed the fix/compound-pipeline-stderr branch from b50e29a to a814e49 Compare November 12, 2026 23:55
@ferdingler

Copy link
Copy Markdown
Collaborator Author

Rebased again after #233 merged. The conflict was in adjacent test insertions; both complete test blocks are retained, and production code merged cleanly. Full Rust checks and all 26 Bash comparisons pass on a814e49.

I also checked the combined capture changes from #231, #233, and this PR. Nine of ten extra MCP probes match Bash's replacement-text output. The remaining group 2>&1 ordering case behaves identically on main and this branch: the existing group-redirection path collects stdout and stderr separately, then emits stdout first. Four baseline/control cases confirm it is an existing limitation; fixing stream interleaving remains separate.

@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. Groups, loops and conditionals piped into a command now keep their stderr at a814e49. I have one non-blocking 🟡 below.

The #222 overlap is handled. proc.pending_exit = false still sits right after the tail runs (src/exec.rs:2323-2324). I checked that the reset matters: on a test merge where I left it out, f() { exit 5; }; { echo e >&2; } | f; echo a; echo b printed a and then quit. The new compound_pipe_tail_function_exit_preserves_stderr test catches that.

🟡 A timeout message is lost when the tail fails (src/exec.rs:2326-2331). The strip assumes "this process will report itself". The caller only re-reports after a zero exit (src/exec.rs:2224). With MCP timeout_ms: 700, { while true; do echo x >&2; done; } | grep nomatch and ! { …; } | cat both return exit 1 with no timeout line, and while true; do :; done | grep nomatch returns empty stderr. Main loses the line too, so this isn't a regression. The fix is to move the exit computation (close/pipefail/negation) above the strip and guard the strip with exit == 0. I tried that: every case printed exactly one message, and the suite still passed 2461/0.

@ferdingler

Suggested change, verification, and the review passes
let (compound_exit, mut stderr) = handle.await.unwrap_or_default();
proc.close(0);
if proc.opt_pipefail && exit == 0 {
    exit = compound_exit;
}
if *negated {
    exit = if exit == 0 { 1 } else { 0 };
}
// The compound's stderr is not piped; pass it on without dropping any,
// except a limit message the caller reports after a zero exit.
if exit == 0
    && let Some(msg) = proc.check_limits()
    && stderr.ends_with(&format!("{msg}\n"))
{

(...then the forwarding block, then Ok(exit).) A regression test could be the existing timeout test with | grep nomatch as the tail.

Verified

  • cargo test --workspace --all-targets at a814e49: 2461 passed, 0 failed. With the suggested change it is also 2461/0.
  • CLI cases matched bash with stdout and stderr compared separately:
    • redirects (2>/dev/null, 2>&1, 2>file) on the compound, on the tail, and on an enclosing group, function or subshell
    • nested compound pipelines, 2 and 3 levels deep
    • pipefail, !, and exit/return/break
    • the recursion limit inside the compound, which gives exactly one message
    • ( … ) | cat, which keeps stderr thanks to #233
  • Large stderr goes through tx.send().await, so it isn't dropped under backpressure.

Passes: one correctness reviewer and one adversarial tester, both independent. Both approved. The 🟡 came from the reviewer and I reproduced it over MCP. The adversarial pass, which used only the CLI, missed it.

Not filed. Pre-existing, or not caused by this PR:

  • With a CLI [limits] timeout, the timeout message prints twice. Both copies come from the parent, and main does the same.
  • while true; do echo o; done | head -1 hangs, because the compound is buffered until it finishes. Main does the same.
  • A single stage writing ≥300 KB to stderr spins. That happens without any pipeline at all.
  • stdout prints before stderr under 2>&1.

Test-gap nit: there's no compound_pipe_* test with a redirect on the enclosing group or function, such as f 2>&1 | tr or { { …; } | cat; } 2>/dev/null.

Evidence: s3://strandly-prod-artifacts-319426459652/strands-agents/shell/pr/235/ (pytest-head-a814e49.log, strip-fix.diff, mcp_probe.py, findings-reviewer.md, findings-adversarial.md, merged-test.log, probe.txt).

@ferdingler
ferdingler merged commit 119d352 into strands-agents:main Nov 13, 2026
30 checks passed
@ferdingler
ferdingler deleted the fix/compound-pipeline-stderr branch November 13, 2026 00:50
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