fix(ops): refuse an account whose rows are not the thousandfold class

The provenance criterion proves where a row came from, not which build
imported it. An account re-imported on the fixed build carries the same
`EXPORT_XML_SOURCE_MAX` stamp and the same `stats:` external id with the
right numbers and no audit row, so it is selected exactly like a broken
one — and its rest days, under 200 m because the phone stayed at home,
survive the plausibility check after another thousandfold multiply.

So one candidate row that would leave the range now refuses the whole
account: nothing written, no audit row, the offending rows and a printed
reason instead. The idempotency guard only ever held against the
script's own prior run; this holds against the case it could not see.

The runbook says the same in words — a re-imported account must not be
repaired, the re-import already did it — and says what the dry run is
for: it is where the decision is made, not a formality in front of one
already taken.

Integration coverage for the re-imported account: correct rows, an
`--apply` run, nothing changes and no audit row is left behind.

Review finding H1.
This commit is contained in:
Marc-André Bombeck committed 2026-09-10 00:27:43 +02:00
1 parent 65e9ecbde6
commit 361e577864
4 files changed
+189 -18

No files matched your search

@@ -187,25 +187,101 @@ describe("Apple Health export distance repair — integration", () => {
expect((await readRow(prisma, rowId)).syncVersion).toBe(2);
});
it("uses the archive's own unit and never writes an implausible value", async () => {
it("uses the archive's own unit", async () => {
const prisma = getPrismaClient();
const userId = await makeUser(prisma, "mi");
const mile = await seedRow(prisma, userId, { value: 1.543 });
const absurd = await seedRow(prisma, userId, { value: 250 });
const plans = await planAppleHealthDistanceRepair(prisma, {
archiveUnit: "mi",
});
expect(plans[0].repairable.map((row) => row.id)).toEqual([mile]);
const outcome = await applyAppleHealthDistanceRepair(prisma, plans[0], {
archiveUnit: "mi",
});
await settleBackgroundTasks();
expect(outcome).toMatchObject({ updated: 1, skipped: 0 });
expect((await readRow(prisma, mile)).value).toBeCloseTo(2483.217792, 6);
});
it("refuses the whole account when one row would leave the range", async () => {
const prisma = getPrismaClient();
const userId = await makeUser(prisma, "mixed");
const plausible = await seedRow(prisma, userId, { value: 1.543 });
const absurd = await seedRow(prisma, userId, { value: 250 });
const plans = await planAppleHealthDistanceRepair(prisma, {
archiveUnit: "mi",
});
expect(plans[0].repairable.map((row) => row.id)).toEqual([plausible]);
expect(plans[0].outOfRange.map((row) => row.id)).toEqual([absurd]);
const outcome = await applyAppleHealthDistanceRepair(prisma, plans[0], {
archiveUnit: "mi",
});
await settleBackgroundTasks();
expect(outcome).toMatchObject({ updated: 1, skipped: 1 });
expect((await readRow(prisma, mile)).value).toBeCloseTo(2483.217792, 6);
expect(outcome.updated).toBe(0);
expect(outcome.skipped).toBe(1);
expect(outcome.skippedRows.map((row) => row.id)).toEqual([absurd]);
expect(outcome.refusedReason).toMatch(/leave the plausible range/);
// Nothing written at all — not the "repairable" row, not the audit row
// that would make a later, correct run a no-op.
expect((await readRow(prisma, plausible)).value).toBe(1.543);
expect((await readRow(prisma, plausible)).syncVersion).toBe(1);
expect((await readRow(prisma, absurd)).value).toBe(250);
expect(
await prisma.auditLog.count({
where: { action: APPLE_HEALTH_DISTANCE_REPAIR_ACTION, userId },
}),
).toBe(0);
});
it("leaves an account re-imported on the fixed build untouched", async () => {
// The scenario the criterion cannot see: the operator re-imported
// `export.zip` on the fixed build, so the rows are already right. They
// still carry `EXPORT_XML_SOURCE_MAX` + the `stats:` external id and
// still have no audit row, so they are selected exactly like a broken
// account's rows. The rest day survives another x1000; the ordinary days
// do not, and that is what has to stop the account.
const prisma = getPrismaClient();
const userId = await makeUser(prisma, "reimported");
const ordinary = await seedRow(prisma, userId, { value: 2484 });
const longWalk = await seedRow(prisma, userId, { value: 7020 });
const restDay = await seedRow(prisma, userId, { value: 150 });
const plans = await planAppleHealthDistanceRepair(prisma, {
archiveUnit: "km",
});
expect(plans).toHaveLength(1);
// The rest day would pass the plausibility check on its own.
expect(plans[0].repairable.map((row) => row.id)).toEqual([restDay]);
expect(plans[0].outOfRange.map((row) => row.id).sort()).toEqual(
[ordinary, longWalk].sort(),
);
const outcome = await applyAppleHealthDistanceRepair(prisma, plans[0], {
archiveUnit: "km",
});
await settleBackgroundTasks();
expect(outcome.updated).toBe(0);
expect(outcome.refusedReason).not.toBeNull();
for (const [id, value] of [
[ordinary, 2484],
[longWalk, 7020],
[restDay, 150],
] as const) {
const row = await readRow(prisma, id);
expect(row.value).toBe(value);
expect(row.syncVersion).toBe(1);
}
expect(
await prisma.auditLog.count({
where: { action: APPLE_HEALTH_DISTANCE_REPAIR_ACTION, userId },
}),
).toBe(0);
});
it("plans one entry per account", async () => {