Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

fix(plugins): bound async IPC send failures to a single terminal exit

koeria47f86696c306
diff --git a/apps/desktop/electron/main/plugin-host-process.mjs b/apps/desktop/electron/main/plugin-host-process.mjs
index 8a5374a67..4599ec051 100644
--- a/apps/desktop/electron/main/plugin-host-process.mjs
+++ b/apps/desktop/electron/main/plugin-host-process.mjs
@@ -27,36 +27,72 @@ import { AsyncLocalStorage } from "node:async_hooks";
const parentPort = process.parentPort;
-/** Electron utilityProcess and node:child_process disagree on the transport. */
-function send(message) {
- if (parentPort) parentPort.postMessage(message);
- else process.send?.(message);
-}
-
// Once the parent channel is gone, diagnostics must never re-enter it: every
// further send attempt would only produce a new failure to report. One stderr
-// note is the last thing a broken channel gets.
+// note is the last thing a broken channel gets. The terminal path is
+// idempotent: whichever failure surface gets here first (a synchronous throw
+// in `send`, an async delivery error callback, or a process-level transport
+// error) runs the single `process.exit` — later arrivals see the flag and stop.
let transportBroken = false;
+let exitStarted = false;
+
+function markBrokenAndExit(error) {
+ if (transportBroken) return;
+ transportBroken = true;
+ const reason = error?.code || error?.message || String(error ?? "unknown");
+ try {
+ process.stderr.write(`plugin host: parent IPC channel is broken (${reason})\n`);
+ } catch {
+ // Even stderr can be gone (closed pipe); nothing left to report to.
+ }
+ if (exitStarted) return;
+ exitStarted = true;
+ // The channel to the owner is gone: nothing this process does is useful
+ // anymore, and lingering here would leak the plugin host. A settled
+ // process.exit cannot recurse through further diagnostics.
+ process.exit(0);
+}
+
+// Async delivery failures on the node IPC channel do not throw inside `send`:
+// with the parent connector gone, `process.send` fails asynchronously and the
+// channel reports it as a process-level `error` event (typically EPIPE).
+// Handling it here keeps it out of `uncaughtException`, where the diagnostics
+// send would otherwise recurse forever. It is the same terminal break.
+process.on("error", (error) => {
+ markBrokenAndExit(error);
+});
+if (parentPort) {
+ // Electron's pinned parent-port never surfaces `disconnect`: the gone
+ // connector shows up as `close`, and undeliverable frames as `messageerror`.
+ // Treat both as the same terminal break.
+ parentPort.on("close", () => markBrokenAndExit(new Error("parent-port closed")));
+ parentPort.on("messageerror", (error) => markBrokenAndExit(error));
+}
+
+/**
+ * Send over the parent transport. On the node IPC channel delivery is
+ * asynchronous: failures are reported through `onDeliveryError` (or the
+ * process-level `error` handler above), never synchronously, so callers must
+ * not assume `send` returning means the parent received the frame.
+ */
+function send(message, onDeliveryError) {
+ if (parentPort) parentPort.postMessage(message);
+ else process.send?.(message, (error) => onDeliveryError?.(error));
+}
/** Best-effort send. Never throws, so callers that report failures through
* this channel cannot recurse into reporting the failure of the report. */
function trySend(message) {
if (transportBroken) return false;
try {
- send(message);
+ send(message, (error) => {
+ if (error) markBrokenAndExit(error);
+ });
return true;
- } catch {
- transportBroken = true;
- try {
- process.stderr.write("plugin host: parent IPC channel is broken\n");
- } catch {
- // Even stderr can be gone (closed pipe); nothing left to report to.
- }
- // The channel to the owner is gone: nothing this process does is useful
- // anymore, and lingering here would leak the plugin host. A settled
- // process.exit cannot recurse through further diagnostics.
- process.exit(0);
+ } catch (error) {
+ markBrokenAndExit(error);
}
+ return false;
}
function log(level, message) {
@@ -94,7 +130,20 @@ function call(api, args = []) {
return new Promise((resolve, reject) => {
pending.set(id, { resolve, reject, invocationId: invocation?.id });
try {
- send({ t: "call", id, api, args, ...(invocation ? { invocationId: invocation.id } : {}) });
+ send(
+ { t: "call", id, api, args, ...(invocation ? { invocationId: invocation.id } : {}) },
+ // An async delivery failure means the channel to the broker is gone:
+ // fail the pending call and take the same terminal break as every
+ // other transport error.
+ (error) => {
+ const entry = pending.get(id);
+ if (entry) {
+ pending.delete(id);
+ entry.reject(error);
+ }
+ if (error) markBrokenAndExit(error);
+ },
+ );
} catch (error) {
pending.delete(id);
reject(error);
diff --git a/apps/desktop/test/plugin-host-lifetime.test.mjs b/apps/desktop/test/plugin-host-lifetime.test.mjs
index b5da0bcc8..0dd0c1ae4 100644
--- a/apps/desktop/test/plugin-host-lifetime.test.mjs
+++ b/apps/desktop/test/plugin-host-lifetime.test.mjs
@@ -72,3 +72,92 @@ test("plugin host exits when a send to the parent channel fails", async () => {
clearTimeout(timer);
assert.equal(code, 0);
});
+
+/**
+ * Fixture whose `process.send` never touches the channel for diagnostics: a
+ * `log` frame succeeds synchronously but reports an EPIPE delivery failure
+ * asynchronously through the process-level `error` event — the boundary Node
+ * IPC actually uses when the parent connector dies between the send and the
+ * flush. Other frames (the init reply) still go through the real channel so
+ * the test can wait for the host to come up. `trigger` then produces the
+ * diagnostics path under test.
+ */
+function writeFixturePluginWithAsyncSendFailure(trigger) {
+ const dir = mkdtempSync(join(tmpdir(), "pi-plugin-async-send-"));
+ writeFileSync(
+ join(dir, "manifest.json"),
+ JSON.stringify({ schemaVersion: 1, id: "fixture.async-send", name: "AsyncSend", version: "0.0.1", main: "main.js" }),
+ "utf8",
+ );
+ writeFileSync(
+ join(dir, "main.js"),
+ `module.exports = {
+ onLoad() {
+ // Keep the host alive until the broken transport tears it down.
+ setInterval(() => {}, 1000);
+ const realSend = process.send.bind(process);
+ process.send = (message, callback) => {
+ if (message && message.t === "log") {
+ setImmediate(() => {
+ const error = new Error("write EPIPE");
+ error.code = "EPIPE";
+ process.emit("error", error);
+ });
+ return true;
+ }
+ return realSend(message, callback);
+ };
+ setImmediate(() => { ${trigger} });
+ },
+ };`,
+ "utf8",
+ );
+ return dir;
+}
+
+/**
+ * Both async scenarios share the same contract: the diagnostic send fails
+ * asynchronously (EPIPE via process.emit("error")), the host must take the
+ * terminal path exactly once — a clean exit(0), not a recursion loop — and
+ * must not emit the "broken channel" note more than once.
+ */
+async function runAsyncSendFailureScenario(trigger) {
+ const dir = writeFixturePluginWithAsyncSendFailure(trigger);
+ const child = forkHost();
+ const exited = once(child, "exit");
+ const ready = once(child, "message");
+ const stderrChunks = [];
+ child.stderr.on("data", (chunk) => stderrChunks.push(String(chunk)));
+ child.send({ t: "init", id: 1, pluginId: "fixture.async-send", pluginPath: dir, main: "main.js" });
+ const [initReply] = await ready;
+ assert.equal(initReply.ok, true, "fixture plugin should load");
+ const timer = setTimeout(() => {
+ child.kill("SIGKILL");
+ }, 5000);
+ const [code, signal] = await exited;
+ clearTimeout(timer);
+ const stderrText = stderrChunks.join("");
+ return { code, signal, stderrText };
+}
+
+test("plugin host exits once when an async send failure surfaces through uncaughtException", async () => {
+ // The plugin's own uncaught exception makes the host log diagnostics through
+ // the transport; the patched send then fails asynchronously with EPIPE.
+ const { code, signal, stderrText } = await runAsyncSendFailureScenario(
+ `throw new Error("plugin boom");`,
+ );
+ assert.equal(signal, null, "host must exit by itself, not be SIGKILLed");
+ assert.equal(code, 0, "host must take the clean terminal path exactly once");
+ const notes = stderrText.split("parent IPC channel is broken").length - 1;
+ assert.equal(notes, 1, `diagnostics must not recurse (got ${notes} broken-channel notes)`);
+});
+
+test("plugin host exits once when an async send failure surfaces through unhandledRejection", async () => {
+ const { code, signal, stderrText } = await runAsyncSendFailureScenario(
+ `Promise.reject(new Error("plugin boom"));`,
+ );
+ assert.equal(signal, null, "host must exit by itself, not be SIGKILLed");
+ assert.equal(code, 0, "host must take the clean terminal path exactly once");
+ const notes = stderrText.split("parent IPC channel is broken").length - 1;
+ assert.equal(notes, 1, `diagnostics must not recurse (got ${notes} broken-channel notes)`);
+});
diff --git a/apps/desktop/test/plugin-host-parent-kill-acceptance.mjs b/apps/desktop/test/plugin-host-parent-kill-acceptance.mjs
new file mode 100644
index 000000000..a16fb9467
--- /dev/null
+++ b/apps/desktop/test/plugin-host-parent-kill-acceptance.mjs
@@ -0,0 +1,226 @@
+/**
+ * Acceptance: the Electron plugin host must not outlive an abruptly killed
+ * parent (ADR 0008 / PR #2381 review item ④).
+ *
+ * Electron's `utilityProcess` child never sees a JavaScript-visible
+ * `disconnect` — the pinned parent-port just goes dead — so the host relies on
+ * its ping/pong keepalive plus the parent-port `close` event. This script
+ * proves both, against the real Electron runtime:
+ *
+ * 1. Healthy-parent responsiveness: with the broker answering every `ping`
+ * with a `pong`, the host keeps round-tripping and stays alive well past
+ * the miss budget — and every ping is answered within a deadline.
+ * 2. Parent kill: SIGKILL the Electron main process (no disconnect
+ * handshake, nothing graceful). The orphaned host must exit by itself
+ * within the keepalive budget.
+ *
+ * Run from apps/desktop (requires `electron` from devDependencies, i.e. a
+ * completed `pnpm install`):
+ *
+ * node test/plugin-host-parent-kill-acceptance.mjs
+ *
+ * Exit code 0 = accepted, 1 = rejected (with the reason on stderr). The script
+ * needs no app build: it forks `electron/main/plugin-host-process.mjs` directly
+ * with a minimal fixture plugin.
+ */
+import { spawn } from "node:child_process";
+import { mkdtempSync, writeFileSync, existsSync, readFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join, resolve } from "node:path";
+import { fileURLToPath } from "node:url";
+import { createRequire } from "node:module";
+
+const here = dirname(fileURLToPath(import.meta.url));
+const desktopRoot = resolve(join(here, ".."));
+const hostEntry = join(desktopRoot, "electron/main/plugin-host-process.mjs");
+
+// Keepalive tuned for a fast acceptance run: 1s interval, 2 misses → an
+// orphaned host must be gone within roughly 3s plus slack.
+const PING_INTERVAL_MS = 1000;
+const PING_MISSES = 2;
+const KILL_GRACE_MS = PING_INTERVAL_MS * (PING_MISSES + 2) + 3000;
+const HEALTHY_MS = PING_INTERVAL_MS * (PING_MISSES + 1) + 1000;
+const PONG_DEADLINE_MS = 3000;
+
+function fail(message) {
+ process.stderr.write(`ACCEPTANCE FAIL: ${message}\n`);
+ if (brokerOutput) process.stderr.write(`--- electron output ---\n${brokerOutput}\n`);
+ process.exit(1);
+}
+
+function findElectronBinary() {
+ if (process.env.ELECTRON_BIN) return process.env.ELECTRON_BIN;
+ try {
+ const require = createRequire(join(desktopRoot, "package.json"));
+ // The `electron` package's index resolves to the installed binary path.
+ return require("electron");
+ } catch {
+ return null;
+ }
+}
+
+// Minimal broker main for Electron: answers pings, relays diagnostics, and
+// records the host's pid so the outer driver can watch it after the kill.
+function writeBrokerMain(dir) {
+ writeFileSync(
+ join(dir, "manifest.json"),
+ JSON.stringify({ schemaVersion: 1, id: "fixture.parentkill", name: "ParentKill", version: "0.0.1", main: "main.js" }),
+ "utf8",
+ );
+ // Referenced timer: proves liveness comes from the transport, not the plugin.
+ writeFileSync(
+ join(dir, "main.js"),
+ "module.exports = { onLoad() { setInterval(() => {}, 1000); } };",
+ "utf8",
+ );
+ writeFileSync(
+ join(dir, "broker.cjs"),
+ `const { app, utilityProcess } = require("electron");
+const { join } = require("node:path");
+const { writeFileSync } = require("node:fs");
+
+const dir = __dirname;
+let answered = 0;
+
+app.whenReady().then(() => {
+ const child = utilityProcess.fork(${JSON.stringify(hostEntry)}, [], {
+ serviceName: "fixture-parent-kill",
+ stdio: "inherit",
+ env: {
+ ...process.env,
+ PI_PLUGIN_PING_INTERVAL_MS: String(${PING_INTERVAL_MS}),
+ PI_PLUGIN_PING_MISSES: String(${PING_MISSES}),
+ },
+ });
+ child.on("spawn", () => {
+ writeFileSync(join(dir, "host-pid"), String(child.pid));
+ });
+ child.on("message", (message) => {
+ if (message && message.t === "ping") {
+ answered += 1;
+ child.postMessage({ t: "pong", id: message.id });
+ writeFileSync(join(dir, "pings-answered"), String(answered));
+ writeFileSync(join(dir, "last-pong-at"), String(Date.now()));
+ } else {
+ process.stdout.write("host: " + JSON.stringify(message) + "\\n");
+ }
+ });
+ child.on("exit", (code) => {
+ writeFileSync(join(dir, "host-exit"), String(code));
+ });
+ child.postMessage({
+ t: "init",
+ id: 1,
+ pluginId: "fixture.parentkill",
+ pluginPath: dir,
+ main: "main.js",
+ });
+});
+`,
+ "utf8",
+ );
+ return join(dir, "broker.cjs");
+}
+
+function waitFor(predicateFile, timeoutMs) {
+ const deadline = Date.now() + timeoutMs;
+ return new Promise((resolvePromise, rejectPromise) => {
+ const tick = () => {
+ try {
+ const content = readFileSync(predicateFile, "utf8").trim();
+ if (content) return resolvePromise(content);
+ } catch {
+ // Not written yet.
+ }
+ if (Date.now() > deadline) return rejectPromise(new Error(`timed out waiting for ${predicateFile}`));
+ setTimeout(tick, 100);
+ };
+ tick();
+ });
+}
+
+function pidAlive(pid) {
+ try {
+ process.kill(pid, 0);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+const electronBin = findElectronBinary();
+if (!electronBin || !existsSync(String(electronBin))) {
+ fail(
+ "electron binary not found — run `pnpm install` in apps/desktop first, " +
+ "or set ELECTRON_BIN. This acceptance must run in an environment with " +
+ "the real Electron runtime (it cannot run in a sandbox without Electron).",
+ );
+}
+
+const dir = mkdtempSync(join(tmpdir(), "pi-parent-kill-"));
+const brokerMain = writeBrokerMain(dir);
+// Electron refuses to start as root without this flag (Linux CI containers).
+const isRoot = typeof process.getuid === "function" && process.getuid() === 0;
+const electron = spawn(String(electronBin), [...(isRoot ? ["--no-sandbox"] : []), brokerMain], {
+ stdio: ["ignore", "pipe", "pipe"],
+ env: { ...process.env, ELECTRON_RUN_AS_NODE: "" },
+});
+let brokerOutput = "";
+electron.stdout.on("data", (chunk) => {
+ brokerOutput += String(chunk);
+});
+electron.stderr.on("data", (chunk) => {
+ brokerOutput += String(chunk);
+});
+
+// --- Phase 1: healthy-parent responsiveness --------------------------------
+// Wait for the first answered keepalive round-trip, then confirm the
+// round-trip keeps flowing (host still alive several intervals later) and
+// that no pong was missed (pings answered keeps increasing past the miss
+// budget — an unresponsive parent would have let the host exit instead).
+try {
+ const hostPid = Number(await waitFor(join(dir, "host-pid"), 15000));
+ await waitFor(join(dir, "pings-answered"), 15000);
+ const firstAnswered = Number(readFileSync(join(dir, "pings-answered"), "utf8"));
+
+ await new Promise((r) => setTimeout(r, HEALTHY_MS));
+ const answeredAfterHealthy = Number(readFileSync(join(dir, "pings-answered"), "utf8"));
+ if (answeredAfterHealthy <= firstAnswered) {
+ fail(`healthy parent received no further pings after the first (${answeredAfterHealthy}/${firstAnswered})`);
+ }
+ if (!pidAlive(hostPid)) {
+ fail("host exited while the parent was healthy and answering pings");
+ }
+ // Responsiveness deadline: the last pong must be recent — the broker answers
+ // synchronously on message receipt, so a stale last-pong-at means pings
+ // stopped flowing.
+ const lastPongAt = Number(readFileSync(join(dir, "last-pong-at"), "utf8"));
+ if (Date.now() - lastPongAt > PONG_DEADLINE_MS) {
+ fail(`healthy-parent pong round-trips stalled (${Date.now() - lastPongAt}ms since last pong)`);
+ }
+ process.stdout.write(
+ `phase 1 OK: healthy parent answered ${answeredAfterHealthy} pings; host pid ${hostPid} alive\n`,
+ );
+
+ // --- Phase 2: parent kill ------------------------------------------------
+ // SIGKILL: no disconnect handshake, no graceful teardown — the exact case
+ // Electron's pinned parent-port cannot signal to the host.
+ process.kill(electron.pid, "SIGKILL");
+ const startedAt = Date.now();
+ const deadline = startedAt + KILL_GRACE_MS;
+ while (pidAlive(hostPid) && Date.now() < deadline) {
+ await new Promise((r) => setTimeout(r, 200));
+ }
+ if (pidAlive(hostPid)) {
+ fail(`orphaned host pid ${hostPid} still alive ${KILL_GRACE_MS}ms after the parent was SIGKILLed`);
+ }
+ const exitCode = existsSync(join(dir, "host-exit")) ? readFileSync(join(dir, "host-exit"), "utf8") : "(no exit record)";
+ process.stdout.write(
+ `phase 2 OK: orphaned host exited on its own after ${Date.now() - startedAt}ms (exit record: ${exitCode})\n`,
+ );
+ process.stdout.write("ACCEPTANCE PASS\n");
+ process.exit(0);
+} catch (error) {
+ process.stderr.write(brokerOutput);
+ fail(error?.message ?? String(error));
+}