fix(codex): harden streaming timeouts + Responses terminal events

Raise stall/connect timeouts to 60s (configurable per-provider), accept
codex response.done, and always emit a terminal response.failed + [DONE]
for Responses passthrough when a stream closes, stalls, or aborts before
a terminal event — preventing codex clients from hanging.

Co-authored-by: jonathanli12 <jonathanli12@users.noreply.github.com>
Co-authored-by: rifuki <rifuki@users.noreply.github.com>
Co-authored-by: nguyenha935 <nguyenha935@users.noreply.github.com>
Co-authored-by: trananhtung <trananhtung@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decolua
2026-06-06 16:07:43 +07:00
co-authored by jonathanli12 rifuki nguyenha935 trananhtung Cursor
parent f161b295a5
commit 9caea88528
15 changed files with 311 additions and 30 deletions
+1 -1
View File
@@ -319,7 +319,7 @@ describe("handleImageGenerationCore", () => {
headers: expect.objectContaining({
authorization: "Bearer codex-token",
"chatgpt-account-id": "account-123",
version: "0.129.0",
version: "0.136.0",
}),
})
);
@@ -0,0 +1,83 @@
import { describe, expect, it } from "vitest";
import { FORMATS } from "../../open-sse/translator/formats.js";
import { createSSETransformStreamWithLogger } from "../../open-sse/utils/stream.js";
async function runTransform(input) {
const encoder = new TextEncoder();
const stream = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode(input));
controller.close();
},
});
const output = stream.pipeThrough(
createSSETransformStreamWithLogger(
FORMATS.OPENAI_RESPONSES,
FORMATS.OPENAI_RESPONSES,
"codex",
null,
null,
"gpt-5.5",
),
);
const reader = output.getReader();
const decoder = new TextDecoder();
let text = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
text += decoder.decode(value, { stream: true });
}
text += decoder.decode();
return text;
}
describe("OpenAI Responses streaming termination", () => {
it("emits a response.failed event when a Responses stream closes before a terminal event", async () => {
const output = await runTransform([
`event: response.created`,
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_test", status: "in_progress" } })}`,
"",
`event: response.output_text.delta`,
`data: ${JSON.stringify({ type: "response.output_text.delta", delta: "partial" })}`,
"",
].join("\n"));
expect(output).toContain("event: response.failed");
expect(output).toContain('"type":"response.failed"');
expect(output).not.toContain("data: null");
expect(output).toContain("data: [DONE]");
});
it("does not add response.failed when a Responses stream already completed", async () => {
const output = await runTransform([
`event: response.completed`,
`data: ${JSON.stringify({ type: "response.completed", response: { id: "resp_test", status: "completed" } })}`,
"",
].join("\n"));
expect(output).toContain("event: response.completed");
expect(output).not.toContain("event: response.failed");
expect(output).not.toContain("data: null");
expect(output).toContain("data: [DONE]");
});
it("emits response.failed before DONE when a Responses stream sends DONE without a terminal event", async () => {
const output = await runTransform([
`event: response.created`,
`data: ${JSON.stringify({ type: "response.created", response: { id: "resp_test", status: "in_progress" } })}`,
"",
"data: [DONE]",
"",
].join("\n"));
expect(output.indexOf("event: response.failed")).toBeLessThan(output.indexOf("data: [DONE]"));
expect(output.match(/data: \[DONE\]/g)).toHaveLength(1);
expect(output).not.toContain("data: null");
});
});
@@ -0,0 +1,72 @@
import { describe, expect, it } from "vitest";
import { createDisconnectAwareStream } from "../../open-sse/utils/streamHandler.js";
import { buildAbortedResponsesTerminalBytes } from "../../open-sse/utils/responsesStreamHelpers.js";
// Minimal stream controller stub
function makeController() {
let connected = true;
return {
signal: new AbortController().signal,
startTime: Date.now(),
isConnected: () => connected,
handleComplete: () => { connected = false; },
handleError: () => { connected = false; },
handleDisconnect: () => { connected = false; },
abort: () => { connected = false; },
};
}
async function readAll(stream) {
const reader = stream.getReader();
const decoder = new TextDecoder();
let text = "";
while (true) {
const { value, done } = await reader.read();
if (done) break;
text += decoder.decode(value, { stream: true });
}
text += decoder.decode();
return text;
}
describe("Responses abort terminal synthesis", () => {
it("emits response.failed + [DONE] when upstream errors (abort/stall)", async () => {
// Upstream readable that errors mid-stream (simulates fetch abort on stall)
const upstream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("event: response.created\ndata: {}\n\n"));
controller.error(new Error("stream stall timeout"));
},
});
const out = createDisconnectAwareStream(
{ readable: upstream, writable: { getWriter: () => ({ abort: () => Promise.resolve() }) } },
makeController(),
buildAbortedResponsesTerminalBytes
);
const text = await readAll(out);
expect(text).toContain("event: response.failed");
expect(text).toContain("data: [DONE]");
});
it("does not synthesize terminal for non-Responses streams (callback null)", async () => {
const upstream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("data: hi\n\n"));
controller.error(new Error("socket hang up"));
},
});
const out = createDisconnectAwareStream(
{ readable: upstream, writable: { getWriter: () => ({ abort: () => Promise.resolve() }) } },
makeController(),
null
);
const text = await readAll(out);
expect(text).not.toContain("response.failed");
expect(text).not.toContain("[DONE]");
});
});