Skip to content

fix: report command substitution status from assignment-only commands - #232

Merged
ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix/assignment-substitution-status
Nov 13, 2026
Merged

ferdingler merged 6 commits into
strands-agents:mainfrom
ferdingler:fix/assignment-substitution-status

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Assignment-only commands always reported success, even when a command substitution failed. That meant an error handler after an assignment could be skipped despite the command failing.

I made assignment-only commands and lone substitutions return the last substitution's status, including substitutions inside arithmetic expressions. This exposes an existing strict-mode issue in functions and groups used as conditions, tracked separately in #259; redirects on assignment-only commands also remain outside this fix.

The full Rust checks and 48 Bash comparisons pass, including the merged function-exit and pipefail behavior. I added arithmetic coverage and deliberately disabled its status propagation to confirm the test fails, then restored it and confirmed it passes.

Related Issues

Part of #207

Documentation PR

N/A

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/assignment-substitution-status branch from 8696777 to bb7bd6f Compare November 12, 2026 23:15
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 23:15
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 23:15
@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 matches bash on every substitution path we tried, with no regressions from the change itself. One heads-up: it makes an existing set -e bug easier to hit. I filed that as #259; it shouldn't block this PR.

The heads-up: errexit isn't suppressed inside function, group, eval or source bodies run from a tested context (f || handler). Assignments used to always return 0, which hid this. With the fix, scripts like these behave differently from main:

set -e; f() { x=$(false); echo in-f; }; f || echo caught   # bash/main: in-f   | PR: caught
set -e; { x=$(false); echo in-g; } || echo caught         # bash/main: in-g   | PR: no output, rc=1

You get the same divergence on main with a plain false, so the root cause is older than this PR. Worth a note in the PR description, or landing #259 soon after.

What I verified
  • Head bb7bd6f: cargo test --workspace --all-targets --no-fail-fast gave 2394 passed, 0 failed, including 1810 in shell_integration and the differential suite.
  • An adversarial pass ran 334 scripts through bash, main and this PR.
    • 124 went from wrong to matching bash.
    • None that matched bash on main stopped matching because of this change. The only new mismatches are the errexit interaction above and wait, which already returns the last job's status on main.
  • Both capture_output callers report the status: expand_part ($(), backticks, ${v:-$(…)}) and expand_arith_expr ($(( ))).
  • Other assignment forms also match bash: x+=, a[0]=, eval 'x=$(exit 3)', { x=$(exit 3); }, and an EXIT trap reading $?. Bonus: while x=$(exit 3); do no longer loops forever.
  • No stale status can leak between commands. The field is only read at src/exec.rs:3213, always after the reset at :3160 in the same run_pipeline call, and fork() starts it at None.
Non-blocking (4)
  • x=$(fi) returns 1 with no error message, because capture_output drops the parse error (src/exec.rs:1681). Bash rejects the whole script with rc 2. The assign_subst_parse_error_status test pins the 1, which matches how this shell already handles top-level syntax errors. That's fine, just be aware the test locks it in.
  • echo | x=$(false); echo $?: bash prints 1, this PR prints 0. An assignment-only command as the last stage of a pipeline doesn't take the new path. The PR doesn't claim to cover this.
  • $? isn't updated during expansion: in bash, x=$(exit 3) y=$? sets y=3 and echo $(exit 3) $? prints 3. Here both give 0, before and after this PR. Is that worth tracking under #207?
  • Also unchanged by this PR: $(exit 300) returns 300 because statuses are never cut to 8 bits, and stderr from inside $( ) is dropped in -c mode, so set -e; x=$(nonexistent) exits 127 without printing an error.

Comment thread tests/shell_integration.rs Resolved
@ferdingler
ferdingler force-pushed the fix/assignment-substitution-status branch from bb7bd6f to dd7df1a Compare November 12, 2026 23:48
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

Added the arithmetic-substitution regression and proved it catches missing status propagation at that caller. I also rebased over the newly merged #222, preserving its exit/refusal state alongside substitution status, and added the #259 strict-mode follow-up to the description. No new production behavior beyond that conflict resolution.

The full Rust checks and 48 Bash comparisons pass, including arithmetic, function exits, and pipefail together. Please re-review dd7df1a.

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

Still approve. The earlier finding is fixed, and there's one small cleanup.

  • ✅ Arithmetic coverage (fixed): assign_arith_subst_status is in. I disabled status propagation at the $(( )) caller (src/exec.rs:780) and the test fails. Restored, it passes.
  • ✅ Rebase over #222: the source diff is unchanged apart from placing the new field next to pending_exit/refused_array in src/os.rs. I don't see any interaction, since substitutions run in a forked Process and that fork resets all three fields.
  • 🟡 New dead_code warning: run_capturing has no callers left, so every cargo build warns about it (inline). I missed this on the first pass, sorry.
What I verified
  • dd7df1a merged with current main (merges cleanly, since main is now 3 commits ahead): cargo test --workspace --all-targets --no-fail-fast gives 2473 passed, 0 failed.
  • cargo fmt --check passes.
  • cargo clippy: 15 warnings, against 14 on main. The extra one is the run_capturing warning.
  • #259 is now noted in the description.

Comment thread src/exec.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

Removed the unused capture wrapper and its obsolete documentation link in db996fc. The full Rust checks and 48 Bash comparisons pass, and clippy no longer reports that dead-code warning. Both the current-main and pending #235 merge trees are clean and contain no remaining caller. Please re-review this cleanup.

@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. db996fc fixes the dead-code finding, and nothing is left open from my reviews.

  • ✅ run_capturing is gone, and the run_capturing_status doc no longer links to it.
  • ✅ No callers are left, including in main's newer captured-output fixes (#231, #233). db996fc merges cleanly with current main.
What I verified

All on db996fc merged with origin/main @ d21a4e3:

  • cargo test --workspace --all-targets --no-fail-fast: 2482 passed, 0 failed
  • cargo fmt --check: clean
  • cargo clippy --workspace --all-targets: 14 warnings, the same as main, with no dead-code warning

@ferdingler
ferdingler merged commit c07d339 into strands-agents:main Nov 13, 2026
31 checks passed
@ferdingler
ferdingler deleted the fix/assignment-substitution-status branch November 13, 2026 00:48
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