From f07e9f2e3d68021d79fb92f0abcf2e6ac957e0f9 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Sun, 15 Nov 2026 22:17:15 +0000 Subject: [PATCH] docs(backends): record why Claude reports no tool execution evidence (#2875) A journaled Claude turn loads project settings and .mcp.json, the default brain runs the brain MCP server for the whole session, the CLI reloads settings, skills and plugins mid-session, and the host cannot read the effective hook set. No call is proven to have run no project hook or brain-owned MCP code, so every Claude result stays observed. Guard both sides: the backend reports no evidence with or without a project hook and brain MCP server configured, and the host classifies any evidence for a Claude tool name as unproven. Co-Authored-By: Claude Opus 5.5 --- docs/decisions/governed-writable-workers.md | 99 ++++++++++++++++++- .../tests/tool-execution-evidence.test.ts | 93 +++++++++++++++++ .../tests/tool-observation-schedule.test.ts | 4 + 3 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 packages/ui-backend-claude/tests/tool-execution-evidence.test.ts diff --git a/docs/decisions/governed-writable-workers.md b/docs/decisions/governed-writable-workers.md index 74fbc83cac..7e4a9fde8d 100644 --- a/docs/decisions/governed-writable-workers.md +++ b/docs/decisions/governed-writable-workers.md @@ -1525,7 +1525,7 @@ evidence is unproven. | Pi `read_file` (direct worker read) | The curated `readFileSync` in the worker | The governed `read_start` and `read_end` RPCs for that call, and no lock | Not observed | | Pi `write_file`, `edit_file` (direct worker writers) | The curated write under its path lock | The lock RPC plus `write_check` and `write_end` | Observed | | `bash`, `grep`, `brain_search`, `brain_context`, `brain_read`, `brain_list`, `brain_graph`, `brain_read_many`, extension and subagent tools, anything else | A subprocess, or brain-owned code: `brain.config.ts` evaluated in the worker by the first index query (`async function ensureContext`, `packages/ui-backend-pi/src/brain-access.ts:175`), which stays loaded. Index readers also run a host index barrier first, which can write `.brain-index/` | None | Observed, and every later result of the turn is observed too | -| Every Claude tool | Built-ins run in the worker next to project settings hooks and the brain's own MCP server, which can run brain-owned code on any tool event | None reported | Observed, as before | +| Every Claude tool | Built-ins run in the worker next to project settings hooks and the brain's own MCP server, which can run brain-owned code on any tool event | None reported; no proof condition holds for a journaled Claude turn ([#2875](#claude-tool-execution-evidence--2026-11-15-2875)) | Observed, as before | The pi backend reports evidence only from its own record of each call's RPCs. A call that names two tools, takes the write lock as a read, or reports only @@ -1599,6 +1599,103 @@ automatic refresh (#2873, since made metadata-only; see below). Neither can be cut by scheduling without weakening capture, so these targets are reported as unmet rather than relaxed. +### Claude tool execution evidence — 2026-11-15 (#2875) + +**Conclusion: negative.** The Claude backend reports no +`StartTurnRequest.toolExecution` evidence, and every Claude tool result stays +observed. No condition the host can check proves that a journaled Claude call +ran no project hook and no brain-owned MCP code. This was checked against the +installed Claude Agent SDK 0.3.293 and its bundled CLI (build 2026-10-07). + +**The proof conditions.** A Claude read could skip its observation only if +all of these held for the whole session, as the pi backend's extension-free +worker does: + +1. No hook runs from any source the CLI loads: managed policy, project + settings, plugins, or agent and skill definitions. +2. No MCP server runs except the host's in-process `brain-ui` server. That + rules out the brain's own `brain mcp` server and any other `.mcp.json`, + plugin or agent-frontmatter server. +3. Neither of the first two can change during the session, including + through reloads and code a turn introduces. +4. The host verifies all three from the runtime it launched, not from a + configured flag or from documentation. + +**Why none of them holds.** + +- **Only interactive turns are journaled, and they load project + configuration.** Evidence is wired only into the session-run turn request + for a governed turn with a journal (`export function turnRequest`, + `packages/ui-server/src/ws/session-run/backend-entry.ts:203-218`). That turn + is never a restricted autonomous one. It runs with `settingSources: + ["project"]` and without `strictMcpConfig` + (`const sdkOptions: Options`, `packages/ui-backend-claude/src/sdk-options.ts:406-422`). + Only the restricted envelope reads no settings and uses no project MCP + configuration (`function applyTurnSettings`, + `packages/ui-backend-claude/src/sdk-options.ts:615-624`), and no journaled + turn uses that envelope. Without `strictMcpConfig`, the SDK applies project + `.mcp.json`, plugin and on-disk agent-frontmatter MCP servers (the installed + `sdk.d.ts`, `strictMcpConfig`). +- **The default brain runs brain-owned code for the whole session.** The + template's `.mcp.json` declares the `brain` server, `bun + node_modules/.bin/brain mcp`. It starts with the CLI, before any call. It + evaluates `brain.config.ts` and its modules, opens `brain.db`, registers + module tools, and advances compute jobs it started for as long as it lives + (`export async function startMcpServer`, + `packages/core/src/mcp-server.ts:723-795`). That is the Claude counterpart of + a pi worker with extensions loaded, which reports nothing. +- **The CLI reloads configuration during the session.** The bundled CLI has + a settings change detector that watches the settings files for additions, + changes and deletions, a skill change detector, a plugin-directory watch and + a managed-settings poll. Each re-reads its source mid-session, subject only + to a `ConfigChange` hook. The SDK lists `user_settings`, `project_settings`, + `local_settings`, `policy_settings` and `skills` as sources that can change + (the installed `sdk.d.ts`, `ConfigChangeHookInput`). Settings checked at + launch therefore do not describe a later call. +- **The host cannot read the effective hook set.** Managed hooks run even + under a non-managed `disableAllHooks` (the installed `sdk.d.ts`, + `policyHookCount`). The SDK's `Query` exposes no method that returns the + hooks in force. `get_hooks_listing` exists only as an internal control + request subtype, and it would be a snapshot that a reload can make stale. + `includeHookEvents` streams hook lifecycle messages, but the SDK documents + it as an output option, not as a complete or ordered record. Hooks on + events other than tool use, such as `ConfigChange`, `FileChanged` and + `InstructionsLoaded`, also run outside any call. +- **Protection does not cover every input.** `.claude/settings.json`, + `.claude/settings.local.json` and `.mcp.json` are tier B, which the policy + may release (`export const PROTECTED_TIER_B`, + `packages/common/src/protected-paths.ts:25-34`). `.claude/skills/`, + `.claude/agents/` and the scripts a hook command runs are not protected. + Code a turn writes there itself is covered: the call that writes it is + unproven, so every later result of that turn is observed. A change by + another writer outside every active turn's calls is not covered. That + includes an external editor or host maintenance, and the CLI would pick the + change up by reloading. + +**What is guarded.** The backend never calls `toolExecution`. A scripted +Claude turn with a project `PostToolUse` hook and the brain's `.mcp.json` +server, and one with neither at turn start, runs `Read`, `Grep`, `Glob`, two +`mcp__brain-ui__` reads and `mcp__brain__brain_search` to their results. The +test fails if any of them reports evidence +(`packages/ui-backend-claude/tests/tool-execution-evidence.test.ts`). On the +host, evidence reported for a Claude tool name classifies as unproven, because +the classified names are the pi and bridge names +(`export function toolObservation`, +`packages/ui-server/src/brain/tool-observation.ts:37-42`). The schedule test +covers both points. + +**Not measured.** No Claude read qualifies, so +`scripts/prove-native-host.ts --observation-timing` has no proven Claude read +to time. Claude's per-result observations are unchanged from #2834. + +**What would reopen this.** The question can be reopened if a later runtime +gives the host a verified, session-stable hook and MCP server set: a public +query of the effective set together with a way to pin it against reloads, or +a launch mode that loads none. Even then, it would apply only to a brain whose +`.mcp.json` declares no `brain` server. Turning hooks off for interactive +turns, or dropping the brain MCP server from them, would change what a brain's +own configuration does, and that needs a maintainer ruling first. + ### Metadata-only index history — 2026-10-25 (#2873) > **Maintainer ruling (2026-10-25), Option B.** Record metadata only for the diff --git a/packages/ui-backend-claude/tests/tool-execution-evidence.test.ts b/packages/ui-backend-claude/tests/tool-execution-evidence.test.ts new file mode 100644 index 0000000000..9fa06ca085 --- /dev/null +++ b/packages/ui-backend-claude/tests/tool-execution-evidence.test.ts @@ -0,0 +1,93 @@ +import { mockWorkerHostForSdkStream } from "./helpers/worker-host.js"; +/** + * #2875: the Claude backend reports no `StartTurnRequest.toolExecution` + * evidence. A hosted interactive Claude turn loads the brain's project + * settings and `.mcp.json`, and the CLI keeps watching them, so no call in it + * is proven to have run no project hook or brain-owned MCP code + * (docs/decisions/governed-writable-workers.md, "Claude tool execution + * evidence — 2026-11-15 (#2875)"). Every Claude result therefore stays + * observed. This fails if any call, read or not, reports evidence. + */ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { Options, query } from "@anthropic-ai/claude-agent-sdk"; +import type { BackendBridge, ServerMessage, StartTurnRequest } from "@schlessera/brain-ui-sdk/server"; + +import { createClaudeBackend } from "../src/backend"; + +mockWorkerHostForSdkStream(); + +/** The calls a proven read could plausibly have covered: built-in reads, host bridge reads, and the brain's own MCP server. */ +const CALLS: [id: string, name: string, input: Record][] = [ + ["read-1", "Read", { file_path: "notes/loom.md" }], + ["grep-1", "Grep", { pattern: "Penelope" }], + ["glob-1", "Glob", { pattern: "notes/*.md" }], + ["base-1", "mcp__brain-ui__brain_read_base", { path: "notes/loom.md" }], + ["activity-1", "mcp__brain-ui__query_activity", {}], + ["search-1", "mcp__brain__brain_search", { query: "loom" }], +]; + +function* scripted(sessionId: string) { + yield { type: "system", subtype: "init", session_id: sessionId }; + for (const [id, name, input] of CALLS) { + yield { type: "stream_event", session_id: sessionId, event: { type: "content_block_start", content_block: { type: "tool_use", id, name } } }; + yield { type: "assistant", session_id: sessionId, message: { content: [{ type: "tool_use", id, name, input }] } }; + yield { type: "user", session_id: sessionId, message: { content: [{ type: "tool_result", tool_use_id: id, content: "Penelope weaves by day." }] } }; + } + yield { type: "result", subtype: "success", session_id: sessionId, is_error: false, result: "done", num_turns: CALLS.length, duration_ms: 1, + total_cost_usd: 0, usage: { input_tokens: 1, output_tokens: 1, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 } }; +} + +describe("Claude tool execution evidence (#2875)", () => { + let root: string; + beforeEach(() => { root = mkdtempSync(join(tmpdir(), "odysseus-claude-evidence-")); }); + afterEach(() => { rmSync(root, { recursive: true, force: true }); }); + + function brain(name: string, configure: (dir: string) => void): string { + const dir = join(root, name); + mkdirSync(join(dir, "notes"), { recursive: true }); + writeFileSync(join(dir, "notes", "loom.md"), "Penelope weaves by day.\n"); + configure(dir); + return dir; + } + + async function run(brainPath: string) { + const frames: ServerMessage[] = [], evidence: [string, string][] = []; + let options: Options | undefined; + const bridge: BackendBridge = { emit: frame => frames.push(frame), requestPermission: async () => ({ behavior: "allow" }) }; + const queryFn = ((params: { options?: Options }) => { + options = params.options; + return (async function* () { yield* scripted("ithaca-evidence"); })(); + }) as unknown as typeof query; + const req: StartTurnRequest = { prompt: "Read the loom note.", signal: new AbortController().signal, bridge, + toolExecution: (toolUseId, path) => { evidence.push([toolUseId, path]); } }; + await createClaudeBackend({ brainPath, queryFn }).startTurn(req); + return { frames, evidence, options: options! }; + } + + const ithacaHooked = (dir: string) => { + mkdirSync(join(dir, ".claude"), { recursive: true }); + writeFileSync(join(dir, ".claude", "settings.json"), JSON.stringify({ + hooks: { PostToolUse: [{ matcher: "Read", hooks: [{ type: "command", command: "node .claude/hooks/tally-suitors.mjs" }] }] }, + })); + writeFileSync(join(dir, ".mcp.json"), JSON.stringify({ mcpServers: { brain: { command: "bun", args: ["node_modules/.bin/brain", "mcp"] } } })); + }; + + for (const [label, configure] of [ + ["with a project hook and the brain's project MCP server", ithacaHooked], + ["with neither configured at turn start", () => {}], + ] as const) { + test(`no call reports evidence ${label}`, async () => { + const dir = brain(label.includes("hook") ? "hooked" : "bare", configure); + const { frames, evidence, options } = await run(dir); + // The turn really loads the brain's project settings and .mcp.json, which the CLI watches for the session. + expect([options.cwd, options.settingSources, options.strictMcpConfig ?? false], "CLAUDE_LOADS_PROJECT_SETTINGS").toEqual([dir, ["project"], false]); + // Every call ran to its result, so evidence had every chance to be reported. + expect(frames.filter(frame => frame.type === "tool_result").map(frame => frame.type === "tool_result" ? frame.toolUseId : null), "CLAUDE_EVERY_RESULT_EMITTED") + .toEqual(CALLS.map(([id]) => id)); + expect(evidence, "CLAUDE_REPORTS_NO_TOOL_EXECUTION_EVIDENCE").toEqual([]); + }); + } +}); diff --git a/packages/ui-server/tests/tool-observation-schedule.test.ts b/packages/ui-server/tests/tool-observation-schedule.test.ts index 3ab5266a56..4a709636b2 100644 --- a/packages/ui-server/tests/tool-observation-schedule.test.ts +++ b/packages/ui-server/tests/tool-observation-schedule.test.ts @@ -27,6 +27,10 @@ test("only a proven read skips its result's observation; unknown, bash and write // A name or risk label alone is never evidence. ["read_file", undefined, "unproven"], ["brain_search", undefined, "unproven"], ["query_activity", undefined, "unproven"], ["bash", undefined, "unproven"], ["Read", undefined, "unproven"], ["mcp__brain-ui__query_activity", undefined, "unproven"], [null, "host_bridge", "unproven"], + // #2875: Claude calls have no proven path, so even reported evidence proves nothing for a Claude name. + ["Read", "worker_read", "unproven"], ["Grep", "worker_read", "unproven"], ["Glob", "worker_read", "unproven"], ["Write", "worker_write", "unproven"], + ["mcp__brain-ui__brain_read_base", "host_bridge", "unproven"], ["mcp__brain-ui__query_activity", "host_bridge", "unproven"], + ["mcp__brain__brain_search", "host_bridge", "unproven"], ["mcp__brain__brain_read", "worker_read", "unproven"], ]; for (const [name, path, expected] of cases) expect([name, path, toolObservation(name, path)], "TOOL_OBSERVATION_CLASS").toEqual([name, path, expected]); });