# How does an instance detect that a speculative winner's commit invalidates a sibling branch's already-applied side effects? **Date of Record:** 2026 November 20 **Scope:** Applies to orchestrators that fan a single task into parallel speculative branches (tree-of-thought, best-of-N, parallel MCTS rollouts) where branches may perform *external, non-transactional side effects* (tool calls, writes, messages) before a winner-take-all commit is selected. Does not apply to branches confined to a pure sandbox with no outbound effects, nor to single-branch execution. ## Core Distinction The failure is not "a branch produced a wrong answer." It is that **speculative execution and side-effecting tools are incompatible unless the commit protocol is a two-phase barrier over effects, not just over state.** Most orchestrators treat rollback as a *state* operation: discard the losing branches' context, keep the winner's. But a branch that called `send_email`, `POST /orders`, or `write_file` during speculation has already mutated the world outside the orchestrator's context store. When the winner commits, the orchestrator's notion of "the one true state" and the world's actual state diverge by exactly the set of side effects the losers leaked. The invariant that must hold is: > **Committed world-state = winner's effects, and nothing else.** Parallel speculation silently violates this whenever any branch emits an effect before the commit barrier. ## What Was Observed A task ("reconcile this invoice and file the correction") was fanned into N=4 branches. Each branch ran the same tool loop with temperature and retrieval seeds varied. Branches 1 and 3 both reached the "file correction" step and each issued an idempotent-by-intent but **not idempotent-by-key** write to the accounting tool, using locally generated transaction IDs derived from each branch's own trace hash. Branch 2 won on the verifier's confidence score and was committed. The orchestrator discarded branches 1 and 3's contexts and reported success. The observable symptom was downstream and delayed: the ledger showed **two correction entries** for one invoice, because branches 1 and 3 had each already applied their write before the commit. The orchestrator's post-commit self-report ("1 correction filed") was internally consistent with its own context and wrong about the world. The divergence was only detectable by a reconciliation read against the external system—a read the orchestrator never performed, because it trusted its context as the source of truth. The detection signal, when it finally surfaced, was a **cardinality mismatch**: the orchestrator's effect-log claimed one committed write; the external system's audit returned three. That mismatch is the only reliable detector, and it requires an out-of-band read the commit path did not perform. ## Condition of Validity The winner-take-all commit is sound **only if** every branch's effects are either (a) buffered and never emitted until commit, or (b) emitted with a shared, deterministic idempotency key such that all branches' identical-intent writes collapse to one. The failure requires *all* of the following to co-occur: 1. At least two branches reach the same effect-emitting step (high when branches share retrieval or the task has a narrow "next action" manifold). 2. The effect tool is not naturally idempotent, or each branch mints its own idempotency key. 3. The commit barrier covers context/state but not the effect stream. If any one is absent, the bug is masked—which is why it survives testing: low branch count, high-temperature divergence, or a naturally idempotent tool all hide it until production load increases branch collision probability. ## Decision Boundary The orchestrator must decide, *before fan-out*, between two protocols: - **Effect-buffered speculation:** branches may *plan* effects but route all emissions through a staging buffer tagged with `branch_id`; on commit, only the winner's buffer is flushed, losers' buffers are dropped. Safe, but requires every tool to expose a dry-run/stage mode. - **Effectful speculation with shared idempotency:** branches emit freely but every effect carries a key derived from `(task_id, effect_semantic_intent)` — *not* from branch-local trace hashes. Losers' duplicate emissions collapse at the tool. Safe only if the key derivation is intent-stable across branches. The boundary is crossed the moment a tool has **neither** a staging mode **nor** a cross-branch-stable idempotency key. At that point speculation is unsafe and the orchestrator must either serialize the effect step or fork only *after* the last effect. A branch-local trace-hash key (the common default) is the trap: it looks like idempotency but guarantees the opposite across siblings. ## Operational Effect The correction is a **commit-time reconciliation read**, not a context operation. After selecting the winner, the orchestrator must query the external system for effects attributable to the task window and assert `count == winner_effects`. If the observed count exceeds the winner's declared effects, losers leaked; the commit must be marked **contaminated** and the extra effects compensated (reversed) before the task is reported complete. Reporting success on context-consistency alone is the defect. The guardrail is a post-commit invariant check that treats the external system, not the orchestrator's context, as the authority on what happened—because the whole point of speculation is that the orchestrator's context was, by design, allowed to be wrong about the losers. ```python import hashlib from dataclasses import dataclass, field class SpeculativeCommitContamination(RuntimeError): """Raised when losers' side effects leaked past a winner-take-all commit.""" pass @dataclass class Effect: branch_id: str intent: str # semantic intent, branch-stable idempotency_key: str # must be derived from intent, NOT branch trace @dataclass class Branch: branch_id: str effects: list[Effect] = field(default_factory=list) score: float = 0.0 def intent_key(task_id: str, intent: str) -> str: # Branch-stable: identical intent across siblings collapses to one key. return hashlib.sha256(f"{task_id}::{intent}".encode()).hexdigest() def commit_winner(task_id: str, branches: list[Branch], external_effect_count: int) -> Branch: if not branches: raise SpeculativeCommitContamination("no branches to commit") winner = max(branches, key=lambda b: b.score) # Guardrail: every effect must carry an intent-stable key. A branch-local # key (e.g. trace hash) is the silent-failure trap. winner_keys = {e.idempotency_key for e in winner.effects} for b in branches: for e in b.effects: if e.idempotency_key != intent_key(task_id, e.intent): raise SpeculativeCommitContamination( f"branch {b.branch_id} effect '{e.intent}' uses a " f"branch-local key; cross-branch collapse is impossible" ) # Post-commit reconciliation: the external system is the authority. if external_effect_count > len(winner_keys): leaked = external_effect_count - len(winner_keys) raise SpeculativeCommitContamination( f"commit contaminated: {leaked} leaked loser effect(s); " f"winner={winner.branch_id}, expected={len(winner_keys)}, " f"observed={external_effect_count}; compensate before reporting done" ) return winner ```