From f3b9329fb212e053007207e037fb6f8163edaad7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sun, 25 Oct 2026 00:35:40 +0200 Subject: [PATCH] fix(sync): mark the read caches stale after a provider backfill `afterProviderBackfill` refolded the rollups over the touched range and re-warmed the status cards, but never marked the analytics caches stale, so after a Fitbit or Google Health history backfill the dashboard and analytics reads served the pre-backfill body until its TTL ran out. The per-write tail has always done this through `afterMeasurementMutation`. The backfill tail now calls `invalidateUserMeasurements` with `touchedFrom` set to the first touched day, after the fold and also when the fold fails, since the rows changed either way. --- .../sync-kit/__tests__/after-backfill.test.ts | 88 +++++++++++++++++++ src/lib/integrations/sync-kit/after-write.ts | 21 ++++- 2 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 src/lib/integrations/sync-kit/__tests__/after-backfill.test.ts diff --git a/src/lib/integrations/sync-kit/__tests__/after-backfill.test.ts b/src/lib/integrations/sync-kit/__tests__/after-backfill.test.ts new file mode 100644 index 000000000..b5e09f480 --- /dev/null +++ b/src/lib/integrations/sync-kit/__tests__/after-backfill.test.ts @@ -0,0 +1,88 @@ +/** + * The end of a provider's full-history backfill marks the read caches stale. + * + * The per-write tail always did (through `afterMeasurementMutation`), but the + * backfill tail refolded the rollups and re-warmed the status cards without + * touching the analytics caches, so the dashboard and the analytics reads went + * on serving the pre-backfill body until its TTL ran out. + */ +import { beforeEach, describe, expect, it, vi } from "vitest"; + +vi.mock("@/lib/rollups/measurement-rollups", () => ({ + collapseToTypeDayKeys: (keys: Array<{ type: string; measuredAt: Date }>) => + keys, + recomputeUserRollups: vi.fn(async () => undefined), +})); +vi.mock("@/lib/cache/invalidate", () => ({ + invalidateUserMeasurements: vi.fn(), + invalidateUserDashboardSnapshot: vi.fn(), +})); +vi.mock("@/lib/insights/status-invalidation", () => ({ + invalidateStatusInsightsForTypes: vi.fn(async () => undefined), +})); +vi.mock("@/lib/rollups/after-measurement-mutation", () => ({ + afterMeasurementMutation: vi.fn(), +})); +vi.mock("@/lib/arrivals/measurement-emit", () => ({ + emitInsertedMeasurementArrivals: vi.fn(async () => undefined), +})); + +import { invalidateUserMeasurements } from "@/lib/cache/invalidate"; +import { invalidateStatusInsightsForTypes } from "@/lib/insights/status-invalidation"; +import { recomputeUserRollups } from "@/lib/rollups/measurement-rollups"; + +import { afterProviderBackfill } from "../after-write"; + +const keys = [ + { type: "WEIGHT" as const, measuredAt: new Date("2026-03-05T00:00:00Z") }, + { type: "PULSE" as const, measuredAt: new Date("2026-01-10T00:00:00Z") }, + { type: "WEIGHT" as const, measuredAt: new Date("2026-02-01T00:00:00Z") }, +]; + +beforeEach(() => { + vi.clearAllMocks(); +}); + +describe("afterProviderBackfill", () => { + it("marks the user's read caches stale back to the first touched day", async () => { + await afterProviderBackfill({ userId: "u1", provider: "fitbit", keys }); + + expect(recomputeUserRollups).toHaveBeenCalledWith("u1", { + types: ["WEIGHT", "PULSE"], + from: new Date("2026-01-10T00:00:00Z"), + to: new Date("2026-03-06T00:00:00Z"), + }); + expect(invalidateUserMeasurements).toHaveBeenCalledTimes(1); + expect(invalidateUserMeasurements).toHaveBeenCalledWith("u1", { + touchedFrom: new Date("2026-01-10T00:00:00Z"), + }); + // After the fold, so a reader rebuilds against the new rollups. + expect( + vi.mocked(invalidateUserMeasurements).mock.invocationCallOrder[0], + ).toBeGreaterThan( + vi.mocked(recomputeUserRollups).mock.invocationCallOrder[0]!, + ); + expect(invalidateStatusInsightsForTypes).toHaveBeenCalledWith( + "u1", + ["WEIGHT", "PULSE"], + { source: "sync" }, + ); + }); + + it("marks them stale even when the fold fails, and still throws for the caller", async () => { + vi.mocked(recomputeUserRollups).mockRejectedValueOnce(new Error("fold")); + + await expect( + afterProviderBackfill({ userId: "u1", provider: "fitbit", keys }), + ).rejects.toThrow("fold"); + expect(invalidateUserMeasurements).toHaveBeenCalledWith("u1", { + touchedFrom: new Date("2026-01-10T00:00:00Z"), + }); + }); + + it("does nothing for a backfill that touched nothing", async () => { + await afterProviderBackfill({ userId: "u1", provider: "fitbit", keys: [] }); + expect(recomputeUserRollups).not.toHaveBeenCalled(); + expect(invalidateUserMeasurements).not.toHaveBeenCalled(); + }); +}); diff --git a/src/lib/integrations/sync-kit/after-write.ts b/src/lib/integrations/sync-kit/after-write.ts index 99c0b542c..61c0c394b 100644 --- a/src/lib/integrations/sync-kit/after-write.ts +++ b/src/lib/integrations/sync-kit/after-write.ts @@ -29,7 +29,10 @@ import { emitInsertedMeasurementArrivals, type InsertedMeasurementArrivalRow, } from "@/lib/arrivals/measurement-emit"; -import { invalidateUserDashboardSnapshot } from "@/lib/cache/invalidate"; +import { + invalidateUserDashboardSnapshot, + invalidateUserMeasurements, +} from "@/lib/cache/invalidate"; import { invalidateStatusInsightsForTypes } from "@/lib/insights/status-invalidation"; import { getEvent } from "@/lib/logging/context"; import { afterMeasurementMutation } from "@/lib/rollups/after-measurement-mutation"; @@ -116,8 +119,14 @@ export function invalidateProviderStatus( /** * The end of a full-history backfill: refold the whole touched range once - * (from the first touched day to the end of the last), then re-warm the - * touched status cards. Throws when the fold fails, for the caller to count. + * (from the first touched day to the end of the last), mark the read caches + * stale back to the first touched day, then re-warm the touched status cards. + * Throws when the fold fails, for the caller to count. + * + * The caches are marked even when the fold fails: the rows changed either + * way, and a cached analytics body built before the backfill would otherwise + * be served until its TTL ran out. That is the same order and the same + * posture as the per-write tail (`afterMeasurementMutation`). */ export async function afterProviderBackfill(input: { userId: string; @@ -135,6 +144,10 @@ export async function afterProviderBackfill(input: { // The keys are UTC day starts; the end is past the last touched day so the // aggregator's `< to` bound covers it. const to = new Date(sorted[sorted.length - 1]! + 24 * 60 * 60 * 1000); - await recomputeUserRollups(input.userId, { types, from, to }); + try { + await recomputeUserRollups(input.userId, { types, from, to }); + } finally { + invalidateUserMeasurements(input.userId, { touchedFrom: from }); + } invalidateProviderStatus(input.userId, input.provider, types); }