Skip to content

Commit 88041ce

Browse files
committed
Simplify Codex usage cache handling
1 parent 8870953 commit 88041ce

3 files changed

Lines changed: 25 additions & 62 deletions

File tree

apps/server/src/provider/Layers/CodexAdapter.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,15 +1725,22 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
17251725
);
17261726
});
17271727

1728+
const cacheCodexUsageSnapshot = (
1729+
snapshot: CodexUsageSnapshot | null,
1730+
): CodexUsageSnapshot | null => {
1731+
if (snapshot) {
1732+
cachedCodexUsage = snapshot;
1733+
return snapshot;
1734+
}
1735+
return cachedCodexUsage ? { ...cachedCodexUsage, source: "cache" as const } : null;
1736+
};
1737+
17281738
const readCodexUsage: CodexAdapterShape["readCodexUsage"] = Effect.fn("readCodexUsage")(
17291739
function* () {
17301740
const session = Array.from(sessions.values()).findLast((candidate) => !candidate.stopped);
17311741
if (!session) {
17321742
const snapshot = yield* readCodexUsageWithoutSession();
1733-
cachedCodexUsage = snapshot ?? cachedCodexUsage;
1734-
return (
1735-
snapshot ?? (cachedCodexUsage ? { ...cachedCodexUsage, source: "cache" as const } : null)
1736-
);
1743+
return cacheCodexUsageSnapshot(snapshot);
17371744
}
17381745
const payload = yield* session.runtime.readAccountRateLimits.pipe(
17391746
Effect.mapError((cause) =>
@@ -1745,10 +1752,7 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
17451752
payload,
17461753
source: "read",
17471754
});
1748-
cachedCodexUsage = snapshot ?? cachedCodexUsage;
1749-
return (
1750-
snapshot ?? (cachedCodexUsage ? { ...cachedCodexUsage, source: "cache" as const } : null)
1751-
);
1755+
return cacheCodexUsageSnapshot(snapshot);
17521756
},
17531757
);
17541758

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

Lines changed: 6 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,6 @@ describe("normalizeCodexUsageSnapshot", () => {
3030
});
3131
});
3232

33-
it("prefers the codex named multi-bucket over the legacy bucket", () => {
34-
const snapshot = normalizeCodexUsageSnapshot({
35-
providerInstanceId: instanceId,
36-
source: "read",
37-
checkedAt: "2026-05-04T00:00:00.000Z",
38-
payload: {
39-
rateLimits: {
40-
primary: { usedPercent: 90, windowDurationMins: 300 },
41-
},
42-
rateLimitsByName: {
43-
codex: {
44-
primary: { usedPercent: 25, windowDurationMins: 300 },
45-
},
46-
},
47-
},
48-
});
49-
50-
expect(snapshot?.windows[0]).toMatchObject({
51-
kind: "five-hour",
52-
usedPercent: 25,
53-
remainingPercent: 75,
54-
});
55-
});
56-
5733
it("falls back to the top-level rateLimits bucket", () => {
5834
const snapshot = normalizeCodexUsageSnapshot({
5935
providerInstanceId: instanceId,
@@ -165,12 +141,15 @@ describe("normalizeCodexUsageSnapshot", () => {
165141
]);
166142
});
167143

168-
it("maps Codex named buckets when duration metadata is absent", () => {
144+
it("maps named buckets when limit-id buckets are empty", () => {
169145
const snapshot = normalizeCodexUsageSnapshot({
170146
providerInstanceId: instanceId,
171147
source: "read",
172148
payload: {
173-
rateLimits: {},
149+
rateLimits: {
150+
primary: { usedPercent: 90, windowDurationMins: 300 },
151+
},
152+
rateLimitsByLimitId: {},
174153
rateLimitsByName: {
175154
"5-hour limit": {
176155
primary: { usedPercent: 12 },
@@ -200,27 +179,6 @@ describe("normalizeCodexUsageSnapshot", () => {
200179
]);
201180
});
202181

203-
it("falls through empty limit-id buckets to named buckets", () => {
204-
const snapshot = normalizeCodexUsageSnapshot({
205-
providerInstanceId: instanceId,
206-
source: "read",
207-
payload: {
208-
rateLimits: {},
209-
rateLimitsByLimitId: {},
210-
rateLimitsByName: {
211-
"5-hour limit": {
212-
primary: { usedPercent: 12 },
213-
},
214-
},
215-
},
216-
});
217-
218-
expect(snapshot?.windows[0]).toMatchObject({
219-
kind: "five-hour",
220-
usedPercent: 12,
221-
});
222-
});
223-
224182
it("sorts fallback limit-id buckets by display priority", () => {
225183
const snapshot = normalizeCodexUsageSnapshot({
226184
providerInstanceId: instanceId,
@@ -286,12 +244,7 @@ describe("normalizeCodexUsageSnapshot", () => {
286244
primary: { usedPercent: 50, windowDurationMins: 300 },
287245
rateLimitReachedType: null,
288246
},
289-
rateLimitsByLimitId: {
290-
FiveHourLimit: {
291-
primary: { usedPercent: 100 },
292-
rateLimitReachedType: "primary",
293-
},
294-
},
247+
rateLimitsByLimitId: {},
295248
},
296249
});
297250

apps/server/src/provider/codexUsage.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,20 @@ function isRateLimitBucketPayload(payload: RateLimitPayload): boolean {
119119
return "primary" in payload || "secondary" in payload;
120120
}
121121

122+
function hasBuckets(buckets: Record<string, RateLimitBucket> | null | undefined): boolean {
123+
return Boolean(buckets && Object.keys(buckets).length > 0);
124+
}
125+
122126
function selectCodexBucket(payload: RateLimitPayload): RateLimitBucket | null {
123127
if (isRateLimitBucketPayload(payload)) {
124128
return payload;
125129
}
126130
return (
127131
payload.rateLimitsByLimitId?.codex ??
128132
payload.rateLimitsByName?.codex ??
129-
payload.rateLimits ??
133+
(hasBuckets(payload.rateLimitsByLimitId) || hasBuckets(payload.rateLimitsByName)
134+
? null
135+
: payload.rateLimits) ??
130136
null
131137
);
132138
}

0 commit comments

Comments
 (0)