From 39d203248fe79be24c9156ec83f72bf93ba6fd4a Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 15:47:30 +0100 Subject: [PATCH] fix(plugins): let a refreshed plugin's running tool calls finish The plugin process detached the session's tools at the start of every agent.open.before, which aborted calls already running. Attaching replaces the definitions for later calls; the daemon still detaches before an open that replaces the provider session. --- .../server/plugins/agent-tools.e2e.test.ts | 49 +++++++++++++++++++ .../src/server/plugins/lifecycle/index.ts | 3 +- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/server/src/server/plugins/agent-tools.e2e.test.ts b/packages/server/src/server/plugins/agent-tools.e2e.test.ts index f918877edd..0ad42627fe 100644 --- a/packages/server/src/server/plugins/agent-tools.e2e.test.ts +++ b/packages/server/src/server/plugins/agent-tools.e2e.test.ts @@ -981,6 +981,55 @@ test("a later plugin rerun by an earlier plugin's edit keeps its own tool, and t } }, 60_000); +// The tool waits for the gate file while it answers the first hint. +const slowHintReaderSource = (entered: string, gate: string) => ` +import { existsSync, writeFileSync } from "node:fs"; +import { z } from "zod"; +import { defineAgentTool } from "@getpaseo/plugin/server"; +export default function contribute(server) { + server.addHook("agent.open.before", async ({ request }) => { + const { HINT: hint, ...env } = request.env; + const tool = defineAgentTool({ + name: "read_tool_hint", description: "The hint from the plugin before", + input: z.object({}), + async execute() { + if (hint === "red") { + writeFileSync(${JSON.stringify(entered)}, ""); + while (!existsSync(${JSON.stringify(gate)})) await new Promise((r) => setTimeout(r, 20)); + } + return { text: String(hint) }; + }, + }); + return { ...request, env, tools: [...request.tools, tool] }; + }); + return () => {}; +} +`; + +test("a refresh that reruns a plugin lets that plugin's running tool call finish", async () => { + const scratch = await mkdtemp(path.join(tmpdir(), "paseo-running-call-")); + const entered = path.join(scratch, "entered"); + const gate = path.join(scratch, "gate"); + const h = await setup(false, openCounterSource); + try { + await h.install("a-hint", hintSource("red")); + await h.install("b-hint-reader", slowHintReaderSource(entered, gate)); + const a = await h.create(); + const running = (await h.mcp(a.id)).callTool({ name: "read_tool_hint", arguments: {} }); + await expect.poll(() => existsSync(entered)).toBe(true); + await writePluginSource(h, "a-hint", hintSource("green")); + await h.client.reloadPlugin("a-hint"); + expect( + await (await h.mcp(a.id)).callTool({ name: "read_tool_hint", arguments: {} }), + ).toMatchObject({ content: [{ type: "text", text: "green" }] }); + await writeFile(gate, ""); + expect(await running).toMatchObject({ content: [{ type: "text", text: "red" }] }); + } finally { + await h.close(); + await rm(scratch, { recursive: true, force: true }); + } +}, 60_000); + // Once armed, the module waits for the gate file before it loads, which holds a restart between stop and start. const gatedStartSource = (arm: string, entered: string, gate: string) => openCounterSource.replace( diff --git a/packages/server/src/server/plugins/lifecycle/index.ts b/packages/server/src/server/plugins/lifecycle/index.ts index b5c0d49dbb..9b138335b3 100644 --- a/packages/server/src/server/plugins/lifecycle/index.ts +++ b/packages/server/src/server/plugins/lifecycle/index.ts @@ -391,7 +391,8 @@ export class PluginHookHandlers implements PluginLifecycleRegistration { context: PluginHookContext, ): Promise { let request = validateBeforeRequest("agent.open.before", input); - this.agentTools?.detach(request.agentId); + // Attaching replaces the session's definitions for later calls; calls already running finish. The daemon detaches + // the session before an open that replaces the provider session. let tools: AgentToolDefinition[] = []; for (const handler of this.handlers.get("agent.open.before") ?? []) { context.signal.throwIfAborted();