Skip to content

fix(parser): treat a digit as a redirect fd only when it touches the operator - #230

Merged
ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/number-before-redirect
Nov 12, 2026
Merged

ferdingler merged 3 commits into
strands-agents:mainfrom
ferdingler:fix/number-before-redirect

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A number separated from a redirect was being treated as its file descriptor, so echo 3 > o wrote an empty file and echo a 2 > q lost the 2. This silently changed ordinary command arguments.

I changed parsing so an unquoted digit only supplies the descriptor when it touches the redirect operator. Digits after input or output duplication operators stay duplication targets, so adjacent redirects such as 2>&1>f continue to work. Multi-digit descriptors and the existing VFS reader-duplication limitation remain outside this parser change.

I checked 32 commands against Bash for identical output and status, plus five invalid forms that both shells reject. I broke the duplication guard on purpose and the regression tests caught it. The full Rust suite, formatting, lint and documentation checks pass locally.

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/number-before-redirect branch from f371772 to 0a2f8f9 Compare November 12, 2026 22:15
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 22:15
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 22: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.

Changes requested, one line to fix. The IO_NUMBER fix is right, and every case you claimed matches bash. But it also turns the target of >&/<& into an fd when that target sits directly before another redirect, so echo hi 2>&1>f now fails with expected filename after '>&'. It works on main and in bash. Suggested fix and two test cases are inline.

  • Suite at 0a2f8f9: cargo test --workspace --all-targets → 2201 passed, 0 failed.
  • I compared 151 redirect forms across PR, main and bash 5.2. The >&N<op> family is the only regression.
  • With the one-line fix applied, shell_integration passes 1625/1625. The two new cases fail without the fix.
What I verified
command PR main bash PR + fix
echo 3 > o; cat o 3 (empty) 3 3
echo a 2 > q; cat q a 2 a a 2 a 2
echo hi 2>&1>f; cat f parse error hi hi hi
echo x >&2>>f; cat f parse error x x x
{ echo a; } 2>&1>f; cat f parse error a a a
echo x >f 2>&1<f; cat f parse error x x x
echo x 2>&1, echo hi 1>&2, echo a 9>&- ok ok ok ok
  • Also newly correct: echo a 1 >f, echo \2>f, a line continuation before >, and echo 3 >o &.
  • Now an error where main silently accepted it: echo hi >2>f, >>2>f, >|2>f, <2>f, cat <<2>f. Bash rejects all of these as syntax errors too, so this is fine. The fix leaves them unchanged.
  • Checked and unaffected: case, for … in, (( )), $(( )), heredocs, compound and prefix redirects, exec 3>f, eval, command substitution. tok_name's slice can't panic, because fd is always 0–9.
  • Not run: clippy and fmt, since they aren't installed in my sandbox. The fix is a single condition line.

Reading order: the tokenizer change at src/parser.rs:1000-1009, then the two fd-prefix consumers at :1144 and :1771, then the test table.

Appendix: non-blocking, not filed (2)
  • Pre-existing, identical on main: x=2>f doesn't create f, and f(){ …; } 2>e drops the redirect. Neither is touched by this PR.
  • Syntax errors exit 1, while bash exits 2. That's the same on main for every syntax error.
Evidence (artifacts)

Uploaded to strands-agents/shell/pr/230/: cargo-test-pr-head.log, cargo-test-with-fix.log, suggested-fix.diff, spotchecks.txt, adversarial-harness.sh, adversarial-results.txt.

Comment thread src/parser.rs Resolved
@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 2>&1>f regression is fixed in 8b48bca, and I found nothing new.

  • ✅ Fixed: 2>&1>f, >&2>>f, { …; } 2>&1>f and >f 2>&1<f give the same output as main and bash again. The spaced variants (2>& 1>f, >& 2>f) also match bash.
  • Suite at 8b48bca: cargo test --workspace --all-targets → 2204 passed, 0 failed.
  • My 151-case harness (PR vs main vs bash 5.2) now has 0 regressions. The case that regressed is back to OK, and the other buckets are unchanged.
Details

The one remaining PR-vs-bash difference I hit is a bare redirect line like 2>f on its own, which doesn't create the file. Main does the same, so it isn't this PR. Your new parser test, which checks that the adjacent and spaced forms produce the same syntax tree, is a good way to pin this down.

Artifacts in strands-agents/shell/pr/230/: cargo-test-8b48bca.log, spotchecks-8b48bca.txt, adversarial-results-8b48bca.txt.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for rechecking the adjacent and spaced cases. I am leaving the bare redirect-line behavior for a separate fix because it also happens on main and does not come from this change.

@ferdingler
ferdingler merged commit d3545c4 into strands-agents:main Nov 12, 2026
32 checks passed
@ferdingler
ferdingler deleted the fix/number-before-redirect branch November 12, 2026 23:49
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