mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-26 02:05:31 +00:00
fix(server): rerun a refreshed plugin callback with the agent's current metadata
A plugin reload used to reopen every session, so its callback saw the agent's current title, workspace, labels, and metadata. A refresh now replays the recorded open request instead, so a hook that filters on metadata set after launch kept returning nothing. The refresh now overlays those agent fields, which plugins cannot change, on the recorded request.
This commit is contained in:
@@ -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<SessionOpenAgentFacts> {
|
||||
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.
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -32,9 +32,17 @@ export type PluginSessionOpenResult = SessionOpenWireRequest & {
|
||||
mcpServerOwners: Record<string, string>;
|
||||
};
|
||||
|
||||
/** 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<ReadonlySet<string>>;
|
||||
/** The agent as it is now, so a rerun callback sees what a reopen would show it. */
|
||||
agentFacts(): Promise<SessionOpenAgentFacts>;
|
||||
}
|
||||
|
||||
/** What the agent manager asks of plugins about a session: its open, its attached tools, and their calls. */
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user