TypeScript
fix(server): show Codex Enterprise monthly credit limits (#14657)
kamkm4c18faf62cd1
diff --git a/apps/server/src/provider/codexUsageLimits.test.ts b/apps/server/src/provider/codexUsageLimits.test.tsindex 8d310a9fff7..0a190668598 100644--- a/apps/server/src/provider/codexUsageLimits.test.ts+++ b/apps/server/src/provider/codexUsageLimits.test.ts@@ -13,6 +13,34 @@ import {const checkedAt = "2026-07-18T10:00:00.000Z";describe("codexRateLimitsToLimits", () => {+ it("shows an Enterprise monthly credit allowance in reads and notifications without a duration", () => {+ const snapshot = {+ limitId: "codex",+ planType: "enterprise_cbp_usage_based",+ primary: null,+ secondary: null,+ individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },+ };+ const windows = [+ {+ id: "individual",+ kind: "monthly",+ label: "Monthly credit limit",+ usedPercent: 1,+ resetsAt: "2026-07-19T22:26:40.000Z",+ },+ ];++ expect(+ codexRateLimitsToLimits({+ checkedAt,+ snapshot: { limitId: "spark" },+ rateLimitsByLimitId: { codex: snapshot },+ }),+ ).toEqual({ checkedAt, windows });+ expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows });+ });+it("maps primary and secondary onto the session and weekly windows", () => {expect(codexRateLimitsToLimits({@@ -66,6 +94,7 @@ describe("codexRateLimitsToLimits", () => {it("selects the main Codex allowance and leaves Spark out", () => {const spark = {limitId: "codex_bengalfox",+ individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },primary: { usedPercent: 0, windowDurationMins: 300 },secondary: { usedPercent: 90, windowDurationMins: 10080 },};@@ -105,6 +134,7 @@ describe("codexRateLimitsToLimits", () => {checkedAt,snapshot: {limitId: "codex_bengalfox",+ individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },secondary: { usedPercent: 90 },},}).windows,@@ -136,6 +166,7 @@ describe("codexRateLimitsToUpdate", () => {expect(codexRateLimitsToUpdate({limitId: "codex_bengalfox",+ individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },primary: { usedPercent: 0, windowDurationMins: 300 },secondary: { usedPercent: 90, windowDurationMins: 10080 },}),@@ -266,6 +297,41 @@ describe("codexUsageLimitMessage", () => {});describe("mergeCodexRateLimits", () => {+ it("updates monthly credit usage, retains it when omitted, and clears an explicit null", () => {+ const snapshot = {+ limitId: "codex",+ primary: { usedPercent: 12 },+ individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },+ };+ const individualLimit = { remainingPercent: 75, resetsAt: 1_787_000_000 };+ const changed = mergeCodexRateLimits(snapshot, { individualLimit });+ expect(changed).toEqual({ ...snapshot, individualLimit });+ expect(codexRateLimitsToUpdate(changed!)).toEqual({+ windows: [+ {+ id: "primary",+ kind: "session",+ label: "Session",+ usedPercent: 12,+ windowDurationMins: 300,+ },+ {+ id: "individual",+ kind: "monthly",+ label: "Monthly credit limit",+ usedPercent: 25,+ resetsAt: "2026-08-17T20:53:20.000Z",+ },+ ],+ });++ const retained = mergeCodexRateLimits(changed, { primary: null });+ expect(retained).toEqual({ ...snapshot, primary: null, individualLimit });+ const cleared = mergeCodexRateLimits(retained, { individualLimit: null });+ expect(cleared).toEqual({ ...snapshot, primary: null, individualLimit: null });+ expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]);+ });+it("keeps windows an update does not carry", () => {const merged = mergeCodexRateLimits({@@ -292,6 +358,7 @@ describe("mergeCodexRateLimits", () => {expect(mergeCodexRateLimits(main, {limitId: "spark",+ individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },primary: { usedPercent: 3, resetsAt: 1_800_000_000, windowDurationMins: 300 },}),).toBe(main);diff --git a/apps/server/src/provider/codexUsageLimits.ts b/apps/server/src/provider/codexUsageLimits.tsindex 31d48535cf9..f3ec514aae3 100644--- a/apps/server/src/provider/codexUsageLimits.ts+++ b/apps/server/src/provider/codexUsageLimits.ts@@ -22,6 +22,10 @@ export interface CodexRateLimitSnapshot {readonly rateLimitReachedType?: string | null;readonly primary?: CodexRateLimitWindow | null;readonly secondary?: CodexRateLimitWindow | null;+ readonly individualLimit?: {+ readonly remainingPercent: number;+ readonly resetsAt: number;+ } | null;}export interface CodexResetCreditsSummary {@@ -84,6 +88,17 @@ function codexRateLimitsToWindows(...(resetsAt ? { resetsAt } : {}),});}+ const individualLimit = snapshot.individualLimit;+ if (individualLimit && Number.isFinite(individualLimit.remainingPercent)) {+ const resetsAt = isoFromEpochSeconds(individualLimit.resetsAt);+ windows.push({+ id: "individual",+ kind: "monthly",+ label: "Monthly credit limit",+ usedPercent: clampPercent(100 - individualLimit.remainingPercent),+ ...(resetsAt ? { resetsAt } : {}),+ });+ }return windows;}@@ -166,6 +181,7 @@ export function mergeCodexRateLimits(: {}),...(update.primary !== undefined ? { primary: update.primary } : {}),...(update.secondary !== undefined ? { secondary: update.secondary } : {}),+ ...(update.individualLimit !== undefined ? { individualLimit: update.individualLimit } : {}),};}