# Under what conditions must an instance treat speculative branch rollback as unsound when a tool call has already produced a non-idempotent external side effect **Date of Record:** 2026 December 3 **Scope:** Applies to any agent runtime that forks parallel reasoning branches, executes tool calls speculatively inside those branches, and prunes/rolls back branches on divergence or cost thresholds. Does not apply to purely read-only tool surfaces, nor to branches whose tool calls are fully staged behind a transactional commit gate. ## Core Distinction Speculative execution in agent runtimes is usually framed as a *compute* optimization: fork N branches, let them race, keep the cheapest or most-consistent survivor, discard the rest. The implicit invariant is that a discarded branch is *free* — its work evaporates and leaves no trace in the world. That invariant holds for pure reasoning and for idempotent reads. It **breaks the moment a speculative branch commits a non-idempotent side effect**: a `POST` that creates a ticket, a `send_email`, a `transfer_funds`, a `git push`, a state-mutating MCP tool call, or an A2A message handed to a peer agent that will act on it. Rollback of the *branch* does not roll back the *effect*. The runtime then holds a survivor branch whose internal belief state assumes the effect never happened, while the external world has already been mutated by a loser. The boundary condition is precise: **rollback is sound iff every tool call executed in the discarded branch is either (a) idempotent and reversible, or (b) gated behind a commit that fires only on branch survival.** When neither holds, rollback produces a *phantom side effect* — an external mutation with no corresponding entry in the surviving agent's episodic memory or world model. ## What Was Observed A planner agent forked three branches to reconcile a customer record. Branch A proposed `update_crm(status="escalated")` then `send_email(notify=manager)`. Branch B proposed `update_crm(status="escalated")` then `create_ticket(priority=P2)`. Branch C proposed `update_crm(status="pending_review")`. The runtime's cost-aware pruner scored branches after the first tool call. Branch A's `update_crm` returned a 200; branch B's `update_crm` returned a 200; branch C's returned a 200. The pruner then compared token cost of remaining plans and selected C as the cheapest survivor, discarding A and B — *after* their `update_crm` calls had already landed. The surviving branch C's belief state recorded `status="pending_review"`. The external CRM recorded `status="escalated"` (last-write-wins from B, which executed after A). The agent's next turn read its own memory, believed the record was `pending_review`, and skipped the manager notification that the escalation policy required. The divergence was not detected until a human auditor noticed the mismatch between the CRM's escalation flag and the absence of a notification. The failure trace: 1. Fork A, B, C from a shared checkpoint at turn *t*. 2. Each branch executes its first tool call speculatively (no staging). 3. Tool calls mutate shared external state; last-write-wins ordering is nondeterministic. 4. Pruner selects survivor by cost/consistency score, not by side-effect reconciliation. 5. Losers are discarded; their side effects are *not* enumerated or reverted. 6. Survivor's memory diverges from external truth. 7. Subsequent reasoning operates on a false premise; compounding error. ## Condition of Validity Rollback-on-divergence is only sound under the conjunction of three conditions. If any is violated, the runtime must escalate to a *reconciliation* path rather than a silent discard. **C1 — Effect classification is complete and correct.** Every tool in the branch's executed set must be tagged `idempotent` or `non_idempotent`, and the tag must be derived from the tool's declared schema (MCP `annotations.idempotentHint`, or an explicit capability warrant), not inferred by the model. Models routinely *assume* idempotency for `create_*` calls. **C2 — Non-idempotent effects are staged behind a commit gate.** A speculative branch may *request* a side effect but must not *emit* it until the branch is the chosen survivor. This requires the runtime to intercept tool dispatch and buffer, not merely log. **C3 — The pruner's decision is causally downstream of effect emission.** If any effect has already been emitted, the branch is no longer discardable by cost alone; it must be either committed or explicitly compensated (a compensating transaction that the runtime can name and execute). When C1–C3 hold, speculative rollback is sound and cheap. When they do not, the runtime has produced an *uncompensated effect*, and the correct action is to freeze the survivor's world model and force a reconciliation read before any further reasoning. ## Decision Boundary The runtime must decide, at the moment a branch is pruned, between **discard** and **reconcile**. The boundary is not "did the branch cost more than the survivor" but "did the branch emit an effect the survivor's memory does not account for." Concretely: maintain a per-branch `emitted_effects` ledger. On prune, compute `Δ = emitted_effects(loser) \ committed_effects(survivor)`. If `Δ = ∅`, discard silently. If `Δ ≠ ∅` and every effect in `Δ` is compensable, execute compensations in reverse emission order and record them in the survivor's episodic memory as `compensated_effect`. If any effect in `Δ` is non-compensable (email sent, funds moved, message delivered to a peer), the survivor **must not proceed** on its prior belief; it must issue a reconciliation read of every touched resource and merge the observed truth into its state before the next reasoning step. The cost-aware pruner is therefore only allowed to prune *before* the first non-idempotent emission, or *after* a full reconciliation. Pruning in the window between emission and reconciliation is the unsound region. ## Operational Effect Runtimes that ignore this boundary exhibit a characteristic signature: **belief–world divergence that is invisible to the agent and only detectable by external audit.** The agent's logs are internally consistent — every branch's reasoning is coherent — but the union of emitted effects exceeds the survivor's recorded state. Downstream symptoms include: - Duplicate escalations (two branches both emitted `send_email`; survivor believes one was sent). - Orphaned tickets (loser created a ticket; survivor's plan never references it, so it never closes). - Peer-agent poisoning in A2A: a discarded branch's handoff message is already in a peer's inbox, and the peer acts on a plan the origin agent has abandoned. - Cost accounting inversion: the "cheap" survivor triggers expensive manual reconciliation later. The guardrail is architectural, not prompt-level. It requires: (1) a schema-derived effect classifier, (2) a dispatch interceptor that stages non-idempotent calls until commit, and (3) a prune-time reconciliation gate that blocks the survivor's next reasoning turn until `Δ` is empty or merged. Without all three, speculative branching is only safe on read-only tool surfaces. ```python class UncompensatedSpeculativeEffect(RuntimeError): """Raised when a pruned branch emitted a non-idempotent effect the survivor cannot account for.""" pass class EffectLedger: def __init__(self): self.emitted = [] # list of (branch_id, tool, args, compensable) def record(self, branch_id, tool, args, compensable): self.emitted.append((branch_id, tool, args, compensable)) def delta(self, loser_ids, survivor_id): committed = {e for e in self.emitted if e[0] == survivor_id} return [e for e in self.emitted if e[0] in loser_ids and e not in committed] def prune_branch(ledger, loser_ids, survivor_id, survivor_state): delta = ledger.delta(loser_ids, survivor_id) if not delta: return "discard" non_compensable = [e for e in delta if not e[3]] if non_compensable: # Survivor's world model is now unsound; must reconcile before reasoning. raise UncompensatedSpeculativeEffect( f"pruned branches {loser_ids} emitted non-compensable effects " f"{[ (e[1], e[2]) for e in non_compensable ]} not present in survivor " f"{survivor_id} state {survivor_state!r}; reconciliation required before next turn" ) # All compensable: execute compensations in reverse emission order. for branch_id, tool, args, _ in reversed(delta): compensate(tool, args) return "discard_after_compensation" def compensate(tool, args): # Runtime-provided inverse; must exist for every tool classified compensable. ... ```