Daemon

Approvals

How an agent's tool call waits for a decision from code or from a human, and how a viewer answers it safely.

An approval is a runner asking for a decision on one tool call. Claude Code asks through the Agent SDK's canUseTool; the fake runner asks when a step has approval: true.

What Claude Code asks about

The claude-code runner starts Claude Code in permission mode default, so every edit and command reaches canUseTool and becomes an approval_requested. Settings files cannot change that:

  • No settings from the cwd. settingSources is ["user"]. The project and local files (.claude/settings.json, .claude/settings.local.json) are never read, because the repo or the agent itself can write them. Their allow rules, defaultMode, hooks and MCP servers have no effect. Neither does a project CLAUDE.md; put instructions in the prompt or systemPrompt.
  • The user source is a sanitized copy. It is the daemon's own config dir, whose settings.json is your ~/.claude/settings.json, copied read-only at every spawn, minus permissions.allow, permissions.defaultMode, permissions.additionalDirectories, hooks, statusLine, enabledPlugins, extraKnownMarketplaces, enableAllProjectMcpServers and enabledMcpjsonServers. So an allow rule you keep for your own claude does not skip the daemon's approvals.
  • Hooks and allow rules only from managed settings. Every Claude Code process gets allowManagedHooksOnly and allowManagedPermissionRulesOnly as policy-tier settings, so a hook, or a skill's allowed-tools, that an agent writes into the config dir it can write does not run or grant anything. Deny and ask rules from your settings are ignored too under that setting: the daemon asks instead. On a machine whose administrator ships managed settings, Claude Code drops these unless the administrator opts in, and the administrator's policy governs hooks and rules.
  • No MCP servers from disk (strictMcpConfig): .mcp.json and the config dir's .claude.json start nothing.
  • No definitions a session wrote. The config dir's agents/, skills/, commands/, rules/, output-styles/, themes/, workflows/, monitors/, routines/, hooks/, plugins/ and dev-mods/ are recreated empty and read-only at every spawn, so a subagent file with permissionMode: acceptEdits, or a skill with allowed-tools, that one session planted never reaches the next. CLAUDE_CODE_RESTRICT_PERSONAL_CONFIG=1 is set as well (after the session's env, so it cannot be unset): user-scope agents cannot widen their permission mode or define MCP servers, and user-scope skills and commands cannot pre-approve tools.

Only the in-process controller can widen this, on purpose: the permissionMode and allowedTools runner options are not on the wire (session.start drops unknown fields), so no viewer or remote controller can set them. allowedTools runs those tools without asking and turns off allowManagedPermissionRulesOnly for that session; permissionMode: "bypassPermissions" never asks at all.

Order of decision

  1. Policy. The supervisor's approvals option runs first: a function, or any object with evaluate(action). It returns "allow", "deny", "ask", or { effect, reason?, updatedInput? }.
  2. Humans. On "ask", or with no policy, the session emits approval_requested and its status becomes waiting_approval. Any controller may answer.
  3. First answer wins. Later answers fail with daemon.approval_already_resolved, with the winning decision in error.details.
  4. Fail closed. No answer within approvalTimeoutMs (default 5 minutes) denies it, resolved_by: "timeout". A session that ends first denies its open approvals, resolved_by: "session_ended".

A denial reaches the agent as a failed tool call (error.type: "approval_denied"); the agent decides what to do next.

Answering from a viewer

approval_requested carries approval_id, action (tool_name, args, call_id), action_digest and expires_at. Answer with approve or deny on the RemoteSession, and pass:

  • actionDigest: the digest of the action you showed, echoed from approval_requested, pending_approvals or session.approval(). Required to allow (a deny may leave it out). Without it, or if the daemon's action differs, the daemon refuses with daemon.approval_action_mismatch and the approval stays open.
  • actor: who decided, for the record (for example the human behind a shared console). The daemon records and echoes it but cannot verify it. The authenticated identity is resolved_by: "peer:<peer_id>".
