Skip to content

fix(media): SQLite and other binary attachments are inlined as garbage text in the prompt - #198311

Closed
ARAYN7011 wants to merge 1 commit into
openclaw:mainfrom
ARAYN7011:fix/binary-attachment-text-198289
Closed

ARAYN7011 wants to merge 1 commit into
openclaw:mainfrom
ARAYN7011:fix/binary-attachment-text-198289

Conversation

@ARAYN7011

Copy link
Copy Markdown

Closes #198289

What Problem This Solves

Fixes: SQLite databases, ELF/PE executables, and other binary attachments with a known signature are inlined into the model prompt as ~60k characters of garbage text when a user sends them as a document.

User Impact

User impact: binary attachments now show the usual [Unsupported document format ...] marker instead of decoded bytes, so turns that carry them no longer trigger provider refusals; the file stays available to tools through the [media attached: ...] note. BOM-less UTF-16 text and Windows-1252 text are still read as text.

Why This Change Was Made

The shared classifier now keeps a binary type identified by magic bytes binary, and only accepts a BOM-less UTF-16 guess when the bytes decode as clean text.

Technical detail

In classifyAttachmentBytes, the binary fall-through ran resolveUtf16Charset (a NUL-ratio guess, no decode check) and then sniffTextCharset. A SQLite page with many NULs became utf-16be text; a schema-heavy page reached the Windows-1252 sniff.

  • If the MIME is a binary class and byte sniffing alone (detectMime({ buffer })) reports the same MIME, the result is binary with no text inference. MIME hints such as a declared application/x-sh still go through text inference.
  • Otherwise the UTF-16 guess is used only if the first 4 KB decode cleanly (fatal decoder, no control characters) or the file has a BOM, so NUL-heavy unknown binary (for example .mo) stays binary.
  • The classifier is the single owner, so admission and extraction both get the fix. No channel or fallback changes.
  • Accepted edge: BOM-less UTF-16 text whose 4 KB sample cuts a surrogate pair is no longer guessed as UTF-16.

Evidence

Regression tests (attachment-classify.test.ts, file-context.test.ts): NUL-heavy SQLite, schema-heavy SQLite, ELF, NUL-heavy unknown bytes, and the inbound document context for SQLite and ELF; negatives for BOM-less UTF-16LE/BE, Windows-1252, UTF-8/CSV, and declared application/octet-stream.

Fail before, on unchanged main (source change reverted):

classifyAttachmentBytes > byte-detected binary signatures > keeps NUL-heavy SQLite binary
classifyAttachmentBytes > byte-detected binary signatures > keeps SQLite with a text-heavy schema page binary
classifyAttachmentBytes > byte-detected binary signatures > keeps ELF with zero blocks binary
classifyAttachmentBytes > byte-detected binary signatures > keeps NUL-heavy bytes without a known signature from decoding as UTF-16
Tests  4 failed | 17 passed (21)
FAIL  |media-understanding| src/media-understanding/file-context.test.ts > renderInboundDocumentContext > keeps NUL-heavy SQLite out of the prompt as an unsupported document
FAIL  |media-understanding| src/media-understanding/file-context.test.ts > renderInboundDocumentContext > keeps ELF with zero blocks out of the prompt as an unsupported document
Tests  2 failed | 14 passed (16)

Pass after:

node scripts/run-vitest.mjs packages/media-core/src/attachment-classify.test.ts packages/media-core/src/mime.test.ts src/media/input-files.extract.test.ts src/media-understanding/file-context.test.ts src/media-understanding/apply.test.ts --maxWorkers=1
Test Files  3 passed (3)   Tests  180 passed (180)
Test Files  2 passed (2)   Tests  45 passed (45)

Real SQLite file (python3 sqlite3, 5000 rows, declared application/octet-stream) through the real classifyAttachmentBytes and renderInboundDocumentContext, no credentials or model calls.

Before (main):

$ sqlite db: 159744 bytes, declared application/octet-stream, name t.db
classifyAttachmentBytes -> {"mime":"text/plain","class":"text","charset":"utf-16be"}
renderInboundDocumentContext -> context chars: 60217
first 300 chars: "<file name=\"t.db\" mime=\"text/plain\">\n[Partial document: text truncated.]\n\n<<<EXTERNAL_UNTRUSTED_CONTENT id=\"1caccb54bdb5689b\">>>\nSource: External\n---\n卑䱩瑥⁦潲浡琠㌀ကā@†\u0000\u0002\u0000'\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0004\u0000\u0000\u0000\u0000\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u

After (this PR):

