Bound list/PDF payloads, stream MinIO I/O with cancellation, paginate history and FHIR $everything, tighten OCR lifetimes/buffers, and add phase tracking docs.
3.2 KiB
3.2 KiB
Phase 21 — OCR Reliability
What exists
OcrProcessingServicepolls withTake(5)and usesstoppingTokenfor queries/Delay, but download / OCR / prefill omit CT.AzureDocumentOcrServiceconstructsDocumentIntelligenceClientper scoped resolve (Program.csAddScoped).TesseractOcrServicebuilds a newTesseractEngineper document;ImagePreprocessorsync-copies PDF and allocates large BGRA rasters.OcrResult.RawTextstores unbounded OCR text.
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.PreFillAsyncand MinIO download fromOcrProcessingService. - Azure analyze must observe CT; Tesseract path checks CT between preprocess and process.
Step 2 — Singleton Azure Document Intelligence client
- Register
DocumentIntelligenceClientas singleton inProgram.cswhen provider is Azure. - Inject into scoped
AzureDocumentOcrService(thin wrapper).
Step 3 — Tesseract / ImagePreprocessor buffer reductions
- Prefer async
CopyToAsyncfor PDF buffering. - Avoid redundant
ToArray()where a single buffer suffices. - Reuse one
TesseractEngineper 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
RawTextlength is capped (or omitted) dotnet testpasses
Out of scope
- Enabling OCR by default
- Parallel OCR across batches
- Fuzzy / seed / PromoteAsync CT (Phase 22)
Verification
./scripts/run-vigilcare-records-phase-13-verification.shdotnet test VigilCareRecordsAPI.Tests/VigilCareRecordsAPI.Tests.csproj- Optional live OCR (
VIGILCARE_OCR_LIVE=1) when credentials exist - 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.