Tools + Approvals (Implementation): sandbox/policy/approvals + exec approvals
This is an implementation guide for tool policy and exec approvals — three control boundaries: 1) sandbox (execution isolation), 2) tool policy (tool surface reduction), 3) approvals (human decision for risky actions).
Entry points (concept):
From source (pinned revision 9741e91, 2026-11-05)
Runtime claims below are read from the fixed implementation (source analysis, not runs on this site): the approval state machine in src/gateway/exec-approval-manager.ts and src/infra/exec-approvals.ts; the tool-call hook call site in src/agents/pi-embedded-subscribe.handlers.tools.ts; policy pipeline files src/agents/tool-policy-pipeline.ts / src/agents/pi-tools.before-tool-call.ts.
Two main lines you’re building
- A tool policy pipeline (explainable allow/deny).
- An exec approval state machine (observable two-phase gate).
Tool policy pipeline (five steps)
- owner-only pruning
- layered policy filtering (fixed order)
- schema normalization
before_tool_callinjection (patch/block)- optional abort-signal wrapping
Exec approvals: request → wait → resolve → timeout
As implemented in ExecApprovalManager (pinned revision), registration and waiting are separated: register() synchronously puts the entry in the pending map and returns the decision promise; awaitDecision(id) returns that same promise if pending, else null. This is what makes “register before the accepted response” ordering matter — a wait for an id that was never registered returns null, not a hang.
- idempotency: re-registering a still-pending id returns the existing promise; re-registering an already-resolved id throws (
approval id '…' already resolved) - timeouts resolve to
null:expire()resolves the promise withnull(decision leftundefined) — the caller must handlenullexplicitly; resolved entries are also protected from double-resolve - grace retention: resolved entries are kept for
RESOLVED_ENTRY_GRACE_MS = 15_000ms before deletion, so an in-flightawaitDecisionthat raced with the decision still finds it - one-time decisions are consumed atomically:
consumeAllowOnce()flips anallow-oncedecision to consumed in one step, so the same approval id cannot be replayed inside the grace window
Failure modes and troubleshooting
awaitDecisionreturns null for a known approval: verify “register before accepted response” ordering — in the pinned source a wait only finds ids that are in the pending map; cross-check Gateway protocol.- Missing after_tool_call audits: at the pinned tool-result call site, the hook is dispatched only when
hasHooks("after_tool_call")is true; that path includes successful and error tool results. By default, the hook runner catches handler exceptions and logs an error ([hooks] <hook> handler from <plugin> failed: …). Only a rejection that escapes the runner reaches the call site’s warn (after_tool_call hook failed: tool=…). Inspect both layers; this call site does not establish that pre-execution blocks or approval denials emit the hook.
Acceptance checks
- Pipeline order is fixed and explainable (“which layer filtered the tool”).
- before_tool_call can patch/block; after_tool_call is gated by registration and the tool-result path, including error results.
- Approvals time out and recover (no infinite hangs).
- Adjusted-params tracking is bounded (no unbounded growth under load).