fix(tools): surface non-UTF-8 files as ToolError in read/edit#1745
Open
sean-kim05 wants to merge 1 commit into
Open
fix(tools): surface non-UTF-8 files as ToolError in read/edit#1745sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
The agent `read` and `edit` tools decode files with `Path.read_text()` (UTF-8) inside a `try` that only catches `ToolError` and `OSError`. `UnicodeDecodeError` is a subclass of `ValueError`, not `OSError`, so a non-UTF-8 file (a binary artifact, or an ordinary latin-1 / UTF-16 text file) escapes the tools' error contract: instead of the intended clean `"<op>: <path>: ..."` `ToolError`, the runner logs a full traceback and hands the model a raw `UnicodeDecodeError` repr. The sibling `grep`/`bash` tools in the same module already decode with `errors="replace"`, so non-UTF-8 files are a known, expected input here. Catch `UnicodeDecodeError` in both tools and raise a clean `ToolError`. Editing deliberately does not fall back to a lossy `errors="replace"` decode, since the replacement round-trip would corrupt the file on write-back.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The agent
readandedittools (src/anthropic/lib/tools/agent_toolset.py) decode files withPath.read_text()(UTF-8) inside atrythat only catchesToolErrorandOSError:UnicodeDecodeErroris a subclass ofValueError, notOSError, so it escapes uncaught. The moment the model callsread/editon a file that isn't valid UTF-8 — a binary artifact, or an ordinary latin-1 / UTF-16 text file — the exception bypasses the tools' error-normalization: instead of the intended clean"read: <path>: ..."ToolError, the messages runner logs a full traceback (log.exception) and hands the model a rawUnicodeDecodeErrorrepr.The sibling
grepandbashtools in the same module already decode witherrors="replace", so non-UTF-8 files are a known, expected input here —read/editjust don't handle them.Reproduction
Fix
Catch
UnicodeDecodeErrorin both tools and raise a cleanToolError. Theeditpath deliberately does not fall back to a lossyerrors="replace"decode, since the replacement round-trip would corrupt the file on write-back.Testing
Added
test_read_rejects_non_utf8_fileandtest_edit_rejects_non_utf8_fileintests/lib/tools/test_agent_toolset.py(a latin-1 file must raiseToolErrormatching "not valid UTF-8"). Verified RED (rawUnicodeDecodeError) → GREEN. Fulltest_agent_toolset.pypasses (42 tests);./scripts/lintclean (ruff + pyright + mypy).