Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): let an ACP agent exit on its own when its client is disposed

Elian Doran3847685d2e54
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 882cbd4ec7..61f0b0cc1e 100644
--- a/apps/server/src/services/llm/providers/acp_client.spec.ts
+++ b/apps/server/src/services/llm/providers/acp_client.spec.ts
@@ -226,15 +226,31 @@ describe("AcpClient", () => {
.rejects.toThrow(/Failed to start the ACP agent/);
});
- it("does not treat a post-dispose exit as an unexpected failure", async () => {
+ it("closes the agent's stdin on dispose and does not treat its exit as an unexpected failure", async () => {
+ vi.useFakeTimers();
const client = AcpClient.start("/bin/copilot", { cwd: "/tmp" });
client.dispose();
- expect(proc.killed).toBe(true);
+ expect(proc.stdin.writableEnded).toBe(true);
+
+ // An agent that exits on end of input is never signalled.
+ proc.emit("exit", 0, null);
+ vi.advanceTimersByTime(60_000);
+ expect(proc.killed).toBe(false);
// A request after disposal fails with the disposal error, not a crash.
await expect(client.request("initialize", {})).rejects.toThrow(/disposed/);
});
+ it("kills an agent that is still running after the grace period", () => {
+ vi.useFakeTimers();
+ const client = AcpClient.start("/bin/copilot", { cwd: "/tmp" });
+ client.dispose();
+ vi.advanceTimersByTime(4_999);
+ expect(proc.killed).toBe(false);
+ vi.advanceTimersByTime(1);
+ expect(proc.killed).toBe(true);
+ });
+
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 a759993c28..5e88a009b5 100644
--- a/apps/server/src/services/llm/providers/acp_client.ts
+++ b/apps/server/src/services/llm/providers/acp_client.ts
@@ -13,6 +13,9 @@ import { getLog } from "@triliumnext/core";
import { type ChildProcessWithoutNullStreams, spawn } from "child_process";
import { createInterface } from "readline";
+/** How long a disposed agent has to exit on its own before it is killed. */
+const DISPOSE_GRACE_MS = 5_000;
+
interface JsonRpcMessage {
jsonrpc: "2.0";
id?: number | string;
@@ -63,6 +66,7 @@ export class AcpClient {
private readonly pending = new Map<number, { resolve: (msg: JsonRpcMessage) => void; reject: (err: Error) => void }>();
private exitError: Error | undefined;
private disposed = false;
+ private exited = false;
private constructor(
private readonly proc: ChildProcessWithoutNullStreams,
@@ -87,7 +91,8 @@ export class AcpClient {
proc.on("error", err => this.failAll(new Error(`Failed to start the ACP agent: ${err.message}`)));
proc.on("exit", (code, sig) => {
- // A deliberate dispose() kills the subprocess — that exit is expected
+ this.exited = true;
+ // A deliberate dispose() ends the subprocess — that exit is expected
// and must not surface as an error for in-flight (cancelled) requests.
if (!this.disposed) {
this.failAll(new Error(`The ACP agent exited unexpectedly (${sig ?? `code ${code}`}).`));
@@ -151,14 +156,26 @@ export class AcpClient {
this.send({ jsonrpc: "2.0", method, params });
}
- /** Kill the subprocess and reject anything still in flight. */
+ /**
+ * Reject anything still in flight and end the subprocess: close its stdin so
+ * it can exit on its own, and kill it only if it is still running after
+ * {@link DISPOSE_GRACE_MS}. `agy_acp_server` answers SIGTERM with a crash
+ * report of some eighty lines on stderr, which would land in the log once per
+ * chat turn.
+ */
dispose(): void {
if (this.disposed) {
return;
}
this.disposed = true;
this.failAll(new Error("The ACP client was disposed."));
- this.proc.kill();
+ this.proc.stdin.end();
+ const killTimer = setTimeout(() => {
+ if (!this.exited) {
+ this.proc.kill();
+ }
+ }, DISPOSE_GRACE_MS);
+ killTimer.unref();
}
private send(message: JsonRpcMessage): void {