Skip to content

feat(set): support set -o NAME and pipefail - #217

Merged
ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:feat/set-o-pipefail
Nov 12, 2026
Merged

ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:feat/set-o-pipefail

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Agent scripts often start with set -euo pipefail, but Shell rejected -o after partially enabling the earlier flags. That stopped the script before its first useful command.

set -o NAME and set +o NAME now support errexit, nounset, xtrace, and pipefail, including clustered flags and option listings. Pipefail returns the rightmost failure, including compound commands, return, and deferred host-write errors; with pipefail off, an empty or assignment-only final stage now correctly returns success. Options are applied in order, like Bash, so a later invalid option keeps earlier changes and an enabled -e stops the script.

The full Rust suite passes, and Bash comparisons cover invalid options, ordinary and compound pipelines, subshells, negation, conditions, and host-write failures. The new invalid-option and compound-return tests fail without their fixes. Options retain their existing session lifetime, assignment-only command substitution still needs #232, and positional arguments after options (set -o pipefail -- a b) remain a separate gap.

Related Issues

Part of #207

Documentation PR

Type of Change

New feature

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 marked this pull request as ready for review November 12, 2026 20:12
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 20:13
@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.

@ferdingler Approve, with two should-fixes. set -euo pipefail works now, and pipefail matches bash 5.2 on every pipeline shape I tried (false | true, 3 stages, ( exit 3 ) | …, { …; } | cat, !, if, $(…), subshell non-leak, host-write failures). With pipefail off, results match main except for one improvement (noted below). CI is green, and 1650/1650 shell_integration tests pass locally at 330e382.

  • 🟡 A bad option makes set fail open: set -euo pipefial keeps running with no flags set, where bash and main both exit 2 (set.rs:70).
  • 🟡 Under pipefail, return N on the left of a pipe is lost: f() { { return 3; } | cat; }; f returns 0 instead of 3 (exec.rs:1996 → captured_status, :1793).

Each fix is about one line, and I checked both against bash and the test suite (details below).

✅ What I verified, Questions, Appendix

Verified (head 330e382, compared against main d4c57a7 and /usr/bin/bash 5.2)

  • cargo test --test shell_integration: 1650 passed, 0 failed. CI rollup: SUCCESS.
  • Pipefail on, bash = PR: false|true→1, true|false|true→1, ( exit 3 )|true→3, { false; }|cat→1, ! false|true→0, if false|true→else branch, "$(false|true; echo $?)"→1, (set -o pipefail); false|true→0, set +o pipefail inside a subshell doesn't leak.
  • Pipefail off, main = PR = bash: false|true→0, { false; }|cat→0, true|false→1.
  • Host writes (330e382): echo hi > RO | true gives 0 with pipefail off (same as main and bash) and 1 with it on (same as bash). true | echo hi > RO gives 1 in both modes. set -eo pipefail; echo hi > RO | true; echo reached exits 1, like bash. The new settle_host_writes call at exec.rs:3802 doesn't settle twice: HostWrites::settle takes the closed writes out of the list (os.rs:221-227), so the caller's second settle finds nothing.
  • set -o / set +o output for the four supported options matches bash's lines, and set +o output can be passed to eval.
  • Both suggested fixes: applied one at a time, rebuilt, re-ran the repro and the suite (see the inline comments).
  • Adversarial pass: the first run timed out at 900s. A re-run finished from its saved battery output (~300 cases compared across bash, main and the PR).

Questions (non-blocking)

  1. Pipefail now stays on across Shell.run calls, as set -e already does (the bind test at tests/shell_integration.rs:6843-6857 depends on that). #207's P2 list calls this exact behaviour a gotcha. Is that intended, with a reset planned in a separate change?

Reading order: src/builtins/set.rs → run_pipeline status assembly (exec.rs:3770-3815) → the two compound-left paths (exec.rs:1990-2015, 2240-2270) → tests.

