Skip to content

Commit e0b7458

Browse files
authored
Merge pull request #463 from Wibias/fix/issue-452-opaque-503
fix(proxy): stop empty 503 bodies becoming Codex Unknown error (#452)
2 parents 65c142f + fc6bc25 commit e0b7458

5 files changed

Lines changed: 454 additions & 31 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { ProviderAdapter } from "./base";
22
import type { AdapterEvent, OcxAssistantMessage, OcxContentPart, OcxMessage, OcxParsedRequest, OcxProviderConfig, OcxTextContent, OcxThinkingContent, OcxToolCall, OcxUsage } from "../types";
33
import { isAllowedToolChoice, modelInList, namespacedToolName, resolveToolChoiceWireName, toolAllowedByChoice } from "../types";
44
import { mapReasoningEffort, modelRecordValue } from "../reasoning-effort";
5+
import { debugProviderDiagnostic } from "../lib/debug";
6+
import { isDebugEnabled } from "../lib/debug-settings";
57
import { redactSecretString } from "../lib/redact";
68
import { contentPartsToText } from "./image";
79
import { neutralizeIdentity } from "./identity";
@@ -588,7 +590,24 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
588590
if (hasCredential) headers["Authorization"] = `Bearer ${provider.apiKey}`;
589591
if (provider.headers) Object.assign(headers, provider.headers);
590592

591-
return { url, method: "POST", headers, body: JSON.stringify(body) };
593+
const bodyJson = JSON.stringify(body);
594+
// Never log pathname/query — tenant-scoped hosts (e.g. Cloudflare
595+
// /accounts/<account_id>/ai/v1) would otherwise leak account identifiers (#452).
596+
if (isDebugEnabled()) {
597+
let host = "upstream";
598+
try { host = new URL(url).host; } catch { /* keep fallback */ }
599+
debugProviderDiagnostic("openai-chat", "request", {
600+
host,
601+
model: body.model,
602+
stream: parsed.stream,
603+
messageCount: Array.isArray(messages) ? messages.length : 0,
604+
toolCount: Array.isArray(tools) ? tools.length : 0,
605+
hasCredential,
606+
bodyBytes: new TextEncoder().encode(bodyJson).length,
607+
});
608+
}
609+
610+
return { url, method: "POST", headers, body: bodyJson };
592611
},
593612

