From e345657355c8e7cd243972018880de42ef4a2874 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:39:40 -0600 Subject: [PATCH 1/2] fix(aggregator/token-adapter): fail closed on out-of-range window usedPct MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue #4269: collectFileExportAdapter validated window shape (object, name, finite usedPct) but not the 0-100 range, while entryFromFileExport silently skips out-of-range windows as malformed — so a success document with a -5 or 250 window published a partial entry as fresh ok, and a document whose only window was out of range published a fresh empty one. The adapter contract is drift => unparseable, never partial: reject usedPct < 0 or > 100 in the adapter validation, with regression tests for the negative/over-100/only-window cases and the inclusive 0/100 boundaries. token-adapter tests 18/18, tsc clean. --- pm-stack/aggregator/src/token-adapter.ts | 6 ++- .../aggregator/test/token-adapter.test.ts | 54 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) 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..71acb56353 100644 --- a/pm-stack/aggregator/test/token-adapter.test.ts +++ b/pm-stack/aggregator/test/token-adapter.test.ts @@ -243,6 +243,60 @@ 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("a non-string window name is unparseable drift, not a healthy reading", () => { const out = collectFileExportAdapter({ now: NOW, From 8e4eb00b4d89a1e8a9236fd5b3c6412787cfe4e3 Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 21:43:01 -0600 Subject: [PATCH 2/2] test(aggregator/token-adapter): property coverage for the usedPct range invariant Codex P1 on PR #6745: AGENTS.md requires property tests for meaningful parser invariants, and the #4269 range validation had only fixed examples. Add a deterministic seeded sweep (mulberry32, fixed seed) generating percentages from the in-range / below-range / above-range / non-finite pools and 1-4 window collections over 200 iterations: any out-of-range window makes the whole document unparseable with the previous entry retained; all-in-range publishes fresh ok with every window preserved. The fixed examples remain as complementary coverage. 19/19 tests pass, tsc clean. --- .../aggregator/test/token-adapter.test.ts | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pm-stack/aggregator/test/token-adapter.test.ts b/pm-stack/aggregator/test/token-adapter.test.ts index 71acb56353..ffe1b60410 100644 --- a/pm-stack/aggregator/test/token-adapter.test.ts +++ b/pm-stack/aggregator/test/token-adapter.test.ts @@ -297,6 +297,59 @@ describe("file-export adapter (the framework's first adapter)", () => { 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,