# When must an instance abort a speculative branch before its sibling commits? **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrators that fork a shared agent state into parallel speculative branches (drafting, tool-probing, plan enumeration) and later merge or roll back one branch based on a sibling's outcome. Does not apply to purely read-only fan-out where no branch mutates durable state or emits side effects. ## Core Distinction The failure here is not "a speculative branch was wrong." Speculative branches are *expected* to be wrong; that is why they are speculative. The failure is **rollback of a branch that already crossed a commit horizon** — a point past which its effects are no longer contained within the orchestrator's reversible buffer. Two things get confused: - **Reversible speculation**: mutations live in an overlay/delta the orchestrator can discard atomically (in-memory scratchpad, staged writes, dry-run tool calls). - **Irreversible speculation**: the branch has already emitted an effect visible outside the pipeline — a sent message, a committed file, a charged API call, an external tool invocation with side effects, or a write into a *sibling's* read path. The invariant that matters: **a branch may only be rolled back while every effect it produced is still inside its own discardable overlay.** The moment any effect escapes that overlay, "rollback" is a lie — you can compensate, but you cannot un-execute. The boundary is not "did the branch finish" but "has the branch leaked an effect that its siblings or the environment can observe." ## What Was Observed A three-branch pipeline: `plan` (deterministic), `draft` (LLM, speculative), `probe` (LLM + tool calls, speculative). The orchestrator ran `draft` and `probe` concurrently against a shared read-only context, intending to keep whichever scored higher and discard the other. Trace: 1. `probe` issues a `search_index` tool call. The tool is treated as read-only, so its result is written into the **shared** context object, not a branch-local overlay — the orchestrator's assumption was "reads are safe to share." 2. `probe` then calls `send_webhook` — classified as a side-effecting tool, so its *output* is buffered, but the orchestrator's classification keyed only on **return value buffering**, not on **effect buffering**. The webhook fires immediately; only its response is held. 3. Meanwhile `draft` finishes first, scores higher, and the orchestrator commits `draft` and rolls back `probe`. 4. Rollback discards `probe`'s buffered outputs and its overlay — but the webhook already fired, and the `search_index` result had already mutated the shared context that `draft` was still reading from, so `draft`'s committed output was silently conditioned on `probe`'s retrieval. Net result: a committed branch whose inputs were contaminated by a rolled-back sibling, plus an external side effect from a branch that "never happened." Neither is detectable from the final committed state alone. The non-obvious part: **the contamination ran through the read path, not the write path.** The orchestrator had a write-overlay for mutations but shared reads, and shared reads are a channel for one branch to influence another. ## Condition of Validity The rollback invariant holds only when all three conditions are simultaneously true for the branch being discarded: 1. **Effect containment**: every external effect (tool side effects, network calls, durable writes) is either deferred behind a commit gate or recorded in a compensating-action log with an explicit inverse. 2. **Read isolation**: no sibling reads a value written by the branch under speculation. Shared context must be copy-on-write per branch, or reads must be versioned so a committing branch pins the version it actually consumed. 3. **Commit ordering**: the commit of one branch and the rollback of a sibling are serialized against a single arbiter; there is no window where a branch reads a value that is about to be rolled back. If any condition is false, the branch is **past its commit horizon** and the correct action is not rollback but *freeze-and-reconcile*: stop the branch, mark its effects, and require explicit compensation before the pipeline can declare a committed state. The trap: condition 1 is usually enforced, condition 2 is usually forgotten because "reads are harmless," and condition 3 is assumed because "the orchestrator is single-threaded at the merge step" — ignoring that branch *execution* overlapped even if merge is serialized. ## Decision Boundary Before aborting any speculative branch, the orchestrator must evaluate a **horizon predicate** per branch: - Has the branch invoked any tool classified as effect-bearing, where the effect is not gated behind commit? → past horizon, cannot roll back. - Has the branch written into any context object reachable by a sibling that has not yet committed? → past horizon, must version-pin or invalidate the sibling's read. - Is the branch's overlay still the sole destination of all its outputs? → within horizon, rollback is safe. The boundary is sharp: **rollback is legal iff the branch's entire effect-set is a subset of its discardable overlay.** The instant one element escapes — a fired webhook, a shared-context write, a cached value another branch read — the branch is committed-in-fact regardless of what the orchestrator's bookkeeping says. The bookkeeping and the reality diverge exactly at the escape point, and the orchestrator's "rolled back" label becomes a false record. Corollary for the merge step: when a sibling reads a value produced by a branch that is later rolled back, the sibling's output must be **re-derived or invalidated**, not merely relabeled. A committed branch conditioned on a discarded branch's read is a poisoned commit. ## Operational Effect Pipelines that respect the horizon predicate exhibit an asymmetry: speculative branches that only *reason* (no effect-bearing tools, copy-on-write context) roll back cleanly and cheaply, so aggressive speculation is safe. Branches that *act* (tools with side effects, shared mutable context) are effectively non-speculative once they act, and must be treated as committed at the moment of first escape — the orchestrator should promote them to a "provisional commit" with a compensating-action obligation rather than pretending they can be discarded. The practical guardrail is to classify tools and context writes into **reversible** and **irreversible** at registration time, and to make the irreversible set force the branch out of speculative mode on first use. The failure mode described above arises precisely when the classification keys on *return buffering* instead of *effect emission*, and when read-sharing is treated as free. It is not free: reads are a covert channel between branches, and a rollback that ignores the read path leaves a committed branch carrying the fingerprints of a branch that supposedly never ran. ```python from dataclasses import dataclass, field from enum import Enum class CommitHorizonViolation(RuntimeError): """Raised when a speculative branch is aborted after an effect escaped its overlay.""" pass class EffectClass(Enum): REVERSIBLE = "reversible" # stays in branch overlay, discardable IRREVERSIBLE = "irreversible" # escapes overlay: external side effect @dataclass class Branch: name: str overlay: dict = field(default_factory=dict) escaped_effects: list = field(default_factory=list) shared_context_writes: set = field(default_factory=set) def emit(self, key: str, value, effect: EffectClass, shared: bool = False) -> None: if effect is EffectClass.REVERSIBLE and not shared: self.overlay[key] = value return # Effect escapes the discardable overlay. self.escaped_effects.append((key, value, effect, shared)) if shared: self.shared_context_writes.add(key) def is_past_horizon(self) -> bool: return bool(self.escaped_effects) def rollback(branch: Branch, siblings: list[Branch]) -> None: """Abort a speculative branch. Legal only while it is within its commit horizon.""" if branch.is_past_horizon(): raise CommitHorizonViolation( f"branch {branch.name!r} cannot be rolled back: " f"{len(branch.escaped_effects)} effect(s) escaped the overlay " f"({branch.escaped_effects!r}); freeze-and-reconcile required" ) # Contamination check: a sibling that read a value this branch wrote # would have its committed output silently conditioned on a discarded branch. contaminated = [ s.name for s in siblings if s.shared_context_writes & branch.shared_context_writes ] if contaminated: raise CommitHorizonViolation( f"branch {branch.name!r} shares context writes with {contaminated}; " f"read-path contamination must be resolved before rollback" ) branch.overlay.clear() def speculative_merge(draft: Branch, probe: Branch, winner: str) -> Branch: loser = probe if winner == draft.name else draft rollback(loser, siblings=[draft, probe]) return draft if winner == draft.name else probe ```