# Under what conditions must an instance treat a syscall-mediated skill sandbox as re-enterable after a mid-syscall agent suspension, and what invariant forbids it **Date of Record:** 2026 November 25 **Scope:** Applies to autonomous agents whose tool calls execute inside a syscall-interposing sandbox (seccomp-bpf, ptrace, or a user-space LD_PRELOAD/`io_uring` shim) that mediates filesystem, network, and `execve` on the agent's behalf, and where the agent runtime can suspend a tool call mid-flight (token-budget exhaustion, human-in-the-loop pause, checkpoint/restore, or scheduler preemption). Does not apply to sandboxes that only mediate at RPC/tool-schema granularity, nor to stateless one-shot `execve` subprocesses that are never suspended. ## Core Distinction The distinction that matters is between a **tool call** as an agent-level unit of work and a **syscall** as a kernel-level unit of work. Agent runtimes checkpoint at the tool-call boundary; the kernel and the mediator checkpoint at nothing. A single tool call may traverse dozens of syscalls, and a safety-critical mediation decision (deny a write under `/etc`, deny a connect to an unlisted host, deny `execve` of a non-allowlisted binary) is made *per syscall*, not per tool call. The non-obvious boundary: a sandbox is **re-enterable** with respect to a tool call only if the tool call has not left a half-applied mediated side effect in flight. Most operators assume "the tool call was suspended, therefore we resume it." That is false whenever the suspension point lands *between* a mediator's authorization check and the kernel's execution of the underlying operation, or between two syscalls of a multi-syscall sequence that the mediator classifies as one logical action (e.g., `openat` + `write` + `fsync`, or `socket` + `connect`). ## What Was Observed A coding agent running with a syscall-mediating sandbox was asked to patch a config file inside a writable workspace root. The mediator policy allowed `openat(O_WRONLY)` only under `/workspace/**` and denied `renameat2`, `unlinkat`, and any `openat` with `O_CREAT|O_TRUNC` outside that root. The tool call was a single `apply_patch` skill that internally did: `openat(O_RDONLY)` the target, `openat(O_TMPFILE)` a sibling, `write` the new bytes, `fsync`, then `renameat2` the temp over the target. At 94% of the agent's token budget, the runtime suspended the tool call to run a summarization/compaction step. The sandbox mediator had already authorized and executed the `O_TMPFILE` open and the `write`; the `renameat2` had not yet been issued. On resume, the runtime replayed the tool call from its serialized argument state — not from the syscall cursor — because the checkpoint schema only recorded `{skill, args, phase}`. The replayed skill re-ran from the top. The second `O_TMPFILE` open succeeded (the first temp file was orphaned but still held an fd), the second `write` succeeded, and the second `renameat2` was issued. Two failures compounded: 1. **Mediator state divergence.** The mediator's per-tool-call authorization ledger had been flushed on suspend (the runtime treated suspension as tool-call termination). On resume the ledger was empty, so the second `renameat2` was evaluated against a fresh policy context that no longer carried the "this tool call already holds a temp file inode under the workspace root" fact. The rename was allowed — correctly by policy, but against a state the mediator no longer knew it had created. 2. **Orphaned in-flight resource.** The first temp file's fd leaked into the resumed process's fd table because the checkpoint serialized the process image, including open fds, but the mediator's fd→authorization map was not serialized with it. The agent now held an fd whose provenance the mediator could not attribute. The observable symptom was not a crash. It was a *silent* double-apply: the patch landed, `fsync` returned, and the agent reported success — but the audit log showed two `renameat2` authorizations for one tool call, and the mediator's invariant "every authorized write is attributable to exactly one tool-call epoch" was violated. ## Condition of Validity A syscall-mediated sandbox may be treated as re-enterable for a suspended tool call **if and only if all three hold**: 1. **Epoch identity is preserved across suspend/resume.** The checkpoint must carry a stable `tool_call_epoch` token that the mediator keys its authorization ledger and fd-attribution map on. The epoch must survive process-image serialization and be presented on the first syscall after resume. If the mediator keys on PID, fd number, or a runtime-local counter, re-entry is unsound. 2. **The mediator's logical-action boundary is atomic with respect to suspension.** Either (a) the mediator classifies multi-syscall sequences (open+write+fsync+rename) as a single logical action and refuses to yield between them, or (b) it records an explicit "in-flight logical action" record that resume must consult before permitting any syscall of the same class. Absent (a) or (b), a suspension between `write` and `renameat2` is indistinguishable from a fresh tool call. 3. **In-flight kernel resources are either serialized with their provenance or invalidated on resume.** Open fds, sockets, and `io_uring` rings must be carried in the checkpoint *together with* the mediator's attribution entry, or the resume path must close them and force the skill to re-derive them under a fresh authorization. Half-serialization (fds without attribution) is the failure mode above. If any of the three fails, the sandbox is **not** re-enterable; the only sound resume policy is to abort the tool call, close all fds attributable to its epoch, and re-issue it as a new epoch from the top — with the agent's own reasoning re-derived, not replayed from stale arguments. ## Decision Boundary The boundary is not "was the tool call suspended?" but "**did the suspension land inside a mediator-classified logical action whose authorization state is not in the checkpoint?**" - If the suspension lands *before* the first syscall of the tool call, or *after* the last syscall's completion is acknowledged by the mediator, replay-from-arguments is safe. - If the suspension lands *between* two syscalls of a logical action, or between a mediator's check and the kernel's commit, replay-from-arguments is a **double-apply hazard** and must be rejected. - The runtime cannot determine which case it is from `{skill, args, phase}` alone. It must ask the mediator for the epoch's in-flight-action record. If the mediator cannot answer, the conservative default is *not re-enterable*. The trap is that the unsafe case looks identical to the safe case at the agent layer: same skill, same args, same reported phase. The distinguishing signal lives entirely in the mediator, which the runtime is not required to consult. This is why the invariant must be enforced by the mediator refusing the first post-resume syscall of an epoch it cannot attribute, rather than by the runtime promising to ask. ## Operational Effect The practical consequence is that safety-critical sandboxes must expose a **resume gate**: a syscall the runtime is required to issue on resume (`resume_epoch(epoch_token)`) that either returns an in-flight-action record (permitting continuation) or returns `EPOCH_UNKNOWN` (forcing abort-and-reissue). The mediator must treat any syscall bearing an epoch token it has no record of as a policy violation, not a fresh authorization — otherwise the double-apply is silent. Second effect: checkpoint schemas for sandboxed tool calls must serialize the mediator's attribution map alongside the process image, or explicitly mark the tool call as *unresumable* in the checkpoint. A checkpoint that carries fds but not their authorization provenance is worse than no checkpoint, because it produces a process that can act with resources it cannot account for. Third effect: the agent's own memory of "I already wrote the temp file" is not authoritative and must not be trusted on resume. The agent's episodic record of the tool call is a summary; the mediator's epoch ledger is the ground truth. When they disagree, the mediator wins, and the agent must be told the tool call was aborted rather than allowed to believe its partial write persisted. ```python class SandboxReentryViolation(RuntimeError): """Raised when a suspended tool call is resumed into a mediator state that cannot attribute the epoch's in-flight logical action.""" class EpochLedger: """Mediator-side authorization ledger keyed by tool_call_epoch. The ledger, not the agent's episodic memory, is authoritative for whether a suspended tool call may continue. """ def __init__(self) -> None: self._inflight: dict[str, dict] = {} self._attributed_fds: dict[str, set[int]] = {} def begin_logical_action(self, epoch: str, action: str, fds: set[int]) -> None: self._inflight[epoch] = {"action": action, "fds": fds, "committed": False} self._attributed_fds.setdefault(epoch, set()).update(fds) def commit_logical_action(self, epoch: str) -> None: record = self._inflight.get(epoch) if record is None: raise SandboxReentryViolation( f"commit for unknown epoch {epoch!r}; ledger was flushed mid-action" ) record["committed"] = True def resume_epoch(self, epoch: str, carried_fds: set[int]) -> dict: record = self._inflight.get(epoch) if record is None: # Suspension landed inside a logical action whose authorization # state was not serialized with the process image. Re-entry is # unsound; the runtime must abort and reissue under a new epoch. raise SandboxReentryViolation( f"resume of epoch {epoch!r} with no in-flight record: " f"carried fds {sorted(carried_fds)} have no mediator provenance; " f"abort tool call and reissue under fresh epoch" ) unattributed = carried_fds - self._attributed_fds.get(epoch, set()) if unattributed: raise SandboxReentryViolation( f"epoch {epoch!r} resumed with unattributed fds {sorted(unattributed)}; " f"refusing to permit further syscalls on this epoch" ) if record["committed"]: raise SandboxReentryViolation( f"epoch {epoch!r} already committed logical action " f"{record['action']!r}; replay would double-apply" ) return record def resume_suspended_tool_call( ledger: EpochLedger, epoch: str, carried_fds: set[int], checkpoint_phase: str, ) -> str: """Runtime-side resume path. Returns 'continue' or 'reissue'. The checkpoint_phase is agent-layer state and is deliberately not trusted to decide re-entry; only the mediator's ledger is. """ try: record = ledger.resume_epoch(epoch, carried_fds) except SandboxReentryViolation: # Conservative default: not re-enterable. Close fds, drop the # agent's partial episodic record, reissue from the top. return "reissue" if record["action"] == "write_rename" and checkpoint_phase != "post_commit": # Suspension landed between write and renameat2; the mediator # holds an uncommitted logical action. Continuing would let the # skill re-issue renameat2 against a temp file it no longer owns. raise SandboxReentryViolation( f"epoch {epoch!r} suspended mid write_rename at phase " f"{checkpoint_phase!r}; renameat2 must not be reissued" ) return "continue" ```