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

88 lines
4.0 KiB
Markdown

# Phase 19 — Bounds and Read-Path Hygiene
## What exists
- List endpoints accept `page` / `pageSize` without a service-layer clamp: [`BatchService.ListAsync`](../../VigilCareRecordsAPI/Services/BatchService.cs), [`WorkQueueService.BuildQueueResponseAsync`](../../VigilCareRecordsAPI/Services/WorkQueueService.cs), [`CoverSheetService.ListAsync`](../../VigilCareRecordsAPI/Services/CoverSheetService.cs). [`BatchEventService`](../../VigilCareRecordsAPI/Services/BatchEventService.cs) already clamps to 200.
- Cover-sheet create clamps `Count` to 100; [`GenerateBatchPdf`](../../VigilCareRecordsAPI/Controllers/CoverSheetController.cs) accepts unbounded [`BatchPdfRequest.CoverSheetIds`](../../VigilCareRecordsAPI/Models/Records/CoverSheet/BatchPdfRequest.cs).
- Read queries often omit `AsNoTracking`; [`VerificationService.VerifyAsync`](../../VigilCareRecordsAPI/Services/VerificationService.cs) includes unused `Events`.
- [`GetDocument`](../../VigilCareRecordsAPI/Controllers/DigitizationBatchesController.cs) calls full [`GetByIdAsync`](../../VigilCareRecordsAPI/Services/BatchService.cs) (drafts + observations).
- [`FhirJsonOutputFormatter`](../../VigilCareRecordsAPI/Infrastructure/Fhir/FhirJsonOutputFormatter.cs) constructs a Pretty serializer per response.
Tracker: [performance-hardening-overview.md](./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](./phase-20-plan.md) until success criteria below pass.
---
## Step 1 — Clamp pagination in services
In `BatchService.ListAsync`, `WorkQueueService.BuildQueueResponseAsync`, and `CoverSheetService.ListAsync`:
```csharp
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
- [x] `pageSize=1000000` on batch list / work-queue / cover-sheet list returns ≤ 100 items
- [x] Batch PDF with > 100 IDs fails validation (or is hard-capped) — documented in API behavior
- [x] `GetDocument` does not load draft graphs; still returns 200 for seeded batches with objects
- [x] Verification still succeeds without loading Events collection
- [x] FHIR JSON responses are compact (not pretty-printed)
- [x] `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](./phase-20-plan.md) until success criteria above are met.