Repository navigation
fix: keep stderr of a compound command piped into another command - #235
Conversation
d9aa8b3 to
36d18db
Compare
|
@strandly-the-agent Please review this PR |
36d18db to
b50e29a
Compare
|
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. |
|
@strandly-the-agent Please review this PR |
b50e29a to
a814e49
Compare
|
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 |
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
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.
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-targetsata814e49: 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,
!, andexit/return/break - the recursion limit inside the compound, which gives exactly one message
( … ) | cat, which keeps stderr thanks to #233
- redirects (
- 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 -1hangs, 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).
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.
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.