Repository navigation
feat(D147): OCR large PDFs in page batches instead of failing at the provider limit - #552
Conversation
Codex review round 1 (gpt-6.1-sol, max effort)VERDICT: REQUEST_CHANGES
Validation: 138 passed, 3 skipped; Ruff and diff checks passed. Rich stitching probes matched for ordered responses, and no converter-version or reuse-key change was found. Docker was not used; the worktree remains unchanged. Disposition (Claude): all accepted, fixed in d701c191, 2 and 4 are fixed by one redesign of |
Codex review, round 2 (gpt-6.1-sol, max)VERDICT: REQUEST_CHANGES
Validation: 184 passed, 3 skipped; Ruff, Pyright and diff checks passed. Below-ceiling payload/artifact equivalence, rich stitching, timeout lock cleanup, page ordering, sizing and URL-route limit behavior verified. No converter-version or reuse-key changes found. Docker/database tests were not run; worktree unchanged.
Validation: 184 passed, 3 skipped; Ruff, Pyright and diff checks passed. Below-ceiling payload/artifact equivalence, rich stitching, timeout lock cleanup, page ordering, sizing and URL-route limit behavior verified. No converter-version or reuse-key changes found. Docker/database tests were not run; worktree unchanged. Dispositions (commit 7aec191)
D147 item 4 updated. Unit lane: 3165 passed. |
Codex review, round 3 (gpt-6.1-sol, max)VERDICT: REQUEST_CHANGES
Validation: 185 passed, 3 skipped; Ruff and Pyright passed. Sixteen settings combinations matched Dispositions (commit cd116af)
Unit lane: 3166 passed. |
Codex review, round 4 (gpt-6.1-sol, max)VERDICT: REQUEST_CHANGES
The round-3 spool-write fix is verified, including retryability and billed usage. 207 tests passed, 36 skipped; Ruff, Pyright and CI gate passed. Below-ceiling requests and artifacts matched Disposition (commit c35ba90)
I measured peak RSS with a 69 MB, 64-page image PDF on disk and a 2 MiB ceiling (64 one-page batches of about 1 MiB), running each stage in its own process:
So the remaining peak comes from the PDF body check, which scans a memory map of the file for the end marker. Those are clean, file-backed pages: the kernel reclaims them under pressure and they go away when the map closes. It isn't anonymous memory. On Cloud Run, Unit lane: 3167 passed. |
…provider limit Owner rule: no caps. A PDF over the OCR provider's per-request ceilings (about 50 MB, 1,000 pages) is split with pdfium into page-range PDFs that each fit; each batch is OCR'd, its pages renumbered from the batch's first page, and the folded response metered (every source page) and normalized exactly like one call. Below both ceilings the request and result are byte-for-byte unchanged, so no version changes. convert_path reads a PDF on disk one batch at a time, for a host that must not hold it in memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
… usage kept Codex round 1: - Each candidate batch opens the source, copies the pages, saves to a temporary file and closes everything inside one locked pdfium operation: memory no longer grows with the source or an oversized candidate, and no native document outlives the lock. Only a fitting batch is read. - Split failures after billed batches carry their usage; batches are stitched by page index; usage_info totals cover every batch. - workspace formats design reconciled with batching and URL routes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
- Grow candidate batches from the previous batch size (one page at first), at most doubling, then binary search, so no trial runs far past what fits. - A disk error while splitting after billed batches keeps their usage (retryable ConverterLaneError). - Keep every batch's response metadata under `batches` in the interchange asset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
- Measure each page alone (one-page PDF saved to a counter) and take pages while their sizes sum within the byte ceiling, so no candidate batch runs past the limit on uneven pages. - pdfium swallows write-callback exceptions; record them in the save sink and re-raise, so a full disk is a retryable error, not an empty batch sent to the provider. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Reopening the source per measured page keeps pdfium from holding more than one page's data while batches are sized. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm
Codex review, round 5 (gpt-6.1-sol, max)VERDICT: APPROVE
Round-4 fix verified; no code regressions found. Below-ceiling requests and artifacts matched 335 tests passed, 21 skipped. Ruff, Pyright, and CI gate passed. Database tests left to CI; no containers started or files modified. Disposition (commit 05e5a96, rebased on main)
|
c35ba90 to
05e5a96
Compare
Owner rule: no caps. Before this PR, the PDF route failed any PDF over the OCR provider's per-request ceiling (Mistral:
max_document_bytes, 50 MB). Now it splits the PDF into page-range batches, OCRs each one, and stitches the results in page order.How it works
MistralOcrSettings.max_document_bytes(50 MB, unchanged) and a newmax_document_pages(1,000). Neither entersversion_fingerprint(), so no representation is re-keyed.pdf.pdf_page_batches).import_pages. It is sized from the average page size and shrunk until its saved bytes fit the byte ceiling, and it never exceeds the page ceiling./v1/ocrcall._usage(every source page, oneocrusage event) and_normalize. With page-wise identical provider output, the result equals a single call: samedocument_md, source map, derivation ranges and asset names (asset names carry page numbers).ConverterLaneErrorcarrying their usage. It is retryable for provider errors and terminal for deterministic 4xx.ConversionError.provider_limit_bytes, so the convert stage does not pre-refuse PDFs over 50 MB. The 100 MB PDF reading limit still applies on the in-process bytes path (feat(D147): oversized files reach a URL route; pass 413 from the spend lease #550).Convert service / URL route
PdfConverter.convert_path(path=, mime=)andMistralOcrConverter.convert_path(path=, mime=)convert a PDF from a local file. pdfium reads it lazily and batches are built one at a time, so a host that downloads the input by pre-signed URL to disk never holds the PDF in memory: one batch (≤ 50 MB) plus its request. Below the ceilings the file is read and sent whole, exactly as byconvert.Tests (
test_mistral_ocr.py, with a mock provider that "OCRs" each batch from its text layer)[4, 4, 2], and the reading, source map and derivation ranges equal the single-call result. 10 pages are metered.ConverterLaneErrorcarrying batch 1's 4 pages.convert_pathon a 10-page file withPath.read_bytesforbidden gives batches[3, 3, 3, 1]and all pages located.Checks
Coordination:
meter-d94(convert-stage metering, E1) has been told which files this touches.🤖 Generated with Claude Code
https://claude.ai/code/session_01F9cMsQBqiW2qU2ow4zeNCm