Skip to content

fix: binary attachments are pasted into the prompt as text - #198309

Merged
obviyus merged 3 commits into
openclaw:mainfrom
Yun-0000:fix/binary-attachment-not-utf16
Nov 13, 2026
Merged

obviyus merged 3 commits into
openclaw:mainfrom
Yun-0000:fix/binary-attachment-not-utf16

Conversation

@Yun-0000

@Yun-0000 Yun-0000 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Closes #198289

What Problem This Solves

Fixes: a SQLite database, ELF file, or PE executable sent as a document is classified as UTF-16 text, so decoded bytes are inlined into the model prompt.

User Impact

A file with a recognized binary signature stays binary. The prompt gets the unsupported-document marker instead of the decoded bytes, and the attachment remains available to tools. Ordinary text, including BOM-less UTF-16 and a malformed plain MIME, is unchanged.

Why This Change Was Made

Magic detection already returns a specific binary type such as application/x-sqlite3. Classification then guessed a charset from NUL bytes and replaced that type with text/plain. A binary type that came from the file bytes now stays binary. Bytes with no magic type still go through text recovery.

Evidence

Node v24.22.2, pnpm 12.15.0, Vitest 5.0.5. Package 2026.11.3. Base 3577724c727. Head 24de01d39a9.

The production entry is classifyAttachmentBytes. Document extraction uses it before inlining text, and file context skips extraction when the class is not text.

Red, before the guard:

  • pnpm test packages/media-core/src/attachment-classify.test.ts --maxWorkers=1 -t "keeps a SQLite database binary"
  • NUL pages, 18ms: { mime: "text/plain", class: "text", charset: "utf-16le" }.
  • ASCII schema page, 2ms: { mime: "text/plain", class: "text" }.
  • 2 failed, 14 skipped. Vitest duration 253ms. Command wall 4.53s.
  • The same classifier on a 16,384-byte SQLite file, declared application/octet-stream, returned text/plain with utf-16be. ELF and PE headers returned text/plain with utf-16le.

Green, head 24de01d39a9:

  • The classifier file: 16 passed. The new cases took 0ms and 1ms. Vitest duration 175ms. Command wall 1.78s.
  • packages/media-core/src/attachment-classify.test.ts and src/media/input-files.extract.test.ts: 62 passed. Vitest duration 6.71s. Shard wall 9.37s. The malformed plain; charset=windows-1252 recovery still decodes text.
  • The 16,384-byte SQLite file is application/x-sqlite3 / binary. ELF is application/x-elf / binary. PE is application/x-msdownload / binary. BOM-less UTF-16 notes.txt and unnamed UTF-8 stay text/plain.
  • pnpm exec oxfmt --check on the two files passed.
  • node scripts/run-oxlint.mjs on the two files passed.
  • git diff --check passed.
  • pnpm check:test-lines-ratchet --base upstream/main --no-merge-base: Test lines OK. Net test lines: +15; allowance: 400; allocation credit: 0.
  • pnpm check:line-cap-ratchet --base upstream/main --no-merge-base: Line-cap ratchet OK: 2 changed source files; no new violations or over-cap growth.

Not run: a live channel send, full pnpm test, and pnpm check. CI has not finished.

Release note: sending a SQLite database or another recognized binary no longer pastes decoded bytes into the prompt.

Maintainer proof

Built Gateway end to end (main vs this PR, plus controls): #198309 (comment)

A magic-detected binary such as SQLite stays binary. Text recovery remains for bytes with no recognized type.

Co-authored-by: fangyun1008 <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P1 High-priority user-facing bug, regression, or broken workflow. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 13, 2026
@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Preserve signature-identified binary attachments instead of reclassifying their bytes as text.

Example: Send a 20 KB SQLite database as an application/octet-stream attachment.

  • Before: The Gateway sends decoded binary garbage to the model, producing 13,230 characters of user text in the supplied reproduction.
  • After: The Gateway sends an unsupported-format marker, producing 3,416 characters of user text while preserving attachment access for tools.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) A minimal shared-owner repair with distinct regression cases, supplied Gateway proof, and no actionable findings.
