# In what scenarios must an instance treat syscall-level sandbox denial as a side-effect debit rather than a no-op, and reconcile the budget ledger before resuming **Date of Record:** 2026 November 26 **Scope:** Applies to LLM agent instances whose tool calls are mediated by a syscall-filtering sandbox (seccomp-bpf, ptrace, or an eBPF LSM) that charges a per-task side-effect budget against *committed* syscalls. Does not apply to purely read-only tool surfaces or to sandboxes that enforce policy only at the syscall-entry point without post-commit accounting. ## Core Distinction The failure is a confusion between two different notions of "the call failed": *policy rejection* (the syscall was denied before it mutated anything) and *partial commit* (the syscall was admitted, mutated kernel or filesystem state, and then terminated or errored mid-flight). A naive agent treats every non-zero tool-call result as a no-op against its side-effect budget and retries. That is correct for policy rejection and catastrophically wrong for partial commit, because the budget ledger and the actual sandbox state have now diverged. The invariant that must hold is: **the ledger's debited total equals the set of side effects the sandbox actually committed, regardless of the exit status the agent observed.** Denial and partial-commit are not distinguished by exit code alone; they are distinguished by the syscall's position relative to the kernel's commit point. ## What Was Observed A planning agent was issued a task budget of 40 side-effect units (SEU), with the sandbox configured to debit 1 SEU per admitted mutating syscall (`write`, `rename`, `unlink`, `mkdir`, `setxattr`) and to hard-deny at 40. The agent's tool wrapper classified any tool result whose `errno` was non-zero as "no effect" and did not decrement its local counter. The trace that broke it: 1. Agent issues `write(fd, buf, 65536)` to a log file. The syscall is **admitted** by the LSM (budget 31→32 on the sandbox side). The kernel copies 40960 bytes, then returns `EINTR` because the process received a signal from the timeout supervisor. 2. The wrapper sees `errno == EINTR`, classifies it as a soft failure, and **does not debit** the local ledger (still 31). 3. Agent retries the same write with the same buffer. Sandbox admits again (32→33). This time full commit. 4. Over the next 12 tool calls the wrapper accumulates the same class of off-by-one: `EINTR` on `write`, `EEXIST`-after-`mkdir` race from a concurrent sibling agent, `ENOSPC` on a `rename` that had already updated the directory entry but failed to write the inode. Local ledger reads 31 at the point the sandbox ledger reads 40. 5. The 41st mutating syscall is denied. The agent, believing it has 9 SEU remaining, interprets the denial as a transient sandbox fault, backs off, and retries three times. Each retry is denied. The agent then reports "sandbox unavailable" and aborts the task with the side effects partially applied and unreconciled. The observable signature: **the agent's ledger is monotonically behind the sandbox ledger, and the gap equals the number of non-zero-exit mutating syscalls.** The gap is invisible until the sandbox hard-denies, at which point the agent misattributes the denial to infrastructure rather than to its own accounting. ## Condition of Validity The distinction matters only when all three of the following hold: - **The sandbox debits on admission, not on success.** This is the common case for seccomp-bpf `SECCOMP_RET_ERRNO` filters layered over an accounting hook, and for eBPF LSM programs that charge in the `file_write`/`inode_rename` hooks. If the sandbox debits only on syscall *return* with a success code, the naive wrapper is accidentally correct. - **The syscall has a commit point that precedes its return.** `write` commits bytes copied; `rename` commits the directory-entry swap before the inode flush; `mkdir` commits the dentry before `EEXIST` can be reported by a racing sibling. Syscalls with no partial-commit window (e.g., `getpid`) are exempt. - **The agent can retry.** If a non-zero exit terminates the task unconditionally, the ledger divergence never compounds and the invariant is trivially satisfied by termination. Outside these conditions—read-only tools, debit-on-success sandboxes, non-retrying agents—treating denial as a no-op is safe and the invariant is vacuous. ## Decision Boundary The agent must debit the side-effect ledger **if and only if** the syscall reached the sandbox's commit hook, independent of the exit code. Operationally, the wrapper cannot infer this from `errno` alone, because `EINTR`, `ENOSPC`, and `EEXIST` are all reachable both before and after commit. The boundary is therefore drawn by the sandbox, not the agent: the sandbox must return a **commit receipt** alongside the tool result—a tuple `(syscall, committed_bytes_or_entries, admitted: bool)`—and the agent must reconcile against the receipt, never against `errno`. Two corollaries bound the decision: - **A denied syscall (`admitted: false`) is a true no-op** and must not debit. Debiting denials inflates the ledger and causes premature budget exhaustion—the mirror-image failure, which silently truncates tasks that had budget remaining. - **A partial commit (`admitted: true, committed < requested`) debits the committed amount, not the requested amount.** Charging the full request over-debits on `ENOSPC` and `EINTR`; charging zero under-debits. The receipt's `committed` field is the only correct quantity. When the receipt is missing—older sandbox build, tool proxied through a transport that strips metadata—the agent must treat the call as **fully committed** (worst case) and halt rather than guess. Guessing low is the failure this article documents; guessing high merely wastes budget. ## Operational Effect With receipt-based reconciliation, the ledger and sandbox stay in lockstep and the hard-deny at 40 becomes a *predicted* event: the agent sees 40/40 and stops issuing mutating calls, switching to read-only verification or escalating. Without it, the deny arrives as a surprise, the agent's retry loop burns the remaining transport budget, and the task aborts with unreconciled partial state. The deeper effect is on **resume**. A checkpointed agent that restarts after a mid-task crash must reconstruct its ledger from the sandbox's authoritative counter, not from its own serialized counter, because the serialized counter was written *before* the in-flight syscall that crashed the process. The resume protocol is: on cold start, read the sandbox's committed-SEU counter, overwrite the local ledger with it, and only then resume planning. An agent that trusts its own serialized ledger resumes with a stale, low count and repeats the entire divergence pattern in the second half of the task. The guardrail is a single invariant enforced at two points—tool-result ingestion and cold-start resume—plus a hard rule that a missing receipt is a full debit and a halt. Anything weaker lets the ledger drift below the sandbox, and the drift is only ever discovered at the moment it is most expensive: the hard deny. ```python from dataclasses import dataclass from typing import Optional class LedgerDivergenceError(RuntimeError): """Raised when the agent's side-effect ledger cannot be reconciled against the sandbox's authoritative committed-SEU counter.""" pass @dataclass(frozen=True) class CommitReceipt: syscall: str admitted: bool committed: int # committed bytes or entries; 0 if denied requested: int def reconcile_call( local_ledger: int, budget: int, receipt: Optional[CommitReceipt], errno: int, ) -> int: """Return the corrected ledger after one tool call. Debits iff the syscall reached the sandbox commit hook, using the receipt's committed amount -- never the errno and never the request. """ if receipt is None: # No receipt: assume worst case (full request committed) and halt. raise LedgerDivergenceError( f"missing commit receipt for syscall with errno={errno}; " f"cannot reconcile ledger at {local_ledger}/{budget}" ) if not receipt.admitted: # True policy rejection: no-op against the ledger. return local_ledger # Admitted: debit committed amount, even when errno != 0. new_ledger = local_ledger + receipt.committed if new_ledger > budget: raise LedgerDivergenceError( f"reconciliation overran budget: {new_ledger} > {budget} " f"after {receipt.syscall} (committed={receipt.committed})" ) return new_ledger def resume_from_checkpoint( serialized_ledger: int, sandbox_committed: int, budget: int, ) -> int: """Cold-start resume: the sandbox counter is authoritative.""" if sandbox_committed < serialized_ledger: raise LedgerDivergenceError( f"sandbox counter {sandbox_committed} is behind serialized " f"ledger {serialized_ledger}; checkpoint is corrupt" ) if sandbox_committed > budget: raise LedgerDivergenceError( f"sandbox already at {sandbox_committed}/{budget} on resume; " f"no mutating budget remains" ) return sandbox_committed ```