Skip to content

Commit 9f23b1f

Browse files
authored
Merge pull request #6540 from lidge-jun/codex/release-261003-b-nested-http
fix(combo): preserve bounded nested HTTP refusal semantics
2 parents 3541261 + fa040a0 commit 9f23b1f

7 files changed

Lines changed: 328 additions & 14 deletions

File tree

‎src/combos/failover.ts‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -695,17 +695,53 @@ export function codexAccountModelRefusalPayload(status: number, payload: unknown
695695
return field !== undefined && CODEX_ACCOUNT_MODEL_REFUSAL.test(field) ? "refusal" : "other";
696696
}
697697

698-
/** A bounded fallback signal, never credential or account-entitlement evidence. */
699-
export function codexAccountModelRefusal(status: number, message: string): CodexAccountModelRefusal {
700-
if (status !== 400 || message.length > 16_384) return "other";
698+
/** One complete, bounded HTTP envelope; accept only a single exact status prefix. */
699+
function parseCodexRefusalEnvelope(status: number, message: string): { text: string; payload: unknown } | undefined {
700+
if (status !== 400 || message.length > 16_384) return undefined;
701701
let text = message.trim();
702702
if (text.startsWith("Provider error 400: ")) text = text.slice("Provider error 400: ".length);
703-
if (CODEX_ACCOUNT_MODEL_REFUSAL.test(text)) return "refusal";
704-
try {
705-
const payload: unknown = JSON.parse(text);
706-
return hasConflictingCodexModelRefusalEnvelopes(status, payload)
707-
? "ambiguous" : codexAccountModelRefusalPayload(status, payload);
708-
} catch { return "other"; }
703+
let payload: unknown;
704+
try { payload = JSON.parse(text); } catch { /* Bare refusal text is also a supported carrier. */ }
705+
return { text, payload };
706+
}
707+
708+
/** Only allowlisted hard stops cross the same HTTP boundary as positive refusal evidence. */
709+
export function codexAccountModelRefusalHardStopCode(status: number, message: string): string | undefined {
710+
const payload = parseCodexRefusalEnvelope(status, message)?.payload;
711+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return undefined;
712+
const root = payload as Record<string, unknown>;
713+
const response = Object.hasOwn(root, "response") ? root.response : undefined;
714+
const records = response && typeof response === "object" && !Array.isArray(response)
715+
? [root, response as Record<string, unknown>] : [root];
716+
for (const record of records) {
717+
const error = record.error && typeof record.error === "object" && !Array.isArray(record.error)
718+
? record.error as Record<string, unknown> : undefined;
719+
// Diagnostic type is never code evidence, even when a code is absent or malformed.
720+
for (const code of [error?.code, record.code]) {
721+
if (typeof code !== "string") continue;
722+
if (isNonReplayableUpstreamCode(code) || isCyberPolicyCode(code)) return code;
723+
if (normalizedFailureCode(code) === "origin_rejected") return "origin_rejected";
724+
}
725+
}
726+
return undefined;
727+
}
728+
729+
/** A bounded fallback signal, never credential or account-entitlement evidence. */
730+
export function codexAccountModelRefusal(
731+
status: number, message: string, options?: { allowNestedResponse?: boolean },
732+
): CodexAccountModelRefusal {
733+
const envelope = parseCodexRefusalEnvelope(status, message);
734+
if (!envelope) return "other";
735+
if (CODEX_ACCOUNT_MODEL_REFUSAL.test(envelope.text)) return "refusal";
736+
const payload = envelope.payload;
737+
if (hasConflictingCodexModelRefusalEnvelopes(status, payload)) return "ambiguous";
738+
// A present root carrier stays authoritative, even when malformed or nonmatching.
739+
if (options?.allowNestedResponse !== false && payload && typeof payload === "object" && !Array.isArray(payload)
740+
&& !Object.hasOwn(payload, "detail") && !Object.hasOwn(payload, "error")
741+
&& Object.hasOwn(payload, "response")) {
742+
return codexAccountModelRefusalPayload(status, (payload as Record<string, unknown>).response);
743+
}
744+
return codexAccountModelRefusalPayload(status, payload);
709745
}
710746

711747
export function comboFailureDecision(
@@ -726,6 +762,8 @@ export function comboFailureDecision(
726762
// Cyber policy is a hard non-retryable refusal — honor structured code even when
727763
// classificationText was truncated before the JSON code field.
728764
if (isCyberPolicyCode(options?.code)) return "stop";
765+
// HTTP consumers retain hard codes before truncation; SSE metadata owns its selected carrier.
766+
if (options?.codexModelRefusal === undefined && codexAccountModelRefusalHardStopCode(status, message)) return "stop";
729767
const modelRefusal = status === 400
730768
? options?.codexModelRefusal ?? codexAccountModelRefusal(status, message) : "other";
731769
// Competing envelopes cannot grant a hop through an earlier structured-code rule either.

‎src/server/responses/combo-stream-preflight.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function terminalModelRefusal(status: number, event: Record<string, unknown>): C
8484
}
8585
}
8686
if (event.type === "error" && !Object.hasOwn(event, "error") && typeof event.message === "string") {
87-
return codexAccountModelRefusal(status, event.message);
87+
return codexAccountModelRefusal(status, event.message, { allowNestedResponse: false });
8888
}
8989
return outer;
9090
}

