From d76bd3c6bfa36f24fad08b80b09a8663d0d37847 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Wed, 28 Oct 2026 11:49:23 -0500 Subject: [PATCH] aggregator: future-dated quota-current entries beyond 60s skew classify stale (#1541) --- pm-stack/aggregator/src/quota-current.ts | 14 ++++- .../aggregator/test/quota-current.test.ts | 57 ++++++++++++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/src/quota-current.ts b/pm-stack/aggregator/src/quota-current.ts index db4dc39865..98c383fd4d 100644 --- a/pm-stack/aggregator/src/quota-current.ts +++ b/pm-stack/aggregator/src/quota-current.ts @@ -33,6 +33,16 @@ import type { /** Freshness bound for gatherer precedence: two missed ticks. */ export const QUOTA_CURRENT_FRESH_MS = 20 * 60_000; +/** + * Future clock-skew tolerance for a gatherer entry's measuredAt (#1541): + * NTP-disciplined hosts skew in seconds, so a read stamped at most this far + * in the future is indistinguishable from skew and stays fresh. Beyond it + * the entry is not fresh at all - it classifies stale, so one skewed write + * cannot pin a pool's windows (or outrank genuinely fresh reads) for as + * long as the skew lasts. + */ +export const QUOTA_CURRENT_FUTURE_SKEW_MS = 60_000; + const isRecord = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v); /** Structural check of a parsed quota-current.json document. */ @@ -55,7 +65,9 @@ type FreshState = PoolFreshness["state"]; function entryState(entry: QuotaCurrentEntry, nowMs: number): FreshState { if (entry.state === "ok") { const t = Date.parse(entry.measuredAt); - return Number.isFinite(t) && nowMs - t <= QUOTA_CURRENT_FRESH_MS ? "ok" : "stale"; + return Number.isFinite(t) && t - nowMs <= QUOTA_CURRENT_FUTURE_SKEW_MS && nowMs - t <= QUOTA_CURRENT_FRESH_MS + ? "ok" + : "stale"; } return "error"; } diff --git a/pm-stack/aggregator/test/quota-current.test.ts b/pm-stack/aggregator/test/quota-current.test.ts index 95bb11f13b..609db25611 100644 --- a/pm-stack/aggregator/test/quota-current.test.ts +++ b/pm-stack/aggregator/test/quota-current.test.ts @@ -1,5 +1,10 @@ import { describe, expect, test } from "bun:test"; -import { overlayQuotaCurrent, parseQuotaCurrent, QUOTA_CURRENT_FRESH_MS } from "../src/quota-current.ts"; +import { + overlayQuotaCurrent, + parseQuotaCurrent, + QUOTA_CURRENT_FRESH_MS, + QUOTA_CURRENT_FUTURE_SKEW_MS, +} from "../src/quota-current.ts"; import type { QuotaCurrentEntry, QuotaCurrentFile, QuotaPool, QuotaView } from "../src/model.ts"; // The final visibility hop (#1151): the gatherer's quota-current.json @@ -165,6 +170,56 @@ describe("quota-current overlay", () => { expect(cline.windows[0].percent).toBe(69); // dated read, not the aged gatherer value }); + test("an ok entry dated past the future-skew tolerance is stale, windows untouched (#1541)", () => { + for (const futureMs of [60 * 60_000, 30 * 24 * 60 * 60_000]) { + const futureAt = iso(-futureMs); + const out = overlayQuotaCurrent( + baseView(), + file([ + entry({ serviceId: "cline-pass:cline-pass", measuredAt: futureAt, windows: [win("monthly", 71, futureAt)] }), + ]), + NOW, + ); + const cline = out.pools.find((p) => p.id === "cline-pass:cline-pass")!; + expect(cline.state).toBe("stale"); + expect(cline.freshness).toEqual({ measuredAt: futureAt, source: "openchamber-quota", state: "stale" }); + expect(cline.windows[0].percent).toBe(69); // dated read, not the future-dated gatherer value + expect(out.gatherer?.providers.find((p) => p.serviceId === "cline-pass:cline-pass")?.state).toBe("stale"); + } + }); + + test("an ok entry dated within the future-skew tolerance stays fresh (#1541)", () => { + const at = iso(-(QUOTA_CURRENT_FUTURE_SKEW_MS - 30_000)); + const out = overlayQuotaCurrent( + baseView(), + file([entry({ serviceId: "cline-pass:cline-pass", measuredAt: at, windows: [win("monthly", 71, at)] })]), + NOW, + ); + const cline = out.pools.find((p) => p.id === "cline-pass:cline-pass")!; + expect(cline.freshness?.state).toBe("ok"); + expect(cline.windows[0].percent).toBe(71); + }); + + test("a future-dated entry cannot outrank a genuinely fresh entry for the same service (#1541)", () => { + const futureAt = iso(-60 * 60_000); + const out = overlayQuotaCurrent( + baseView(), + file([ + entry({ serviceId: "cline-pass:cline-pass", measuredAt: futureAt, windows: [win("monthly", 99, futureAt)] }), + entry({ + serviceId: "cline-pass:cline-pass", + measuredAt: iso(5 * 60_000), + windows: [win("monthly", 71, iso(5 * 60_000))], + }), + ]), + NOW, + ); + const cline = out.pools.find((p) => p.id === "cline-pass:cline-pass")!; + expect(cline.freshness?.state).toBe("ok"); + expect(cline.freshness?.measuredAt).toBe(iso(5 * 60_000)); + expect(cline.windows[0].percent).toBe(71); // the genuinely fresh read, not the future-dated 99 + }); + test("a balance-only ok entry stamps freshness without inventing windows", () => { const out = overlayQuotaCurrent( baseView(),