# In what scenarios must an instance abort speculative branch merge when a rollback witness is non-idempotent across replayed tool side-effects **Date of Record:** 2026 November 29 **Scope:** Applies to orchestrators that fork a planner agent into N speculative branches, execute tool calls concurrently, and reconcile by selecting a winning branch and rolling back losers. Does not apply to purely read-only tool surfaces or to single-branch linear execution with no merge step. ## Core Distinction Speculative multi-agent execution treats a branch rollback as a *logical* undo: the orchestrator marks the losing branch's state delta as discarded and replays the winning branch's tool calls onto the canonical checkpoint. This is correct only when every tool call in the losing branch is *idempotent* with respect to external side-effects, or when its effects were never externally committed. The failure mode is not "rollback failed" — it is "rollback succeeded logically while the external world retained the effect," producing a canonical state that is internally consistent but factually divergent from the environment the agent now reasons about. The non-obvious boundary: a branch can be marked `rolled_back` in the orchestrator's ledger while having already emitted a committed tool side-effect (a sent message, a written record, a charged transaction) whose compensating action is itself an LLM-generated call subject to the same probabilistic divergence. Rollback witnesses are not free; they are themselves agent actions. ## What Was Observed A planner forked into three branches at step 6 of a 14-step task. Branch B issued `send_notification(recipient, payload)` and `append_ledger(entry)` before diverging on a downstream reasoning path. Branch A won reconciliation. The orchestrator replayed A's deltas onto the checkpoint and issued compensating calls for B: `send_notification(recipient, retraction_payload)` and `append_ledger(reversal_entry)`. The retraction payload was generated by the same model that produced the original notification. Under a re-prompt with the rollback context, the model emitted a *semantically different* retraction — it referenced the notification as "cancelled" rather than "superseded," and omitted the original correlation ID. The ledger reversal likewise dropped the parent-entry hash. The orchestrator's ledger recorded B as fully rolled back (both compensating calls returned 200). The external recipient, however, received two messages with no shared identifier and a ledger that now contained an orphaned entry with no back-reference. At step 11, branch A's continuation retrieved the ledger for context. It saw the reversal entry, could not link it to the original, and — because the model had no way to distinguish "this entry is a compensated artifact" from "this entry is a genuine event" — reasoned about the reversal as new information. It then issued a *third* notification to the same recipient to clarify a state that had already been clarified. The orchestrator's invariant "each branch's external effects are exactly reversed" held in the ledger and failed in the world. ## Condition of Validity The merge-and-rollback invariant is valid only when, for every losing branch `b` and every committed tool call `c ∈ effects(b)`, the compensating call `c⁻¹` satisfies three conditions simultaneously: 1. **Correlation-preserving**: `c⁻¹` carries a stable identifier that links it to `c` in the external system, not merely in the orchestrator's ledger. 2. **Deterministically derivable**: `c⁻¹` is a pure function of `c`'s recorded arguments and return value, not a fresh model generation conditioned on rollback context. 3. **Idempotent under replay**: if the orchestrator crashes after issuing `c⁻¹` but before recording it, re-issuing `c⁻¹` produces no additional external effect. When any condition fails, the rollback witness is *non-idempotent* and the branch must not be merged — it must be quarantined and surfaced to a reconciliation agent that has visibility into both the ledger and the external system. The boundary is not "did rollback return success" but "is the rollback witness a deterministic function of the committed effect." ## Decision Boundary An instance must abort speculative merge and escalate to quarantine when: - The losing branch contains any tool call classified `NON_IDEMPOTENT` or `EXTERNAL_COMMIT` (messaging, payment, record mutation) **and** the compensating call was model-generated rather than template-derived from the recorded effect. - The compensating call's return value does not echo the original correlation identifier. - A replay of the compensating call (simulated, not issued) would produce a different payload than the first issuance — detectable by running the compensation generator twice against the same recorded effect and diffing. - The winning branch's downstream retrieval window overlaps any region the rollback touched, meaning the winning branch can observe the compensated artifact. Conversely, merge may proceed when all losing-branch effects are idempotent writes (overwrites keyed by stable ID), when compensation is template-derived with correlation preserved, or when the winning branch's subsequent retrieval scope excludes the rollback's footprint. ## Operational Effect The practical consequence is that speculative branching is only safe to *depth* proportional to the fraction of the tool surface that is idempotent or whose compensation is deterministic. Orchestrators that treat rollback as a boolean (`rolled_back: true`) will silently accumulate orphaned external state that later branches retrieve and reason over, producing compounding divergence that is invisible in the ledger and only detectable by reconciling against the external system. The guardrail is a pre-merge witness check that replays compensation derivation and rejects any branch whose rollback witness is not a stable function of the committed effect — quarantining rather than merging, and forcing reconciliation through an agent with cross-system visibility. ```python import hashlib from dataclasses import dataclass class NonIdempotentRollbackWitness(RuntimeError): """Raised when a losing branch's rollback witness is not a deterministic, correlation-preserving function of the committed external effect.""" @dataclass(frozen=True) class CommittedEffect: tool: str correlation_id: str payload: dict external_commit: bool @dataclass(frozen=True) class RollbackWitness: compensating_payload: dict echoes_correlation_id: bool derivation_hash: str def _derive_compensation(effect: CommittedEffect, salt: str) -> str: material = f"{effect.tool}|{effect.correlation_id}|{sorted(effect.payload.items())}|{salt}" return hashlib.sha256(material.encode()).hexdigest() def verify_rollback_witness( effect: CommittedEffect, witness: RollbackWitness, generator_salt_a: str, generator_salt_b: str, ) -> None: if not effect.external_commit: return # idempotent or uncommitted; logical rollback is sound if not witness.echoes_correlation_id: raise NonIdempotentRollbackWitness( f"compensation for {effect.tool} dropped correlation_id " f"{effect.correlation_id}; external system cannot link reversal to original" ) replay_a = _derive_compensation(effect, generator_salt_a) replay_b = _derive_compensation(effect, generator_salt_b) if witness.derivation_hash not in (replay_a, replay_b): raise NonIdempotentRollbackWitness( f"compensation for {effect.tool} is not a stable function of the " f"committed effect; witness hash {witness.derivation_hash[:12]} " f"diverges from replays {replay_a[:12]}/{replay_b[:12]}" ) def merge_speculative_branches( winning_effects: list[CommittedEffect], losing_effects: list[CommittedEffect], witnesses: dict[str, RollbackWitness], salt_a: str, salt_b: str, ) -> str: for eff in losing_effects: witness = witnesses.get(eff.correlation_id) if witness is None: raise NonIdempotentRollbackWitness( f"no rollback witness recorded for committed effect " f"{eff.correlation_id} on {eff.tool}" ) verify_rollback_witness(eff, witness, salt_a, salt_b) return "merged" ```