Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pm-stack/aggregator/src/aa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
47 changes: 47 additions & 0 deletions pm-stack/aggregator/test/aa.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
aaCatalogFromConfig,
aaPoints,
caiFamily,
num,
aaSectionFromSupabase,
computeAaCoverage,
effortRank,
Expand Down Expand Up @@ -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();
});
});