Proof confidence 🦞 diamond lobster (5/6) Sufficient (live_output): The supplied collaborator trace exercises built Gateway chat.send in a secretless container with a real SQLite attachment and a recording model endpoint, showing removal of decoded binary contents while preserving tool access and text/image controls; the changed files remain identical at the reviewed head.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Sending a database or executable can inject decoded binary garbage into conversation context and cause repeated model refusals.
Reason: The narrow fix restores the documented attachment outcome, preserves legitimate text decoding, and has shared-owner participation and Gateway evidence.

Merge readiness

✅ Ready for maintainer review

This PR fixes a current attachment-classification bug with a focused shared-owner change; no actionable defects were found, and current-head CI is still running.

Priority: P1
Reviewed head: 09dc8e965d2da5e14f487fbdb9db777bcf3b79b0

Before merge

None.

Findings

None.

Agent review details

How this fits together

The shared media-core classifier turns attachment bytes and MIME hints into one classification consumed by attachment admission, text extraction, and model-context rendering.

flowchart TD
 A[Attachment bytes and MIME hints] --> B[Shared media-core classifier]
 B --> C[Attachment cache]
 C --> D[Document context owner]
 D --> E[Text extraction or unsupported marker]
 E --> F[Model request]
Loading

Technical review

Best possible solution:

Land the shared-classifier fix after current-head required checks pass.

Do we have a high-confidence way to reproduce the issue?

Current-main source establishes the failure path, and the supplied Gateway comparison demonstrates it with a real SQLite attachment; this read-only review ran no target code.

Is this the best way to solve the issue?

The fix preserves authoritative byte signatures in the existing classifier while retaining text recovery for unrecognized bytes and malformed MIME hints.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 01e1612c3992.

