diff --git a/pm-stack/aggregator/src/quota-extract.ts b/pm-stack/aggregator/src/quota-extract.ts index de4a6f1a9a..6861083678 100644 --- a/pm-stack/aggregator/src/quota-extract.ts +++ b/pm-stack/aggregator/src/quota-extract.ts @@ -44,9 +44,103 @@ function monthDayTime(mon: string, day: string, hhmm: string, now: Date): string return d.toISOString(); } -/** "2026-10-28" + "14:00" CDT (UTC-5) -> ISO. */ -function dateTimeCDT(date: string, hhmm: string): string { - return `${date}T${hhmm}:00-05:00`; +const CHICAGO_WALL_FORMATTER = new Intl.DateTimeFormat("en-US", { + timeZone: "America/Chicago", + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + hourCycle: "h23", +}); + +interface WallTimeParts { + year: number; + month: number; + day: number; + hour: number; + minute: number; +} + +function wallPartsAt(instant: number): WallTimeParts { + const parts = Object.fromEntries( + CHICAGO_WALL_FORMATTER.formatToParts(new Date(instant)).map(({ type, value }) => [type, value]), + ); + return { + year: Number(parts.year), + month: Number(parts.month), + day: Number(parts.day), + hour: Number(parts.hour), + minute: Number(parts.minute), + }; +} + +function wallPartsEpoch(parts: WallTimeParts): number { + const date = new Date(0); + date.setUTCFullYear(parts.year, parts.month - 1, parts.day); + date.setUTCHours(parts.hour, parts.minute, 0, 0); + return date.getTime(); +} + +/** Interpret a dated Central wall time using its America/Chicago offset. */ +function dateTimeCentral(date: string, hhmm: string): string { + const dateMatch = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date); + const timeMatch = /^(\d{2}):(\d{2})$/.exec(hhmm); + if (!dateMatch || !timeMatch) throw new RangeError(`Invalid Central wall time: ${date} ${hhmm}`); + + const requested: WallTimeParts = { + year: Number(dateMatch[1]), + month: Number(dateMatch[2]), + day: Number(dateMatch[3]), + hour: Number(timeMatch[1]), + minute: Number(timeMatch[2]), + }; + const wallEpoch = wallPartsEpoch(requested); + const normalizedInput = new Date(wallEpoch); + if ( + requested.month < 1 || + requested.month > 12 || + requested.day < 1 || + requested.hour > 23 || + requested.minute > 59 || + normalizedInput.getUTCFullYear() !== requested.year || + normalizedInput.getUTCMonth() + 1 !== requested.month || + normalizedInput.getUTCDate() !== requested.day || + normalizedInput.getUTCHours() !== requested.hour || + normalizedInput.getUTCMinutes() !== requested.minute + ) { + throw new RangeError(`Invalid Central wall time: ${date} ${hhmm}`); + } + + // Sampling either side of the wall date finds both offsets on transition + // days. Round-trip candidates to reject skipped times; for a repeated time, + // choose the earlier instant deterministically. + const offsets = new Set( + [-36, 0, 36].map((hours) => { + const instant = wallEpoch + hours * 3_600_000; + return wallPartsEpoch(wallPartsAt(instant)) - instant; + }), + ); + const candidates = [...offsets] + .map((offset) => wallEpoch - offset) + .filter((instant) => { + const actual = wallPartsAt(instant); + return ( + actual.year === requested.year && + actual.month === requested.month && + actual.day === requested.day && + actual.hour === requested.hour && + actual.minute === requested.minute + ); + }) + .sort((a, b) => a - b); + if (candidates.length === 0) throw new RangeError(`Nonexistent Central wall time: ${date} ${hhmm}`); + + const offsetMinutes = Math.round((wallEpoch - candidates[0]) / 60_000); + const sign = offsetMinutes < 0 ? "-" : "+"; + const absoluteMinutes = Math.abs(offsetMinutes); + const offset = `${sign}${String(Math.floor(absoluteMinutes / 60)).padStart(2, "0")}:${String(absoluteMinutes % 60).padStart(2, "0")}`; + return `${date}T${hhmm}:00${offset}`; } function pctWindow(name: string, pct: number, resetsAt: string | null): QuotaWindow { @@ -102,7 +196,7 @@ const extractClaude: Extractor = (s, now) => { if (!pct) return null; const reset = first(s, /weekly reset \w+ (\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}) CDT/i); return { - windows: [pctWindow("weekly", Number(pct[1]), reset ? dateTimeCDT(reset[1], reset[2]) : null)], + windows: [pctWindow("weekly", Number(pct[1]), reset ? dateTimeCentral(reset[1], reset[2]) : null)], note: "Freshest dated read in the stewardship quota view; Claude exposes no host-side meter.", }; }; @@ -156,7 +250,7 @@ const extractMeta: Extractor = (s, now) => { }; } const reset = first(s, /[Ww]eekly reset (?:today )?(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}) CDT/); - const resetIso = reset ? dateTimeCDT(reset[1], reset[2]) : null; + const resetIso = reset ? dateTimeCentral(reset[1], reset[2]) : null; const burned = /burned to empty/i.test(s); if (resetIso && now.getTime() >= Date.parse(resetIso)) { return { @@ -213,7 +307,7 @@ const extractClinePass: Extractor = (s) => { /5-hour \*\*(\d+(?:\.\d+)?)%\*\* used; weekly \*\*(\d+(?:\.\d+)?)%\*\* used \(resets in (\d+)d (\d+)h\); monthly \*\*(\d+(?:\.\d+)?)%\*\* used \(resets in (\d+)d (\d+)h\)/i, ); if (!measured || !m) return null; - const base = Date.parse(dateTimeCDT(measured[1], measured[2])); + const base = Date.parse(dateTimeCentral(measured[1], measured[2])); const rel = (d: string, h: string) => new Date(base + (Number(d) * 24 + Number(h)) * 3_600_000).toISOString(); return { windows: [ diff --git a/pm-stack/aggregator/test/quota.test.ts b/pm-stack/aggregator/test/quota.test.ts index a04c8f0f20..900d84856b 100644 --- a/pm-stack/aggregator/test/quota.test.ts +++ b/pm-stack/aggregator/test/quota.test.ts @@ -201,6 +201,44 @@ describe("quota extractors", () => { }); }); +describe("America/Chicago dated reads", () => { + const extractClaudeReset = (date: string, time: string) => + extractQuotaPools(`### Claude\nweekly 68% used; weekly reset Wed ${date} ${time} CDT`).find( + (pool) => pool.id === "anthropic:claude-pro", + )!.windows[0].resetsAt; + + test("CST-month reads use UTC-6", () => { + expect(extractClaudeReset("2026-12-16", "14:00")).toBe("2026-12-16T14:00:00-06:00"); + expect(Date.parse(extractClaudeReset("2026-12-16", "14:00")!)).toBe(Date.parse("2026-12-16T20:00:00.000Z")); + }); + + test("CDT-month reads keep UTC-5", () => { + expect(extractClaudeReset("2026-07-16", "14:00")).toBe("2026-07-16T14:00:00-05:00"); + expect(Date.parse(extractClaudeReset("2026-07-16", "14:00")!)).toBe(Date.parse("2026-07-16T19:00:00.000Z")); + }); + + test("a fall-back transition date uses the post-transition offset", () => { + expect(extractClaudeReset("2026-11-01", "03:30")).toBe("2026-11-01T03:30:00-06:00"); + expect(Date.parse(extractClaudeReset("2026-11-01", "03:30")!)).toBe(Date.parse("2026-11-01T09:30:00.000Z")); + }); + + test("Meta does not treat a winter reset as passed an hour early", () => { + const view = `### Meta Model API\nweekly quota 46% used at 03:20 CDT, then burned to empty. Weekly reset today 2026-12-16 14:00 CDT.`; + const pool = extractQuotaPools(view, new Date("2026-12-16T19:30:00.000Z")).find( + (candidate) => candidate.id === "meta:high-usage", + )!; + expect(pool.state).toBe("walled"); + expect(pool.wallLiftsAt).toBe("2026-12-16T14:00:00-06:00"); + }); + + test("ClinePass relative windows anchor to a winter read in CST", () => { + const view = `### ClinePass\nMeasured 2026-12-16 ~14:00 CDT: 5-hour **0%** used; weekly **26%** used (resets in 0d 1h); monthly **69%** used (resets in 0d 2h).`; + const pool = extractQuotaPools(view).find((candidate) => candidate.id === "cline-pass:cline-pass")!; + expect(pool.windows[1].resetsAt).toBe("2026-12-16T21:00:00.000Z"); + expect(pool.windows[2].resetsAt).toBe("2026-12-16T22:00:00.000Z"); + }); +}); + describe("windowPercent + guidance", () => { test("explicit percent wins when the limit is unknown", () => { expect(windowPercent({ name: "w", unit: "other", limit: null, used: 0, resetsAt: null, percent: 42 })).toBe(42);