Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): keep one Codex chat's tool approvals out of another's

Elian Doran0a731bc6a574
diff --git a/apps/server/src/services/llm/providers/acp_agent.ts b/apps/server/src/services/llm/providers/acp_agent.ts
index f4c4153372..5edde1f4d6 100644
--- a/apps/server/src/services/llm/providers/acp_agent.ts
+++ b/apps/server/src/services/llm/providers/acp_agent.ts
@@ -1212,42 +1212,55 @@ export interface AnnouncedMcpCall {
}
/**
- * The tool calls an agent announced and has not finished, by call id. `codex-acp` asks
- * permission for an MCP call with only its id, so the permission policy looks the call up here.
+ * The tool calls an agent announced and has not finished, by session and call id. `codex-acp` asks
+ * permission for an MCP call with only its id, so the permission policy looks the call up here. One
+ * agent process serves the sessions of several chats, so every lookup is confined to one session.
*/
export class AnnouncedMcpCalls {
- private readonly byToolCallId = new Map<string, AnnouncedMcpCall>();
- private readonly updatesByToolCallId = new Map<string, AcpToolCallUpdate>();
+ private readonly bySession = new Map<string, {
+ calls: Map<string, AnnouncedMcpCall>;
+ updates: Map<string, AcpToolCallUpdate>;
+ }>();
observe(method: string, params: unknown): void {
- const update = method === "session/update" ? (params as AcpSessionUpdate | undefined)?.update : undefined;
- if (!update?.toolCallId) {
+ const { sessionId, update } = method === "session/update" ? (params as Partial<AcpSessionUpdate> | undefined) ?? {} : {};
+ if (!sessionId || !update?.toolCallId) {
return;
}
if (update.sessionUpdate === "tool_call") {
- this.updatesByToolCallId.set(update.toolCallId, update);
+ let session = this.bySession.get(sessionId);
+ if (!session) {
+ session = { calls: new Map(), updates: new Map() };
+ this.bySession.set(sessionId, session);
+ }
+ session.updates.set(update.toolCallId, update);
const { server, tool } = mcpToolCall(update) ?? {};
if (server && tool) {
- this.byToolCallId.set(update.toolCallId, { toolCallId: update.toolCallId, server, tool, input: mcpArguments(update.rawInput) });
+ session.calls.set(update.toolCallId, { toolCallId: update.toolCallId, server, tool, input: mcpArguments(update.rawInput) });
}
} else if (update.sessionUpdate === "tool_call_update" && (update.status === "completed" || update.status === "failed")) {
- this.byToolCallId.delete(update.toolCallId);
- this.updatesByToolCallId.delete(update.toolCallId);
+ const session = this.bySession.get(sessionId);
+ session?.calls.delete(update.toolCallId);
+ session?.updates.delete(update.toolCallId);
+ if (session?.updates.size === 0) {
+ this.bySession.delete(sessionId);
+ }
}
}
- /** The announcement of any unfinished tool call, including one with no MCP marking. */
- announcementOf(toolCallId: string | undefined): AcpToolCallUpdate | undefined {
- return toolCallId ? this.updatesByToolCallId.get(toolCallId) : undefined;
+ /** The announcement of an unfinished tool call of `sessionId`, including one with no MCP marking. */
+ announcementOf(sessionId: string | undefined, toolCallId: string | undefined): AcpToolCallUpdate | undefined {
+ return sessionId && toolCallId ? this.bySession.get(sessionId)?.updates.get(toolCallId) : undefined;
}
- callOf(toolCallId: string | undefined): AnnouncedMcpCall | undefined {
- return toolCallId ? this.byToolCallId.get(toolCallId) : undefined;
+ callOf(sessionId: string | undefined, toolCallId: string | undefined): AnnouncedMcpCall | undefined {
+ return sessionId && toolCallId ? this.bySession.get(sessionId)?.calls.get(toolCallId) : undefined;
}
- /** The calls to `server` that have not finished. */
- pendingOn(server: string): AnnouncedMcpCall[] {
- return [ ...this.byToolCallId.values() ].filter(call => call.server === server);
+ /** The calls of `sessionId` to `server` that have not finished. */
+ pendingOn(sessionId: string | undefined, server: string): AnnouncedMcpCall[] {
+ const calls = sessionId ? this.bySession.get(sessionId)?.calls.values() : undefined;
+ return calls ? [ ...calls ].filter(call => call.server === server) : [];
}
}
diff --git a/apps/server/src/services/llm/providers/codex_agent.spec.ts b/apps/server/src/services/llm/providers/codex_agent.spec.ts
index 1a983b1f82..e0ae0856b3 100644
--- a/apps/server/src/services/llm/providers/codex_agent.spec.ts
+++ b/apps/server/src/services/llm/providers/codex_agent.spec.ts
@@ -114,7 +114,7 @@ class FakeAcpClient {
vi.mock("./acp_client.js", () => ({ AcpClient: FakeAcpClient, AcpError: FakeAcpError }));
-const { resetAcpAgentStateForTests } = await import("./acp_agent.js");
+const { AnnouncedMcpCalls, resetAcpAgentStateForTests } = await import("./acp_agent.js");
const { ToolApprovals } = await import("@triliumnext/core/src/services/llm/tool_approval.js");
const { buildCodexModelList, CitationStripper, CodexAgentProvider, decideCodexPermission, resetCodexCatalogForTests } = await import("./codex_agent.js");
@@ -405,6 +405,31 @@ describe("CodexAgentProvider note tools", () => {
]);
});
+ it("asks only about the calls of the session that sends the request", async () => {
+ const mcpCalls = new AnnouncedMcpCalls();
+ const announce = (sessionId: string, toolCallId: string, tool: string) =>
+ mcpCalls.observe("session/update", { sessionId, update: mcpToolCall(toolCallId, "trilium", tool, {}) });
+ announce("sess-1", "exec-1", "get_note");
+ announce("sess-1", "exec-2", "get_note");
+ announce("sess-2", "exec-3", "delete_note");
+ // The same call id in another session is another call.
+ announce("sess-2", "exec-1", "delete_note");
+ const asked: string[] = [];
+ const toolApprovals = new ToolApprovals();
+ toolApprovals.onRequest = (request) => {
+ asked.push(`${request.toolCallId}:${request.toolName}`);
+ queueMicrotask(() => toolApprovals.answer(request.toolCallId, true));
+ };
+
+ const standalone = mcpApproval("elicitation:sess-1:trilium:1", { title: "MCP tool call approval", rawInput: { serverName: "trilium" } });
+ expect(await decideCodexPermission(standalone, "test", { mcpCalls, toolApprovals })).toEqual(ALLOWED);
+ expect(asked).toEqual([ "exec-1:get_note", "exec-2:get_note" ]);
+
+ const fromSecond = { ...mcpApproval("exec-1"), sessionId: "sess-2" };
+ expect(await decideCodexPermission(fromSecond, "test", { mcpCalls, toolApprovals })).toEqual(ALLOWED);
+ expect(asked).toEqual([ "exec-1:get_note", "exec-2:get_note", "exec-1:delete_note" ]);
+ });
+
it("outside a chat turn, approves a call the adapter could not match to its announcement by the server it names", async () => {
const standalone = (serverName: string) => ({
...mcpApproval("elicitation:sess-1:trilium:1", { title: "MCP tool call approval", rawInput: { serverName, description: "Allow?" } })
diff --git a/apps/server/src/services/llm/providers/codex_agent.ts b/apps/server/src/services/llm/providers/codex_agent.ts
index 3dbb13f242..4fb32c19da 100644
--- a/apps/server/src/services/llm/providers/codex_agent.ts
+++ b/apps/server/src/services/llm/providers/codex_agent.ts
@@ -316,7 +316,7 @@ export function decideCodexPermission(
if (request._meta?.is_mcp_tool_approval !== true) {
return denyPermission(request, logLabel);
}
- const announced = mcpCalls?.callOf(toolCall?.toolCallId);
+ const announced = mcpCalls?.callOf(request.sessionId, toolCall?.toolCallId);
if (announced) {
return announced.server === NOTE_TOOLS_MCP_SERVER_NAME
? answerNoteToolCalls(request, logLabel, [ announced ], toolApprovals)
@@ -326,7 +326,7 @@ export function decideCodexPermission(
if (standaloneServer !== NOTE_TOOLS_MCP_SERVER_NAME) {
return denyPermission(request, logLabel);
}
- return answerNoteToolCalls(request, logLabel, mcpCalls?.pendingOn(NOTE_TOOLS_MCP_SERVER_NAME) ?? [], toolApprovals);
+ return answerNoteToolCalls(request, logLabel, mcpCalls?.pendingOn(request.sessionId, NOTE_TOOLS_MCP_SERVER_NAME) ?? [], toolApprovals);
}
/**
diff --git a/apps/server/src/services/llm/providers/copilot_agent.spec.ts b/apps/server/src/services/llm/providers/copilot_agent.spec.ts
index d412e1f273..b9f3eee3f0 100644
--- a/apps/server/src/services/llm/providers/copilot_agent.spec.ts
+++ b/apps/server/src/services/llm/providers/copilot_agent.spec.ts
@@ -1012,19 +1012,19 @@ describe("decideCopilotPermission", () => {
expect(await decideCopilotPermission(rename("rename_note"), "Copilot")).toEqual(ALLOWED);
});
- it("takes the tool and its arguments from the announced call when the request leaves them out", async () => {
+ it("takes the tool and its arguments from the call its own session announced when the request leaves them out", async () => {
const mcpCalls = new AnnouncedMcpCalls();
mcpCalls.observe("session/update", {
+ sessionId: "sess-1",
update: { sessionUpdate: "tool_call", toolCallId: "toolu_s", title: "search_notes", rawInput: { query: "x" } }
});
const asked: unknown[] = [];
+ const request = (sessionId: string) => ({ sessionId, toolCall: { toolCallId: "toolu_s" }, options: OPTIONS });
- expect(await decideCopilotPermission(
- { toolCall: { toolCallId: "toolu_s" }, options: OPTIONS },
- "Copilot",
- { mcpCalls, toolApprovals: answering(true, asked) }
- )).toEqual(ALLOWED);
+ expect(await decideCopilotPermission(request("sess-1"), "Copilot", { mcpCalls, toolApprovals: answering(true, asked) })).toEqual(ALLOWED);
expect(asked).toEqual([ { toolCallId: "toolu_s", toolName: "search_notes", toolInput: { query: "x" } } ]);
+ expect(await decideCopilotPermission(request("sess-2"), "Copilot", { mcpCalls, toolApprovals: answering(true, asked) })).toEqual(REJECTED_ALWAYS);
+ expect(asked).toHaveLength(1);
});
it("denies a built-in tool, another server's tool and a tool Trilium does not define, without asking", async () => {
diff --git a/apps/server/src/services/llm/providers/copilot_agent.ts b/apps/server/src/services/llm/providers/copilot_agent.ts
index 455b166aba..1f6092b738 100644
--- a/apps/server/src/services/llm/providers/copilot_agent.ts
+++ b/apps/server/src/services/llm/providers/copilot_agent.ts
@@ -195,7 +195,7 @@ export function decideCopilotPermission(
{ mcpCalls, toolApprovals }: { mcpCalls?: AnnouncedMcpCalls; toolApprovals?: ToolApprovals } = {}
): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {
const toolCall = request.toolCall;
- const announced = mcpCalls?.announcementOf(toolCall?.toolCallId);
+ const announced = mcpCalls?.announcementOf(request.sessionId, toolCall?.toolCallId);
const tool = noteToolName(toolCall?.title) ?? noteToolName(announced?.title);
if (!toolCall?.toolCallId || !tool) {
return denyPermission(request, logLabel);