Skip to content

Commit 4c18faf

Browse files
fix(server): show Codex Enterprise monthly credit limits (pingdotgg#14657)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 3dd9706 commit 4c18faf

2 files changed

Lines changed: 83 additions & 0 deletions

File tree

‎apps/server/src/provider/codexUsageLimits.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@ import {
1313
const checkedAt = "2026-07-18T10:00:00.000Z";
1414

1515
describe("codexRateLimitsToLimits", () => {
16+
it("shows an Enterprise monthly credit allowance in reads and notifications without a duration", () => {
17+
const snapshot = {
18+
limitId: "codex",
19+
planType: "enterprise_cbp_usage_based",
20+
primary: null,
21+
secondary: null,
22+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
23+
};
24+
const windows = [
25+
{
26+
id: "individual",
27+
kind: "monthly",
28+
label: "Monthly credit limit",
29+
usedPercent: 1,
30+
resetsAt: "2026-07-19T22:26:40.000Z",
31+
},
32+
];
33+
34+
expect(
35+
codexRateLimitsToLimits({
36+
checkedAt,
37+
snapshot: { limitId: "spark" },
38+
rateLimitsByLimitId: { codex: snapshot },
39+
}),
40+
).toEqual({ checkedAt, windows });
41+
expect(codexRateLimitsToUpdate(snapshot)).toEqual({ windows });
42+
});
43+
1644
it("maps primary and secondary onto the session and weekly windows", () => {
1745
expect(
1846
codexRateLimitsToLimits({
@@ -66,6 +94,7 @@ describe("codexRateLimitsToLimits", () => {
6694
it("selects the main Codex allowance and leaves Spark out", () => {
6795
const spark = {
6896
limitId: "codex_bengalfox",
97+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
6998
primary: { usedPercent: 0, windowDurationMins: 300 },
7099
secondary: { usedPercent: 90, windowDurationMins: 10080 },
71100
};
@@ -105,6 +134,7 @@ describe("codexRateLimitsToLimits", () => {
105134
checkedAt,
106135
snapshot: {
107136
limitId: "codex_bengalfox",
137+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
108138
secondary: { usedPercent: 90 },
109139
},
110140
}).windows,
@@ -136,6 +166,7 @@ describe("codexRateLimitsToUpdate", () => {
136166
expect(
137167
codexRateLimitsToUpdate({
138168
limitId: "codex_bengalfox",
169+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
139170
primary: { usedPercent: 0, windowDurationMins: 300 },
140171
secondary: { usedPercent: 90, windowDurationMins: 10080 },
141172
}),
@@ -266,6 +297,41 @@ describe("codexUsageLimitMessage", () => {
266297
});
267298

268299
describe("mergeCodexRateLimits", () => {
300+
it("updates monthly credit usage, retains it when omitted, and clears an explicit null", () => {
301+
const snapshot = {
302+
limitId: "codex",
303+
primary: { usedPercent: 12 },
304+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
305+
};
306+
const individualLimit = { remainingPercent: 75, resetsAt: 1_787_000_000 };
307+
const changed = mergeCodexRateLimits(snapshot, { individualLimit });
308+
expect(changed).toEqual({ ...snapshot, individualLimit });
309+
expect(codexRateLimitsToUpdate(changed!)).toEqual({
310+
windows: [
311+
{
312+
id: "primary",
313+
kind: "session",
314+
label: "Session",
315+
usedPercent: 12,
316+
windowDurationMins: 300,
317+
},
318+
{
319+
id: "individual",
320+
kind: "monthly",
321+
label: "Monthly credit limit",
322+
usedPercent: 25,
323+
resetsAt: "2026-08-17T20:53:20.000Z",
324+
},
325+
],
326+
});
327+
328+
const retained = mergeCodexRateLimits(changed, { primary: null });
329+
expect(retained).toEqual({ ...snapshot, primary: null, individualLimit });
330+
const cleared = mergeCodexRateLimits(retained, { individualLimit: null });
331+
expect(cleared).toEqual({ ...snapshot, primary: null, individualLimit: null });
332+
expect(codexRateLimitsToLimits({ checkedAt, snapshot: cleared! }).windows).toEqual([]);
333+
});
334+
269335
it("keeps windows an update does not carry", () => {
270336
const merged = mergeCodexRateLimits(
271337
{
@@ -292,6 +358,7 @@ describe("mergeCodexRateLimits", () => {
292358
expect(
293359
mergeCodexRateLimits(main, {
294360
limitId: "spark",
361+
individualLimit: { remainingPercent: 99, resetsAt: 1_784_500_000 },
295362
primary: { usedPercent: 3, resetsAt: 1_800_000_000, windowDurationMins: 300 },
296363
}),
297364
).toBe(main);

‎apps/server/src/provider/codexUsageLimits.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export interface CodexRateLimitSnapshot {
2222
readonly rateLimitReachedType?: string | null;
2323
readonly primary?: CodexRateLimitWindow | null;
2424
readonly secondary?: CodexRateLimitWindow | null;
25+
readonly individualLimit?: {
26+
readonly remainingPercent: number;
27+
readonly resetsAt: number;
28+
} | null;
2529
}
2630

2731
export interface CodexResetCreditsSummary {
@@ -84,6 +88,17 @@ function codexRateLimitsToWindows(
8488
...(resetsAt ? { resetsAt } : {}),
8589
});
8690
}
91+
const individualLimit = snapshot.individualLimit;
92+
if (individualLimit && Number.isFinite(individualLimit.remainingPercent)) {
93+
const resetsAt = isoFromEpochSeconds(individualLimit.resetsAt);
94+
windows.push({
95+
id: "individual",
96+
kind: "monthly",
97+
label: "Monthly credit limit",
98+
usedPercent: clampPercent(100 - individualLimit.remainingPercent),
99+
...(resetsAt ? { resetsAt } : {}),
100+
});
101+
}
87102
return windows;
88103
}
89104

@@ -166,6 +181,7 @@ export function mergeCodexRateLimits(
166181
: {}),
167182
...(update.primary !== undefined ? { primary: update.primary } : {}),
168183
...(update.secondary !== undefined ? { secondary: update.secondary } : {}),
184+
...(update.individualLimit !== undefined ? { individualLimit: update.individualLimit } : {}),
169185
};
170186
}
171187

0 commit comments

Comments
 (0)