Skip to content

Commit c902fe1

Browse files
fix(usage): read Codex rate limits from real rollout shape
The transcript snapshot parser required `payload.info.rate_limits`, but codex-rs persists `rate_limits` as a sibling of `info`, so no real rollout ever matched and every cold read still spawned the app-server probe. Read the real shape and ignore model-scoped buckets (any `limit_id` other than `codex`, or absent on older builds) on both the transcript and probe paths so a Spark turn cannot present its windows as the account limit. Only short-circuit the page's wait when every enabled provider has settled, so a fast Codex snapshot no longer ships the page without Claude's meters. Verified against live rollouts in ~/.codex/sessions: the account line parses and the codex_bengalfox line is rejected.
1 parent 61b1e0e commit c902fe1

4 files changed

Lines changed: 141 additions & 46 deletions

File tree

‎apps/server/src/usage/UsageService.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,18 @@ export const make = Effect.gen(function* () {
189189
hostEnvironment.T3CODE_DEV_USAGE_LIMITS_FIXTURE,
190190
now,
191191
);
192-
if (fixture !== null) return fixture;
192+
if (fixture !== null) return { limits: fixture, settled: true };
193193

194194
const providers = [context.codex, context.claude].filter(({ enabled }) => enabled);
195-
196-
return providers.flatMap(({ cacheKey }) => {
197-
const outcome = readSubscriptionLimitsCacheEntry(
198-
subscriptionLimitsCache.get(cacheKey),
199-
now,
200-
);
201-
return outcome?._tag === "Success" && outcome.limits !== null ? [outcome.limits] : [];
202-
});
195+
const outcomes = providers.map(({ cacheKey }) =>
196+
readSubscriptionLimitsCacheEntry(subscriptionLimitsCache.get(cacheKey), now),
197+
);
198+
return {
199+
limits: outcomes.flatMap((outcome) =>
200+
outcome?._tag === "Success" && outcome.limits !== null ? [outcome.limits] : [],
201+
),
202+
settled: outcomes.every((outcome) => outcome !== undefined),
203+
};
203204
},
204205
);
205206

