From 8d159abf2c06e4cda8789fbf02d8ea5efc60c8e5 Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Sun, 8 Nov 2026 18:34:06 -0600 Subject: [PATCH] fix(aggregator): clinepass percent gate takes plain decimals only; state the #3005 measurement grammar in the README (#3006) Closing inventory audit for design #3005 (issue #3006, task 4350): the whole pm-stack measurement surface was grepped for bare Number( coercions and bare count extractions and every hit dispositioned on #3006. One missed field-form boundary found and fixed here: clinepass-adapter percentUsedOf accepted any Number()-parseable string (0x10 -> 16, 1e2 -> 100, padded) for a provider percent measurement; it now takes the plain-decimal field form, with probe-matrix tests. The README gains the recorded measurement grammar next to the #2576 ingest contract. The #2789 meta-exporter embodiment was found drifted (gate lost in the #6333 re-home); restoration is filed as #7880 per the audit acceptance, not fixed here. --- pm-stack/aggregator/README.md | 42 +++++++++++++++++++ pm-stack/aggregator/src/clinepass-adapter.ts | 13 +++++- .../aggregator/test/clinepass-adapter.test.ts | 16 +++++++ 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/README.md b/pm-stack/aggregator/README.md index cdb17894d5..bbf48b8217 100644 --- a/pm-stack/aggregator/README.md +++ b/pm-stack/aggregator/README.md @@ -426,6 +426,48 @@ The contract's existing embodiment is `src/token-adapter.ts` (#1153): it validates its document once at the boundary and rejects-and-retains on failure, and new boundaries are written to its standard. +## Measurement grammar (#3005) + +A measurement string is a **plain decimal**. Everywhere the PM stack +turns a string into a measurement — a coercion gate in this app or in +the laptop exporters — exactly two forms exist: + +- **Field form** (a value that IS the measurement — API fields, sheet + cells): an optional sign where the gate's domain already admits + negatives, digits, and an optional single fraction + (`/^[+-]?[0-9]+(\.[0-9]+)?$/`, narrowed per gate — counts are + unsigned integers; kind rules stay with their gates). Everything + else — empty, whitespace-padded, radix-prefixed (`0x10`), + scientific (`1e2`), thousands separators in a field value — is + *not a measurement*: the gate returns its existing not-measured + outcome (`null` here; the exporters' fail-closed error), never a + reinterpreted number. +- **Prose form** (a count embedded in a human-written bullet): the + extraction captures the grouped form (`[\d,]+`, strip commas — + the idiom in `src/dns.ts` `parsePriceUsd` and + `src/snapshot-records.ts`). A bare `\d+` adjacent to a count noun + is a defect; where a grouped figure cannot match, the metric is + absent — never truncated, never a partial-group match. + +Pins: scientific spellings are **refused** in both forms (no source +emits them; the landed meta-exporter gate refuses them); whitespace +padding is **refused** (padding is drift, not a spelling); grouping +well-formedness is **stripped, not validated** (`1,2,3` parses as +123 — strict grouping was considered and rejected). Already-typed +numbers are untouched: a `typeof number` input keeps each gate's +existing finite check — the grammar governs strings only. + +The grammar lives as **per-app copies with one shared probe matrix** +(these apps deploy independently): `"12"`/`"12.5"` measure; +`"1,234"` + noun reads 1234 in prose form and is not a measurement +in field form; `""`, `"0x10"`, `"1e2"`, `" 12 "`, `"12abc"` are not +measured. Embodiments: `src/aa.ts` `num()` (#2985), +`src/saas-extract.ts` count patterns (#2997, #3000), +cloudflare-export and supabase-export gates (#2791, #2743), +meta-exporter's `parseSubscriptionQuota` (#2789 — drift found by +the closing audit: the #6333 re-home restored the pre-#2789 bare +coercion; restoration is filed as #7880). Closing audit: issue #3006. + ## First-class requirements (from the design record) 1. **Cross-repo links.** Any board card or roadmap row links any GitHub diff --git a/pm-stack/aggregator/src/clinepass-adapter.ts b/pm-stack/aggregator/src/clinepass-adapter.ts index d6761ab011..6a620b8dd6 100644 --- a/pm-stack/aggregator/src/clinepass-adapter.ts +++ b/pm-stack/aggregator/src/clinepass-adapter.ts @@ -56,11 +56,20 @@ const WINDOW_KINDS = new Map Math.min(100, Math.max(0, value)); +// Plain decimal spellings only (#3005 field form, closing audit +// task 4350 / issue #3006). Number() also accepts "", whitespace- +// padded, hex ("0x10" -> 16), and scientific ("1e2" -> 100) strings, +// which would turn a drifted provider field into a real measurement; +// reject every non-decimal spelling so drift reads as not-a-reading. +// The sign stays: this gate's domain admits negatives (clamped by +// clampPercent), like aa.ts's PLAIN_DECIMAL. +const PLAIN_DECIMAL = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/; + /** percentUsed as the provider reads it: a finite number, or a - * non-empty numeric string; anything else is not a reading. */ + * plain-decimal string; anything else is not a reading. */ function percentUsedOf(value: unknown): number | null { if (typeof value === "number") return Number.isFinite(value) ? value : null; - if (typeof value === "string" && value.trim() !== "") { + if (typeof value === "string" && PLAIN_DECIMAL.test(value)) { const n = Number(value); return Number.isFinite(n) ? n : null; } diff --git a/pm-stack/aggregator/test/clinepass-adapter.test.ts b/pm-stack/aggregator/test/clinepass-adapter.test.ts index 91b72207ba..a2383c4885 100644 --- a/pm-stack/aggregator/test/clinepass-adapter.test.ts +++ b/pm-stack/aggregator/test/clinepass-adapter.test.ts @@ -85,6 +85,22 @@ describe("parseClinePassPayload (ported OpenChamber mapping)", () => { expect(reading.windows.map((w) => w.usedPct)).toEqual([51, 100, 0]); }); + test("refuses non-decimal percent strings (#3005 field form): hex, scientific, padded", () => { + const reading = parseClinePassPayload({ + data: { + limits: [ + { type: "weekly", percentUsed: "0x10" }, + { type: "monthly", percentUsed: "1e2" }, + { type: "five_hour", percentUsed: " 51 " }, + { type: "weekly", percentUsed: "51.5" }, + ], + }, + }); + expect(reading.windows).toEqual([ + { name: "weekly", usedPct: 51.5, windowSeconds: 604800, resetsAt: null }, + ]); + }); + test("skips malformed windows and unknown types, keeping valid siblings", () => { const reading = parseClinePassPayload({ data: {