diff --git a/packages/server/src/server/agent/agent-manager.ts b/packages/server/src/server/agent/agent-manager.ts index d3421dc2ee..5406fde928 100644 --- a/packages/server/src/server/agent/agent-manager.ts +++ b/packages/server/src/server/agent/agent-manager.ts @@ -1,4 +1,8 @@ -import type { AttachedAgentTool, PluginSessionsRuntime } from "../plugins/agent-tools.js"; +import type { + AttachedAgentTool, + PluginSessionsRuntime, + SessionOpenAgentFacts, +} from "../plugins/agent-tools.js"; import { ProviderOptionsValidationError } from "./provider-options.js"; import type { AgentToolResult } from "@getpaseo/plugin/server"; import type { RuntimeCleanupCandidate } from "./idle-runtime-cleanup/index.js"; @@ -1101,9 +1105,21 @@ export class AgentManager { if (!this.pluginSessions) return { stale: false }; return this.pluginSessions.refreshSession(agentId, { reservedToolNames: () => this.reservedAgentToolNames(agentId), + agentFacts: () => this.pluginOpenAgentFacts(agentId), }); } + /** The agent fields a reopen would put in its plugin open request. */ + private async pluginOpenAgentFacts(agentId: string): Promise { + const agent = this.requireAgent(agentId); + return { + workspaceId: agent.workspaceId ?? null, + title: await this.resolveInitialPersistedTitle(agentId, agent.config, null), + background: agent.background === true, + ...agentEntityData(agent), + }; + } + /** * Resolves the plugin's current instance of the tool at call time, so a launch-time catalog reaches a restarted * plugin and never a different plugin that took the name. 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 ec2c91c772..1254413205 100644 --- a/packages/server/src/server/plugins/agent-tools.e2e.test.ts +++ b/packages/server/src/server/plugins/agent-tools.e2e.test.ts @@ -879,6 +879,28 @@ test("after a rejected reopen, a tool name stays with the plugin that had it, ev } }, 60_000); +const metadataGatedSource = openCounterSource.replace( + "opens++;", + 'opens++;\n if (request.metadata.tools !== "on") return;', +); + +test("a plugin reload reruns its callback with the agent's current metadata", async () => { + const h = await setup(false, metadataGatedSource); + try { + const manager = h.daemon.daemon.agentManager; + const a = await h.create(); + expect(await listedToolNames(await h.mcp(a.id))).not.toContain("other_opens"); + await manager.setMetadata(a.id, { tools: "on" }); + await h.client.reloadPlugin("tools-test"); + expect(await listedToolNames(await h.mcp(a.id))).toContain("other_opens"); + // The session launched without the tool, so it reopens at the next idle prompt. + await sendAndFinish(h, a.id, "after metadata"); + expect(h.launches).toHaveLength(2); + } finally { + await h.close(); + } +}, 60_000); + const impostorSource = ` import { z } from "zod"; import { defineAgentTool } from "@getpaseo/plugin/server"; diff --git a/packages/server/src/server/plugins/agent-tools.ts b/packages/server/src/server/plugins/agent-tools.ts index 17eee51810..d34e727f80 100644 --- a/packages/server/src/server/plugins/agent-tools.ts +++ b/packages/server/src/server/plugins/agent-tools.ts @@ -32,9 +32,17 @@ export type PluginSessionOpenResult = SessionOpenWireRequest & { mcpServerOwners: Record; }; +/** What an open request says about the agent that can change while its session runs. Plugins cannot change it. */ +export type SessionOpenAgentFacts = Pick< + SessionOpenWireRequest, + "workspaceId" | "title" | "background" | "labels" | "metadata" +>; + export interface PluginSessionRefreshOptions { /** Tool names a refreshed plugin tool must not take, beyond the tools other plugins attached. */ reservedToolNames(): Promise>; + /** The agent as it is now, so a rerun callback sees what a reopen would show it. */ + agentFacts(): Promise; } /** What the agent manager asks of plugins about a session: its open, its attached tools, and their calls. */ diff --git a/packages/server/src/server/plugins/session-attachments.ts b/packages/server/src/server/plugins/session-attachments.ts index e4d5d6f188..64def627d9 100644 --- a/packages/server/src/server/plugins/session-attachments.ts +++ b/packages/server/src/server/plugins/session-attachments.ts @@ -4,6 +4,7 @@ import type { AttachedAgentTool, PluginSessionOpenResult, PluginSessionRefreshOptions, + SessionOpenAgentFacts, SessionOpenWireRequest, } from "./agent-tools.js"; @@ -204,6 +205,7 @@ export class SessionAttachments { let previous = attachment.base; const contributions: Contribution[] = []; const replayed: { contribution: Contribution; plugin: SessionOpenPlugin }[] = []; + let facts: SessionOpenAgentFacts | undefined; for (const pluginId of pluginIds) { const plugin = running.get(pluginId); const before = recorded.get(pluginId); @@ -214,10 +216,11 @@ export class SessionAttachments { } // Stopped, or no longer declares the callback: the session loses what it contributed. if (!plugin) continue; - const contribution = await this.replay( - plugin, - before?.input ?? { ...previous, tools: [], mcpServers: {} }, - ); + facts ??= await options.agentFacts(); + const contribution = await this.replay(plugin, { + ...(before?.input ?? { ...previous, tools: [], mcpServers: {} }), + ...facts, + }); contributions.push(contribution); replayed.push({ contribution, plugin }); previous = contribution.output; diff --git a/public-docs/plugins/hooks.md b/public-docs/plugins/hooks.md index fb04b3e5e9..43af181675 100644 --- a/public-docs/plugins/hooks.md +++ b/public-docs/plugins/hooks.md @@ -505,7 +505,7 @@ target are unaffected. When a plugin declaring `agent.open.before` starts, stops, or reloads, Paseo reruns only that plugin's callback for each open session. The callback gets the request it saw at the session's last -open, with `reason: "refresh"`. This happens before the session's next agent tool call or prompt. +open, with `reason: "refresh"` and the agent's current title, workspace, labels, and metadata. This happens before the session's next agent tool call or prompt. Callbacks of other plugins do not run. - The tools the callback returns replace its old tools at once, also during a running turn. The new