fix(session): read text attachments as text for non-text/plain MIME types#34786
Open
adityachaudhary99 wants to merge 1 commit into
Open
fix(session): read text attachments as text for non-text/plain MIME types#34786adityachaudhary99 wants to merge 1 commit into
adityachaudhary99 wants to merge 1 commit into
Conversation
…ypes resolveUserPart only routed a file attachment through the readable-text (Read tool) path when its MIME was exactly "text/plain". An extension-less or unrecognized text file resolves to application/octet-stream, so it fell through to the branch that base64-encodes the raw bytes and prepends a synthetic Read tool-call frame — the model then received binary garbage (anomalyco#17301). This is the consumer side; the earlier anomalyco#26152 only fixed the `run --file` producer. Gate the text path on a shared isTextMime() check (text/*, json/xml/js, empty, and the octet-stream unknown fallback) so text files are read as UTF-8, sniffed images/PDFs still become proper attachments, and genuine binary fails closed. Closes anomalyco#17301 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Issue for this PR
Closes #17301
Type of change
What does this PR do?
Attaching a text file could send binary-looking garbage to the model instead of the file's contents.
resolveUserPart(session/prompt.ts) only routed an attachment through the readable-text path (the Read tool) when its MIME was exactlytext/plain. A file with no extension — or any unrecognized text file — resolves toapplication/octet-stream, so it skipped that path and hit the branch that base64-encodes the raw bytes and prepends a syntheticCalled the Read tool…frame. The model then received a data-URL of raw bytes, which is the garbled output in the issue.The earlier #26152 fixed the
run --fileproducer (real MIME lookup), but the consumer here still only recognized literaltext/plain, so the interactive/upload path stayed broken (and extension-less files arguably got worse, since they now resolve to octet-stream).The fix gates the text path on a shared
isTextMime()check (text/*, JSON/XML/JS, empty, and theapplication/octet-streamunknown fallback) instead of an exact string. The Read tool already content-sniffs, so real text reads cleanly, sniffed images/PDFs still become proper attachments, and genuine binary now fails closed with an honest message instead of a garbled blob. Image/PDF MIMEs are untouched.How did you verify your code works?
Added two cases to
test/session/prompt.test.ts: a text file attached asapplication/octet-stream(the exact repro — an extension-less file) and astext/markdownboth produce a clean synthetic text part containing the file's text, with zero forwarded binary parts. I couldn't run the full suite in my isolated worktree (the@opencode-ai/coreworkspace alias resolves to a stale checkout there), so I rely on CI for the suite.Screenshots / recordings
N/A — not a UI change.
Checklist