diff --git a/pm-stack/aggregator/src/token-adapter.ts b/pm-stack/aggregator/src/token-adapter.ts index b9e498c84c..df7496d020 100644 --- a/pm-stack/aggregator/src/token-adapter.ts +++ b/pm-stack/aggregator/src/token-adapter.ts @@ -278,14 +278,16 @@ export function collectFileExportAdapter(opts: { } if ( !isNonEmptyString(w.name) || - !isFiniteNumber(w.usedPct) + !isFiniteNumber(w.usedPct) || + w.usedPct < 0 || + w.usedPct > 100 ) { return retainedOutcome( "unparseable", previous, now, "ok", - `file-export drift: window ${String(w.name)} lacks a name or a finite usedPct`, + `file-export drift: window ${String(w.name)} lacks a name, a finite usedPct, or a usedPct within 0-100`, ); } } diff --git a/pm-stack/aggregator/test/token-adapter.test.ts b/pm-stack/aggregator/test/token-adapter.test.ts index f736a5bf2b..ffe1b60410 100644 --- a/pm-stack/aggregator/test/token-adapter.test.ts +++ b/pm-stack/aggregator/test/token-adapter.test.ts @@ -243,6 +243,113 @@ describe("file-export adapter (the framework's first adapter)", () => { } }); + test("an out-of-range window usedPct is unparseable drift, previous retained (#4269)", () => { + // The mapping treats pct < 0 or pct > 100 as malformed and silently + // skips the window; the adapter must fail closed instead of publishing + // the partial (or empty) entry as a fresh ok. + for (const windows of [ + [ + { name: "5h", usedPct: -5 }, + { name: "weekly", usedPct: 10 }, + ], + [ + { name: "5h", usedPct: 250 }, + { name: "weekly", usedPct: 10 }, + ], + [{ name: "5h", usedPct: -5 }], + ]) { + const out = collectFileExportAdapter({ + now: NOW, + previous: PREV_ENTRY, + fileMtimeIso: "2026-10-29T04:59:00Z", + stale: false, + doc: { + service: "meta", + source: "file-export/meta-laptop", + status: "ok", + measuredAt: "2026-10-29T04:59:00Z", + windows, + }, + }); + expect(out.status).toBe("unparseable"); + expect(out.value?.measuredAt).toBe("2026-10-29T04:30:00Z"); + } + }); + + test("window usedPct at the 0 and 100 boundaries is a healthy reading (#4269)", () => { + const out = collectFileExportAdapter({ + now: NOW, + previous: null, + fileMtimeIso: "2026-10-29T04:59:00Z", + stale: false, + doc: { + service: "meta", + source: "file-export/meta-laptop", + status: "ok", + measuredAt: "2026-10-29T04:59:00Z", + windows: [ + { name: "5h", usedPct: 0 }, + { name: "weekly", usedPct: 100 }, + ], + }, + }); + expect(out.status).toBe("ok"); + expect(out.value?.windows).toHaveLength(2); + }); + + test("property: any out-of-range window rejects the whole document; all-in-range preserves every window (#4269)", () => { + // Deterministic seeded sweep (fixed seed: failures reproduce exactly). + // Generates percentages from the in-range, out-of-range, and non-finite + // pools and window collections of varying length, and asserts the + // adapter invariant over all of them: one bad window => the whole + // document is unparseable and the previous entry is retained; all + // windows finite within the inclusive 0-100 bounds => fresh ok with + // every window preserved. + let state = 0x4269; + const rand = () => { + state |= 0; + state = (state + 0x6d2b79f5) | 0; + let x = Math.imul(state ^ (state >>> 15), 1 | state); + x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x; + return ((x ^ (x >>> 14)) >>> 0) / 4294967296; + }; + const genPct = (): number => { + const pool = rand(); + if (pool < 0.45) return rand() * 100; // in range + if (pool < 0.65) return -(rand() * 1000 + 0.001); // below range + if (pool < 0.85) return 100.001 + rand() * 1000; // above range + return [NaN, Infinity, -Infinity][Math.floor(rand() * 3)]; // non-finite + }; + const inRange = (pct: number) => Number.isFinite(pct) && pct >= 0 && pct <= 100; + for (let iter = 0; iter < 200; iter++) { + const count = 1 + Math.floor(rand() * 4); + const windows = Array.from({ length: count }, (_, i) => ({ name: `w${i}`, usedPct: genPct() })); + const out = collectFileExportAdapter({ + now: NOW, + previous: PREV_ENTRY, + fileMtimeIso: "2026-10-29T04:59:00Z", + stale: false, + doc: { + service: "meta", + source: "file-export/meta-laptop", + status: "ok", + measuredAt: "2026-10-29T04:59:00Z", + windows, + }, + }); + if (windows.every((w) => inRange(w.usedPct))) { + expect(out.status).toBe("ok"); + expect(out.value?.windows).toHaveLength(count); + for (const w of windows) { + expect(out.value?.windows.some((v) => v.name === w.name && v.pctOfLimit === w.usedPct)).toBe(true); + } + } else { + expect(out.status).toBe("unparseable"); + expect(out.value?.measuredAt).toBe("2026-10-29T04:30:00Z"); + } + } + }); + test("a non-string window name is unparseable drift, not a healthy reading", () => { const out = collectFileExportAdapter({ now: NOW,