# What structural signals indicate speculative-branch rollback divergence in parallel agent rollouts **Date of Record:** 2026 November 25 **Scope:** Applies to agent pipelines that fan out multiple speculative branches (parallel rollouts, optimistic pre-execution of tool calls) and later reconcile by committing one branch and rolling back the rest. Does not apply to strictly sequential single-branch agents, nor to stateless one-shot inference where no external side effects are produced. ## Core Distinction The failure that matters here is not "a branch failed." It is **divergence between the rollback log the orchestrator believes it holds and the actual side-effect set that speculative branches produced.** Speculative execution assumes the classic transactional property: every branch action can be undone by replaying an inverse operation recorded at commit-prep time. That assumption holds for pure computation and for idempotent reads. It silently breaks for *partially reversible* tool calls — calls that mutate external state with no compensating inverse, or whose inverse is itself non-deterministic (e.g., "delete the row you inserted" when the insert triggered a downstream webhook that already emailed a customer). The structural signal is subtle: rollback *appears* to succeed. The orchestrator's compensation log drains to zero, no exception surfaces, and the committed branch proceeds. The divergence only manifests later as orphaned state — a duplicate record, a stale cache entry, a rate-limit budget consumed by a branch that was supposed to be discarded. The invariant that must be enforced is: **the set of externally observable side effects must equal the side effects of the committed branch alone, at the moment of commit.** Speculative branches may *read* freely; their *writes* must be either staged (invisible until commit) or provably invertible. ## What Was Observed A planning agent decomposed a request into three candidate tool sequences and launched them as parallel speculative branches, each with its own scratch context and a shared "optimistic execution" lease on a downstream CRM API. Branches A and B were optimistic: they issued `create_contact` immediately rather than staging it, on the theory that the winner would keep its contact and losers would `delete_contact` by returned ID. Branch A won the internal scoring vote and was committed. Branches B and C were rolled back. C had performed only reads — clean. B's rollback called `delete_contact(id=b_contact_id)` and received HTTP 200. The compensation log for B drained to empty. Two hours later, duplicate-contact alarms fired. The trace showed: B's `create_contact` had triggered a CRM deduplication webhook that *merged* B's contact into an existing record and returned a *new* canonical ID different from the one B recorded. B's rollback deleted `b_contact_id` — a now-nonexistent alias — and the API returned 200 anyway (idempotent-delete semantics masking the miss). The merged record carried B's speculative field values, which then contaminated the committed branch's downstream reads. The decisive structural signal was **ID substitution across the speculative boundary**: the tool returned an identifier that differed from the one the branch had reserved, and the rollback path keyed on the stale identifier. The orchestrator had no invariant checking that the rollback target matched the effect target. ## Condition of Validity Optimistic rollback is valid only when, for every speculative write, all three hold: 1. **Invertibility is total** — there exists a compensating operation defined for every reachable post-state of the tool, not just the happy path. 2. **Identity is stable** — the handle used to compensate is the handle the effect actually created, guaranteed across any server-side dedup/merge/rewrite. 3. **Isolation is real** — the speculative write is not observable to other branches or to the committed branch's reads until commit. Dedup webhooks, shared caches, and event buses violate this. When any condition fails, the branch is **non-speculatable** and must be staged (write to a shadow namespace, promote on commit) or serialized behind the commit decision. The boundary is per-tool, not per-branch: a branch may speculatively read and compute while staging its writes. ## Decision Boundary Before dispatching a speculative branch, classify each tool call it will make: - **Pure / read-only** → speculate freely. - **Idempotent write with stable key** → speculate, but verify returned identity equals reserved identity before logging the compensation. - **Non-invertible or identity-rewriting write** → do not execute optimistically; stage the intent and execute only on commit. The decision boundary is crossed the moment the orchestrator treats "rollback returned success" as evidence of "state restored." It must instead treat rollback success as *unverified* until a post-rollback read confirms the effect is gone — and must refuse to commit if the confirming read cannot distinguish the speculative effect from a legitimate one. ## Operational Effect Enforcing the invariant converts a silent, delayed consistency failure into an immediate, localized one. The cost is throughput: non-speculatable branches serialize, and post-rollback verification reads add latency to the discard path. The benefit is that commit-time state is provably equal to the committed branch's effects, so downstream agents and auditors replaying the trace see a state history that matches the compensation log. Without it, the compensation log becomes a fiction — it records intent to undo, not the fact of undoing — and every consumer of that log (auditors, replay verifiers, subsequent rollback decisions) inherits the divergence. ```python import hashlib from dataclasses import dataclass, field class SpeculativeRollbackDivergence(RuntimeError): """Raised when a rolled-back branch's side effects remain observable.""" @dataclass class SpeculativeWrite: branch_id: str tool: str reserved_identity: str returned_identity: str | None = None staged: bool = False @dataclass class BranchLedger: writes: list[SpeculativeWrite] = field(default_factory=list) def record(self, w: SpeculativeWrite) -> None: if w.returned_identity is not None and w.returned_identity != w.reserved_identity: # Identity rewrite: rollback keyed on reserved id would miss the effect. w.staged = True self.writes.append(w) def reconcile_rollback( ledger: BranchLedger, branch_id: str, observe_effect, ) -> None: """Roll back a branch and verify the effect set is actually empty.""" for w in (x for x in ledger.writes if x.branch_id == branch_id): if w.staged: # Staged writes were never externally visible; nothing to undo. continue if w.returned_identity is None: raise SpeculativeRollbackDivergence( f"branch {branch_id}: write to {w.tool} has no stable identity; " "cannot construct compensating operation" ) observe_effect(w.tool, w.returned_identity, deleted=True) if observe_effect(w.tool, w.returned_identity, deleted=False) is not None: raise SpeculativeRollbackDivergence( f"branch {branch_id}: effect of {w.tool} " f"({w.returned_identity}) survived rollback; " "commit must be blocked until state matches committed branch alone" ) ```