Skip to content

Commit 80e1e4d

Browse files
shivamhwpPR Batch Testerclaudemaria-rcks
authored
fix(server): Codex first-request usage limits keep their reset time (#27791)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent ac5e48c commit 80e1e4d

2 files changed

Lines changed: 146 additions & 25 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5774,8 +5774,15 @@ describe("CodexAdapterV2 post-settle continuation", () => {
57745774
);
57755775

57765776
it.effect.each([
5777+
// regression: #27773 a first-request 429 only notifies the reset-less premium limit
57775778
{
5778-
name: "usage",
5779+
name: "first-request-reset",
5780+
code: "usageLimitExceeded",
5781+
notification: false,
5782+
expectedClass: "usage_limit",
5783+
},
5784+
{
5785+
name: "newer-reset-during-read",
57795786
code: "usageLimitExceeded",
57805787
notification: false,
57815788
expectedClass: "usage_limit",
@@ -5891,6 +5898,37 @@ describe("CodexAdapterV2 post-settle continuation", () => {
58915898
},
58925899
},
58935900
};
5901+
const readsReset =
5902+
scenario.name === "first-request-reset" || scenario.name === "newer-reset-during-read";
5903+
// Usage limits with no known reset read the account before publishing their terminal.
5904+
const readsAccount =
5905+
scenario.expectedClass === "usage_limit" &&
5906+
!("expectedCode" in scenario) &&
5907+
scenario.name !== "deferred-reset";
5908+
const premium = {
5909+
...snapshot,
5910+
frame: {
5911+
method: "account/rateLimits/updated",
5912+
params: { rateLimits: { limitId: "premium", primary: null, secondary: null } },
5913+
},
5914+
};
5915+
const newerSnapshot = {
5916+
...snapshot,
5917+
frame: {
5918+
method: "account/rateLimits/updated",
5919+
params: {
5920+
rateLimits: {
5921+
limitId: "codex",
5922+
primary: { usedPercent: 100, resetsAt: 2000200000, windowDurationMins: 300 },
5923+
},
5924+
},
5925+
},
5926+
};
5927+
const readRequest = {
5928+
type: "expect_outbound" as const,
5929+
label: "account/rateLimits/read",
5930+
frame: { id: 4, method: "account/rateLimits/read", params: null },
5931+
};
58945932
const transcript = makeCodexReplayTranscript({
58955933
scenario: `codex-limit-${scenario.name}`,
58965934
entries: [
@@ -5915,6 +5953,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
59155953
},
59165954
]
59175955
: []),
5956+
...(readsReset ? [premium] : []),
59185957
...(scenario.name === "deferred-reset" ? [snapshot] : []),
59195958
...(scenario.name === "deferred-reset"
59205959
? [
@@ -5999,7 +6038,36 @@ describe("CodexAdapterV2 post-settle continuation", () => {
59996038
},
60006039
},
60016040
},
6002-
...(scenario.name === "late-reset" || "resetAfter" in scenario ? [snapshot] : []),
6041+
...(readsAccount ? [readRequest] : []),
6042+
...(scenario.name === "newer-reset-during-read" ? [newerSnapshot] : []),
6043+
...(readsReset
6044+
? [
6045+
{
6046+
type: "emit_inbound" as const,
6047+
label: "account/rateLimits/read",
6048+
frame: {
6049+
id: 4,
6050+
result: {
6051+
...premium.frame.params,
6052+
rateLimitsByLimitId: { codex: snapshot.frame.params.rateLimits },
6053+
},
6054+
},
6055+
},
6056+
]
6057+
: []),
6058+
...(readsAccount && !readsReset
6059+
? [
6060+
{
6061+
type: "emit_inbound" as const,
6062+
label: "account/rateLimits/read",
6063+
frame: { id: 4, error: { code: -32600, message: "Not signed in." } },
6064+
},
6065+
]
6066+
: []),
6067+
...(scenario.name === "late-reset"
6068+
? [{ ...snapshot, label: "account/rateLimits/updated/late" }]
6069+
: []),
6070+
...("resetAfter" in scenario ? [snapshot] : []),
60036071
...(scenario.name === "deferred-reset"
60046072
? [
60056073
{
@@ -6038,17 +6106,24 @@ describe("CodexAdapterV2 post-settle continuation", () => {
60386106
});
60396107
const resetReceipt = yield* Deferred.make<void>();
60406108
const snapshotReceipt = yield* Deferred.make<void>();
6109+
const terminalReceipt = yield* Deferred.make<void>();
60416110
const harness = yield* makeCodexReplayHarness(
60426111
transcript,
60436112
(event) =>
6044-
event.type === "turn_item.updated" &&
6045-
event.turnItem.type === "error" &&
6046-
event.turnItem.failure.resetAt === resetAt
6047-
? Deferred.succeed(resetReceipt, undefined)
6048-
: Effect.void,
6049-
undefined,
6113+
event.type === "turn.terminal"
6114+
? Deferred.succeed(terminalReceipt, undefined)
6115+
: event.type === "turn_item.updated" &&
6116+
event.turnItem.type === "error" &&
6117+
event.turnItem.failure.resetAt === resetAt
6118+
? Deferred.succeed(resetReceipt, undefined)
6119+
: Effect.void,
60506120
undefined,
60516121
undefined,
6122+
// The late reset must land after the terminal its failed read released.
6123+
(entry) =>
6124+
entry.label === "account/rateLimits/updated/late"
6125+
? Deferred.await(terminalReceipt)
6126+
: Effect.void,
60526127
() => Deferred.succeed(snapshotReceipt, undefined),
60536128
);
60546129
yield* harness.runtime.startTurn(
@@ -6066,7 +6141,10 @@ describe("CodexAdapterV2 post-settle continuation", () => {
60666141
if (terminal?.status !== "failed") return;
60676142
assert.equal(terminal.failure.class, scenario.expectedClass);
60686143
assert.equal(terminal.threadDisposition, "reusable");
6069-
if (scenario.name === "deferred-reset") assert.equal(terminal.failure.resetAt, resetAt);
6144+
if (scenario.name === "deferred-reset" || scenario.name === "first-request-reset")
6145+
assert.equal(terminal.failure.resetAt, resetAt);
6146+
if (scenario.name === "newer-reset-during-read")
6147+
assert.equal(terminal.failure.resetAt, "2033-05-20T11:06:40.000Z");
60706148
if (scenario.name === "matching-details")
60716149
assert.equal(terminal.failure.message, "Detailed provider allowance explanation.");
60726150
if ("resetAfter" in scenario) {

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4567,6 +4567,26 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
45674567
}).pipe(Effect.orDie),
45684568
);
45694569

4570+
// A run's first request can 429 before Codex pushes a codex snapshot.
4571+
const readUsageLimitReset = Effect.gen(function* () {
4572+
const sent = yield* Ref.get(rateLimitSnapshot);
4573+
const response = yield* client
4574+
.request("account/rateLimits/read", null)
4575+
.pipe(Effect.timeout("10 seconds"));
4576+
const read = response.rateLimitsByLimitId?.codex ?? response.rateLimits;
4577+
// A notification that landed during the read is newer than it.
4578+
yield* Ref.update(rateLimitSnapshot, (current) =>
4579+
current === sent ? mergeCodexRateLimits(current, read) : current,
4580+
);
4581+
}).pipe(
4582+
// The terminal waits on this read, so no failure may skip it; interruption ends both.
4583+
Effect.catchCauseIf(
4584+
(cause) => !Cause.hasInterrupts(cause),
4585+
(cause) =>
4586+
Effect.logWarning("orchestration-v2.codex-usage-limit-reset-read-failed", { cause }),
4587+
),
4588+
);
4589+
45704590
yield* client.handleServerNotification("thread/tokenUsage/updated", (payload) =>
45714591
Effect.gen(function* () {
45724592
accumulateCodexTurnTokenUsage(
@@ -5916,7 +5936,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59165936
},
59175937
);
59185938

5919-
const emitRootTerminal = Effect.fnUntraced(function* (
5939+
const publishRootTerminal = Effect.fnUntraced(function* (
59205940
context: ActiveCodexTurnContext,
59215941
event: CodexRootTerminalEvent,
59225942
) {
@@ -5933,8 +5953,38 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59335953
}
59345954
: event;
59355955
yield* emitProviderEvent(current);
5956+
if (current.status === "failed" && current.failure.class === "usage_limit") {
5957+
const item = makeProviderFailureTurnItem({
5958+
driver: CODEX_PROVIDER,
5959+
threadId: context.input.threadId,
5960+
runId: context.input.runId,
5961+
nodeId: context.input.rootNodeId,
5962+
providerThreadId: current.providerThreadId,
5963+
providerTurnId: current.providerTurnId,
5964+
itemOrdinal: current.failureItemOrdinal,
5965+
failure: current.failure,
5966+
occurredAt: yield* DateTime.now,
5967+
});
5968+
yield* Ref.update(limitedTurnItems, (items) =>
5969+
new Map(items).set(context.providerThread.id, item),
5970+
);
5971+
}
5972+
});
5973+
5974+
const emitRootTerminal = Effect.fnUntraced(function* (
5975+
context: ActiveCodexTurnContext,
5976+
event: CodexRootTerminalEvent,
5977+
) {
5978+
const awaitsReset =
5979+
event.status === "failed" &&
5980+
event.failure.class === "usage_limit" &&
5981+
event.failure.resetAt == null &&
5982+
// The account read only reports the ordinary allowance, never a sharing cap's reset.
5983+
!classifyCodexManagedError(event.failure.code)?.usageLimit &&
5984+
codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)) === null;
5985+
if (!awaitsReset) yield* publishRootTerminal(context, event);
59365986
const continued = continuedRuns.get(context.providerTurnId) ?? [context];
5937-
if (current.status !== "completed") {
5987+
if (event.status !== "completed") {
59385988
yield* Ref.update(
59395989
terminalizedNonCompletedNativeTurns,
59405990
(nativeTurns) =>
@@ -5950,20 +6000,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59506000
if (nativeThreadId != null && rootProviderThread !== undefined) {
59516001
rootProviderThreads.set(nativeThreadId, { ...rootProviderThread, status: "idle" });
59526002
}
5953-
if (current.status === "failed" && current.failure.class === "usage_limit") {
5954-
const item = makeProviderFailureTurnItem({
5955-
driver: CODEX_PROVIDER,
5956-
threadId: context.input.threadId,
5957-
runId: context.input.runId,
5958-
nodeId: context.input.rootNodeId,
5959-
providerThreadId: current.providerThreadId,
5960-
providerTurnId: current.providerTurnId,
5961-
itemOrdinal: current.failureItemOrdinal,
5962-
failure: current.failure,
5963-
occurredAt: yield* DateTime.now,
5964-
});
5965-
yield* Ref.update(limitedTurnItems, (items) =>
5966-
new Map(items).set(context.providerThread.id, item),
6003+
// Notification handlers block response routing, so the read and terminal get a fiber.
6004+
if (awaitsReset) {
6005+
yield* readUsageLimitReset.pipe(
6006+
Effect.andThen(
6007+
turnTerminalizationPermit.withPermits(1)(publishRootTerminal(context, event)),
6008+
),
6009+
Effect.forkIn(scope, { startImmediately: true }),
59676010
);
59686011
}
59696012
});

0 commit comments

Comments
 (0)