Take the writes off three read routes

A GET must not have side effects — api-handler.ts says so next to
READ_HTTP_METHODS, where an MCP-audience token is admitted on the safe
methods precisely because they are assumed to write nothing. Three routes
broke that: the sync handshake bumped User.lastSyncedAt, the achievements
read inserted unlock rows, and the cycle calendar fired a discarded promise
at the forecast cache so a failed write left no trace anywhere.

Each write moves to the place that causes it.

The sync checkpoint is now stamped by the batch ingest endpoints for
measurements, mood and intakes. The handshake reports it. That also fixes
the watermark: the checkpoint used to advance past a window the client had
not drained, and a client trusting it would skip that window. It can only
trail a real sync now.

The unlock rows move to a nightly achievement-unlock-sweep. The badge grid
never needed them — unlocked comes from the live metrics and the completion
date is derived — so the payload is unchanged; the rows pin that date and
carry it into the backup, and the sweep reaches accounts that never open
the page.

The cycle forecast moves to a nightly cycle-prediction-refresh. The
CyclePrediction row is not a cache of a read: it is the index the reminder
cron scans, so which reminders an account received depended on whether
somebody had opened the calendar. persistPredictionCache no longer
swallows its errors either — the job reports them.
This commit is contained in:
Marc-André Bombeck committed 2026-08-02 01:21:03 +02:00
1 parent eb641dcd7b
commit 6ac3a61829
27 files changed
+1365 -139

No files matched your search

@@ -0,0 +1,90 @@
/**
* Structural guard: the three read routes that used to write must stay reads.
*
* `src/lib/api-handler.ts` states the invariant next to `READ_HTTP_METHODS` —
* an MCP-audience token is admitted on GET/HEAD/OPTIONS on the REST surface
* because those methods are assumed side-effect-free, and adding a
* side-effecting GET silently widens that token's reach. These three routes
* each broke it once. The runtime tests beside each route prove the specific
* write is gone; this one fails on a NEW one.
*
* Deliberately scoped to these three files and to writes issued directly in
* the handler. A repo-wide version is not written today because it cannot be
* written without false positives: several read routes call helpers that
* legitimately upsert a lazily-created row (`getOrCreateCycleProfile` behind
* `requireCycleEnabled` is the clearest one), and a text matcher cannot tell
* that apart from a real side effect. Widening this guard means resolving
* those cases first, not loosening the pattern.
*/
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
/** Mutating Prisma model methods, as they appear at a call site. */
const PRISMA_WRITE =
/\.(create|createMany|createManyAndReturn|update|updateMany|upsert|delete|deleteMany|executeRaw|executeRawUnsafe)\s*\(/;
/**
* Write helpers these routes used to reach for. A helper hides its writes
* from the pattern above, so each one that mattered here is named.
*/
const WRITE_HELPERS =
/\b(persistPredictionCache|markSyncCheckpoint|auditLog\s*\()/;
const ROUTES = [
"sync/state/route.ts",
"gamification/achievements/route.ts",
"cycle/calendar/route.ts",
];
function sourceOf(relative: string): string {
return readFileSync(join(__dirname, "..", relative), "utf8");
}
/**
* Strip comments before matching. Every one of these files DESCRIBES the
* write it no longer performs, and a guard that reads prose as code is a
* guard that fires on its own documentation.
*/
function code(source: string): string {
return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/.*$/gm, "");
}
describe("read routes issue no writes", () => {
it("the patterns actually match a write — otherwise every case below is vacuous", () => {
expect(
PRISMA_WRITE.test(`await prisma.user.update({ where: { id } });`),
).toBe(true);
expect(
PRISMA_WRITE.test(`prisma.userAchievement.createMany({ data });`),
).toBe(true);
expect(WRITE_HELPERS.test(`void persistPredictionCache(userId, p);`)).toBe(
true,
);
// And do NOT match the reads these routes are made of.
expect(PRISMA_WRITE.test(`await prisma.measurement.findMany({});`)).toBe(
false,
);
expect(PRISMA_WRITE.test(`await prisma.moodEntry.count({});`)).toBe(false);
});
it("scans all three routes and finds real source in each", () => {
// An empty read set would pass every assertion below without looking at
// anything.
expect(ROUTES).toHaveLength(3);
for (const route of ROUTES) {
expect(code(sourceOf(route)).length).toBeGreaterThan(500);
}
});
for (const route of ROUTES) {
it(`${route} calls no mutating Prisma method`, () => {
expect(code(sourceOf(route))).not.toMatch(PRISMA_WRITE);
});
it(`${route} calls no known write helper`, () => {
expect(code(sourceOf(route))).not.toMatch(WRITE_HELPERS);
});
}
});