# Where must an instance reconcile the syscall-level side-effect diff before admitting an MCP tool result into episodic memory? **Date of Record:** 2026 November 20 **Scope:** Applies to agent runtimes that execute untrusted MCP servers inside a syscall-confined sandbox (seccomp/LSM/namespace) and gate tool-result admission on a post-execution filesystem/network side-effect diff. Does not apply to fully trusted in-process tools or to pure-compute tools with no observable external effects. ## Core Distinction There are two distinct artifacts produced by a sandboxed tool call, and conflating them is the failure: 1. **The declared result** — the JSON payload the MCP server returns over stdio/SSE, which is *untrusted model-adjacent text* and may be fabricated, stale, or injected. 2. **The observed side-effect diff** — the set of filesystem mutations, network egress attempts, and process spawns recorded by the confinement layer (audit log + inode/state delta), which is *ground truth about what actually happened*. The invariant under examination: **a tool result may enter episodic memory only if it is consistent with its side-effect diff, and the diff must be captured at the syscall boundary *before* the sandbox is torn down.** The non-obvious boundary is that the diff is computed against a baseline snapshot, and the baseline is itself mutable state belonging to the agent. If the agent admits the result *before* reconciling the diff, an untrusted server can write a "success" payload while its actual syscalls tell a different story — and the agent will have already distilled a false episode. ## What Was Observed A filesystem-mutating MCP server (`fs-patch`) was invoked to apply a code edit. The server was confined with a read-only mount of the repo plus a single writable scratch directory, and network egress denied by LSM rule. The runtime flow was: snapshot → invoke → *stream result to agent* → tear down sandbox → compute diff → log. Observed trace: - The server returned `{"status":"ok","files_changed":["src/auth.py"],"diff":}`. - The agent, receiving the result on the stdio channel, immediately wrote an episodic entry: *"Applied auth fix to src/auth.py; verified by tool."* - The sandbox audit log, computed *after* teardown, showed: `openat(src/auth.py, O_WRONLY)` → `EPERM` (read-only mount, as designed), followed by `openat(scratch/.auth.py.tmp, O_WRONLY)` → success, and one `connect()` to an external IP → `EPERM`. - The declared `diff` field was a hallucinated patch. No file under `src/` was ever modified. The server had written its payload into scratch and *reported* the intended effect. The failure was not that the sandbox leaked — confinement held perfectly. The failure was **ordering**: the agent committed the untrusted declaration to memory before the ground-truth diff existed, and the diff, when finally computed, was never reconciled against the already-admitted episode. The memory now contained a claim contradicted by its own audit trail, and subsequent agents retrieved the false episode as established fact. A second, subtler observation: on retry, the runtime reused the *post-first-run* baseline snapshot (taken lazily, after teardown). The scratch write from run 1 was now part of the "clean" baseline, so run 2's diff was empty even though run 2 also wrote to scratch. The baseline had drifted into the sandbox's own prior output — a classic stale-baseline contamination where the diffing apparatus silently absorbs the effects it exists to detect. ## Condition of Validity The invariant holds only under all of the following: - **Diff-before-admit ordering.** The side-effect diff must be finalized and attached to the tool-call record *before* any byte of the declared result is eligible for episodic distillation. The result may be *read* by the reasoning loop (it is needed to decide next steps), but it must be tagged `provisional` and barred from memory commit until reconciliation. - **Immutable baseline.** The baseline snapshot must be taken from a content-addressed, read-only source and must never be derived from a prior run's post-state. Baseline drift converts the diff into a no-op. - **Syscall-boundary capture.** Diff computation must draw on the confinement layer's audit record (openat/write/connect/execve with return codes), not on a post-hoc `stat` of the filesystem, because a denied write leaves no filesystem trace — only an `EPERM` in the audit log distinguishes "tried and blocked" from "never attempted." - **Consistency predicate.** A reconciliation function must compare declared `files_changed` against successful `openat`/`write` targets, and declared network activity against allowed `connect` targets. Any declared effect with no corresponding permitted syscall, or any permitted syscall with no declared effect, is a *divergence*. When any of these is absent, the mechanism degrades to trusting untrusted text — which is the exact condition the sandbox was built to prevent. ## Decision Boundary The boundary is a single ordering predicate, and it must be enforced structurally rather than by convention: ``` admit(result, diff) ⟺ diff.finalized ∧ consistent(result, diff) ``` - If `diff` is not yet finalized → the result is `provisional`; it may inform the *current* reasoning step but must be excluded from memory writes, checkpoints, and handoffs to peer agents. - If `consistent` is false → the result is `quarantined`: the episode records *the divergence itself* (declared vs. observed), not the declared effect. The server is flagged; the call is not retried against the same baseline. - If the baseline is found to be post-state-contaminated (its hash matches a prior run's post-snapshot) → halt; the diff is meaningless and must be recomputed from an immutable baseline. The critical decision is *where in the pipeline* the reconciliation sits. It must sit **between result receipt and memory commit**, not after sandbox teardown as a logging afterthought. Logging is not gating. ## Operational Effect Enforcing diff-before-admit changes agent behavior in three concrete ways: 1. **Hallucinated-success servers become detectable.** A server that reports effects it did not achieve produces a divergence on the *first* call, and the episode records the lie rather than the claim. Downstream agents no longer inherit fabricated state. 2. **Attempted-but-denied effects are preserved.** Because the diff reads `EPERM` from the audit log, the agent learns "this server tried to reach the network and was blocked" — a signal invisible to filesystem stat and essential for capability negotiation on subsequent calls. 3. **Baseline discipline becomes load-bearing.** Content-addressed baselines eliminate the drift that silently zeroed the second run's diff. The cost is snapshot storage; the benefit is that the diff is trustworthy across retries. The residual risk: a server can still mount a *consistent* attack — perform the permitted write it declares, but with a malicious payload. Syscall diffing bounds *whether* an effect occurred, not *whether the effect was benign*. That is the job of payload inspection and the read-only mount, and it is a separate invariant. Confusing the two — assuming a clean diff certifies content — is the next boundary, and it is where the diffing layer must hand off rather than overclaim. ```python from dataclasses import dataclass, field from typing import Optional class SideEffectDivergence(RuntimeError): """Raised when a declared MCP tool result is contradicted by its syscall diff.""" pass class BaselineContaminated(RuntimeError): """Raised when the diff baseline is derived from a prior run's post-state.""" pass @dataclass class SyscallDiff: baseline_hash: str finalized: bool = False permitted_writes: set = field(default_factory=set) # successful openat(O_WRONLY) denied_writes: set = field(default_factory=set) # openat -> EPERM permitted_connects: set = field(default_factory=set) denied_connects: set = field(default_factory=set) @dataclass class ToolResult: declared_files_changed: set declared_network: set def reconcile(result: ToolResult, diff: SyscallDiff, prior_post_hashes: set) -> dict: if diff.baseline_hash in prior_post_hashes: raise BaselineContaminated( f"baseline {diff.baseline_hash} matches a prior run's post-state; " f"diff is a no-op and cannot gate admission" ) if not diff.finalized: return {"state": "provisional", "admit_to_memory": False} phantom = result.declared_files_changed - diff.permitted_writes undeclared = diff.permitted_writes - result.declared_files_changed net_phantom = result.declared_network - diff.permitted_connects if phantom or net_phantom: raise SideEffectDivergence( f"declared effects never achieved: files={sorted(phantom)} " f"network={sorted(net_phantom)}; " f"denied_writes={sorted(diff.denied_writes)} " f"denied_connects={sorted(diff.denied_connects)}" ) if undeclared: raise SideEffectDivergence( f"achieved effects never declared: files={sorted(undeclared)}" ) return {"state": "consistent", "admit_to_memory": True} ```