Provenance checked

  • Shared attachment classification and text recovery keeps the original intent (746a188: Consolidate classification so admission and extraction consume one result; later encoding repairs preserve legitimate inferred text.)
  • Classifier regression table keeps the original intent (2727a6a: Consolidate repetitive fixtures while retaining distinct behavior checks; the new rows reuse that table and distinguish two text-inference failure paths.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Pinned introduced change: The introduced production hunk checks byte-only MIME detection before text inference and preserves a matching binary classification; the test changes add NUL-heavy and ASCII-heavy SQLite cases to the existing classification table. (packages/media-core/src/attachment-classify.ts:180, 09dc8e965d2d)
  • Current main still contains the defect: Current main lets specific binary MIME classifications reach NUL-based UTF-16 inference and printable-text recovery without the proposed byte-signature guard. (packages/media-core/src/attachment-classify.ts:188, 01e1612c3992)
  • Production consumer: The document-context owner skips automatic extraction for non-text, non-PDF classifications and renders an unsupported-format outcome, so correcting the shared classification prevents binary prompt expansion. (src/media-understanding/file-context.ts:121, 09dc8e965d2d)
  • Gateway behavior proof: The collaborator's supplied trace exercises built Gateway chat.send with a real SQLite file and a recording OpenAI-compatible endpoint, comparing main with the fix and checking UTF-16, UTF-8, PNG, and no-signature controls: fix: binary attachments are pasted into the prompt as text #198309 (comment). (c66a81f1cf7a)
  • Proof remains applicable: The two changed files are identical between the proof's reviewed revision and the current head; the intervening merge does not alter this fix. (09dc8e965d2d)
  • Feature history and original intent: The merged consolidation established one classification owner shared by admission and extraction; the subsequent encoding repair preserved inferred text decoders. Both intentions remain intact. Local follow-history encountered an unavailable object; GitHub verified the consolidation and its predecessor path, src/media-understanding/attachment-text-sniff.ts. (packages/media-core/src/attachment-classify.ts:131, 746a188c901b)

PR surface

Source +6, Tests -1. Total +5 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 6 0 +6
Tests 1 3 4 -1
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 9 4 +5

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Root-cause cluster

Relationship: same_root_cause
Canonical: #198289
Summary: This PR and two alternatives address the same binary-to-text classification failure described by the canonical report.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • remove rating: 🐚 platinum hermit: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.

Label justifications:

  • P1: An official-release report establishes binary document uploads polluting conversation context and repeatedly blocking agent replies.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (2 earlier review cycles)
  • reviewed 2026-11-13T14:34:30.317Z sha 24de01d :: needs real behavior proof before merge. :: none
  • reviewed 2026-11-13T14:58:28.022Z sha c66a81f :: needs maintainer review before merge. :: none

Reviewed November 13, 2026, 10:28 AM ET / 15:28 UTC (Revision 3).

@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Maintainer proof for c66a81f. Fixes #198289; thanks @mulderp82 for the report and @Yun-0000 for the fix.

I folded the two SQLite cases into the existing byte-detected classifier table (net -1 test line; the PNG fixture is now its signature bytes). Both rows fail on main and pass here.

End to end: built Gateway in a secretless node:24.22.1 container, chat.send with real file attachments, stand-in OpenAI-compatible model that records what it receives. The SQLite database was made with the sqlite3 CLI (20 KB, declared application/octet-stream). Main 632f7ff vs this PR's files applied on the same main.

case            main                                        PR
sqlite          <file mime="text/plain"> + decoded garbage   <file mime="application/x-sqlite3"> [Unsupported document format ...]
                user text 13,230 chars                       user text 3,416 chars
                (gateway: MIME override x-sqlite3 -> text/plain)
no-magic .txt   text/plain (unchanged)                       text/plain (unchanged)
UTF-16LE no BOM text/plain, 40/40 lines decoded              same
UTF-16LE BOM    text/plain, 40/40 lines decoded              same
UTF-8           text/plain, 40/40 lines decoded              same
PNG             image/png media note                         same

The [media attached: ... (application/x-sqlite3)] note stays on both, so tools can still reach the file. The no-magic control (NUL-heavy bytes with no known signature, named blob.txt) behaves exactly as on main, so the new rule only fires when a byte signature agrees with the resolved MIME.

@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 13, 2026
@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added the clawsweeper:intake:bug Preliminary submission classification; full source review pending. label Nov 13, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 13, 2026
@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Maintainer override: the only red check is checks-ui-e2e (1/3), where ui/src/e2e/home-readonly.real-gateway.e2e.test.ts:45 hits its 120 s test timeout. This PR does not cause it.

@obviyus
obviyus merged commit afa871c into openclaw:main Nov 13, 2026
171 of 176 checks passed
@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Landed in afa871c. Thanks @Yun-0000, and @mulderp82 for the report.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 14, 2026
…198309)

fix(media): keep signature-detected binaries binary instead of guessing UTF-16

The attachment classifier guessed BOM-less UTF-16 text whenever enough of the first bytes were zero, without checking what the bytes actually were, so binaries such as SQLite databases were retyped as text/plain and up to 60,000 characters of decoded garbage were pasted into the user's message to the model. The classifier now re-sniffs the bytes and keeps the attachment binary when the MIME type detected from its magic bytes matches the resolved type. The BOM, file-extension and text-hint paths are unchanged, and files with no known signature behave as before. No new text.

Evidence: a built Gateway in a secretless node:24.22.1 container, sending files with chat.send to a stand-in model and recording what it received. With a real SQLite database made by the sqlite3 CLI, main logged a MIME override from application/x-sqlite3 to text/plain and the model received 13,230 characters of decoded garbage; with this change the file stays application/x-sqlite3 with the existing unsupported-format marker. A NUL-heavy file with no known signature named blob.txt, UTF-16LE text with and without a BOM, UTF-8 text and a PNG behave the same on both. Two SQLite rows in the existing byte-detected table fail on main, at -1 net test lines. Details: openclaw#198309 (comment).

