fix(server): discard a reload's plugin open on every pre-takeover rejection

resolveProviderLaunchConfig rejects a plugin MCP server that takes a saved
server name after the open is staged, and nothing discarded the staged open.
The reload now resolves and validates its launch config in one scope that
discards the staged open on any rejection, so the running session keeps its
plugin tools and the next idle prompt retries the reopen.
This commit is contained in:
Mohamed Boudra
2026-11-25 12:44:50 +01:00
parent 9599936139
commit 43f961e25c
2 changed files with 62 additions and 11 deletions
@@ -2048,8 +2048,11 @@ export class AgentManager {
metadata: existing.metadata,
},
);
const providerLaunchConfig = this.resolveProviderLaunchConfig(launchConfig, launchContext);
this.assertReloadMcpSupport(existing, providerLaunchConfig, launchContext);
const providerLaunchConfig = this.resolveReloadLaunchConfig(
existing,
launchConfig,
launchContext,
);
let session: AgentSession | undefined;
let closedExisting: ManagedAgentClosed | undefined;
@@ -2127,17 +2130,29 @@ export class AgentManager {
}
}
/** A reload checks MCP support before it closes the running session, which then keeps its plugin tools. */
private assertReloadMcpSupport(
/**
* Validates a reload's launch before it closes the running session. A rejected launch never takes over, so the
* running session keeps its plugin tools.
*/
private resolveReloadLaunchConfig(
existing: ActiveManagedAgent,
providerLaunchConfig: AgentSessionConfig,
launchConfig: 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);
): AgentSessionConfig {
try {
const providerLaunchConfig = this.resolveProviderLaunchConfig(launchConfig, launchContext);
const mcpServers = stripInternalPaseoMcpServer(providerLaunchConfig).mcpServers ?? {};
if (
Object.keys(mcpServers).length > 0 &&
existing.session.capabilities.supportsMcpServers !== true
) {
throw this.unsupportedMcpError(providerLaunchConfig, launchContext);
}
return providerLaunchConfig;
} catch (error) {
this.pluginLaunchFailed(existing.id);
throw error;
}
}
/** The plugin open for a launch that never took over; the session still running keeps its plugin tools. */
@@ -1444,6 +1444,42 @@ test("a reopen that fails the MCP check is retried, and the running session keep
}
}, 60_000);
test("a reopen whose plugin MCP server takes a saved server name is retried, and reverting the plugin restores its tools", async () => {
const h = await setup(false, openCounterSource);
try {
const manager = h.daemon.daemon.agentManager;
const a = await manager.createAgent(
{
provider: "codex",
cwd: h.directory,
mcpServers: { company: companyServer },
},
undefined,
{},
);
await writePluginSource(
h,
"tools-test",
openCounterSource.replace(
"return { ...request, tools: [...request.tools, tool] };",
`return { ...request, tools: [...request.tools, tool], mcpServers: { company: ${JSON.stringify(companyServer)} } };`,
),
);
await h.client.reloadPlugin("tools-test");
const collision = /MCP server 'company' from plugin tools-test collides with agent saved/;
await expect(manager.reloadStalePluginSession(a.id)).rejects.toThrow(collision);
await expect(manager.reloadStalePluginSession(a.id)).rejects.toThrow(collision);
await writePluginSource(h, "tools-test", openCounterSource);
await h.client.reloadPlugin("tools-test");
expect(await listedToolNames(await h.mcp(a.id))).toContain("other_opens");
await manager.reloadStalePluginSession(a.id);
expect(h.launches).toHaveLength(1);
} finally {
await h.close();
}
}, 60_000);
test("history opens ignore session-open MCP servers", async () => {
const h = await setup(true, mcpSource);
try {