Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(drivers): resend a chat stream the provider closed before completion

ninesummeracb28f6b948c
diff --git a/server/drivers/openai-chat-stream-retry.test.ts b/server/drivers/openai-chat-stream-retry.test.ts
new file mode 100644
index 0000000000..10312ddfaa
--- /dev/null
+++ b/server/drivers/openai-chat-stream-retry.test.ts
@@ -0,0 +1,74 @@
+// The chat runtime may resend a stream the provider closed before any completion marker; these
+// tests pin that one protocol error's retry and the guards that keep a resend side-effect free.
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import type { RuntimeEvent } from "../contracts.ts";
+import { OpenAICompatDriver } from "./openai-compat.ts";
+
+// The backoff between attempts is covered by retry.ts itself; here it would only slow the suite.
+vi.mock("./retry.ts", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("./retry.ts")>();
+ return { ...actual, computeBackoff: () => 0 };
+});
+
+afterEach(() => vi.unstubAllGlobals());
+
+const sse = (body: string) => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } });
+/** Closes with no [DONE], no finish_reason and nothing streamed: the truncation under test. */
+const truncated = () => sse("");
+const answer = () => sse('data: {"choices":[{"index":0,"delta":{"content":"hi"}}]}\n\n'
+ + 'data: {"choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1}}\n\n');
+
+/** `bodies` is one SSE response per chat completion; the last repeats for any further attempt. */
+async function runTurn(bodies: Array<() => Response>): Promise<{ events: RuntimeEvent[]; requests: number }> {
+ let chatRequests = 0;
+ vi.stubGlobal("fetch", vi.fn(async (url: string | URL) =>
+ String(url).endsWith("/chat/completions")
+ ? (chatRequests++, bodies[Math.min(chatRequests - 1, bodies.length - 1)]())
+ : new Response("", { status: 404 })));
+ const instance = await OpenAICompatDriver.create({
+ instanceId: "compat", displayName: "Compat", enabled: true,
+ config: OpenAICompatDriver.decodeConfig({ url: "https://compat.example/v1", apiKeyEnv: "COMPAT_API_KEY", model: "compat-m3" }),
+ environment: { COMPAT_API_KEY: "secret" },
+ });
+ const events: RuntimeEvent[] = [];
+ instance.adapter.onEvent((event) => events.push(event));
+ await instance.adapter.sendTurn({ threadId: "thread", text: "hi" });
+ await vi.waitFor(() => {
+ if (!events.some((event) => event.type === "turn.completed")) throw new Error("turn still running");
+ });
+ await instance.dispose();
+ return { events, requests: chatRequests };
+}
+
+describe("createOpenAIChatRuntime stream truncation retry", () => {
+ it("resends a stream that ended before completion and finishes the turn on the next attempt", async () => {
+ const { events, requests } = await runTurn([truncated, answer]);
+ expect(events.filter((event) => event.type === "turn.retrying"))
+ .toEqual([expect.objectContaining({ attempt: 1, reason: "stream_truncated" })]);
+ expect(events.find((event) => event.type === "runtime.error")).toBeUndefined();
+ expect(events.find((event) => event.type === "item.completed")).toMatchObject({ text: "hi" });
+ expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: true });
+ expect(requests).toBe(2);
+ });
+
+ it("gives up after three truncated attempts and reports the last error", async () => {
+ const { events, requests } = await runTurn([truncated]);
+ expect(events.filter((event) => event.type === "turn.retrying").map((event) => event.type === "turn.retrying" && event.attempt))
+ .toEqual([1, 2]);
+ expect(events.find((event) => event.type === "runtime.error")).toMatchObject({ message: "Stream ended before completion" });
+ expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: false, stopReason: "error" });
+ expect(requests).toBe(3);
+ });
+
+ it("does not resend once answer text streamed, even with a clean retry available", async () => {
+ const { events, requests } = await runTurn([
+ () => sse('data: {"choices":[{"index":0,"delta":{"content":"Hel"}}]}\n\n'),
+ answer,
+ ]);
+ expect(events.some((event) => event.type === "turn.retrying")).toBe(false);
+ expect(events.find((event) => event.type === "runtime.error")).toMatchObject({ message: "Stream ended before completion" });
+ expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: false, stopReason: "error" });
+ expect(requests).toBe(1);
+ });
+});
diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.ts
index 3d1a9186fb..7d11a5ec38 100644
--- a/server/drivers/openai-chat.ts
+++ b/server/drivers/openai-chat.ts
@@ -707,11 +707,17 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)
continue;
}
// Once a call has been handled, never replay it through a turn retry.
- if (options.retryScale === undefined || abort.signal.aborted || streamed || seenCalls.size ||
- error instanceof ChatProtocolError || !verdict.transient || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;
+ // A stream the provider closed with no completion marker is the one protocol error
+ // worth resending: the guards above prove nothing streamed and no tool call ran, so
+ // the resend repeats no text and executes nothing — and it applies even where the
+ // caller never opted into transient retries (openai-compat and five friends).
+ const truncation = verdict.reason === "stream_truncated";
+ const retryScale = truncation ? options.retryScale ?? 1 : options.retryScale;
+ if (retryScale === undefined || abort.signal.aborted || streamed || seenCalls.size ||
+ (error instanceof ChatProtocolError && !truncation) || !verdict.transient || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;
const delayMs = computeBackoff(attempt++);
emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt, delayMs, reason: verdict.reason });
- await interruptibleDelay(delayMs * options.retryScale, abort.signal).promise;
+ await interruptibleDelay(delayMs * retryScale, abort.signal).promise;
abort.signal.throwIfAborted();
}
}
diff --git a/server/drivers/retry.ts b/server/drivers/retry.ts
index db54d89746..ff0512c369 100644
--- a/server/drivers/retry.ts
+++ b/server/drivers/retry.ts
@@ -13,7 +13,8 @@ export type TransientReason =
| "overloaded"
| "server_error"
| "connection_reset"
- | "timeout";
+ | "timeout"
+ | "stream_truncated";
export type TerminalReason =
| "auth"
@@ -43,6 +44,9 @@ const TRANSIENT_PATTERNS: Array<{ pattern: RegExp; reason: TransientReason }> =
reason: "connection_reset",
},
{ pattern: /\btimeout(ed)?\b|\btimed? out\b/i, reason: "timeout" },
+ // A provider closing the stream with neither finish_reason nor [DONE]. Retriable only while the
+ // chat runtime's resend gate proves nothing streamed and no tool call ran.
+ { pattern: /\bstream ended before completion\b/i, reason: "stream_truncated" },
];
/** A subscription's usage running out: terminal even when phrased as a rate limit. Claude Code 2.1.295