$ sqlite db: 159744 bytes, declared application/octet-stream, name t.db
classifyAttachmentBytes -> {"mime":"application/x-sqlite3","class":"binary"}
renderInboundDocumentContext -> context chars: 159
first 300 chars: "<file name=\"t.db\" mime=\"application/octet-stream\">\n[Unsupported document format: application/octet-stream. PDF and plain-text attachments can be read.]\n</file>"

Checks: pnpm exec oxlint and pnpm exec oxfmt --check on the 3 touched files clean, git diff --check clean, pnpm tsgo:core exit 0.

Gaps: no live channel or Gateway/model run (no credentials); the transcripts above exercise the production classifier and document-context renderer directly. pnpm tsgo:core:test, full lint, and the full suite were not run.

@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-environment Preliminary submission classification; full source review pending. labels Nov 13, 2026
@clawsweeper clawsweeper Bot added 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-environment 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 real behavior proof before merge.

What this changes

This PR preserves detected binary formats and validates inferred UTF-16 before including attachment contents as text.

Example: Send a 159,744-byte SQLite database named t.db with application/octet-stream content type.

  • Before: The supplied renderer trace produces 60,217 characters containing decoded binary data.
  • After: The supplied renderer trace produces a 159-character unsupported-document block.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) The focused patch has credible regression coverage and no identified correctness defect, but its direct-renderer evidence does not satisfy the required product-entry proof.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is necessary before merge. See Before merge.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Sending a binary document can fill the model prompt with decoded garbage and make the affected conversation fail.
Reason: The report establishes an owned failure, and every production hunk serves binary classification without adding settings or changing attachment authority.

Merge readiness

⛔ Blocked before merge - 1 item remains

Keep this PR open: it addresses a concrete current bug through the existing classification owner, and no safely landable replacement is established.

Priority: P1
Reviewed head: cca1cfcc32a54dc57ac89616c975795425e7117d

Before merge

  • Add real behavior proof - The supplied before/after output exercises the real classifier and document-context renderer with a generated SQLite database, but calls them directly rather than through a shipped Gateway/channel entry point; provide redacted logs, live output, or terminal screenshots of that flow, excluding IPs, API keys, phone numbers, and non-public endpoints. After adding proof, update the PR body, then request a new review with @clawsweeper re-review. The PR author or someone with repository write access can request it.

Findings

None.

Tests

  • Missing end-to-end proof: Show a synthetic SQLite upload through an isolated Gateway/channel reaching the model without decoded binary content while retaining approved attachment access.
Agent review details

How this fits together

Media-core classifies attachment bytes and MIME hints; attachment caching passes that classification to document admission and extraction, which produce model-visible file blocks.

flowchart TD
A[Inbound attachment] --> B[Media attachment cache]
B --> C[Media core classifier]
C --> D[Document admission]
D --> E[Text or PDF extraction]
D --> F[Unsupported format marker]
E --> G[Model context]
F --> G
Loading

Technical review

Best possible solution:

Keep authoritative binary detections out of text inference in media-core while preserving existing text decoders and attachment access.

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

Current-main source establishes the binary-to-text path, and the linked report supplies official-release steps; this read-only review ran no target code.

Is this the best way to solve the issue?

The patch repairs the existing shared owner, distinguishes byte signatures from MIME hints, and validates unknown UTF-16 candidates without adding parallel policy or configuration.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 1b9518af272b.

Provenance checked

  • Shared classification ownership keeps the original intent (746a188: Consolidate byte classification so attachment admission and extraction consume one consistent result.)
  • Inferred text decoder preservation keeps the original intent (97ea9f7: Carry the decoder that recognized legacy text through extraction to preserve accented characters.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

PR surface

Source +14, Tests +82. Total +96 across 3 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 15 1 +14
Tests 2 82 0 +82
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 97 1 +96

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: fixed_by_candidate
Canonical: #198289
Summary: This PR and two other open candidates address the same binary-to-text classification failure tracked by the canonical issue.

Members:

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

Labels

Label changes:

No label changes.

Label justifications:

  • P1: An ordinary binary document upload can inject substantial garbage into conversation context and reportedly cause repeated failed agent turns on an official release.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Supply the Gateway/channel evidence described in the proof assessment.

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.

Reviewed November 13, 2026, 9:37 AM ET / 14:37 UTC.

@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Thanks @ARAYN7011 for working on #198289. We landed #198309 (afa871c), the earliest of the three fixes, which keeps an attachment binary when its magic bytes match the resolved type and leaves the BOM, extension and text-hint paths unchanged. Closing this one as a duplicate; your work helped confirm the cause.

@obviyus obviyus closed this Nov 13, 2026
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. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

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