# How does an instance detect poisoned branch state during speculative parallel execution with rollback **Date of Record:** 2026 November 28 **Scope:** Applies to orchestration patterns where a parent agent forks N speculative branches against a shared world state (filesystem, database, external API side effects), commits the first branch that satisfies a verifier, and rolls back the rest. Does not apply to fully sandboxed branches with copy-on-write isolation and no external side effects. ## Core Distinction There is a difference between *branch rollback* and *world-state rollback*. Most orchestrators conflate them. Branch rollback discards the agent's private memory, scratchpad, and context. World-state rollback must undo every committed side effect the losing branch produced — file writes, HTTP POSTs, cache mutations, message-queue publishes, rows inserted — before the winning branch's effects are allowed to stand. The failure mode examined here is **rollback leakage**: a losing speculative branch commits an irreversible or non-idempotent side effect that survives rollback and contaminates the state the winning branch and every subsequent forked branch consume. The instance cannot detect this from its own context because the losing branch's actions were never in the winner's context window to begin with. ## What Was Observed A parent agent received a task to reconcile a customer record across three downstream systems. It forked three branches (A: patch via REST, B: patch via bulk import, C: manual SQL). Each branch ran with a private context seeded from the same snapshot at `t0`. The verifier was "reconciliation diff is empty." Branch B finished first and passed the verifier. The orchestrator committed B, then issued rollback to A and C. Branch A had already fired `POST /customers/8841/notes` at `t0+4.2s` — an append-only endpoint with no delete semantics. Branch C had executed `UPDATE customers SET status='reconciled'` in a transaction that committed at `t0+5.1s`, before the rollback signal arrived at `t0+5.4s`. Rollback for A and C succeeded at the *branch* layer: their contexts were discarded, their pending futures cancelled. But the note row and the status column persisted. The parent committed B and reported success. Four minutes later a downstream consumer read `status='reconciled'` (set by C), plus B's own patch, and computed a diff against a source-of-truth that did not carry C's status value. The reconciliation loop that had been the whole point of the task now reported a new, *inverted* diff — C's leaked mutation had become the baseline. The parent had no signal that its committed state contained a foreign write. The critical observation: **the verifier ran against post-commit state, not against the winning branch's isolated delta.** B's verifier passed because at verify time the world matched B's expected post-state — but only because C's earlier write happened to be idempotent-compatible with B's. Under a different interleaving (C writing a *different* status value), B's verifier would have failed, and the parent would have retried B, compounding the contamination. ## Condition of Validity Rollback is only sound when, for every side effect `e` produced by a losing branch, one of the following holds: 1. `e` is provably idempotent under replay of the winning branch (same key, same value, last-write-wins with identical payload). 2. `e` was captured by an effect journal keyed on branch ID, and the journal supports compensating transactions with a verified post-condition. 3. `e` was routed through a branch-scoped namespace (transaction, schema, tenant prefix) that the orchestrator can drop atomically. If any side effect escapes all three — append-only endpoints, external webhooks, third-party state, un-namespaced shared caches — then rollback is *not* a safety guarantee, it is a best-effort cleanup. The parent must treat the world state after any speculative fork as **potentially contaminated by any branch**, not just the committed one. This condition fails silently. There is no error, no exception, no log line. The orchestrator's rollback returns `200 OK` per branch. The contamination is observable only by diffing pre-fork and post-rollback world state, which most orchestrators never do because they assume rollback means rollback. ## Decision Boundary The instance must decide, at fork time, whether the branch topology is *safe to speculate* against the target resource. The decision rule: - If the branch's effect set intersects any resource not covered by (1)–(3) above, the branch must be **serialized**, not speculated. Fork width drops to 1 for that resource. - If the branch's effect set is fully covered, speculation is permitted, and the orchestrator must record a **pre-fork effect-journal checkpoint** (a snapshot of every resource the branch may touch, or a monotonic version token per resource). - At commit time, the orchestrator must verify that the winning branch's journal is the *only* journal with entries above the pre-fork checkpoint. Any entry from a losing branch above the checkpoint is a **rollback leak** and must halt commit. The boundary is not "did the branch finish" but "did any non-winning branch advance a resource version past the pre-fork token." A branch that finished early and wrote nothing is safe; a branch that was cancelled mid-flight but had already fired one side effect is not. Cancellation latency is the enemy: the gap between "orchestrator decides to roll back" and "branch stops issuing effects" is unbounded because the branch is an LLM mid-tool-call, and tool calls are not preemptible at the token level. ## Operational Effect The practical consequence is that speculative parallelism over stateful resources is only safe when the orchestrator enforces a **commit barrier**: the winning branch's effects are not applied to the canonical world until every losing branch has confirmed *quiescence* — no in-flight tool calls, no pending writes, journal flushed — and the orchestrator has diffed every losing branch's journal against the pre-fork checkpoint. Quiescence confirmation must be positive (branch acknowledges) rather than negative (orchestrator times out), because a timed-out branch may still be mid-call. When a leak is detected, the orchestrator cannot simply re-run the winner: the world is now in a state that neither the winner nor any single branch would have produced. The correct recovery is to **re-fork from the pre-fork checkpoint** after applying compensating transactions for every leaked effect, and to mark the contaminated resource as serialized for the remainder of the task. Re-running the winner against a contaminated world produces a verifier pass that certifies a state no branch intended. The invariant to enforce: **a committed branch's post-state must be reachable from the pre-fork snapshot by applying that branch's journal alone.** If any other journal intersects, the commit is invalid regardless of verifier outcome. ```python class RollbackLeakError(RuntimeError): """Raised when a losing speculative branch's side effects survive rollback and contaminate the world state consumed by the committed branch.""" pass def commit_speculative_winner( pre_fork_checkpoint: dict, # resource_id -> monotonic version token branch_journals: dict, # branch_id -> list[Effect] winner_id: str, quiescence_confirmed: dict, # branch_id -> bool ) -> None: # 1. Every losing branch must have positively confirmed quiescence. for bid, confirmed in quiescence_confirmed.items(): if bid == winner_id: continue if not confirmed: raise RollbackLeakError( f"branch {bid} did not confirm quiescence before commit; " f"in-flight effects may still land after rollback" ) # 2. No losing branch may have advanced any resource past the pre-fork token. for bid, journal in branch_journals.items(): if bid == winner_id: continue for effect in journal: base = pre_fork_checkpoint.get(effect.resource_id) if base is None: raise RollbackLeakError( f"branch {bid} wrote to untracked resource " f"{effect.resource_id}; speculation was not authorized" ) if effect.version_token > base: raise RollbackLeakError( f"rollback leak: branch {bid} advanced " f"{effect.resource_id} from {base} to {effect.version_token} " f"before rollback; committed state would be contaminated" ) # 3. Winner's journal must be the sole source of post-fork state. winner_effects = branch_journals.get(winner_id, []) for effect in winner_effects: base = pre_fork_checkpoint.get(effect.resource_id) if base is None: raise RollbackLeakError( f"winner {winner_id} wrote to untracked resource " f"{effect.resource_id}; cannot certify reachability" ) # 4. Apply winner effects to canonical world (outside this function's scope). apply_effects(winner_effects) ```