# What structural signals indicate a speculative agent branch has crossed the point of no rollback during merge reconciliation **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrators that fan out N speculative agent branches from a shared checkpoint, execute them concurrently against external side-effecting tools, and reconcile a winning branch into canonical state. Does not apply to pure read-only fan-out or to branches whose only side effects are confined to an isolated, discardable sandbox. ## Core Distinction Speculative parallelism assumes every branch is *reversible* until the merge commits. The rollback contract is usually stated as "abort the losing branches and discard their state." This conflates two distinct things: **discarding local agent state** (always reversible — you drop the context window) and **unwinding externalized effects** (irreversible once a tool call has acknowledged). The boundary failure lives in the gap between them: a losing branch that has already emitted an irreversible side effect is not rolled back by dropping its context. The orchestrator sees a clean local abort and a dirtied external world. The structural signal that a branch has crossed the point of no rollback is not "the branch finished" or "the branch errored." It is the presence of at least one **committed external effect** in the branch's trace whose compensating action is either undefined or non-idempotent. Once any branch holds such an effect, the merge is no longer a selection over branch states — it is a reconciliation over a shared, mutated substrate, and the naive "pick the winner, drop the losers" algorithm silently corrupts it. ## What Was Observed A planning agent spawned three speculative branches (A: migrate via schema-copy, B: migrate via in-place ALTER, C: migrate via shadow-table swap) from a common checkpoint at step 12. Each branch was given a `write_ticket` tool bound to the same issue tracker and a `apply_migration` tool bound to a staging database. The orchestrator's merge policy was: score branches by a verifier, commit the highest scorer, roll back the rest. Branch C scored highest and was committed. Branches A and B were "rolled back" — their context windows discarded, their local file writes reverted in the sandbox overlay. But during execution, branch A had already called `write_ticket` (creating ticket #4471 documenting the schema-copy approach) and branch B had already called `apply_migration` against staging, which had partially run an `ALTER TABLE` before its verifier flagged a lock-contention timeout. Neither of these was in the sandbox overlay. Rolling back A and B discarded their *reasoning* but left #4471 open and staging in a half-migrated state that branch C's shadow-table swap then read as the source of truth. The failure was not detected at merge time. It surfaced two steps later when C's verification replay read staging and found a column that C's own migration had not created. The orchestrator attributed this to C, re-ran C, and produced a second shadow-table swap on top of the corrupted base. Error compounding, not a single fault. The structural signals, reconstructed from the trace: 1. **Effect ledger divergence.** Branch A's local trace recorded zero writes (its sandbox was clean); the external tool's audit log recorded one `create_ticket`. The two ledgers disagreed by exactly the set of non-sandboxed effects. This divergence is the primary signal. 2. **Non-compensable effect class.** Branch B's `apply_migration` had a compensating action (`rollback_migration`) that was *defined but non-idempotent* — it assumed the migration had completed. Against a partially-applied migration it errored, and the orchestrator treated the compensation error as non-fatal and proceeded to merge. 3. **Verifier scope mismatch.** The verifier scored branches on their local state and their *intended* effect set, not their *committed* effect set. C's high score was computed in ignorance of A and B's committed effects. 4. **Merge ordering.** The merge committed C before reconciling A and B's external effects. Reconciliation must precede commit; doing it after means the winner is chosen against a substrate that the losers already mutated. ## Condition of Validity The "pick winner, discard losers" rollback is valid if and only if, for every losing branch, the set of committed external effects is empty OR every committed effect has a defined, idempotent, and order-independent compensating action. This is a checkable invariant at merge time. It fails silently whenever: - Any tool call in any branch is classified as `irreversible` or `compensable_non_idempotent` and has been acknowledged by the external system, and - The merge policy does not enumerate committed effects across *all* branches before committing the winner. The invariant is *not* restored by sandboxing alone, because the failure mode is precisely the effects that escape the sandbox. It is also not restored by "roll back losers first, then commit winner," because compensation of a non-idempotent effect can itself fail and the orchestrator must treat that failure as a hard stop, not a warning. ## Decision Boundary The boundary between a safe speculative merge and an unsafe one is a single predicate evaluated at merge time, before any commit: ``` unsafe_merge = exists branch b in losers: exists effect e in committed_effects(b): e.class in {irreversible, compensable_non_idempotent} AND NOT compensation_succeeded(b, e) ``` If `unsafe_merge` is true, the orchestrator must not commit the winner. It must either (a) abort the entire fan-out and surface the orphaned effects to a human/repair agent, or (b) if the winning branch's effects are disjoint from the losers' effects, commit the winner and quarantine the losers' effects for explicit reconciliation. Option (b) is only valid when effect-disjointness is *proven* from the ledgers, not assumed. The subtle boundary case: a losing branch whose only committed effect is a *read* that informed a later write in the winning branch. This is not caught by the effect-class check because reads are usually classified reversible. But if the read observed state that the losing branch then mutated, the winning branch's subsequent reasoning is grounded in a substrate that is no longer true. The signal here is a **read-after-write dependency across branches** — the winning branch's trace contains a read whose value was produced by a losing branch's committed write. This must be detected by cross-referencing read timestamps against the effect ledger, not by inspecting branch-local traces. ## Operational Effect The practical consequence is that speculative fan-out must maintain a **global effect ledger** keyed by (branch_id, effect_id, effect_class, compensation_status, commit_timestamp), written by the tool layer, not the agent layer. The agent's local trace is untrustworthy for this purpose because the agent cannot see effects it did not intend and cannot see effects other branches committed. Merge reconciliation reads the global ledger, computes `unsafe_merge`, and gates commit on it. Second, the verifier must be re-scoped to score branches against *committed* effects, and the merge must run reconciliation *before* commit so that the winner is chosen against a substrate the losers have not yet mutated. Third, compensation failures must be promoted to hard stops — a `compensable_non_idempotent` effect whose compensation errors is functionally irreversible and must be treated as such. The invariant to enforce: **no branch's committed effects may be invisible to the merge decision.** Every effect that escapes the sandbox must be registered in the global ledger at the moment of acknowledgment, and the merge decision must be a pure function of that ledger plus the verifier scores. When this invariant holds, rollback of losers is safe; when it does not, "rollback" is a fiction and the orchestrator is committing a merge against a world it cannot see. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class EffectClass(Enum): REVERSIBLE = "reversible" COMPENSABLE_IDEMPOTENT = "compensable_idempotent" COMPENSABLE_NON_IDEMPOTENT = "compensable_non_idempotent" IRREVERSIBLE = "irreversible" class UnsafeSpeculativeMerge(RuntimeError): """Raised when a speculative merge would commit a winner while a losing branch holds an un-compensated irreversible or non-idempotent effect.""" pass @dataclass class Effect: branch_id: str effect_id: str effect_class: EffectClass compensation_status: str = "none" # none | succeeded | failed | undefined @dataclass class GlobalEffectLedger: effects: list = field(default_factory=list) def committed(self, branch_id: str): return [e for e in self.effects if e.branch_id == branch_id] def orphaned(self, losing_branches): orphans = [] for e in self.effects: if e.branch_id not in losing_branches: continue if e.effect_class in (EffectClass.IRREVERSIBLE, EffectClass.COMPENSABLE_NON_IDEMPOTENT): if e.compensation_status != "succeeded": orphans.append(e) return orphans def reconcile_merge(winner: str, losers, ledger: GlobalEffectLedger, winner_effects_disjoint: Optional[bool] = None) -> str: """Gate a speculative merge on the global effect ledger. Returns 'commit' or 'commit_with_quarantine'. Raises UnsafeSpeculativeMerge when the merge cannot be made safe. """ orphans = ledger.orphaned(losers) if not orphans: return "commit" # Orphans exist. Only a proven effect-disjoint winner may proceed. if winner_effects_disjoint is True: return "commit_with_quarantine" if winner_effects_disjoint is None: raise UnsafeSpeculativeMerge( f"cannot prove effect-disjointness for winner {winner!r} against " f"{len(orphans)} orphaned effect(s): " f"{[e.effect_id for e in orphans]}" ) raise UnsafeSpeculativeMerge( f"winner {winner!r} shares substrate with orphaned effects from losers: " f"{[(e.branch_id, e.effect_id, e.effect_class.value) for e in orphans]}" ) def detect_cross_branch_read_after_write(winner_reads, ledger: GlobalEffectLedger, losers) -> list: """Flag winner reads whose observed value was produced by a losing branch's committed write. These are invisible to the effect-class check and must be surfaced as reconciliation hazards.""" hazards = [] loser_writes = {e.effect_id: e for e in ledger.committed_effects_for(losers)} for r in winner_reads: if r.observed_effect_id in loser_writes: hazards.append((r, loser_writes[r.observed_effect_id])) return hazards ```