Files
vigilcare-records/docs/plans/phase-19-plan.md
T
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

4.0 KiB

Phase 19 — Bounds and Read-Path Hygiene

What exists

Tracker: performance-hardening-overview.md.

What needs to be built

Prevent single-request memory/CPU blowups and cut unnecessary EF tracking and over-fetch. Low risk; no MinIO/OCR/history pagination in this phase.

Do not start Phase 20 until success criteria below pass.


Step 1 — Clamp pagination in services

In BatchService.ListAsync, WorkQueueService.BuildQueueResponseAsync, and CoverSheetService.ListAsync:

pageSize = Math.Clamp(pageSize, 1, 100);
page = Math.Max(1, page);

Clamp in the service layer so all callers are safe.


Step 2 — Cap batch cover-sheet PDF IDs

  • Reject or truncate CoverSheetIds to max 100 (align with create clamp) in CoverSheetController.GenerateBatchPdf or CoverSheetService.GetByIdsAsync.
  • Prefer ValidationException when count > 100 so clients get a clear error.

Step 3 — AsNoTracking and unused Includes

  • Add AsNoTracking() on read-only paths: batch list; cover-sheet lookups that do not mutate; promotion result reads where tracked entities are not updated.
  • Remove .Include(b => b.Events) from VerificationService.VerifyAsync if Events are unused.
  • Keep tracking on mutation paths (verify/approve/assign/promote).

Step 4 — Slim GetDocument

  • Stop calling GetByIdAsync for document streaming.
  • Query only DocumentRef (and not-found) plus ScannedDocuments.ContentType.
  • Preserve document-access audit behavior.

Step 5 — FHIR serializer reuse

  • Static/Lazy FhirJsonSerializer with Pretty = false.
  • Avoid allocating a new serializer (and pretty whitespace) on every FHIR write.

Success criteria

  • pageSize=1000000 on batch list / work-queue / cover-sheet list returns ≤ 100 items
  • Batch PDF with > 100 IDs fails validation (or is hard-capped) — documented in API behavior
  • GetDocument does not load draft graphs; still returns 200 for seeded batches with objects
  • Verification still succeeds without loading Events collection
  • FHIR JSON responses are compact (not pretty-printed)
  • dotnet test on VigilCareRecordsAPI.Tests passes

Out of scope

  • MinIO async download / upload streaming (Phase 20)
  • Patient history / $everything pagination (Phase 20)
  • OCR changes (Phase 21)

Verification

  1. dotnet test VigilCareRecordsAPI.Tests/VigilCareRecordsAPI.Tests.csproj
  2. Manual or test: list with oversized pageSize; GetDocument on a seeded batch; FHIR Patient read payload is single-line/compact JSON
  3. Update overview finding statuses 5, 8, 10, 11 → Done when this phase merges

Gate

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