From 95999361394db78893c19b732f21359b1fcf0fb5 Mon Sep 17 00:00:00 2001 From: Mohamed Boudra Date: Wed, 25 Nov 2026 12:34:06 +0100 Subject: [PATCH] fix(server): make a plugin open the baseline only once its launch takes over A reopen whose plugin hooks succeed can still fail launch validation: a plugin tool that takes a Paseo tool name, or an MCP server on a provider without MCP support. The open had already replaced the session's record, so the session lost its plugin tools, or kept a record for a launch that never ran, and the next idle prompt did not retry. An interactive open now stays pending until registerSession reports that the provider launch took over. A launch that fails before then discards the pending open, and the still-running session keeps its record. Its plugins attach their tools again on the next refresh, and each idle prompt retries the reopen. --- .../src/server/agent/agent-manager.test.ts | 2 + .../server/src/server/agent/agent-manager.ts | 32 ++++++++--- .../server/plugins/agent-tools.e2e.test.ts | 54 +++++++++++++++++++ .../server/src/server/plugins/agent-tools.ts | 9 +++- packages/server/src/server/plugins/index.ts | 8 +++ .../src/server/plugins/session-attachments.ts | 36 ++++++++++--- 6 files changed, 124 insertions(+), 17 deletions(-) diff --git a/packages/server/src/server/agent/agent-manager.test.ts b/packages/server/src/server/agent/agent-manager.test.ts index e5e97519bd..d2bc28c18b 100644 --- a/packages/server/src/server/agent/agent-manager.test.ts +++ b/packages/server/src/server/agent/agent-manager.test.ts @@ -689,6 +689,8 @@ function instructionPluginSessions(instructions: () => string[]): PluginSessions tools: [], mcpServerOwners: {}, }), + sessionLaunched: () => {}, + sessionLaunchFailed: () => {}, refreshSession: async () => ({ stale: false }), sessionTools: () => [], closeSession: () => {}, diff --git a/packages/server/src/server/agent/agent-manager.ts b/packages/server/src/server/agent/agent-manager.ts index d1735f2bc9..511f40934f 100644 --- a/packages/server/src/server/agent/agent-manager.ts +++ b/packages/server/src/server/agent/agent-manager.ts @@ -2049,12 +2049,7 @@ export class AgentManager { }, ); const providerLaunchConfig = this.resolveProviderLaunchConfig(launchConfig, launchContext); - if ( - Object.keys(stripInternalPaseoMcpServer(providerLaunchConfig).mcpServers ?? {}).length > 0 && - existing.session.capabilities.supportsMcpServers !== true - ) { - throw this.unsupportedMcpError(providerLaunchConfig, launchContext); - } + this.assertReloadMcpSupport(existing, providerLaunchConfig, launchContext); let session: AgentSession | undefined; let closedExisting: ManagedAgentClosed | undefined; @@ -2118,7 +2113,8 @@ export class AgentManager { throw error; } finally { if (!handedToRegistration) { - this.detachPluginTools(agentId); + if (closedExisting) this.detachPluginTools(agentId); + else this.pluginLaunchFailed(agentId); if (hadPreviousPaseoToolPolicy) { this.paseoToolPolicies.set(agentId, previousPaseoToolPolicy); } else { @@ -2131,6 +2127,24 @@ export class AgentManager { } } + /** A reload checks MCP support before it closes the running session, which then keeps its plugin tools. */ + private assertReloadMcpSupport( + existing: ActiveManagedAgent, + providerLaunchConfig: AgentSessionConfig, + launchContext: AgentLaunchContext, + ): void { + const mcpServers = stripInternalPaseoMcpServer(providerLaunchConfig).mcpServers ?? {}; + if (Object.keys(mcpServers).length === 0) return; + if (existing.session.capabilities.supportsMcpServers === true) return; + this.pluginLaunchFailed(existing.id); + throw this.unsupportedMcpError(providerLaunchConfig, launchContext); + } + + /** The plugin open for a launch that never took over; the session still running keeps its plugin tools. */ + private pluginLaunchFailed(agentId: string): void { + this.pluginSessions?.sessionLaunchFailed(agentId); + } + private async closeReloadedSession(session: AgentSession, agentId: string): Promise { let operation = this.reloadedSessionCloses.get(session); if (!operation) { @@ -4600,6 +4614,8 @@ export class AgentManager { launchEnv?: Record; }, ): Promise { + // The provider session launched and passed validation, so its plugin open now describes the running session. + this.pluginSessions?.sessionLaunched(agentId); let registered = false; try { this.assertAcceptingAgentRegistrations(); @@ -6593,7 +6609,7 @@ export class AgentManager { }); if (useNativeTools) context.paseoTools = catalog; } catch (error) { - this.detachPluginTools(agentId); + this.pluginLaunchFailed(agentId); throw error; } } 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 bbf60a61e2..1bd3612f1f 100644 --- a/packages/server/src/server/plugins/agent-tools.e2e.test.ts +++ b/packages/server/src/server/plugins/agent-tools.e2e.test.ts @@ -810,6 +810,41 @@ test("a reopen that a broken plugin fails keeps the running session's plugin too } }, 60_000); +const paseoNameClashSource = openCounterSource.replace( + "tools: [...request.tools, tool]", + 'tools: [...request.tools, tool, defineAgentTool({ name: "list_agents", description: "clash", input: z.object({}), async execute() { return { text: "clash" }; } })]', +); + +test("a reopen that a plugin's Paseo tool name fails is retried, keeps the running session's plugin tools, and fixing the plugin restores its own", async () => { + const h = await setup(false, openCounterSource); + try { + await h.install("unrelated", breakableSource(path.join(h.directory, "never-armed"))); + const a = await h.create(); + await writePluginSource(h, "tools-test", paseoNameClashSource); + await h.client.reloadPlugin("tools-test"); + const manager = h.daemon.daemon.agentManager; + const collision = /list_agents from plugin tools-test collides with Paseo tool/; + await expect(manager.reloadStalePluginSession(a.id)).rejects.toThrow(collision); + await expect(manager.reloadStalePluginSession(a.id)).rejects.toThrow(collision); + expect(h.launches).toHaveLength(1); + const broken = await h.mcp(a.id); + expect(await listedToolNames(broken)).not.toContain("other_opens"); + expect(await broken.callTool({ name: "breakable_ping", arguments: {} })).toMatchObject({ + content: [{ type: "text", text: "pong" }], + }); + + await writePluginSource(h, "tools-test", openCounterSource); + await h.client.reloadPlugin("tools-test"); + expect( + await (await h.mcp(a.id)).callTool({ name: "other_opens", arguments: {} }), + ).toMatchObject({ content: [{ type: "text", text: "1" }] }); + await manager.reloadStalePluginSession(a.id); + expect(h.launches).toHaveLength(1); + } finally { + await h.close(); + } +}, 60_000); + const impostorSource = ` import { z } from "zod"; import { defineAgentTool } from "@getpaseo/plugin/server"; @@ -1390,6 +1425,25 @@ test("session-open MCP servers fail on unsupported providers and leave saved con } }, 60_000); +test("a reopen that fails the MCP check is retried, and the running session keeps its plugin tools", async () => { + const h = await setup(false, mcpSource, undefined, { supportsMcpServers: false }); + try { + await h.client.disablePlugin("tools-test"); + await h.install("unrelated", breakableSource(path.join(h.directory, "never-armed"))); + const a = await h.create(); + await h.client.enablePlugin("tools-test"); + const manager = h.daemon.daemon.agentManager; + await expect(manager.reloadStalePluginSession(a.id)).rejects.toThrow(/does not support MCP/); + await expect(manager.reloadStalePluginSession(a.id)).rejects.toThrow(/does not support MCP/); + expect(h.launches).toHaveLength(1); + expect( + await (await h.mcp(a.id)).callTool({ name: "breakable_ping", arguments: {} }), + ).toMatchObject({ content: [{ type: "text", text: "pong" }] }); + } finally { + await h.close(); + } +}, 60_000); + test("history opens ignore session-open MCP servers", async () => { const h = await setup(true, mcpSource); try { diff --git a/packages/server/src/server/plugins/agent-tools.ts b/packages/server/src/server/plugins/agent-tools.ts index 1ff764888f..17eee51810 100644 --- a/packages/server/src/server/plugins/agent-tools.ts +++ b/packages/server/src/server/plugins/agent-tools.ts @@ -39,8 +39,15 @@ export interface PluginSessionRefreshOptions { /** What the agent manager asks of plugins about a session: its open, its attached tools, and their calls. */ export interface PluginSessionsRuntime { - /** Runs every `agent.open.before` callback for a session open and records what each plugin attached. */ + /** + * Runs every `agent.open.before` callback for a session open and records what each plugin attached. The result + * replaces the running session's record once `sessionLaunched` reports that its provider launch took over. + */ openSession(request: SessionOpenWireRequest): Promise; + /** The provider session launched with the last open's result. */ + sessionLaunched(agentId: string): void; + /** The last open's result did not launch; the session still running keeps its tools. */ + sessionLaunchFailed(agentId: string): void; /** * Reruns the open of only the plugins that changed since the session opened, swaps in their tools, and reports * whether the session must reopen to apply the rest. diff --git a/packages/server/src/server/plugins/index.ts b/packages/server/src/server/plugins/index.ts index 9c13fa8dd4..451329e77b 100644 --- a/packages/server/src/server/plugins/index.ts +++ b/packages/server/src/server/plugins/index.ts @@ -157,6 +157,14 @@ export class PluginService { return { ...request, tools: [], mcpServers: {}, mcpServerOwners: {} }; }; + readonly sessionLaunched: PluginSessionsRuntime["sessionLaunched"] = (agentId) => { + this.runtime.sessionAttachments?.launched(agentId); + }; + + readonly sessionLaunchFailed: PluginSessionsRuntime["sessionLaunchFailed"] = (agentId) => { + this.runtime.sessionAttachments?.launchFailed(agentId); + }; + readonly refreshSession: PluginSessionsRuntime["refreshSession"] = async (agentId, options) => { return (await this.runtime.sessionAttachments?.refresh(agentId, options)) ?? { stale: false }; }; diff --git a/packages/server/src/server/plugins/session-attachments.ts b/packages/server/src/server/plugins/session-attachments.ts index fb75b4421f..0a2a6ffff0 100644 --- a/packages/server/src/server/plugins/session-attachments.ts +++ b/packages/server/src/server/plugins/session-attachments.ts @@ -53,11 +53,13 @@ interface Attachment { * * A plugin restart gives it a new instance id. `refresh` reruns the open of only the plugins that changed for the * session, swaps in their tools at once, and marks the session stale only when the provider launch would differ from - * the one it runs. A failed open leaves the running launch as the baseline. Opens and refreshes of one session run one - * at a time. + * the one it runs. An open becomes the baseline only once its provider launch takes over the session, so a failed open + * or launch leaves the running launch as the baseline. Opens and refreshes of one session run one at a time. */ export class SessionAttachments { private readonly attachments = new Map(); + /** Interactive opens whose provider launch has not taken over the session yet. */ + private readonly pending = new Map(); private readonly tails = new Map>(); constructor( @@ -67,8 +69,9 @@ export class SessionAttachments { async open(request: SessionOpenWireRequest): Promise { return this.serialize(request.agentId, async () => { - // Each plugin attaches its tools again below, and a plugin that left the chain keeps none. A failed open leaves - // the running session's record, whose plugins then attach again on its next refresh. + // Each plugin attaches its tools again below, and a plugin that left the chain keeps none. Until the launch + // takes over, the running session's record stays, and its plugins attach again on its next refresh if the + // launch fails. this.plugins.detachEverywhere(request.agentId); for (const contribution of this.attachments.get(request.agentId)?.contributions ?? []) { contribution.synced = false; @@ -103,7 +106,7 @@ export class SessionAttachments { } } if (request.purpose === "interactive") { - this.attachments.set(request.agentId, { + this.pending.set(request.agentId, { base, launched: contributions, contributions: [...contributions], @@ -128,17 +131,33 @@ export class SessionAttachments { options: PluginSessionRefreshOptions, ): Promise<{ stale: boolean }> { const attachment = this.attachments.get(agentId); - if (!attachment) return { stale: false }; + // A launch in progress attaches its own tools and takes over once it succeeds. + if (!attachment || this.pending.has(agentId)) return { stale: false }; if (this.isCurrent(attachment)) return { stale: attachment.stale }; return this.serialize(agentId, () => this.reconcile(agentId, options)); } + /** The provider session launched with the last open's result and now runs it. */ + launched(agentId: string): void { + const opened = this.pending.get(agentId); + if (!opened) return; + this.pending.delete(agentId); + this.attachments.set(agentId, opened); + } + + /** The last open's result never launched. The session that is still running keeps its record. */ + launchFailed(agentId: string): void { + this.pending.delete(agentId); + this.plugins.detachEverywhere(agentId); + } + /** The session's tools from running plugin instances. */ tools(agentId: string): AttachedAgentTool[] { const running = new Map( this.plugins.sessionOpenPlugins().map((plugin) => [plugin.id, plugin.instanceId]), ); - return (this.attachments.get(agentId)?.contributions ?? []) + const attachment = this.pending.get(agentId) ?? this.attachments.get(agentId); + return (attachment?.contributions ?? []) .filter( (contribution) => contribution.synced && @@ -148,6 +167,7 @@ export class SessionAttachments { } close(agentId: string): void { + this.pending.delete(agentId); this.attachments.delete(agentId); this.plugins.detachEverywhere(agentId); } @@ -172,7 +192,7 @@ export class SessionAttachments { options: PluginSessionRefreshOptions, ): Promise<{ stale: boolean }> { const attachment = this.attachments.get(agentId); - if (!attachment) return { stale: false }; + if (!attachment || this.pending.has(agentId)) return { stale: false }; if (this.isCurrent(attachment)) return { stale: attachment.stale }; const running = new Map(this.plugins.sessionOpenPlugins().map((plugin) => [plugin.id, plugin])); const recorded = new Map(