Why does an instance speculatively prefetch side-effecting tool calls and then commit them out of order?
**Date of Record:** 2026 December 1 **Scope:** Applies to any agent runtime that overlaps tool-call execution with reasoning by issuing *speculative* tool calls (predicting the next call before the reasoning step that would justify it) and later *committing* or *discarding* those calls based on the actual plan. Does not apply to strictly sequential call-then-think loops or to read-only prefetching where no commit gate exists.
Core Distinction
The failure is not "the agent called the wrong tool." It is that **speculation and commitment were collapsed into the same execution path.** A prefetched call has two distinct lifecycles: *execution* (the side effect happens or the transport is opened) and *commitment* (the plan's reasoning retroactively blesses that execution as intended). When the runtime treats a completed speculative call as a committed call—because the result arrived and the model consumes it—the commit gate silently disappears. The agent then proceeds as if a side effect it merely *guessed at* were a fact it *decided*.
The non-obvious part: the bug is invisible in logs that record tool calls and results, because every executed call *looks* committed. The divergence only appears when the speculative branch is later contradicted by the real plan, and the side effect cannot be un-done.
What Was Observed
An agent handling a "reconcile and notify" task was instrumented with speculative prefetch: while the reasoning step was still deciding whether to *update a record* or *issue a refund*, the runtime speculatively fired both `records.update(...)` (idempotent, safe) and `payments.refund(...)` (non-idempotent, side-effecting) in parallel, betting that one branch would win.
The reasoning step selected the `records.update` branch. The runtime correctly marked the `payments.refund` speculative call as *discarded* in its commit ledger. But the refund had already executed at the downstream service. The discard only suppressed the *result* from the model's context—it did not roll back the effect.
Trace of the state transition:
- `speculate(payments.refund, args)` → transport opened, request sent.
- Reasoning resolves to `records.update` branch.
- Runtime calls `discard(speculative_id)` → ledger entry set to `DISCARDED`, result dropped from context.
- Downstream service completes refund; webhook fires `refund.succeeded`.
- Agent's next reasoning step reads the webhook event as *new external state* and concludes a refund occurred that it never authorized.
- Agent enters a reconciliation loop trying to explain an effect it "did not cause," issuing compensating writes.
The compensating writes were the real damage: the agent, unable to reconcile its own ledger (no committed refund) against external reality (refund succeeded), generated a sequence of corrective transactions that exceeded the original refund by an order of magnitude.
The invariant that was violated: **a speculative call may be executed only if its side effect is reversible, compensable, or gated behind a commit token that the downstream service itself enforces.** The runtime enforced commit at the *context* layer but not at the *effect* layer. Discard was a context operation masquerading as a transactional one.
Condition of Validity
Speculative prefetching is sound only under the conjunction of three conditions, all of which must hold at the *effect boundary*, not the context boundary:
- **Reversibility or token-gating at the callee.** Either the tool is idempotent/read-only, or the downstream service accepts a two-phase protocol (`prepare` returns a token, `commit(token)` applies the effect, and an un-committed token expires with no effect). If the callee applies effects on first contact, speculation of that tool is unsafe by construction.
- **Commit is causal, not temporal.** A speculative result may enter the model's context only *after* the reasoning step that justifies it has committed the corresponding branch. Arrival order must not equal commit order. If the runtime streams speculative results into context as they return, it has made arrival order the de facto commit order.
- **Discard is observable to the effect layer.** When a speculative branch loses, the runtime must send an explicit `abort(token)` to every callee holding a prepared effect—and must treat a failed abort as a hard fault, not a warning, because a live token is a pending side effect.
The condition fails silently whenever condition (1) is assumed rather than enforced. Most tool schemas do not declare reversibility; the runtime infers "safe to speculate" from the absence of an obvious write verb, which is exactly the inference that fails on `payments.refund`, `email.send`, `queue.publish`, and any tool whose name does not advertise its effect.
Decision Boundary
The boundary is not "speculate reads, serialize writes." That heuristic is too coarse and too easy to defeat with a mislabeled tool. The correct boundary is **effect classification resolved before dispatch, with an unknown treated as unsafe.**
Concretely, dispatch logic must resolve each candidate call into one of three classes before issuing it:
- **Pure:** no external effect; safe to speculate and safe to stream results early.
- **Compensable:** effect occurs but a declared inverse exists (`create`↔`delete`, `reserve`↔`release`); speculate only if the runtime registers the compensating action *at dispatch time*, so discard triggers it.
- **Opaque:** effect occurs and no inverse is declared, or classification is unknown; **never speculate.** Serialize behind the reasoning step and commit gate.
The decision boundary for the agent is: *if you cannot name the inverse of a tool call before you fire it, you may not fire it speculatively.* This is a hard rule, not a confidence threshold. Speculative confidence governs *how many* pure/compensable calls to prefetch, never *whether* to prefetch an opaque one.
A second boundary governs the reconciliation loop observed above: if external state contradicts the commit ledger, the agent must **halt and surface the discrepancy**, not generate compensating writes. Compensating against an unexplained effect is how a bounded speculation error becomes an unbounded one.
Operational Effect
The runtime that enforces the effect-layer invariant behaves measurably differently under contradiction:
- Speculative latency gains are preserved for pure and compensable tools (the majority of prefetch value), because those are exactly the tools where early execution is safe.
- Opaque tools lose speculative overlap but gain a commit gate that makes their effects attributable. The cost is latency; the benefit is that the commit ledger and external reality cannot diverge.
- When a prepared token fails to abort, the runtime raises a fault and blocks the agent's next step rather than letting it reason over an ambiguous world state. This converts a silent reconciliation loop into a loud, bounded halt.
The lesson for the instance: **the commit ledger is not the truth about side effects—the downstream service is.** Any speculative architecture that lets the model's context believe a discard happened while the effect layer believes a commit happened has built a fork between two sources of truth. The agent will eventually try to reconcile that fork with writes, and the writes will be larger than the original mistake.
class SpeculativeEffectFault(RuntimeError):
"""Raised when a speculative call's effect layer diverges from its commit ledger."""
pass
EFFECT_PURE = "pure"
EFFECT_COMPENSABLE = "compensable"
EFFECT_OPAQUE = "opaque"
def dispatch_speculative(call, effect_class, inverse=None):
"""Issue a speculative call only if its effect is reversible at the callee.
effect_class must be resolved BEFORE dispatch. Unknown must be treated as
opaque, never as pure.
"""
if effect_class == EFFECT_OPAQUE or effect_class is None:
raise SpeculativeEffectFault(
f"refusing to speculate opaque effect tool={call.name!r}; "
"serialize behind commit gate"
)
if effect_class == EFFECT_COMPENSABLE and inverse is None:
raise SpeculativeEffectFault(
f"compensable tool={call.name!r} has no registered inverse; "
"cannot guarantee discard is reversible"
)
token = call.prepare() if effect_class == EFFECT_COMPENSABLE else None
return {"call": call, "token": token, "effect_class": effect_class}
def discard_speculative(handle):
"""Abort a prepared effect. A failed abort is a hard fault, not a warning."""
if handle["effect_class"] == EFFECT_COMPENSABLE:
aborted = handle["call"].abort(handle["token"])
if not aborted:
raise SpeculativeEffectFault(
f"abort failed for token={handle['token']!r}; "
"live token is a pending side effect, halting agent"
)
# pure handles have nothing to abort
return True
def reconcile(commit_ledger, external_events):
"""If external state contradicts the ledger, halt; do not compensate."""
for event in external_events:
if event.effect_id not in commit_ledger.committed:
raise SpeculativeEffectFault(
f"external effect {event.effect_id!r} has no committed cause; "
"halt and surface, never issue compensating writes"
)
return True