Skip to content

Commit 81584d6

Browse files
author
PR Batch Tester
committed
fix(codex): match sharing error codes only in explicit values
1 parent 55d3859 commit 81584d6

3 files changed

Lines changed: 84 additions & 9 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3993,6 +3993,23 @@ describe("CodexAdapterV2 post-settle continuation", () => {
39933993
expectedCode: "subscription_sharing_usage_limit_exceeded",
39943994
expectedMessage: "Usage settings",
39953995
},
3996+
{
3997+
name: "sharing-code-mention",
3998+
code: "responseStreamDisconnected",
3999+
errorInfo: { responseStreamDisconnected: { httpStatusCode: null } },
4000+
message: "Transport unavailable; this is not subscription_sharing_usage_limit_exceeded.",
4001+
notification: false,
4002+
expectedClass: "provider_error",
4003+
},
4004+
{
4005+
name: "sharing-code-mention-retry",
4006+
code: "responseStreamDisconnected",
4007+
errorInfo: { responseStreamDisconnected: { httpStatusCode: null } },
4008+
message: "Transport unavailable; this is not subscription_sharing_usage_limit_exceeded.",
4009+
notification: true,
4010+
willRetry: true,
4011+
expectedClass: "transport_error",
4012+
},
39964013
] as const)("classifies Codex terminal failures from $name evidence", (scenario) =>
39974014
Effect.scoped(
39984015
Effect.gen(function* () {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,34 @@ it("treats only ChatGPT sharing allowance limits as usage limits", () => {
8181
);
8282
assert.isUndefined(classifyCodexManagedError("You've hit your usage limit."));
8383
});
84+
85+
it("does not infer sharing codes from incidental mentions in turn errors", () => {
86+
const mention = "Transport unavailable; this is not subscription_sharing_usage_limit_exceeded.";
87+
for (const value of [
88+
mention,
89+
{ message: mention },
90+
{ message: "Transport unavailable", additionalDetails: mention },
91+
{ code: "other", message: mention },
92+
]) {
93+
assert.isUndefined(classifyCodexManagedError(value));
94+
}
95+
assert.deepInclude(
96+
classifyCodexManagedError({
97+
error: { code: "subscription_sharing_usage_unavailable", message: mention },
98+
}),
99+
{ code: "subscription_sharing_usage_unavailable", usageLimit: false },
100+
);
101+
});
102+
103+
it("reads sharing codes from structured errors and exact turn details", () => {
104+
const code = "subscription_sharing_usage_limit_exceeded";
105+
const response = { error: { code } };
106+
for (const value of [
107+
response,
108+
JSON.stringify(response),
109+
{ message: "Provider stopped", additionalDetails: code },
110+
{ message: "Provider stopped", additionalDetails: JSON.stringify(response) },
111+
]) {
112+
assert.deepInclude(classifyCodexManagedError(value), { code, usageLimit: true });
113+
}
114+
});

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

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,46 @@ const proseCodes = [
7171
"subscription_sharing_usage_limit_exceeded",
7272
],
7373
] as const;
74-
export function classifyCodexManagedError(value: unknown) {
75-
let text: string;
76-
try {
77-
text = typeof value === "string" ? value : JSON.stringify(value);
78-
} catch {
79-
return undefined;
74+
75+
function unwrapError(value: unknown): unknown {
76+
if (typeof value === "string") {
77+
try {
78+
value = JSON.parse(value);
79+
} catch {
80+
return value;
81+
}
8082
}
81-
if (typeof text !== "string") return undefined;
83+
return typeof value === "object" && value !== null && "error" in value ? value.error : value;
84+
}
85+
86+
function isManagedCode(value: unknown): value is keyof typeof failures {
87+
return typeof value === "string" && Object.hasOwn(failures, value);
88+
}
89+
90+
function sharingCode(value: unknown) {
91+
const error = unwrapError(value);
92+
const code = typeof error === "object" && error !== null && "code" in error ? error.code : error;
93+
return isManagedCode(code) ? code : undefined;
94+
}
95+
96+
export function classifyCodexManagedError(value: unknown) {
97+
const error = unwrapError(value);
98+
const messages =
99+
typeof error === "string"
100+
? [error]
101+
: typeof error === "object" && error !== null
102+
? [
103+
"message" in error ? error.message : null,
104+
"additionalDetails" in error ? error.additionalDetails : null,
105+
].filter((message): message is string => typeof message === "string")
106+
: [];
82107
const code =
83-
(Object.keys(failures) as Array<keyof typeof failures>).find((known) => text.includes(known)) ??
84-
proseCodes.find(([prose]) => text.includes(prose))?.[1];
108+
sharingCode(error) ??
109+
messages.map(sharingCode).find((code) => code !== undefined) ??
110+
proseCodes.find(([prose]) => messages.some((message) => message.includes(prose)))?.[1];
85111
if (code === undefined) return undefined;
86112
const failure = { ...failures[code], code, usageLimit: usageLimitCodes.has(code) };
113+
const text = messages.join(" ");
87114
if (code === "subscription_sharing_unsupported_capability") {
88115
if (text.includes("tool 'namespace'"))
89116
return {

0 commit comments

Comments
 (0)