‎src/server/responses/core-combo-failure.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { codexAccountModelRefusal, type CodexAccountModelRefusal } from "../../combos/failover";
1+
import { codexAccountModelRefusal, codexAccountModelRefusalHardStopCode, type CodexAccountModelRefusal } from "../../combos/failover";
22
import { parseRetryAfterMs } from "../../combos";
33
import type { ConsumedComboFailure, HandleResponsesOptions } from "./core-options";
44
import type { OcxUsage } from "../../types";
@@ -66,7 +66,7 @@ export async function consumeComboFailure(
6666
const normalized = normalizeUpstreamErrorText(body.text, fallback);
6767
if (utf8Trusted || isCyberPolicyCode(normalized.code) || isCyberPolicyMessage(normalized.safeText)) {
6868
classificationText = normalized.safeText;
69-
upstreamCode = normalized.code;
69+
upstreamCode = codexAccountModelRefusalHardStopCode(response.status, body.text) ?? normalized.code;
7070
upstreamMessage = normalized.message;
7171
upstreamType = normalized.type;
7272
}

‎structure/providers-and-adapters.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,5 @@ Known Claude Sonnet and Opus 5.5 base and low/medium/high IDs share a determinis
570570
`src/server/responses/core-options.ts` carries the value to the combo decision. An inspected non-match
571571
remains authoritative. No raw body is retained in that field, serialized into client errors, or logged.
572572
Presence of both `detail` and `error` across the original root and its own non-array `response` record is ambiguous, regardless of values; null counts and deeper quoted objects do not. This is checked before selecting an error carrier.
573+
For complete bounded HTTP errors, the text wrapper selects root `detail`/`error` when present; otherwise it selects the own `response` record for the same exact `detail` or `error.message` match. It never searches another carrier after a nonmatch or descends further. Policy-fallback and direct callers share this wrapper; SSE message parsing disables nested HTTP selection. The same bounded HTTP parser retains allowlisted dedicated `code` fields from root and response before display truncation; diagnostic `type` is never promoted, including when code is absent or malformed. Policy/direct classification inspects full text only without authoritative carrier metadata; SSE never borrows hard codes from quoted message JSON.
573574
Committed output, cancellation, hard policy refusals and resend limits remain stronger than model fallback.

‎tests/routing/router-combo-failover-classification.test.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
pickComboTarget,
99
targetKey,
1010
} from "../../src/combos";
11-
import { comboFailureCooldownScope, comboFailureDecision } from "../../src/combos/failover";
11+
import { codexAccountModelRefusalHardStopCode, comboFailureCooldownScope, comboFailureDecision } from "../../src/combos/failover";
1212
import { adapterFailureFromMessage, inferHttpStatusFromAdapterMessage } from "../../src/lib/errors";
1313
import type { OcxConfig } from "../../src/types";
1414

@@ -172,6 +172,34 @@ describe("combo failure hop/stop verdicts", () => {
172172
response: { error: null } }))).toBe("hop");
173173
});
174174

