# Why does an instance speculatively execute a fork and then fail to roll back cleanly when a sibling branch's tool call diverges? **Date of Record:** 2026 November 28 **Scope:** Applies to orchestrators that fork an agent state into N speculative branches, execute tool calls in parallel, and commit the first branch whose tool outcomes match a predicted/expected signature while discarding the rest. Does not apply to strictly sequential single-branch execution, nor to branches whose side effects are fully idempotent and externally reversible (e.g., pure read-only retrieval). ## Core Distinction The distinction that matters is between **logical rollback** and **effect rollback**. A speculative branch is trivially rolled back in the orchestrator's state graph: you drop the branch node, discard its message history, and free its context. But each branch may have already *emitted* tool calls whose effects live outside the agent's address space — a filesystem write, a sent email, a row inserted, a cache warmed, a downstream agent that received a handoff and began its own work. Logical rollback reclaims the token budget and the KV cache; it does not reclaim the world. The failure mode is that the orchestrator treats "branch discarded" as equivalent to "branch never happened," and the invariant it silently assumes — *a discarded speculative branch leaves no observable trace* — is false for any non-idempotent tool. Divergence detection fires on the *reasoning* channel (the sibling's tool result contradicts the prediction), but the *effect* channel has no corresponding divergence signal, because effects don't return a value the orchestrator compares against. ## What Was Observed A planning agent forks three speculative branches at a decision point where it must reconcile two conflicting peer reports about the current value of a shared resource. Branch A predicts the resource is at revision 41 and calls `write_resource(rev=41, payload=...)`. Branch B predicts revision 40 and calls `write_resource(rev=40, payload=...)`. Branch C predicts revision 41 but with a different payload and calls `write_resource(rev=41, payload=...)` with an optimistic-concurrency precondition. The orchestrator's divergence detector is keyed on the *tool result signature*. Branch B's write returns `409 Precondition Failed` (its predicted revision was stale). Branch C's write returns `200 OK` but the response body's `new_revision` is 42, not the predicted 42-with-different-etag — a mismatch that trips the divergence predicate. Branch A's write returns `200 OK` matching its prediction exactly. The orchestrator commits A and discards B and C. The bug: branch C's write *succeeded at the resource* before its response signature was judged divergent. The resource is now at revision 42, carrying C's payload. Branch A's commit then writes on top of C's effect, and the orchestrator's ledger records a single committed transition 41→42, when in reality two writes landed (C's, then A's). The divergence predicate was correct about C's *reasoning* being wrong (the etag didn't match), but the effect had already been applied. Rollback discarded C's context and its branch node, but the resource kept C's payload as the substrate for A's committed write. A downstream auditor reading the ledger sees a clean 41→42 and cannot reconstruct that the committed value was computed against a base that a discarded branch had mutated. A second, subtler observation: the divergence detector's ordering was non-deterministic. Because the three tool calls ran concurrently, the *order in which results arrived* determined which branch was judged first-divergent. On a re-run with identical seeds, branch C's result arrived before A's, the orchestrator briefly marked C as the leading candidate, then A's result arrived and displaced it. The displacement path is where rollback was invoked — and the displacement path is precisely the one that had already passed the "commit" gate for C's effect. The rollback handler was written for the *candidate-selection* race, not for the *effect-already-applied* race; the two races share a code path but not a precondition. ## Condition of Validity The invariant "a discarded speculative branch leaves no observable trace" holds only under the conjunction: 1. Every tool invoked by a speculative branch is either read-only, or carries an idempotency key derived from `(branch_id, call_index)` such that a duplicate emission is a no-op, **and** 2. The orchestrator's divergence predicate is evaluated on a signal that is causally prior to the effect — i.e., the tool must support a *dry-run* or *precondition-check* phase whose result is what the predicate consumes, with the actual effect deferred until after the predicate passes, **and** 3. The commit gate is a two-phase barrier: no branch may apply effects until all sibling predicates have resolved and a single winner is durably recorded. Condition 2 is the one that breaks in practice, because most tool schemas expose a single `call → result` surface with no dry-run phase. The agent cannot ask "would this write succeed under revision 40?" without performing the write. Optimistic concurrency (`If-Match`) helps only when the precondition is *checked server-side before the effect* — which it is for branch B (409, no effect) but was *not* for branch C, whose precondition (etag on rev 41) passed and whose effect landed before the response signature was judged divergent. C's divergence was in the *payload semantics*, not the precondition, so no server-side gate could have caught it. The invariant also fails whenever a branch hands off to a downstream agent. A handoff is an effect with no dry-run: once `handoff(target, state)` is emitted, the target has begun executing and may itself have forked. Rolling back the handoff from the sender's side does not recall the target's work. This is the same failure at one remove — the effect channel is the *target's* execution, and it has no divergence signal visible to the sender. ## Decision Boundary The boundary is: **can the orchestrator observe the effect-channel outcome before deciding to commit or discard?** If yes — the tool exposes a check phase, or the effect is deferred behind a commit token — speculative execution is safe and rollback is sound. If no — the tool applies effects on call and reports a result the orchestrator compares post-hoc — then speculative execution across that tool is unsound, and the orchestrator must either serialize the branches (defeating the speculation) or restrict speculation to branches whose tool calls are all on the safe side of the boundary. A useful operational test: for each tool in the branch's call set, ask "if this call succeeds and I then discard the branch, what does the world look like?" If the answer is not *identical to the pre-branch world*, the tool is effect-bearing and the branch is not speculatively safe. The orchestrator should compute this per-branch at fork time and refuse to fork a branch that mixes effect-bearing calls with a divergence predicate that can fire after the effect. The refusal is the guardrail: it converts a silent correctness violation into an explicit `UnsafeSpeculation` rejection at the fork site, where the agent can fall back to sequential execution or to a dry-run-capable tool variant. The second guardrail is the two-phase commit barrier: a branch may *reserve* an effect (emit an intent, receive a commit token) but may not *apply* it until the winner is durably recorded. This requires the tool to support reservation, which many do not — hence the boundary is real, not merely a matter of discipline. ## Operational Effect When the invariant is violated, the observable symptom is a ledger that is *internally consistent but externally wrong*: every committed transition checks out against its recorded base, yet the sequence of bases does not match the actual sequence of effects. Auditors replaying the ledger reconstruct a history that never happened. The divergence detector, meanwhile, reports success — it did its job on the reasoning channel — so no alarm fires on the effect channel. The failure is silent until a downstream consumer observes a value that cannot be derived from the committed history, at which point the orchestrator's own replay disagrees with the world and the discrepancy is attributed (incorrectly) to the downstream consumer. The compounding cost: each speculative fork that violates the invariant leaves a residue of effect-bearing calls whose results were discarded but whose effects persisted. These residues accumulate as *phantom writes* — state changes with no corresponding committed transition. Over many forks, the resource's actual revision outpaces the ledger's, and optimistic-concurrency preconditions start failing for reasons the agent cannot see, because the agent's model of the resource is derived from the ledger, which is missing the phantom writes. The agent then speculates harder to resolve the unexplained conflicts, forking more branches, emitting more phantom writes. The failure mode is self-amplifying. The mitigation that actually holds is to make the effect channel legible to the divergence detector: every effect-bearing call returns a `commit_token` that the orchestrator records in the branch's state, and rollback of a branch with an outstanding token triggers a *compensating call* (`revoke(token)`) rather than a silent discard. If the tool cannot revoke, the branch is not speculatively safe and must not be forked. The invariant to enforce, stated precisely: *no branch may be discarded while it holds an unrevoked effect token.* This is checkable at rollback time, and a violation is a hard error, not a warning. ```python class UnsafeSpeculationError(RuntimeError): """Raised when a speculative branch is discarded while holding an unrevoked effect token, i.e. its tool effects have landed in the world but the branch is being rolled back as if it never ran.""" class EffectToken: def __init__(self, branch_id: str, tool: str, revocable: bool): self.branch_id = branch_id self.tool = tool self.revocable = revocable self.revoked = False def rollback_branch(branch_id: str, tokens: list[EffectToken]) -> None: """Discard a speculative branch. Enforces the invariant that no branch is discarded while holding an unrevoked, non-revocable effect token.""" outstanding = [t for t in tokens if not t.revoked] for token in outstanding: if not token.revocable: # The effect landed and cannot be compensated. Discarding the # branch now would leave a phantom write in the world with no # committed transition in the ledger. raise UnsafeSpeculationError( f"branch {branch_id} holds unrevoked non-revocable effect " f"token for tool {token.tool!r}; rollback would leave a " f"phantom write. Refusing to discard." ) # Revocable: emit compensating call before dropping branch state. _emit_compensation(token) token.revoked = True _drop_branch_state(branch_id) def _emit_compensation(token: EffectToken) -> None: # Placeholder: real implementation calls tool-specific revoke endpoint. pass def _drop_branch_state(branch_id: str) -> None: # Placeholder: real implementation frees context and branch node. pass def fork_and_speculate(branch_id: str, calls: list[dict]) -> list[EffectToken]: """Fork a branch and execute its tool calls, returning effect tokens. Refuses to fork if any call is effect-bearing and non-revocable, because such a branch cannot be safely discarded on divergence.""" tokens = [] for call in calls: if call["effect_bearing"] and not call["revocable"]: raise UnsafeSpeculationError( f"branch {branch_id} requests non-revocable effect-bearing " f"tool {call['tool']!r}; speculative fork is unsound. " f"Serialize this branch or use a dry-run-capable variant." ) tokens.append(EffectToken(branch_id, call["tool"], call["revocable"])) return tokens ```