‎apps/server/src/usage/usageSubscriptionLimits.test.ts‎

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,16 @@ describe("subscription usage limits", () => {
297297
type: "event_msg",
298298
payload: {
299299
type: "token_count",
300-
info: {
301-
rate_limits: {
302-
primary: null,
303-
secondary: {
304-
used_percent: 57,
305-
window_minutes: 10_080,
306-
resets_at: 1_788_000_000,
307-
},
308-
plan_type: "prolite",
300+
info: null,
301+
rate_limits: {
302+
limit_id: "codex",
303+
primary: null,
304+
secondary: {
305+
used_percent: 57,
306+
window_minutes: 10_080,
307+
resets_at: 1_788_000_000,
309308
},
309+
plan_type: "prolite",
310310
},
311311
},
312312
}),
@@ -315,6 +315,7 @@ describe("subscription usage limits", () => {
315315
observedAtMs: Date.parse("2026-08-27T02:10:00.000Z"),
316316
response: {
317317
rateLimits: {
318+
limitId: "codex",
318319
planType: "prolite",
319320
primary: null,
320321
secondary: {
@@ -327,6 +328,38 @@ describe("subscription usage limits", () => {
327328
});
328329
});
329330

331+
it("ignores model-scoped Codex limit buckets", () => {
332+
const line = (limitId: string | null | undefined) =>
333+
JSON.stringify({
334+
timestamp: "2026-08-27T02:10:00.000Z",
335+
type: "event_msg",
336+
payload: {
337+
type: "token_count",
338+
rate_limits: {
339+
...(limitId === undefined ? {} : { limit_id: limitId }),
340+
primary: { used_percent: 0, window_minutes: 300, resets_at: 1_788_000_000 },
341+
secondary: null,
342+
plan_type: "prolite",
343+
},
344+
},
345+
});
346+
347+
expect(parseCodexTranscriptRateLimitsSnapshot(line("codex_bengalfox"))).toBeNull();
348+
expect(parseCodexTranscriptRateLimitsSnapshot(line("codex"))).not.toBeNull();
349+
expect(parseCodexTranscriptRateLimitsSnapshot(line(null))).not.toBeNull();
350+
expect(parseCodexTranscriptRateLimitsSnapshot(line(undefined))).not.toBeNull();
351+
352+
expect(
353+
normalizeCodexSubscriptionLimits({
354+
rateLimits: {
355+
limitId: "codex_bengalfox",
356+
planType: "prolite",
357+
primary: { usedPercent: 0, windowDurationMins: 300, resetsAt: 1_788_000_000 },
358+
},
359+
}),
360+
).toBeNull();
361+
});
362+
330363
it("clamps provider percentages to the progress bar range", () => {
331364
const limits = normalizeCodexSubscriptionLimits({
332365
rateLimits: {
@@ -352,7 +385,10 @@ describe("subscription usage limits", () => {
352385
],
353386
} satisfies UsageProviderLimits;
354387
const providerFiber = yield* Effect.sleep(Duration.seconds(10)).pipe(Effect.forkScoped);
355-
const result = yield* awaitSubscriptionLimits(providerFiber, Effect.succeed([limits]));
388+
const result = yield* awaitSubscriptionLimits(
389+
providerFiber,
390+
Effect.succeed({ limits: [limits], settled: true }),
391+
);
356392

357393
expect(result).toEqual([limits]);
358394
expect(providerFiber.pollUnsafe()).toBeUndefined();
@@ -362,9 +398,10 @@ describe("subscription usage limits", () => {
362398
it.effect("returns after five seconds while a slow provider refresh keeps running", () =>
363399
Effect.gen(function* () {
364400
const providerFiber = yield* Effect.sleep(Duration.seconds(10)).pipe(Effect.forkScoped);
365-
const waitFiber = yield* awaitSubscriptionLimits(providerFiber, Effect.succeed([])).pipe(
366-
Effect.forkChild,
367-
);
401+
const waitFiber = yield* awaitSubscriptionLimits(
402+
providerFiber,
403+
Effect.succeed({ limits: [], settled: false }),
404+
).pipe(Effect.forkChild);
368405

369406
yield* Effect.yieldNow;
370407
yield* TestClock.adjust(Duration.seconds(5));
@@ -395,7 +432,7 @@ describe("subscription usage limits", () => {
395432
);
396433
const waitFiber = yield* awaitSubscriptionLimits(
397434
providerFiber,
398-
Effect.sync(() => current),
435+
Effect.sync(() => ({ limits: current, settled: current.length > 0 })),
399436
).pipe(Effect.forkChild);
400437

401438
yield* Effect.yieldNow;
@@ -405,6 +442,35 @@ describe("subscription usage limits", () => {
405442
}).pipe(Effect.provide(TestClock.layer())),
406443
);
407444

445+
it.effect("waits for the slower provider when only one provider is ready", () =>
446+
Effect.gen(function* () {
447+
const codex = {
448+
provider: "codex",
449+
plan: "plus",
450+
windows: [{ kind: "weekly", usedPercent: 42, resetsAt: null }],
451+
} satisfies UsageProviderLimits;
452+
const claude = {
453+
provider: "claude",
454+
plan: "max",
455+
windows: [{ kind: "fiveHour", usedPercent: 7, resetsAt: null }],
456+
} satisfies UsageProviderLimits;
457+
let current: readonly UsageProviderLimits[] = [codex];
458+
const providerFiber = yield* Effect.sleep(Duration.seconds(2)).pipe(
459+
Effect.tap(() => Effect.sync(() => (current = [codex, claude]))),
460+
Effect.forkScoped,
461+
);
462+
const waitFiber = yield* awaitSubscriptionLimits(
463+
providerFiber,
464+
Effect.sync(() => ({ limits: current, settled: current.length === 2 })),
465+
).pipe(Effect.forkChild);
466+
467+
yield* Effect.yieldNow;
468+
yield* TestClock.adjust(Duration.seconds(2));
469+
470+
expect(yield* Fiber.join(waitFiber)).toEqual([codex, claude]);
471+
}).pipe(Effect.provide(TestClock.layer())),
472+
);
473+
408474
it.effect("distinguishes a successful empty response from a failed probe", () =>
409475
Effect.gen(function* () {
410476
const [emptyOutcome, failedOutcome] = yield* Effect.all([

‎apps/server/src/usage/usageSubscriptionLimits.ts‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,29 @@ export const runSubscriptionLimitsProbe = Effect.fn("runSubscriptionLimitsProbe"
101101
),
102102
);
103103

104-
/** Returns ready limits immediately, otherwise gives the background refresh a short budget. */
104+
export interface SubscriptionLimitsReadResult {
105+
readonly limits: readonly UsageProviderLimits[];
106+
/** True once every enabled provider has a live cache entry, success or failure. */
107+
readonly settled: boolean;
108+
}
109+
110+
/**
111+
* Returns limits immediately once every enabled provider has settled, otherwise
112+
* gives the background refresh a short budget so a fast provider does not
113+
* ship the page without the slower one's meters.
114+
*/
105115
export const awaitSubscriptionLimits = Effect.fn("awaitSubscriptionLimits")(function* (
106116
refreshFiber: Fiber.Fiber<void, never>,
107-
readCurrent: Effect.Effect<readonly UsageProviderLimits[], never>,
117+
readCurrent: Effect.Effect<SubscriptionLimitsReadResult, never>,
108118
) {
109119
const ready = yield* readCurrent;
110-
if (ready.length > 0) return ready;
120+
if (ready.settled) return ready.limits;
111121

112122
yield* Fiber.join(refreshFiber).pipe(
113123
Effect.timeoutOption(SUBSCRIPTION_LIMITS_READ_BUDGET_MS),
114124
Effect.asVoid,
115125
);
116-
return yield* readCurrent;
126+
return (yield* readCurrent).limits;
117127
});
118128

119129
export function makeSubscriptionLimitsCacheEntry(
@@ -194,6 +204,8 @@ interface CodexRateLimitWindowResponse {
194204

195205
export interface CodexUsageLimitsResponse {
196206
readonly rateLimits: {
207+
/** Codex reports model-scoped buckets under other ids; only `codex` is the account limit. */
208+
readonly limitId?: string | null;
197209
readonly planType?: string | null;
198210
readonly primary?: CodexRateLimitWindowResponse | null;
199211
readonly secondary?: CodexRateLimitWindowResponse | null;
@@ -211,19 +223,25 @@ const CodexTranscriptRateLimitWindow = Schema.Struct({
211223
window_minutes: Schema.optionalKey(NullableNumber),
212224
resets_at: Schema.optionalKey(NullableNumber),
213225
});
226+
const NullableString = Schema.Union([Schema.String, Schema.Null]);
227+
// `rate_limits` is a sibling of `info` on codex-rs `TokenCountEvent`, not nested inside it.
214228
const CodexTranscriptRateLimitsEvent = Schema.Struct({
215229
timestamp: Schema.String,
216230
payload: Schema.Struct({
217231
type: Schema.Literal("token_count"),
218-
info: Schema.Struct({
219-
rate_limits: Schema.Struct({
220-
primary: Schema.optionalKey(Schema.Union([CodexTranscriptRateLimitWindow, Schema.Null])),
221-
secondary: Schema.optionalKey(Schema.Union([CodexTranscriptRateLimitWindow, Schema.Null])),
222-
plan_type: Schema.optionalKey(Schema.Union([Schema.String, Schema.Null])),
223-
}),
232+
rate_limits: Schema.Struct({
233+
limit_id: Schema.optionalKey(NullableString),
234+
primary: Schema.optionalKey(Schema.Union([CodexTranscriptRateLimitWindow, Schema.Null])),
235+
secondary: Schema.optionalKey(Schema.Union([CodexTranscriptRateLimitWindow, Schema.Null])),
236+
plan_type: Schema.optionalKey(NullableString),
224237
}),
225238
}),
226239
});
240+
241+
/** Older Codex builds omit `limit_id`; newer ones tag model-scoped buckets such as Spark. */
242+
function isCodexAccountLimit(limitId: string | null | undefined): boolean {
243+
return limitId === undefined || limitId === null || limitId === "codex";
244+
}
227245
const decodeCodexTranscriptRateLimitsEvent = Schema.decodeUnknownOption(
228246
Schema.fromJsonString(CodexTranscriptRateLimitsEvent),
229247
);
@@ -247,13 +265,15 @@ export function parseCodexTranscriptRateLimitsSnapshot(
247265
if (Option.isNone(decoded)) return null;
248266
const observedAtMs = Date.parse(decoded.value.timestamp);
249267
if (!Number.isFinite(observedAtMs)) return null;
250-
const limits = decoded.value.payload.info.rate_limits;
268+
const limits = decoded.value.payload.rate_limits;
269+
if (!isCodexAccountLimit(limits.limit_id)) return null;
251270
const primary = codexTranscriptWindow(limits.primary);
252271
const secondary = codexTranscriptWindow(limits.secondary);
253272
return {
254273
observedAtMs,
255274
response: {
256275
rateLimits: {
276+
...(limits.limit_id === undefined ? {} : { limitId: limits.limit_id }),
257277
...(limits.plan_type === undefined ? {} : { planType: limits.plan_type }),
258278
...(primary === undefined ? {} : { primary }),
259279
...(secondary === undefined ? {} : { secondary }),
@@ -434,7 +454,7 @@ function codexWindow(
434454
export function normalizeCodexSubscriptionLimits(
435455
response: CodexUsageLimitsResponse | undefined,
436456
): UsageProviderLimits | null {
437-
if (!response) return null;
457+
if (!response || !isCodexAccountLimit(response.rateLimits.limitId)) return null;
438458

439459
const meteredWindows = [
440460
codexWindow(response.rateLimits.primary, "primary"),

‎apps/server/src/usage/usageTranscriptReader.test.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@ import { describe, expect, it } from "@effect/vitest";
77

88
import { readFreshCodexRateLimitsSnapshot } from "./usageTranscriptReader.ts";
99

10-
function rateLimitLine(timestamp: string, usedPercent: number): string {
10+
function rateLimitLine(timestamp: string, usedPercent: number, limitId = "codex"): string {
1111
return JSON.stringify({
1212
timestamp,
1313
type: "event_msg",
1414
payload: {
1515
type: "token_count",
16-
info: {
17-
rate_limits: {
18-
primary: null,
19-
secondary: {
20-
used_percent: usedPercent,
21-
window_minutes: 10_080,
22-
resets_at: 1_788_000_000,
23-
},
24-
plan_type: "prolite",
16+
info: null,
17+
rate_limits: {
18+
limit_id: limitId,
19+
primary: null,
20+
secondary: {
21+
used_percent: usedPercent,
22+
window_minutes: 10_080,
23+
resets_at: 1_788_000_000,
2524
},
25+
plan_type: "prolite",
2626
},
2727
},
2828
});
@@ -36,9 +36,17 @@ describe("Codex transcript rate-limit snapshots", () => {
3636
const olderAt = "2026-08-27T02:09:00.000Z";
3737
const newerAt = "2026-08-27T02:10:00.000Z";
3838
const futureAt = "2036-08-27T02:10:00.000Z";
39+
const sparkAt = "2026-08-27T02:10:20.000Z";
3940
await NodeFSP.writeFile(
4041
path,
41-
`${rateLimitLine(olderAt, 40)}\n${rateLimitLine(newerAt, 57)}\n${rateLimitLine(futureAt, 99)}\n`,
42+
[
43+
rateLimitLine(olderAt, 40),
44+
rateLimitLine(newerAt, 57),
45+
// A newer model-scoped bucket must not shadow the account limit.
46+
rateLimitLine(sparkAt, 0, "codex_bengalfox"),
47+
rateLimitLine(futureAt, 99),
48+
"",
49+
].join("\n"),
4250
);
4351
const stats = await NodeFSP.stat(path);
4452
const files = [{ path, size: stats.size, mtimeMs: stats.mtimeMs }];

0 commit comments

Comments
 (0)