From af3b32910a9b4f1dbf0f0a862073a88066b61243 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 09:50:09 -0600 Subject: [PATCH] fix(aggregator): strict decimal contract in aa num() (#2985) A string counts as a measurement only if it is a plain decimal spelling (optional sign, digits with optional fraction, or .digits): no trim, no 0x/0b/0o prefix, no exponent. Radix, scientific, and whitespace-padded spellings return null. Header comment records both pins (scientific REJECTED: Postgres numeric->jsonb never emits e-notation; padded REJECTED: padding is import drift). Direct num() unit tests cover the triage probe matrix. --- pm-stack/aggregator/src/aa.ts | 18 ++++++++++- pm-stack/aggregator/test/aa.test.ts | 47 +++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/pm-stack/aggregator/src/aa.ts b/pm-stack/aggregator/src/aa.ts index e691fb03d0..fa9fb7df04 100644 --- a/pm-stack/aggregator/src/aa.ts +++ b/pm-stack/aggregator/src/aa.ts @@ -26,10 +26,26 @@ import type { // The Supabase Management API serializes numeric columns as JSON // strings, so every number crosses a coercion gate here; anything // unparseable becomes null ("not measured"), never 0. +// +// Strict decimal contract (#2985; pins from triage task 4291, design +// #3005 Phase 1 "a measurement is a plain decimal"): a string counts +// as a measurement ONLY if it is a plain decimal spelling — optional +// sign, digits with optional fraction, or `.digits` — with no trim, +// no `0x`/`0b`/`0o` prefix (any case), and no exponent. Radix, +// scientific, and whitespace-padded spellings all return null. +// PIN 1 (scientific REJECTED): no AA source serializes a measurement +// in e-notation — Postgres `numeric` → jsonb spelling is always plain +// decimal, and Test_Runs cells are human-entered decimals where +// e-notation is formatting drift, not a serialization. +// PIN 2 (padded REJECTED): the legitimate string producers (Postgres +// jsonb numerics, JSON config limits) never emit padding; padding is +// import drift, covered by the same no-trim rule. + +const PLAIN_DECIMAL = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/; export function num(v: unknown): number | null { if (typeof v === "number") return Number.isFinite(v) ? v : null; - if (typeof v === "string" && v.trim() !== "") { + if (typeof v === "string" && PLAIN_DECIMAL.test(v)) { const n = Number(v); return Number.isFinite(n) ? n : null; } diff --git a/pm-stack/aggregator/test/aa.test.ts b/pm-stack/aggregator/test/aa.test.ts index 69e1568d50..c2165bc060 100644 --- a/pm-stack/aggregator/test/aa.test.ts +++ b/pm-stack/aggregator/test/aa.test.ts @@ -14,6 +14,7 @@ import { aaCatalogFromConfig, aaPoints, caiFamily, + num, aaSectionFromSupabase, computeAaCoverage, effortRank, @@ -884,3 +885,49 @@ describe("prototype-key lookups in aa maps (#3056)", () => { expect(section!.cai!.map((r) => r.creator)).toEqual(["constructor", "__proto__", "DeepSeek", "acme"]); }); }); + +describe("num() strict decimal contract (#2985)", () => { + test("plain decimal spellings parse", () => { + expect(num("12")).toBe(12); + expect(num("12.5")).toBe(12.5); + expect(num("-3")).toBe(-3); + expect(num("+3")).toBe(3); + expect(num(".5")).toBe(0.5); + expect(num("5.")).toBe(5); + expect(num("0")).toBe(0); + }); + + test("radix-prefixed spellings return null (any case)", () => { + for (const s of ["0x10", "0X10", "0b101", "0B101", "0o17", "0O17"]) { + expect(num(s)).toBeNull(); + } + }); + + test("scientific spellings return null (PIN 1: no AA source emits e-notation)", () => { + for (const s of ["1e2", "1E2", "1e-3", "1E+2"]) { + expect(num(s)).toBeNull(); + } + }); + + test("whitespace-padded spellings return null (PIN 2: padding is drift, no trim)", () => { + for (const s of [" 12 ", " 12", "12 ", "\t12\n"]) { + expect(num(s)).toBeNull(); + } + }); + + test("other unparseable strings return null", () => { + for (const s of ["", "abc", "12abc", "Infinity", "1,200", "1_000", "-", ".", "+"]) { + expect(num(s)).toBeNull(); + } + }); + + test("finite numbers pass through; non-finite and non-numeric inputs return null", () => { + expect(num(42)).toBe(42); + expect(num(12.5)).toBe(12.5); + expect(num(NaN)).toBeNull(); + expect(num(Infinity)).toBeNull(); + expect(num(null)).toBeNull(); + expect(num(undefined)).toBeNull(); + expect(num(true)).toBeNull(); + }); +});