Skip to content

Commit 9b2fc10

Browse files
authored
fix(audio): record real upstream status before body handling in audio routes (lidge-jun#4502) (lidge-jun#4512)
1 parent 10d61fc commit 9b2fc10

3 files changed

Lines changed: 13 additions & 5 deletions

File tree

‎src/server/audio-live.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export async function handleExternalLive(
100100
? frameless ? forwardLiveUrl(relay.providerBaseUrl, false) : keyedLiveUrl(relay.providerBaseUrl)
101101
: forwardLiveUrl(relay.providerBaseUrl, true);
102102
const upstream = await fetch(url, { method: "POST", headers, body, signal: deadline.signal, redirect: "manual" });
103-
outcome = upstream.ok ? 502 : upstream.status;
103+
outcome = upstream.status;
104104
const detach = cancelBodyOnAbort(upstream.body, deadline.signal);
105105
let responseBody: ArrayBuffer | Response;
106106
try { responseBody = await readBodyCapped(upstream.body, LIVE_RESPONSE_MAX_BYTES, () => "Live answer too large", deadline.signal); }
@@ -122,7 +122,6 @@ export async function handleExternalLive(
122122
sidebandBaseUrl: config.experimentalRealtimeWsBaseUrl,
123123
});
124124
if (!alias) return formatErrorResponse(503, "server_busy", "Live call could not be registered");
125-
outcome = upstream.status;
126125
return new Response(responseBody, { status: upstream.status, headers: {
127126
"content-type": upstream.headers.get("content-type") ?? "application/sdp",
128127
location: `/v1/${frameless ? "live" : "realtime/calls"}/${alias}`,

‎src/server/audio-transcriptions.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async function transcribeAdmitted(
117117
form.append("response_format", "json");
118118
}
119119
const upstream = await fetch(url, { method: "POST", headers, body: form, signal: signal.signal, redirect: "manual" });
120-
outcome = upstream.ok ? 502 : upstream.status;
120+
outcome = upstream.status;
121121
const detach = cancelBodyOnAbort(upstream.body, signal.signal);
122122
let body: ArrayBuffer | Response;
123123
try {
@@ -137,7 +137,6 @@ async function transcribeAdmitted(
137137
if (!payload || typeof payload !== "object" || !("text" in payload) || typeof payload.text !== "string") {
138138
return formatErrorResponse(502, "upstream_error", "Audio upstream response is missing text");
139139
}
140-
outcome = upstream.status;
141140
return input.format === "text"
142141
? new Response(payload.text, { headers: { "content-type": "text/plain; charset=utf-8" } })
143142
: Response.json({ text: payload.text });

‎tests/server/audio-transcriptions.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,20 @@ describe("standalone transcription API", () => {
327327
expect((await captured[0]!.formData()).get("model")).toBeNull();
328328
});
329329

330-
test("malformed Pool response records one failure and no provisional success", async () => {
330+
test("malformed Pool response records upstream status before body validation (#4502)", async () => {
331331
savePoolConfig();
332332
respond = () => Response.json({ missing: "text" });
333333
const outcomes = spyOn(routing, "recordCodexUpstreamOutcome");
334+
try {
335+
expect((await request()).status).toBe(502);
336+
expect(outcomes.mock.calls.filter(call => call[1] === "pool-a").map(call => call[2])).toEqual([200]);
337+
} finally { outcomes.mockRestore(); }
338+
});
339+
340+
test("upstream HTTP error records real failure status for Pool account", async () => {
341+
savePoolConfig();
342+
respond = () => new Response("upstream failure", { status: 502 });
343+
const outcomes = spyOn(routing, "recordCodexUpstreamOutcome");
334344
try {
335345
expect((await request()).status).toBe(502);
336346
expect(outcomes.mock.calls.filter(call => call[1] === "pool-a").map(call => call[2])).toEqual([502]);

0 commit comments

Comments
 (0)