diff --git a/.changeset/3788-in-worker-apify-disallowed.md b/.changeset/3788-in-worker-apify-disallowed.md new file mode 100644 index 0000000000..9c99021d70 --- /dev/null +++ b/.changeset/3788-in-worker-apify-disallowed.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-backend-claude": patch +--- + +Fix (#3788): a hosted Claude turn no longer sees the in-worker core server's `mcp__brain__brain_apify_*` tools. A worker holds neither the Apify token nor the ledger, so those copies could only refuse with `ledger_unset`, under the same names and descriptions as the working `mcp__brain-ui__brain_apify_*` tools. Every Claude session now disallows them, whatever the configuration, as it already does the in-worker compute tools. The host's Apify tools and standalone `brain mcp` are unchanged. diff --git a/docs/decisions/apify-capability.md b/docs/decisions/apify-capability.md index 288458697c..aa42babeb9 100644 --- a/docs/decisions/apify-capability.md +++ b/docs/decisions/apify-capability.md @@ -407,6 +407,14 @@ both chat backends; none widens it. lock. Nothing is asked on a terminal: only an exact grant admits a run. Neither variable reaches a worker, so a shell call inside a chat turn refuses. +- **A chat turn sees only the host's copies (#3788).** An interactive Claude + turn loads the brain's `.mcp.json`, whose core server registers the same + `brain_apify_*` names and descriptions. Inside a worker those copies can + only refuse with `ledger_unset`, so every Claude session disallows the + `mcp__brain__brain_apify_*` names, whatever the configuration, as it does + the in-worker compute tools; the tool list never depends on it + ([provider-history.md](provider-history.md)). Pi has no MCP client: its + `brain_apify_*` tools are the host bridge's. - **Two additive codes.** `provider_unavailable` (the API could not be reached or verified, so nothing was admitted) keeps that case distinct from an incompatible answer; `invalid_input` is the operation's own refusal before diff --git a/packages/ui-backend-claude/src/sdk-options.ts b/packages/ui-backend-claude/src/sdk-options.ts index 9ffc940c49..d47d538c8e 100644 --- a/packages/ui-backend-claude/src/sdk-options.ts +++ b/packages/ui-backend-claude/src/sdk-options.ts @@ -51,6 +51,15 @@ import type { TurnLockBinding } from "./turn-lock.js"; * tool list never depends on configuration (provider-history.md). */ export const IN_WORKER_COMPUTE_TOOLS: readonly string[] = Object.freeze(COMPUTE_TOOL_CONTRACTS.map(contract => `mcp__brain__${contract.name}`)); +/** + * The in-worker brain MCP server's fixed Apify tools (#3788). That server + * registers `brain_apify_*` unconditionally, but a worker holds neither the + * token nor the ledger (`HOST_ONLY_SECRET_ENV`), so every call there refuses + * with `ledger_unset`, under the same names and descriptions as the host's + * working `mcp__brain-ui__brain_apify_*` tools. Disallowed in every session, + * like compute, so the tool list never depends on configuration. + */ +export const IN_WORKER_APIFY_TOOLS: readonly string[] = Object.freeze(Object.values(APIFY_TOOL_NAMES).map(name => `mcp__brain__${name}`)); /** The prompt-brief keys of the compute contracts, as `SurfaceTools` names them. */ const COMPUTE_SURFACE_KEYS = [["computeHealth", "brain_compute_health"], ["computePlan", "brain_compute_plan"], ["computeRun", "brain_compute_run"], ["computeStatus", "brain_compute_status"], ["computeCancel", "brain_compute_cancel"], ["computeCollect", "brain_compute_collect"]] as const; @@ -424,7 +433,7 @@ function assembleClaudeSdkTurn( allowedTools: allowed, // The built-in AskUserQuestion picker needs a TTY; keep it disabled even // when no ask-user handler is present. - disallowedTools: ["AskUserQuestion", ...IN_WORKER_COMPUTE_TOOLS, ...((hosted || req.autonomous) ? [ + disallowedTools: ["AskUserQuestion", ...IN_WORKER_COMPUTE_TOOLS, ...IN_WORKER_APIFY_TOOLS, ...((hosted || req.autonomous) ? [ "mcp__brain__brain_add", "mcp__brain__brain_update", "mcp__brain__brain_archive", ] : []), ...(hosted && req.autonomous && (sandbox.profile !== "governed" || req.posture === "voice") ? ["Write", "Edit", "NotebookEdit"] : [])], ...createPermissionWiring({ diff --git a/packages/ui-backend-claude/tests/fixtures/sdk-options-snapshot.json b/packages/ui-backend-claude/tests/fixtures/sdk-options-snapshot.json index 3570dfa7b5..1b62265c14 100644 --- a/packages/ui-backend-claude/tests/fixtures/sdk-options-snapshot.json +++ b/packages/ui-backend-claude/tests/fixtures/sdk-options-snapshot.json @@ -39,6 +39,16 @@ "mcp__brain__brain_compute_status", "mcp__brain__brain_compute_cancel", "mcp__brain__brain_compute_collect", + "mcp__brain__brain_apify_availability", + "mcp__brain__brain_apify_approvals", + "mcp__brain__brain_apify_revoke", + "mcp__brain__brain_apify_discover", + "mcp__brain__brain_apify_inspect", + "mcp__brain__brain_apify_prepare", + "mcp__brain__brain_apify_run", + "mcp__brain__brain_apify_status", + "mcp__brain__brain_apify_results", + "mcp__brain__brain_apify_abort", "mcp__brain__brain_add", "mcp__brain__brain_update", "mcp__brain__brain_archive" @@ -225,6 +235,16 @@ "mcp__brain__brain_compute_status", "mcp__brain__brain_compute_cancel", "mcp__brain__brain_compute_collect", + "mcp__brain__brain_apify_availability", + "mcp__brain__brain_apify_approvals", + "mcp__brain__brain_apify_revoke", + "mcp__brain__brain_apify_discover", + "mcp__brain__brain_apify_inspect", + "mcp__brain__brain_apify_prepare", + "mcp__brain__brain_apify_run", + "mcp__brain__brain_apify_status", + "mcp__brain__brain_apify_results", + "mcp__brain__brain_apify_abort", "mcp__brain__brain_add", "mcp__brain__brain_update", "mcp__brain__brain_archive" @@ -400,7 +420,17 @@ "mcp__brain__brain_compute_run", "mcp__brain__brain_compute_status", "mcp__brain__brain_compute_cancel", - "mcp__brain__brain_compute_collect" + "mcp__brain__brain_compute_collect", + "mcp__brain__brain_apify_availability", + "mcp__brain__brain_apify_approvals", + "mcp__brain__brain_apify_revoke", + "mcp__brain__brain_apify_discover", + "mcp__brain__brain_apify_inspect", + "mcp__brain__brain_apify_prepare", + "mcp__brain__brain_apify_run", + "mcp__brain__brain_apify_status", + "mcp__brain__brain_apify_results", + "mcp__brain__brain_apify_abort" ], "systemPrompt": { "type": "preset", @@ -593,6 +623,16 @@ "mcp__brain__brain_compute_status", "mcp__brain__brain_compute_cancel", "mcp__brain__brain_compute_collect", + "mcp__brain__brain_apify_availability", + "mcp__brain__brain_apify_approvals", + "mcp__brain__brain_apify_revoke", + "mcp__brain__brain_apify_discover", + "mcp__brain__brain_apify_inspect", + "mcp__brain__brain_apify_prepare", + "mcp__brain__brain_apify_run", + "mcp__brain__brain_apify_status", + "mcp__brain__brain_apify_results", + "mcp__brain__brain_apify_abort", "mcp__brain__brain_add", "mcp__brain__brain_update", "mcp__brain__brain_archive" @@ -782,6 +822,16 @@ "mcp__brain__brain_compute_status", "mcp__brain__brain_compute_cancel", "mcp__brain__brain_compute_collect", + "mcp__brain__brain_apify_availability", + "mcp__brain__brain_apify_approvals", + "mcp__brain__brain_apify_revoke", + "mcp__brain__brain_apify_discover", + "mcp__brain__brain_apify_inspect", + "mcp__brain__brain_apify_prepare", + "mcp__brain__brain_apify_run", + "mcp__brain__brain_apify_status", + "mcp__brain__brain_apify_results", + "mcp__brain__brain_apify_abort", "mcp__brain__brain_add", "mcp__brain__brain_update", "mcp__brain__brain_archive" @@ -872,6 +922,16 @@ "mcp__brain__brain_compute_status", "mcp__brain__brain_compute_cancel", "mcp__brain__brain_compute_collect", + "mcp__brain__brain_apify_availability", + "mcp__brain__brain_apify_approvals", + "mcp__brain__brain_apify_revoke", + "mcp__brain__brain_apify_discover", + "mcp__brain__brain_apify_inspect", + "mcp__brain__brain_apify_prepare", + "mcp__brain__brain_apify_run", + "mcp__brain__brain_apify_status", + "mcp__brain__brain_apify_results", + "mcp__brain__brain_apify_abort", "mcp__brain__brain_add", "mcp__brain__brain_update", "mcp__brain__brain_archive" diff --git a/packages/ui-backend-claude/tests/sdk-options-snapshot.test.ts b/packages/ui-backend-claude/tests/sdk-options-snapshot.test.ts index 060ae77098..f6474fe9ad 100644 --- a/packages/ui-backend-claude/tests/sdk-options-snapshot.test.ts +++ b/packages/ui-backend-claude/tests/sdk-options-snapshot.test.ts @@ -19,6 +19,8 @@ import { SNAPSHOT_MODES, sdkOptionsSnapshot } from "./helpers/sdk-options-snapsh * those four modes; provider tools, prompts and autonomous modes stay unchanged. * #3624 re-records only the tool-description digest for approved Actor reuse * and the user-selected shortlist; tool names, allowlists and prompts stay fixed. + * #3788 adds the ten in-worker `mcp__brain__brain_apify_*` names to every mode's + * `disallowedTools`; nothing else moves. * Regenerate it only for a reviewed, intended change to * the roster, allowlist or system prompt, and say so in the PR. */ diff --git a/packages/ui-backend-claude/tests/stable-roster.test.ts b/packages/ui-backend-claude/tests/stable-roster.test.ts index 6558b7e74d..4ce61b6a14 100644 --- a/packages/ui-backend-claude/tests/stable-roster.test.ts +++ b/packages/ui-backend-claude/tests/stable-roster.test.ts @@ -6,18 +6,21 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js"; import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; import { createClaudeSdkTurn } from "../src/sdk-options.js"; import { createPermissionWiring } from "../src/permission-hooks.js"; +import { APIFY_TOOL_NAMES } from "@schlessera/brain-ui-sdk/server"; const refused = ["request_image_mask", "ask_user_list", "ask_user_rank", "ask_user_form", "propose_policy_change", "inbox_add", "resolve_action", "complete_share"]; const bridge = () => Object.fromEntries(["askUser", "askUserList", "askUserRank", "askUserForm", "requestMask", "proposePolicyChange", "inboxList", "inboxGet", "inboxAdd", "resolveAction", "completeShare", "queryActivity", "getLocation", "applyBrain", "readBrainBase", "compute"].map(name => [name, async () => { throw new Error("PRIVILEGED_HANDLER_EXECUTED"); }])); +// The in-worker core server's copies of every fixed Apify operation (#3788), derived from the contract so a new operation is covered. +const inWorkerApify = Object.values(APIFY_TOOL_NAMES).map(name => `mcp__brain__${name}`); test("Claude complete tool schemas and native exclusions stay identical voice-first, typed, missing-handler and strict", async () => { const brain = mkdtempSync(join(tmpdir(), "odysseus-roster-")); const rosters: string[] = [], exclusions: string[] = [], appends: string[] = []; try { - for (const mode of ["voice", "typed", "missing", "strict", "no-grant", "unhosted", "partial"] as const) { + for (const mode of ["voice", "typed", "missing", "strict", "no-grant", "unhosted", "partial", "apify"] as const) { const turn = createClaudeSdkTurn({ backend: { brainPath: brain }, req: { prompt: "Return to Ithaca", signal: new AbortController().signal, - bridge: (["missing", "unhosted"].includes(mode) ? {} : mode === "partial" ? { ...bridge(), inboxList: undefined } : bridge()) as never, + bridge: (["missing", "unhosted"].includes(mode) ? {} : mode === "partial" ? { ...bridge(), inboxList: undefined } : mode === "apify" ? { ...bridge(), apify: async () => { throw new Error("PRIVILEGED_HANDLER_EXECUTED"); } } : bridge()) as never, ...(mode !== "unhosted" ? { applicationAuthority: "server" as const } : {}), ...(mode === "voice" ? { posture: "voice", enforceAllowedTools: true } : {}), noGrantSurface: mode === "voice" || mode === "no-grant", @@ -39,7 +42,10 @@ test("Claude complete tool schemas and native exclusions stay identical voice-fi // The in-worker compute tools are disallowed in every session, whatever the host configured (#3108). for (const name of ["health", "plan", "run", "status", "cancel", "collect"]) expect(turn.options.disallowedTools, `IN_WORKER_COMPUTE_DISALLOWED_${mode}_${name}`).toContain(`mcp__brain__brain_compute_${name}`); - rosters.push(JSON.stringify(tools)); exclusions.push(JSON.stringify(turn.options.disallowedTools?.filter(name => ["Write", "Edit", "NotebookEdit"].includes(name) || name.includes("compute")))); + // So are the in-worker Apify tools: a worker holds neither the token nor the ledger, so they only refuse (#3788). + expect(inWorkerApify.filter(name => !turn.options.disallowedTools?.includes(name)), `IN_WORKER_APIFY_DISALLOWED_${mode}`).toEqual([]); + if (mode === "apify") expect(turn.options.allowedTools, "HOST_APIFY_UNAFFECTED").toContain("mcp__brain-ui__brain_apify_run"); + rosters.push(JSON.stringify(tools)); exclusions.push(JSON.stringify(turn.options.disallowedTools?.filter(name => ["Write", "Edit", "NotebookEdit"].includes(name) || name.includes("compute") || name.includes("apify")))); appends.push(JSON.stringify(turn.options.systemPrompt)); } finally { await client.close(); await turn.finishWorker(); } } @@ -50,6 +56,29 @@ test("Claude complete tool schemas and native exclusions stay identical voice-fi } finally { rmSync(brain, { recursive: true, force: true }); } }); +for (const autonomous of [false, true]) for (const hosted of [false, true]) for (const apify of [false, true]) for (const posture of ["typed", "voice"] as const) + test(`Claude ${autonomous ? "autonomous" : "interactive"} ${hosted ? "hosted" : "unhosted"} ${posture} turn with Apify ${apify ? "enabled" : "disabled"} disallows every in-worker Apify tool`, async () => { + const brain = mkdtempSync(join(tmpdir(), "odysseus-apify-roster-")); + try { + const turn = createClaudeSdkTurn({ backend: { brainPath: brain }, req: { + prompt: "Count the cattle of the sun", signal: new AbortController().signal, + bridge: { ...bridge(), ...(apify ? { apify: async () => { throw new Error("PRIVILEGED_HANDLER_EXECUTED"); } } : {}) } as never, + ...(hosted ? { applicationAuthority: "server" as const } : {}), + ...(posture === "voice" ? { posture: "voice", enforceAllowedTools: true } : {}), + ...(autonomous ? { enforceAllowedTools: true, noGrantSurface: true, autonomous: { origin: "autonomous", persistence: "none", containment: "restricted", allowedTools: [], systemPromptAppend: "Server snapshot." } } : {}), + workerSandbox: { brain, profile: "governed", restricted: false, areas: [], scratchAliasesRemoved: [] }, + } as never, profile: { id: "fixture", label: "Fixture", requiredEnvKeys: [], ...(autonomous ? { billing: "api" } : {}), + buildEnv: () => autonomous ? { ANTHROPIC_API_KEY: "sk-odysseus-fixture", ANTHROPIC_AUTH_TOKEN: "", CLAUDE_CODE_OAUTH_TOKEN: "" } : {} } as never, + abortController: new AbortController(), allowedTools: [], confirmPatterns: [], + turnLock: { acquireForTool: async () => ({ ok: true }), releaseForTool() {} } as never, log() {} }); + try { + expect(inWorkerApify.filter(name => !turn.options.disallowedTools?.includes(name)), "IN_WORKER_APIFY_DISALLOWED").toEqual([]); + // The host's own Apify tools are never caught by the exclusion. + expect(turn.options.disallowedTools?.filter(name => name.startsWith("mcp__brain-ui__")), "HOST_TOOLS_NOT_DISALLOWED").toEqual([]); + } finally { await turn.finishWorker(); } + } finally { rmSync(brain, { recursive: true, force: true }); } + }); + for (const mode of ["voice", "no-grant", "missing"] as const) for (const name of refused) test(`Claude ${mode} refuses ${name} before approval or handler`, async () => { let cards = 0; const req = { prompt: "Return to Ithaca", signal: new AbortController().signal, diff --git a/tests/installed-turn-surface.test.ts b/tests/installed-turn-surface.test.ts index 23d17e4ba8..15aaa40a12 100644 --- a/tests/installed-turn-surface.test.ts +++ b/tests/installed-turn-surface.test.ts @@ -19,6 +19,8 @@ test("actual installed CLI request preserves shipped deferred core tools, eager "mcp__brain__brain_add", // The core server's fixed Apify tools (#3162): deferred like every core tool, and refused in a worker, // which holds neither the token nor the ledger; the turn's own Apify tools are the eager bridge ones. + // Every session disallows them, like the in-worker compute tools (#3788). This capture serves core + // in-process, where the CLI still lists a disallowed tool; over the `.mcp.json` stdio server it drops them. "mcp__brain__brain_apify_abort", "mcp__brain__brain_apify_approvals", "mcp__brain__brain_apify_availability", "mcp__brain__brain_apify_discover", "mcp__brain__brain_apify_inspect", "mcp__brain__brain_apify_prepare", "mcp__brain__brain_apify_results", "mcp__brain__brain_apify_revoke", "mcp__brain__brain_apify_run", "mcp__brain__brain_apify_status", "mcp__brain__brain_archive",