diff --git a/pm-stack/aggregator/src/snapshots.ts b/pm-stack/aggregator/src/snapshots.ts index 1fad80bf63..ca24c02b6d 100644 --- a/pm-stack/aggregator/src/snapshots.ts +++ b/pm-stack/aggregator/src/snapshots.ts @@ -296,6 +296,10 @@ export function isSnapshotRecord(v: unknown): v is SnapshotRecord { if (typeof v !== "object" || v === null) return false; const r = v as Partial; if (typeof r.ts !== "string" || Number.isNaN(Date.parse(r.ts))) return false; + // The store routes by ts.slice(0, 7) and readAll only reads + // usage-YYYY-MM.jsonl (or rollups): a ts that does not route to such + // a file would be written where readAll can never return it. + if (!FILE_RE.test(snapshotFileFor(r.ts))) return false; if (typeof r.serviceId !== "string" || r.serviceId === "") return false; if (typeof r.metricId !== "string" || r.metricId === "") return false; if (typeof r.unit !== "string" || !UNITS.has(r.unit)) return false; @@ -304,7 +308,16 @@ export function isSnapshotRecord(v: unknown): v is SnapshotRecord { if (typeof r.value === "number") return Number.isFinite(r.value); if (typeof r.value === "object" && r.value !== null) { const p = r.value as { low?: unknown; high?: unknown }; - return typeof p.low === "number" && typeof p.high === "number"; + // Pairs must round-trip: non-finite endpoints serialize to null + // and readAll drops them, and an inverted pair is never produced + // by the writers — mirror the scalar branch's finiteness. + return ( + typeof p.low === "number" && + typeof p.high === "number" && + Number.isFinite(p.low) && + Number.isFinite(p.high) && + p.low <= p.high + ); } return false; } diff --git a/pm-stack/aggregator/test/snapshot-writers.test.ts b/pm-stack/aggregator/test/snapshot-writers.test.ts index c0e2b1b88d..20cda431c9 100644 --- a/pm-stack/aggregator/test/snapshot-writers.test.ts +++ b/pm-stack/aggregator/test/snapshot-writers.test.ts @@ -252,6 +252,61 @@ describe("DeepSeek CNY balance (#1052)", () => { }); }); +describe("unreadable records are rejected (#1540)", () => { + const base = { + serviceId: "cloudflare", + metricId: "gateway-accrual-est", + unit: "usd-est", + source: "estimate", + observedBy: "stewardship-cycle", + ts: "2026-10-26T12:00:00Z", + } as const; + + test("non-finite and inverted pairs never validate", () => { + expect(isSnapshotRecord({ ...base, value: { low: NaN, high: 10 } })).toBe(false); + expect(isSnapshotRecord({ ...base, value: { low: Number.POSITIVE_INFINITY, high: 10 } })).toBe(false); + expect(isSnapshotRecord({ ...base, value: { low: 1, high: Number.NEGATIVE_INFINITY } })).toBe(false); + expect(isSnapshotRecord({ ...base, value: { low: 50, high: 10 } })).toBe(false); + }); + + test("a ts outside the usage-YYYY-MM routing never validates", () => { + // Date.parse accepts it, but snapshotFileFor routes it to + // usage-October.jsonl, which readAll never reads. + expect(isSnapshotRecord({ ...base, ts: "October 20, 2026", value: { low: 1, high: 2 } })).toBe(false); + }); + + test("unreadable records append nothing and write no files", async () => { + const dir = await mkdtemp(join(tmpdir(), "snap-unreadable-")); + try { + const store = new SnapshotStore(dir); + const bad = [ + { ...base, value: { low: NaN, high: 10 } }, + { ...base, value: { low: Number.POSITIVE_INFINITY, high: 10 } }, + { ...base, value: { low: 50, high: 10 } }, + { ...base, ts: "October 20, 2026", value: { low: 1, high: 2 } }, + ]; + expect(await store.append(bad as never)).toBe(0); + expect(await store.readAll()).toEqual([]); + } finally { + await rm(dir, { recursive: true, force: true }); + } + }); + + test("the finite-pair ISO control still appends once and dedupes", async () => { + const dir = await mkdtemp(join(tmpdir(), "snap-pair-control-")); + try { + const store = new SnapshotStore(dir); + const rec = { ...base, value: { low: 29.6, high: 45.1 } }; + expect(isSnapshotRecord(rec)).toBe(true); + expect(await store.append([rec] as never)).toBe(1); + expect(await store.append([rec] as never)).toBe(0); + expect(await store.readAll()).toHaveLength(1); + } finally { + await rm(dir, { recursive: true, force: true }); + } + }); +}); + describe("writer dedupe invariant", () => { test("re-appending a derived batch appends nothing the second time", async () => { const dir = await mkdtemp(join(tmpdir(), "snap-writers-"));