A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, bug] fix(ai): recover stale Codex chains after metadata (#2374)
Seth Kartene683fcb53db6
diff --git a/packages/ai/.changes/fix-codex-chain-reset-metadata.md b/packages/ai/.changes/fix-codex-chain-reset-metadata.mdnew file mode 100644index 0000000000..625d23d1d6--- /dev/null+++ b/packages/ai/.changes/fix-codex-chain-reset-metadata.md@@ -0,0 +1 @@+- Fixed Codex stale-continuation recovery when WebSocket lifecycle or metadata events arrive before the provider error.diff --git a/packages/ai/src/providers/openai-codex-responses.ts b/packages/ai/src/providers/openai-codex-responses.tsindex a3af062915..f373812c9f 100644--- a/packages/ai/src/providers/openai-codex-responses.ts+++ b/packages/ai/src/providers/openai-codex-responses.ts@@ -174,10 +174,13 @@ export const streamOpenAICodexResponses: StreamFunction<"openai-codex-responses"return;} catch (error) {const aborted = options?.signal?.aborted;- // Only reset the chain while nothing was streamed yet: after the- // first event the retry would duplicate "start"/content events.+ // Only reset the chain before visible output starts; retrying after+ // content events would duplicate streamed assistant output.if (!aborted && !websocketStarted && !chainResetRetried && isStaleCodexContinuationError(error)) {chainResetRetried = true;+ // A failed attempt may have supplied response metadata before+ // rejecting the continuation. Do not retain that dead anchor.+ delete output.responseId;continue;}if (aborted || isCodexNonTransportError(error)) {@@ -1097,7 +1100,19 @@ function buildCachedWebSocketRequestBody(entry: CachedWebSocketConnection, body:};}-async function* startWebSocketOutputOnFirstEvent(+function isCodexVisibleResponseEvent(event: ResponseStreamEvent): boolean {+ return (+ event.type === "response.completed" ||+ event.type === "response.incomplete" ||+ event.type.startsWith("response.output_") ||+ event.type.startsWith("response.reasoning_") ||+ event.type.startsWith("response.content_") ||+ event.type.startsWith("response.refusal.") ||+ event.type.startsWith("response.function_")+ );+}++async function* startWebSocketOutputOnFirstVisibleEvent(events: AsyncIterable<ResponseStreamEvent>,output: AssistantMessage,stream: AssistantMessageEventStream,@@ -1105,7 +1120,10 @@ async function* startWebSocketOutputOnFirstEvent(): AsyncGenerator<ResponseStreamEvent> {let started = false;for await (const event of events) {- if (!started) {+ // Codex can emit lifecycle, telemetry, or vendor metadata before rejecting+ // a stale continuation. Keep the attempt retryable until an event can+ // produce output consumed by processResponsesStream.+ if (!started && isCodexVisibleResponseEvent(event)) {started = true;onStart();stream.push({ type: "start", partial: output });@@ -1152,7 +1170,7 @@ async function processWebSocketStream(try {socket.send(JSON.stringify({ type: "response.create", ...requestBody }));await processResponsesStream(- startWebSocketOutputOnFirstEvent(+ startWebSocketOutputOnFirstVisibleEvent(mapCodexEvents(parseWebSocket(socket, options?.signal)),output,stream,diff --git a/packages/ai/test/openai-codex-stream.test.ts b/packages/ai/test/openai-codex-stream.test.tsindex 5c139612b5..3073b1e210 100644--- a/packages/ai/test/openai-codex-stream.test.ts+++ b/packages/ai/test/openai-codex-stream.test.ts@@ -12,7 +12,7 @@ import {} from "../src/providers/openai-codex-responses.js";import { convertResponsesMessages, processResponsesStream } from "../src/providers/openai-responses-shared.js";import { buildBaseOptions } from "../src/providers/simple-options.js";-import type { Api, AssistantMessage, Context, Model, ToolResultMessage } from "../src/types.js";+import type { Api, AssistantMessage, AssistantMessageEvent, Context, Model, ToolResultMessage } from "../src/types.js";import { AssistantMessageEventStream } from "../src/utils/event-stream.js";const originalFetch = global.fetch;@@ -198,71 +198,23 @@ describe("openai-codex streaming", () => {expect(sawDone).toBe(true);});- it("completes after response.completed even when the SSE body stays open", async () => {- const tempDir = mkdtempSync(join(tmpdir(), "pi-codex-stream-"));- process.env.PI_CODING_AGENT_DIR = tempDir;- const token = mockToken();- const encoder = new TextEncoder();- const sse = buildSSEPayload({ status: "completed", includeDone: true });-- const stream = new ReadableStream<Uint8Array>({- start(controller) {- controller.enqueue(encoder.encode(sse));- },- });-- global.fetch = vi.fn(async (input: string | URL) => {- const url = typeof input === "string" ? input : input.toString();- if (url === "https://api.github.com/repos/openai/codex/releases/latest") {- return new Response(JSON.stringify({ tag_name: "rust-v0.0.0" }), { status: 200 });- }- if (url.startsWith("https://raw.githubusercontent.com/openai/codex/")) {- return new Response("PROMPT", { status: 200, headers: { etag: '"etag"' } });- }- if (url === "https://chatgpt.com/backend-api/codex/responses") {- return new Response(stream, {- status: 200,- headers: { "content-type": "text/event-stream" },- });- }- return new Response("not found", { status: 404 });- }) as typeof fetch;-- const model: Model<"openai-codex-responses"> = {- id: "gpt-5.1-codex",- name: "GPT-5.1 Codex",- api: "openai-codex-responses",- provider: "openai-codex",- baseUrl: "https://chatgpt.com/backend-api",- reasoning: true,- input: ["text"],- cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },- contextWindow: 400000,- maxTokens: 128000,- };-- const context: Context = {- systemPrompt: "You are a helpful assistant.",- messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }],- };-- const result = await Promise.race([- streamOpenAICodexResponses(model, context, { apiKey: token, transport: "sse" }).result(),- new Promise<never>((_, reject) => {- setTimeout(() => reject(new Error("Timed out waiting for completed SSE stream")), 1000);- }),- ]);-- expect(result.content.find((c) => c.type === "text")?.text).toBe("Hello");- expect(result.stopReason).toBe("stop");- });-- it("maps response.incomplete to stopReason length even when the SSE body stays open", async () => {+ it.each([+ {+ label: "completes after response.completed",+ payload: { status: "completed" as const, includeDone: true },+ stopReason: "stop" as const,+ },+ {+ label: "maps response.incomplete to stopReason length",+ payload: { status: "incomplete" as const },+ stopReason: "length" as const,+ },+ ])("$label even when the SSE body stays open", async ({ payload, stopReason }) => {const tempDir = mkdtempSync(join(tmpdir(), "pi-codex-stream-"));process.env.PI_CODING_AGENT_DIR = tempDir;const token = mockToken();const encoder = new TextEncoder();- const sse = buildSSEPayload({ status: "incomplete" });+ const sse = buildSSEPayload(payload);const stream = new ReadableStream<Uint8Array>({start(controller) {@@ -305,15 +257,12 @@ describe("openai-codex streaming", () => {messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }],};- const result = await Promise.race([- streamOpenAICodexResponses(model, context, { apiKey: token, transport: "sse" }).result(),- new Promise<never>((_, reject) => {- setTimeout(() => reject(new Error("Timed out waiting for incomplete SSE stream")), 1000);- }),- ]);+ // The SSE body never closes: the terminal event must end the stream on its own, so+ // the suite timeout is the only bound (no wall-clock race).+ const result = await streamOpenAICodexResponses(model, context, { apiKey: token, transport: "sse" }).result();expect(result.content.find((c) => c.type === "text")?.text).toBe("Hello");- expect(result.stopReason).toBe("length");+ expect(result.stopReason).toBe(stopReason);});it("sets session_id/x-client-request-id headers and prompt_cache_key when sessionId is provided", async () => {@@ -1175,14 +1124,66 @@ describe("openai-codex streaming", () => {});});+ it("recovers a stale previous_response_id after lifecycle and metadata events without exposing an error", async () => {+ const token = mockToken();+ const sentBodies = installScriptedCodexWebSocket([+ (socket) => socket.emit(codexResponseEvents({ responseId: "resp_1", messageId: "msg_1", text: "Hello" })),+ (socket) =>+ socket.emit([+ { type: "response.created", response: { id: "resp_stale" } },+ { type: "response.in_progress", response: { id: "resp_stale" } },+ { type: "codex.response.metadata", headers: {} },+ { type: "responsesapi.websocket_timing", elapsed_ms: 1 },+ ...codexErrorEvents("previous_response_not_found", "Previous response with id 'resp_1' not found"),+ ]),+ (socket) => socket.emit(codexResponseEvents({ responseId: "resp_2", messageId: "msg_2", text: "Done" })),+ ]);++ const model = codexTestModel();+ const firstContext: Context = {+ systemPrompt: "You are a helpful assistant.",+ messages: [{ role: "user", content: "Say hello", timestamp: 1 }],+ };+ const first = await streamOpenAICodexResponses(model, firstContext, {+ apiKey: token,+ sessionId: "session-chain-reset-lifecycle",+ transport: "websocket-cached",+ }).result();++ const secondContext: Context = {+ systemPrompt: "You are a helpful assistant.",+ messages: [...firstContext.messages, first, { role: "user", content: "Now finish", timestamp: 2 }],+ };+ const secondStream = streamOpenAICodexResponses(model, secondContext, {+ apiKey: token,+ sessionId: "session-chain-reset-lifecycle",+ transport: "websocket-cached",+ });+ const secondEvents: AssistantMessageEvent[] = [];+ for await (const event of secondStream) secondEvents.push(event);+ const second = await secondStream.result();++ expect(second.stopReason).toBe("stop");+ expect(second.content[0]).toMatchObject({ type: "text", text: "Done" });+ expect(second.responseId).toBe("resp_2");+ expect(sentBodies).toHaveLength(3);+ expect(sentBodies[1].previous_response_id).toBe("resp_1");+ expect(sentBodies[2].previous_response_id).toBeUndefined();+ expect(secondEvents.filter((event) => event.type === "start")).toHaveLength(1);+ expect(secondEvents.at(-1)?.type).toBe("done");+ expect(global.fetch).not.toHaveBeenCalled();+ });+it("surfaces the error without further retries when the chain-reset retry fails again", async () => {const token = mockToken();const sentBodies = installScriptedCodexWebSocket([(socket) => socket.emit(codexResponseEvents({ responseId: "resp_1", messageId: "msg_1", text: "Hello" })),(socket) =>- socket.emit(- codexErrorEvents("previous_response_not_found", "Previous response with id 'resp_1' not found"),- ),+ socket.emit([+ // Metadata arriving before the stale rejection must not survive as the anchor.+ { type: "response.created", response: { id: "resp_stale" } },+ ...codexErrorEvents("previous_response_not_found", "Previous response with id 'resp_1' not found"),+ ]),(socket) =>socket.emit(codexErrorEvents("previous_response_not_found", "Previous response with id 'resp_9' not found"),@@ -1212,6 +1213,7 @@ describe("openai-codex streaming", () => {expect(second.stopReason).toBe("error");expect(second.errorMessage).toBe("Codex error: Previous response with id 'resp_9' not found");+ expect(second.responseId).toBeUndefined();// Exactly one chain-reset retry, then the error surfaces unchanged.expect(sentBodies).toHaveLength(3);const retryBody = sentBodies[2] as { previous_response_id?: string };