The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 00:45 UTC
fix(server): Codex first-request usage limits keep their reset time (#27791)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: maria-rcks <maria@kuuro.net>
shivam committed
Nov 10, 2026 at 19:35 UTC
80e1e4d14dddae107d017ed8a1baf0acd3389f17
2 files changed
+146
-25
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts
+87
-9
@@ -5774,8 +5774,15 @@ describe("CodexAdapterV2 post-settle continuation", () => {
5774
);
5775
5776
it.effect.each([
5777
+ // regression: #27773 a first-request 429 only notifies the reset-less premium limit
5778
{
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",
5786
code: "usageLimitExceeded",
5787
notification: false,
5788
expectedClass: "usage_limit",
@@ -5891,6 +5898,37 @@ describe("CodexAdapterV2 post-settle continuation", () => {
5898
},
5899
},
5900
};
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
+ };
5932
const transcript = makeCodexReplayTranscript({
5933
scenario: `codex-limit-${scenario.name}`,
5934
entries: [
@@ -5915,6 +5953,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
5953
},
5954
]
5955
: []),
5956
+ ...(readsReset ? [premium] : []),
5957
...(scenario.name === "deferred-reset" ? [snapshot] : []),
5958
...(scenario.name === "deferred-reset"
5959
? [
@@ -5999,7 +6038,36 @@ describe("CodexAdapterV2 post-settle continuation", () => {
6038
},
6039
},
6040
},
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] : []),
6071
...(scenario.name === "deferred-reset"
6072
? [
6073
{
@@ -6038,17 +6106,24 @@ describe("CodexAdapterV2 post-settle continuation", () => {
6106
});
6107
const resetReceipt = yield* Deferred.make<void>();
6108
const snapshotReceipt = yield* Deferred.make<void>();
6109
+ const terminalReceipt = yield* Deferred.make<void>();
6110
const harness = yield* makeCodexReplayHarness(
6111
transcript,
6112
(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,
6120
undefined,
6121
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,
6127
() => Deferred.succeed(snapshotReceipt, undefined),
6128
);
6129
yield* harness.runtime.startTurn(
@@ -6066,7 +6141,10 @@ describe("CodexAdapterV2 post-settle continuation", () => {
6141
if (terminal?.status !== "failed") return;
6142
assert.equal(terminal.failure.class, scenario.expectedClass);
6143
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");
6148
if (scenario.name === "matching-details")
6149
assert.equal(terminal.failure.message, "Detailed provider allowance explanation.");
6150
if ("resetAfter" in scenario) {
apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts
+59
-16
@@ -4567,6 +4567,26 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
4567
}).pipe(Effect.orDie),
4568
);
4569
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
+
4590
yield* client.handleServerNotification("thread/tokenUsage/updated", (payload) =>
4591
Effect.gen(function* () {
4592
accumulateCodexTurnTokenUsage(
@@ -5916,7 +5936,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5936
},
5937
);
5938
5919
- const emitRootTerminal = Effect.fnUntraced(function* (
5939
+ const publishRootTerminal = Effect.fnUntraced(function* (
5940
context: ActiveCodexTurnContext,
5941
event: CodexRootTerminalEvent,
5942
) {
@@ -5933,8 +5953,38 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
5953
}
5954
: event;
5955
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);
5986
const continued = continuedRuns.get(context.providerTurnId) ?? [context];
5937
- if (current.status !== "completed") {
5987
+ if (event.status !== "completed") {
5988
yield* Ref.update(
5989
terminalizedNonCompletedNativeTurns,
5990
(nativeTurns) =>
@@ -5950,20 +6000,13 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
6000
if (nativeThreadId != null && rootProviderThread !== undefined) {
6001
rootProviderThreads.set(nativeThreadId, { ...rootProviderThread, status: "idle" });
6002
}
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 }),
6010
);
6011
}
6012
});