Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

fix(daemon): never escalate on an agent root that already exited

Marcelito0a45652274f5
diff --git a/packages/daemon/src/supervisor.ts b/packages/daemon/src/supervisor.ts
index f30fc4e..2c200b3 100644
--- a/packages/daemon/src/supervisor.ts
+++ b/packages/daemon/src/supervisor.ts
@@ -265,18 +265,19 @@ export class AgentSupervisor {
};
const onClose = () => finish();
let forced = false;
+ // A root that already exited may still be current until its children release the pipes,
+ // but its PID is free then and may be reused: never escalate on it.
+ const running = () => this.child === child && child.exitCode === null && child.signalCode === null;
const escalate = (force: boolean) => {
- if (this.child !== child || (force && forced)) return;
+ if (!running() || (force && forced)) return;
if (force) forced = true;
- void terminate(child, force, { platform, isCurrent: () => this.child === child }).catch(
- (error: Error) => {
- errors.push(error.message);
- this.log(error.message);
- // taskkill /T without /F cannot end a windowless console process and fails at once:
- // force the tree now instead of waiting out the hard window.
- if (!force && platform === "win32") escalate(true);
- },
- );
+ void terminate(child, force, { platform, isCurrent: running }).catch((error: Error) => {
+ errors.push(error.message);
+ this.log(error.message);
+ // taskkill /T without /F cannot end a windowless console process and fails at once:
+ // force the tree now instead of waiting out the hard window.
+ if (!force && platform === "win32") escalate(true);
+ });
};
this.closeWaiters.push(onClose);
child.stdin?.end();
diff --git a/packages/daemon/test/supervisor.test.ts b/packages/daemon/test/supervisor.test.ts
index e726fac..9620276 100644
--- a/packages/daemon/test/supervisor.test.ts
+++ b/packages/daemon/test/supervisor.test.ts
@@ -402,6 +402,48 @@ describe("AgentSupervisor", () => {
}
});
+ it("never escalates on a root that already exited while its children still hold the pipes", async () => {
+ // The engine exits once its stdin closes, but close waits for children holding the inherited
+ // pipes: its PID is already free then, and Windows may reuse it.
+ const agent = `
+const { spawn } = require("node:child_process");
+spawn(process.execPath, ["-e", "setTimeout(() => {}, 1500)"], { stdio: "inherit" });
+process.stdin.setEncoding("utf8");
+let buffer = "";
+process.stdin.on("data", (chunk) => {
+ buffer += chunk;
+ for (;;) {
+ const newline = buffer.indexOf("\\n");
+ if (newline < 0) break;
+ const record = JSON.parse(buffer.slice(0, newline));
+ buffer = buffer.slice(newline + 1);
+ if (record.type === "get_state")
+ process.stdout.write(JSON.stringify({ type: "response", id: record.id, command: "get_state", success: true, data: {} }) + "\\n");
+ }
+});
+process.stdin.on("end", () => process.exit(0));
+`;
+ const escalations: boolean[] = [];
+ const { supervisor } = track(
+ fakeSupervisor({
+ platform: "win32",
+ command: process.execPath,
+ args: ["-e", agent, "--"],
+ stopTimeoutMs: 200,
+ hardStopTimeoutMs: 200,
+ terminationFailureTimeoutMs: 5000,
+ terminateProcessTree: async (_child, force) => {
+ escalations.push(force);
+ return true;
+ },
+ }),
+ );
+ await supervisor.start();
+ await supervisor.stop();
+ expect(supervisor.state).toBe("stopped");
+ expect(escalations).toEqual([]);
+ });
+
it("keeps both Windows default stages bounded without rejecting before taskkill can finish", async () => {
const escalations: boolean[] = [];
const { supervisor } = track(