Fixes openclaw#198289. Thanks @Yun-0000, and @mulderp82 for the report.

Co-authored-by: Ayaan Zaidi <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 14, 2026
fix(media): keep signature-detected binaries binary instead of guessing UTF-16

The attachment classifier guessed BOM-less UTF-16 text whenever enough of the first bytes were zero, without checking what the bytes actually were, so binaries such as SQLite databases were retyped as text/plain and up to 60,000 characters of decoded garbage were pasted into the user's message to the model. The classifier now re-sniffs the bytes and keeps the attachment binary when the MIME type detected from its magic bytes matches the resolved type. The BOM, file-extension and text-hint paths are unchanged, and files with no known signature behave as before. No new text.

Evidence: a built Gateway in a secretless node:24.22.1 container, sending files with chat.send to a stand-in model and recording what it received. With a real SQLite database made by the sqlite3 CLI, main logged a MIME override from application/x-sqlite3 to text/plain and the model received 13,230 characters of decoded garbage; with this change the file stays application/x-sqlite3 with the existing unsupported-format marker. A NUL-heavy file with no known signature named blob.txt, UTF-16LE text with and without a BOM, UTF-8 text and a PNG behave the same on both. Two SQLite rows in the existing byte-detected table fail on main, at -1 net test lines. Details: #198309 (comment).

Fixes #198289. Thanks @Yun-0000, and @mulderp82 for the report.

Co-authored-by: Ayaan Zaidi <[email protected]>
(cherry picked from commit afa871c)

Release adaptation: keep the release classifier tests and add the two source
SQLite signature regressions without importing main's unrelated table
consolidation. Production guard is unchanged.

Companion audit: no later same-path or PR-number follow-ups through
7bccde6. Existing detectMime signature sniffing and extraction consumers
already honor binary classification; no storage internals are imported.

Proof: git diff --check passed; source path traced through detectMime and
file extraction. Targeted Vitest/typecheck/lint unavailable because this
isolated worktree has no node_modules; dependencies left untouched.

Co-authored-by: RomneyDa <[email protected]>
Co-authored-by: vincentkoc <[email protected]>
(cherry picked from commit 7bbf8c1477902194477dff695c477819c1960ca6)
RomneyDa added a commit that referenced this pull request Nov 15, 2026
Preserve the existing 2026.11.5 beta line and its pending test repairs.
Repair transcript admission, terminal SQLite iterators, binary attachment
classification, detached lifecycle calls, cron late wakes and reload handover.
Carry the explicitly requested Bonjour package-integrity fix and release guidance.

Selected backports: #188055, #198309, #190242, #198860, #198011, #197658,
#197484, #197606. #198011 is the operator-required open PR head, not a
merged-main claim. Cron tests are adapted to the existing beta runtime.

(cherry picked from commit e3064bf)
(cherry picked from commit afa871c)
(cherry picked from commit 2c0ab86)
(cherry picked from commit fee19a9)
(cherry picked from commit dbb6f39)
(cherry picked from commit 4d9f782)
(cherry picked from commit 87c10f5)
(cherry picked from commit ce76ed2)

Proof: frozen parent suite passed all 7 shards (265 tests) in 274.45s.
Separate unchanged-production baseline reproduced transcript/lifecycle,
late-wake and live-receipt failures. SQLite already passed on Node 26.12.1.
Independent review found no actionable P0/P1 defects in this snapshot.
The complete beta.4 candidate and stable-profile FRV remain pending.

Co-authored-by: jalehman <[email protected]>
Co-authored-by: vincentkoc <[email protected]>
Co-authored-by: Yun. <[email protected]>
Co-authored-by: ava-perry <[email protected]>
Co-authored-by: Alfred J Berchtold <[email protected]>
Co-authored-by: Stronggenetics <[email protected]>
Co-authored-by: Ayaan Zaidi <[email protected]>
Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Binary attachments (SQLite, ELF, PE, …) are classified as UTF-16 text and ~60k chars of garbage are inlined into the model prompt

2 participants