# Phase 21 — OCR Reliability ## What exists - [`OcrProcessingService`](../../VigilCareRecordsAPI/BackgroundServices/OcrProcessingService.cs) polls with `Take(5)` and uses `stoppingToken` for queries/`Delay`, but download / OCR / prefill omit CT. - [`AzureDocumentOcrService`](../../VigilCareRecordsAPI/Services/Ocr/AzureDocumentOcrService.cs) constructs `DocumentIntelligenceClient` per scoped resolve (`Program.cs` `AddScoped`). - [`TesseractOcrService`](../../VigilCareRecordsAPI/Services/Ocr/TesseractOcrService.cs) builds a new `TesseractEngine` per document; [`ImagePreprocessor`](../../VigilCareRecordsAPI/Services/Ocr/ImagePreprocessor.cs) sync-copies PDF and allocates large BGRA rasters. - [`OcrResult.RawText`](../../VigilCareRecordsAPI/Domain/Entities/OcrResult.cs) stores unbounded OCR text. Requires [Phase 20](./phase-20-plan.md). OCR remains opt-in (`Ocr:Enabled`, default false). Tracker: [performance-hardening-overview.md](./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](./phase-22-plan.md) 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`](../../VigilCareRecordsAPI/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 - [x] Phase 13 verification script passes with OCR disabled - [x] CT is on `IOcrService` / prefill / OCR hosted heavy path - [x] Azure client is singleton when Azure provider is configured - [x] Tesseract does not construct a new engine per page when the same scope processes one batch - [x] Stored `RawText` length is capped (or omitted) - [x] `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](./phase-22-plan.md) until success criteria above are met.