Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

[RSI, bug] fix(coding-agent): absorb owned worker bridge EPIPE instead of crashing the frontend (#2382)

Seth Kartend73349d50807
diff --git a/packages/coding-agent/.changes/fix-owned-worker-bridge-epipe.md b/packages/coding-agent/.changes/fix-owned-worker-bridge-epipe.md
new file mode 100644
index 0000000000..07d8a9e9a2
--- /dev/null
+++ b/packages/coding-agent/.changes/fix-owned-worker-bridge-epipe.md
@@ -0,0 +1,3 @@
+- Fixed a bridge write into a dead or stopped owned session worker (write EPIPE) crashing the whole CLI frontend: pipe errors are now absorbed so the existing worker close-based recovery (failing pending RPC commands and relaunching) runs instead.
+- Fixed RPC commands that arrive while the worker bridge is down being reported failed ("uncertain and was not replayed") and then replayed to the replacement worker anyway: a command is now tracked as pending only once it is actually written to a worker, so buffered commands replay exactly once and commands that reached a dead worker fail exactly once.
+- Fixed a worker that closed its stdin read end while staying alive hanging the frontend forever: the absorbed bridge EPIPE now kills the worker so the crash-recovery loop (failure responses, relaunch from the recovery descriptor) runs, and commands that were only buffered are failed when no recovery can replay them.
diff --git a/packages/coding-agent/src/cli/owned-session-worker.ts b/packages/coding-agent/src/cli/owned-session-worker.ts
index aa6467ed35..10e75d09bb 100644
--- a/packages/coding-agent/src/cli/owned-session-worker.ts
+++ b/packages/coding-agent/src/cli/owned-session-worker.ts
@@ -214,12 +214,16 @@ export async function runOwnedSessionWorkerFrontend(
let rpcStdoutPaused = false;
let detachRpcInput: (() => void) | undefined;
let detachRpcOutput: (() => void) | undefined;
- const bufferedRpcInput: string[] = [];
+ type PreparedRpcInput = {
+ framed: string;
+ pending?: { internalId: string; publicId?: string; command: string };
+ };
+ const bufferedRpcInput: PreparedRpcInput[] = [];
const pendingRpcCommands = new Map<string, { publicId?: string; command: string }>();
const anonymousRpcIdPrefix = `prime-agent-owned-${randomUUID()}`;
let anonymousRpcCommandId = 0;
- const prepareRpcInput = (line: string): string => {
+ const prepareRpcInput = (line: string): PreparedRpcInput => {
try {
const command = JSON.parse(line) as { id?: unknown; type?: unknown } | null;
if (
@@ -229,16 +233,31 @@ export async function runOwnedSessionWorkerFrontend(
command.type === "extension_ui_response" ||
command.type === "ack_result"
) {
- return `${line}\n`;
+ return { framed: `${line}\n` };
}
const publicId = typeof command.id === "string" ? command.id : undefined;
const internalId = publicId ?? `${anonymousRpcIdPrefix}-${++anonymousRpcCommandId}`;
- pendingRpcCommands.set(internalId, { publicId, command: command.type });
- return publicId !== undefined ? `${line}\n` : serializeJsonLine({ ...command, id: internalId });
+ return {
+ framed: publicId !== undefined ? `${line}\n` : serializeJsonLine({ ...command, id: internalId }),
+ pending: { internalId, publicId, command: command.type },
+ };
} catch {
// The worker preserves the existing parse-error response contract.
- return `${line}\n`;
+ return { framed: `${line}\n` };
+ }
+ };
+ // A command is pending only once it is actually written to a worker. A
+ // line that merely sits in bufferedRpcInput was never started, so recovery
+ // replays it to the replacement worker; a command that reached the dead
+ // worker is failed as uncertain below and never replayed. Registering at
+ // receipt instead reported a buffered command as "was not replayed" while
+ // recovery replayed the same id anyway.
+ const writeRpcInput = (input: NodeJS.WritableStream, prepared: PreparedRpcInput): boolean => {
+ if (prepared.pending) {
+ const { internalId, ...pending } = prepared.pending;
+ pendingRpcCommands.set(internalId, pending);
}
+ return input.write(prepared.framed);
};
const observeRpcOutput = (line: string) => {
let parsed: unknown;
@@ -273,20 +292,33 @@ export async function runOwnedSessionWorkerFrontend(
pendingRpcCommands.delete(id);
}
};
+ const writeFailureResponse = (publicId: string | undefined, command: string) => {
+ process.stdout.write(
+ serializeJsonLine({
+ ...(publicId !== undefined ? { id: publicId } : {}),
+ type: "response",
+ command,
+ success: false,
+ error: "The isolated session worker stopped during this command; its result is uncertain and was not replayed",
+ }),
+ );
+ };
const failPendingRpcCommands = () => {
for (const pending of pendingRpcCommands.values()) {
- process.stdout.write(
- serializeJsonLine({
- ...(pending.publicId !== undefined ? { id: pending.publicId } : {}),
- type: "response",
- command: pending.command,
- success: false,
- error: "The isolated session worker stopped during this command; its result is uncertain and was not replayed",
- }),
- );
+ writeFailureResponse(pending.publicId, pending.command);
}
pendingRpcCommands.clear();
};
+ // Buffered commands never started, so the pending map cannot fail them.
+ // When recovery will not run they would otherwise leave the client without
+ // any terminal outcome, so fail them on the way out instead.
+ const failBufferedRpcCommands = () => {
+ for (const prepared of bufferedRpcInput.splice(0)) {
+ if (prepared.pending) {
+ writeFailureResponse(prepared.pending.publicId, prepared.pending.command);
+ }
+ }
+ };
const reapWorkerResources = (workerPid: number | undefined) => {
if (!workerPid) {
return;
@@ -309,10 +341,15 @@ export async function runOwnedSessionWorkerFrontend(
if (profile === "rpc") {
detachRpcInput = attachJsonlLineReader(process.stdin, (line) => {
- const framed = prepareRpcInput(line);
+ const prepared = prepareRpcInput(line);
const input = currentRpcInput;
if (input?.writable) {
- if (!input.write(framed)) {
+ // A refused write only means backpressure while the bridge is
+ // still writable: pause until it drains. An errored bridge stays
+ // unwritable and never drains, so keep reading and let the
+ // buffer branch below hold later commands for the replacement
+ // worker instead of stalling stdin until the close event.
+ if (!writeRpcInput(input, prepared) && input.writable) {
process.stdin.pause();
input.once("drain", () => {
if (currentRpcInput === input && !stdinEnded) {
@@ -321,7 +358,7 @@ export async function runOwnedSessionWorkerFrontend(
});
}
} else {
- bufferedRpcInput.push(framed);
+ bufferedRpcInput.push(prepared);
}
});
process.stdin.once("end", () => {
@@ -351,6 +388,28 @@ export async function runOwnedSessionWorkerFrontend(
stdio,
});
currentChild = child;
+ // A bridge write into a worker that closed its stdin lands as an
+ // unhandled 'error' event on that pipe (write EPIPE) and takes down the
+ // whole frontend, defeating the crash-recovery loop below. The child
+ // 'close' handler owns the fallout (failing pending RPC commands,
+ // relaunching with the recovery descriptor), so this listener keeps the
+ // pipe error from becoming a fatal one. When the pipe broke while the
+ // worker was still alive the bridge is permanently unusable: reap the
+ // worker process group so the close event always fires — a descendant
+ // holding inherited stdio would otherwise block it forever — and that
+ // close drives the fallout instead of waiting indefinitely on a worker
+ // that can no longer accept commands.
+ child.stdin?.on("error", (error) => {
+ if ((error as NodeJS.ErrnoException).code !== "EPIPE") {
+ return;
+ }
+ // Guard like forwardSignal: an exited child's pid may have been
+ // reassigned, so never signal the child itself after its exit.
+ if (child.exitCode === null && child.signalCode === null) {
+ child.kill("SIGKILL");
+ }
+ reapWorkerResources(child.pid);
+ });
if (!interactive) {
const childInput = child.stdin ?? undefined;
const childOutput = child.stdout ?? undefined;
@@ -371,7 +430,7 @@ export async function runOwnedSessionWorkerFrontend(
}
detachRpcOutput = attachJsonlLineReader(childOutput, observeRpcOutput);
for (const buffered of bufferedRpcInput.splice(0)) {
- childInput.write(buffered);
+ writeRpcInput(childInput, buffered);
}
if (stdinEnded) {
childInput.end();
@@ -431,10 +490,16 @@ export async function runOwnedSessionWorkerFrontend(
child.disconnect();
}
reapWorkerResources(workerPid);
+ // Buffered commands never started, so they replay instead of
+ // failing whenever recovery can run; count them like written ones
+ // so a worker death still routes through the recovery path.
const rpcCrashed =
profile === "rpc" &&
!terminating &&
- (exit.code !== 0 || exit.signal !== null || pendingRpcCommands.size > 0);
+ (exit.code !== 0 ||
+ exit.signal !== null ||
+ pendingRpcCommands.size > 0 ||
+ bufferedRpcInput.some((prepared) => prepared.pending !== undefined));
const workerExitCode = rpcCrashed && exit.code === 0 ? 1 : exit.code;
if (Date.now() - workerStartedAt >= 60_000) {
recoveryAttempt = 0;
@@ -444,10 +509,12 @@ export async function runOwnedSessionWorkerFrontend(
}
const shouldRecover = rpcCrashed && !stdinEnded && recoveryAttempt < 3;
if (!shouldRecover) {
+ failBufferedRpcCommands();
return terminationSignal ? exitCodeForSignal(terminationSignal) : workerExitCode;
}
const descriptor = readOwnedRecoveryDescriptor(recoveryDescriptorPath);
if (!descriptor?.sessionFile) {
+ failBufferedRpcCommands();
return terminationSignal ? exitCodeForSignal(terminationSignal) : workerExitCode;
}
workerArgs = createRpcRecoveryArgs(args, descriptor.sessionFile);
diff --git a/packages/coding-agent/test/fixtures/owned-session-worker-fixture.ts b/packages/coding-agent/test/fixtures/owned-session-worker-fixture.ts
index db76aa3192..f2164d20b3 100644
--- a/packages/coding-agent/test/fixtures/owned-session-worker-fixture.ts
+++ b/packages/coding-agent/test/fixtures/owned-session-worker-fixture.ts
@@ -1,4 +1,5 @@
-import { existsSync, writeFileSync } from "node:fs";
+import { spawn } from "node:child_process";
+import { closeSync, existsSync, writeFileSync } from "node:fs";
import {
closeOwnedSessionWorkerOwnerWatch,
installOwnedSessionWorkerOwnerWatch,
@@ -39,6 +40,20 @@ if (process.env.PRIME_AGENT_INTERNAL_OWNED_WORKER === "1") {
}),
);
};
+ const writeRecoveryDescriptor = () => {
+ const recoveryPath = process.env.PRIME_AGENT_INTERNAL_OWNED_RECOVERY_DESCRIPTOR;
+ if (pidPath && recoveryPath) {
+ writeFileSync(
+ recoveryPath,
+ `${JSON.stringify({
+ version: 1,
+ sessionId: "fixture-session",
+ sessionFile: `${pidPath}.jsonl`,
+ cwd: process.cwd(),
+ })}\n`,
+ );
+ }
+ };
attachJsonlLineReader(process.stdin, (line) => {
const command = JSON.parse(line) as { id?: string; type: string; marker?: string };
if (process.env.PRIME_AGENT_TEST_EXIT_ZERO_ON_COMMAND === command.type) {
@@ -47,23 +62,31 @@ if (process.env.PRIME_AGENT_INTERNAL_OWNED_WORKER === "1") {
if (process.env.PRIME_AGENT_TEST_CRASH_ON_COMMAND === command.type) {
process.exit(1);
}
+ if (process.env.PRIME_AGENT_TEST_CLOSE_STDIN_ON_COMMAND === command.type) {
+ outputResponse(command);
+ writeRecoveryDescriptor();
+ // A descendant holding the worker's inherited stdout/stderr keeps
+ // the child's close event from firing after a kill: the frontend
+ // must reap the whole worker group. The timer leases its life.
+ spawn(process.execPath, ["-e", "setTimeout(() => process.exit(0), 60000)"], {
+ stdio: ["ignore", "inherit", "inherit"],
+ });
+ // Close fd 0 while the worker stays alive, then mark it deaf.
+ closeSync(0);
+ if (pidPath) {
+ writeFileSync(`${pidPath}.deaf`, "deaf\n");
+ }
+ // fd 0 going away drains this loop, so the worker would exit
+ // cleanly before the test buffers follow-ups against it. Hold
+ // the loop: the frontend's EPIPE kill is the only worker death.
+ // test-policy: allow wall-clock-timer -- holds the deaf worker's loop so the frontend EPIPE kill is the only death
+ setInterval(() => {}, 1000);
+ return;
+ }
if (command.type === "ack_result") {
if (process.env.PRIME_AGENT_TEST_CRASH_ON_ACK === "1" && pidPath && !existsSync(`${pidPath}.crashed`)) {
writeFileSync(`${pidPath}.crashed`, "crashed\n");
- const recoveryPath = process.env.PRIME_AGENT_INTERNAL_OWNED_RECOVERY_DESCRIPTOR;
- if (recoveryPath) {
- writeFileSync(
- recoveryPath,
- `${JSON.stringify({
- version: 1,
- profile: "rpc",
- sessionId: "fixture-session",
- sessionFile: `${pidPath}.jsonl`,
- cwd: process.cwd(),
- updatedAt: new Date().toISOString(),
- })}\n`,
- );
- }
+ writeRecoveryDescriptor();
process.exit(1);
}
return;
diff --git a/packages/coding-agent/test/owned-session-worker-process.test.ts b/packages/coding-agent/test/owned-session-worker-process.test.ts
index 0b661ff631..9d03c02f93 100644
--- a/packages/coding-agent/test/owned-session-worker-process.test.ts
+++ b/packages/coding-agent/test/owned-session-worker-process.test.ts
@@ -119,6 +119,45 @@ async function waitForProcessGone(pid: number): Promise<void> {
throw new Error(`Owned worker ${pid} remained alive`);
}
+const waitForStdoutContains = (frontend: ChildProcess, stdout: () => string, needle: string) =>
+ new Promise<void>((resolve) => {
+ if (stdout().includes(needle)) return resolve();
+ frontend.stdout?.on("data", function check() {
+ if (stdout().includes(needle)) {
+ frontend.stdout?.off("data", check);
+ resolve();
+ }
+ });
+ });
+
+async function spawnRpcFrontend(
+ args: string[] = ["--mode", "rpc"],
+ env: Record<string, string> = {},
+ interactive = false,
+) {
+ const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
+ tempDirs.push(root);
+ const pidPath = join(root, "worker.pid");
+ const frontend = spawnFrontend(args, pidPath, interactive, env);
+ // Absorb EPIPE from writing into a frontend that a regression crashed.
+ frontend.stdin?.on("error", () => {});
+ let stdout = "";
+ frontend.stdout?.on("data", (chunk: Buffer) => {
+ stdout += chunk.toString("utf8");
+ });
+ const workerPid = await waitForWorkerPid(pidPath);
+ return { frontend, workerPid, pidPath, stdout: () => stdout };
+}
+
+const endWithGetState = (frontend: ChildProcess) => {
+ frontend.stdin?.end(`${JSON.stringify({ id: "request-1", type: "get_state" })}\n`);
+};
+
+const uncertainError =
+ "The isolated session worker stopped during this command; its result is uncertain and was not replayed";
+const failedResponse = (id: string) =>
+ `${JSON.stringify({ id, type: "response", command: "get_state", success: false, error: uncertainError })}\n`;
+
describe("owned session worker processes", () => {
it("routes every headless surface through its real worker profile", async () => {
const cases: Array<[string[], string | undefined, string, boolean?]> = [
@@ -150,86 +189,54 @@ describe("owned session worker processes", () => {
});
it("keeps RPC framing in the frontend and exits its worker on stdin EOF", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["--mode", "rpc"], pidPath);
- let stdout = "";
- frontend.stdout?.on("data", (chunk: Buffer) => {
- stdout += chunk.toString("utf8");
- });
+ const { frontend, workerPid, stdout } = await spawnRpcFrontend();
frontend.stdin?.end(`${JSON.stringify({ id: "request-1", type: "get_state" })}\n`);
- const workerPid = await waitForWorkerPid(pidPath);
const exit = await waitForExit(frontend);
children.delete(frontend);
expect(exit).toEqual({ code: 0, signal: null });
- expect(stdout).toBe(
+ expect(stdout()).toBe(
`${JSON.stringify({ id: "request-1", type: "response", command: "get_state", success: true })}\n`,
);
await waitForProcessGone(workerPid);
});
it("correlates overlapping anonymous RPC commands without exposing internal ids", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["--mode", "rpc"], pidPath, false, {
+ const { frontend, workerPid, stdout } = await spawnRpcFrontend(undefined, {
PRIME_AGENT_TEST_REVERSE_RPC_RESPONSES: "1",
});
- let stdout = "";
- frontend.stdout?.on("data", (chunk: Buffer) => {
- stdout += chunk.toString("utf8");
- });
frontend.stdin?.end(
`${JSON.stringify({ type: "get_state", marker: "first" })}\n${JSON.stringify({ type: "get_state", marker: "second" })}\n`,
);
- const workerPid = await waitForWorkerPid(pidPath);
const exit = await waitForExit(frontend);
children.delete(frontend);
expect(exit).toEqual({ code: 0, signal: null });
- expect(stdout).toBe(
+ expect(stdout()).toBe(
`${JSON.stringify({ type: "response", command: "get_state", success: true, marker: "second" })}\n${JSON.stringify({ type: "response", command: "get_state", success: true, marker: "first" })}\n`,
);
await waitForProcessGone(workerPid);
});
it("drops malformed worker output instead of corrupting public RPC JSONL", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["--mode", "rpc"], pidPath, false, {
+ const { frontend, workerPid, stdout } = await spawnRpcFrontend(undefined, {
PRIME_AGENT_TEST_INVALID_RPC_OUTPUT: "1",
});
- let stdout = "";
- frontend.stdout?.on("data", (chunk: Buffer) => {
- stdout += chunk.toString("utf8");
- });
frontend.stdin?.end(`${JSON.stringify({ id: "request-1", type: "get_state" })}\n`);
- const workerPid = await waitForWorkerPid(pidPath);
const exit = await waitForExit(frontend);
children.delete(frontend);
expect(exit).toEqual({ code: 0, signal: null });
- expect(stdout).toBe(
+ expect(stdout()).toBe(
`${JSON.stringify({ id: "request-1", type: "response", command: "get_state", success: true })}\n`,
);
await waitForProcessGone(workerPid);
});
it("does not fabricate a recovery response for response-less acknowledgements", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["--mode", "rpc"], pidPath, false, {
+ const { frontend, workerPid, pidPath, stdout } = await spawnRpcFrontend(undefined, {
PRIME_AGENT_TEST_CRASH_ON_ACK: "1",
});
- let stdout = "";
- frontend.stdout?.on("data", (chunk: Buffer) => {
- stdout += chunk.toString("utf8");
- });
- const workerPid = await waitForWorkerPid(pidPath);
frontend.stdin?.write(`${JSON.stringify({ type: "ack_result", commandId: "command-1" })}\n`);
const replacementPid = await waitForReplacementWorkerPid(pidPath, workerPid);
frontend.stdin?.end(`${JSON.stringify({ id: "request-1", type: "get_state" })}\n`);
@@ -237,88 +244,102 @@ describe("owned session worker processes", () => {
children.delete(frontend);
expect(exit).toEqual({ code: 0, signal: null });
- expect(stdout).toBe(
+ expect(stdout()).toBe(
`${JSON.stringify({ id: "request-1", type: "response", command: "get_state", success: true })}\n`,
);
await waitForProcessGone(workerPid);
await waitForProcessGone(replacementPid);
});
- it("fails pending RPC commands when stdin closes before the worker crashes", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["--mode", "rpc"], pidPath, false, {
- PRIME_AGENT_TEST_CRASH_ON_COMMAND: "get_state",
- });
- let stdout = "";
- frontend.stdout?.on("data", (chunk: Buffer) => {
- stdout += chunk.toString("utf8");
+ it.each([
+ ["crashes mid-command", { PRIME_AGENT_TEST_CRASH_ON_COMMAND: "get_state" }, endWithGetState, "", "request-1"],
+ [
+ "exits successfully without responding",
+ { PRIME_AGENT_TEST_EXIT_ZERO_ON_COMMAND: "get_state" },
+ endWithGetState,
+ "",
+ "request-1",
+ ],
+ ] as Array<[string, Record<string, string>, (frontend: ChildProcess) => void, string, string]>)(
+ "fails pending RPC commands when the worker %s",
+ async (_label, env, drive, prefix, failedId) => {
+ const { frontend, workerPid, stdout } = await spawnRpcFrontend(undefined, env);
+ drive(frontend);
+ const exit = await waitForExit(frontend);
+ children.delete(frontend);
+
+ expect(exit).toEqual({ code: 1, signal: null });
+ expect(stdout()).toBe(prefix + failedResponse(failedId));
+ await waitForProcessGone(workerPid);
+ },
+ );
+
+ it("absorbs a bridge write EPIPE and replays buffered follow-ups exactly once", async () => {
+ const { frontend, workerPid, pidPath, stdout } = await spawnRpcFrontend(undefined, {
+ PRIME_AGENT_TEST_CLOSE_STDIN_ON_COMMAND: "close_stdin",
});
- frontend.stdin?.end(`${JSON.stringify({ id: "request-1", type: "get_state" })}\n`);
- const workerPid = await waitForWorkerPid(pidPath);
+ // The worker acks close_stdin and closes its stdin read end (marked
+ // via the .deaf file): the next bridge write EPIPEs and the frontend
+ // kills the deaf worker so recovery can replay later arrivals.
+ frontend.stdin?.write(`${JSON.stringify({ id: "close-1", type: "close_stdin" })}\n`);
+ await waitForStdoutContains(frontend, stdout, "close_stdin");
+ while (!existsSync(`${pidPath}.deaf`)) await new Promise((r) => setImmediate(r));
+ frontend.stdin?.write(`${JSON.stringify({ type: "ack_result" })}\n`);
+ // The absorbed EPIPE kills the worker with no observable signal, so
+ // yield before the follow-up: after-1 must land in bufferedRpcInput
+ // during the recovery delay, never in a racing write into the dead pipe.
+ await new Promise((resolveDelay) =>
+ // test-policy: allow wall-clock-timer -- no observable exists for the frontend's absorbed EPIPE teardown; this only selects the buffered interleaving
+ setTimeout(resolveDelay, 150),
+ );
+ frontend.stdin?.write(`${JSON.stringify({ id: "after-1", type: "get_state" })}\n`);
+ const replacementPid = await waitForReplacementWorkerPid(pidPath, workerPid);
+ await waitForStdoutContains(frontend, stdout, '"after-1"');
+ frontend.stdin?.end();
const exit = await waitForExit(frontend);
children.delete(frontend);
- expect(exit).toEqual({ code: 1, signal: null });
- expect(stdout).toBe(
- `${JSON.stringify({
- id: "request-1",
- type: "response",
- command: "get_state",
- success: false,
- error: "The isolated session worker stopped during this command; its result is uncertain and was not replayed",
- })}\n`,
+ expect(exit).toEqual({ code: 0, signal: null });
+ expect(stdout()).toBe(
+ `${JSON.stringify({ id: "close-1", type: "response", command: "close_stdin", success: true })}\n${JSON.stringify({ id: "after-1", type: "response", command: "get_state", success: true })}\n`,
);
await waitForProcessGone(workerPid);
+ await waitForProcessGone(replacementPid);
});
- it("fails pending RPC commands when the worker exits successfully without responding", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["--mode", "rpc"], pidPath, false, {
- PRIME_AGENT_TEST_EXIT_ZERO_ON_COMMAND: "get_state",
- });
- let stdout = "";
- frontend.stdout?.on("data", (chunk: Buffer) => {
- stdout += chunk.toString("utf8");
+ it("fails buffered follow-ups when recovery cannot replay them", async () => {
+ const { frontend, workerPid, pidPath, stdout } = await spawnRpcFrontend(undefined, {
+ PRIME_AGENT_TEST_CLOSE_STDIN_ON_COMMAND: "close_stdin",
});
- frontend.stdin?.end(`${JSON.stringify({ id: "request-1", type: "get_state" })}\n`);
- const workerPid = await waitForWorkerPid(pidPath);
+ // Same deaf worker, but the client ends stdin with the follow-up in
+ // flight: it must be failed, not silently dropped.
+ frontend.stdin?.write(`${JSON.stringify({ id: "close-1", type: "close_stdin" })}\n`);
+ await waitForStdoutContains(frontend, stdout, "close_stdin");
+ while (!existsSync(`${pidPath}.deaf`)) await new Promise((r) => setImmediate(r));
+ const epipeChunk = `${JSON.stringify({ type: "ack_result" })}\n${JSON.stringify({ id: "after-1", type: "get_state" })}\n`;
+ frontend.stdin?.end(epipeChunk);
const exit = await waitForExit(frontend);
children.delete(frontend);
+ const ackLine = `${JSON.stringify({ id: "close-1", type: "response", command: "close_stdin", success: true })}\n`;
expect(exit).toEqual({ code: 1, signal: null });
- expect(stdout).toBe(
- `${JSON.stringify({
- id: "request-1",
- type: "response",
- command: "get_state",
- success: false,
- error: "The isolated session worker stopped during this command; its result is uncertain and was not replayed",
- })}\n`,
- );
+ expect(stdout()).toBe(ackLine + failedResponse("after-1"));
await waitForProcessGone(workerPid);
});
it("terminates the owned worker when its frontend is killed", async () => {
- const root = mkdtempSync(join(tmpdir(), "prime-owned-worker-test-"));
- tempDirs.push(root);
- const pidPath = join(root, "worker.pid");
- const frontend = spawnFrontend(["-p", "hello"], pidPath, true);
- const workerPid = await waitForWorkerPid(pidPath);
+ const { frontend, workerPid, pidPath } = await spawnRpcFrontend(["-p", "hello"], {}, true);
const frontendPid = Number(readFileSync(`${pidPath}.frontend`, "utf8").trim());
frontendPids.add(frontendPid);
expect(Number(readFileSync(`${pidPath}.ppid`, "utf8").trim())).toBe(frontendPid);
-
process.kill(frontendPid, "SIGKILL");
await waitForExit(frontend);
children.delete(frontend);
frontendPids.delete(frontendPid);
- const terminationDeadline = Date.now() + 5000;
- while (!existsSync(`${pidPath}.terminated`) && Date.now() < terminationDeadline) {
- await new Promise((resolveDelay) => setTimeout(resolveDelay, 10));
+ // Owner watch: the orphaned worker writes .terminated as it shuts down.
+ // Polling yields via setImmediate (no wall-clock timer; hang = vitest timeout).
+ while (!existsSync(`${pidPath}.terminated`)) {
+ await new Promise((resolveTick) => setImmediate(resolveTick));
}
expect(existsSync(`${pidPath}.terminated`)).toBe(true);
await waitForProcessGone(workerPid);