diff --git a/pm-stack/aggregator/src/activity.ts b/pm-stack/aggregator/src/activity.ts index 9b65d67a8a..5b5f5f55c0 100644 --- a/pm-stack/aggregator/src/activity.ts +++ b/pm-stack/aggregator/src/activity.ts @@ -39,6 +39,7 @@ import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; import { dirname } from "node:path"; import type { EventsStore } from "./events.ts"; import type { TierItem } from "./model.ts"; +import { isIsoDate, isNonNegative } from "./validate.ts"; export type ActivitySourceId = "watchdog" | "kaneo" | "github"; @@ -301,10 +302,11 @@ function isGapRecord(v: unknown): v is GapRecord { return ( typeof g.id === "string" && typeof g.source === "string" && - typeof g.from === "string" && - typeof g.to === "string" && + isIsoDate(g.from) && + isIsoDate(g.to) && (g.status === "open" || g.status === "backfilled" || g.status === "unrecoverable") && - typeof g.detectedAt === "string" + isIsoDate(g.detectedAt) && + (g.resolvedAt === undefined || isIsoDate(g.resolvedAt)) ); } @@ -314,10 +316,12 @@ function isCheckpoint(v: unknown): v is SourceCheckpoint { return ( typeof c.source === "string" && (c.lastSourceId === null || typeof c.lastSourceId === "string") && - (c.lastEventAt === null || typeof c.lastEventAt === "string") && + (c.lastEventAt === null || isIsoDate(c.lastEventAt)) && + isNonNegative(c.deliveries) && Number.isInteger(c.deliveries) && + isNonNegative(c.duplicates) && Number.isInteger(c.duplicates) && - typeof c.updatedAt === "string" + isIsoDate(c.updatedAt) ); } diff --git a/pm-stack/aggregator/test/activity-3321.test.ts b/pm-stack/aggregator/test/activity-3321.test.ts index 3ab6d1d43b..a03f9c5809 100644 --- a/pm-stack/aggregator/test/activity-3321.test.ts +++ b/pm-stack/aggregator/test/activity-3321.test.ts @@ -4,7 +4,7 @@ // backfill flips. GitHub stays unsupported pending #2072 (asserted in // the coverage table so the gap is documented, not implied away). import { describe, expect, test } from "bun:test"; -import { mkdtempSync } from "node:fs"; +import { existsSync, mkdtempSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { @@ -32,6 +32,50 @@ function tmpStores(): { eventsFile: string; activityFile: string; events: Events return { eventsFile, activityFile, events: new EventsStore(eventsFile), store: new IngestionStore(activityFile) }; } +function validIngestionFile() { + return { + version: 1, + checkpoints: { + kaneo: { + source: "kaneo", + lastSourceId: "kaneo:card-1", + lastEventAt: "2024-02-29T10:00:00.000Z", + deliveries: 2, + duplicates: 1, + updatedAt: "2024-02-29T10:00:00.000Z", + }, + watchdog: { + source: "watchdog", + lastSourceId: null, + lastEventAt: null, + deliveries: 0, + duplicates: 0, + updatedAt: "2024-02-29T10:00:00.000Z", + }, + }, + seen: { kaneo: ["kaneo:card-1"] }, + gaps: [ + { + id: "gap:kaneo:2024-02-29", + source: "kaneo", + from: "2024-02-29T10:00:00.000Z", + to: "2024-03-01T10:00:00.000Z", + status: "backfilled" as const, + detectedAt: "2024-02-29T10:00:00.000Z", + resolvedAt: "2024-03-01T10:00:00.000Z", + }, + { + id: "gap:watchdog:2024-03-02", + source: "watchdog", + from: "2024-03-02T10:00:00.000Z", + to: "2024-03-03T10:00:00.000Z", + status: "open" as const, + detectedAt: "2024-03-02T10:00:00.000Z", + }, + ], + }; +} + const iso = (msAgo: number) => new Date(Date.now() - msAgo).toISOString(); function watchdogItem(id: string, sinceMsAgo: number, title = "Watchdog: fleet feed is stale"): TierItem { @@ -344,6 +388,83 @@ describe("ingestion store (#3321)", () => { }); }); +describe("ingestion store load validation (#5232)", () => { + const invalidRecords: Array<{ + description: string; + record: "gap" | "checkpoint"; + patch: Record; + }> = [ + { description: "impossible gap start", record: "gap", patch: { from: "2026-02-30T00:00:00.000Z" } }, + { description: "impossible gap end", record: "gap", patch: { to: "2026-04-31T00:00:00.000Z" } }, + { description: "non-ISO gap detection date", record: "gap", patch: { detectedAt: "March 2, 2026" } }, + { description: "garbage gap date", record: "gap", patch: { from: "not-a-date" } }, + { + description: "impossible gap resolution date", + record: "gap", + patch: { resolvedAt: "2026-02-30T00:00:00.000Z" }, + }, + { + description: "impossible checkpoint last-event date", + record: "checkpoint", + patch: { lastEventAt: "2026-02-30T00:00:00.000Z" }, + }, + { + description: "non-ISO checkpoint update date", + record: "checkpoint", + patch: { updatedAt: "March 2, 2026" }, + }, + { description: "garbage checkpoint date", record: "checkpoint", patch: { updatedAt: "not-a-date" } }, + { description: "negative deliveries count", record: "checkpoint", patch: { deliveries: -1 } }, + { description: "negative duplicates count", record: "checkpoint", patch: { duplicates: -2 } }, + ]; + + test.each(invalidRecords)("quarantines $description and starts empty", async ({ record, patch }) => { + const dir = mkdtempSync(join(tmpdir(), "agg-activity-invalid-")); + const file = join(dir, "activity.json"); + const persisted = validIngestionFile(); + Object.assign(record === "gap" ? persisted.gaps[0]! : persisted.checkpoints.kaneo, patch); + const raw = JSON.stringify(persisted); + writeFileSync(file, raw); + + const errors: string[] = []; + const origError = console.error; + console.error = (...args: unknown[]) => { + errors.push(args.map(String).join(" ")); + }; + try { + const store = new IngestionStore(file); + expect(await store.checkpoints()).toEqual([]); + expect(await store.seenIds("kaneo")).toEqual([]); + expect(await store.gaps()).toEqual([]); + } finally { + console.error = origError; + } + + expect( + errors.some((message) => message.includes("is unparseable") && message.includes("quarantined the original bytes")), + ).toBe(true); + const quarantined = readdirSync(dir).filter((name) => name.startsWith("activity.json.corrupt-")); + expect(quarantined).toHaveLength(1); + expect(readFileSync(join(dir, quarantined[0]!), "utf8")).toBe(raw); + expect(existsSync(file)).toBe(false); + }); + + test("valid ISO dates and counts load unchanged without quarantine", async () => { + const dir = mkdtempSync(join(tmpdir(), "agg-activity-valid-")); + const file = join(dir, "activity.json"); + const persisted = validIngestionFile(); + const raw = JSON.stringify(persisted); + writeFileSync(file, raw); + + const store = new IngestionStore(file); + expect(await store.checkpoints()).toEqual([persisted.checkpoints.kaneo, persisted.checkpoints.watchdog]); + expect(await store.seenIds("kaneo")).toEqual(["kaneo:card-1"]); + expect(await store.gaps()).toEqual(persisted.gaps); + expect(readFileSync(file, "utf8")).toBe(raw); + expect(readdirSync(dir).filter((name) => name.startsWith("activity.json.corrupt-"))).toEqual([]); + }); +}); + describe("activity summary caps never split a surrogate pair (#7725)", () => { const roundTrips = (s: string) => new TextDecoder().decode(new TextEncoder().encode(s)) === s;