Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Train E2: a stream cut still never resends a call that already streamed text
milind-sonib0b545aa3c87
diff --git a/server/drivers/openai-chat-stream-retry.test.ts b/server/drivers/openai-chat-stream-retry.test.tsindex 36580db0e3..5b484e8d59 100644--- a/server/drivers/openai-chat-stream-retry.test.ts+++ b/server/drivers/openai-chat-stream-retry.test.ts@@ -1,5 +1,5 @@-// A stream cut retries the one model call even after output streamed or a tool call ran earlier in the-// turn; these tests pin that resend and the side-effect budget it keeps.+// A stream cut retries the one model call even after a tool call ran earlier in the turn, but never once+// this call streamed text; these tests pin that resend and the side-effect budget it keeps.import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";@@ -66,16 +66,15 @@ describe("createOpenAIChatRuntime stream truncation retry", () => {expect(requests).toBe(3);});- it("resends a cut that streamed answer text and still finishes the turn", async () => {- const cutAfterText = () => sse('data: {"choices":[{"index":0,"delta":{"content":"Hel"}}]}\n\n'- + 'data: {"error":"terminated"}\n\n');- const { events, requests } = await runTurn([cutAfterText, answer]);- expect(events.filter((event) => event.type === "turn.retrying"))- .toEqual([expect.objectContaining({ attempt: 1, reason: "stream_cut" })]);- 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.each([+ ["a plain cut", () => sse('data: {"choices":[{"index":0,"delta":{"content":"Hel"}}]}\n\n'), "Stream ended before completion"],+ ["a terminated chunk", () => sse('data: {"choices":[{"index":0,"delta":{"content":"Hel"}}]}\n\n' + 'data: {"error":"terminated"}\n\n'), "terminated"],+ ])("does not resend %s once answer text streamed, even with a clean retry available", async (_name, cut, message) => {+ const { events, requests } = await runTurn([cut, answer]);+ expect(events.some((event) => event.type === "turn.retrying")).toBe(false);+ expect(events.find((event) => event.type === "runtime.error")).toMatchObject({ message: expect.stringContaining(message) });+ expect(events.at(-1)).toMatchObject({ type: "turn.completed", ok: false });+ expect(requests).toBe(1);});it("resends a cut in a round after a tool call ran, without running the tool twice", async () => {diff --git a/server/drivers/openai-chat.ts b/server/drivers/openai-chat.tsindex 684f40a3dc..ab9d089bf9 100644--- a/server/drivers/openai-chat.ts+++ b/server/drivers/openai-chat.ts@@ -710,9 +710,9 @@ export function createOpenAIChatRuntime<Config>(options: RuntimeOptions<Config>)tools.definitions.length = 0;continue;}- // A stream cut retries the one model call even after earlier output — the resend may re-run a side-effectful tool call under a fresh ID, still better than failing the whole turn; without a caller opt-in only the cut itself retries.+ // Never resend once this call streamed (the person already saw or heard it). A stream cut ran nothing upstream, so it resends after earlier tool rounds too (their results are in the request); without a caller opt-in only the cut itself retries.const truncation = error instanceof StreamTruncatedError, retryScale = truncation ? options.retryScale ?? 1 : options.retryScale;- if (retryScale === undefined || abort.signal.aborted || error instanceof RuntimeNoticeError || (!truncation && ((error instanceof ChatProtocolError && verdict.reason !== "stream_cut") || !verdict.transient)) || (verdict.reason !== "stream_cut" && (streamed || seenCalls.size)) || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;+ if (retryScale === undefined || abort.signal.aborted || streamed || error instanceof RuntimeNoticeError || (!truncation && ((error instanceof ChatProtocolError && verdict.reason !== "stream_cut") || !verdict.transient)) || (verdict.reason !== "stream_cut" && seenCalls.size) || attempt >= RETRY_MAX_ATTEMPTS - 1) throw error;const delayMs = computeBackoff(attempt++);emit({ ...base(turn.threadId, turnId), type: "turn.retrying", attempt, delayMs, reason: truncation ? "stream_truncated" : verdict.reason });await interruptibleDelay(delayMs * retryScale, abort.signal).promise;