Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): end an ACP agent's whole process tree on Windows

Elian Doran0be584305252
diff --git a/apps/server/src/services/llm/providers/acp_client.spec.ts b/apps/server/src/services/llm/providers/acp_client.spec.ts
index 61f0b0cc1e..8bb53b0e2c 100644
--- a/apps/server/src/services/llm/providers/acp_client.spec.ts
+++ b/apps/server/src/services/llm/providers/acp_client.spec.ts
@@ -12,6 +12,7 @@ class FakeProc extends EventEmitter {
stdout = new PassThrough();
stderr = new PassThrough();
killed = false;
+ pid = 4242;
written: string[] = [];
constructor() {
@@ -39,6 +40,12 @@ function lastWritten(proc: FakeProc): Record<string, unknown> {
return JSON.parse(proc.written[proc.written.length - 1]);
}
+const originalPlatform = Object.getOwnPropertyDescriptor(process, "platform");
+
+function stubPlatform(platform: NodeJS.Platform) {
+ Object.defineProperty(process, "platform", { ...originalPlatform, value: platform });
+}
+
describe("AcpClient", () => {
let proc: FakeProc;
@@ -51,6 +58,9 @@ describe("AcpClient", () => {
afterEach(() => {
vi.useRealTimers();
+ if (originalPlatform) {
+ Object.defineProperty(process, "platform", originalPlatform);
+ }
});
it("spawns with exactly the given args and the process environment", () => {
@@ -242,6 +252,7 @@ describe("AcpClient", () => {
});
it("kills an agent that is still running after the grace period", () => {
+ stubPlatform("linux");
vi.useFakeTimers();
const client = AcpClient.start("/bin/copilot", { cwd: "/tmp" });
client.dispose();
@@ -251,6 +262,16 @@ describe("AcpClient", () => {
expect(proc.killed).toBe(true);
});
+ it("kills the agent's whole process tree on Windows, where it runs in a child of the spawned process", () => {
+ stubPlatform("win32");
+ vi.useFakeTimers();
+ const client = AcpClient.start("C:\\agy\\agy_acp_server.exe", { cwd: "C:\\tmp" });
+ client.dispose();
+ vi.advanceTimersByTime(5_000);
+ expect(spawnMock).toHaveBeenLastCalledWith("taskkill", [ "/pid", "4242", "/T", "/F" ], expect.objectContaining({ windowsHide: true }));
+ expect(proc.killed).toBe(false);
+ });
+
it("rejects in-flight requests on dispose and ignores a second dispose", async () => {
const client = AcpClient.start("/bin/copilot", { cwd: "/tmp" });
const promise = client.request("session/prompt", {});
diff --git a/apps/server/src/services/llm/providers/acp_client.ts b/apps/server/src/services/llm/providers/acp_client.ts
index 162c705507..1c47c1fb74 100644
--- a/apps/server/src/services/llm/providers/acp_client.ts
+++ b/apps/server/src/services/llm/providers/acp_client.ts
@@ -171,12 +171,27 @@ export class AcpClient {
this.proc.stdin.end();
const killTimer = setTimeout(() => {
if (!this.exited) {
- this.proc.kill();
+ this.kill();
}
}, DISPOSE_GRACE_MS);
killTimer.unref();
}
+ /**
+ * End the agent and every process it started. On Windows `proc.kill()`
+ * ends only the spawned process, while the agent can run in a child of it:
+ * the PyInstaller build of `agy_acp_server` starts its server that way, and
+ * a `.cmd` shim runs the CLI under `cmd.exe`. `taskkill /T` ends the tree.
+ */
+ private kill(): void {
+ if (process.platform === "win32" && this.proc.pid !== undefined) {
+ const taskkill = spawn("taskkill", [ "/pid", String(this.proc.pid), "/T", "/F" ], { stdio: "ignore", windowsHide: true });
+ taskkill.on("error", err => getLog().error(`Failed to end the ACP agent's process tree: ${err.message}`));
+ return;
+ }
+ this.proc.kill();
+ }
+
private send(message: JsonRpcMessage): void {
this.proc.stdin.write(`${JSON.stringify(message)}\n`);
}