Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): ask the user before Codex calls a note tool
Elian Dorand1cd9956ba02
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.htmlindex 11d07d9ffc..56e0252d02 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html@@ -302,8 +302,8 @@runs on the provider's side and never asks.</p><aside class="admonition important"><p>Among the <a class="reference-link" href="#root/_help_eFhxp9laTW1S">Subscription-based providers</a>,- Claude Code and Google Antigravity ask like any other provider. GitHub- Copilot and OpenAI Codex don't ask yet: their tool calls run without approval.</p>+ Claude Code, Google Antigravity and OpenAI Codex ask like any other provider.+ GitHub Copilot doesn't ask yet: its tool calls run without approval.</p></aside><aside class="admonition note"><p>Most changes you allow can be undone: a deleted note can be restored,diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.htmlindex 9bf8166c91..ab59d7ca53 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI/Privacy.html@@ -69,10 +69,10 @@beyond the current one.</li><li>It decides what to fetch, and each call waits for you to allow it, unlessthe chat's permission mode lets reads or every call run on their own, or- you allowed that tool for the chat (and except with the <a class="reference-link"- href="#root/_help_eFhxp9laTW1S">Subscription-based providers</a> other than- Claude Code and Google Antigravity). There is no per-note permission system,- so the reachable scope is your whole tree.</li>+ you allowed that tool for the chat (and except with GitHub Copilot, among+ the <a class="reference-link" href="#root/_help_eFhxp9laTW1S">Subscription-based providers</a>).+ There is no per-note permission system, so the reachable scope is your+ whole tree.</li></ul></li><li><strong>Attachments and mentions</strong>, when you add them: images,diff --git a/apps/server/src/services/llm/providers/acp_agent.ts b/apps/server/src/services/llm/providers/acp_agent.tsindex e67d2ed2cf..1549b35fd4 100644--- a/apps/server/src/services/llm/providers/acp_agent.ts+++ b/apps/server/src/services/llm/providers/acp_agent.ts@@ -258,13 +258,13 @@ export abstract class AcpAgentProvider implements LlmProvider {* Permission policy. The default denies every request; a subclass whose* agent asks before running Trilium's own note tools approves those.* `config` is the chat turn's configuration, and absent outside a chat- * turn (the model probe, the title). `mcpServerOf` names the MCP server of- * a tool call the agent announced before asking about it.+ * turn (the model probe, the title). `mcpCalls` holds the MCP tool calls the+ * agent announced before asking about them.*/protected decidePermission(request: AcpPermissionRequest,_config?: LlmProviderConfig,- _mcpServerOf?: (toolCallId: string | undefined) => string | undefined+ _mcpCalls?: AnnouncedMcpCalls): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {return denyPermission(request, this.logLabel);}@@ -768,21 +768,21 @@ export abstract class AcpAgentProvider implements LlmProvider {} = {}): Promise<AcpClient> {const launch = await this.launchSpec();- const mcpServers = new McpToolCallServers();+ const mcpCalls = new AnnouncedMcpCalls();const client = (launch.worker ? AcpClient.startWorker : AcpClient.start)(launch.binary, {cwd: this.agentCwd(),shell: launch.shell,args: launch.args,env: launch.env,onNotification: (method, params) => {- mcpServers.observe(method, params);+ mcpCalls.observe(method, params);onNotification(method, params);},onAgentRequest: (method, params) => this.handleAgentRequest(method,params,options.turnConfig?.((params as { sessionId?: string } | undefined)?.sessionId),- toolCallId => mcpServers.serverOf(toolCallId)+ mcpCalls),onExit: options.onExit});@@ -816,10 +816,10 @@ export abstract class AcpAgentProvider implements LlmProvider {method: string,params: unknown,config: LlmProviderConfig | undefined,- mcpServerOf: (toolCallId: string | undefined) => string | undefined+ mcpCalls: AnnouncedMcpCalls): unknown {if (method === "session/request_permission") {- return this.decidePermission(params as AcpPermissionRequest, config, mcpServerOf);+ return this.decidePermission(params as AcpPermissionRequest, config, mcpCalls);}throw new Error(`Trilium does not support "${method}".`);}@@ -864,32 +864,52 @@ export abstract class AcpAgentProvider implements LlmProvider {}/**- * Answer a permission request for a call to Trilium's note tools, which the caller has already- * recognized as one, with the user's decision through the turn's `toolApprovals`. A declined call- * is rejected once, so the agent asks again about the tool's next call. Without approvals (outside- * a chat turn) the call is allowed, as the API providers run tools unasked there.+ * Answer a permission request for a call to Trilium's note tools, which the request itself names,+ * with the user's decision through the turn's `toolApprovals` (see {@link answerNoteToolCalls}).*/-export async function askNoteToolPermission(+export function askNoteToolPermission(request: AcpPermissionRequest,logLabel: string,toolApprovals: ToolApprovals | undefined): Promise<AcpPermissionOutcome> {const toolCall = request.toolCall;- const toolName = toolCall ? mcpToolCall(toolCall)?.tool : undefined;+ const tool = toolCall ? mcpToolCall(toolCall)?.tool : undefined;+ const calls = toolCall?.toolCallId && tool+ ? [ { toolCallId: toolCall.toolCallId, server: NOTE_TOOLS_MCP_SERVER_NAME, tool, input: mcpArguments(toolCall.rawInput) } ]+ : [];+ return answerNoteToolCalls(request, logLabel, calls, toolApprovals);+}++/**+ * Answer a permission request about calls to Trilium's note tools with the user's decision through+ * the turn's `toolApprovals`. The request is allowed once only when every call in `calls` is+ * approved: an agent that cannot tell which of several waiting calls asks names none, and allowing+ * it must not run a call the user declined. Each call is asked about once. A declined request is+ * rejected once, so the agent asks again about the tool's next call. Without approvals (outside a+ * chat turn) the request is allowed, as the API providers run tools unasked there.+ */+export async function answerNoteToolCalls(+ request: AcpPermissionRequest,+ logLabel: string,+ calls: AnnouncedMcpCall[],+ toolApprovals: ToolApprovals | undefined+): Promise<AcpPermissionOutcome> {const allowOnce = request.options?.find(o => o.kind === "allow_once");- if (!allowOnce || !toolCall?.toolCallId || !toolName) {+ if (!allowOnce || (toolApprovals && calls.length === 0)) {return denyPermission(request, logLabel);}- const approved = !toolApprovals || await toolApprovals.request({- toolCallId: toolCall.toolCallId,- toolName,- toolInput: unwrapMcpArguments(toolCall.rawInput) as Record<string, unknown>- });- if (approved) {+ const decisions = toolApprovals+ ? await Promise.all(calls.map(call => call.decision ??= toolApprovals.request({+ toolCallId: call.toolCallId,+ toolName: call.tool,+ toolInput: call.input+ })))+ : [];+ if (decisions.every(Boolean)) {return { outcome: { outcome: "selected", optionId: allowOnce.optionId } };}- getLog().info(`${logLabel}: the user declined the call to "${toolName}".`);+ getLog().info(`${logLabel}: the user declined the call to "${calls.map(call => call.tool).join("\", \"")}".`);const rejectOnce = request.options?.find(o => o.kind === "reject_once");return rejectOnce? { outcome: { outcome: "selected", optionId: rejectOnce.optionId } }@@ -1166,13 +1186,22 @@ function mcpToolCall(update: { _meta?: unknown; rawInput?: unknown }): { server?return { server: pick(meta.mcp?.server, input?.server), tool: pick(meta.mcp?.tool, input?.tool) };}+/** An MCP tool call an agent announced and has not finished. */+export interface AnnouncedMcpCall {+ toolCallId: string;+ server: string;+ tool: string;+ input: Record<string, unknown>;+ /** The user's decision, once asked, so a call is asked about once. */+ decision?: Promise<boolean>;+}+/**- * The MCP server of each tool call 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 server up here.+ * The MCP 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.*/-class McpToolCallServers {- private readonly byToolCallId = new Map<string, string>();+export class AnnouncedMcpCalls {+ private readonly byToolCallId = new Map<string, AnnouncedMcpCall>();observe(method: string, params: unknown): void {const update = method === "session/update" ? (params as AcpSessionUpdate | undefined)?.update : undefined;@@ -1180,21 +1209,32 @@ class McpToolCallServers {return;}if (update.sessionUpdate === "tool_call") {- const server = mcpToolCall(update)?.server;- if (server) {- this.byToolCallId.set(update.toolCallId, server);+ const { server, tool } = mcpToolCall(update) ?? {};+ if (server && tool) {+ this.byToolCallId.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);}}- serverOf(toolCallId: string | undefined): string | undefined {+ callOf(toolCallId: string | undefined): AnnouncedMcpCall | undefined {return toolCallId ? this.byToolCallId.get(toolCallId) : undefined;}++ /** The calls to `server` that have not finished. */+ pendingOn(server: string): AnnouncedMcpCall[] {+ return [ ...this.byToolCallId.values() ].filter(call => call.server === server);+ }}/** An MCP call's arguments, which `agy_acp_server` and `codex-acp` report wrapped as `{ arguments: … }`. */+/** The arguments of an MCP tool call, as an object. */+function mcpArguments(rawInput: unknown): Record<string, unknown> {+ const args = unwrapMcpArguments(rawInput);+ return typeof args === "object" && args !== null ? args as Record<string, unknown> : {};+}+function unwrapMcpArguments(rawInput: unknown): unknown {const wrapped = rawInput as { arguments?: unknown } | undefined;return wrapped && typeof wrapped.arguments === "object" && wrapped.arguments !== null ? wrapped.arguments : rawInput ?? {};diff --git a/apps/server/src/services/llm/providers/codex_agent.spec.ts b/apps/server/src/services/llm/providers/codex_agent.spec.tsindex cdf04f8a33..4d81c43259 100644--- a/apps/server/src/services/llm/providers/codex_agent.spec.ts+++ b/apps/server/src/services/llm/providers/codex_agent.spec.ts@@ -115,6 +115,7 @@ class FakeAcpClient {vi.mock("./acp_client.js", () => ({ AcpClient: FakeAcpClient, AcpError: FakeAcpError }));const { 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");/** Part of the catalog codex-acp 1.13.1 reports on session/new for a ChatGPT account. */@@ -341,14 +342,63 @@ describe("CodexAgentProvider note tools", () => {]);});- it("approves a call the adapter could not match to its announcement by the server it names", () => {+ it("asks the turn's approvals about each note-tool call, and allows a request naming only the server when every waiting call is allowed", async () => {+ const requests: { toolCallId: string; toolName: string; toolInput: Record<string, unknown> }[] = [];+ const toolApprovals = new ToolApprovals();+ // The user allows everything but the deletion.+ toolApprovals.onRequest = (asked) => {+ requests.push(asked);+ queueMicrotask(() => toolApprovals.answer(asked.toolCallId, asked.toolName !== "delete_note"));+ };+ const answers: unknown[] = [];+ const standalone = mcpApproval("elicitation:sess-1:trilium:1", { title: "MCP tool call approval", rawInput: { serverName: "trilium", description: "Allow?" } });+ FakeAcpClient.onPrompt = async client => {+ const update = (u: Record<string, unknown>) => client.onNotification?.("session/update", { sessionId: "sess-1", update: u });+ const ask = async (request: unknown) => answers.push(await client.onAgentRequest?.("session/request_permission", request));+ const finish = (toolCallId: string) => update({ sessionUpdate: "tool_call_update", toolCallId, status: "completed", rawOutput: "done" });++ update(mcpToolCall("exec-1", "trilium", "get_note", { noteId: "a" }));+ await ask(mcpApproval("exec-1"));+ update(mcpToolCall("exec-2", "trilium", "delete_note", { noteId: "b" }));+ await ask(mcpApproval("exec-2"));+ finish("exec-1");+ finish("exec-2");++ // Two calls wait at once, and the adapter cannot tell which one asks.+ update(mcpToolCall("exec-3", "trilium", "get_note", { noteId: "c" }));+ update(mcpToolCall("exec-4", "trilium", "rename_note", { noteId: "c", newTitle: "C" }));+ await ask(standalone);+ await ask(standalone);+ finish("exec-3");+ finish("exec-4");++ update(mcpToolCall("exec-5", "trilium", "get_note", { noteId: "d" }));+ update(mcpToolCall("exec-6", "trilium", "delete_note", { noteId: "d" }));+ await ask(standalone);+ };++ await collect(new CodexAgentProvider().chatChunks([{ role: "user", content: "hi" }], { enableNoteTools: true, toolApprovals }));++ expect(answers).toEqual([ ALLOWED, DENIED, ALLOWED, ALLOWED, DENIED ]);+ // Each call is asked about once, by the name and arguments it was announced with.+ expect(requests).toEqual([+ { toolCallId: "exec-1", toolName: "get_note", toolInput: { noteId: "a" } },+ { toolCallId: "exec-2", toolName: "delete_note", toolInput: { noteId: "b" } },+ { toolCallId: "exec-3", toolName: "get_note", toolInput: { noteId: "c" } },+ { toolCallId: "exec-4", toolName: "rename_note", toolInput: { noteId: "c", newTitle: "C" } },+ { toolCallId: "exec-5", toolName: "get_note", toolInput: { noteId: "d" } },+ { toolCallId: "exec-6", toolName: "delete_note", toolInput: { noteId: "d" } }+ ]);+ });++ 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?" } })});- expect(decideCodexPermission(standalone("trilium"), "test")).toEqual(ALLOWED);+ expect(await decideCodexPermission(standalone("trilium"), "test")).toEqual(ALLOWED);expect(decideCodexPermission(standalone("codex_apps"), "test")).toEqual(DENIED);// No one-shot allow on offer: nothing is approved permanently.- expect(decideCodexPermission({ ...standalone("trilium"), options: MCP_APPROVAL_OPTIONS.slice(1) }, "test")).toEqual(DENIED);+ expect(await decideCodexPermission({ ...standalone("trilium"), options: MCP_APPROVAL_OPTIONS.slice(1) }, "test")).toEqual(DENIED);});});diff --git a/apps/server/src/services/llm/providers/codex_agent.ts b/apps/server/src/services/llm/providers/codex_agent.tsindex 461455e0ab..de9521342d 100644--- a/apps/server/src/services/llm/providers/codex_agent.ts+++ b/apps/server/src/services/llm/providers/codex_agent.ts@@ -16,11 +16,12 @@*/import { LLM_REASONING_EFFORTS, type LlmCitation, type LlmMessage, type LlmReasoningEffort, type LlmStreamChunk } from "@triliumnext/commons";+import type { ToolApprovals } from "@triliumnext/core/src/services/llm/tool_approval.js";import type { LlmProviderConfig, ModelInfo } from "@triliumnext/core/src/services/llm/types.js";import path from "path";import dataDirs from "../../data_dir.js";-import { AcpAgentProvider, type AcpLaunchSpec, type AcpModel, type AcpNewSessionParams, type AcpPermissionOutcome, type AcpPermissionRequest, type AcpSessionModelState, type AcpToolCallUpdate, type BuiltInToolDisplay, denyPermission, describeError, nearestEffort, NOTE_TOOLS_MCP_SERVER_NAME, sortEfforts } from "./acp_agent.js";+import { AcpAgentProvider, type AcpLaunchSpec, type AcpModel, type AcpNewSessionParams, type AcpPermissionOutcome, type AcpPermissionRequest, type AcpSessionModelState, type AcpToolCallUpdate, type AnnouncedMcpCalls, answerNoteToolCalls, type BuiltInToolDisplay, denyPermission, describeError, nearestEffort, NOTE_TOOLS_MCP_SERVER_NAME, sortEfforts } from "./acp_agent.js";import type { AcpClient } from "./acp_client.js";import { getAcpHookEndpointUrl } from "./acp_mcp_endpoint.js";import { buildHookCommand, resolveCurlPath } from "./acp_hook.js";@@ -146,10 +147,10 @@ export class CodexAgentProvider extends AcpAgentProvider {protected decidePermission(request: AcpPermissionRequest,- _config?: LlmProviderConfig,- mcpServerOf?: (toolCallId: string | undefined) => string | undefined- ): AcpPermissionOutcome {- return decideCodexPermission(request, this.logLabel, mcpServerOf);+ config?: LlmProviderConfig,+ mcpCalls?: AnnouncedMcpCalls+ ): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {+ return decideCodexPermission(request, this.logLabel, { mcpCalls, toolApprovals: config?.toolApprovals });}/** The adapter reports each MCP server's startup as a tool call of its own, `mcp_startup.<server>`. */@@ -285,30 +286,39 @@ export class CodexAgentProvider extends AcpAgentProvider {}/**- * Approve once a call to Trilium's note tools; deny everything else.+ * Ask the user about a call to Trilium's note tools through the turn's `toolApprovals`; deny+ * everything else.** Codex asks before running an MCP tool that is not marked read-only, and the* adapter forwards that as a permission request marked* `_meta.is_mcp_tool_approval`. The request carries only the id of the tool- * call, which the adapter announced just before with its server in `rawInput`,- * so the server is looked up with `mcpServerOf`. When several calls to one- * server wait at once, the adapter cannot tell which one is asking and names- * the server in `toolCall.rawInput.serverName` instead. Codex fills in both,- * not the model.+ * call, which the adapter announced just before with its server, tool and+ * arguments in `rawInput`, so the call is looked up in `mcpCalls`. When several+ * calls to one server wait at once, the adapter cannot tell which one is asking+ * and names the server in `toolCall.rawInput.serverName` instead; the request is+ * then allowed only when the user allows every note-tool call that waits. Codex+ * fills in both, not the model.*/export function decideCodexPermission(request: AcpPermissionRequest,logLabel: string,- mcpServerOf?: (toolCallId: string | undefined) => string | undefined-): AcpPermissionOutcome {+ { mcpCalls, toolApprovals }: { mcpCalls?: AnnouncedMcpCalls; toolApprovals?: ToolApprovals } = {}+): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {const toolCall = request.toolCall;+ if (request._meta?.is_mcp_tool_approval !== true) {+ return denyPermission(request, logLabel);+ }+ const announced = mcpCalls?.callOf(toolCall?.toolCallId);+ if (announced) {+ return announced.server === NOTE_TOOLS_MCP_SERVER_NAME+ ? answerNoteToolCalls(request, logLabel, [ announced ], toolApprovals)+ : denyPermission(request, logLabel);+ }const standaloneServer = (toolCall?.rawInput as { serverName?: unknown } | undefined)?.serverName;- const server = mcpServerOf?.(toolCall?.toolCallId) ?? standaloneServer;- const allowOnce = request.options?.find(o => o.kind === "allow_once");- if (request._meta?.is_mcp_tool_approval === true && server === NOTE_TOOLS_MCP_SERVER_NAME && allowOnce) {- return { outcome: { outcome: "selected", optionId: allowOnce.optionId } };+ if (standaloneServer !== NOTE_TOOLS_MCP_SERVER_NAME) {+ return denyPermission(request, logLabel);}- return denyPermission(request, logLabel);+ return answerNoteToolCalls(request, logLabel, mcpCalls?.pendingOn(NOTE_TOOLS_MCP_SERVER_NAME) ?? [], toolApprovals);}/**diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 1a972e1b1b..7f22395119 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -178,7 +178,7 @@ A new chat starts in the mode you picked last, in any chat, or in _Ask before chA tool allowed for the chat runs without asking in every mode. So does looking something up in what ships with Trilium (its User Guide, the skill sheets that teach the AI Trilium's search syntax and note types, and the icons), since it reads none of your notes. A model's built-in web search runs on the provider's side and never asks.> [!IMPORTANT]-> Among the <a class="reference-link" href="AI/Providers/Subscription-based%20providers.md">Subscription-based providers</a>, Claude Code and Google Antigravity ask like any other provider. GitHub Copilot and OpenAI Codex don't ask yet: their tool calls run without approval.+> Among the <a class="reference-link" href="AI/Providers/Subscription-based%20providers.md">Subscription-based providers</a>, Claude Code, Google Antigravity and OpenAI Codex ask like any other provider. GitHub Copilot doesn't ask yet: its tool calls run without approval.> [!NOTE]> Most changes you allow can be undone: a deleted note can be restored, and an edit reverted from its revision. Setting an attribute is harder to undo, since attributes have no history.diff --git a/docs/User Guide/User Guide/AI/Privacy.md b/docs/User Guide/User Guide/AI/Privacy.mdindex 847e19c5e6..4324668ed7 100644--- a/docs/User Guide/User Guide/AI/Privacy.md+++ b/docs/User Guide/User Guide/AI/Privacy.md@@ -28,7 +28,7 @@ This is the reason the provider list is grouped this way, and why Trilium pre-se* Notes above a size threshold send a size hint instead of the text, and the model must call a tool to read further.* **Any note the model asks for, when note access is enabled.*** Tools let the model search your tree and read notes, attributes and attachments beyond the current one.- * It decides what to fetch, and each call waits for you to allow it, unless the chat's permission mode lets reads or every call run on their own, or you allowed that tool for the chat (and except with the <a class="reference-link" href="Providers/Subscription-based%20providers.md">Subscription-based providers</a> other than Claude Code and Google Antigravity). There is no per-note permission system, so the reachable scope is your whole tree.+ * It decides what to fetch, and each call waits for you to allow it, unless the chat's permission mode lets reads or every call run on their own, or you allowed that tool for the chat (and except with GitHub Copilot, among the <a class="reference-link" href="Providers/Subscription-based%20providers.md">Subscription-based providers</a>). There is no per-note permission system, so the reachable scope is your whole tree.* **Attachments and mentions**, when you add them: images, PDFs and text files are sent to the provider, and an `@` mention makes that note fetchable.* **Your search query**, when web search is enabled, the model's search terms reach the provider's own search infrastructure, or the search provider chosen in the _Tools_ menu (Brave Search, Tavily, Exa, Serper, Perplexity or your SearXNG instance).* **The pages the model reads**, when web search goes through a search provider: Trilium fetches each page the model asks for from the site itself, so the site sees a request from the machine running Trilium. The page's text is then sent to the model's provider.