From 7d0abb7e1a4be18b1e6dd00bd56799a17e9c362e Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 23:48:35 -0600 Subject: [PATCH] fix(aggregator): entryFromTurso omits negative usage metrics (#2553) A negative Turso usage count passed the finiteness-only num() gate and published as an ok window with negative value/pctOfLimit, persisted as negative snapshot records. Per the function's documented philosophy, a negative count is treated exactly like a metric the response does not carry: the five usage reads in entryFromTurso go through a local numNonNegative guard (negative -> null -> window omitted). The shared num() is unchanged for the other mappers. All-negative reads fall into the existing no-known-metrics error entry. Regression tests: all- negative -> error entry/no records; mixed -> negative metrics omitted, rest unchanged, no negative records. Queue task 5364. --- pm-stack/aggregator/src/quota-gather-map.ts | 19 +++++++--- .../aggregator/test/quota-gather-map.test.ts | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/pm-stack/aggregator/src/quota-gather-map.ts b/pm-stack/aggregator/src/quota-gather-map.ts index 5123b62f7a..cb2d970048 100644 --- a/pm-stack/aggregator/src/quota-gather-map.ts +++ b/pm-stack/aggregator/src/quota-gather-map.ts @@ -426,6 +426,15 @@ export const TURSO_SOURCE = "turso-api"; const gb = (bytes: number): number => Math.round((bytes / 1e9) * 1e6) / 1e6; const pct3 = (used: number, limit: number): number => Math.round((used / limit) * 100 * 1000) / 1000; +/** Turso usage counts are counts: a negative value is impossible, so it is + * treated exactly like a metric the response does not carry (#2553) — the + * window is omitted, never published negative. Local to entryFromTurso: + * the shared num() keeps its semantics for the other mappers. */ +const numNonNegative = (v: unknown): number | null => { + const n = num(v); + return n !== null && n < 0 ? null : n; +}; + /** * Map one Turso usage + subscription read onto a quota-current * entry (serviceId "turso", the SaaS card's snapshot id, so the @@ -486,9 +495,9 @@ export function entryFromTurso( state: "ok", }); }; - const databases = num(usage.databases); + const databases = numNonNegative(usage.databases); push("databases", databases, TURSO_LIMITS.databases, databases ?? 0, `${databases} / ${TURSO_LIMITS.databases}`); - const storageBytes = num(usage.storage_bytes); + const storageBytes = numNonNegative(usage.storage_bytes); push( "storage", storageBytes, @@ -496,7 +505,7 @@ export function entryFromTurso( storageBytes == null ? 0 : gb(storageBytes), `${storageBytes == null ? "?" : gb(storageBytes)} GB / 5 GB`, ); - const rowsRead = num(usage.rows_read); + const rowsRead = numNonNegative(usage.rows_read); push( "rows-read", rowsRead, @@ -504,7 +513,7 @@ export function entryFromTurso( rowsRead ?? 0, `${rowsRead?.toLocaleString("en-US")} / ${TURSO_LIMITS.rowsRead.toLocaleString("en-US")}`, ); - const rowsWritten = num(usage.rows_written); + const rowsWritten = numNonNegative(usage.rows_written); push( "rows-written", rowsWritten, @@ -512,7 +521,7 @@ export function entryFromTurso( rowsWritten ?? 0, `${rowsWritten?.toLocaleString("en-US")} / ${TURSO_LIMITS.rowsWritten.toLocaleString("en-US")}`, ); - const bytesSynced = num(usage.bytes_synced); + const bytesSynced = numNonNegative(usage.bytes_synced); push( "sync", bytesSynced, diff --git a/pm-stack/aggregator/test/quota-gather-map.test.ts b/pm-stack/aggregator/test/quota-gather-map.test.ts index 62f8fb5ad2..eacedc373b 100644 --- a/pm-stack/aggregator/test/quota-gather-map.test.ts +++ b/pm-stack/aggregator/test/quota-gather-map.test.ts @@ -485,6 +485,42 @@ describe("entryFromTurso (#1049)", () => { }); }); +describe("entryFromTurso negative usage (#2553)", () => { + const subDoc = { + subscription: { plan: "starter", name: "starter", current_billing_period_start: "2026-10-01T04:00:00+00:00", current_billing_period_end: "2026-11-01T04:00:00+00:00" }, + }; + const at = "2026-10-29T02:30:00.000Z"; + test("all-negative usage read is an error entry with no windows and no records", () => { + const entry = entryFromTurso( + { organization: { usage: { databases: -3, storage_bytes: -2, rows_read: -5, rows_written: -1, bytes_synced: -1 } } }, + subDoc, + { measuredAt: at }, + ); + expect(entry.state).toBe("error"); + expect(entry.error).toContain("no known metrics"); + expect(entry.windows).toEqual([]); + expect(recordsFromEntries([entry], new Map(), "quota-gatherer").records).toEqual([]); + }); + test("one negative metric is omitted; the valid metrics publish unchanged and no record is negative", () => { + const entry = entryFromTurso( + { organization: { usage: { databases: -3, storage_bytes: 1_053_323_264, rows_read: 1_905_385, rows_written: 30_026, bytes_synced: -1 } } }, + subDoc, + { measuredAt: at }, + ); + expect(entry.state).toBe("ok"); + expect(entry.windows.map((w) => w.name).sort()).toEqual(["rows-read", "rows-written", "storage"]); + for (const w of entry.windows) { + expect(w.value).toBeGreaterThanOrEqual(0); + expect(w.pctOfLimit).toBeGreaterThanOrEqual(0); + } + const { records } = recordsFromEntries([entry], new Map(), "quota-gatherer"); + expect(records.map((r) => r.metricId).sort()).toEqual(["rows-read", "rows-written", "storage"]); + for (const r of records) { + expect(r.value).toBeGreaterThanOrEqual(0); + } + }); +}); + describe("own-key guards on quota-gather-map lookups (#2781)", () => { const at = "2026-10-27T05:30:00.000Z"; test("entryFromFileExport: prototype spellings get windowSeconds null; known names unchanged", () => {