diff --git a/packages/server/src/server/agent/agent-manager.test.ts b/packages/server/src/server/agent/agent-manager.test.ts index e6d374999c..81a8ecedcc 100644 --- a/packages/server/src/server/agent/agent-manager.test.ts +++ b/packages/server/src/server/agent/agent-manager.test.ts @@ -1,4 +1,4 @@ -import type { PluginSessionsRuntime } from "../plugins/agent-tools.js"; +import type { AttachedAgentTool, PluginSessionsRuntime } from "../plugins/agent-tools.js"; import { createIdleRuntimeCleanup } from "./idle-runtime-cleanup/index.js"; import { prepareAgentMessage, @@ -16317,49 +16317,71 @@ class LaunchWrapRecordingClient extends TestAgentClient { } } +type TestLaunchWrap = [string, ...string[]]; + +/** + * A session-open plugin that selects `wrap()` and attaches one tool while it does. Like the real runtime, an open's + * tools take over only once its launch does, and a failed launch leaves the running session's tools. + */ +function launchWrapPluginSessions(wrap: () => TestLaunchWrap | undefined) { + const pending = new Map(); + const running = new Map(); + const failedLaunches: string[] = []; + const sessions: PluginSessionsRuntime = { + openSession: async (request) => { + const selected = wrap(); + const tools: AttachedAgentTool[] = selected + ? [ + { + name: "sandbox_status", + description: "Report the sandbox", + inputSchema: {}, + pluginId: "sandbox", + pluginInstanceId: "sandbox-1", + }, + ] + : []; + if (request.purpose === "interactive") pending.set(request.agentId, tools); + return { ...request, ...(selected ? { wrap: selected } : {}), tools, mcpServerOwners: {} }; + }, + sessionLaunched: (agentId) => { + const tools = pending.get(agentId); + pending.delete(agentId); + if (tools) running.set(agentId, tools); + }, + sessionLaunchFailed: (agentId) => { + pending.delete(agentId); + failedLaunches.push(agentId); + }, + refreshSession: async () => ({ stale: false }), + sessionTools: (agentId) => running.get(agentId) ?? [], + closeSession: (agentId) => { + pending.delete(agentId); + running.delete(agentId); + }, + callTool: async () => ({ isError: true, text: "No plugin tools" }), + }; + return { sessions, failedLaunches }; +} + function createLaunchWrapManager(client: LaunchWrapRecordingClient) { const workdir = mkdtempSync(join(tmpdir(), "agent-launch-wrap-")); onTestFinished(() => rmSync(workdir, { recursive: true, force: true })); const storage = new AgentStorage(join(workdir, "agents"), logger); - const detached: string[] = []; - const hook: { wrap: [string, ...string[]] | undefined } = { wrap: undefined }; + const hook: { wrap: TestLaunchWrap | undefined } = { wrap: undefined }; + const { sessions, failedLaunches } = launchWrapPluginSessions(() => hook.wrap); const manager = new AgentManager({ clients: { codex: client }, registry: storage, logger, - pluginAgentTools: { - isToolPluginAvailable: () => true, - detachTools: (agentId) => detached.push(agentId), - callTool: async () => { - throw new Error("unused"); - }, - }, - pluginLifecycle: { - emit: () => {}, - before: async (name, request) => - name === "agent.open.before" && hook.wrap - ? { - ...request, - wrap: hook.wrap, - tools: [ - { - name: "sandbox_status", - description: "Report the sandbox", - inputSchema: {}, - pluginId: "sandbox", - pluginInstanceId: "sandbox-1", - }, - ], - } - : request, - }, + pluginSessions: sessions, }); - return { manager, storage, workdir, detached, hook }; + return { manager, storage, workdir, failedLaunches, hook }; } test("a wrapped open rejects on every path for a provider that does not apply wraps", async () => { const client = new LaunchWrapRecordingClient(false); - const { manager, storage, workdir, detached, hook } = createLaunchWrapManager(client); + const { manager, storage, workdir, failedLaunches, hook } = createLaunchWrapManager(client); const created = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, { workspaceId: undefined, }); @@ -16387,9 +16409,10 @@ test("a wrapped open rejects on every path for a provider that does not apply wr expect(client.calls).toEqual(calls); expect(existing.closed).toBe(false); expect(manager.getAgent(created.id)?.session).toBe(existing); - // The hook attached a tool for this open; the rejection takes it away again. + // Each refused open is discarded, so the running session keeps the tools it launched with. + expect(failedLaunches).toHaveLength(4); + expect(failedLaunches.at(-1)).toBe(created.id); expect(manager.getPluginTools(created.id)).toEqual([]); - expect(detached.filter((id) => id === created.id).length).toBeGreaterThanOrEqual(2); await manager.flush(); expect(JSON.stringify(await storage.get(created.id))).not.toContain("/opt/sandbox"); await manager.closeAgent(created.id); @@ -16460,13 +16483,7 @@ test("a wrapped refresh passes a Windows script provider command to the wrapper const manager = new AgentManager({ clients: { codex: client }, logger, - pluginLifecycle: { - emit: () => {}, - before: async (name, request) => - name === "agent.open.before" - ? { ...request, wrap: [process.execPath, launcher, launches] } - : request, - }, + pluginSessions: launchWrapPluginSessions(() => [process.execPath, launcher, launches]).sessions, }); const created = await manager.createAgent({ provider: "codex", cwd: workdir }, undefined, { workspaceId: undefined, 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 0ad42627fe..4571d1b993 100644 --- a/packages/server/src/server/plugins/agent-tools.e2e.test.ts +++ b/packages/server/src/server/plugins/agent-tools.e2e.test.ts @@ -46,6 +46,7 @@ function recordingProvider(native: boolean, callbacks: TestAgentClientOptions = const provider: AgentClient = { provider: inner.provider, capabilities: { ...inner.capabilities, supportsNativePaseoTools: native }, + supportsLaunchWrap: true, createSession: async (config, context, options) => { launches.push({ config, context }); return inner.createSession(config, context, options); @@ -587,6 +588,44 @@ test("an idle agent reopens once before its next prompt after a session-hook plu } }, 60_000); +function wrapPluginSource(profile: string) { + return ` +export default function contribute(server) { + server.addHook("agent.open.before", async ({ request }) => ({ + ...request, + wrap: [${JSON.stringify(process.execPath)}, "--sandbox-profile", ${JSON.stringify(profile)}], + })); + return () => {}; +} +`; +} + +test("a plugin reload that changes the launch wrap reopens the idle session with the new wrap", async () => { + const h = await setup(false, wrapPluginSource("first")); + try { + const a = await h.create(); + expect(h.launches.at(-1)!.context?.wrap).toEqual([ + process.execPath, + "--sandbox-profile", + "first", + ]); + await writeFile( + path.join(h.directory, "tools-test", "index.server.ts"), + wrapPluginSource("second"), + ); + await h.client.reloadPlugin("tools-test"); + await sendAndFinish(h, a.id, "after reload"); + expect(h.launches).toHaveLength(2); + expect(h.launches.at(-1)!.context?.wrap).toEqual([ + process.execPath, + "--sandbox-profile", + "second", + ]); + } finally { + await h.close(); + } +}, 60_000); + test("a busy agent is steered after plugin reload and reopens only at its next idle prompt", async () => { let release!: () => void; const blocked = new Promise((resolve) => { diff --git a/packages/server/src/server/plugins/session-attachments.ts b/packages/server/src/server/plugins/session-attachments.ts index 39a7620944..eb3c79b45b 100644 --- a/packages/server/src/server/plugins/session-attachments.ts +++ b/packages/server/src/server/plugins/session-attachments.ts @@ -358,9 +358,16 @@ function isReusable( ); } -/** The fields one plugin's callback passes on to the next. Tools and MCP servers are collected per plugin instead. */ +/** + * The fields one plugin's callback passes on to the next, including the launch wrap an earlier plugin selected. Tools + * and MCP servers are collected per plugin instead. + */ function chainedFields(request: SessionOpenWireRequest) { - return { env: request.env, appendSystemPrompt: request.appendSystemPrompt ?? [] }; + return { + env: request.env, + appendSystemPrompt: request.appendSystemPrompt ?? [], + wrap: request.wrap ?? null, + }; } /**