fix(medication-compliance): scope coverage-miss backfill enqueue to caller user

QA F-SEC-M-01 (v1.4.39): the route's coverage-miss fallback was firing
`enqueueBootTimeMedicationComplianceBackfill` on every authenticated
hit, running a cluster-wide `LEFT JOIN medication_intake_events x
medication_compliance_rollups` discovery scan in the request path. On a
multi-tenant cluster with millions of intake events the planner walked
all of them per request — a soft DoS amplifier. Split into two helpers:
`enqueueUserMedicationComplianceBackfill(userId)` for the request path
(one targeted enqueue, no discovery scan) and the existing cluster-wide
discovery stays for the worker-boot path. Mirrors the mood-rollup
`ensureUserMoodRollupsFresh` posture. Pinned with a unit test that
asserts the user-scoped helper never issues `$queryRaw`.
This commit is contained in:
Marc-André Bombeck committed 2026-05-18 12:57:25 +02:00
1 parent aacfbf5b61
commit 6ec2d499fb
3 files changed
+97 -5

No files matched your search

+11 -5
View File
@@ -30,7 +30,7 @@ import {
readMedicationCompliance,
hasMedicationComplianceCoverage,
recomputeMedicationComplianceForEvent,
enqueueBootTimeMedicationComplianceBackfill,
enqueueUserMedicationComplianceBackfill,
type ComplianceBucket as RollupComplianceBucket,
} from "@/lib/medications/compliance-rollups";
@@ -169,10 +169,16 @@ async function readComplianceBucketsWithFallback(
}
// Coverage miss — the legacy aggregator stays correct over an empty
// rollup window. Fire the boot backfill in the background so the
// next request lands on the rollup tier; the current request still
// returns the live-derived buckets.
void enqueueBootTimeMedicationComplianceBackfill();
// rollup window. Fire a user-scoped backfill in the background so
// the next request lands on the rollup tier; the current request
// still returns the live-derived buckets.
//
// QA F-SEC-M-01 (v1.4.39): pre-fix this fired the cluster-wide
// `enqueueBootTimeMedicationComplianceBackfill` on every coverage-
// miss request, opening a soft-DoS amplifier (every authenticated
// user could drive a multi-tenant `LEFT JOIN` scan on each hit).
// The user-scoped helper enqueues exactly the caller's account.
void enqueueUserMedicationComplianceBackfill(userId);
annotate({
meta: {
medication_compliance_path: "live-fallback",