diff --git a/pm-stack/aggregator/src/snapshots.ts b/pm-stack/aggregator/src/snapshots.ts index 4d62a2afa8..d864e72728 100644 --- a/pm-stack/aggregator/src/snapshots.ts +++ b/pm-stack/aggregator/src/snapshots.ts @@ -99,7 +99,9 @@ export function isSnapshotRollup(v: unknown): v is SnapshotRollup { if (typeof r.metricId !== "string" || r.metricId === "") return false; if (typeof r.unit !== "string" || !UNITS.has(r.unit)) return false; if (typeof r.source !== "string" || !SOURCES.has(r.source)) return false; - if (typeof r.count !== "number" || !(r.count >= 1)) return false; + // count is a record tally: fractional or infinite counts are malformed + // and must not seed readAll/rollupKeys/compaction with corrupt totals. + if (typeof r.count !== "number" || !Number.isInteger(r.count) || r.count < 1) return false; if (typeof r.observedBy !== "string" || r.observedBy === "") return false; for (const f of [r.firstTs, r.lastTs, r.minTs, r.maxTs]) { if (typeof f !== "string" || Number.isNaN(Date.parse(f))) return false; diff --git a/pm-stack/aggregator/test/snapshot-retention.test.ts b/pm-stack/aggregator/test/snapshot-retention.test.ts index 17f1b94082..910c1d3722 100644 --- a/pm-stack/aggregator/test/snapshot-retention.test.ts +++ b/pm-stack/aggregator/test/snapshot-retention.test.ts @@ -12,6 +12,7 @@ import { SnapshotStore, compactRecordsToRollups, expandRollup, + isSnapshotRollup, rollupFileFor, shiftMonth, withSnapshotStoreLock, @@ -387,6 +388,60 @@ describe("Codex review regressions (PR #2379)", () => { }); }); +describe("isSnapshotRollup count gate (#3443)", () => { + function validRollup(count: unknown): Record { + return { + kind: "snapshot-rollup/v1", + month: "2025-06", + serviceId: "svc-a", + metricId: "weekly", + unit: "pct", + source: "api", + observedBy: "test", + count, + firstTs: "2025-06-03T12:00:00Z", + lastTs: "2025-06-20T12:00:00Z", + valueMin: 10, + minTs: "2025-06-20T12:00:00Z", + valueMax: 85, + maxTs: "2025-06-10T12:00:00Z", + valueLast: 10, + keys: [], + }; + } + + test("accepts positive integer counts", () => { + expect(isSnapshotRollup(validRollup(3))).toBe(true); + expect(isSnapshotRollup(validRollup(1))).toBe(true); + }); + + test("rejects fractional, infinite, and NaN counts", () => { + for (const bad of [1.5, Number.POSITIVE_INFINITY, JSON.parse("1e999"), Number.NaN, 0, -2]) { + expect(isSnapshotRollup(validRollup(bad))).toBe(false); + } + }); + + test("stored rollup lines with fractional or 1e999 counts are dropped by readAll and rollupKeys", async () => { + const dir = await mkdtemp(join(tmpdir(), "snap-retention-")); + try { + const fractional = JSON.stringify(validRollup(1.5)); + const infinite = JSON.stringify(validRollup(3)).replace('"count":3', '"count":1e999'); + await writeFile(join(dir, rollupFileFor("2025-06")), fractional + "\n" + infinite + "\n"); + await writeFile( + join(dir, "usage-2026-10.jsonl"), + JSON.stringify(rec("2026-10-05T12:00:00Z", "svc-a", "weekly", 12)) + "\n", + ); + const store = new SnapshotStore(dir); + const all = await store.readAll(); + expect(all).toHaveLength(1); + expect(all[0].value).toBe(12); + expect(await store.rollupKeys()).toHaveLength(0); + } finally { + await rm(dir, { recursive: true, force: true }); + } + }); +}); + describe("withSnapshotStoreLock stale reclamation (PR #2379 Codex round 2)", () => { test("concurrent reclaimers of an abandoned lock never overlap critical sections", async () => { // Regression: reclamation used to rm the lock path unconditionally,