Skip to content

Commit 8870953

Browse files
committed
Fix codex usage rate limit fallback
1 parent 6849abf commit 8870953

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

apps/server/src/provider/codexUsage.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,27 @@ describe("normalizeCodexUsageSnapshot", () => {
277277
expect(snapshot?.rateLimitReachedType).toBe("primary");
278278
});
279279

280+
it("does not fall back when the selected bucket explicitly has no rate limit reached", () => {
281+
const snapshot = normalizeCodexUsageSnapshot({
282+
providerInstanceId: instanceId,
283+
source: "read",
284+
payload: {
285+
rateLimits: {
286+
primary: { usedPercent: 50, windowDurationMins: 300 },
287+
rateLimitReachedType: null,
288+
},
289+
rateLimitsByLimitId: {
290+
FiveHourLimit: {
291+
primary: { usedPercent: 100 },
292+
rateLimitReachedType: "primary",
293+
},
294+
},
295+
},
296+
});
297+
298+
expect(snapshot?.rateLimitReachedType).toBeNull();
299+
});
300+
280301
it("uses Codex primary and secondary semantics when durations are unknown", () => {
281302
const snapshot = normalizeCodexUsageSnapshot({
282303
providerInstanceId: instanceId,

apps/server/src/provider/codexUsage.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ function rateLimitReachedTypeFromBucketGroups(
192192
return null;
193193
}
194194

195+
function rateLimitReachedTypeFromSelectedBucket(
196+
bucket: RateLimitBucket | null,
197+
fallback: () => string | null,
198+
): string | null {
199+
if (bucket && "rateLimitReachedType" in bucket) {
200+
return bucket.rateLimitReachedType ?? null;
201+
}
202+
return fallback();
203+
}
204+
195205
export function normalizeCodexUsageSnapshot(input: {
196206
readonly providerInstanceId: ProviderInstanceId;
197207
readonly payload: RateLimitPayload;
@@ -220,12 +230,12 @@ export function normalizeCodexUsageSnapshot(input: {
220230
providerInstanceId: input.providerInstanceId,
221231
checkedAt: input.checkedAt ?? new Date().toISOString(),
222232
windows,
223-
rateLimitReachedType:
224-
bucket?.rateLimitReachedType ??
233+
rateLimitReachedType: rateLimitReachedTypeFromSelectedBucket(bucket, () =>
225234
rateLimitReachedTypeFromBucketGroups(
226235
input.payload.rateLimitsByLimitId,
227236
input.payload.rateLimitsByName,
228237
),
238+
),
229239
source: input.source,
230240
};
231241
}

0 commit comments

Comments
 (0)