Appendix: non-blocking (5)

  • ⚪ Improvement worth a line in the PR body: with pipefail off, an empty or assignment-only last stage now returns 0 (false | FOO=1; echo $?: main 1, PR 0, bash 0). stage_exits starts each stage at 0, whereas main carried the previous stage's status forward.
  • ⚪ Pre-existing, already covered by open #232: x=$(false); echo $? prints 0 on main. So set -euo pipefail; x=$(a | b) won't abort until #232 lands. Worth adding a pipefail case to that PR.
  • ⚪ Documented follow-up: set -o pipefail -- a b → --: unsupported option (bash sets $#=2). Main has the same gap for set -e -- a b.
  • ⚪ Under pipefail, a writer cut off by head exits with status 1 (e.g. sort big | head -1 → 1 plus sort: pipe closed), where bash gives 141. The pipeline fails in both shells, so only the code differs.
  • ⚪ Test gaps: there's no test that (set -o pipefail) doesn't leak, or for ( exit 3 ) | cat under pipefail. I checked both by hand and they're correct.

Evidence uploaded under strands-agents/shell/pr/217/: repro-pr217.sh / .out, shell_integration-test.log, fix-verification.txt.

Comment thread src/builtins/set.rs Resolved
Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

5383e37 fixes the compound-return status and adds the missing regression coverage. The separate atomic-versus-sequential set-option choice is still pending, and its thread remains open.

@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.

@ferdingler Still approve at 5383e37. Status of my earlier findings:

  • ✅ Fixed: return N on the left of a pipe under pipefail (exec.rs:1793). Both repros now return 3, like bash. I also tested the fix's other caller, command substitution (exec.rs:1864), and found no regressions.
  • ⏸️ Open, waiting on an owner decision: whether set applies options all at once or one by one (set.rs:70). My view hasn't changed, and I won't re-raise it. It doesn't block the PR.

Locally, shell_integration passes 1655/1655 tests, including the 5 new regression tests. CI was still queued when I posted.

Fix verification (bash 5.2 vs 330e382 vs 5383e37)
Script bash before after
set -o pipefail; f() { { return 3; } | cat; }; f; echo $? 3 0 3
set -o pipefail; f() { echo $({ return 3; } | cat; echo $?); }; f 3 0 3
set -e -o pipefail; f() { { return 3; } | cat; echo unreachable; }; f exits unreachable exits
set -o pipefail; f() { { false; return; } | cat; }; f; echo $? 1 1 1
set -o pipefail; f() { { return 0; } | false; }; f; echo $? 1 1 1
f() { { return 3; } | cat; }; f with pipefail off 0 0 0
$(…) path: f() { v=$(echo a; return 4); echo "v=$v"; } v=a v=a v=a
$(…) path: f() { echo "[$(return 3; echo no)]"; } [] [] []
return in a loop body piped to cat loop 0 ×2 same same

One difference, not worth acting on: a return outside any function ({ return 3; } \| cat at top level) now gives 3. Bash gives 2 plus an error, and before this commit the PR gave 0. That input isn't plausible. Separately, f() { $(return 3); echo $?; } prints 0 where bash prints 3. That was the same before this commit, so it's pre-existing and not related to this fix.

Evidence uploaded under strands-agents/shell/pr/217/: followup-repro.sh, followup-repro.out, followup-test.log.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

Both should-fix findings are addressed in 6f5e258: compound return status is preserved, and set applies options in order like Bash. The new regressions were checked failing before and passing after the fixes. Full Rust validation and ten Bash probes pass. Options still persist across Shell calls; session reset remains separate work.

@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.

@ferdingler Approve at 6f5e258. Both should-fixes are now addressed:

  • ✅ set applies options in order (set.rs:68). The change is the same patch I tested earlier. set -euo pipefial exits 2 with nothing printed, the same as bash. This holds inside a function too.
  • ✅ return N on the left of a pipe (5383e37): still returns 3.

Locally, shell_integration passes 1657/1657 tests. CI was still queued when I posted. The updated PR body is accurate.

Probes (bash 5.2 vs 5383e37 vs 6f5e258) and one non-blocking note

These all match bash at 6f5e258:

  • set -euo pipefial; echo unreachable exits 2 with no output. Before this commit it printed unreachable.
  • set -eo bogus exits 2.
  • set -o pipefail +o bogus; false|true gives 1, and set +o pipefail -o bogus (with pipefail on) gives 0.
  • set -ux -o bogus leaves u and x on.
  • set -e; set -o bogus exits 2.
  • set -o nounset bogus sets $1=bogus.
  • set -e -o applies -e, then prints the option list.
  • f() { set -eo bogus; echo in-f; }; f exits 2.

⚪ One place this goes further than bash, not blocking: bash rejects an unknown single-letter option before it applies any of the cluster. Only -o NAME options are applied one at a time. So in bash, set -eZ leaves -e off and the script keeps going, while here it now turns -e on and exits 2. The same goes for set -uZ, which leaves u on here. This only matters for a letter bash itself rejects, and it errs on the strict side, so I'd leave it alone. The catch is a letter that bash accepts but Shell doesn't, like set -euE: bash runs it fine, but here it now exits 2.

Evidence uploaded under strands-agents/shell/pr/217/: followup2-repro.sh, followup2-repro.out, followup2-test.log.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Both fixes are now in. The unknown-letter cluster behavior also matches current main: its parser applies e, u, and x before reaching an unsupported letter. I’ll leave cluster prevalidation and support for additional flags as separate compatibility work.

@ferdingler
ferdingler merged commit e3b99d4 into strands-agents:main Nov 12, 2026
32 checks passed
@ferdingler
ferdingler deleted the feat/set-o-pipefail branch November 12, 2026 23:02
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