Skip to content

fix(media): recognize UTF-8 attachments across sniff boundaries - #135227

Merged
steipete merged 2 commits into
mainfrom
codex/qa-20260901-media-utf8-sample
Sep 1, 2026
Merged

steipete merged 2 commits into
mainfrom
codex/qa-20260901-media-utf8-sample

Conversation

@steipete

@steipete steipete commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes valid UTF-8 attachments being classified as binary when a multibyte character crosses the 4,096-byte sniff boundary. A 4,200-byte Chinese attachment with a generic filename could produce an unsupported-document marker instead of its text in the agent's context.

Why This Change Was Made

The byte classifier finalized a bounded prefix as if it were the complete input. Fatal UTF-8 decoding rejected its incomplete final character even when the remaining bytes existed in the attachment. The classifier now completes only the sequence that starts inside the sample, reading at most three additional bytes, and then performs a final fatal decode. It neither starts another out-of-window sequence nor silently discards an incomplete character at actual EOF.

The canonical text-ratio calculation is simplified, and redundant empty-input and length checks are removed to absorb the repair. Production +16/−16 (net 0); tests +141/−6 (net +135). The existing ratio calculation rejects empty text. No configuration, dependency, full-file validation, or downstream workaround is added.

User Impact

Untyped UTF-8 text survives local classification, caching, and extraction with its complete contents. Existing MIME handling and Windows-1252 fallback policy remain; that fallback still receives the original 4 KiB sample. Bytes after an already complete sample remain outside this bounded heuristic.

The earlier classifier consolidation in #122168 and performance repair in #133424 both retained the prefix-finalization behavior. This does not resolve outbound validation in #120229 or document interception in #129539. Charset propagation for Windows-1252 remains a named follow-up; this repair makes no claim to fix its downstream decoding.

Evidence

The original classifier and real local-cache regressions failed before repair. After ClawSweeper identified that streaming decode could ignore a malformed boundary sequence, two additional cases failed on the reviewed PR head: an invalid continuation and an incomplete sequence at actual 4,097-byte EOF. The final implementation completes and validates that sequence instead of leaving it buffered.

All 35 classifier tests and 12 cache tests pass, including two-, three-, and four-byte boundary splits, overlong encoding, actual EOF, signature/MIME precedence, complete-buffer preservation, and the bounded treatment of bytes outside a completed sample. The named attachment-application case also passes with all 700 Chinese repetitions in prepared agent text and a text/plain file wrapper. It uses a real file with local self-service disabled and exercises classification, caching, extraction, and context preparation. The other 85 application cases were filtered. Total focused AFTER coverage: 48 passing cases.

Scoped formatting and Git diff checks pass. Formatting changed only two test-expression layouts; tested production bytes are unchanged. Independent Codex review is clean. The ClawSweeper boundary finding is addressed, with its failing cases retained as regressions. This is local attachment-flow proof, not a Gateway, provider, or live-channel run. Required exact-head CI remains the landing gate.

Keep bounded UTF-8 prefix decoding open until the actual input ends. Preserve strict complete-input handling and remove redundant empty and slice-limit branches. Add classifier, local-cache, and full attachment-context regressions.
@clawsweeper

clawsweeper Bot commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added size: S maintainer Maintainer-authored PR labels Sep 1, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 1, 2026
@clawsweeper

clawsweeper Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 1, 2026, 11:33 AM ET / 15:33 UTC.

ClawSweeper review

What this changes

The PR completes and validates a UTF-8 sequence that begins inside the 4 KiB attachment sniff window, then adds classifier, cache, and agent-context regression coverage.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep this maintainer PR open. Current main still finalizes the 4 KiB UTF-8 prefix, while the updated head repairs that owner boundary and addresses the prior malformed-continuation finding; no introduced blocking defect was found.

Priority: P2
Reviewed head: 4a3f261722976d6dc4caa40561ea0d96598d8757

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused, net-neutral owner-boundary repair with meaningful regression coverage and no remaining actionable review finding.
Proof confidence 🌊 off-meta tidepool Not applicable: The supplied repository state identifies this as a MEMBER-authored maintainer PR, so the external-contributor proof gate does not apply; its body nevertheless describes a local actual-file cache-to-context run, which was not independently rerun in this read-only review.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: The supplied repository state identifies this as a MEMBER-authored maintainer PR, so the external-contributor proof gate does not apply; its body nevertheless describes a local actual-file cache-to-context run, which was not independently rerun in this read-only review.
Evidence reviewed 6 items Current main still has the reported boundary behavior: Fetched main and the pinned PR base resolve to the same classifier blob. Its UTF-8 path decodes only the first 4 KiB as a complete input, so a valid code point split at that boundary reaches the fallback path.
The repair stays at the classification owner: The new logic finds the final sampled lead byte and extends decoding only through its possible 2-, 3-, or 4-byte sequence; fatal decoding then rejects malformed or incomplete data while the Windows-1252 fallback still receives only the original sample.
Caller and callee preserve one classification fact: The approved local-read cache computes the classification once, and Media Understanding consumes that stored fact to admit text and pass it to the byte extractor.
Findings None None.
Security None None.

