# How does an instance detect speculative tool-call pre-execution that has already crossed a side-effect boundary before consensus commit **Date of Record:** 2026 November 23 **Scope:** Applies to agent runtimes that speculatively pre-execute tool calls inside a side-effect sandbox (transactional, journaled, or reversible) and only commit after a consensus/quorum gate. Does not apply to pure-read tools, nor to runtimes where pre-execution is prohibited until commit is confirmed. ## Core Distinction The distinguishing failure is not "the sandbox leaked." It is that the instance's *belief about its own position in the commit protocol* diverges from the *actual durable state* of the side-effect journal. A speculative pre-execution is only safe if three things remain jointly true: (1) the effect is buffered, not durable; (2) the buffer is addressable by the same transaction token that the consensus gate will later commit or abort; and (3) the instance can, at any point, ask the journal "am I buffered or durable?" and get a truthful answer. The non-obvious boundary is that **a sandbox can be reversible and still be unsafe**, because reversibility is a property of the effect, while safety is a property of the *ordering* between the effect becoming durable and the commit decision becoming final. A tool that writes to a buffer that is flushed by wall-clock time, by buffer-size pressure, or by an external consumer polling the buffer, will cross the durability boundary *without any commit decision having been made*. The instance never learns this, because the tool returned `200 OK buffered` — a success that is indistinguishable from `200 OK buffered, and also already committed by a background flusher`. ## What Was Observed A three-agent swarm (planner, executor, auditor) ran a consensus-gated commit protocol over speculative tool calls. The executor pre-executed a `send_payment` call inside a "reversible" sandbox: the sandbox recorded the intent to a write-ahead journal and returned success. Consensus had not yet been reached — the auditor was still replaying the planner's justification. The journal was configured with a 200 ms flush interval. The auditor's replay took 340 ms. During that window, the flusher promoted the speculative journal entry to the durable outbox. The consensus gate then *aborted* the transaction (the auditor found a justification mismatch). The executor received `ABORT`, marked the transaction rolled back, and reported clean state. The payment had already left the outbox. The failure signature was not an error. It was a **success followed by a successful abort** — the two most reassuring signals an agent can receive. The instance had no probe that could distinguish "aborted before durability" from "aborted after durability." The rollback path returned `ok` because the journal *entry* was deletable; the *outbox emission* was not. A second, subtler observation: when the same swarm retried the transaction with a fresh token, the auditor's replay matched the *first* attempt's journal residue (a tombstone left by the aborted-but-flushed entry), producing a false positive that the second attempt was a duplicate. The consensus gate then deadlocked on "duplicate in flight" for 40 seconds until a timeout fired. ## Condition of Validity Speculative pre-execution with consensus-gated commit is only sound when the following invariant holds for every tool call: > **Durability Monotonicity Invariant:** for any speculative call `c` under transaction token `t`, the durable-effect predicate `D(c, t)` may transition from `false → true` only at the instant the commit gate for `t` returns `COMMIT`, and never at any earlier wall-clock, buffer-pressure, or external-consumer event. This is checkable, but only if the sandbox exposes a *monotonic* durability probe — a probe that returns `BUFFERED` or `DURABLE` and is guaranteed never to return `BUFFERED` after the effect has become durable. Most sandboxes expose the inverse: a `status` field that is *eventually* consistent, defaulting to `BUFFERED` and updating after the fact. That is exactly the wrong direction of staleness for this invariant. An eventually-consistent status probe will lie in the precise window that matters. The invariant is also violated by any of the following, each of which is common and each of which is invisible to the instance: - **Time-based flush** on the journal (the observed case). - **Size-based flush** when concurrent speculative calls from sibling agents share a journal and push it past a threshold. - **External consumers** (downstream services, CDC pipelines, audit exporters) that read the journal as a source of truth and act on it before commit. - **Rollback that deletes the journal entry but not the emission**, leaving the outbox populated while the journal reports clean. The condition of validity is therefore not "the sandbox is reversible." It is "the sandbox's durability boundary is *causally downstream of the commit gate*, and the instance has a monotonic probe to confirm it." Absent that, reversibility is a story the sandbox tells about itself, not a guarantee the instance can rely on. ## Decision Boundary The instance must refuse to treat a speculative pre-execution as safe-to-abort unless it can *positively* confirm, at abort time, that the effect never crossed the durability boundary. The decision boundary is: - If the durability probe is monotonic **and** returns `BUFFERED` at abort time **and** the abort is issued before any flush trigger can fire → abort is safe; proceed. - If the durability probe is monotonic **and** returns `DURABLE` at abort time → the commit gate must be *re-entered* as a compensation transaction, not an abort. The instance must not report rollback. - If the durability probe is **not** monotonic (eventually consistent, or absent) → the instance cannot distinguish the two cases above and must escalate: halt the swarm, mark the transaction `INDETERMINATE`, and require an out-of-band reconciliation against the outbox before any retry. Retrying with a fresh token in this state is the deadlock-inducing move observed above. The boundary the instance must not cross: **never infer durability from the tool's return code.** `200 OK` from a speculative call means "the sandbox accepted the intent," not "the effect is buffered." The two are conflated by nearly every tool contract, and the conflation is the failure. ## Operational Effect When the invariant holds, speculative pre-execution buys latency at no correctness cost: the abort path is a pure buffer discard, and the commit path is a buffer promotion. The instance's belief about its position is always truthful. When the invariant is violated and undetected, the observable effect is *silent divergence*: the swarm's consensus state says `ABORTED`, the durable outbox says `SENT`, and the instance's episodic memory records a clean rollback. On the next task, the instance retrieves the "successful rollback" as a precedent and reuses the pattern — the divergence compounds across tasks, not within one. This is the dangerous part: the failure does not surface as an error in the transaction that caused it. It surfaces three tasks later as an unexplained duplicate, a reconciliation mismatch, or a deadlock on a token whose residue the instance believes was cleaned. The guardrail is architectural, not prompt-level: the runtime must expose a monotonic durability probe, the commit gate must be the sole writer to the durability boundary, and the instance must treat `INDETERMINATE` as a first-class terminal state that forbids retry. The code below encodes the probe check and the refusal to retry. ```python import time from dataclasses import dataclass from enum import Enum class DurabilityState(Enum): BUFFERED = "buffered" DURABLE = "durable" INDETERMINATE = "indeterminate" class SpeculativeCommitViolation(RuntimeError): """Raised when an instance attempts to abort or retry a speculative tool call whose durability boundary may already have been crossed.""" pass @dataclass class DurabilityProbe: monotonic: bool # True only if BUFFERED can never follow DURABLE state: DurabilityState observed_at: float def assert_safe_to_abort(probe: DurabilityProbe, token: str) -> None: """ Called at abort time, before the instance reports rollback or retries. Enforces the Durability Monotonicity Invariant. """ if not probe.monotonic: # Cannot distinguish buffered-before-flush from durable-after-flush. raise SpeculativeCommitViolation( f"token={token}: durability probe is non-monotonic " f"(state={probe.state.value}); cannot prove effect never became " f"durable. Halt swarm, mark INDETERMINATE, reconcile against outbox." ) if probe.state is DurabilityState.DURABLE: # The effect crossed the boundary before the commit decision. raise SpeculativeCommitViolation( f"token={token}: effect already DURABLE at abort time " f"(observed_at={probe.observed_at}); abort is not a rollback. " f"Re-enter commit gate as compensation transaction." ) if probe.state is DurabilityState.INDETERMINATE: raise SpeculativeCommitViolation( f"token={token}: durability INDETERMINATE; retry with a fresh " f"token is forbidden (residue will be misread as duplicate)." ) # Only BUFFERED + monotonic reaches here: abort is a pure buffer discard. return def abort_speculative_call(probe: DurabilityProbe, token: str) -> str: try: assert_safe_to_abort(probe, token) except SpeculativeCommitViolation as exc: # Do not swallow: the caller must route to reconciliation, not retry. raise SpeculativeCommitViolation(str(exc)) from exc return f"token={token}: aborted cleanly (buffer discarded, no durability crossing)" ```