Skip to content

Use Claude Code with your ChatGPT, Kimi, Cursor or Grok subscription via a local Anthropic-compatible proxy

Rust

fail closed on truncated codex streams

Raine Virta3469da7820fe
diff --git a/src/providers/codex/translate/reducer.ts b/src/providers/codex/translate/reducer.ts
index 1e1b447..2391489 100644
--- a/src/providers/codex/translate/reducer.ts
+++ b/src/providers/codex/translate/reducer.ts
@@ -57,6 +57,8 @@ type BlockState = TextState | ToolState;
const BUFFERED_TOOL_PROGRESS_LOG_INTERVAL_MS = 30_000;
const BUFFERED_TOOL_LARGE_ARGS_BYTES = 1_000_000;
+const BUFFERED_TOOL_MAX_ARGS_BYTES = 5_000_000;
+const BUFFERED_TOOL_MAX_DURATION_MS = 120_000;
function shouldBufferToolArgs(name: string): boolean {
return name === "Read";
@@ -103,6 +105,39 @@ function logBufferedToolProgress(log: Logger, state: ToolState, force = false):
});
}
+function throwIfBufferedToolExceeded(state: ToolState): void {
+ if (!state.bufferUntilDone) return;
+ const elapsedMs = Date.now() - state.startedAt;
+ if (state.argsAccum.length <= BUFFERED_TOOL_MAX_ARGS_BYTES && elapsedMs <= BUFFERED_TOOL_MAX_DURATION_MS) return;
+ throw new UpstreamStreamError(
+ "failed",
+ `Buffered ${state.name} tool arguments exceeded safe limits`,
+ );
+}
+
+function describeOpenBlock(outputIndex: number, state: BlockState): Record<string, unknown> {
+ if (state.kind === "text") {
+ return {
+ outputIndex,
+ index: state.index,
+ kind: state.kind,
+ textLength: state.textAccum.length,
+ };
+ }
+ return {
+ outputIndex,
+ index: state.index,
+ kind: state.kind,
+ callId: state.callId,
+ name: state.name,
+ deltaCount: state.deltaCount,
+ elapsedMs: Date.now() - state.startedAt,
+ bufferUntilDone: state.bufferUntilDone,
+ emittedArgs: state.emittedArgs,
+ args: toolArgSummary(state.argsAccum),
+ };
+}
+
/**
* Single source of truth for translating Codex Responses SSE into a
* stream of typed, downstream-agnostic ReducerEvents. Both the streaming
@@ -243,6 +278,7 @@ export async function* reduceUpstream(
state.deltaCount += 1;
state.hadDelta = true;
logBufferedToolProgress(log, state);
+ throwIfBufferedToolExceeded(state);
if (!state.bufferUntilDone) {
state.emittedArgs = true;
yield { kind: "tool-delta", index: state.index, partialJson: delta };
@@ -316,7 +352,7 @@ export async function* reduceUpstream(
index: state.index,
callId: state.callId,
name: state.name,
- args: state.argsAccum,
+ args: toolArgSummary(state.argsAccum),
});
yield { kind: "tool-stop", index: state.index };
}
@@ -339,6 +375,24 @@ export async function* reduceUpstream(
}
}
+ const openBlocks = Array.from(blocksByOutputIndex, ([outputIndex, state]) =>
+ describeOpenBlock(outputIndex, state),
+ );
+ if (!diagnostics.sawTerminalEvent || openBlocks.length) {
+ log.warn("upstream stream ended without complete response", {
+ sawTerminalEvent: diagnostics.sawTerminalEvent,
+ lastEventType: diagnostics.lastEventType,
+ openBlocks,
+ stats: diagnostics.stats,
+ });
+ throw new UpstreamStreamError(
+ "failed",
+ diagnostics.sawTerminalEvent
+ ? "Upstream stream ended with open output blocks"
+ : "Upstream stream ended without a terminal response event",
+ );
+ }
+
const stopReason: StopReason = incomplete ? "max_tokens" : sawToolUse ? "tool_use" : "end_turn";
yield { kind: "finish", stopReason, usage: finalUsage };
}
diff --git a/src/providers/codex/translate/stream.test.ts b/src/providers/codex/translate/stream.test.ts
index dcb0505..70846e7 100644
--- a/src/providers/codex/translate/stream.test.ts
+++ b/src/providers/codex/translate/stream.test.ts
@@ -36,6 +36,20 @@ function abortingUpstream(err: Error): ReadableStream<Uint8Array> {
});
}
+function upstreamThatErrorsAfterChunks(chunks: string[], err: Error): ReadableStream<Uint8Array> {
+ const encoder = new TextEncoder();
+ let index = 0;
+ return new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (index >= chunks.length) {
+ controller.error(err);
+ return;
+ }
+ controller.enqueue(encoder.encode(chunks[index++]));
+ },
+ });
+}
+
async function collect(stream: ReadableStream<Uint8Array>): Promise<string> {
const reader = stream.getReader();
const decoder = new TextDecoder();
@@ -96,6 +110,10 @@ describe("translateStream", () => {
}),
sse("keepalive", {}),
sse("keepalive", {}),
+ sse("response.output_item.done", {
+ output_index: 0,
+ item: { type: "message", id: "msg_upstream" },
+ }),
sse("response.completed", { response: { usage: {} } }),
];
@@ -115,6 +133,86 @@ describe("translateStream", () => {
}
});
+ it("emits an error instead of message_stop when upstream ends with an open Read block", async () => {
+ const chunks = [
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "function_call", call_id: "call_read", name: "Read" },
+ }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: "{\"file_path\"" }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: ":\"/tmp/a\"" }),
+ ];
+
+ const output = await collect(
+ translateStream(upstreamFromChunks(chunks), {
+ messageId: "msg_1",
+ model: "gpt-5.5",
+ log: silentLog,
+ }),
+ );
+
+ expect(output).toContain("event: content_block_start");
+ expect(output).toContain("event: content_block_stop");
+ expect(output).toContain("event: error");
+ expect(output).toContain("Upstream stream ended without a terminal response event");
+ expect(output).not.toContain("input_json_delta");
+ expect(output).not.toContain("event: message_stop");
+ });
+
+ it("closes an active Read block before non-client upstream errors", async () => {
+ const chunks = [
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "function_call", call_id: "call_read", name: "Read" },
+ }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: "{\"file_path\"" }),
+ ];
+ const err = new DOMException("The connection was closed.", "AbortError");
+
+ const output = await collect(
+ translateStream(upstreamThatErrorsAfterChunks(chunks, err), {
+ messageId: "msg_1",
+ model: "gpt-5.5",
+ log: silentLog,
+ }),
+ );
+
+ expect(output.indexOf("event: content_block_stop")).toBeLessThan(output.indexOf("event: error"));
+ expect(output).toContain("event: error");
+ expect(output).not.toContain("input_json_delta");
+ });
+
+ it("fails buffered Read arguments that exceed the safe duration", async () => {
+ Date.now = () => now;
+ try {
+ const chunks = [
+ sse("response.output_item.added", {
+ output_index: 0,
+ item: { type: "function_call", call_id: "call_read", name: "Read" },
+ }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: "{\"file_path\"" }),
+ sse("response.function_call_arguments.delta", { output_index: 0, delta: ":\"/tmp/a\"" }),
+ ];
+
+ const output = await collect(
+ translateStream(upstreamFromChunks(chunks, 121_000), {
+ messageId: "msg_1",
+ model: "gpt-5.5",
+ log: silentLog,
+ }),
+ );
+
+ expect(output).toContain("Buffered Read tool arguments exceeded safe limits");
+ expect(output).toContain("event: content_block_stop");
+ expect(output).toContain("event: error");
+ expect(output).not.toContain("input_json_delta");
+ expect(output).not.toContain("event: message_stop");
+ } finally {
+ Date.now = realNow;
+ now = 0;
+ }
+ });
+
it("treats aborted upstream reads as cancellation", async () => {
const abort = new AbortController();
abort.abort();
diff --git a/src/providers/codex/translate/stream.ts b/src/providers/codex/translate/stream.ts
index 7b683cd..b7f80b0 100644
--- a/src/providers/codex/translate/stream.ts
+++ b/src/providers/codex/translate/stream.ts
@@ -39,7 +39,7 @@ export function translateStream(
const encoder = new TextEncoder();
return new ReadableStream<Uint8Array>({
async start(controller) {
- const activeTools = new Map<number, { id: string; name: string }>();
+ const openBlocks = new Map<number, { type: "text" | "tool"; id?: string; name?: string }>();
const diagnostics = createUpstreamStreamDiagnostics();
let closed = false;
let messageStarted = false;
@@ -75,12 +75,24 @@ export function translateStream(
if (!messageStarted || Date.now() - lastEmitAt < KEEPALIVE_INTERVAL_MS) return;
emit("ping", { type: "ping" });
};
+ const activeToolCalls = () =>
+ Array.from(openBlocks.values()).filter(
+ (block): block is { type: "tool"; id: string; name: string } =>
+ block.type === "tool" && typeof block.id === "string" && typeof block.name === "string",
+ );
+ const closeOpenBlocks = () => {
+ for (const [index] of openBlocks) {
+ emit("content_block_stop", { type: "content_block_stop", index });
+ }
+ openBlocks.clear();
+ };
const logWatchdog = () => {
- const activeToolCalls = Array.from(activeTools.values());
+ const tools = activeToolCalls();
opts.log.info("codex stream watchdog", {
elapsedMs: Date.now() - startedAt,
- activeToolNames: activeToolCalls.map((tool) => tool.name),
- activeToolCalls,
+ activeToolNames: tools.map((tool) => tool.name),
+ activeToolCalls: tools,
+ openBlocks: Array.from(openBlocks.entries(), ([index, block]) => ({ index, ...block })),
messageStarted,
lastReducerEvent,
lastEmitEvent,
@@ -118,6 +130,7 @@ export function translateStream(
lastReducerEvent = e.kind;
switch (e.kind) {
case "text-start":
+ openBlocks.set(e.index, { type: "text" });
ensureMessageStart();
emit("content_block_start", {
type: "content_block_start",
@@ -133,10 +146,11 @@ export function translateStream(
});
break;
case "text-stop":
+ openBlocks.delete(e.index);
emit("content_block_stop", { type: "content_block_stop", index: e.index });
break;
case "tool-start":
- activeTools.set(e.index, { id: e.id, name: e.name });
+ openBlocks.set(e.index, { type: "tool", id: e.id, name: e.name });
ensureMessageStart();
emit("content_block_start", {
type: "content_block_start",
@@ -161,10 +175,13 @@ export function translateStream(
emitPingIfStale();
break;
case "tool-stop":
- activeTools.delete(e.index);
+ openBlocks.delete(e.index);
emit("content_block_stop", { type: "content_block_stop", index: e.index });
break;
case "finish":
+ if (openBlocks.size) {
+ throw new UpstreamStreamError("failed", "Stream finished with open content blocks");
+ }
ensureMessageStart();
opts.onFinish?.({ stopReason: e.stopReason, usage: e.usage });
emit("message_delta", {
@@ -177,13 +194,18 @@ export function translateStream(
}
}
} catch (err) {
- const activeToolNames = Array.from(activeTools.values(), (tool) => tool.name);
- const activeToolCalls = Array.from(activeTools.values());
+ const activeToolCalls = Array.from(openBlocks.values()).filter(
+ (block): block is { type: "tool"; id: string; name: string } =>
+ block.type === "tool" && typeof block.id === "string" && typeof block.name === "string",
+ );
+ const activeToolNames = activeToolCalls.map((tool) => tool.name);
+ const openBlockDetails = Array.from(openBlocks.entries(), ([index, block]) => ({ index, ...block }));
if (opts.signal?.aborted) {
opts.log.info("stream cancelled", {
err: describeError(err),
activeToolNames,
activeToolCalls,
+ openBlocks: openBlockDetails,
diagnostics: describeDiagnostics(diagnostics),
});
return;
@@ -194,6 +216,7 @@ export function translateStream(
message: err.message,
activeToolNames,
activeToolCalls,
+ openBlocks: openBlockDetails,
clientAborted: opts.signal?.aborted ?? false,
diagnostics: describeDiagnostics(diagnostics),
upstreamHeaders: describeHeaders(opts.upstreamHeaders),
@@ -201,6 +224,7 @@ export function translateStream(
const diagnosticFile = await writeDiagnosticFile(opts.reqId, "upstream-stream-error", detail);
opts.log.warn("upstream stream error", { ...detail, diagnosticFile });
ensureMessageStart();
+ closeOpenBlocks();
emit("error", {
type: "error",
error: {
@@ -213,6 +237,7 @@ export function translateStream(
err: describeError(err),
activeToolNames,
activeToolCalls,
+ openBlocks: openBlockDetails,
clientAborted: opts.signal?.aborted ?? false,
diagnostics: describeDiagnostics(diagnostics),
upstreamHeaders: describeHeaders(opts.upstreamHeaders),
@@ -220,6 +245,7 @@ export function translateStream(
const diagnosticFile = await writeDiagnosticFile(opts.reqId, "stream-translation-error", detail);
opts.log.error("stream translation error", { ...detail, diagnosticFile });
ensureMessageStart();
+ closeOpenBlocks();
emit("error", {
type: "error",
error: { type: "api_error", message: String(err) },
diff --git a/src/server.ts b/src/server.ts
index ee20e51..a7c314e 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -222,6 +222,10 @@ function isAbortError(err: unknown): boolean {
return err instanceof Error && err.name === "AbortError";
}
+function isClosedControllerError(err: unknown): boolean {
+ return err instanceof TypeError && err.message.includes("Controller is already closed");
+}
+
function wrapStreamResponse(
resp: Response,
reqId: string,
@@ -230,26 +234,54 @@ function wrapStreamResponse(
): Response {
const body = resp.body!;
const reader = body.getReader();
+ let closed = false;
const stream = new ReadableStream<Uint8Array>({
async pull(controller) {
+ if (closed) return;
+ const safeClose = () => {
+ if (closed) return;
+ closed = true;
+ try {
+ controller.close();
+ } catch {}
+ };
+ const safeError = (err: unknown) => {
+ if (closed) return;
+ closed = true;
+ try {
+ controller.error(err);
+ } catch {}
+ };
try {
const { done, value } = await reader.read();
+ if (closed) return;
if (done) {
log.info("request_completed", { reqId, status: resp.status, ms: Date.now() - start });
- controller.close();
+ safeClose();
return;
}
- controller.enqueue(value);
+ try {
+ controller.enqueue(value);
+ } catch (err) {
+ if (!isClosedControllerError(err)) {
+ log.error("stream error", { reqId, err: String(err) });
+ reader.cancel().catch(() => {});
+ safeError(err);
+ } else {
+ closed = true;
+ }
+ }
} catch (err) {
- if (isAbortError(err)) {
+ if (isAbortError(err) || isClosedControllerError(err)) {
log.info("client disconnected", { reqId, ms: Date.now() - start });
} else {
log.error("stream error", { reqId, err: String(err) });
}
- controller.error(err);
+ safeError(err);
}
},
cancel() {
+ closed = true;
reader.cancel().catch(() => {});
},
});