How this fits together

Media Core classifies attachment bytes before Media Understanding caches supported files and extracts their text into agent context. This change affects the bounded UTF-8 decision for otherwise untyped local attachments.

flowchart LR
  A[Local attachment bytes] --> B[Media Core classifier]
  B --> C[UTF-8 sniff boundary]
  C --> D[Attachment cache]
  D --> E[Text extraction]
  E --> F[Agent context]
Loading

Before merge

  • Complete next step (P2) - No repair dispatch is needed because this PR already contains the narrow source-owner fix; await the normal exact-head CI gate.
Agent review details

Security

None.

PR surface

Source 0, Tests +135. Total +135 across 4 files.

View PR surface stats
Area Files Added Removed Net
Source 1 16 16 0
Tests 3 141 6 +135
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 157 22 +135

Review metrics

Metric Value Why it matters
Production versus regression coverage production +16/-16 (net 0); tests +141/-6 (net +135) The implementation remains net-neutral while coverage spans unit, cache, and context boundaries.

Technical review

Best possible solution:

Land the bounded classifier repair after exact-head CI completes, preserving the 4 KiB heuristic and the existing Windows-1252 fallback policy.

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

Yes, source-reproducible: current main decodes the bounded prefix as complete UTF-8, and the cache-to-application path carries that classification into agent context. The reviewer did not execute the focused regression under the read-only review contract.

Is this the best way to solve the issue?

Yes. Completing only the code point that starts within the fixed sample window fixes the shared classifier without a full-file scan, downstream workaround, or competing classification path.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 8361f3704dd5.

Labels

Label changes:

  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The supplied repository state identifies this as a MEMBER-authored maintainer PR, so the external-contributor proof gate does not apply; its body nevertheless describes a local actual-file cache-to-context run, which was not independently rerun in this read-only review.
  • remove rating: 🦐 gold shrimp: Current PR rating is rating: 🐚 platinum hermit, so this older rating label is no longer current.
  • remove status: ⏳ waiting on author: Current PR status label is status: 👀 ready for maintainer look.

Label justifications:

  • P2: This corrects misclassification of a bounded attachment path without evidence of an outage, data loss, or security bypass.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: The supplied repository state identifies this as a MEMBER-authored maintainer PR, so the external-contributor proof gate does not apply; its body nevertheless describes a local actual-file cache-to-context run, which was not independently rerun in this read-only review.

Evidence

What I checked:

  • Current main still has the reported boundary behavior: Fetched main and the pinned PR base resolve to the same classifier blob. Its UTF-8 path decodes only the first 4 KiB as a complete input, so a valid code point split at that boundary reaches the fallback path. (packages/media-core/src/attachment-classify.ts:102, 8361f3704dd5)
  • The repair stays at the classification owner: The new logic finds the final sampled lead byte and extends decoding only through its possible 2-, 3-, or 4-byte sequence; fatal decoding then rejects malformed or incomplete data while the Windows-1252 fallback still receives only the original sample. (packages/media-core/src/attachment-classify.ts:97, 4a3f26172297)
  • Caller and callee preserve one classification fact: The approved local-read cache computes the classification once, and Media Understanding consumes that stored fact to admit text and pass it to the byte extractor. (src/media-understanding/attachments.cache.ts:282, 4a3f26172297)
  • Boundary regressions cover the former gap: Added cases exercise valid two-, three-, and four-byte splits, an invalid continuation, an incomplete actual EOF, overlong UTF-8, and bytes that begin only after a complete sample; the application case observes extracted text in the prepared context. (packages/media-core/src/attachment-classify.test.ts:24, 4a3f26172297)
  • Prior review finding was addressed in a follow-up commit: The reviewed head directly follows the earlier reviewed commit and adds the final-sequence calculation plus invalid-continuation and actual-EOF regressions requested by the prior ClawSweeper review. (packages/media-core/src/attachment-classify.ts:101, 4a3f26172297)
  • Review execution limitation: The read-only environment did not rerun tests; pnpm docs:list stopped when Corepack attempted to create its cache on a read-only filesystem, and deeper historical traversal requiring unfetched promisor objects could not resolve GitHub DNS. Current checked-out source and tests were inspected directly.

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • obviyus: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (1 earlier review cycle)
  • reviewed 2026-09-01T13:37:35.639Z sha e8af04c :: found issues before merge. :: [P2] Validate continuation bytes after split UTF-8

@clawsweeper clawsweeper Bot added 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: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 1, 2026
@steipete
steipete merged commit 064b33e into main Sep 1, 2026
223 checks passed
@steipete
steipete deleted the codex/qa-20260901-media-utf8-sample branch September 1, 2026 17:00
@steipete

steipete commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Sep 2, 2026
…claw#135227)

* fix(media): recognize UTF-8 attachments across sniff boundaries

Keep bounded UTF-8 prefix decoding open until the actual input ends. Preserve strict complete-input handling and remove redundant empty and slice-limit branches. Add classifier, local-cache, and full attachment-context regressions.

* fix(media): validate the final sampled UTF-8 sequence
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S 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.

1 participant