examples/ts/approval-round-trip/main.ts
const session = await client.startSession({ runner: "agent", cwd: os.tmpdir(), prompt: "clean up" });
const seen: SessionEvent[] = [];
for await (const event of session.events()) {
  seen.push(event);
  if (event.type === "approval_requested") {
    const { tool_name, args } = event.action;
    console.log(`asked  ${event.approval_id} ${tool_name} ${JSON.stringify(args)} ${short(event.action_digest)}`);
    // Bind the answer to what this viewer showed: the daemon refuses it if the action differs.
    const answer = { actor: "alice", actionDigest: event.action_digest };
    if (args.command === "rm -rf build") {
      const result = await session.approve(event.approval_id, answer);
      console.log(`answer ${result.decision} by ${result.resolved_by === `peer:${client.peerId}` ? "this peer" : "?"}`);
    } else {
      await demoMismatch(event.approval_id);
      const result = await session.deny(event.approval_id, { ...answer, reason: "No force pushes." });
      console.log(`answer ${result.decision} by ${result.resolved_by === `peer:${client.peerId}` ? "this peer" : "?"}`);
    }
  }
  if (event.type === "approval_resolved") {
    console.log(`event  approval_resolved ${event.decision} actor=${event.actor} reason=${event.reason ?? "-"}`);
  }
  if (event.type === "tool_call") console.log(`event  tool ${event.tool_name} ${event.status}`);
  if (event.type === "message" && event.role === "assistant") console.log(`event  assistant: ${event.content}`);
  if (event.type === "status" && event.status === "exited") console.log("event  exited");
}

A stale digest is refused and changes nothing:

examples/ts/approval-round-trip/main.ts
async function demoMismatch(approvalId: string): Promise<void> {
  const stale = `sha256:${"0".repeat(64)}`;
  try {
    await session.approve(approvalId, { actor: "alice", actionDigest: stale });
    assert.fail("a stale digest must be refused");
  } catch (error) {
    if (!isDaemonError(error, "daemon.approval_action_mismatch")) throw error;
    console.log(`refused ${error.code}: the approval stays open`);
  }
}

The transcript:

event  assistant: Cleaning the build directory.
asked  apr_00000000000000000000000001 Bash {"command":"rm -rf build"} sha256:aa7ad0eeb502...
answer allow by this peer
event  approval_resolved allow actor=alice reason=-
event  tool Bash started
event  tool Bash completed
asked  apr_00000000000000000000000002 Bash {"command":"git push --force"} sha256:5b406b77ccd6...
refused daemon.approval_action_mismatch: the approval stays open
answer deny by this peer
event  approval_resolved deny actor=alice reason=No force pushes.
event  tool Bash failed
event  assistant: Build cleaned. Not pushed.
event  exited

Large actions

When an action's args exceed 16 KiB, approval_requested carries args: {} and args_truncated: true. Fetch the full action with session.approval(approvalId), which joins the chunks and checks the digest, show it, and answer with its actionDigest. Inside a SessionInfo (pending_approvals, session.updated), args over 2 KiB are blanked the same way, with the digest kept.

What the digest does and does not do: every allow carries the digest of the whole action (tool_name, args, call_id), so the answer is bound to that one exact action, and a mix-up between approvals or a changed action is refused. The daemon cannot know what a viewer displayed, so it cannot stop a viewer from echoing the digest of args it showed as {}. Showing the full action before allowing is the viewer's duty: never allow an action whose args you showed truncated without fetching it first. RemoteSession.approve enforces this: it refuses, before sending anything, to allow an approval this connection has seen only truncated until session.approval(id) fetched it.

Changing the input

approve(id, { updatedInput }) replaces the tool input when the runner supports it (capabilities.updated_input; Claude Code does). Claude Code denies the call if updatedInput is not an object.

Who may answer

Only paired controller peers. The local socket token cannot answer unless the server is started with localApprovals: true: an agent running as your OS user must not approve its own tool calls. With it, viewport-daemon approve <id> prints the full action, then asks on a terminal (only y or yes allows) and refuses without one unless --yes is given; viewport-daemon approval show <id> prints the action alone.

Retries

Repeating the winning answer of the same caller, with every field the same, returns the original result. Any other answer to a resolved approval fails with daemon.approval_already_resolved.

On this page