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 GitHub Copilot calls a note tool
Elian Doran2920b7c797f6
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 c198b5f604..27411e30ee 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@@ -301,10 +301,10 @@a web page never ask either: turning on web search in the <em>Tools</em> menuis what allows them, whether the model searches on its own or through asearch provider.</p>-<aside class="admonition important">- <p>Among the <a class="reference-link" href="#root/_help_eFhxp9laTW1S">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.</p>+<aside class="admonition note">+ <p>The <a class="reference-link" href="#root/_help_eFhxp9laTW1S">Subscription-based providers</a> (Claude+ Code, GitHub Copilot, Google Antigravity and OpenAI Codex) ask like any+ other provider.</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 ab59d7ca53..6544920072 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,8 @@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 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>+ you allowed that tool for the chat. 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 1549b35fd4..93a8f4ee55 100644--- a/apps/server/src/services/llm/providers/acp_agent.ts+++ b/apps/server/src/services/llm/providers/acp_agent.ts@@ -1197,11 +1197,12 @@ export interface AnnouncedMcpCall {}/**- * The MCP tool calls an agent announced and has not finished, by call id. `codex-acp` asks+ * 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.*/export class AnnouncedMcpCalls {private readonly byToolCallId = new Map<string, AnnouncedMcpCall>();+ private readonly updatesByToolCallId = new Map<string, AcpToolCallUpdate>();observe(method: string, params: unknown): void {const update = method === "session/update" ? (params as AcpSessionUpdate | undefined)?.update : undefined;@@ -1209,15 +1210,22 @@ export class AnnouncedMcpCalls {return;}if (update.sessionUpdate === "tool_call") {+ this.updatesByToolCallId.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) });}} else if (update.sessionUpdate === "tool_call_update" && (update.status === "completed" || update.status === "failed")) {this.byToolCallId.delete(update.toolCallId);+ this.updatesByToolCallId.delete(update.toolCallId);}}+ /** 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;+ }+callOf(toolCallId: string | undefined): AnnouncedMcpCall | undefined {return toolCallId ? this.byToolCallId.get(toolCallId) : undefined;}@@ -1228,9 +1236,11 @@ export class AnnouncedMcpCalls {}}-/** 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> {+/**+ * The arguments of an MCP tool call, as an object. `agy_acp_server` and `codex-acp` report them+ * wrapped as `{ arguments: … }`.+ */+export function mcpArguments(rawInput: unknown): Record<string, unknown> {const args = unwrapMcpArguments(rawInput);return typeof args === "object" && args !== null ? args as Record<string, unknown> : {};}diff --git a/apps/server/src/services/llm/providers/copilot_agent.spec.ts b/apps/server/src/services/llm/providers/copilot_agent.spec.tsindex 433e609986..d412e1f273 100644--- a/apps/server/src/services/llm/providers/copilot_agent.spec.ts+++ b/apps/server/src/services/llm/providers/copilot_agent.spec.ts@@ -32,6 +32,11 @@ vi.mock("./acp_mcp_endpoint.js", () => ({ getAcpMcpEndpointUrl: mcpEndpointMockconst buildNoteHintMock = vi.hoisted(() => vi.fn((noteId: string): string | null => `NOTE_META(${noteId})`));vi.mock("@triliumnext/core/src/services/llm/note_hint.js", () => ({ buildNoteHint: buildNoteHintMock }));+// Trilium's note tools, as the permission policy looks them up.+vi.mock("@triliumnext/core/src/services/llm/tools/index.js", () => ({+ allToolRegistries: [ new Map([ [ "search_notes", {} ], [ "rename_note", {} ] ]) ]+}));+const resolveAttachmentPartMock = vi.hoisted(() => vi.fn());vi.mock("@triliumnext/core/src/services/llm/attachment_content.js", async (importOriginal) => ({...await importOriginal<typeof import("@triliumnext/core/src/services/llm/attachment_content.js")>(),@@ -141,10 +146,11 @@ class FakeAcpError extends Error {vi.mock("./acp_client.js", () => ({ AcpClient: FakeAcpClient, AcpError: FakeAcpError }));-const { buildPromptBlocks, createUpdateCollector, denyPermission } = await import("./acp_agent.js");+const { AnnouncedMcpCalls, buildPromptBlocks, createUpdateCollector, denyPermission } = await import("./acp_agent.js");+const { ToolApprovals } = await import("@triliumnext/core/src/services/llm/tool_approval.js");const { MAX_TITLES_PER_SESSION } = await import("./transcript.js");const { IDLE_TIMEOUT_MS } = await import("./acp_client_pool.js");-const { buildCopilotModelList, CopilotAgentProvider, resetModelCatalogCacheForTests } = await import("./copilot_agent.js");+const { buildCopilotModelList, CopilotAgentProvider, decideCopilotPermission, resetModelCatalogCacheForTests } = await import("./copilot_agent.js");const decidePermission = (request: Parameters<typeof denyPermission>[0]) => denyPermission(request, "Copilot Agent provider");@@ -375,13 +381,13 @@ describe("CopilotAgentProvider.chatChunks", () => {expect(mcpEndpointMock).not.toHaveBeenCalled();});- it("spawns the CLI with the note-tool allow-list and built-in tool denials", async () => {+ it("spawns the CLI with the built-in tools denied and the note tools left to ask", async () => {const startSpy = vi.spyOn(FakeAcpClient, "start");const provider = new CopilotAgentProvider();await collect(provider.chatChunks([userMessage("hi")], {}));const args = startSpy.mock.calls[0][1].args ?? [];- expect(args).toContain("--allow-tool=trilium");+ expect(args.some(arg => arg.startsWith("--allow-tool"))).toBe(false);expect(args).toContain("--deny-tool=shell");expect(args).toContain("--deny-tool=write");startSpy.mockRestore();@@ -971,10 +977,83 @@ describe("CopilotAgentProvider.generateTitle", () => {});});+describe("decideCopilotPermission", () => {+ beforeEach(resetFakes);++ const OPTIONS = [+ { optionId: "allow_once", kind: "allow_once" },+ { optionId: "allow_always", kind: "allow_always" },+ { optionId: "reject_always", kind: "reject_always" },+ { optionId: "reject_once", kind: "reject_once" }+ ];+ const ALLOWED = { outcome: { outcome: "selected", optionId: "allow_once" } };+ const REJECTED_ONCE = { outcome: { outcome: "selected", optionId: "reject_once" } };+ const REJECTED_ALWAYS = { outcome: { outcome: "selected", optionId: "reject_always" } };+ const rename = (title: string) => ({+ toolCall: { toolCallId: "toolu_r", title, kind: "other", rawInput: { noteId: "n", newTitle: "New" } },+ options: OPTIONS+ });+ const answering = (approved: boolean, asked: unknown[]) => {+ const approvals = new ToolApprovals();+ approvals.onRequest = (request) => {+ asked.push(request);+ queueMicrotask(() => approvals.answer(request.toolCallId, approved));+ };+ return approvals;+ };++ it("asks the user about a note tool named bare or behind the trilium server, and rejects a declined call once", async () => {+ const asked: unknown[] = [];+ expect(await decideCopilotPermission(rename("rename_note"), "Copilot", { toolApprovals: answering(true, asked) })).toEqual(ALLOWED);+ expect(await decideCopilotPermission(rename("trilium-rename_note"), "Copilot", { toolApprovals: answering(true, asked) })).toEqual(ALLOWED);+ expect(await decideCopilotPermission(rename("trilium/rename_note"), "Copilot", { toolApprovals: answering(false, asked) })).toEqual(REJECTED_ONCE);+ expect(asked).toEqual(Array(3).fill({ toolCallId: "toolu_r", toolName: "rename_note", toolInput: { noteId: "n", newTitle: "New" } }));+ // Outside a chat turn there is no one to ask, as with the API providers.+ 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 () => {+ const mcpCalls = new AnnouncedMcpCalls();+ mcpCalls.observe("session/update", {+ update: { sessionUpdate: "tool_call", toolCallId: "toolu_s", title: "search_notes", rawInput: { query: "x" } }+ });+ const asked: unknown[] = [];++ expect(await decideCopilotPermission(+ { toolCall: { toolCallId: "toolu_s" }, options: OPTIONS },+ "Copilot",+ { mcpCalls, toolApprovals: answering(true, asked) }+ )).toEqual(ALLOWED);+ expect(asked).toEqual([ { toolCallId: "toolu_s", toolName: "search_notes", toolInput: { query: "x" } } ]);+ });++ it("denies a built-in tool, another server's tool and a tool Trilium does not define, without asking", async () => {+ const asked: unknown[] = [];+ const toolApprovals = answering(true, asked);+ for (const title of [ "Echo hello", "github-rename_note", "trilium-create_issue", "create_issue" ]) {+ expect(await decideCopilotPermission(rename(title), "Copilot", { toolApprovals })).toEqual(REJECTED_ALWAYS);+ }+ expect(await decideCopilotPermission({ toolCall: { title: "rename_note" }, options: OPTIONS }, "Copilot", { toolApprovals })).toEqual(REJECTED_ALWAYS);+ expect(asked).toEqual([]);+ });++ it("asks through the approvals of the turn that drives the session", async () => {+ const asked: unknown[] = [];+ const answers: unknown[] = [];+ FakeAcpClient.promptScript = async client => {+ answers.push(await client.onAgentRequest?.("session/request_permission", { sessionId: "sess-1", ...rename("rename_note") }));+ return { stopReason: "end_turn" };+ };+ await collect(new CopilotAgentProvider().chatChunks([userMessage("hi")], { enableNoteTools: true, toolApprovals: answering(false, asked) }));++ expect(answers).toEqual([ REJECTED_ONCE ]);+ expect(asked).toHaveLength(1);+ });+});+describe("denyPermission (fail-closed)", () => {- // Note tools are pre-approved via --allow-tool and never reach the callback;- // anything that does is denied. Payload shapes mirror the real CLI 1.0.71- // permission request captured live (opaque toolCallId, kind "execute").+ // Payload shapes mirror the real CLI 1.0.71 permission request captured+ // live (opaque toolCallId, kind "execute").it("rejects a built-in shell tool, preferring a persistent reject_always", () => {const decision = decidePermission({toolCall: { toolCallId: "toolu_011pPt1cC28vErGNDqhwut15", title: "Echo hello", kind: "execute" },diff --git a/apps/server/src/services/llm/providers/copilot_agent.ts b/apps/server/src/services/llm/providers/copilot_agent.tsindex 97fec4283c..455b166aba 100644--- a/apps/server/src/services/llm/providers/copilot_agent.ts+++ b/apps/server/src/services/llm/providers/copilot_agent.ts@@ -10,12 +10,14 @@* own installed `copilot` CLI (see copilot_binary.ts). The protocol handling is* shared with the other ACP providers (see acp_agent.ts); Copilot's built-in* file/shell tools are denied on its command line and again in the permission- * callback.+ * callback, which asks the user about each call to Trilium's note tools.*/-import type { ModelInfo } from "@triliumnext/core/src/services/llm/types.js";+import type { ToolApprovals } from "@triliumnext/core/src/services/llm/tool_approval.js";+import { allToolRegistries } from "@triliumnext/core/src/services/llm/tools/index.js";+import type { LlmProviderConfig, ModelInfo } from "@triliumnext/core/src/services/llm/types.js";-import { AcpAgentProvider, type AcpLaunchSpec, type AcpModel, type AcpSessionModelState, describeError, resetAcpAgentStateForTests } from "./acp_agent.js";+import { AcpAgentProvider, type AcpLaunchSpec, type AcpModel, type AcpPermissionOutcome, type AcpPermissionRequest, type AcpSessionModelState, type AnnouncedMcpCalls, answerNoteToolCalls, denyPermission, describeError, mcpArguments, NOTE_TOOLS_MCP_SERVER_NAME, resetAcpAgentStateForTests } from "./acp_agent.js";import { AcpError } from "./acp_client.js";import { needsShell } from "./binary_lookup.js";import { resolveCopilotBinaryPath } from "./copilot_binary.js";@@ -46,12 +48,9 @@ const premiumMultiplierById = new Map<string, number>();/*** CLI arguments. These form the *primary* security boundary; the permission- * callback (see `denyPermission` in acp_agent.ts) is a fail-closed backstop.+ * callback (see {@link decideCopilotPermission}) asks the user about note tools+ * and denies everything else.* - `--acp` runs the CLI as an ACP agent over stdio.- * - `--allow-tool=trilium` auto-approves every tool from our "trilium" MCP- * server, so note tools run without a permission round-trip (and without- * relying on us recognizing them — the CLI presents MCP tool calls with- * opaque IDs and human-friendly titles that don't embed the server name).* - `--deny-tool` on each built-in file/shell/network tool guarantees they* can never run even if a future CLI build changed the permission-prompt* defaults. The agent's only capability surface is Trilium's note tools.@@ -61,7 +60,6 @@ const premiumMultiplierById = new Map<string, number>();*/const COPILOT_ACP_ARGS = ["--acp",- "--allow-tool=trilium",...["shell", "powershell", "write", "edit", "create", "view", "glob", "grep", "task", "web_fetch"].map(t => `--deny-tool=${t}`),"--no-custom-instructions","--no-auto-update"@@ -98,6 +96,14 @@ export class CopilotAgentProvider extends AcpAgentProvider {return TITLE_MODEL;}+ protected decidePermission(+ request: AcpPermissionRequest,+ config?: LlmProviderConfig,+ mcpCalls?: AnnouncedMcpCalls+ ): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {+ return decideCopilotPermission(request, this.logLabel, { mcpCalls, toolApprovals: config?.toolApprovals });+ }+protected async launchSpec(): Promise<AcpLaunchSpec> {const binary = await resolveCopilotBinaryPath();return { binary, args: COPILOT_ACP_ARGS, shell: needsShell(binary) };@@ -173,6 +179,47 @@ export function buildCopilotModelList(remote: AcpModel[], curated: ModelInfo[]):return models.map(m => (m === preferred ? { ...m, isDefault: true } : m));}+/**+ * Ask the user about a call to Trilium's note tools through the turn's `toolApprovals`; deny+ * everything else.+ *+ * Copilot asks before every MCP tool call it was not told to allow. Its request marks neither the+ * server nor the tool: the call is recognized by its title, which Copilot fills in with the tool's+ * name, from the request or from the `tool_call` it announced under the same id. A title that+ * names the `trilium` server in front of the tool is recognized too. Only a name one of Trilium's+ * tool registries defines counts, so a built-in tool never reaches the user.+ */+export function decideCopilotPermission(+ request: AcpPermissionRequest,+ logLabel: string,+ { mcpCalls, toolApprovals }: { mcpCalls?: AnnouncedMcpCalls; toolApprovals?: ToolApprovals } = {}+): AcpPermissionOutcome | Promise<AcpPermissionOutcome> {+ const toolCall = request.toolCall;+ const announced = mcpCalls?.announcementOf(toolCall?.toolCallId);+ const tool = noteToolName(toolCall?.title) ?? noteToolName(announced?.title);+ if (!toolCall?.toolCallId || !tool) {+ return denyPermission(request, logLabel);+ }+ const call = {+ toolCallId: toolCall.toolCallId,+ server: NOTE_TOOLS_MCP_SERVER_NAME,+ tool,+ input: mcpArguments(toolCall.rawInput ?? announced?.rawInput)+ };+ return answerNoteToolCalls(request, logLabel, [ call ], toolApprovals);+}++/** The note tool a title names, bare or behind the `trilium` server's name. */+function noteToolName(title: string | undefined): string | undefined {+ const name = title?.trim().replace(NOTE_TOOLS_SERVER_PREFIX, "");+ return name && allToolRegistries.some(registry => [ ...registry ].some(([ toolName ]) => toolName === name))+ ? name+ : undefined;+}++/** `trilium-`, `trilium/`, `trilium.`, `trilium_`, `trilium: ` or `mcp__trilium__` in front of a tool name. */+const NOTE_TOOLS_SERVER_PREFIX = new RegExp(`^(?:mcp__)?${NOTE_TOOLS_MCP_SERVER_NAME}(?:__|[-/._]|:\\s*)`);+/** For tests: forget the probed catalog and the premium rates recorded from it. */export function resetModelCatalogCacheForTests(): void {resetAcpAgentStateForTests();diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.mdindex 13da2c1199..c59f3377f1 100644--- a/docs/User Guide/User Guide/AI.md+++ b/docs/User Guide/User Guide/AI.md@@ -177,8 +177,8 @@ 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. Searching the web and reading a web page never ask either: turning on web search in the _Tools_ menu is what allows them, whether the model searches on its own or through a search provider.-> [!IMPORTANT]-> 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]+> The <a class="reference-link" href="AI/Providers/Subscription-based%20providers.md">Subscription-based providers</a> (Claude Code, GitHub Copilot, Google Antigravity and OpenAI Codex) ask like any other provider.> [!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 4324668ed7..947689ac49 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 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.+ * 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. 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.