Files
Trent a17bc2b734
CI / changes (push) Successful in 17s
dotnet8-ci / build-and-test (push) Successful in 8m23s
CI / backend (push) Successful in 8m24s
node-ci / build (push) Failing after 2m1s
CI / frontend (push) Failing after 2m2s
Harden API memory and CPU usage across Phases 19–22.
Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
2026-11-30 23:55:56 +08:00

3.2 KiB

Phase 21 — OCR Reliability

What exists

Requires Phase 20. OCR remains opt-in (Ocr:Enabled, default false). Tracker: performance-hardening-overview.md.

What needs to be built

Lower LOH/CPU spikes and make shutdown cooperative when OCR is enabled.

Do not start Phase 22 until success criteria below pass.


Step 1 — CancellationToken through OCR pipeline

  • Extend IOcrService.ExtractAsync(..., CancellationToken ct = default).
  • Pass CT into OcrDraftPreFiller.PreFillAsync and MinIO download from OcrProcessingService.
  • Azure analyze must observe CT; Tesseract path checks CT between preprocess and process.

Step 2 — Singleton Azure Document Intelligence client

  • Register DocumentIntelligenceClient as singleton in Program.cs when provider is Azure.
  • Inject into scoped AzureDocumentOcrService (thin wrapper).

Step 3 — Tesseract / ImagePreprocessor buffer reductions

  • Prefer async CopyToAsync for PDF buffering.
  • Avoid redundant ToArray() where a single buffer suffices.
  • Reuse one TesseractEngine per scoped service instance (hosted service remains sequential).
  • Keep processing sequential (no Parallel fan-out).

Step 4 — Cap RawText persistence

  • Truncate RawText (e.g. 64 KB) or store null when only field confidences are needed by draft GET.
  • Document the cap in the phase checklist / overview.

Success criteria

  • Phase 13 verification script passes with OCR disabled
  • CT is on IOcrService / prefill / OCR hosted heavy path
  • Azure client is singleton when Azure provider is configured
  • Tesseract does not construct a new engine per page when the same scope processes one batch
  • Stored RawText length is capped (or omitted)
  • dotnet test passes

Out of scope

  • Enabling OCR by default
  • Parallel OCR across batches
  • Fuzzy / seed / PromoteAsync CT (Phase 22)

Verification

  1. ./scripts/run-vigilcare-records-phase-13-verification.sh
  2. dotnet test VigilCareRecordsAPI.Tests/VigilCareRecordsAPI.Tests.csproj
  3. Optional live OCR (VIGILCARE_OCR_LIVE=1) when credentials exist
  4. Update overview findings 2, 6, 7 (OCR CT), 16 → Done when this phase merges

Gate

Do not start Phase 22 until success criteria above are met.