fix(review): lock OCR backend init, warn on rollback fallthrough, zero-page metric

Address PR #858 review round 2:

- OcrProcessor backend resolution is now guarded by an anyio.Lock (lazy-init,
  double-checked) so a burst of concurrent first-OCR calls resolves the backend
  once instead of each fetching its own gateway M2M token.
- The document_tier1_engine=pymupdf rollback now logs a warning when it falls
  back to the fast processor (no 'structured' registered) instead of silently
  using the very engine the operator opted out of.
- classify_from_text defaults ocr_frac to 0.0 (not 1.0) for a zero-page PDF, so
  the recorded classification metric is "fast" (no OCR evidence) rather than a
  misleading "ocr"; the no_text_layer/bad_text_layer flags are gated on having
  sampled at least one page.

New tests: zero-page classify routes fast, rollback-fallback warning.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Chris Coutinho
2026-06-05 02:24:08 +02:00
co-authored by Claude Opus 4.8
parent 1634e8adc2
commit f1272dfe84
5 changed files with 55 additions and 11 deletions
@@ -203,11 +203,19 @@ class ProcessorRegistry:
settings = get_settings()
if settings.document_tier1_engine == "pymupdf":
processor = self._pdf_processor_for_tier(
"structured"
) or self.find_processor(content_type)
processor = self._pdf_processor_for_tier("structured")
if processor is None:
raise ProcessorError("No PDF processor registered")
# The rollback was set to opt OUT of pypdfium2, so falling back
# to it (the highest-priority PDF processor) silently would
# defeat that intent -- warn loudly.
processor = self.find_processor(content_type)
if processor is None:
raise ProcessorError("No PDF processor registered")
logger.warning(
"document_tier1_engine=pymupdf but no 'structured' processor "
"is registered; falling back to '%s'",
processor.name,
)
return await self._run_processor(
processor, content, content_type, filename, options, progress_callback
)