From 0c213c09a0194634e050351b0dd61ae887ba8f7d Mon Sep 17 00:00:00 2001 From: jmspwr <152643299+jmspwr@users.noreply.github.com> Date: Sat, 7 Nov 2026 11:10:40 +0000 Subject: [PATCH] fix: recover x.ai Responses WS continuations rejected without a type x.ai answers a stale previous_response_id with an HTTP-style error envelope and no top-level type. The OpenAI parser closed the socket, which the client treated as an unclassified transport failure and answered by arming emergency shrink. Treat that envelope as the existing continuation-rejected resend. --- src/responses-ws.ts | 37 +++++- tests/xai-responses-ws-error-envelope.test.ts | 124 ++++++++++++++++++ 2 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 tests/xai-responses-ws-error-envelope.test.ts diff --git a/src/responses-ws.ts b/src/responses-ws.ts index f42046cdb6..7b5f0f6711 100644 --- a/src/responses-ws.ts +++ b/src/responses-ws.ts @@ -76,6 +76,25 @@ function object(value: unknown): value is JsonObject { return value !== null && typeof value === "object" && !Array.isArray(value); } +// x.ai's Responses WebSocket rejects a stale previous_response_id with an +// HTTP-style envelope and no top-level type: +// {"error":{"message":"gRPC error: Response with id= not found","type":"api_error"}} +// OpenAI uses {type:"error", error:{code:"previous_response_not_found"}}. +// Closing the socket on the x.ai frame hides the miss and arms emergency shrink. +const XAI_RESPONSE_MISSING = /Response with id=\S+ not found/; + +function continuationRejected(error: JsonObject, status: unknown): boolean { + if (error.code === "previous_response_not_found") return true; + if (status === 400 && (error.code === undefined || error.code === "invalid_request_error")) return true; + const message = error.message; + return error.type === "api_error" && typeof message === "string" && XAI_RESPONSE_MISSING.test(message); +} + +function untypedErrorEnvelope(frame: unknown): { frame: JsonObject; error: JsonObject } | undefined { + if (!object(frame) || typeof frame.type === "string" || !object(frame.error)) return undefined; + return { frame, error: frame.error }; +} + function describeEventType(frame: unknown): string { if (frame === null) return "null"; if (!object(frame)) return Array.isArray(frame) ? "array" : String(typeof frame); @@ -495,6 +514,22 @@ export class ResponsesWsUpstream { try { if (typeof event.data !== "string" || Buffer.byteLength(event.data) > this.requestBytes) throw new Error("Invalid or oversized Responses WebSocket frame"); const frame: unknown = JSON.parse(event.data); + const bare = untypedErrorEnvelope(frame); + if (bare && !settled) { + if (retryFull && continuationRejected(bare.error, bare.frame.status)) { + retryFull = false; + this.resetHistory("continuation-rejected"); + this.log("debug", "upstream retry reason=continuation-rejected action=resend-full"); + sendCreate(body); + return; + } + clean(); + this.resetHistory("upstream-error"); + const status = typeof bare.frame.status === "number" && bare.frame.status >= 400 && bare.frame.status <= 599 ? bare.frame.status : 400; + this.log("warn", `upstream rejected phase=await-first-event status=${status}`); + resolve(new Response(JSON.stringify(bare.frame), { status, headers: { "content-type": "application/json" } })); + return; + } if (!object(frame) || typeof frame.type !== "string") throw new Error("Invalid Responses WebSocket event"); receivedEvents++; const completed = output.observe(frame); @@ -507,7 +542,7 @@ export class ResponsesWsUpstream { resolve(this.fetch(url, options, false)); return; } - if (retryFull && (error.code === "previous_response_not_found" || frame.status === 400 && (error.code === undefined || error.code === "invalid_request_error"))) { + if (retryFull && continuationRejected(error, frame.status)) { retryFull = false; this.resetHistory("continuation-rejected"); this.log("debug", "upstream retry reason=continuation-rejected action=resend-full"); diff --git a/tests/xai-responses-ws-error-envelope.test.ts b/tests/xai-responses-ws-error-envelope.test.ts new file mode 100644 index 0000000000..19b7696bc8 --- /dev/null +++ b/tests/xai-responses-ws-error-envelope.test.ts @@ -0,0 +1,124 @@ +// x.ai rejects a stale previous_response_id on the Responses WebSocket with an +// HTTP-style envelope and no top-level type. OpenAI sends +// {type:"error", error:{code:"previous_response_not_found"}}. Treating the x.ai +// frame as a protocol violation closed the socket and armed emergency shrink +// even though the existing resend-full path recovers the turn. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { WebSocketServer } from "ws"; +import { ResponsesWsUpstream } from "../src/responses-ws.ts"; + +type Item = Record; + +const user = (text: string): Item => ({ type: "message", role: "user", content: text }); +const assistant = (text: string): Item => ({ type: "message", role: "assistant", content: text }); + +function completedFrame(id: string, output: Item[]): string { + return JSON.stringify({ + type: "response.completed", + response: { id, status: "completed", output }, + }); +} + +async function upstream() { + const seen: Item[] = []; + const server = http.createServer((_req, res) => { res.writeHead(404).end(); }); + const wss = new WebSocketServer({ server }); + let serial = 0; + wss.on("connection", peer => { + peer.on("message", raw => { + const request = JSON.parse(raw.toString()) as Item; + seen.push(request); + if (typeof request.previous_response_id === "string") { + peer.send(JSON.stringify({ + error: { + message: `gRPC error: Response with id=${request.previous_response_id} not found`, + type: "api_error", + }, + })); + return; + } + const id = `resp_xai_${++serial}`; + const output = [assistant(`ok-${serial}`)]; + peer.send(JSON.stringify({ type: "response.created", response: { id, status: "in_progress", output: [] } })); + peer.send(completedFrame(id, output)); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const port = (server.address() as { port: number }).port; + return { + seen, + url: `http://127.0.0.1:${port}/v1/responses`, + close: () => new Promise(resolve => { wss.close(); server.close(() => resolve()); }), + }; +} + +test("x.ai untyped response-not-found envelope resends the full request", { timeout: 15000 }, async () => { + const fake = await upstream(); + const log: string[] = []; + const transport = new ResponsesWsUpstream((_level, message) => log.push(message)); + const firstInput = [user("one")]; + const firstOutput = [assistant("ok-1")]; + try { + const first = await transport.fetch(fake.url, { + method: "POST", + body: JSON.stringify({ model: "grok-4", input: firstInput }), + signal: AbortSignal.timeout(10000), + }); + assert.equal(first.status, 200); + assert.equal((await first.json() as Item).id, "resp_xai_1"); + const second = await transport.fetch(fake.url, { + method: "POST", + body: JSON.stringify({ model: "grok-4", input: [...firstInput, ...firstOutput, user("two")] }), + signal: AbortSignal.timeout(10000), + }); + assert.equal(second.status, 200, "continuation miss must recover, not fail the turn"); + const recovered = await second.json() as Item; + assert.equal(recovered.status, "completed"); + assert.equal(recovered.id, "resp_xai_2"); + assert.equal(fake.seen.length, 3, JSON.stringify(fake.seen.map(row => row.previous_response_id ?? "full"))); + assert.equal(fake.seen[0].previous_response_id, undefined); + assert.equal(fake.seen[1].previous_response_id, "resp_xai_1"); + assert.equal(fake.seen[2].previous_response_id, undefined, "retry must drop the dead checkpoint"); + assert.deepEqual(fake.seen[2].input, [...firstInput, ...firstOutput, user("two")]); + const text = log.join("\n"); + assert.match(text, /upstream retry reason=continuation-rejected action=resend-full/); + assert.doesNotMatch(text, /resp_xai_1|Invalid Responses WebSocket event/); + } finally { + transport.close(); + await fake.close(); + } +}); + +test("an untyped error envelope that is not a missing response is surfaced, not a socket kill", { timeout: 15000 }, async () => { + const server = http.createServer((_req, res) => { res.writeHead(404).end(); }); + const wss = new WebSocketServer({ server }); + let sends = 0; + wss.on("connection", peer => { + peer.on("message", () => { + sends++; + peer.send(JSON.stringify({ error: { message: "rate limit", type: "api_error" } })); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const port = (server.address() as { port: number }).port; + const transport = new ResponsesWsUpstream(); + try { + const response = await transport.fetch(`http://127.0.0.1:${port}/v1/responses`, { + method: "POST", + body: JSON.stringify({ model: "grok-4", input: [user("one")] }), + signal: AbortSignal.timeout(10000), + }); + assert.equal(response.status, 400); + const body = await response.json() as Item; + assert.equal((body.error as Item).message, "rate limit"); + assert.equal(sends, 1, "a non-continuation error must not be resent"); + } finally { + transport.close(); + await new Promise(resolve => { wss.close(); server.close(() => resolve()); }); + } +});