Skip to content

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

fix(engines): handle handshake input pipe failures

Afloat03d4b0b043ba
diff --git a/server/engine-handshake-pipe.test.ts b/server/engine-handshake-pipe.test.ts
new file mode 100644
index 0000000000..4b92deef8b
--- /dev/null
+++ b/server/engine-handshake-pipe.test.ts
@@ -0,0 +1,56 @@
+import { ChildProcess } from "node:child_process";
+import { PassThrough } from "node:stream";
+import { afterEach, describe, expect, it, vi } from "vitest";
+
+import { acpHandshake } from "./engine-handshake.ts";
+import { killCliTree, spawnCli } from "./procs.ts";
+
+vi.mock("./procs.ts", () => ({ spawnCli: vi.fn(), killCliTree: vi.fn(async () => true) }));
+
+afterEach(() => vi.clearAllMocks());
+
+function fixture() {
+ const child = Object.assign(new ChildProcess(), {
+ stdin: new PassThrough(), stdout: new PassThrough(), stderr: new PassThrough(),
+ });
+ vi.mocked(spawnCli).mockReturnValue(child as ReturnType<typeof spawnCli>);
+ const answer = () => child.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", id: 1, result: { protocolVersion: 1 } })}\n`);
+ return { child, answer };
+}
+
+describe("engine update handshake input errors", () => {
+ it.each(["EPIPE", "ERR_STREAM_DESTROYED"])("reports %s instead of emitting an unhandled stdin error", async (code) => {
+ const { child, answer } = fixture();
+ const result = acpHandshake("fixture-agent", ["acp"], {});
+ const error = Object.assign(new Error(`fixture input closed: ${code}`), { code });
+ try {
+ expect(() => child.stdin.emit("error", error)).not.toThrow();
+ // Output already buffered by a child that exited must not turn the failed write into a passing handshake.
+ answer();
+ expect(await result).toBe(error.message);
+ expect(killCliTree).toHaveBeenCalledExactlyOnceWith(child, 2_000);
+ child.emit("exit", 1);
+ expect(killCliTree).toHaveBeenCalledTimes(1);
+ } finally {
+ answer();
+ await result;
+ }
+ });
+
+ it("keeps a completed handshake settled when closing the child later errors its input", async () => {
+ const { child, answer } = fixture();
+ const result = acpHandshake("fixture-agent", ["acp"], {});
+ answer();
+ expect(await result).toBeNull();
+ expect(() => child.stdin.emit("error", new Error("late input close"))).not.toThrow();
+ expect(killCliTree).toHaveBeenCalledTimes(1);
+ });
+
+ it("continues to report a child process launch error", async () => {
+ const { child } = fixture();
+ const result = acpHandshake("fixture-agent", ["acp"], {});
+ child.emit("error", new Error("fixture launch error"));
+ expect(await result).toBe("fixture launch error");
+ expect(killCliTree).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/server/engine-handshake.ts b/server/engine-handshake.ts
index 44bb29f68e..cb8e27136e 100644
--- a/server/engine-handshake.ts
+++ b/server/engine-handshake.ts
@@ -29,6 +29,7 @@ export function acpHandshake(command: string, args: readonly string[], env: Node
const timer = setTimeout(() => finish("it did not answer the ACP handshake in time"), timeoutMs);
timer.unref?.();
child.once("error", (error) => finish(error.message));
+ child.stdin.on("error", (error) => finish(error.message));
child.once("exit", (code) => {
const said = stripVTControlCharacters(stderr).split(/\r?\n/u).map((line) => line.trim()).find(Boolean);
finish(said ? said.slice(0, 400) : `it stopped with exit code ${code} before the ACP handshake`);