Skip to content

fix(media): extract large managed inbound PDFs via media-understanding - #92220

Merged
amknight merged 2 commits into
mainfrom
fix/inbound-pdf-extraction-limits
Jun 16, 2026
Merged

amknight merged 2 commits into
mainfrom
fix/inbound-pdf-extraction-limits

Conversation

@amknight

Copy link
Copy Markdown
Member

Summary

Large inbound PDFs/documents from messaging channels and the Control UI were delivered to text-only/locked-down agents as just an attachment marker — the agent never saw the document text. This fixes that by extending the existing media-understanding extraction pipeline rather than adding a parallel one.

The canonical pipeline already extracts inbound document text, wraps it as untrusted content, and injects it into the prompt (applyMediaUnderstanding → extractFileBlocks in src/media-understanding/apply.ts, invoked from getReplyFromConfig). It just inherited the OpenResponses input_file limits — 5 MB / 4 pages — so anything larger was silently skipped.

Changes

  • Extraction limits (Inject bounded text context for managed inbound PDFs #90096) — new resolveFileExtractionLimits (src/media-understanding/file-extraction-limits.ts) sizes inbound file extraction from the agent's media config: agents.defaults.mediaMaxMb (default 20 MB, host-side hard cap 25 MB) and agents.defaults.pdfMaxPages (default 20, cap 150). An explicit gateway.http.endpoints.responses.files operator config still wins per-field. No new config keys.
  • chat.send pass-through (Pass through already-managed inbound PDFs in chat.send #90097) — prestageMediaPathOffloads now lets oversized (>5 MB) managed inbound PDFs pass through sandbox staging with their managed media path instead of returning a 4xx. Host-side extraction reads them via the media-store root, so sandboxed/locked-down agents get the text without copying a large PDF into every sandbox. Non-PDF oversize files are still rejected; staged + pass-through files preserve attachment order.

This is an alternative, narrower implementation of #90111 (closed). It reuses the existing extraction/injection path — no new inbound-pdf-context module, no second prompt-injection site, no prompt-string scanning, no parallel MediaExtractedContext field, and so it avoids the duplicate-extraction and sandbox-path bugs in that approach. Net +161 / −43 non-test LOC.

Thanks to @joeykrug for the original report and investigation in #90096/#90097/#90111.

Behavior

  • Before: a 15.6 MB / 58-page PDF sent to a locked-down agent → prompt contained only [media attached: media://inbound/...pdf (application/pdf)]; on a sandboxed agent, chat.send rejected it with a sandbox staging-limit 4xx.
  • After: the PDF is extracted host-side (bounded text, marked untrusted) and reaches the agent through the existing media-understanding body injection; oversized managed PDFs pass chat.send without the staging-cap rejection.

Verification

Commands run (local, macOS):

  • node scripts/run-vitest.mjs src/media-understanding/file-extraction-limits.test.ts src/media-understanding/apply.test.ts src/gateway/server-methods/chat.directive-tags.test.ts — green (limits 7, apply 53, gateway 218)
  • pnpm tsgo:core / pnpm tsgo:test:src — clean
  • node scripts/run-oxlint.mjs <changed src> — clean
  • pnpm exec oxfmt --check on changed files — clean
  • pnpm build — clean (no [INEFFECTIVE_DYNAMIC_IMPORT])
  • Fresh multi-angle autoreview — no surviving correctness findings; applied the one cleanup (single-pass partition).

Regression coverage added:

  • file-extraction-limits.test.ts — limit derivation (defaults, mediaMaxMb/pdfMaxPages, 25 MB & 150-page ceilings, non-positive fallback, explicit responses.files override).
  • apply.test.ts — a ~6 MB file is now extracted end-to-end through applyMediaUnderstanding (would have been skipped at the old 5 MB cap).
  • chat.directive-tags.test.ts — oversized managed PDF passes through to dispatch with the managed path + MediaStaged; the existing oversize-rejection test repointed to a non-PDF to keep that 4xx covered.

Proof gaps

  • No live large-PDF run yet (Crabbox/real channel); proof is unit/integration tests + typecheck + build. Happy to add a Crabbox end-to-end if wanted before merge.
  • Out of scope here: Stack-safe large attachment handling for Control UI and gateway #90098 (base64/data-URL stack-safety) is independent. Image-only/scanned PDFs still rasterize discarded page images (pre-existing waste, slightly amplified by 4→20 pages; output is correct) — worth a follow-up maxPixels tweak.

Closes #90096
Closes #90097

@openclaw-barnacle openclaw-barnacle Bot added app: web-ui App: web-ui gateway Gateway runtime size: M maintainer Maintainer-authored PR labels Jun 11, 2026
@clawsweeper

clawsweeper Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 15, 2026, 3:32 PM ET / 19:32 UTC.

Summary
The PR raises inbound media-understanding file extraction limits from agent media/PDF defaults and changes chat.send managed-PDF sandbox staging pass-through and fallback behavior with regression tests.

PR surface: Source +149, Tests +365. Total +514 across 6 files.

Reproducibility: yes. at source level: current main still uses the 5 MB / 4-page input_file defaults for media-understanding file extraction, so large managed PDFs can still be skipped for extracted text. The gateway staging half is no longer reproducible on current main because #90115 has merged.

Review metrics: 1 noteworthy metric.

  • Effective Extraction Defaults: 2 increased: 5 MB to 20 MB, 4 pages to 20 pages. This changes host-side extraction cost and prompt-visible document content for existing inbound document workflows.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🌊 off-meta tidepool
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

Mantis proof suggestion
A real Control UI or visible chat proof would materially show that a large managed PDF reaches a locked-down agent as extracted document context instead of only a marker. A maintainer can ask Mantis to capture proof by posting a new PR comment that starts with the OpenClaw Mantis account mention, followed by:

visual task: verify that a large managed PDF upload to a locked-down agent produces extracted document context instead of only a media marker.

Risk before merge

  • [P2] Current main already merged fix(gateway): pass managed inbound PDFs through chat.send #90115 for the gateway fallback, so this dirty branch needs conflict resolution that preserves the merged path-based managed-media trust predicate.
  • [P1] The PR changes default inbound extraction from 5 MB / 4 pages to 20 MB / 20 pages, which increases host-side parsing and prompt-injected untrusted document content for existing setups.
  • [P1] Managed PDFs can bypass or fall back from sandbox staging, so maintainers still need to accept the managed-media trust boundary and the lack of a live large-PDF Control UI or channel smoke on this branch.

Maintainer options:

  1. Rebase Over The Merged Gateway Fix (recommended)
    Resolve the dirty branch by preserving current main's fix(gateway): pass managed inbound PDFs through chat.send #90115 managed-path trust predicate and layering only the still-needed extraction-limit behavior.
  2. Accept The Larger Extraction Envelope
    Maintainers can intentionally accept the 20 MB / 20-page default extraction behavior after reviewing the prompt-content and host-cost impact.
  3. Pause If The Envelope Is Too Broad
    If the new defaults need a narrower product policy, pause this PR and keep the open extraction issue as the canonical decision point.

Next step before merge

  • [P2] Maintainers need to resolve the dirty branch and decide whether to accept the expanded extraction envelope and managed-media boundary; there is no narrow automated repair to queue from this review.

Security
Cleared: No concrete supply-chain, secret-handling, or exploitable path-escape defect was found, but the managed-media boundary remains a maintainer merge risk.

Review details

Best possible solution:

Rebase onto current main, preserve the merged #90115 gateway trust gate, and land the extraction-limit change for #90096 only if maintainers accept the expanded defaults.

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

Yes, at source level: current main still uses the 5 MB / 4-page input_file defaults for media-understanding file extraction, so large managed PDFs can still be skipped for extracted text. The gateway staging half is no longer reproducible on current main because #90115 has merged.

Is this the best way to solve the issue?

No, not as the branch currently stands: the extraction-limit direction is still the right narrow fix, but the gateway half is now stale against current main. The best path is a rebase that preserves the merged gateway fallback and makes the expanded extraction envelope an explicit maintainer choice.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: The PR addresses bounded but real managed-PDF content delivery failures without evidence of a broad core-runtime outage.
  • merge-risk: 🚨 compatibility: Existing default setups would process more PDF/text content and inject more untrusted document text into prompts after merge.
  • merge-risk: 🚨 message-delivery: The diff changes the gateway path deciding whether PDF attachments reach dispatch as staged or managed media paths.
  • merge-risk: 🚨 security-boundary: The managed-PDF pass-through/fallback path is sandbox-adjacent and must remain constrained to saved inbound media files.
  • 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: This is a maintainer/member PR, so the external-contributor real behavior proof gate does not apply; the PR body lists local tests and notes no live large-PDF smoke yet.
Evidence reviewed

PR surface:

Source +149, Tests +365. Total +514 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 3 205 56 +149
Tests 3 383 18 +365
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 588 74 +514

What I checked:

Likely related people:

  • harjothkhara: Authored the merged current-main managed inbound PDF chat.send fallback that now overlaps this PR's gateway changes. (role: recent merged gateway fallback owner; confidence: high; commits: 158d3db85a47; files: src/gateway/server-methods/chat.ts, src/gateway/server-methods/chat.directive-tags.test.ts)
  • steipete: Shortlog and git history show heavy recent work across media-understanding, media input files, PDF extraction, and media helper surfaces. (role: adjacent media/PDF area owner; confidence: high; commits: 1f71137d1e15, 1687c672a790, 7240830ca42f; files: src/media-understanding/apply.ts, src/media/input-files.ts, src/media/pdf-extract.ts)
  • Syysean: History points to the large-attachment claim-check/media inbound offload contract that this PR extends. (role: introduced adjacent offload behavior; confidence: medium; commits: c6f2db150687; files: src/gateway/chat-attachments.ts, src/gateway/server-methods/chat.ts)
  • Sash Zats: Current blame for the media-understanding limit helper and input-file defaults points to this recent shallow-history commit, though the title suggests the ownership signal is weak. (role: recent current-line contributor; confidence: low; commits: a3070e3ddf36; files: src/media-understanding/apply.ts, src/media/input-files.ts, src/gateway/server-methods/chat.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@clawsweeper clawsweeper Bot added the rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. label Jun 11, 2026
@amknight

Copy link
Copy Markdown
Member Author

/clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added 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. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. and removed rating: 🌊 off-meta tidepool PR readiness rating does not apply to this item. labels Jun 15, 2026
@amknight

Copy link
Copy Markdown
Member Author

/clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 15, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@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. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. 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 Jun 15, 2026
amknight and others added 2 commits June 16, 2026 12:45
#90096, #90097)

Inbound PDF/document text already flows to agents through the canonical
media-understanding pipeline (applyMediaUnderstanding -> extractFileBlocks),
but it inherited the OpenResponses input_file limits (5MB / 4 pages), so large
managed PDFs from channels/Control UI were skipped and locked-down agents saw
only an attachment marker.

- Size inbound file extraction from agents.defaults.mediaMaxMb (default 20MB,
  cap 25MB) and pdfMaxPages (default 20, cap 150) via a new
  resolveFileExtractionLimits; explicit gateway responses.files config still
  wins per-field. (#90096)
- chat.send: let oversized (>5MB) managed inbound PDFs pass through sandbox
  staging with their managed media path instead of a 4xx, so host-side
  extraction reaches sandboxed agents without copying the file into every
  sandbox; non-PDF oversize files are still rejected. (#90097)

Reuses the existing extraction/injection path; no parallel module or extra
prompt-injection sites.
@amknight
amknight force-pushed the fix/inbound-pdf-extraction-limits branch from 451ed20 to 845c5ea Compare June 16, 2026 02:49
@amknight

Copy link
Copy Markdown
Member Author

/clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@amknight

amknight commented Jun 16, 2026 •

Copy link
Copy Markdown
Member Author

Rebase resolution: why this replaces the merged resolveManagedInboundPdfFallback (#90115)

This branch was rebased onto current main. The conflict in src/gateway/server-methods/chat.ts was resolved by removing main's resolveManagedInboundPdfFallback (#90115) and replacing it with this PR's per-ref prestageMediaPathOffloads logic. To be explicit: #90115's helper is gone at the line level, but its behavior is fully preserved and extended — this is a replacement-with-a-superset, not a removal of the fix.

Behavior is preserved + improved. #90115 had three managed-PDF fallback sites (oversized >5MB, stageSandboxMedia throws, staging incomplete); all three are covered here, but per-ref instead of all-or-nothing. In a mixed batch (e.g. one oversized PDF + one small doc), #90115 fails the whole chat.send; this PR passes the managed PDF through its host-readable managed path, still stages the small file, and preserves attachment order. PDF detection also widens to application/pdf, +pdf, and .pdf.

The trust check is equivalent, not weakened. #90115 gated the fallback with resolveInboundMediaReference(ref.path) (an async FS path-containment check under <mediaDir>/inbound). This PR gates it with parseInboundMediaUri(ref.mediaRef) (a sync parse of media://inbound/<id>). These reach the same conclusion because OffloadedRef has exactly one construction site (src/gateway/chat-attachments.ts), where both mediaRef and path are minted by the same server-side saveMediaBuffer(buffer, mime, "inbound", …) call from the same savedMedia object — the client supplies neither, only the raw bytes. So parseInboundMediaUri(ref.mediaRef) !== null is true exactly when the gateway itself saved the file into the managed store, which is what #90115's containment check proves. The FS round-trip is redundant defensive work against a client-controlled-path case that cannot occur in this pipeline, so dropping it loses no security property while removing an async race.

Net new vs main. Beyond the #90097 staging fix, this PR also lands the #90096 piece that main still lacks: resolveFileExtractionLimits sizes media-understanding extraction from the agent's media config (20 MB / 20 pages defaults, 25 MB / 150-page caps) instead of the 5 MB / 4-page input_file defaults, so large managed PDFs actually reach a locked-down agent as extracted document text rather than only a marker.

@amknight
amknight merged commit 67c80e9 into main Jun 16, 2026
165 checks passed
@amknight
amknight deleted the fix/inbound-pdf-extraction-limits branch June 16, 2026 03:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

app: web-ui App: web-ui gateway Gateway runtime maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. merge-risk: 🚨 security-boundary 🚨 May affect sandboxing, authorization, credentials, or sensitive data. P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: L 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.

Pass through already-managed inbound PDFs in chat.send Inject bounded text context for managed inbound PDFs

1 participant