GeezerVison is showing github.com — fix(codex): sharing usage limits offer the existing recovery options … · pingdotgg/t3code@935818e
File treeExpand file treeCollapse file tree Open diff view settings orchestration-v2/Adapters
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts +101-13Lines changed: 101 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -480,6 +480,8 @@ describe("CodexAdapterV2 fork boundary", () => { | 480 | 480 | }); | 481 | 481 |
| 482 | 482 | const DEFAULT_CODEX_SETTINGS = Schema.decodeSync(CodexSettings)({}); | | 483 | +const CHATGPT_SHARING_LIMIT_MESSAGE = | | 484 | + "stream disconnected before completion: The ChatGPT user has reached their Subscription Sharing usage limit. Ask the user to try again after their usage limit resets or use an API key instead."; | 483 | 485 | const CODEX_TEST_MODEL_SELECTION = { | 484 | 486 | instanceId: CodexAdapterV2.CODEX_DEFAULT_INSTANCE_ID, | 485 | 487 | model: "gpt-5.4", | @@ -1235,6 +1237,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 1235 | 1237 | onRequest: (method: string, params: unknown) => Effect.Effect<void> = () => Effect.void, | 1236 | 1238 | readChildMetadata?: Parameters<typeof withCodexReplayChildMetadata>[2], | 1237 | 1239 | beforeEmitInbound?: CodexReplay.CodexAppServerReplayDriver["beforeEmitInbound"], | | 1240 | + onUsageLimits?: CodexAdapterV2.CodexAdapterV2Options["onUsageLimits"], | 1238 | 1241 | ) => | 1239 | 1242 | Effect.gen(function* () { | 1240 | 1243 | const fileSystem = yield* FileSystem.FileSystem; | @@ -1292,6 +1295,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 1292 | 1295 | settings: DEFAULT_CODEX_SETTINGS, | 1293 | 1296 | environment: {}, | 1294 | 1297 | clientFactory, | | 1298 | + ...(onUsageLimits ? { onUsageLimits } : {}), | 1295 | 1299 | digest: yield* Digest.Digest, | 1296 | 1300 | fileSystem, | 1297 | 1301 | idAllocator, | @@ -5612,14 +5616,58 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5612 | 5616 | code: "other", | 5613 | 5617 | details: "subscription_sharing_workplace_individual_usage_limit_exceeded", | 5614 | 5618 | notification: true, | | 5619 | + usedBefore: 100, | | 5620 | + expectedClass: "usage_limit", | | 5621 | + expectedCode: "subscription_sharing_workplace_individual_usage_limit_exceeded", | | 5622 | + expectedMessage: "workspace admin", | | 5623 | + }, | | 5624 | + // regression: #25333 ChatGPT sharing limit prose classifies as a usage limit | | 5625 | + { | | 5626 | + name: "chatgpt-sharing-prose-retry", | | 5627 | + code: "responseStreamDisconnected", | | 5628 | + errorInfo: { responseStreamDisconnected: { httpStatusCode: null } }, | | 5629 | + message: CHATGPT_SHARING_LIMIT_MESSAGE, | | 5630 | + notification: true, | | 5631 | + willRetry: true, | | 5632 | + usedBefore: 100, | | 5633 | + expectedClass: "usage_limit", | | 5634 | + expectedCode: "subscription_sharing_usage_limit_exceeded", | | 5635 | + expectedMessage: "Usage settings", | | 5636 | + }, | | 5637 | + { | | 5638 | + name: "chatgpt-sharing-prose", | | 5639 | + code: "responseStreamDisconnected", | | 5640 | + errorInfo: { responseStreamDisconnected: { httpStatusCode: null } }, | | 5641 | + message: CHATGPT_SHARING_LIMIT_MESSAGE, | | 5642 | + notification: false, | | 5643 | + resetAfter: true, | | 5644 | + expectedClass: "usage_limit", | | 5645 | + expectedCode: "subscription_sharing_usage_limit_exceeded", | | 5646 | + expectedMessage: "Usage settings", | | 5647 | + }, | | 5648 | + { | | 5649 | + name: "sharing-code-mention", | | 5650 | + code: "responseStreamDisconnected", | | 5651 | + errorInfo: { responseStreamDisconnected: { httpStatusCode: null } }, | | 5652 | + message: "Transport unavailable; this is not subscription_sharing_usage_limit_exceeded.", | | 5653 | + notification: false, | 5615 | 5654 | expectedClass: "provider_error", | 5616 | 5655 | }, | | 5656 | + { | | 5657 | + name: "sharing-code-mention-retry", | | 5658 | + code: "responseStreamDisconnected", | | 5659 | + errorInfo: { responseStreamDisconnected: { httpStatusCode: null } }, | | 5660 | + message: "Transport unavailable; this is not subscription_sharing_usage_limit_exceeded.", | | 5661 | + notification: true, | | 5662 | + willRetry: true, | | 5663 | + expectedClass: "transport_error", | | 5664 | + }, | 5617 | 5665 | ] as const)("classifies Codex terminal failures from $name evidence", (scenario) => | 5618 | 5666 | Effect.scoped( | 5619 | 5667 | Effect.gen(function* () { | 5620 | 5668 | const nativeThreadId = `native-limit-${scenario.name}`; | 5621 | 5669 | const nativeTurnId = `turn-limit-${scenario.name}`; | 5622 | | - const message = "Provider stopped this request."; | | 5670 | + const message = "message" in scenario ? scenario.message : "Provider stopped this request."; | 5623 | 5671 | const errorInfo = "errorInfo" in scenario ? scenario.errorInfo : scenario.code; | 5624 | 5672 | const resetAt = "2033-05-19T07:20:00.000Z"; | 5625 | 5673 | const snapshot = { | @@ -5639,6 +5687,26 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5639 | 5687 | scenario: `codex-limit-${scenario.name}`, | 5640 | 5688 | entries: [ | 5641 | 5689 | ...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt: "Continue." }), | | 5690 | + ...("usedBefore" in scenario | | 5691 | + ? [ | | 5692 | + { | | 5693 | + ...snapshot, | | 5694 | + frame: { | | 5695 | + method: "account/rateLimits/updated", | | 5696 | + params: { | | 5697 | + rateLimits: { | | 5698 | + limitId: "codex", | | 5699 | + primary: { | | 5700 | + usedPercent: scenario.usedBefore, | | 5701 | + resetsAt: 2000100000, | | 5702 | + windowDurationMins: 300, | | 5703 | + }, | | 5704 | + }, | | 5705 | + }, | | 5706 | + }, | | 5707 | + }, | | 5708 | + ] | | 5709 | + : []), | 5642 | 5710 | ...(scenario.name === "deferred-reset" ? [snapshot] : []), | 5643 | 5711 | ...(scenario.name === "deferred-reset" | 5644 | 5712 | ? [ | @@ -5686,7 +5754,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5686 | 5754 | params: { | 5687 | 5755 | threadId: nativeThreadId, | 5688 | 5756 | turnId: nativeTurnId, | 5689 | | - willRetry: scenario.name === "retry", | | 5757 | + willRetry: scenario.name === "retry" || "willRetry" in scenario, | 5690 | 5758 | error: { | 5691 | 5759 | message, | 5692 | 5760 | codexErrorInfo: errorInfo, | @@ -5723,7 +5791,7 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5723 | 5791 | }, | 5724 | 5792 | }, | 5725 | 5793 | }, | 5726 | | - ...(scenario.name === "late-reset" ? [snapshot] : []), | | 5794 | + ...(scenario.name === "late-reset" || "resetAfter" in scenario ? [snapshot] : []), | 5727 | 5795 | ...(scenario.name === "deferred-reset" | 5728 | 5796 | ? [ | 5729 | 5797 | { | @@ -5761,12 +5829,19 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5761 | 5829 | ], | 5762 | 5830 | }); | 5763 | 5831 | const resetReceipt = yield* Deferred.make<void>(); | 5764 | | - const harness = yield* makeCodexReplayHarness(transcript, (event) => | 5765 | | - event.type === "turn_item.updated" && | 5766 | | - event.turnItem.type === "error" && | 5767 | | - event.turnItem.failure.resetAt === resetAt | 5768 | | - ? Deferred.succeed(resetReceipt, undefined) | 5769 | | - : Effect.void, | | 5832 | + const snapshotReceipt = yield* Deferred.make<void>(); | | 5833 | + const harness = yield* makeCodexReplayHarness( | | 5834 | + transcript, | | 5835 | + (event) => | | 5836 | + event.type === "turn_item.updated" && | | 5837 | + event.turnItem.type === "error" && | | 5838 | + event.turnItem.failure.resetAt === resetAt | | 5839 | + ? Deferred.succeed(resetReceipt, undefined) | | 5840 | + : Effect.void, | | 5841 | + undefined, | | 5842 | + undefined, | | 5843 | + undefined, | | 5844 | + () => Deferred.succeed(snapshotReceipt, undefined), | 5770 | 5845 | ); | 5771 | 5846 | yield* harness.runtime.startTurn( | 5772 | 5847 | makeCodexTestTurnInput({ | @@ -5786,6 +5861,17 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5786 | 5861 | if (scenario.name === "deferred-reset") assert.equal(terminal.failure.resetAt, resetAt); | 5787 | 5862 | if (scenario.name === "matching-details") | 5788 | 5863 | assert.equal(terminal.failure.message, "Detailed provider allowance explanation."); | | 5864 | + if ("resetAfter" in scenario) { | | 5865 | + yield* Deferred.await(snapshotReceipt); | | 5866 | + assert.isFalse( | | 5867 | + harness.events.some( | | 5868 | + (event) => | | 5869 | + event.type === "turn_item.updated" && | | 5870 | + event.turnItem.type === "error" && | | 5871 | + event.turnItem.failure.resetAt != null, | | 5872 | + ), | | 5873 | + ); | | 5874 | + } | 5789 | 5875 | if (scenario.name === "late-reset") { | 5790 | 5876 | yield* Deferred.await(resetReceipt); | 5791 | 5877 | const item = harness.events.find( | @@ -5796,10 +5882,12 @@ describe("CodexAdapterV2 post-settle continuation", () => { | 5796 | 5882 | ); | 5797 | 5883 | assert.isDefined(item); | 5798 | 5884 | } | 5799 | | - if (scenario.name === "retry") assert.equal(terminal.retry?.attempt, 1); | 5800 | | - if (scenario.name === "chatgpt-workspace-limit") { | 5801 | | - assert.equal(terminal.failure.code, scenario.details); | 5802 | | - assert.include(terminal.failure.message, "workspace admin"); | | 5885 | + if (scenario.name === "retry" || "willRetry" in scenario) | | 5886 | + assert.equal(terminal.retry?.attempt, 1); | | 5887 | + if ("expectedCode" in scenario && "expectedMessage" in scenario) { | | 5888 | + assert.equal(terminal.failure.code, scenario.expectedCode); | | 5889 | + assert.include(terminal.failure.message, scenario.expectedMessage); | | 5890 | + assert.equal(terminal.failure.resetAt, null); | 5803 | 5891 | } | 5804 | 5892 | }).pipe( | 5805 | 5893 | Effect.provide( | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts +22-5Lines changed: 22 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1148,7 +1148,8 @@ function codexUsageLimitError( | 1148 | 1148 |
| 1149 | 1149 | /** | 1150 | 1150 | * ChatGPT sharing reports its own reason inside Codex's generic turn error, so | 1151 | | - * the code it names replaces Codex's, and clients pick a recovery from it. | | 1151 | + * the code it names replaces Codex's, and clients pick a recovery from it. Its | | 1152 | + * allowance limits get the usage-limit recovery; other sharing failures keep their class. | 1152 | 1153 | */ | 1153 | 1154 | function withCodexManagedFailure( | 1154 | 1155 | failure: OrchestrationV2ProviderFailure, | @@ -1157,7 +1158,22 @@ function withCodexManagedFailure( | 1157 | 1158 | const managed = classifyCodexManagedError(nativeError); | 1158 | 1159 | return managed === undefined | 1159 | 1160 | ? failure | 1160 | | - : { ...failure, message: managed.message, code: managed.code }; | | 1161 | + : { | | 1162 | + ...failure, | | 1163 | + message: managed.message, | | 1164 | + code: managed.code, | | 1165 | + ...(managed.usageLimit ? { class: "usage_limit" } : {}), | | 1166 | + }; | | 1167 | +} | | 1168 | + | | 1169 | +function failureResetAt( | | 1170 | + failure: OrchestrationV2ProviderFailure, | | 1171 | + snapshot: CodexRateLimitSnapshot | undefined, | | 1172 | +) { | | 1173 | + // Sharing caps have no reset timestamp in the ordinary Codex allowance snapshot. | | 1174 | + return classifyCodexManagedError(failure.code)?.usageLimit | | 1175 | + ? null | | 1176 | + : codexUsageLimitResetAt(snapshot); | 1161 | 1177 | } | 1162 | 1178 |
| 1163 | 1179 | interface ActiveCodexTurnContext { | @@ -4480,8 +4496,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 4480 | 4496 | const previous = yield* Ref.getAndUpdate(rateLimitSnapshot, (previous) => | 4481 | 4497 | mergeCodexRateLimits(previous, payload.rateLimits), | 4482 | 4498 | ); | 4483 | | - const resetAt = codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)); | | 4499 | + const snapshot = yield* Ref.get(rateLimitSnapshot); | 4484 | 4500 | for (const item of (yield* Ref.get(limitedTurnItems)).values()) { | | 4501 | + const resetAt = failureResetAt(item.failure, snapshot); | 4485 | 4502 | if (resetAt === null || item.failure.resetAt != null) continue; | 4486 | 4503 | const updated = { | 4487 | 4504 | ...item, | @@ -5826,7 +5843,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 5826 | 5843 | failure.class === "usage_limit" | 5827 | 5844 | ? { | 5828 | 5845 | ...failure, | 5829 | | - resetAt: codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)), | | 5846 | + resetAt: failureResetAt(failure, yield* Ref.get(rateLimitSnapshot)), | 5830 | 5847 | } | 5831 | 5848 | : failure, | 5832 | 5849 | ...(input.providerRetry === undefined | @@ -5866,7 +5883,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* ( | 5866 | 5883 | ...event.failure, | 5867 | 5884 | resetAt: | 5868 | 5885 | event.failure.resetAt ?? | 5869 | | - codexUsageLimitResetAt(yield* Ref.get(rateLimitSnapshot)), | | 5886 | + failureResetAt(event.failure, yield* Ref.get(rateLimitSnapshot)), | 5870 | 5887 | }, | 5871 | 5888 | } | 5872 | 5889 | : event; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/provider/CodexManagedErrors.test.ts +50Lines changed: 50 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ it("maps streamed failures to safe actionable messages and reconnect decisions", | 12 | 12 | message: | 13 | 13 | "ChatGPT could not validate this connection. Check the selected account and sharing permissions.", | 14 | 14 | code: "subscription_sharing_v2_invalid_user", | | 15 | + usageLimit: false, | 15 | 16 | }, | 16 | 17 | ); | 17 | 18 | assert.include( | @@ -58,3 +59,52 @@ it("preserves credentials for the current subscriber and permission error codes" | 58 | 59 | assert.strictEqual(classifyCodexManagedError({ error: { code } })?.code, code); | 59 | 60 | } | 60 | 61 | }); | | 62 | + | | 63 | +it("treats only ChatGPT sharing allowance limits as usage limits", () => { | | 64 | + assert.deepInclude( | | 65 | + classifyCodexManagedError( | | 66 | + "stream disconnected before completion: The ChatGPT user has reached their Subscription Sharing usage limit. Ask the user to try again after their usage limit resets or use an API key instead.", | | 67 | + ), | | 68 | + { code: "subscription_sharing_usage_limit_exceeded", usageLimit: true }, | | 69 | + ); | | 70 | + assert.isTrue( | | 71 | + classifyCodexManagedError("subscription_sharing_workplace_individual_usage_limit_exceeded")! | | 72 | + .usageLimit, | | 73 | + ); | | 74 | + assert.isFalse(classifyCodexManagedError("subscription_sharing_usage_unavailable")!.usageLimit); | | 75 | + assert.isUndefined( | | 76 | + classifyCodexManagedError("stream disconnected before completion: error decoding response"), | | 77 | + ); | | 78 | + assert.isUndefined(classifyCodexManagedError("You've hit your usage limit.")); | | 79 | +}); | | 80 | + | | 81 | +it("does not infer sharing codes from incidental mentions in turn errors", () => { | | 82 | + const mention = "Transport unavailable; this is not subscription_sharing_usage_limit_exceeded."; | | 83 | + for (const value of [ | | 84 | + mention, | | 85 | + { message: mention }, | | 86 | + { message: "Transport unavailable", additionalDetails: mention }, | | 87 | + { code: "other", message: mention }, | | 88 | + ]) { | | 89 | + assert.isUndefined(classifyCodexManagedError(value)); | | 90 | + } | | 91 | + assert.deepInclude( | | 92 | + classifyCodexManagedError({ | | 93 | + error: { code: "subscription_sharing_usage_unavailable", message: mention }, | | 94 | + }), | | 95 | + { code: "subscription_sharing_usage_unavailable", usageLimit: false }, | | 96 | + ); | | 97 | +}); | | 98 | + | | 99 | +it("reads sharing codes from structured errors and exact turn details", () => { | | 100 | + const code = "subscription_sharing_usage_limit_exceeded"; | | 101 | + const response = { error: { code } }; | | 102 | + for (const value of [ | | 103 | + response, | | 104 | + JSON.stringify(response), | | 105 | + { message: "Provider stopped", additionalDetails: code }, | | 106 | + { message: "Provider stopped", additionalDetails: JSON.stringify(response) }, | | 107 | + ]) { | | 108 | + assert.deepInclude(classifyCodexManagedError(value), { code, usageLimit: true }); | | 109 | + } | | 110 | +}); |
|