Skip to content

[ENG-20522] fix(pdf): large plan sets read nothing past the page cap and scans get no page images - #265

Closed
orswn wants to merge 3 commits into
boonfrom
okka/eng-20522/pdf-page-cap-ocr
Closed

orswn wants to merge 3 commits into
boonfrom
okka/eng-20522/pdf-page-cap-ocr

Conversation

@orswn

@orswn orswn commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Related: ENG-20522

What Problem This Solves

Fixes an issue where the agent's pdf tool returned no content for large plan sets when it asked for pages above the page limit, and where scanned sheets with an OCR text layer reached the model as plain text with no page image.

  • With the fleet value agents.defaults.pdfMaxPages = 120, pages="200-300" read nothing. The model then answered "I don't see any pages". On one host, 13 calls for pages above 120 on a 922-page set all came back empty, and none was marked as an error.
  • A call with no pages stops at page 120. The coverage note said "processed pages 1-120 of 793" but did not say how to read the rest.
  • A scanned sheet with an OCR layer has enough text (200 characters or more) to skip page rendering. The model got only the OCR text and was not told that it came from a scan.

Why This Change Was Made

  • pdfMaxPages now limits how many pages one call reads, not which page numbers it can read.
    • Any page can be requested. The first pdfMaxPages requested pages are read.
    • The result names the pages that were left over and gives the exact pages= value for the next call.
    • Requested pages past the end of the document are named and skipped. No empty chunk is sent to the model.
    • When no requested page exists in any file, the tool returns an error with the page count. This happens before the image-model fallback, so the error is not retried per model or reported as "All image models failed". Other extraction errors keep their cause text, for example the password error.
  • Pages with an OCR text layer are labelled, and they get a page image when one fits.
    • A page counts as OCR when at least 30% of its characters (50 or more in total) use an invisible render mode (3 or 7). The check reads the PDFium text page.
    • The pdf tool opts in with a new optional request field, ocrPageImages. Those pages are rendered within the existing per-batch pixel budget. Renders with a longest side under 1000 px are dropped because they cannot be read.
    • The chunk text, the coverage instruction, and the result text name the OCR pages. Pages without a usable image get the "do not claim absence" instruction, also when coverage is complete.
    • OpenResponses input_file extraction does not opt in, so its output does not change.
  • Fork-only: upstream still limits by page number. Expect a conflict in parsePageRange on the next upstream sync.
  • The detector uses clawpdf engine internals behind a feature check. It never throws. An unmocked test with generated PDFs fails if a clawpdf upgrade removes those internals.

User Impact

  • The agent can read any page range of a large plan set, pdfMaxPages pages per call, and gets told how to continue.
  • Out-of-range requests give a clear error instead of an empty answer.
  • On scanned plan sets the model sees which pages are scans and gets a page image when the budget allows. It is told not to claim that something is absent from pages it could not read.
  • Calls without pages on vector PDFs return the same output as before.

Evidence

Classifier on real plan sets (all pages, real clawpdf):

set pages detected OCR pages time
54-page scanned set with OCR layer 54 54 of 54 931 ms
243-page permit set (vector CAD, 6 raster-linework pages) 243 0 3324 ms
32-page mixed-use set 32 0 196 ms
59-page CAD set, 44k objects per page 59 0 1399 ms
40 unrelated PDFs (slides, scanned forms, ebooks) up to 300 each 0
  • The extractor on the scanned set, pages 1-10: one 2366x1690 page image is kept, ocrPages 1-10, ocrImagePages [1].
  • The extractor on the scanned set, pages 1-30: 538 ms with the flag off, 3135 ms with it on.

Tests and checks:

  • node scripts/run-vitest.mjs on the 18 related test files: all shards pass. These include the pdf tool, helpers, document-extract extractor and OCR detector, media plumbing, input-files, OpenResponses file-only, and config schema help.
  • run-tsgo lanes core, extensions, core-test, extensions-test: exit 0.
  • run-oxlint on changed core files and extensions/document-extract: exit 0. oxfmt --check: clean.
  • generate-config-doc-baseline --check: OK. git diff --check: clean.

Live agent smoke: pending, added below when complete.


Summary by cubic

Fixes the pdf tool so requests past the 120-page default read actual pages instead of nothing, and scanned sheets with OCR text layers reach the model as labeled page images.

Page limiting

  • pdfMaxPages now caps how many pages one call reads, not which page numbers; any page can be requested and the first pdfMaxPages requested pages are read.
  • The result names the skipped pages with the exact pages= value for the next call, including when no pages is given and the document exceeds the limit. Pages past the document end are clipped and reported. The continuation hint stays in the tool result only; the analysis model has no tools and is instead told the skipped pages were not provided and must not be described, so it cannot invent their content.
  • When no requested page exists in any file, the tool errors with the page count, before the image-model fallback so it is not retried per model. Page numbers outside the safe-integer range are rejected instead of blocking the tool.
  • The page limit applies per PDF in a call; the tool description, config help, and docs now say so.
  • Fork-only: upstream still caps by page number; expect a conflict in parsePageRange on the next upstream sync.

