Skip to content

Commit 721e6fb

Browse files
committed
fix(codex): classify detail-scoped workspace denials
1 parent a5ec641 commit 721e6fb

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎src/codex/quota-rejection.ts‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,29 @@ async function denialFromResponse(
7171
}
7272
}
7373

74-
/** Own-property `code` lookup at the top level or under `error`. No coercion, no accessors. */
74+
function ownStringField(container: Record<string, unknown>, field: string): string | undefined {
75+
const descriptor = Object.getOwnPropertyDescriptor(container, field);
76+
return descriptor && "value" in descriptor && typeof descriptor.value === "string"
77+
? descriptor.value
78+
: undefined;
79+
}
80+
81+
/** Own-property `code` lookup at the top level or under `error` / `detail`. No coercion or accessors. */
7582
function structuredDenialCode(payload: unknown): string | undefined {
7683
if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return undefined;
77-
const direct = (payload as Record<string, unknown>).code;
78-
if (typeof direct === "string") return direct;
79-
const error = (payload as Record<string, unknown>).error;
80-
if (error === null || typeof error !== "object" || Array.isArray(error)) return undefined;
81-
const nested = (error as Record<string, unknown>).code;
82-
return typeof nested === "string" ? nested : undefined;
84+
const record = payload as Record<string, unknown>;
85+
const direct = ownStringField(record, "code");
86+
if (direct !== undefined) return direct;
87+
88+
for (const field of ["error", "detail"] as const) {
89+
const descriptor = Object.getOwnPropertyDescriptor(record, field);
90+
if (!descriptor || !("value" in descriptor)) continue;
91+
const nested = descriptor.value;
92+
if (nested === null || typeof nested !== "object" || Array.isArray(nested)) continue;
93+
const code = ownStringField(nested as Record<string, unknown>, "code");
94+
if (code !== undefined) return code;
95+
}
96+
return undefined;
8397
}
8498

8599
const RESET_ELIGIBLE_CODES: ReadonlySet<string> = new Set(RESET_ELIGIBLE_CODE_VALUES);

‎tests/codex-quota-rejection.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,23 @@ describe("Codex pre-stream quota rejection classification", () => {
2626
}));
2727
expect(topLevel).toMatchObject({ kind: "permission-error", denial: "workspace" });
2828

29+
const detail = await classifyCodexPreStreamRejection(jsonPayload(403, {
30+
detail: {
31+
code: "codex_workspace_access_denied",
32+
message: "workspace access denied",
33+
},
34+
}));
35+
expect(detail).toMatchObject({ kind: "permission-error", denial: "workspace" });
36+
2937
const entitlement = await classifyCodexPreStreamRejection(jsonRejection(403, {
3038
code: "codex_entitlement_missing",
3139
}));
3240
expect(entitlement).toMatchObject({ kind: "permission-error", denial: "entitlement" });
41+
42+
const detailEntitlement = await classifyCodexPreStreamRejection(jsonPayload(403, {
43+
detail: { code: "entitlement_missing" },
44+
}));
45+
expect(detailEntitlement).toMatchObject({ kind: "permission-error", denial: "entitlement" });
3346
});
3447

3548
test("a 403 without denial evidence stays an ordinary permission error (#1789)", async () => {
@@ -46,6 +59,11 @@ describe("Codex pre-stream quota rejection classification", () => {
4659

4760
const malformed = await classifyCodexPreStreamRejection(new Response("{not json", { status: 403 }));
4861
expect(malformed.denial).toBeUndefined();
62+
63+
const invalidDetail = await classifyCodexPreStreamRejection(jsonPayload(403, {
64+
detail: { code: 403 },
65+
}));
66+
expect(invalidDetail.denial).toBeUndefined();
4967
});
5068

5169
test.each([

0 commit comments

Comments
 (0)