Skip to content

fix: keep captured output that contains invalid UTF-8 - #231

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/capture-invalid-utf8
Nov 12, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/capture-invalid-utf8

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When the shell captures output for command substitution or returns it to the caller, one invalid UTF-8 byte made that command’s whole output disappear while leaving its status successful. For example, a file containing ok\377bad\n produced an empty substitution and lost the valid ok and bad text too.

Captured text now keeps its valid parts and replaces invalid bytes with U+FFFD; pipes and files continue carrying raw bytes. The capture reader still limits raw bytes, while the later stdout check can truncate and report an error when replacement characters expand the text past that limit; stderr has no corresponding check after conversion. Silent drops for raw-byte overflow and Lua’s existing stdin-decoding behavior remain follow-ups. This pairs with #215, which lets printf emit raw bytes.

I checked substitution, pipelines, captured stdout and stderr, raw pipes and files, and the output-limit boundaries; the added replacement-expansion test fails with the old capture reads and passes with this change. The full Rust suite, doc tests, warning-denying docs build, formatting, clippy, and seven CLI probes passed locally, with existing clippy warnings; Python and Node are covered by CI and their local suites were not run.

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/capture-invalid-utf8 branch from 33c681d to d3b706c Compare November 12, 2026 22:28
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 22:28
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 22:28
@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 works: $(...) and run() output with invalid UTF-8 is kept, with U+FFFD where the bad bytes were, and pipes and files still carry the raw bytes. @ferdingler, one 🟡 below: the max_output test passes on main too, so the new behaviour at the limit has no test.

  • 🟡 invalid_utf8_capture_still_enforces_max_output doesn't tell main and this PR apart (suggestion inline).
  • Question (non-blocking): src/builtins/lua.rs:94 is the last read_to_string_limited(..).unwrap_or_default(). cat /g/bin | lua -e 'io.write(io.read("a"))' silently loses all of its stdin. Do you want to swap it here as well, or leave it for a follow-up?
  • I found an unrelated bug while probing and filed it as #255: read decodes input as Latin-1, so café comes back as café.
✅ What I verified
  • Head d3b706c, base 5a0fa2b. cargo test --workspace --all-targets passed 2204 tests with 0 failures, and cargo fmt --check is clean. Running clippy locally with Rust 1.100 fails, but only on lints in files this PR doesn't touch (find.rs, lua.rs, ls.rs). The stable Rust/Python jobs in CI are green.
  • Main vs PR: I ran the new tests against main's src/. subst_* and run_output_* fail there, and invalid_utf8_capture_still_enforces_max_output passes.
  • A file containing ok\xffbad\n with cat /g/bin gives: max=6 → "", rc 0. max=7 → "ok\u{FFFD}ba" plus a limit error, rc 1. max=8 → "ok\u{FFFD}bad", rc 1. max=9 → the full output, rc 0. On main, every one of these returns "" with rc 0.
  • 8191 × a followed by é, run through $(cat | cat), comes back intact with no false U+FFFD at the 8 KiB read boundary. The bytes are only decoded after the whole stream has been read (src/os.rs:819).
  • Probe log: s3://…/strands-agents/shell/pr/231/pr231-limit-probe.txt. Test log: pr231-test.log.
Appendix: non-blocking (2)
  • PR body: the limit claim is right, but the reason is different. read_to_end_limited (src/os.rs:854) counts raw bytes, so U+FFFD never trips it. The error comes from the check on the converted string in execute_pipeline (src/exec.rs:3037). That check covers stdout only: cat /g/bin >&2 at max=7 returns all 9 bytes of stderr with rc 0. One more oddity: an overflow that is over the limit in raw bytes is still dropped silently (max=6 gives ""), while a smaller one is reported (max=7). The silent drop already happens on main, through the same unwrap_or_default, so it's a follow-up and not part of this PR.
  • The adversarial pass I spawned timed out. I ran its chunk-boundary and limit-edge probes myself (results above).

Comment thread tests/shell_integration.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

I reproduced the Lua case: invalid UTF-8 makes its stdin pre-read empty, while the ASCII control works. That input conversion is unchanged from main and happens before shell output capture, so I’m leaving it for a separate Lua fix; replacing it here would also change the bytes that Lua code receives.

I corrected the PR description to distinguish the raw-byte cap from the later stdout check. Replacement expansion can trigger that stdout check, while stderr has no equivalent check after conversion; raw-byte overflow still drops silently. Those existing limit behaviors stay outside this PR. Thanks for filing the separate read decoding issue as #255.

The requested limit regression is now in d727544, with a failing run against the old capture reads and a passing run with the fix. The full local Rust checks and seven CLI probes passed; CI is running again for the new commit.

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

Still approve. The 🟡 is fixed. With main's capture reads, the new max=7 case in invalid_utf8_capture_still_enforces_max_output fails (left: "", right: "ok\u{FFFD}ba"), and with this PR it passes. @ferdingler, nothing else needed from me.

  • Leaving Lua stdin for a follow-up makes sense to me, since changing it would also change the bytes Lua code receives.
  • The corrected PR description now matches the code.
✅ Verified at d727544
  • The only change since my last review is +7 lines in tests/shell_integration.rs.
  • cargo test --workspace --all-targets: 2204 passed, 0 failed. cargo fmt --check is clean.
  • I swapped main's src/exec.rs and src/os.rs into the PR checkout, ran the new assertion and saw it fail, then restored the PR's versions.

@ferdingler
ferdingler merged commit d2cac2a into strands-agents:main Nov 12, 2026
32 checks passed
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