fix(workouts): keep a deleted workout's insight slot spent

The insight claim row is now the daily-cap ledger. Its workout_id is
nullable with ON DELETE SET NULL (migration 0367), so deleting a workout
no longer frees the day's slot. The cap counts claims that reached the
provider plus live leases, and drops the separate count of insight rows.
A daily workout-insight-claim-cleanup queue removes claim rows untouched
for seven days.
This commit is contained in:
Marc-André Bombeck committed 2026-10-03 17:53:36 +02:00
1 parent 556bd6d6cd
commit ab081d2a85
11 files changed
+324 -135

No files matched your search

+1 -1
View File
@@ -579,7 +579,7 @@ export const DERIVED_MODELS: Readonly<Record<string, string>> = {
WorkoutInsight:
"Generated narrative over `Workout`; regenerates on demand. Carrying it would restore an interpretation of data rather than the data.",
WorkoutInsightGenerationClaim:
"A concurrency claim guarding the generator above. Meaningless outside the run that took it.",
"A concurrency claim and the daily-cap ledger for the generator above. It outlives a deleted workout for at most a week and means nothing on another instance or day.",
InsightNarrative:
"Generated prose over measurements. Same reasoning as WorkoutInsight — the record restores, the essay about it does not.",
InsightStatusCache:
@@ -54,7 +54,10 @@ vi.mock("@/lib/measurements/pick-canonical-workout-rows", () => ({
pickCanonicalWorkoutRows: vi.fn((rows) => rows),
}));
import { runWorkoutInsightGenerate } from "../workout-insight-generate";
import {
runWorkoutInsightGenerate,
workoutInsightCapWhere,
} from "../workout-insight-generate";
import { prisma } from "@/lib/db";
import { aiCapabilityForJob } from "@/lib/ai/capabilities/gate";
import { resolveUserTimezone } from "@/lib/tz/resolver";
@@ -224,8 +227,10 @@ describe("Activity Insight — the gate stack", () => {
expect(outcome.status).toBe("generated");
});
it("gate 3 — refuses once four paragraphs exist for the local day", async () => {
vi.mocked(prisma.workoutInsight.count).mockResolvedValue(4 as never);
it("gate 3 — refuses once four slots are spent for the local day", async () => {
vi.mocked(prisma.workoutInsightGenerationClaim.count).mockResolvedValue(
4 as never,
);
const outcome = await runWorkoutInsightGenerate(
{ userId: USER, workoutId: WORKOUT },
@@ -237,36 +242,33 @@ describe("Activity Insight — the gate stack", () => {
});
it("gate 3 — counts in the USER's timezone, not UTC", async () => {
// 00:30 Berlin on the 19th is 22:30 UTC on the 18th. A UTC-keyed window
// would still be counting the 18th's paragraphs against the new local day.
// 00:30 Berlin on the 19th is 22:30 UTC on the 18th. A UTC-keyed day
// would still be counting the 18th's claims against the new local day.
vi.mocked(resolveUserTimezone).mockResolvedValue("Europe/Berlin");
await runWorkoutInsightGenerate(
{ userId: USER, workoutId: WORKOUT },
new Date("2026-07-18T22:30:00.000Z"),
);
const where = vi.mocked(prisma.workoutInsight.count).mock.calls[0][0]
?.where as { generatedAt: { gte: Date } };
// Midnight Berlin on the 19th == 22:00 UTC on the 18th.
expect(where.generatedAt.gte.toISOString()).toBe(
"2026-07-18T22:00:00.000Z",
);
const where = vi.mocked(prisma.workoutInsightGenerationClaim.count).mock
.calls[0][0]?.where as { localDate: string };
expect(where.localDate).toBe("2026-07-19");
});
it("gate 3 — starts a midday cap window at exact local midnight", async () => {
vi.mocked(resolveUserTimezone).mockResolvedValue("Europe/Berlin");
vi.mocked(prisma.workoutInsight.count).mockResolvedValue(4 as never);
it("gate 3 — the cap reads the claim ledger, never the insight rows", async () => {
// A paragraph whose workout was deleted is gone from `workout_insights`,
// so counting those rows would hand the slot back. Only the claims count.
vi.mocked(prisma.workoutInsightGenerationClaim.count).mockResolvedValue(
4 as never,
);
vi.mocked(prisma.workoutInsight.count).mockResolvedValue(0 as never);
const outcome = await runWorkoutInsightGenerate(
{ userId: USER, workoutId: WORKOUT },
new Date("2026-07-18T15:00:45.678Z"),
);
const where = vi.mocked(prisma.workoutInsight.count).mock.calls[0][0]
?.where as { generatedAt: { gte: Date } };
expect(where.generatedAt.gte.toISOString()).toBe(
"2026-07-17T22:00:00.000Z",
);
expect(prisma.workoutInsight.count).not.toHaveBeenCalled();
expect(outcome).toEqual({ status: "skipped", reason: "daily_cap" });
expect(runStatusCompletion).not.toHaveBeenCalled();
});
@@ -304,7 +306,9 @@ describe("Activity Insight — the gate stack", () => {
vi.clearAllMocks();
arrangeHappyPath();
vi.mocked(prisma.workoutInsight.count).mockResolvedValue(0 as never);
vi.mocked(prisma.workoutInsightGenerationClaim.count).mockResolvedValue(
0 as never,
);
vi.mocked(prisma.workoutInsight.findFirst).mockResolvedValue({
id: "wi-1",
inputHash: stored.inputHash,
@@ -321,7 +325,9 @@ describe("Activity Insight — the gate stack", () => {
// The mirror of the test above: a fresh workout (no stored row, so the hash
// gate cannot fire) is still refused once the day's count is spent.
vi.mocked(prisma.workoutInsight.findFirst).mockResolvedValue(null as never);
vi.mocked(prisma.workoutInsight.count).mockResolvedValue(4 as never);
vi.mocked(prisma.workoutInsightGenerationClaim.count).mockResolvedValue(
4 as never,
);
const outcome = await runWorkoutInsightGenerate(
{ userId: USER, workoutId: WORKOUT },
@@ -660,3 +666,37 @@ describe("Activity Insight — own-history comparison", () => {
expect(where.id).toEqual({ not: WORKOUT });
});
});
describe("Activity Insight — the cap predicate", () => {
const staleBefore = new Date("2026-07-18T14:50:00.000Z");
it("counts every provider attempt and every live lease, nothing narrower", () => {
const where = workoutInsightCapWhere({
userId: USER,
localDate: "2026-07-18",
staleBefore,
});
expect(where).toEqual({
userId: USER,
localDate: "2026-07-18",
OR: [
{ providerInvokedAt: { not: null } },
{ claimId: { not: null }, claimedAt: { gte: staleBefore } },
],
});
// A completed claim whose workout is gone is still a spent slot: the
// predicate must not narrow on the workout or on completion.
expect(JSON.stringify(where)).not.toContain("workoutId");
expect(JSON.stringify(where)).not.toContain("completedAt");
});
it("leaves the reclaiming row out of its own count", () => {
const where = workoutInsightCapWhere({
userId: USER,
localDate: "2026-07-18",
staleBefore,
excludeClaimRowId: "claim-row-1",
});
expect(where).toMatchObject({ id: { not: "claim-row-1" } });
});
});
+1
View File
@@ -291,6 +291,7 @@ export const QUEUE_RUNTIME: Readonly<Record<string, QueueRuntime>> = {
"idempotency-cleanup": short("One retention DELETE."),
"step-up-elevation-cleanup": short("One retention DELETE."),
"coach-message-cleanup": short("One retention DELETE."),
"workout-insight-claim-cleanup": short("One retention DELETE."),
"mcp-token-cleanup": short("One retention DELETE."),
"audit-log-cleanup": short("At most 40 batches of 5 000 rows per leg."),
"measurement-tombstone-cleanup": short(
+25
View File
@@ -31,6 +31,7 @@ import { cleanupExpiredIdempotencyKeys } from "@/lib/jobs/idempotency-cleanup";
import { cleanupExpiredMcpTokens } from "@/lib/jobs/mcp-token-cleanup";
import { cleanupOldAuditLogs } from "@/lib/jobs/audit-log-cleanup";
import { cleanupOldCoachMessages } from "@/lib/jobs/coach-message-cleanup";
import { cleanupOldWorkoutInsightClaims } from "@/lib/jobs/workout-insight-claim-cleanup";
import { cleanupExpiredWithingsOAuthStates } from "@/lib/jobs/withings-oauth-state-cleanup";
import { cleanupExpiredOidcNativeHandoffs } from "@/lib/jobs/oidc-handoff-cleanup";
import {
@@ -395,6 +396,30 @@ export async function handleCoachMessageCleanup(
});
}
export interface WorkoutInsightClaimCleanupPayload {
triggeredAt: string;
}
export async function handleWorkoutInsightClaimCleanup(
jobs: Job<WorkoutInsightClaimCleanupPayload>[],
): Promise<JobOutcome> {
void jobs;
return withBackgroundEvent(
"job.workout_insight_claim_cleanup",
async (evt) => {
const p = getWorkerPrisma();
try {
const deleted = await cleanupOldWorkoutInsightClaims(p);
evt.addMeta("workout_insight_claim_cleanup_deleted", deleted);
return jobDone({ deleted });
} catch (err) {
evt.addWarning(`workout-insight-claim-cleanup failed: ${err}`);
return jobFailed("workout-insight-claim cleanup failed", err);
}
},
);
}
export interface McpTokenCleanupPayload {
triggeredAt: string;
}
@@ -189,6 +189,8 @@ import {
handlePushAttemptCleanup,
ArrivalReactionCleanupPayload,
handleArrivalReactionCleanup,
WorkoutInsightClaimCleanupPayload,
handleWorkoutInsightClaimCleanup,
MeasurementTombstoneCleanupPayload,
handleMeasurementTombstoneCleanup,
handleRateLimitCleanup,
@@ -330,6 +332,13 @@ const MEASUREMENT_TOMBSTONE_CLEANUP_CRON = "40 3 * * *";
const COACH_MESSAGE_CLEANUP_QUEUE = "coach-message-cleanup";
const COACH_MESSAGE_CLEANUP_CRON = "50 3 * * *";
// v1.40 — daily prune of the workout-insight claim ledger. Claims outlive a
// deleted workout so the delete cannot free a daily slot; rows older than a
// week are never read by the cap again.
const WORKOUT_INSIGHT_CLAIM_CLEANUP_QUEUE = "workout-insight-claim-cleanup";
const WORKOUT_INSIGHT_CLAIM_CLEANUP_CRON = "52 3 * * *";
// v1.23 — converging backfill that migrates the free-text health-note columns
// (mood + measurement) from plaintext to AES-256-GCM at rest. Boot discovery
// enqueues one per-user job; a daily 03:55 Europe/Berlin discovery tick (empty
@@ -445,6 +454,10 @@ const allQueues = [
// the daily schedule silently no-ops and the encrypted coach_messages
// table grows unbounded.
COACH_MESSAGE_CLEANUP_QUEUE,
// v1.40 — workout-insight claim ledger retention prune. Without this entry
// the daily schedule silently no-ops and the claim table keeps every row
// whose workout was deleted.
WORKOUT_INSIGHT_CLAIM_CLEANUP_QUEUE,
// v1.23 — free-text health-note encryption backfill. Boot discovery + a
// daily discovery cron enqueue one per-user job per account still holding a
// plaintext note; without this entry pg-boss never provisions the queue and
@@ -605,6 +618,12 @@ const schedules: ScheduleEntry[] = [
[MEASUREMENT_TOMBSTONE_CLEANUP_QUEUE, MEASUREMENT_TOMBSTONE_CLEANUP_CRON],
// v1.18.7 — daily 03:50 Europe/Berlin prune for stale Coach history.
[COACH_MESSAGE_CLEANUP_QUEUE, COACH_MESSAGE_CLEANUP_CRON, cronIsTheRetry],
// v1.40 — daily 03:52 Europe/Berlin prune for the workout-insight claims.
[
WORKOUT_INSIGHT_CLAIM_CLEANUP_QUEUE,
WORKOUT_INSIGHT_CLAIM_CLEANUP_CRON,
cronIsTheRetry,
],
// v1.23 — daily 03:55 Europe/Berlin note-encryption backfill discovery.
// The empty cron payload (no `userId`) is the handler's signal to fan out
// one per-user job per account still holding a plaintext note.
@@ -918,6 +937,14 @@ export async function registerMaintenanceQueues(
{ localConcurrency: 1 },
handleCoachMessageCleanup,
);
// v1.40 — daily prune of the workout-insight claim ledger. Single-flight
// like every other cleanup queue.
await createAndWork<WorkoutInsightClaimCleanupPayload>(
boss,
WORKOUT_INSIGHT_CLAIM_CLEANUP_QUEUE,
{ localConcurrency: 1 },
handleWorkoutInsightClaimCleanup,
);
// MCP Phase 3 (M2) — daily prune of dead MCP connector access tokens +
// long-revoked connection anchors. Single-flight like every other cleanup.
await createAndWork<McpTokenCleanupPayload>(
+1
View File
@@ -124,6 +124,7 @@ const subsystemSurface = {
"achievement-unlock-sweep": { audience: "system" },
"measurement-tombstone-cleanup": { audience: "system" },
"coach-message-cleanup": { audience: "system" },
"workout-insight-claim-cleanup": { audience: "system" },
"note-encryption-backfill": { audience: "system" },
"encryption-key-rotate": { audience: "system" },
"mcp-token-cleanup": { audience: "system" },
@@ -0,0 +1,30 @@
/**
* Daily retention for `workout_insight_generation_claims`.
*
* The claim row is the ledger behind the per-workout insight daily cap, and it
* outlives its workout on purpose: deleting a workout nulls the claim's
* `workout_id` instead of removing the row, so the delete cannot free the
* day's slot. The cap only ever reads today's rows, so anything older than the
* retention window is dead weight and is dropped here.
*
* Keyed on `updated_at`, which moves on every claim, reclaim, provider
* invocation and completion. A row written within the window is kept whatever
* its `local_date` says, so no row that the cap can still read is ever
* deleted.
*/
import type { PrismaClient } from "@/generated/prisma/client";
export const WORKOUT_INSIGHT_CLAIM_RETENTION_DAYS = 7;
export async function cleanupOldWorkoutInsightClaims(
prisma: Pick<PrismaClient, "workoutInsightGenerationClaim">,
now: Date = new Date(),
): Promise<number> {
const cutoff = new Date(
now.getTime() - WORKOUT_INSIGHT_CLAIM_RETENTION_DAYS * 86_400_000,
);
const { count } = await prisma.workoutInsightGenerationClaim.deleteMany({
where: { updatedAt: { lt: cutoff } },
});
return count;
}
+41 -18
View File
@@ -55,7 +55,6 @@ import { withBackgroundEvent } from "@/lib/logging/background";
import { aiCapabilityForJob } from "@/lib/ai/capabilities/gate";
import { userDayKey } from "@/lib/tz/format";
import { resolveUserTimezone } from "@/lib/tz/resolver";
import { startOfLocalDayInTz } from "@/lib/tz/local-day";
import { buildWorkoutHrSeries } from "@/lib/workouts/hr-series";
import {
buildWorkoutInsightEvidence,
@@ -98,10 +97,42 @@ type ClaimWorkoutInsightGenerationArgs = {
userId: string;
workoutId: string;
localDate: string;
dayStart: Date;
now: Date;
};
/**
* Which claim rows spend one of the day's slots.
*
* The claim row is the ledger. A slot is spent by every attempt that reached
* the provider (completed or not, and whether or not its workout still exists:
* deleting a workout nulls the claim's `workout_id` instead of removing the
* row, so the delete cannot buy the slot back) plus every live lease that may
* still reach the provider. A lease past its expiry that never invoked the
* provider spent nothing and is reclaimable. Rows older than a week are
* dropped by `workout-insight-claim-cleanup`.
*/
export function workoutInsightCapWhere({
userId,
localDate,
staleBefore,
excludeClaimRowId,
}: {
userId: string;
localDate: string;
staleBefore: Date;
excludeClaimRowId?: string;
}) {
return {
userId,
localDate,
...(excludeClaimRowId ? { id: { not: excludeClaimRowId } } : {}),
OR: [
{ providerInvokedAt: { not: null } },
{ claimId: { not: null }, claimedAt: { gte: staleBefore } },
],
};
}
/**
* Serialize the local-day capacity check with the durable claim insert.
* PostgreSQL advisory transaction locks keep the count and write atomic across
@@ -112,7 +143,6 @@ export async function claimWorkoutInsightGeneration({
userId,
workoutId,
localDate,
dayStart,
now,
}: ClaimWorkoutInsightGenerationArgs): Promise<WorkoutInsightClaimResult> {
const claimId = randomUUID();
@@ -153,20 +183,15 @@ export async function claimWorkoutInsightGeneration({
return { status: "skipped", reason: "already_claimed" };
}
const [generatedToday, claimedToday] = await Promise.all([
tx.workoutInsight.count({
where: { userId, generatedAt: { gte: dayStart } },
const spentToday = await tx.workoutInsightGenerationClaim.count({
where: workoutInsightCapWhere({
userId,
localDate,
staleBefore,
excludeClaimRowId: existing?.id,
}),
tx.workoutInsightGenerationClaim.count({
where: {
userId,
localDate,
completedAt: null,
...(existing ? { workoutId: { not: workoutId } } : {}),
},
}),
]);
if (generatedToday + claimedToday >= MAX_INSIGHTS_PER_DAY) {
});
if (spentToday >= MAX_INSIGHTS_PER_DAY) {
return { status: "skipped", reason: "daily_cap" };
}
@@ -270,13 +295,11 @@ export async function runWorkoutInsightGenerate(
// The capacity check and durable claim write are one serialized PostgreSQL
// transaction. A count performed before the write would let concurrent
// workouts all observe the same free slot and exceed the cap.
const dayStart = startOfLocalDayInTz(now, tz);
const localDate = userDayKey(now, tz);
const claim = await claimWorkoutInsightGeneration({
userId,
workoutId,
localDate,
dayStart,
now,
});
if (claim.status === "skipped") {