Merge branch 'release/v1.45.0' into feat/v145-runtime

This commit is contained in:
Marc-André Bombeck committed 2026-10-22 09:41:54 +02:00
commit 6dfb7eada6
45 files changed
+3547 -1834

No files matched your search

@@ -335,5 +335,12 @@ describe("dashboard all-time counts — parity with the live count", () => {
expect(legacy.get("PULSE")).toBeGreaterThan(500_000);
expect(legacy.get("WEIGHT")).toBe(5);
expect(legacy.get("BLOOD_GLUCOSE")).toBe(2);
});
// Over half a million rows written and folded into DAY buckets: about
// 35 s on a CI runner when the file ran alone. With three files sharing
// the runner's Postgres it took 1.7 times as long (measured with the
// database capped at two CPUs, HEALTHLOG_INTEGRATION_PG_CPUS=2: 14.6 s
// alone, 25.3 s beside the sharing matrix and the restore memory test),
// past the suite's 60 s default. The work is the point of the case, so
// the budget is sized to it: twice the contended estimate.
}, 150_000);
});
+16 -8
View File
@@ -44,7 +44,7 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PROJECT_ROOT = resolve(__dirname, "..", "..");
let container: StartedPostgreSqlContainer | null = null;
let started: StartedPostgreSqlContainer | null = null;
declare module "vitest" {
export interface ProvidedContext {
@@ -56,7 +56,7 @@ declare module "vitest" {
export default async function setup(
project: TestProject,
): Promise<() => Promise<void>> {
container = await new PostgreSqlContainer("postgres:16-alpine")
let container = new PostgreSqlContainer("postgres:16-alpine")
.withDatabase(TEMPLATE_DATABASE)
.withUsername("healthlog")
.withPassword("healthlog")
@@ -74,10 +74,18 @@ export default async function setup(
"full_page_writes=off",
"-c",
"max_connections=400",
])
.start();
]);
// A CPU quota for the database container, to reproduce a CI runner's
// contention on a larger machine: HEALTHLOG_INTEGRATION_PG_CPUS=2.
const cpus = Number.parseFloat(
process.env.HEALTHLOG_INTEGRATION_PG_CPUS ?? "",
);
if (Number.isFinite(cpus) && cpus > 0) {
container = container.withResourcesQuota({ cpu: cpus });
}
started = await container.start();
const url = container.getConnectionUri() + "?schema=public&pgbouncer=false";
const url = started.getConnectionUri() + "?schema=public&pgbouncer=false";
process.env.DATABASE_URL = url;
// The explicit child environment keeps Prisma CLI on the same container.
@@ -91,9 +99,9 @@ export default async function setup(
project.provide("integrationDatabasePlan", await readTemplate(url));
return async () => {
if (container) {
await container.stop({ remove: true });
container = null;
if (started) {
await started.stop({ remove: true });
started = null;
}
};
}
@@ -2,27 +2,21 @@
* The full-history admission lane against a real pg-boss on a real Postgres:
* what happens to an import that runs past its job's expiry.
*
* pg-boss 12.34, measured with the lane's worker shape (localConcurrency 1,
* Up to 12.37, measured with the lane's worker shape (localConcurrency 1,
* groupConcurrency 1, exclusive policy, retries with backoff), a two-second
* expiry, a one-second retry delay and a handler that ignores the abort
* signal and runs for eight seconds:
* signal and runs for eight seconds, pg-boss failed the job at the expiry,
* fired the signal, freed the worker and group slots, and started the retry
* in the same worker BESIDE the run it had given up on: two runs of one job
* at once, which the lane's lock existed to absorb.
*
* +5ms start #1 (job 1709c8a6…) running=1
* +2008ms abort signal #1
* +4051ms start #2 (same job id) running=2
* +6054ms abort signal #2
* +8007ms end #1 running=1
* +9593ms start #3 (same job id) running=2
* +12053ms end #2
* +17595ms end #3
* +19166ms start #4 (same job id) running=1
*
* At the expiry pg-boss fails the job, fires the signal, frees the worker and
* group slots, and retries the job after its delay; the handler keeps going.
* The retry then starts in the same worker beside the run it replaced, and
* the lane's one-at-a-time holds for everything except the import too long
* for its expiry. The first test keeps that as an executable statement about
* pg-boss; the second runs the lane's own handler under the same conditions.
* 12.38 changed that. At the expiry it still fails the job and fires the
* signal, but the worker fetches nothing more until the handler returns (and
* records a `handler_overrun` warning). The retry starts after the overrun
* ends, in the same worker, so the lane's one-at-a-time now holds without the
* lock's help; the lock stays as the guard for a second worker or instance.
* The first test keeps that as an executable statement about pg-boss; the
* second runs the lane's own handler under the same conditions.
*/
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import { PgBoss, type Job } from "pg-boss";
@@ -84,18 +78,23 @@ afterAll(async () => {
});
describe("an import that outlives its expiry", () => {
it("pg-boss starts the retry of the same job beside the run it expired", async () => {
it("pg-boss retries the expired job only once the run it gave up on returns", async () => {
const queue = `lane-expiry-raw-${suffix}`;
await boss.createQueue(queue, { policy: "exclusive" });
const ids: string[] = [];
const runs: Array<{ id: string; start: number; end?: number }> = [];
let running = 0;
let maxRunning = 0;
await boss.work(queue, WORK_OPTIONS, async ([job]: Job<object>[]) => {
ids.push(job!.id);
const run: { id: string; start: number; end?: number } = {
id: job!.id,
start: Date.now(),
};
runs.push(run);
running += 1;
maxRunning = Math.max(maxRunning, running);
await sleep(8_000);
running -= 1;
run.end = Date.now();
});
await boss.send(queue, payload, {
...integrationBackfillAdmissionSendOptions(payload),
@@ -105,12 +104,13 @@ describe("an import that outlives its expiry", () => {
await sleep(12_000);
await boss.offWork(queue);
expect(ids.length).toBeGreaterThanOrEqual(2);
expect(new Set(ids).size).toBe(1);
expect(maxRunning).toBeGreaterThanOrEqual(2);
expect(runs.length).toBeGreaterThanOrEqual(2);
expect(new Set(runs.map((r) => r.id)).size).toBe(1);
expect(maxRunning).toBe(1);
expect(runs[1]!.start).toBeGreaterThanOrEqual(runs[0]!.end!);
}, 60_000);
it("the lane's handler runs the import once while its retries find it running", async () => {
it("the lane's handler runs one import at a time while the expired run overruns", async () => {
const queue = `lane-expiry-drain-${suffix}`;
await boss.createQueue(queue, { policy: "exclusive" });
const handler = runJob(queue, drainIntegrationBackfillAdmission);
@@ -128,14 +128,16 @@ describe("an import that outlives its expiry", () => {
...EXPIRY_OVERRIDE,
});
// The run holds for eight seconds; its retries arrive while it does.
await sleep(7_000);
// The run holds for eight seconds, past its two-second expiry; the retry
// waits for it and starts after.
await sleep(12_000);
await boss.offWork(queue);
await sleep(2_000);
await sleep(8_500);
expect(deliveries.length).toBeGreaterThanOrEqual(2);
expect(runner.calls).toBe(1);
expect(new Set(deliveries).size).toBe(1);
expect(runner.maxRunning).toBe(1);
expect(runner.calls).toBe(deliveries.length);
}, 60_000);
});
@@ -145,6 +145,47 @@ async function holdLock(): Promise<Client> {
return holder;
}
/** Sessions of this database queued behind a lock, other than the holder. */
async function lockWaiters(db: Client, holderPid: number): Promise<number> {
const { rows } = await db.query<{ n: string }>(
`SELECT count(*) AS n FROM pg_stat_activity
WHERE datname = current_database()
AND wait_event_type = 'Lock'
AND pid <> $1`,
[holderPid],
);
return Number(rows[0]?.n ?? 0);
}
/**
* Let go of the held lock once a migration attempt has queued behind it and
* given up. Resolves false when no attempt queued within the budget, so the
* test reports that instead of passing on a run that never met the lock.
*/
async function releaseAfterFirstTimeout(holder: Client): Promise<boolean> {
const { rows } = await holder.query<{ pid: number }>(
"SELECT pg_backend_pid() AS pid",
);
const holderPid = rows[0].pid;
const watcher = await connect();
const until = async (predicate: (n: number) => boolean) => {
const deadline = Date.now() + 60_000;
while (Date.now() < deadline) {
if (predicate(await lockWaiters(watcher, holderPid))) return true;
await new Promise((resolve) => setTimeout(resolve, 25));
}
return false;
};
try {
const queued = await until((n) => n > 0);
if (queued) await until((n) => n === 0);
await holder.query("ROLLBACK");
return queued;
} finally {
await watcher.end();
}
}
async function state(db: Client) {
const rows = await db.query(
`SELECT migration_name, finished_at IS NOT NULL AS finished,
@@ -181,15 +222,23 @@ describe("migration 0412 under a held lock", () => {
add0412(dir, { marked: true });
const holder = await holdLock();
// The first attempt meets the lock; the holder lets go during the backoff.
const release = setTimeout(() => {
void holder.query("ROLLBACK");
}, 2_500);
// The release follows the migration rather than a clock: it waits until
// the first attempt is seen queued behind the lock and then gone again
// (its one-second timeout fired), and lets go before the retry. A fixed
// delay raced the CLI's own start-up, which on a loaded runner outlasted
// the delay, so the first attempt found the lock already free.
const released = releaseAfterFirstTimeout(holder);
const startedAt = Date.now();
const result = await migrateDeploy(dir);
clearTimeout(release);
const releasedInTime = await released;
await holder.query("ROLLBACK").catch(() => {});
await holder.end();
expect(
releasedInTime,
"the first attempt never queued behind the held lock",
).toBe(true);
expect(result.code, result.stderr).toBe(0);
expect(result.stderr).toContain("Database error code: 55P03");
expect(result.stdout).toContain(
@@ -174,9 +174,6 @@ describe("PATCH /api/auth/me/disable-coach", () => {
route: ROUTE,
headers: { "content-type": "application/json" },
auth: await makeSession(user),
contract: {
skip: "the registry declares no 400 for this PATCH, which answers one for a body that is not JSON",
},
});
expect(res.status).toBe(400);
expect(res.error).toBe("Invalid JSON body");
@@ -37,11 +37,6 @@ type ModulesResponse = {
updatedAt?: string;
};
const NO_400 =
"the registry declares no 400 for PATCH /api/auth/me/modules although the handler answers one for a body that is not JSON";
const NO_409 =
"the registry declares no 409 for PATCH /api/auth/me/modules although the handler answers modules_conflict for a stale base token";
beforeEach(() => truncateAllTables(prisma));
async function storedPrefs(userId: string): Promise<unknown> {
@@ -63,14 +58,12 @@ async function storedUpdatedAt(userId: string): Promise<Date> {
async function patch(
auth: Awaited<ReturnType<typeof makeSession>>,
body: Record<string, unknown>,
contract?: { skip: string },
) {
return callRoute<ModulesResponse>(PATCH, {
method: "PATCH",
route: ROUTE,
body,
auth,
contract,
});
}
@@ -229,7 +222,6 @@ describe("PATCH /api/auth/me/modules", () => {
route: ROUTE,
headers: { "content-type": "application/json" },
auth: await makeSession(user),
contract: { skip: NO_400 },
});
expect(res.status).toBe(400);
expect(res.error).toBe("Invalid JSON body");
@@ -281,11 +273,10 @@ describe("PATCH /api/auth/me/modules evicts the cached score", () => {
const user = await makeUser();
caches.analytics.set(`${user.id}|score`, { score: 71 });
const res = await patch(
await makeSession(user),
{ sleep: false, baseUpdatedAt: "2020-01-01T00:00:00.000Z" },
{ skip: NO_409 },
);
const res = await patch(await makeSession(user), {
sleep: false,
baseUpdatedAt: "2020-01-01T00:00:00.000Z",
});
expect(res.status).toBe(409);
expect(caches.analytics.get(`${user.id}|score`)).toEqual({ score: 71 });
});
@@ -320,11 +311,7 @@ describe("PATCH /api/auth/me/modules optimistic concurrency", () => {
const first = await patch(session, { mood: false, baseUpdatedAt: base });
expect(first.status).toBe(200);
const second = await patch(
session,
{ sleep: false, baseUpdatedAt: base },
{ skip: NO_409 },
);
const second = await patch(session, { sleep: false, baseUpdatedAt: base });
expect(second.status).toBe(409);
expect(second.meta?.errorCode).toBe("modules_conflict");
expect(await storedPrefs(user.id)).toEqual({ mood: false });
@@ -171,9 +171,6 @@ describe("PATCH /api/auth/me/unit-preference", () => {
route: ROUTE,
headers: { "content-type": "application/json" },
auth: await makeSession(user),
contract: {
skip: "the registry declares no 400 for this PATCH although the description names it",
},
});
expect(res.status).toBe(400);
expect(await storedPreference(user.id)).toBe("metric");
@@ -199,9 +196,6 @@ describe("PATCH /api/auth/me/unit-preference", () => {
body: "unitPreference=imperial",
headers: { "content-type": "application/x-www-form-urlencoded" },
auth: await makeSession(user),
contract: {
skip: "the registry declares no 415 for this PATCH although the description names it",
},
});
expect(res.status).toBe(415);
expect(await storedPreference(user.id)).toBe("metric");
@@ -10,9 +10,6 @@
* record leaves; the static CapabilityStatement carries no user data and
* stays reachable. The module is switched the way the settings switch it,
* through the stored per-account preferences.
*
* The registry declares no 403 for these operations; the refusals below skip
* the contract check for that reason.
*/
import { beforeEach, describe, expect, it } from "vitest";
@@ -45,9 +42,6 @@ import { getPrismaClient, truncateAllTables } from "../setup";
const prisma = getPrismaClient();
const INSURANCE_NUMBER = "A123456789";
const MODULE_OFF_SKIP =
"the FHIR operations declare no 403; the module gate's module.disabled envelope is undocumented on them";
type Handler = Parameters<typeof callRoute>[0];
/** Every data route, by the path a caller hits; the reads name a held id. */
@@ -135,7 +129,7 @@ async function seedOwner(doctorReport: boolean) {
return user;
}
async function callEach(auth: RouteAuth, skip: boolean) {
async function callEach(auth: RouteAuth) {
const results = [];
for (const { handler, route, params } of DATA_ROUTES) {
const res = await callRoute(handler, {
@@ -143,7 +137,6 @@ async function callEach(auth: RouteAuth, skip: boolean) {
route,
params,
auth,
contract: skip ? { skip: MODULE_OFF_SKIP } : undefined,
});
results.push({ route, res });
}
@@ -158,7 +151,7 @@ describe("/api/fhir/* and the doctorReport module", () => {
await makeToken(owner, { permissions: [FHIR_READ_SCOPE] }),
await makeSession(owner),
]) {
for (const { route, res } of await callEach(auth, true)) {
for (const { route, res } of await callEach(auth)) {
expect(res.status, route).toBe(403);
expect(res.data, route).toBeNull();
expect(res.meta, route).toMatchObject({
@@ -177,7 +170,7 @@ describe("/api/fhir/* and the doctorReport module", () => {
const owner = await seedOwner(true);
const auth = await makeToken(owner, { permissions: [FHIR_READ_SCOPE] });
for (const { route, res } of await callEach(auth, false)) {
for (const { route, res } of await callEach(auth)) {
expect(res.status, route).toBe(200);
expect(res.headers.get("content-type"), route).toContain(
"application/fhir+json",
+2 -6
View File
@@ -758,9 +758,6 @@ describe("FHIR paging", () => {
route: "/api/fhir/Observation",
query: { _offset: "1e20" },
auth,
contract: {
skip: "the registry declares no 400 on the FHIR searches, though the description names the 400 OperationOutcome",
},
});
expect(res.status).toBe(400);
@@ -784,12 +781,11 @@ describe("FHIR paging", () => {
method: "GET",
route: "/api/fhir/Patient",
auth,
contract: {
skip: "the registry declares the shared 429 as an application/json error envelope; the FHIR routes answer an application/fhir+json OperationOutcome",
},
});
expect(res.status).toBe(429);
expect(res.headers.get("content-type")).toContain("application/fhir+json");
expect(Number(res.headers.get("retry-after"))).toBeGreaterThanOrEqual(1);
expect(outcomeSchema.parse(res.body).issue[0].code).toBe("throttled");
expect(JSON.stringify(res.body)).not.toContain("Sample Patient");
});
@@ -76,15 +76,17 @@ describe("callRoute's contract check", () => {
).rejects.toThrow(/answered 409, which the contract does not declare/);
});
it("refuses a 403 that is not the generic scope refusal", async () => {
it("refuses the scope gate's 403 on an operation that declares none", async () => {
// `/api/version` takes no credential, so no 403 is declared on it; the
// generic refusal is accepted only where the contract says so.
await expect(
callRoute(
answering(403, {
data: null,
error: "nope",
meta: { errorCode: "http.forbidden" },
error: "Insufficient scope",
meta: { errorCode: "auth.scope.insufficient" },
}),
{ method: "GET", route: "/api/tokens", auth: anonymous },
{ method: "GET", route: "/api/version", auth: anonymous },
),
).rejects.toThrow(/answered 403/);
});
@@ -1124,9 +1124,6 @@ describe("every setting a person can choose", () => {
route: "/api/admin/backups/upload",
body: exported.body,
auth: operator,
contract: {
skip: "the operator's backup routes are not in the published contract",
},
});
expect(
uploaded.status,
+2 -22
View File
@@ -207,20 +207,10 @@ interface ResponseObject {
$ref?: string;
}
/**
* The generic scope gate answers 403 `auth.scope.insufficient` on any route a
* narrow Bearer token does not reach. The registry documents that refusal in
* the prose of the shared 401 (`stdResponses`) rather than as a 403 entry on
* every operation, so an operation that declares the shared 401 is taken to
* declare this 403 as well, and nothing else is.
*/
const SCOPE_REFUSAL_CODE = "auth.scope.insufficient";
function lookupResponse(
method: HttpMethod,
route: string,
status: number,
body: unknown,
): ResponseObject {
const pathItem = (openApiPaths as Record<string, unknown>)[route];
if (!isRecord(pathItem)) {
@@ -235,17 +225,7 @@ function lookupResponse(
);
}
const responses = isRecord(operation.responses) ? operation.responses : {};
let declared = responses[String(status)] ?? responses.default;
if (
!isRecord(declared) &&
status === 403 &&
isRecord(responses["401"]) &&
isRecord(body) &&
isRecord(body.meta) &&
body.meta.errorCode === SCOPE_REFUSAL_CODE
) {
declared = responses["401"];
}
const declared = responses[String(status)] ?? responses.default;
if (!isRecord(declared)) {
throw new ContractViolation(
`${method} ${route} answered ${status}, which the contract does not declare (declared: ${Object.keys(responses).join(", ")}).`,
@@ -274,7 +254,7 @@ function checkContract(
body: unknown,
isJson: boolean,
): void {
const declared = lookupResponse(method, route, status, body);
const declared = lookupResponse(method, route, status);
const content = declared.content ?? {};
const mediaTypes = Object.keys(content);
if (!isJson) {