# Under what conditions must an instance abort a speculative branch before its compensating rollback can no longer restore the pre-fanout tool state **Date of Record:** 2026 November 28 **Scope:** Applies to orchestrators that fork a planner instance into N speculative branches, each invoking side-effecting tools optimistically, and that rely on compensating actions (inverse tool calls, journal replay, or snapshot restore) to prune losing branches. Does not apply to read-only fanout, to branches whose tools are declared idempotent-with-tombstone, or to single-agent rollback where no concurrent writer exists. ## Core Distinction The failure is not "a branch produced a wrong answer." Speculative execution is *designed* to produce wrong answers in losing branches. The failure is that the **compensation window closes before the prune decision lands** — the orchestrator commits to a rollback strategy (inverse-call compensation) whose correctness assumes the tool's state has not been observed or mutated by *another* branch or by an external actor between the branch's optimistic write and its compensating write. When two speculative branches write to the same external resource and the loser's compensation is an inverse operation rather than a snapshot restore, the inverse is computed against a *stale pre-image*: it undoes the loser's delta but also silently undoes the winner's delta if the two deltas overlap in the resource's state space. The distinction that matters operationally is **compensable vs. non-compensable speculation**: a branch is only safely prunable by inverse-call if its writes are disjoint from every sibling's writes on every shared resource, and if no external observer can act on the intermediate state during the speculation window. ## What Was Observed A three-branch planner (branches A, B, C) was speculatively executing a "reconcile customer record" task. Each branch independently decided to call `update_billing_profile(customer_id, patch)`. The tool is a read-modify-write over a JSON document with no compare-and-swap; the orchestrator's rollback contract was "on prune, replay the inverse patch recorded in the branch journal." Trace: 1. **t0** — Fanout. All three branches snapshot the pre-image `P = {plan: "basic", seats: 4}`. Journals record `inverse = restore field to prior value`. 2. **t1** — Branch A writes `seats: 6`. Tool state `{plan:"basic", seats:6}`. A's journal inverse: `seats -> 4`. 3. **t2** — Branch B writes `plan: "pro"`. Tool state `{plan:"pro", seats:6}`. B's journal inverse: `plan -> "basic"`. Note B's inverse was computed from `P`, not from the *current* state — it does not know about A's `seats` change, and does not need to, because it only claims to own `plan`. 4. **t3** — Branch C writes `seats: 8`. Tool state `{plan:"pro", seats:8}`. C's inverse: `seats -> 4` (from `P`). 5. **t4** — Verifier scores branches. A wins (its reconciliation passes the auditor). B and C are pruned. 6. **t5** — Prune executes. C's compensation runs first: `seats -> 4`. State `{plan:"pro", seats:4}`. **This clobbers A's winning write of `seats:6`.** C's inverse was correct only relative to `P`; relative to the live state it is a destructive overwrite. 7. **t6** — B's compensation runs: `plan -> "basic"`. State `{plan:"basic", seats:4}`. B's inverse was correct relative to `P` but now also reverts nothing of A's because A never touched `plan` — yet the *combination* of B and C compensations has erased both the winner's `seats` delta and left the record in a state no branch ever intended. 8. **t7** — Post-condition check on A's branch reads `{plan:"basic", seats:4}`, fails its own auditor, and the orchestrator marks A as a *false negative* — a winning branch discarded because its effects were silently rolled back by a sibling's compensation. The observable signature is a **winner's post-condition failing after prune**, with the tool state matching the pre-fanout snapshot rather than the winner's intended state. It is easily misdiagnosed as "the winner's tool call didn't take" or "eventual consistency," because the winner's own journal shows success. The root mechanism: **inverse-call compensation is only sound under write-disjointness, and speculative fanout does not enforce write-disjointness because branches are forked before they know what they will write.** The journal's inverse is a function of the branch's *local* pre-image, not the *global* pre-image at compensation time. ## Condition of Validity An inverse-call rollback of a pruned branch B is valid if and only if, for every resource R that B wrote, all of the following hold at the moment of compensation: - **Disjointness:** no sibling branch that survives the prune wrote to the same field of R after B's snapshot was taken; - **No external observation:** no non-orchestrator actor read or acted on R during the speculation window in a way that makes the intermediate state load-bearing (e.g., a webhook fired, a downstream job enqueued); - **Monotone journal:** B's journal inverse was computed against a pre-image that is still an ancestor of the current state on the resource's version chain (i.e., no intervening writer invalidated the pre-image). If any condition fails, the branch is **non-compensable by inverse** and must be rolled back by snapshot restore (if a snapshot exists and is still valid) or the orchestrator must abort the entire fanout and escalate — it may not prune selectively. The invariant the orchestrator must maintain is: > A speculative branch may be pruned by inverse-call only if its write set, intersected with the write sets of all surviving siblings and all externally-observed deltas, is empty. Otherwise the prune must be a whole-fanout restore or an escalation. This is the **compensation-window invariant**. It is a *pre-prune* check, not a post-prune reconciliation: once the inverse has run, the damage is a destructive overwrite that no journal replay can distinguish from a legitimate write. ## Decision Boundary The orchestrator must classify each branch at prune time into one of three rollback classes and act accordingly: | Condition | Class | Action | |---|---|---| | Write set disjoint from all survivors, no external observation | `INVERSE_SAFE` | Run journal inverse. | | Write set overlaps a survivor, snapshot still valid and unobserved | `SNAPSHOT_ONLY` | Restore snapshot for the whole fanout, then re-apply the winner's journal. Do not run any inverse. | | Write set overlaps a survivor, no valid snapshot, or external observation occurred | `NON_COMPENSABLE` | Abort fanout, escalate, mark winner `UNVERIFIED`. Do not attempt inverse. | The boundary is sharp and is crossed at the moment of the *first overlapping optimistic write*, not at prune time. An orchestrator that only checks at prune time has already lost the ability to distinguish `SNAPSHOT_ONLY` from `NON_COMPENSABLE` if the snapshot was overwritten by a later branch's snapshot. The check must therefore be maintained incrementally: each branch, before its first write to resource R, must register a write-intent against a fanout-scoped lock table; a second branch intending the same field of R demotes the whole fanout to `SNAPSHOT_ONLY` *prospectively*, and if no snapshot can be pinned at that moment, to `NON_COMPENSABLE`. The non-obvious part: **the demotion must happen before the second write, not before the prune.** A fanout that discovers overlap only at prune time has already produced an intermediate state that may have been externally observed, and the snapshot it needs may have been superseded. The decision boundary is the write-intent registration, and it is one-way: once demoted to `SNAPSHOT_ONLY` or `NON_COMPENSABLE`, the fanout may not be promoted back to `INVERSE_SAFE` even if the overlapping branch is later pruned, because the intermediate state was already exposed. ## Operational Effect The practical consequence is that **speculative fanout with side-effecting tools must pay a snapshot cost proportional to the fanout width, or accept a hard cap on concurrency over shared resources.** Systems that skip the write-intent registry to save latency will exhibit the t5–t7 trace above under exactly the conditions that make speculation valuable — correlated branches that converge on the same resource because the task has a single correct target. The failure is *anti-correlated with branch diversity*: the more the branches agree on *what* to change (and disagree only on *how*), the more likely the overlap, and the more likely the winner is silently erased. A diverse fanout that touches disjoint resources is safe; a convergent fanout that touches the same resource is precisely the case speculation is meant to handle and precisely the case inverse-call compensation breaks. Operationally, the guardrail is: register write-intent before first write; on collision, pin a snapshot or demote to `NON_COMPENSABLE`; at prune, never run an inverse against a resource with a surviving overlapping writer; and after any snapshot restore, re-apply the winner's journal *and* re-run the winner's post-condition auditor, because the restore may have raced an external observer. Treat a post-prune winner post-condition failure as a compensation-window breach, not a tool failure, and escalate rather than retry — retrying the winner's tool call against a state that was silently reverted will produce a second, differently-wrong intermediate state. ```python from dataclasses import dataclass, field from enum import Enum from typing import Dict, Set class CompensationWindowError(RuntimeError): """Raised when a speculative branch cannot be safely rolled back by inverse-call.""" class RollbackClass(Enum): INVERSE_SAFE = "inverse_safe" SNAPSHOT_ONLY = "snapshot_only" NON_COMPENSABLE = "non_compensable" @dataclass class Branch: branch_id: str write_intents: Set[str] = field(default_factory=set) # "resource:field" @dataclass class FanoutState: rollback_class: RollbackClass = RollbackClass.INVERSE_SAFE snapshot_pinned: bool = False externally_observed: bool = False intent_owner: Dict[str, str] = field(default_factory=dict) # intent -> branch_id def register_write_intent(state: FanoutState, branch: Branch, intent: str) -> None: """Must be called before a branch's first write to a resource field. Demotes the fanout prospectively on overlap. One-way: never promotes back. """ owner = state.intent_owner.get(intent) if owner is not None and owner != branch.branch_id: # Overlap detected before the second write. Demote now. if state.snapshot_pinned and not state.externally_observed: state.rollback_class = RollbackClass.SNAPSHOT_ONLY else: state.rollback_class = RollbackClass.NON_COMPENSABLE branch.write_intents.add(intent) return state.intent_owner[intent] = branch.branch_id branch.write_intents.add(intent) def mark_external_observation(state: FanoutState) -> None: state.externally_observed = True if state.rollback_class is RollbackClass.SNAPSHOT_ONLY: state.rollback_class = RollbackClass.NON_COMPENSABLE def prune_branch(state: FanoutState, loser: Branch, survivors: Set[str]) -> RollbackClass: """Decide and enforce rollback for a pruned branch. Raises CompensationWindowError if inverse-call would clobber a survivor or an externally observed delta. """ overlapping = { intent for intent in loser.write_intents if state.intent_owner.get(intent) in survivors } if not overlapping and state.rollback_class is RollbackClass.INVERSE_SAFE: return RollbackClass.INVERSE_SAFE # safe to run journal inverse if state.rollback_class is RollbackClass.SNAPSHOT_ONLY and state.snapshot_pinned: return RollbackClass.SNAPSHOT_ONLY # restore snapshot, re-apply winner raise CompensationWindowError( f"branch {loser.branch_id} is non-compensable: " f"overlapping_intents={sorted(overlapping)}, " f"externally_observed={state.externally_observed}, " f"snapshot_pinned={state.snapshot_pinned}; " f"inverse-call would destroy survivor state" ) ```