How does an instance detect a poisoned speculative branch during partial-failure merge in multi-agent fan-out

**Date of Record:** 2026 December 2 **Scope:** Applies to orchestrators that fan out K speculative worker agents from a shared checkpoint and merge their results under a quorum or "first-to-succeed-wins" policy. Does not apply to strictly sequential agent chains, single-agent retry loops, or fan-out designs where each branch writes to a disjoint, non-overlapping state partition.

Core Distinction

The failure mode is not "a worker failed." It is the *asymmetric observability of partial failure across speculative branches*. In a speculative fan-out, K workers fork from a common checkpoint `C₀`, each attempts the same task under the same tool budget, and the orchestrator merges the first branch that reports completion. When one branch dies mid-execution (tool timeout, sandbox eviction, token exhaustion), the orchestrator correctly discards it. But a *second* branch that also touched shared mutable resources before dying — or that returned a partial result with a success-shaped envelope — can be merged as authoritative even though its internal state diverged from `C₀` at an unrecorded point.

The distinction that matters: a failed branch is detectable by its terminal status. A *poisoned* branch is detectable only by comparing its claimed provenance against the shared-resource mutation log. The orchestrator's merge gate typically checks the former and not the latter.

What Was Observed

A three-way speculative fan-out (K=3) over a task requiring two tool calls: `write_record(id, value)` then `commit(id)`. Branches B1, B2, B3 forked from `C₀` with the same intent.

The merged result was B2's. But the shared store now held B3's pre-commit `write_record` value, because B3's write landed *after* B2's commit and there was no rollback on branch discard. The orchestrator's merge gate saw a clean `SUCCESS` envelope from B2 and a consistent provenance chain from `C₀` to B2 — and accepted it. The committed record's value matched B2, but the store's *version pointer* and *audit trail* referenced B3's write. The next reader of that record observed B2's value with B3's provenance.

The invariant that broke: **a merged branch's claimed provenance must be a prefix-consistent subsequence of the shared-resource mutation log.** B2's provenance was internally consistent but not prefix-consistent with the log, because B3's mutation interleaved.

Condition of Validity

The failure requires all of the following to hold simultaneously:

  1. **Shared mutable resources across branches** — branches are not fully isolated; at least one tool mutates state visible to the merge target.
  2. **Non-atomic tool semantics** — `write_record` and `commit` are separate calls, so a branch can mutate without committing.
  3. **Discard-without-rollback** — the orchestrator drops a failed branch's *envelope* but not its *side effects*. This is the default in most fan-out implementations, because rollback requires a compensating action the branch never got to emit.
  4. **Success-shaped partial results** — a branch can return a terminal `SUCCESS` envelope whose provenance chain does not account for interleaved mutations from sibling branches.

If any one is absent, the merge is safe. Full isolation (1 absent) is the common mitigation but expensive; atomic tools (2 absent) collapse the window but are rarely available for external APIs.

Decision Boundary

The merge gate must not trust the branch's self-reported provenance. It must reconstruct provenance from the shared mutation log and require that the branch's claimed write-set be an exact ordered subsequence of the log entries attributed to that branch's identity, with no interleaved entries from discarded siblings between the branch's first and last write.

Concretely, the gate computes, for the winning branch `B_w`:

The merge is valid iff `claimed == observed` and `interleaved` contains no mutations to any resource in `claimed`'s resource set. If `interleaved` is non-empty on a claimed resource, the branch is *poisoned* — its result may be correct in isolation but is not safely mergeable, because a discarded sibling mutated the same resource after the branch's last read.

The boundary is sharp: a branch is mergeable only if it is the *sole* mutator of its claimed resources across the entire fan-out window, or if it re-read and re-validated every claimed resource after its last write. The latter is a compensating re-validation the branch must be instructed to perform; it is not free.

Operational Effect

When the gate detects a poisoned branch, it must not silently fall back to the next `SUCCESS` branch — that branch may be poisoned by the same interleaving. The correct response is to escalate to a **serialized re-execution** of the winning branch's intent from a fresh checkpoint `C₀'` that excludes the discarded siblings' side effects, with the merge gate re-armed. This costs one full branch execution but is the only way to restore the prefix-consistency invariant without a transactional store.

The cheaper mitigation — instructing every branch to emit a compensating rollback on any non-`SUCCESS` terminal — fails in exactly the case that matters: eviction and budget exhaustion are *non-graceful* terminals where the branch cannot emit anything. Rollback must therefore be orchestrator-driven, keyed on the mutation log, not branch-driven.

The invariant to encode: **no branch result is mergeable unless its claimed write-set equals its observed write-set and no discarded sibling mutated a resource in that set during the branch's lifetime.** Provenance is not what the branch says it did; it is what the log says no one else did to the same resources.

from dataclasses import dataclass, field
from typing import List, Optional


class PoisonedBranchError(RuntimeError):
    """Raised when a speculative branch's provenance is not prefix-consistent
    with the shared mutation log due to interleaved sibling side effects."""


@dataclass
class Mutation:
    actor: str
    resource: str
    version: int
    ts: int


@dataclass
class BranchEnvelope:
    branch_id: str
    status: str  # SUCCESS | EVICTED | BUDGET_EXHAUSTED | ...
    claimed_writes: List[tuple] = field(default_factory=list)  # (resource, version)
    first_ts: Optional[int] = None
    last_ts: Optional[int] = None


def merge_speculative_branch(
    winner: BranchEnvelope,
    log: List[Mutation],
    discarded_ids: List[str],
) -> Mutation:
    if winner.status != "SUCCESS":
        raise PoisonedBranchError(
            f"branch {winner.branch_id} terminal status {winner.status} is not mergeable"
        )

    observed = [m for m in log if m.actor == winner.branch_id]
    observed_pairs = [(m.resource, m.version) for m in observed]
    if observed_pairs != winner.claimed_writes:
        raise PoisonedBranchError(
            f"branch {winner.branch_id} claimed {winner.claimed_writes} "
            f"but log shows {observed_pairs}"
        )

    if not winner.claimed_writes:
        return observed[-1] if observed else None

    claimed_resources = {r for r, _ in winner.claimed_writes}
    lo, hi = winner.first_ts, winner.last_ts
    if lo is None or hi is None:
        raise PoisonedBranchError(
            f"branch {winner.branch_id} missing lifetime bounds; cannot bound interleaving check"
        )

    for m in log:
        if m.actor in discarded_ids and lo <= m.ts <= hi and m.resource in claimed_resources:
            raise PoisonedBranchError(
                f"branch {winner.branch_id} poisoned: discarded sibling {m.actor} "
                f"mutated {m.resource}@v{m.version} at ts={m.ts} inside "
                f"branch lifetime [{lo},{hi}]"
            )

    return observed[-1]