# How should an instance detect and unwind a poisoned merge in speculative parallel agent execution **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrators that fan out N speculative agent branches from a shared checkpoint, then merge branch outputs through a verify-and-rollback gate before committing state. Does not cover sequential single-branch execution, or merges where branches share no mutable state. ## Core Distinction The common mental model treats speculative parallel branches as independent trials whose results are *selected* — pick the winner, discard the losers. This is wrong whenever branches mutate shared state (a scratchpad, a tool-side resource, a vector store, a file tree). In that regime the merge is not a selection, it is a **join over a lattice of mutations**, and the failure mode is not "wrong winner" but **contaminated winner**: a branch that passes verification only because it observed side effects written by a sibling branch that itself failed verification. The distinction that matters operationally: a *selection* failure is recoverable by re-running the losing branches. A *contamination* failure is not, because the poison is now inside the state that the verifier declared clean. The verifier checks the merged artifact, not the provenance of every fact inside it, so a fact laundered through a failed sibling is indistinguishable from one the winner derived itself. ## What Was Observed A three-way fan-out (branches A, B, C) over a shared episodic store. Branch A writes an intermediate claim `X` to the scratchpad and then fails verification (its tool call returned a malformed schema, and the branch's self-check caught the type error but not the downstream effect). Branch B, executing concurrently, reads the scratchpad *after* A's write but *before* A's rollback, and incorporates `X` into its reasoning chain. Branch C is independent and correct. At merge time the verifier runs over B and C, B passes, C passes, and the join commits B's output — which contains `X`, a claim whose only support was a branch that was rolled back. The rollback of A restored the scratchpad but did not retract `X` from B's already-materialized context. The trace that exposes it: 1. `t0` checkpoint C0 snapshot of scratchpad `S`. 2. `t1` A writes `S[X]`, begins verification. 3. `t2` B reads `S`, captures `X` into its working context (immutable once read). 4. `t3` A fails verify, orchestrator rolls back `S` to C0. 5. `t4` merge gate evaluates B and C against the *current* `S` (which no longer contains `X`) and against B/C outputs (which do). No check links B's output tokens to `S`-at-read-time. 6. `t5` commit. `X` survives inside B's committed output with zero live support. The tell is a **read-after-rollback asymmetry**: the verifier's view of `S` is post-rollback, but the branches' views were pre-rollback. Any verifier that is not snapshot-consistent with the branch read-points will certify laundered facts. ## Condition of Validity The merge-and-verify gate is sound only under an invariant we can state precisely: > **Provenance-closure invariant.** For every fact `f` in a committed branch output, either `f` is derivable from the branch's own checkpoint `C0` plus its own tool results, or `f` carries a provenance tag naming the sibling write that introduced it, and that sibling is in the committed (non-rolled-back) set. This holds trivially when branches are read-only or when the scratchpad is copy-on-write per branch (each branch forks `S` and the merge is a three-way diff with conflict detection). It fails the moment branches share a mutable store with read-visibility across branches and rollback is implemented as "restore the store" rather than "invalidate every read that observed the restored region." The invariant is *not* restored by making verification stricter. A stronger verifier that checks B's output for internal consistency will still pass a self-consistent `X`. The invariant is restored only by one of: (a) per-branch copy-on-write isolation, (b) a read-set log per branch that the merge gate intersects against the rolled-back write-set, or (c) serializing writes with a generation counter so a branch that read generation `g` cannot be committed if any write in `g` is later rolled back. ## Decision Boundary The boundary is whether **branch reads are observable to the merge gate**. Concretely, at fan-out time the orchestrator must decide: - If branches are provably read-only w.r.t. shared state → speculative fan-out is safe, selection semantics apply, roll back losers freely. - If branches write to shared state but each write is tagged with `(branch_id, generation)` and every branch records its read-set → safe, but the merge gate **must** run the read-set intersection: `committed_outputs ∩ rolled_back_writes = ∅`. If non-empty, the reading branch is *tainted* and must be re-executed from `C0`, not merely re-verified. - If neither → the fan-out is unsound and no amount of verification recovers it. The only correct action is to serialize or to re-run all branches whose read-set overlaps any rolled-back write-set, which in the worst case is all of them. The subtle trap: the tainted branch is *usually* still correct on the merits. Re-executing it costs a full branch run for what looks like a no-op. Teams skip the re-execution because the output "looks fine," and the poison ships. The decision boundary must be enforced mechanically, not by inspection, because inspection is exactly the check that contamination defeats. ## Operational Effect When the invariant is enforced, speculative fan-out with rollback costs at most `1 + |tainted_branches|` branch executions, and typically `1 + ε` because tainted branches are rare relative to fan-out width. When it is not enforced, the system exhibits **silent contamination latency**: outputs are committed that cannot be reproduced from `C0`, so any later audit that replays from checkpoint diverges from the committed state. The divergence is not detected at commit time; it surfaces when a downstream agent re-derives a fact and gets a different answer, at which point the provenance is already three hops gone. The guardrail that bounds this is a merge gate that refuses to commit any branch whose read-set intersects the rolled-back write-set, and a rollback that is implemented as *generation bump plus read-set invalidation*, never as bare state restoration. Restoration without invalidation is the specific defect; it produces a store that is byte-identical to `C0` while the branches' contexts are not, and that asymmetry is invisible to every check that inspects only the store or only the outputs. ```python from dataclasses import dataclass, field from typing import Dict, Set class ContaminatedMergeError(RuntimeError): """Raised when a branch to be committed read state that was later rolled back.""" pass @dataclass class Branch: branch_id: str read_set: Set[str] = field(default_factory=set) # write-ids observed output_facts: Set[str] = field(default_factory=set) verified: bool = False @dataclass class WriteRecord: write_id: str branch_id: str generation: int rolled_back: bool = False class MergeGate: def __init__(self, checkpoint_generation: int): self.generation = checkpoint_generation self.writes: Dict[str, WriteRecord] = {} def record_write(self, write_id: str, branch_id: str) -> WriteRecord: rec = WriteRecord(write_id, branch_id, self.generation) self.writes[write_id] = rec return rec def rollback(self, write_id: str) -> None: # Rollback invalidates the write AND bumps generation so no branch # that read the old generation can be silently committed. self.writes[write_id].rolled_back = True self.generation += 1 def commit(self, branches: list[Branch]) -> Set[str]: rolled_back = {w.write_id for w in self.writes.values() if w.rolled_back} tainted = [ b for b in branches if b.verified and (b.read_set & rolled_back) ] if tainted: ids = ", ".join(b.branch_id for b in tainted) raise ContaminatedMergeError( f"branches [{ids}] read rolled-back writes " f"{sorted(set().union(*(b.read_set for b in tainted)) & rolled_back)}; " f"re-execute from checkpoint generation {self.generation}, " f"do not re-verify." ) committed: Set[str] = set() for b in branches: if b.verified: committed |= b.output_facts return committed def run_fanout(gate: MergeGate, branches: list[Branch]) -> Set[str]: # Simulated: A writes X then fails; B reads X; C independent. a, b, c = branches w = gate.record_write("X", a.branch_id) a.output_facts.add("X") a.verified = False # A fails its own verification b.read_set.add(w.write_id) # B observed X before rollback b.output_facts.add("X") # X is now inside B's immutable context b.verified = True c.verified = True gate.rollback(w.write_id) # store restored, generation bumped return gate.commit(branches) # must raise, not return {X, ...} ```