Skip to content

Commit 20594ca

Browse files
perf(usage): skip the publish when a runtime rate-limit event changes nothing
Codex sends account/rateLimits/updated beside every token-usage tick, almost always with unchanged numbers. The merge now compares per window on the way through and hands back the published object itself when nothing moved, so the ingestion path drops the event by identity without allocating a snapshot or running a structural equality. Also records in resolveUsageLimitsAfterProbe why a successful probe replaces a runtime update that landed while it ran. Co-Authored-By: Claude Code <[email protected]>
1 parent cc62a4e commit 20594ca

3 files changed

Lines changed: 109 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ export const makeManagedServerProvider = Effect.fn("makeManagedServerProvider")(
191191
update,
192192
checkedAt: update.checkedAt,
193193
});
194-
if (Equal.equals(state.snapshot.usageLimits, usageLimits)) {
194+
// `applyUsageLimitsUpdate` hands back the same object when nothing
195+
// moved, which is the common case for Codex's per-tick notification.
196+
if (usageLimits === state.snapshot.usageLimits) {
195197
return [null, state] as const;
196198
}
197199
const snapshot = withUsageLimits(state.snapshot, usageLimits);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { applyUsageLimitsUpdate, resolveUsageLimitsAfterProbe } from "./providerUsageLimits.ts";
4+
5+
const checkedAt = "2026-09-03T12:00:00.000Z";
6+
const session = {
7+
id: "five_hour",
8+
kind: "session",
9+
label: "Session",
10+
usedPercent: 40,
11+
windowDurationMins: 300,
12+
resetsAt: "2026-09-03T14:00:00.000Z",
13+
} as const;
14+
const weekly = {
15+
id: "seven_day",
16+
kind: "weekly",
17+
label: "Weekly",
18+
usedPercent: 20,
19+
windowDurationMins: 10_080,
20+
} as const;
21+
const published = { checkedAt, windows: [session, weekly] };
22+
23+
describe("applyUsageLimitsUpdate", () => {
24+
it("returns the published object itself when no window moved", () => {
25+
// Codex repeats the same numbers beside every token-usage tick; the
26+
// ingestion path relies on identity to skip the publish.
27+
const next = applyUsageLimitsUpdate({
28+
previous: published,
29+
checkedAt: "2026-09-03T12:00:05.000Z",
30+
update: {
31+
windows: [
32+
{ ...weekly },
33+
{ id: "five_hour", kind: "session", label: "Session", usedPercent: 40 },
34+
],
35+
},
36+
});
37+
expect(next).toBe(published);
38+
});
39+
40+
it("upserts by id and keeps the reset a percent-only update omits", () => {
41+
const next = applyUsageLimitsUpdate({
42+
previous: published,
43+
checkedAt: "2026-09-03T12:00:05.000Z",
44+
update: {
45+
windows: [{ id: "five_hour", kind: "session", label: "Session", usedPercent: 55 }],
46+
},
47+
});
48+
expect(next).not.toBe(published);
49+
expect(next).toEqual({
50+
checkedAt: "2026-09-03T12:00:05.000Z",
51+
windows: [{ ...session, usedPercent: 55 }, weekly],
52+
});
53+
});
54+
55+
it("leaves an unsupported account and an empty update alone", () => {
56+
const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };
57+
expect(
58+
applyUsageLimitsUpdate({ previous: unsupported, checkedAt, update: { windows: [session] } }),
59+
).toBe(unsupported);
60+
expect(
61+
applyUsageLimitsUpdate({ previous: published, checkedAt, update: { windows: [] } }),
62+
).toBe(published);
63+
});
64+
});
65+
66+
describe("resolveUsageLimitsAfterProbe", () => {
67+
it("keeps the last good windows through a failed probe but not an unsupported one", () => {
68+
const failed = { checkedAt, windows: [], unavailable: { reason: "probeFailed" as const } };
69+
const unsupported = { checkedAt, windows: [], unavailable: { reason: "unsupported" as const } };
70+
expect(resolveUsageLimitsAfterProbe({ published, probed: failed })).toBe(published);
71+
expect(resolveUsageLimitsAfterProbe({ published, probed: unsupported })).toBe(unsupported);
72+
expect(resolveUsageLimitsAfterProbe({ published: undefined, probed: failed })).toBe(failed);
73+
});
74+
});

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ export function applyUsageLimitsUpdate(input: {
6767
return previous;
6868
}
6969
const merged = new Map(previous?.windows.map((window) => [window.id, window] as const));
70+
// Codex sends this notification beside every token-usage tick, almost
71+
// always with unchanged numbers. Decide "nothing changed" per window on
72+
// the way through so the no-op case never allocates a new snapshot.
73+
let changed = false;
7074
for (const window of update.windows) {
7175
const existing = merged.get(window.id);
72-
merged.set(window.id, {
76+
const next: ServerProviderUsageWindow = {
7377
...window,
7478
usedPercent: clampPercent(window.usedPercent),
7579
...(window.resetsAt === undefined && existing?.resetsAt !== undefined
@@ -78,16 +82,42 @@ export function applyUsageLimitsUpdate(input: {
7882
...(window.windowDurationMins === undefined && existing?.windowDurationMins !== undefined
7983
? { windowDurationMins: existing.windowDurationMins }
8084
: {}),
81-
});
85+
};
86+
if (existing === undefined || !usageWindowEquals(existing, next)) {
87+
merged.set(window.id, next);
88+
changed = true;
89+
}
90+
}
91+
if (!changed && previous !== undefined && previous.unavailable === undefined) {
92+
return previous;
8293
}
8394
return makeUsageLimits({ checkedAt: input.checkedAt, windows: merged.values() });
8495
}
8596

97+
function usageWindowEquals(a: ServerProviderUsageWindow, b: ServerProviderUsageWindow): boolean {
98+
return (
99+
a.id === b.id &&
100+
a.kind === b.kind &&
101+
a.label === b.label &&
102+
a.usedPercent === b.usedPercent &&
103+
a.resetsAt === b.resetsAt &&
104+
a.windowDurationMins === b.windowDurationMins
105+
);
106+
}
107+
86108
/**
87109
* Choose what to publish after a status probe finishes. A probe that failed
88110
* this time must not wipe bars a previous probe or a turn already
89111
* established, so the last good snapshot stays; `unsupported` is
90112
* authoritative and replaces them.
113+
*
114+
* A successful probe replaces the published windows outright, including any
115+
* runtime update that landed while it was running. That is a deliberate
116+
* trade-off: the Codex and Claude reads take a few seconds at most, the
117+
* probe is the fresher full read in every case except that window, and the
118+
* per-window epoch bookkeeping needed to reconcile the two was more code
119+
* than the sub-second regression it prevented. The next runtime event
120+
* corrects it.
91121
*/
92122
export function resolveUsageLimitsAfterProbe(input: {
93123
readonly published: ServerProviderUsageLimits | undefined;

0 commit comments

Comments
 (0)