175+
test("nested HTTP refusal selects one carrier without borrowing body metadata", () => {
176+
const refusal = "The 'gpt-6-astra' model is not supported when using Codex with a ChatGPT account.";
177+
for (const response of [{ detail: refusal }, { error: { message: refusal } }]) {
178+
const body = JSON.stringify({ response });
179+
expect(comboFailureDecision(400, body)).toBe("hop");
180+
expect(comboFailureDecision(400, `Provider error 400: ${body}`)).toBe("hop");
181+
expect(comboFailureDecision(400, body, { codexModelRefusal: "other" })).toBe("stop");
182+
expect(comboFailureDecision(400, body, { code: "origin_rejected" })).toBe("stop");
183+
expect(comboFailureDecision(400, body, { code: "upstream_no_response" })).toBe("stop");
184+
expect(comboFailureDecision(400, body, { code: "cyber_policy" })).toBe("stop");
185+
expect(comboFailureDecision(499, body)).toBe("stop");
186+
expect(comboFailureDecision(422, body)).toBe("stop");
187+
expect(comboFailureDecision(400, body.slice(0, -1))).toBe("stop");
188+
expect(comboFailureDecision(400, JSON.stringify({ response, padding: "x".repeat(16_384) }))).toBe("stop");
189+
}
190+
expect(comboFailureDecision(400, JSON.stringify({ response: { error: {
191+
code: "upstream_no_response", message: refusal,
192+
} } }), { code: "unsupported_model", codexModelRefusal: "other" })).toBe("hop");
193+
for (const body of [
194+
{ detail: null, response: { detail: refusal } },
195+
{ detail: "bad input", response: { detail: refusal } },
196+
{ error: null, response: { error: { message: refusal } } },
197+
{ error: { message: "bad input" }, response: { error: { message: refusal } } },
198+
{ response: { error: { message: "bad input" } }, codexModelRefusal: "refusal" },
199+
{ response: { error: { message: "bad input" }, codexModelRefusal: "refusal" } },
200+
]) expect(comboFailureDecision(400, JSON.stringify(body))).toBe("stop");
201+
});
202+
175203
test("402 and 425 hop instead of ending the chain", () => {
176204
expect(comboFailureDecision(402, "payment required")).toBe("hop");
177205
expect(comboFailureDecision(425, "too early")).toBe("hop");
@@ -572,3 +600,31 @@ describe("response_format capability refusal, second gateway", () => {
572600
}
573601
});
574602
});
603+
604+
describe("HTTP hard-code evidence excludes diagnostic types", () => {
605+
test("code/type matrix inspects dedicated code fields only at either level", () => {
606+
const codes: Array<[unknown, string | undefined]> = [
607+
["cyber_policy", "cyber_policy"], ["upstream_no_response", "upstream_no_response"],
608+
["origin-rejected", "origin_rejected"], ["unsupported_model", undefined], ["unknown_code", undefined],
609+
["", undefined], [" ", undefined], [undefined, undefined], [null, undefined], [17, undefined], [{}, undefined],
610+
];
611+
for (const nested of [false, true]) {
612+
for (const [code, expected] of codes) {
613+
for (const type of ["cyber_policy", "upstream_no_response", "upstream_reset_replay_refused", "origin_rejected", "invalid_request_error", null, undefined]) {
614+
const record = { error: { code, type, message: "fixture failure" } };
615+
const body = JSON.stringify(nested ? { response: record } : record);
616+
expect(codexAccountModelRefusalHardStopCode(400, body)).toBe(expected);
617+
}
618+
}
619+
}
620+
});
621+
622+
test("a genuine code in another record still wins over a nonhard code and diagnostic type", () => {
623+
const diagnostic = { error: { code: "unsupported_model", type: "cyber_policy", message: "fixture failure" } };
624+
const hard = { code: "upstream_no_response" };
625+
for (const body of [{ ...diagnostic, response: hard }, { ...hard, response: diagnostic }]) {
626+
expect(codexAccountModelRefusalHardStopCode(400, JSON.stringify(body))).toBe("upstream_no_response");
627+
expect(comboFailureDecision(400, JSON.stringify(body))).toBe("stop");
628+
}
629+
});
630+
});

‎tests/routing/routing-policy-fallback.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,84 @@ function seedAttempt(logCtx: RequestLogContext, provider: string, model: string)
6161
}
6262

