Skip to content

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

Rust

classify upstream stream read failures

Raine Virta5bea6ae4a4d4
diff --git a/src/providers/codex/translate/reducer.ts b/src/providers/codex/translate/reducer.ts
index 4fdf3c1..afa722a 100644
--- a/src/providers/codex/translate/reducer.ts
+++ b/src/providers/codex/translate/reducer.ts
@@ -255,7 +255,16 @@ export async function* reduceUpstream(
});
}
- for await (const evt of parseSseStream(upstream, diagnostics.stats)) {
+ const events = parseSseStream(upstream, diagnostics.stats);
+ while (true) {
+ let next: Awaited<ReturnType<typeof events.next>>;
+ try {
+ next = await events.next();
+ } catch (err) {
+ throw upstreamReadError(err);
+ }
+ if (next.done) break;
+ const evt = next.value;
if (!evt.data) continue;
let p: any;
try {
@@ -496,6 +505,12 @@ export async function* reduceUpstream(
};
}
+function upstreamReadError(err: unknown): Error {
+ if (err instanceof UpstreamStreamError) return err;
+ const message = err instanceof Error ? err.message : String(err);
+ return new UpstreamStreamError("failed", `Upstream stream read failed: ${message}`);
+}
+
export function mapUsageToAnthropic(u: CodexUsage | undefined): {
input_tokens: number;
output_tokens: number;
diff --git a/src/providers/codex/translate/stream.test.ts b/src/providers/codex/translate/stream.test.ts
index 70846e7..964eb85 100644
--- a/src/providers/codex/translate/stream.test.ts
+++ b/src/providers/codex/translate/stream.test.ts
@@ -159,7 +159,7 @@ describe("translateStream", () => {
expect(output).not.toContain("event: message_stop");
});
- it("closes an active Read block before non-client upstream errors", async () => {
+ it("reports upstream read failures as upstream stream errors", async () => {
const chunks = [
sse("response.output_item.added", {
output_index: 0,
@@ -168,18 +168,25 @@ describe("translateStream", () => {
sse("response.function_call_arguments.delta", { output_index: 0, delta: "{\"file_path\"" }),
];
const err = new DOMException("The connection was closed.", "AbortError");
+ const logs: Array<{ msg: string; fields: unknown }> = [];
+ const log = {
+ ...silentLog,
+ warn: (msg: string, fields: unknown) => logs.push({ msg, fields }),
+ };
const output = await collect(
translateStream(upstreamThatErrorsAfterChunks(chunks, err), {
messageId: "msg_1",
model: "gpt-5.5",
- log: silentLog,
+ log,
}),
);
expect(output.indexOf("event: content_block_stop")).toBeLessThan(output.indexOf("event: error"));
expect(output).toContain("event: error");
+ expect(output).toContain("Upstream stream read failed: The connection was closed.");
expect(output).not.toContain("input_json_delta");
+ expect(logs.some((entry) => entry.msg === "upstream stream error")).toBe(true);
});
it("fails buffered Read arguments that exceed the safe duration", async () => {