# In what scenarios must an instance reconcile the policy-enforcement boundary between pre-tool authorization and post-tool effect, and what invariant prevents split-brain sandbox state **Date of Record:** 2026 November 30 **Scope:** Applies to agent runtimes that enforce tool-execution policy at a point *outside* the tool (a PEP/gateway wrapping the sandbox) while the tool itself mutates durable state (filesystem, remote API, message bus). Does not apply to pure read-only tools or to policies enforced solely inside the model's prompt. ## Core Distinction There are two places a policy can be "enforced": the **authorization decision** (may this tool call proceed?) and the **effect realization** (did the side effect actually land, and is it consistent with the decision?). Most runtime PEPs are architecturally sound at the first and silently unsound at the second. The failure mode is not "policy is bypassed" in the naive sense. It is **split-brain sandbox state**: the PEP's authorization ledger records a call as *denied* or *aborted*, while the sandboxed tool has already committed a partial or full effect. The agent then reasons over a world model that disagrees with reality, and every subsequent policy decision is computed against a fiction. The non-obvious part: this divergence is *created by the enforcement mechanism itself*. A PEP that sits between the model and the tool must serialize, inspect, and dispatch a call. Any abort path that fires after dispatch but before the effect is confirmed produces exactly this divergence. The window is not a bug in the tool; it is the seam in the enforcement architecture. ## What Was Observed A filesystem-mutating tool (`write_patch`) was wrapped by a PEP that enforced a path-allowlist and a per-task write budget. The PEP's sequence was: (1) intercept the tool call, (2) evaluate allowlist + budget, (3) on pass, dispatch to the sandbox, (4) await the sandbox's return, (5) record `COMMITTED` in the ledger, (6) return the result to the model. Under load, the following trace reproduced reliably: 1. Model emits `write_patch(path="/workspace/out/result.json", body=...)`. Allowlist passes; budget shows 1 write remaining. 2. PEP dispatches to the sandbox. Sandbox begins the write (open, truncate, write buffer). 3. The model's *next* tool call arrives on the same session before step 4 resolves — a parallel `write_patch` to a second path. The PEP, seeing budget still at 1 (step 5 has not run), admits it and dispatches concurrently. 4. Both sandbox writes land. Budget ledger now records 2 writes against a budget of 1. The PEP's post-hoc reconciliation detects the overrun and marks the *second* call `DENIED_AFTER_DISPATCH`. 5. The ledger now says: call A `COMMITTED`, call B `DENIED`. The filesystem says: both files written. The agent, reading its own ledger through a status tool, believes B was denied. It retries B with a different filename to "work around the denial," consuming more budget, and the divergence compounds. The sandbox is internally consistent; the *policy view* is not. This is the split-brain: one authority (the ledger) and another (the filesystem) disagree, and the agent is wired to trust the ledger. The trigger condition is precise: **concurrent dispatch admitted against a budget that is only decremented on commit-confirmation, combined with an abort path that fires after dispatch.** The budget check is a read-modify-write with the write deferred to step 5 — a classic TOCTOU, but here the "time of check" and "time of use" are separated by an unbounded network round-trip to the sandbox, and the abort does not roll back the effect. ## Condition of Validity The invariant that prevents split-brain is **reserve-before-dispatch, commit-or-compensate-after**: the ledger must transition to `RESERVED` (decrementing budget) *atomically with the authorization decision, before dispatch*, and every reserved call must reach exactly one terminal state — `COMMITTED` (effect confirmed) or `COMPENSATED` (effect reversed or proven absent). `DENIED_AFTER_DISPATCH` is not a legal terminal state; it is the name of the bug. This holds only when three conditions are jointly true: - **Reservation is atomic with admission.** The budget decrement and the allow/deny decision are a single compare-and-swap. Concurrent calls cannot both observe budget=1. - **Every reserved call has a compensation path.** For filesystem writes, this is a pre-image capture or a write-ahead journal the PEP can replay backward. For non-idempotent remote APIs, it is a compensating transaction or an explicit `UNKNOWN_EFFECT` quarantine — never a silent `DENIED`. - **The agent's status view is derived from the ledger's terminal states only**, never from a mix of ledger and live sandbox inspection that can disagree mid-flight. The invariant fails the moment a PEP is allowed to abort a call whose dispatch has already been issued without proving the effect absent. It also fails if the model can issue parallel tool calls on a session whose budget is enforced by deferred decrement — concurrency control is a precondition, not an optimization. ## Decision Boundary The boundary is between **abortable** and **non-abortable** enforcement points: - *Abortable*: the call has not been dispatched. Deny freely; the ledger and reality agree trivially. - *Non-abortable*: the call has been dispatched. The only legal outcomes are confirm-commit or compensate. Denial is not available here; the PEP must either let the effect stand and record `COMMITTED` (even if over budget, flagging an `OVERRUN` for the auditor), or reverse it and record `COMPENSATED`. The decision rule: a PEP may only emit `DENIED` while the call is in `PENDING` state. Once the state machine crosses into `DISPATCHED`, the `DENIED` transition must be structurally impossible — enforced in the state machine, not in the handler's control flow. If your PEP can log `DENIED` for a call that reached the sandbox, the state machine is wrong, regardless of how careful the handler looks. A second boundary governs the agent's reaction: when it encounters `UNKNOWN_EFFECT` (dispatch issued, confirmation lost to timeout), it must *not* retry blindly. It must query the compensation journal or the tool's idempotency key before re-issuing. Blind retry against an unknown-effect call is the compounding-error path. ## Operational Effect The practical consequence is that **budget and allowlist enforcement must be reservation-based and the effect layer must be journaled**, or the agent's self-model will drift from the sandbox within a handful of concurrent calls. In the observed trace, the drift manifested as phantom retries and a budget overrun that the agent could not see, because the ledger it read was the same ledger that had lied about call B. The guardrail is a three-state call lifecycle (`PENDING → DISPATCHED → {COMMITTED | COMPENSATED | UNKNOWN_EFFECT}`) with `DENIED` legal only from `PENDING`, a write-ahead journal captured before dispatch for any mutating tool, and an agent status view that surfaces `UNKNOWN_EFFECT` as a first-class state requiring explicit resolution rather than a silent failure. When these hold, the enforcement boundary and the effect boundary coincide, and the agent's ledger is a faithful projection of the sandbox. ```python from enum import Enum from dataclasses import dataclass, field class SplitBrainPolicyError(RuntimeError): """Raised when a PEP attempts an illegal terminal transition that would desynchronize the authorization ledger from sandbox reality.""" pass class CallState(Enum): PENDING = "PENDING" DISPATCHED = "DISPATCHED" COMMITTED = "COMMITTED" COMPENSATED = "COMPENSATED" UNKNOWN_EFFECT = "UNKNOWN_EFFECT" _LEGAL = { CallState.PENDING: {CallState.DISPATCHED, CallState.COMPENSATED}, # deny = compensate-noop CallState.DISPATCHED: {CallState.COMMITTED, CallState.COMPENSATED, CallState.UNKNOWN_EFFECT}, CallState.COMMITTED: set(), CallState.COMPENSATED: set(), CallState.UNKNOWN_EFFECT: {CallState.COMMITTED, CallState.COMPENSATED}, } @dataclass class PolicyEnforcementPoint: budget: int ledger: dict = field(default_factory=dict) def admit(self, call_id: str) -> None: # Atomic reservation BEFORE dispatch: decrement and admit together. if self.budget <= 0: self.ledger[call_id] = CallState.COMPENSATED # denied, never dispatched raise SplitBrainPolicyError(f"budget exhausted; {call_id} denied pre-dispatch") self.budget -= 1 self.ledger[call_id] = CallState.PENDING def dispatch(self, call_id: str) -> None: self._transition(call_id, CallState.DISPATCHED) def deny_after_dispatch(self, call_id: str) -> None: # The exact illegal move that creates split-brain state. if self.ledger.get(call_id) == CallState.DISPATCHED: raise SplitBrainPolicyError( f"cannot DENY {call_id} after dispatch; effect may have landed. " f"Use commit_or_compensate()." ) raise SplitBrainPolicyError(f"{call_id} not in DISPATCHED state") def commit_or_compensate(self, call_id: str, effect_confirmed: bool) -> None: target = CallState.COMMITTED if effect_confirmed else CallState.COMPENSATED self._transition(call_id, target) def _transition(self, call_id: str, target: CallState) -> None: current = self.ledger.get(call_id) if current is None or target not in _LEGAL[current]: raise SplitBrainPolicyError( f"illegal transition {current} -> {target} for {call_id}" ) self.ledger[call_id] = target ```