Skip to content

A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.

fix(proxy): 被拒的 WebSocket 连接被客户端重置时引擎不再退出

chipfighter526be18e997a
diff --git a/plugin/src/proxy.ts b/plugin/src/proxy.ts
index 2ca374d..45fc0a4 100644
--- a/plugin/src/proxy.ts
+++ b/plugin/src/proxy.ts
@@ -838,6 +838,9 @@ async function main(): Promise<void> {
const started = Date.now();
const path = (req.url ?? "/").split("?")[0];
const identity = requestIdentity(req.headers);
+ // Node takes its own 'error' listener off an upgraded socket. Without one, a client resetting the connection (Codex
+ // can, right after a 426) throws and stops the engine (2026-10-19: four probe engines at once).
+ client.on("error", () => {});
// A retired engine takes no new WebSocket; Codex opens it again, on the new engine.
if (retiredAt !== null) {
client.end("HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
@@ -852,6 +855,8 @@ async function main(): Promise<void> {
let down = 0;
let upstreamStatus: string | null = null;
connectUpstream(via).then((socket) => {
+ // The client went away while the upstream connected: its close has passed, nothing would close this one.
+ if (client.destroyed) { socket.destroy(); return; }
const connection: Live = { client, socket, last: Date.now(), closedForSelection: false, activeWhileUnchecked: false, path, identity };
sockets.add(connection);
if (identity.threadId) {
diff --git a/spike/src/proxy-http-fixture.ts b/spike/src/proxy-http-fixture.ts
index 894ce7c..79881e5 100644
--- a/spike/src/proxy-http-fixture.ts
+++ b/spike/src/proxy-http-fixture.ts
@@ -14,5 +14,7 @@ tls.connect = ((_options: unknown, callback: () => void) => net.connect(upstream
})) as typeof tls.connect;
const scriptUrl = new URL("../../plugin/src/proxy.ts", import.meta.url);
const script = fileURLToPath(scriptUrl);
-process.argv = [process.execPath, script, "--stay", "--port", process.env.CAM_TEST_PROXY_PORT!];
+// CAM_TEST_ENGINE_ARGS: more of the engine's own flags, e.g. --force-http.
+const more = (process.env.CAM_TEST_ENGINE_ARGS ?? "").split(" ").filter(Boolean);
+process.argv = [process.execPath, script, "--stay", "--port", process.env.CAM_TEST_PROXY_PORT!, ...more];
await import(scriptUrl.href);
diff --git a/spike/src/proxy-http.test.ts b/spike/src/proxy-http.test.ts
index 9eedd28..f1f8553 100644
--- a/spike/src/proxy-http.test.ts
+++ b/spike/src/proxy-http.test.ts
@@ -4,6 +4,7 @@ import assert from "node:assert/strict";
import { spawn } from "node:child_process";
import { mkdtempSync, rmSync } from "node:fs";
import http from "node:http";
+import net from "node:net";
import { tmpdir } from "node:os";
import { join } from "node:path";
import test, { type TestContext } from "node:test";
@@ -16,7 +17,7 @@ async function bounded<T>(promise: Promise<T>, message: string, ms = 2000): Prom
} finally { clearTimeout(timer!); }
}
-async function fixture(t: TestContext, handler: http.RequestListener, connectDelay = 0) {
+async function fixture(t: TestContext, handler: http.RequestListener, connectDelay = 0, engineArgs: string[] = []) {
const root = mkdtempSync(join(tmpdir(), "cam-http-lifecycle-"));
const upstream = http.createServer(handler);
await new Promise<void>((resolve) => upstream.listen(0, "127.0.0.1", resolve));
@@ -26,7 +27,7 @@ async function fixture(t: TestContext, handler: http.RequestListener, connectDel
const port = (reservation.address() as { port: number }).port;
await new Promise<void>((resolve) => reservation.close(() => resolve()));
const child = spawn(process.execPath, [fileURLToPath(new URL("./proxy-http-fixture.ts", import.meta.url))], {
- env: { ...process.env, CAM_DATA_DIR: root, CAM_TEST_PROXY_PORT: String(port), CAM_TEST_UPSTREAM_PORT: String(upstreamPort), CAM_TEST_CONNECT_DELAY: String(connectDelay), NO_PROXY: "*", no_proxy: "*" },
+ env: { ...process.env, CAM_DATA_DIR: root, CAM_TEST_PROXY_PORT: String(port), CAM_TEST_UPSTREAM_PORT: String(upstreamPort), CAM_TEST_CONNECT_DELAY: String(connectDelay), CAM_TEST_ENGINE_ARGS: engineArgs.join(" "), NO_PROXY: "*", no_proxy: "*" },
stdio: ["ignore", "pipe", "pipe"],
});
let output = "";
@@ -126,3 +127,23 @@ test("a complete HTTP response keeps its status, headers, and bytes", async (t)
}), "complete response did not finish");
assert.deepEqual(result, { status: 201, header: "unchanged", body: expected });
});
+
+// 2026-10-19: Node leaves an upgraded socket without an 'error' listener of its own. Codex resetting the connection
+// the engine had just declined with 426 threw there and stopped the engine (four probe engines at once).
+test("a declined WebSocket upgrade that the client resets leaves the engine running", async (t) => {
+ const { port } = await fixture(t, () => assert.fail("a declined upgrade must not reach the upstream"), 0, ["--force-http"]);
+ for (let i = 0; i < 5; i++) {
+ const socket = net.connect(port, "127.0.0.1");
+ socket.on("error", () => {});
+ const answer = await bounded(new Promise<string>((resolve) => {
+ socket.once("data", (chunk) => { resolve(String(chunk)); socket.resetAndDestroy(); });
+ socket.write(["GET /backend-api/codex/responses HTTP/1.1", `Host: 127.0.0.1:${port}`, "Connection: Upgrade", "Upgrade: websocket", "Sec-WebSocket-Version: 13", "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==", "", ""].join("\r\n"));
+ }), `upgrade ${i + 1} got no answer`);
+ assert.match(answer, /^HTTP\/1\.1 426 /);
+ }
+ await new Promise((resolve) => setTimeout(resolve, 300));
+ const status = await bounded(new Promise<number>((resolve, reject) => {
+ http.get({ hostname: "127.0.0.1", port, path: "/__cam/health" }, (response) => { response.resume(); resolve(response.statusCode ?? 0); }).on("error", reject);
+ }), "the engine did not answer after the resets");
+ assert.equal(status, 200);
+});