Skip to content

fix: keep output truncation on a UTF-8 character boundary - #176

Open
ferdingler wants to merge 1 commit into
strands-agents:mainfrom
ferdingler:fix/utf8-safe-truncation
Open

ferdingler wants to merge 1 commit into
strands-agents:mainfrom
ferdingler:fix/utf8-safe-truncation

Conversation

@ferdingler

@ferdingler ferdingler commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

When output hits the max_output limit, the shell cuts it at a byte count. If that byte lands in the middle of a multibyte character, the shell panics instead of truncating. Something as small as X=$(printf abc; printf é) with a 4-byte limit crashes it, both in command substitution and in captured top-level output, and the CLI hits the same crash.

Now the cut moves back to the start of the character it would have split, so the output stays valid UTF-8 and never goes over the limit. The limit itself, what counts toward it, and the "limit exceeded" message and exit status are unchanged. Box's copy of Shell already cuts top-level output safely (here), but command substitution has the same crash there (here), so this fixes it there too once Box moves to upstream.

I added tests for a multibyte character crossing the limit in both places, plus ASCII and exact-limit cases. Without the fix they panic; with it they pass, and the rest of the suite still passes. I also ran the original repro through the CLI with a config limit, and it now prints the truncated value instead of crashing. This touches the same file as #169 and #173 but in different spots.

Related Issues

None.

Documentation PR

None.

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 requested a review from a team as a code owner October 21, 2026 05:04
@ferdingler
ferdingler requested review from opieter-aws and removed request for a team October 21, 2026 05:04

This branch has not been deployed

No deployments
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.

1 participant