OCR page detection

  • Pages whose text is mostly invisible (at least 30% of 50+ characters in render modes 3 or 7) are detected and labeled as scanned. The detector never throws and classifies pages one at a time, so a failing page drops only its own label.
  • The pdf tool opts in via a new ocrPageImages field; those pages are rendered within the existing pixel budget and renders under 1000 px on the longest side are dropped.
  • OCR pages without a usable image get the no-absence-claims instruction, even when coverage is complete. For text-only models, ocrImagePages is dropped along with the images so the notes match what the model received.
  • Only the pdf tool opts in; OpenResponses input_file extraction is unchanged.

Written for commit 6d4595a. Summary will update on new commits.

Review in cubic

The pdf tool treated agents.defaults.pdfMaxPages as a cap on the page
number, not on the page count. With the fleet value of 120,
pages="200-300" parsed to an empty list, extraction was skipped, and
the pdf model got the prompt with no document content and no error.
On a 922-page set every call for pages above 120 came back as "I
don't see any pages".

Make pdfMaxPages a per-call page count. Any page number can be
requested; the first pdfMaxPages requested pages are read and the
rest are reported with the exact pages= value for the next call.
Pages past the document end are clipped after the first batch and
named in the coverage text instead of producing empty chunks. When no
requested page exists in any file, return a tool input error that
names the page count. With pages set, extraction now runs before the
image model fallback, so a page-selection or extraction error is not
retried per model or reported as a model outage; the extraction
error keeps its cause text.

Scanned sheets with an OCR text layer passed the 200-character text
check, so they never got page images and the model read their OCR
text as if it were exact. Detect pages whose text is mostly invisible
(render mode 3 or 7, at least 30% of 50 or more characters) from the
PDFium text page. When the caller opts in with ocrPageImages, render
those pages as images within the existing per-batch pixel budget,
drop renders whose longest side is under 1000 px, and label the pages
in the chunk text, coverage instruction, and result text. Pages with
no usable image carry the no-absence-claims instruction even when
coverage is complete. Only the pdf tool opts in; OpenResponses
input_file extraction is unchanged.

The detector uses clawpdf engine internals behind a feature check and
never throws; an unmocked test with generated PDFs fails if a clawpdf
upgrade removes them.

This diverges from upstream, which still caps by page number.

Refs: ENG-20522
@orswn
orswn requested a review from a team October 5, 2026 10:36

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cubic analysis

All reported issues were addressed across 19 files

Linked issue analysis

Linked issue: ENG-20522: OpenClaw pdf tool: large plan sets stop at the page cap and never send page images for OCR-layer scans

Status Acceptance criteria Notes
✅ Read requested pages beyond the per-call page limit pdf-tool.ts reads requested page numbers and reports skipped ranges with the exact pages value for the next call.
✅ Send scanned OCR pages as images or label their text The extractor detects invisible OCR text, attaches readable page images when possible, and labels OCR pages in the model context.
⚠️ Keep the pdf-coverage-integrity regression scenario green The PR reports related test shards passing, but the diff does not show this named regression scenario running.

Tip: instead of fixing issues one by one fix them all with cubic

Re-trigger cubic

Comment thread src/agents/tools/pdf-tool.helpers.ts Resolved
Comment thread src/agents/tools/pdf-tool.test.ts Resolved
Comment thread src/agents/tools/pdf-tool.ts Resolved
Comment thread src/agents/tools/pdf-tool.ts Resolved
Comment thread src/config/schema.help.ts Resolved
Comment thread src/agents/tools/pdf-tool.ts Resolved
- Reject page numbers outside the safe-integer range. A range such as
  9007199254740992-9007199254741000 never advanced and blocked the
  tool.
- Give a read without pages the same continuation line as an
  explicit range when the document is longer than the page limit,
  e.g. pages="121-240".
- For a text-only model, drop ocrImagePages together with the images,
  so the OCR note and the no-absence-claims instruction match what
  the model received.
- Classify OCR pages one at a time, so a page that fails to load
  drops only its own label.
- Say in the tool description, config help, and docs that the page
  limit applies to each PDF in a call, which is what the tool does.
- Remove a wall-clock assertion from a parser test and assert that
  the OCR page image reaches the model.

Refs: ENG-20522

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 12 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread docs/tools/pdf.md Resolved
The coverage note for pages skipped by the per-call limit tells the
reader to "Call pdf again with pages=...". The same note also went into
the prompt of the pdf analysis model, which has no tools. In a live run
on a 300-page set with no `pages`, that model wrote a fake pdf call and
then invented page text for pages 121-230. The agent reported the
invented sheets as read.

Tell the analysis model that the skipped pages were not provided and
must not be described. Keep the continuation hint in the tool result,
where the agent can act on it.

Refs: ENG-20522
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant