fix(server): decide a plugin reopen from the same launch the open builds

The open and the refresh now compose the launch with one function. The staleness check compares which plugin provides each tool, so a native-tool catalog bound to a removed plugin reopens, and a proposed launch with an MCP server name collision counts as changed, so the reopen reports it.
This commit is contained in:
Mohamed Boudra
2026-11-25 15:28:33 +01:00
parent 033255a333
commit 8747a78457
3 changed files with 107 additions and 32 deletions
@@ -1579,6 +1579,44 @@ test("a create that fails the MCP check leaves no plugin tools behind for its ag
}
}, 60_000);
test("switching a native tool to an equivalent plugin under another id reopens the session, so its catalog reaches the replacement", async () => {
const h = await setup(true, openCounterSource);
try {
const a = await h.create();
await h.client.disablePlugin("tools-test");
await h.install("replacement", openCounterSource);
await sendAndFinish(h, a.id, "after replacement");
expect(h.launches).toHaveLength(2);
// The replacement answers: it saw the refresh and the reopen.
expect(
await h.launches.at(-1)!.context!.paseoTools!.executeTool("other_opens", {}),
).toMatchObject({ content: [{ type: "text", text: "2" }] });
} finally {
await h.close();
}
}, 60_000);
const mcpOnlySource = `
export default function contribute(server) {
server.addHook("agent.open.before", async ({ request }) => ({ ...request, mcpServers: { ...request.mcpServers, company: ${JSON.stringify(companyServer)} } }));
return () => {};
}
`;
test("a plugin that adds an MCP server name another plugin already uses makes the reopen report the collision", async () => {
const h = await setup(false, mcpOnlySource);
try {
const a = await h.create();
await h.install("duplicate-mcp", mcpOnlySource);
await expect(h.daemon.daemon.agentManager.reloadStalePluginSession(a.id)).rejects.toThrow(
/MCP server company from plugin tools-test collides with plugin duplicate-mcp/,
);
expect(h.launches).toHaveLength(1);
} finally {
await h.close();
}
}, 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 {
@@ -79,14 +79,11 @@ export class SessionAttachments {
}
const base: SessionOpenWireRequest = { ...request, tools: [], mcpServers: {} };
const contributions: Contribution[] = [];
const mcpServers: SessionOpenWireRequest["mcpServers"] = {};
const mcpServerOwners: Record<string, string> = {};
let current = base;
for (const plugin of this.plugins.sessionOpenPlugins()) {
const input: SessionOpenWireRequest = { ...current, tools: [], mcpServers: {} };
try {
const output = await plugin.open(input);
collectMcpServers(mcpServers, mcpServerOwners, plugin.id, output.mcpServers);
const tools = stampTools(plugin, output.tools);
assertNoToolCollision(contributions, plugin.id, tools);
contributions.push({
@@ -106,6 +103,13 @@ export class SessionAttachments {
});
}
}
let launch: PluginSessionOpenResult;
try {
launch = composeLaunch(base, contributions);
} catch (error) {
this.plugins.detachEverywhere(request.agentId);
throw error;
}
if (request.purpose === "interactive") {
this.pending.set(request.agentId, {
base,
@@ -114,12 +118,7 @@ export class SessionAttachments {
stale: false,
});
}
return {
...current,
tools: contributions.flatMap((contribution) => contribution.tools),
mcpServers,
mcpServerOwners,
};
return launch;
});
}
@@ -238,10 +237,7 @@ export class SessionAttachments {
attachment.contributions = contributions;
attachment.stale =
contributions.some((contribution) => contribution.failed) ||
!isDeepStrictEqual(
composedLaunch(attachment.base, attachment.launched),
composedLaunch(attachment.base, contributions),
);
!launchesMatch(attachment.base, attachment.launched, contributions);
return { stale: attachment.stale };
}
@@ -367,22 +363,62 @@ function chainedFields(request: SessionOpenWireRequest) {
return { env: request.env, appendSystemPrompt: request.appendSystemPrompt ?? [] };
}
/** What the provider launches with when the session opens with these contributions, in open order. */
function composedLaunch(base: SessionOpenWireRequest, contributions: readonly Contribution[]) {
/**
* What the provider launches with when the session opens with these contributions, in open order. Throws when two
* plugins return the same MCP server name.
*/
function composeLaunch(
base: SessionOpenWireRequest,
contributions: readonly Contribution[],
): PluginSessionOpenResult {
const mcpServers: SessionOpenWireRequest["mcpServers"] = {};
const mcpServerOwners: Record<string, string> = {};
for (const contribution of contributions) {
collectMcpServers(
mcpServers,
mcpServerOwners,
contribution.pluginId,
contribution.output.mcpServers,
);
}
return {
...chainedFields(contributions.at(-1)?.output ?? base),
mcpServers: Object.assign(
{},
...contributions.map((contribution) => contribution.output.mcpServers),
) as SessionOpenWireRequest["mcpServers"],
tools: contributions.flatMap((contribution) =>
contribution.tools.map(({ name, title, description, inputSchema }) => ({
name,
title,
description,
inputSchema,
})),
),
...(contributions.at(-1)?.output ?? base),
tools: contributions.flatMap((contribution) => contribution.tools),
mcpServers,
mcpServerOwners,
};
}
/**
* Whether the proposed contributions launch the session as it runs now. A tool's plugin counts, since native-tool
* catalogs dispatch to it; its instance does not. A launch the open would reject never matches, so the reopen
* reports it.
*/
function launchesMatch(
base: SessionOpenWireRequest,
launched: readonly Contribution[],
proposed: readonly Contribution[],
): boolean {
let next: PluginSessionOpenResult;
try {
next = composeLaunch(base, proposed);
} catch {
return false;
}
return isDeepStrictEqual(launchContract(composeLaunch(base, launched)), launchContract(next));
}
function launchContract(launch: PluginSessionOpenResult) {
return {
...chainedFields(launch),
mcpServers: launch.mcpServers,
tools: launch.tools.map(({ pluginId, name, title, description, inputSchema }) => ({
pluginId,
name,
title,
description,
inputSchema,
})),
};
}
@@ -423,7 +459,7 @@ function collectMcpServers(
for (const [name, server] of Object.entries(contributions)) {
if (Object.hasOwn(servers, name)) {
throw new Error(
`MCP server ${name} from plugin ${pluginId} collides with plugin ${owners[name]}`,
`Plugin ${pluginId} agent.open.before failed: MCP server ${name} from plugin ${pluginId} collides with plugin ${owners[name]}`,
);
}
servers[name] = server;
+4 -3
View File
@@ -515,8 +515,8 @@ the session continues as if the plugin stopped.
- The tools the callback returns replace its old tools at once, also during a running turn. The new
plugin instance answers the calls.
- If the env, instructions, MCP servers, or tool definitions it returns differ from what the
session launched with, the session reopens before its next idle prompt. A running turn continues without interruption.
- If the env, instructions, MCP servers, tool definitions, or the plugin that provides a tool
differ from what the session launched with, the session reopens before its next idle prompt. A running turn continues without interruption.
A callback whose result depends on `reason` always differs from a `create` open. Reverting the
change before that prompt cancels the reopen.
- If nothing differs, the session keeps running with its background work and subagents. A plugin
@@ -524,7 +524,8 @@ the session continues as if the plugin stopped.
- If the callback throws or times out, its tools stay off, and the session reopens before its next
idle prompt. Paseo does not rerun it for the same plugin instance.
- If a returned tool collides with a Paseo tool or another plugin's tool, that plugin's tools stay
off, and the session reopens before its next idle prompt.
off, and the session reopens before its next idle prompt. The same happens when two plugins return
the same MCP server name. That prompt fails with the collision error.
Agents that receive Paseo tools natively (OpenCode with the bridge, OMP) keep the tool list they
launched with until the reopen. Their calls to a listed tool reach the new instance of the plugin