Skip to content

Commit 4a019a7

Browse files
committed
fix(codex): keep sharing cap resets independent of Codex windows
1 parent 5060576 commit 4a019a7

3 files changed

Lines changed: 48 additions & 23 deletions

File tree

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
917917
Effect.void,
918918
onRequest: (method: string, params: unknown) => Effect.Effect<void> = () => Effect.void,
919919
readChildMetadata?: Parameters<typeof withCodexReplayChildMetadata>[2],
920+
onUsageLimits?: CodexAdapterV2.CodexAdapterV2Options["onUsageLimits"],
920921
) =>
921922
Effect.gen(function* () {
922923
const fileSystem = yield* FileSystem.FileSystem;
@@ -961,6 +962,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
961962
settings: DEFAULT_CODEX_SETTINGS,
962963
environment: {},
963964
clientFactory,
965+
...(onUsageLimits ? { onUsageLimits } : {}),
964966
crypto: yield* Crypto.Crypto,
965967
fileSystem,
966968
idAllocator,
@@ -3977,7 +3979,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39773979
message: CHATGPT_SHARING_LIMIT_MESSAGE,
39783980
notification: true,
39793981
willRetry: true,
3980-
usedBefore: 0,
3982+
usedBefore: 100,
39813983
expectedClass: "usage_limit",
39823984
expectedCode: "subscription_sharing_usage_limit_exceeded",
39833985
expectedMessage: "Usage settings",
@@ -4177,12 +4179,18 @@ describe("CodexAdapterV2 post-settle continuation", () => {
41774179
],
41784180
});
41794181
const resetReceipt = yield* Deferred.make<void>();
4180-
const harness = yield* makeCodexReplayHarness(transcript, (event) =>
4181-
event.type === "turn_item.updated" &&
4182-
event.turnItem.type === "error" &&
4183-
event.turnItem.failure.resetAt === resetAt
4184-
? Deferred.succeed(resetReceipt, undefined)
4185-
: Effect.void,
4182+
const snapshotReceipt = yield* Deferred.make<void>();
4183+
const harness = yield* makeCodexReplayHarness(
4184+
transcript,
4185+
(event) =>
4186+
event.type === "turn_item.updated" &&
4187+
event.turnItem.type === "error" &&
4188+
event.turnItem.failure.resetAt === resetAt
4189+
? Deferred.succeed(resetReceipt, undefined)
4190+
: Effect.void,
4191+
undefined,
4192+
undefined,
4193+
() => Deferred.succeed(snapshotReceipt, undefined),
41864194
);
41874195
yield* harness.runtime.startTurn(
41884196
makeCodexTestTurnInput({
@@ -4202,7 +4210,18 @@ describe("CodexAdapterV2 post-settle continuation", () => {
42024210
if (scenario.name === "deferred-reset") assert.equal(terminal.failure.resetAt, resetAt);
42034211
if (scenario.name === "matching-details")
42044212
assert.equal(terminal.failure.message, "Detailed provider allowance explanation.");
4205-
if (scenario.name === "late-reset" || "resetAfter" in scenario) {
4213+
if ("resetAfter" in scenario) {
4214+
yield* Deferred.await(snapshotReceipt);
4215+
assert.isFalse(
4216+
harness.events.some(
4217+
(event) =>
4218+
event.type === "turn_item.updated" &&
4219+
event.turnItem.type === "error" &&
4220+
event.turnItem.failure.resetAt != null,
4221+
),
4222+
);
4223+
}
4224+
if (scenario.name === "late-reset") {
42064225
yield* Deferred.await(resetReceipt);
42074226
const item = harness.events.find(
42084227
(event) =>
@@ -4217,10 +4236,7 @@ describe("CodexAdapterV2 post-settle continuation", () => {
42174236
if ("expectedCode" in scenario && "expectedMessage" in scenario) {
42184237
assert.equal(terminal.failure.code, scenario.expectedCode);
42194238
assert.include(terminal.failure.message, scenario.expectedMessage);
4220-
assert.equal(
4221-
terminal.failure.resetAt,
4222-
"usedBefore" in scenario && scenario.usedBefore >= 100 ? resetAt : null,
4223-
);
4239+
assert.equal(terminal.failure.resetAt, null);
42244240
}
42254241
}).pipe(
42264242
Effect.provide(

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,16 @@ function withCodexManagedFailure(
11141114
};
11151115
}
11161116

1117+
function failureResetAt(
1118+
failure: OrchestrationV2ProviderFailure,
1119+
snapshot: CodexRateLimitSnapshot | undefined,
1120+
) {
1121+
// Sharing caps have no reset timestamp in the ordinary Codex allowance snapshot.
1122+
return classifyCodexManagedError(failure.code)?.usageLimit
1123+
? null
1124+
: codexUsageLimitResetAt(snapshot);
1125+
}
1126+
11171127
interface ActiveCodexTurnContext {
11181128
closedCommandItemIds?: ReadonlySet<string>;
11191129
latestProviderFailure?: {
@@ -4270,8 +4280,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
42704280
yield* Ref.update(rateLimitSnapshot, (previous) =>
42714281
mergeCodexRateLimits(previous, payload.rateLimits),
42724282
);
4273-
const resetAt = codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot));
4283+
const snapshot = yield* Ref.get(rateLimitSnapshot);
42744284
for (const item of (yield* Ref.get(limitedTurnItems)).values()) {
4285+
const resetAt = failureResetAt(item.failure, snapshot);
42754286
if (resetAt === null || item.failure.resetAt != null) continue;
42764287
const updated = {
42774288
...item,
@@ -5581,7 +5592,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
55815592
failure.class === "usage_limit"
55825593
? {
55835594
...failure,
5584-
resetAt: codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)),
5595+
resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot)),
55855596
}
55865597
: failure,
55875598
...(input.providerRetry === undefined
@@ -5618,7 +5629,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
56185629
...event.failure,
56195630
resetAt:
56205631
event.failure.resetAt ??
5621-
codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)),
5632+
failureResetAt(event.failure, yield* Ref.get(rateLimitSnapshot)),
56225633
},
56235634
}
56245635
: event;

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,6 @@ const usageLimitCodes = new Set<string>([
6464
"subscription_sharing_usage_limit_exceeded",
6565
"subscription_sharing_workplace_individual_usage_limit_exceeded",
6666
]);
67-
// Codex can stream only ChatGPT's prose for a sharing failure, without its code.
68-
const proseCodes = [
69-
[
70-
"has reached their Subscription Sharing usage limit",
71-
"subscription_sharing_usage_limit_exceeded",
72-
],
73-
] as const;
7467

7568
function unwrapError(value: unknown): unknown {
7669
if (typeof value === "string") {
@@ -107,7 +100,12 @@ export function classifyCodexManagedError(value: unknown) {
107100
const code =
108101
sharingCode(error) ??
109102
messages.map(sharingCode).find((code) => code !== undefined) ??
110-
proseCodes.find(([prose]) => messages.some((message) => message.includes(prose)))?.[1];
103+
// Codex can stream ChatGPT's sharing-limit prose without its code.
104+
(messages.some((message) =>
105+
message.includes("has reached their Subscription Sharing usage limit"),
106+
)
107+
? "subscription_sharing_usage_limit_exceeded"
108+
: undefined);
111109
if (code === undefined) return undefined;
112110
const failure = { ...failures[code], code, usageLimit: usageLimitCodes.has(code) };
113111
const text = messages.join(" ");

0 commit comments

Comments
 (0)