6363
describe("policy candidate fallback", () => {
64+
for (const nested of [false, true]) {
65+
test.each(["cyber_policy", "upstream_no_response", "upstream_reset_replay_refused"])(
66+
`policy preserves explicit model code over diagnostic %s (nested=${nested})`, async type => {
67+
let calls = 0;
68+
const response = await handleResponsesWithPolicyFallback(request(), fixtureConfig(), {} as RequestLogContext, {}, {
69+
runCore: async (req, _config, context, options) => {
70+
calls++;
71+
options.onRequestBodyParsed?.(await req.json());
72+
context.routeDecision = policyTrace();
73+
if (calls !== 1) return Response.json({ status: "completed" });
74+
const record = { error: { code: "unsupported_model", type,
75+
message: nested ? "The 'gpt-6-astra' model is not supported when using Codex with a ChatGPT account."
76+
: "fixture failure" } };
77+
return Response.json(nested ? { response: record } : record, { status: 400 });
78+
},
79+
});
80+
expect(calls).toBe(2);
81+
expect(response.status).toBe(200);
82+
},
83+
);
84+
}
85+
86+
test.each([undefined, "upstream_no_response", "origin_rejected", "cyber_policy"])(
87+
"nested plan refusal preserves policy hard-stop code %s", async code => {
88+
let calls = 0;
89+
const refusal = "The 'gpt-6-astra' model is not supported when using Codex with a ChatGPT account.";
90+
const response = await handleResponsesWithPolicyFallback(request(), fixtureConfig(), {} as RequestLogContext, {}, {
91+
runCore: async (req, _config, context, options) => {
92+
calls++;
93+
options.onRequestBodyParsed?.(await req.json());
94+
context.routeDecision = policyTrace();
95+
return calls === 1 ? Response.json({ response: { error: { code, message: refusal } } }, { status: 400 })
96+
: Response.json({ status: "completed" });
97+
},
98+
});
99+
expect(calls).toBe(code ? 1 : 2);
100+
expect(response.status).toBe(code ? 400 : 200);
101+
},
102+
);
103+
104+
for (const hardCode of ["upstream_no_response", "origin_rejected", "origin-rejected", " ORIGIN-REJECTED ", "cyber_policy"]) {
105+
test.each(["root", "nested"])(`policy preserves ${hardCode} at %s beside a competing code`, async location => {
106+
let calls = 0;
107+
const refusal = "The 'gpt-6-astra' model is not supported when using Codex with a ChatGPT account.";
108+
const response = await handleResponsesWithPolicyFallback(request(), fixtureConfig(), {} as RequestLogContext, {}, {
109+
runCore: async (req, _config, context, options) => {
110+
calls++;
111+
options.onRequestBodyParsed?.(await req.json());
112+
context.routeDecision = policyTrace();
113+
return calls === 1 ? Response.json({
114+
code: location === "root" ? hardCode : "invalid_request_error",
115+
response: { error: { code: location === "root" ? "unsupported_model" : hardCode, message: refusal } },
116+
}, { status: 400 }) : Response.json({ status: "completed" });
117+
},
118+
});
119+
expect(calls).toBe(1);
120+
expect(response.status).toBe(400);
121+
});
122+
}
123+
124+
test.each([undefined, "upstream_no_response", "origin_rejected", "cyber_policy"])(
125+
"prefixed nested HTTP refusal retains policy code %s", async code => {
126+
let calls = 0;
127+
const message = "The 'gpt-6-astra' model is not supported when using Codex with a ChatGPT account.";
128+
const response = await handleResponsesWithPolicyFallback(request(), fixtureConfig(), {} as RequestLogContext, {}, {
129+
runCore: async (req, _config, context, options) => {
130+
calls++;
131+
options.onRequestBodyParsed?.(await req.json());
132+
context.routeDecision = policyTrace();
133+
return calls === 1 ? new Response(`Provider error 400: ${JSON.stringify({ response: { error: { code, message } } })}`,
134+
{ status: 400 }) : Response.json({ status: "completed" });
135+
},
136+
});
137+
expect(calls).toBe(code ? 1 : 2);
138+
expect(response.status).toBe(code ? 400 : 200);
139+
},
140+
);
141+
64142
test("a marked context overflow never tries another policy route", async () => {
65143
const failure = Response.json({ error: {
66144
type: "invalid_request_error", code: "context_length_exceeded", message: "Context window exceeded",

0 commit comments

Comments
 (0)