From 6fcd39ac097ca67ee4db8bf50dd4581078ffd5b8 Mon Sep 17 00:00:00 2001 From: luvs01 Date: Sun, 30 Aug 2026 08:16:33 +0900 Subject: [PATCH] fix(quota): drop expiry timestamps that no date formatter can render MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `epochMillis` accepted any finite number, but finite is not the same as representable. ECMAScript caps time values at ±8.64e15 ms, and `Intl.DateTimeFormat.format()` throws a RangeError past that rather than rendering an approximation. So a provider reporting a bogus expiry did not produce a wrong date; it produced a value that faults every consumer that formats it. Measured against the current code, 1e20 and 1e16 both yield "date value is not finite in DateTimeFormat format()", while 8.64e15 remains representable. Resolve an unrepresentable value to undefined so it never enters a report. Seconds inference, the zero/negative sentinel handling, and every representable timestamp are unchanged. --- src/providers/quota-wire.ts | 7 ++++++- tests/command-code-quota.test.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/providers/quota-wire.ts b/src/providers/quota-wire.ts index 0abcb69efe..31a0c81457 100644 --- a/src/providers/quota-wire.ts +++ b/src/providers/quota-wire.ts @@ -30,7 +30,12 @@ export const QUOTA_JSON_READ_FAILURE = Symbol("quota-json-read-failure"); /** Unix 0 / negative values are sentinels, not reset clocks (Command Code fiveHour.resetAt: 0). */ export function epochMillis(value: number): number | undefined { if (!Number.isFinite(value) || value <= 0) return undefined; - return value > 10_000_000_000 ? value : value * 1000; + const milliseconds = value > 10_000_000_000 ? value : value * 1000; + // A finite number is not necessarily a representable date. ECMAScript caps time values at + // ±8.64e15 ms, and `Intl.DateTimeFormat.format()` throws a RangeError past that instead of + // rendering something wrong. A provider that reports a bogus expiry must not become a + // rendering fault in every consumer that formats it. + return Number.isFinite(new Date(milliseconds).getTime()) ? milliseconds : undefined; } export function normalizeResetAt(value: unknown): number | undefined { diff --git a/tests/command-code-quota.test.ts b/tests/command-code-quota.test.ts index 3e8da6d0d3..8ba447baaf 100644 --- a/tests/command-code-quota.test.ts +++ b/tests/command-code-quota.test.ts @@ -364,6 +364,38 @@ describe("Command Code provider quota", () => { }); }); + // An out-of-range period end is not merely a wrong date: every consumer that formats it + // through Intl throws a RangeError. Drop the field and keep the usable credit figures. + test("an out-of-range subscription period end is dropped, not carried into the report", async () => { + globalThis.fetch = (async (input: RequestInfo | URL) => { + const url = String(input); + const body = url.includes("/alpha/whoami") + ? {} + : url.includes("/alpha/billing/subscriptions") + ? { data: { currentPeriodStart: "2026-08-01T00:00:00.000Z", currentPeriodEnd: 1e20 } } + : url.includes("/alpha/usage/summary") + ? { totalCost: 12 } + : { + credits: { monthlyCredits: 0, purchasedCredits: 0, freeCredits: 0 }, + windowLimits: { fiveHour: { cap: 100, used: 40 } }, + }; + return new Response(JSON.stringify(body), { status: 200, headers: { "content-type": "application/json" } }); + }) as typeof fetch; + + const result = await fetchProviderQuotaReports(commandCodeConfig(), true); + + expect(result.reports[0]?.quota).toEqual({ + fiveHourPercent: 40, + creditsUsd: { + used: 12, + limit: 12, + remaining: 0, + percent: 100, + }, + updatedAt: expect.any(Number), + }); + }); + test("a mixed balance with roll-over purchased credits carries no subscription expiry", async () => { globalThis.fetch = (async (input: RequestInfo | URL) => { const url = String(input);