fix(rollups): register read-path warm-ups with the background registry

The mood rollup warm-up on the dashboard summary was started with a bare
`void`, so `settleBackgroundTasks()` never saw it. On a stale account the
warm-up started a full recompute that could still be writing when the
statement counter closed its window, and the hot-route budget read 30
against 28 depending on where it landed.

Every read-path start of that shape now goes through
`trackBackgroundTask(runDetached(...))`: the measurement and mood rollup
warm-ups (seven call sites), the dashboard's today-briefing warm, and the
two delegated-access bookkeeping writes in the record guard. A guard
refuses a bare `void` on any of them.

`trackBackgroundTask` now wraps its handle in `Promise.resolve`: a test
double that returns nothing used to leave `undefined` in the set, and
settle spun on it at full CPU.

The budget fixture mints the mood rollups in its seed, as it already did
for the measurement rollups. Eight serial runs of
query-budget-hot-routes read the same counts every time (dashboard
summary 28, snapshot 99, day 47, health-status 59, correlations 43).
This commit is contained in:
Marc-André Bombeck committed 2026-10-25 00:33:55 +02:00
1 parent 3984ceb01c
commit da2cd62876
14 files changed
+148 -21

No files matched your search

@@ -142,6 +142,14 @@ describe("background-task settle contract", () => {
expect(pendingBackgroundTaskCount()).toBe(0);
});
it("drains a handle that is not a promise instead of spinning on it", async () => {
// A mocked background starter that returns nothing hands the registry
// `undefined`; left in the set, settle would loop on it forever.
trackBackgroundTask(undefined as unknown as Promise<unknown>);
await settleBackgroundTasks();
expect(pendingBackgroundTaskCount()).toBe(0);
});
it("registers what fireAndForget starts", async () => {
let release: () => void = () => {};
fireAndForget(
+53
View File
@@ -91,3 +91,56 @@ describe("detached work cannot rename the request's event", () => {
expect(startsDetached(detached)).toBe(true);
});
});
/**
* Read-path work a request starts and never awaits. Each of these runs real
* statements against Postgres after the response is already on its way, so a
* bare `void` hides it from `settleBackgroundTasks()`: the statement counter
* then closes its window while a stale account's rollup recompute is still
* writing, and the next test inherits the tail. The dashboard summary budget
* read 30 against 28 that way, depending on which side of the window a full
* mood recompute landed.
*/
const READ_PATH_BACKGROUND_STARTERS = [
"ensureUserRollupsFresh",
"ensureUserMoodRollupsFresh",
"requestTodayBriefingWarm",
"touchGrantUsage",
"recordDelegatedAccess",
] as const;
describe("read-path background work is registered", () => {
const files = readSourceFolder("src", {
floor: 3000,
include: (rel) =>
!rel.startsWith("generated/") &&
!rel.includes("__tests__/") &&
!/\.test\.tsx?$/.test(rel),
});
const bare = new RegExp(
String.raw`\bvoid\s+(${READ_PATH_BACKGROUND_STARTERS.join("|")})\s*\(`,
);
it("starts none of them with a bare void", () => {
const offenders = files.flatMap(([rel, text]) =>
stripComments(text)
.split("\n")
.flatMap((line, i) => (bare.test(line) ? [`src/${rel}:${i + 1}`] : [])),
);
expect(
offenders,
"wrap the call in trackBackgroundTask(runDetached(() => ...)) or " +
"fireAndForget(...) so the suite can await it",
).toEqual([]);
});
it("still finds each starter at a detached call site", () => {
const joined = files.map(([, text]) => stripComments(text)).join("\n");
for (const name of READ_PATH_BACKGROUND_STARTERS) {
expect(
new RegExp(String.raw`=>\s*${name}\s*\(`).test(joined),
`${name} is no longer started detached anywhere; drop it from the list`,
).toBe(true);
}
});
});