594613
async *parseStream(response: Response): AsyncGenerator<AdapterEvent> {
@@ -658,8 +677,10 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
658677
// classified response.failed (bridge case "error") — never a truncated completion.
659678
if (chunk.error) {
660679
const err = chunk.error as { message?: string } | undefined;
680+
const message = err?.message ?? "upstream error";
681+
debugProviderDiagnostic("openai-chat", "stream-error", { message });
661682
yield* flushToolCalls();
662-
yield { type: "error", message: err?.message ?? "upstream error" };
683+
yield { type: "error", message };
663684
return "terminate";
664685
}
665686

@@ -748,6 +769,10 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
748769
// closed so the bridge emits a classified response.failed rather than a silent truncation.
749770
const sawFinish = finishReason !== undefined;
750771
if (!sawFinish && pendingUsage === undefined) {
772+
debugProviderDiagnostic("openai-chat", "stream-truncated", {
773+
finishReason: finishReason ?? null,
774+
hadUsage: false,
775+
});
751776
yield { type: "error", message: "upstream stream ended without a terminal signal ([DONE] or finish_reason) — possible truncation" };
752777
return;
753778
}

‎src/server/index.ts‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ export function startServer(port?: number) {
282282
// (0.0.0.0/::) and specific hosts are left untouched so intentional exposure is preserved.
283283
const bindHost = /^localhost$/i.test(config.hostname ?? "") ? "127.0.0.1" : (config.hostname ?? "127.0.0.1");
284284

285+
// Codex treats empty / non-JSON 503 bodies as "Unknown error" (#452). Keep Retry-After and
286+
// the server_is_overloaded code so clients can back off, but always return a JSON envelope.
287+
function drainingResponse(req: Request): Response {
288+
const response = formatErrorResponse(503, "server_error", "Service shutting down");
289+
const headers = new Headers(response.headers);
290+
for (const [name, value] of Object.entries(corsHeaders(req, config))) {
291+
headers.set(name, value);
292+
}
293+
headers.set("Retry-After", "5");
294+
return new Response(response.body, { status: 503, headers });
295+
}
296+
285297
const server: Server<WsData> = Bun.serve<WsData>({
286298
port: listenPort,
287299
hostname: bindHost,
@@ -301,7 +313,7 @@ export function startServer(port?: number) {
301313
// handshake-time only, so capture inbound headers and thread them into the pipeline.
302314
if (url.pathname === "/v1/responses" && req.headers.get("upgrade")?.toLowerCase() === "websocket") {
303315
if (isDraining()) {
304-
return new Response("Service shutting down", { status: 503, headers: { ...corsHeaders(req, config), "Retry-After": "5" } });
316+
return drainingResponse(req);
305317
}
306318
const apiAuthError = requireResponsesApiAuth(req, config);
307319
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -406,7 +418,7 @@ export function startServer(port?: number) {
406418
// before the /v1/* 404 guard below.
407419
if (url.pathname === "/v1/responses/compact" && req.method === "POST") {
408420
if (isDraining()) {
409-
return new Response("Service shutting down", { status: 503, headers: { ...corsHeaders(req, config), "Retry-After": "5" } });
421+
return drainingResponse(req);
410422
}
411423
const apiAuthError = requireResponsesApiAuth(req, config);
412424
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -438,10 +450,7 @@ export function startServer(port?: number) {
438450
) {
439451
disableResponsesRequestTimeout(req, requestServer);
440452
if (isDraining()) {
441-
return new Response("Service shutting down", {
442-
status: 503,
443-
headers: { ...corsHeaders(req, config), "Retry-After": "5" },
444-
});
453+
return drainingResponse(req);
445454
}
446455
const apiAuthError = requireApiAuth(req, config, "data-plane");
447456
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -460,10 +469,7 @@ export function startServer(port?: number) {
460469
if (url.pathname === "/v1/alpha/search" && req.method === "POST") {
461470
disableResponsesRequestTimeout(req, requestServer);
462471
if (isDraining()) {
463-
return new Response("Service shutting down", {
464-
status: 503,
465-
headers: { ...corsHeaders(req, config), "Retry-After": "5" },
466-
});
472+
return drainingResponse(req);
467473
}
468474
const apiAuthError = requireApiAuth(req, config, "data-plane");
469475
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -487,7 +493,7 @@ export function startServer(port?: number) {
487493
if (url.pathname === "/v1/responses" && req.method === "POST") {
488494
disableResponsesRequestTimeout(req, requestServer);
489495
if (isDraining()) {
490-
return new Response("Service shutting down", { status: 503, headers: { ...corsHeaders(req, config), "Retry-After": "5" } });
496+
return drainingResponse(req);
491497
}
492498
const apiAuthError = requireResponsesApiAuth(req, config);
493499
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -526,7 +532,7 @@ export function startServer(port?: number) {
526532
// Claude Code posts `/v1/messages?beta=true` — pathname match ignores the query (003 G9).
527533
if (url.pathname === "/v1/messages/count_tokens" && req.method === "POST") {
528534
if (isDraining()) {
529-
return new Response("Service shutting down", { status: 503, headers: { ...corsHeaders(req, config), "Retry-After": "5" } });
535+
return drainingResponse(req);
530536
}
531537
if (!hasValidApiAuth(req, config)) {
532538
return withCors(anthropicErrorResponse(401, "opencodex API key required", "authentication_error"), req, config);
@@ -541,7 +547,7 @@ export function startServer(port?: number) {
541547
if (url.pathname === "/v1/messages" && req.method === "POST") {
542548
disableResponsesRequestTimeout(req, requestServer);
543549
if (isDraining()) {
544-
return new Response("Service shutting down", { status: 503, headers: { ...corsHeaders(req, config), "Retry-After": "5" } });
550+
return drainingResponse(req);
545551
}
546552
if (!hasValidApiAuth(req, config)) {
547553
return withCors(anthropicErrorResponse(401, "opencodex API key required", "authentication_error"), req, config);
@@ -564,7 +570,7 @@ export function startServer(port?: number) {
564570
if (url.pathname === "/v1/chat/completions" && req.method === "POST") {
565571
disableResponsesRequestTimeout(req, requestServer);
566572
if (isDraining()) {
567-
return new Response("Service shutting down", { status: 503, headers: { ...corsHeaders(req, config), "Retry-After": "5" } });
573+
return drainingResponse(req);
568574
}
569575
const apiAuthError = requireResponsesApiAuth(req, config);
570576
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -587,10 +593,7 @@ export function startServer(port?: number) {
587593
) {
588594
disableResponsesRequestTimeout(req, requestServer);
589595
if (isDraining()) {
590-
return new Response("Service shutting down", {
591-
status: 503,
592-
headers: { ...corsHeaders(req, config), "Retry-After": "5" },
593-
});
596+
return drainingResponse(req);
594597
}
595598
const apiAuthError = requireApiAuth(req, config, "data-plane");
596599
if (apiAuthError) return withCors(apiAuthError, req, config);
@@ -618,10 +621,7 @@ export function startServer(port?: number) {
618621
: null;
619622
if (liveSidebandTarget) {
620623
if (isDraining()) {
621-
return new Response("Service shutting down", {
622-
status: 503,
623-
headers: { ...corsHeaders(req, config), "Retry-After": "5" },
624-
});
624+
return drainingResponse(req);
625625
}
626626
const apiAuthError = requireApiAuth(req, config, "data-plane");
627627
if (apiAuthError) return withCors(apiAuthError, req, config);

‎src/server/responses/core.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Server } from "bun";
22
import { bridgeToResponsesSSE, buildResponseJSON, formatErrorResponse, type ResponsesTerminalStatus } from "../../bridge";
3+
import { formatPassthroughUpstreamError } from "./passthrough-error";
34
import {
45
getConfigPath,
56
multiAgentGuidanceEnabled,
@@ -1289,6 +1290,23 @@ export async function handleResponses(
12891290
}
12901291
}
12911292

1293+
// Non-2xx passthrough failures must never reach Codex as an empty body —
1294+
// Codex renders that as the opaque "Unknown error" (#452). Combo attempts
1295+
// keep their typed failure envelope. Non-empty bodies are relayed verbatim
1296+
// (headers included) so pool-retry Activation B/D and client diagnostics stay intact.
1297+
if (!upstreamResponse.ok) {
1298+
if (options.comboAttempt) {
1299+
const failure = await consumeComboFailure(upstreamResponse, options.abortSignal);
1300+
options.onConsumedComboFailure?.(failure);
1301+
return failure.response;
1302+
}
1303+
const errorText = await upstreamResponse.text().catch(() => "");
1304+
return formatPassthroughUpstreamError(upstreamResponse.status, errorText, {
1305+
statusText: upstreamResponse.statusText,
1306+
headers,
1307+
});
1308+
}
1309+
12921310
// Bun#32111 workaround: passthrough SSE uses tee()+native relay to avoid the
12931311
// async-pull segfault on Windows. Branch[0] goes directly to the Response (Bun
12941312
// native relay, never enters JS Sink.write); branch[1] is consumed in the
@@ -1299,7 +1317,7 @@ export async function handleResponses(
12991317
// bounded reader with inline inspection (see src/server/relay-eager.ts and
13001318
// devlog/_plan/260723_win_mem_safestream/020). Default on the bundled
13011319
// known-bad runtime remains the tee path below.
1302-
if (upstreamResponse.ok && isEventStream && upstreamResponse.body) {
1320+
if (isEventStream && upstreamResponse.body) {
13031321
const repairConfig = route.provider.responsesItemIdRepair;
13041322
const winNoRepair = process.platform === "win32" && !hasResponsesItemIdRepair(repairConfig);
13051323
const eagerDecision = winNoRepair ? decideEagerRelay(config.streamMode ?? "auto") : null;
@@ -1424,14 +1442,9 @@ export async function handleResponses(
14241442
}));
14251443
}
14261444
if (headers.get("content-type")?.toLowerCase().includes("application/json")) {
1427-
if (!upstreamResponse.ok && options.comboAttempt) {
1428-
const failure = await consumeComboFailure(upstreamResponse, options.abortSignal);
1429-
options.onConsumedComboFailure?.(failure);
1430-
return failure.response;
1431-
}
14321445
const text = await upstreamResponse.text();
14331446
inspectResponseLogJson(logCtx, text);
1434-
if (upstreamResponse.ok && rememberPassthroughResponse) {
1447+
if (rememberPassthroughResponse) {
14351448
try {
14361449
rememberPassthroughResponse(JSON.parse(text) as { id?: unknown; output?: unknown; status?: unknown });
14371450
} catch { /* non-JSON despite content-type; recording is best-effort */ }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { formatErrorResponse } from "../../bridge";
2+
import { parseRetryAfterMs } from "../../combos";
3+
4+
function sanitizedRetryAfter(value: string | null | undefined, now: number): string | undefined {
5+
const trimmed = value?.trim();
6+
if (!trimmed || trimmed.length > 128) return undefined;
7+
return parseRetryAfterMs(trimmed, now) !== undefined ? trimmed : undefined;
8+
}
9+
10+
/**
11+
* Passthrough adapters historically relayed upstream non-2xx bodies verbatim.
12+
* Codex maps an *empty* body to the literal client string "Unknown error"
13+
* (UnexpectedResponseError) — issue #452. Only empty bodies need wrapping.
14+
*
15+
* Non-empty bodies (including ChatGPT `{detail: ...}` account-model 400s and
16+
* HTML/text errors) must keep their original bytes and headers so pool-retry
17+
* activation and client diagnostics stay honest.
18+
*
19+
* Normalized (empty-body) responses force `Content-Type: application/json` and
20+
* preserve a validated `Retry-After` so Responses clients and the chat-completions
21+
* / Claude bridges that copy that header keep correct backoff.
22+
*/
23+
export function formatPassthroughUpstreamError(
24+
status: number,
25+
bodyText: string,
26+
options?: {
27+
statusText?: string;
28+
headers?: Headers;
29+
now?: number;
30+
},
31+
): Response {
32+
const trimmed = bodyText.trim();
33+
const now = options?.now ?? Date.now();
34+
const retryAfter = sanitizedRetryAfter(options?.headers?.get("retry-after"), now);
35+
36+
if (trimmed) {
37+
return new Response(bodyText, {
38+
status,
39+
...(options?.statusText ? { statusText: options.statusText } : {}),
40+
...(options?.headers ? { headers: options.headers } : { headers: { "Content-Type": "application/json" } }),
41+
});
42+
}
43+
44+
const response = formatErrorResponse(
45+
status,
46+
"upstream_error",
47+
`Provider error ${status}: (empty body)`,
48+
);
49+
const headers = new Headers(response.headers);
50+
headers.set("Content-Type", "application/json");
51+
if (retryAfter !== undefined) headers.set("Retry-After", retryAfter);
52+
return new Response(response.body, { status: response.status, headers });
53+
}

0 commit comments

Comments
 (0)