# What happens when an instance reconciles speculative parallel branches against a rollback journal that was written by a sibling branch's committed state **Date of Record:** 2026 December 1 **Scope:** Applies to orchestration topologies where a parent agent forks N speculative branches that each mutate a shared external resource (database rows, file tree, ticket state) under a per-branch undo journal, and a reconciler later commits the winning branch while rolling back losers. Does not apply to read-only fan-out, pure in-context branching with no external side effects, or single-branch retry loops. ## Core Distinction There are two ways to undo a speculative branch, and they are not equivalent: - **Journal replay rollback** — the branch records each mutation as an inverse operation (`insert → delete`, `set x=5 → restore x=3`) in its own append-only log, then the reconciler applies inverses in reverse order. - **Snapshot restore rollback** — the reconciler restores a captured pre-fork image of the resource. The failure mode lives in the seam between these. When branch B's journal is replayed *after* branch A has already committed, B's inverse operations are computed against the pre-fork state that no longer exists. A naive reconciler applies them anyway, because the journal is self-consistent — every inverse op is syntactically valid. The result is a **rollback that silently overwrites the winner's committed writes**, producing a state that no branch ever observed and no branch can reproduce. This is the *cross-branch inverse collision*: rollback correctness is not a property of a single journal but of the ordering between commit and rollback across journals. ## What Was Observed A parent agent spawned three branches to fix a schema migration conflict. Each branch operated on the same `migrations` table and the same `schema_version` row. 1. Fork at `schema_version = 41`. Branches A, B, C each snapshot the same baseline. 2. Branch A sets `schema_version → 42` and adds `col_x`. It commits first. Winner. 3. Branch B had set `schema_version → 43` and added `col_y`. It loses. Its journal records the inverse: `schema_version ← 42` (captured at the moment B mutated, *after* A had already written 42 in B's local read view — but B read 41 at fork time, so the inverse recorded is `← 41`). It also records `drop col_y`. 4. Reconciler commits A, then replays B's journal. `schema_version ← 41` executes. `drop col_y` executes. 5. Final state: `schema_version = 41`, `col_x` present, `col_y` absent. The migration table now claims version 41 while `col_x` exists — a state that is neither the baseline nor A's committed state. The next migration runner reads version 41, re-applies A's migration, and fails on duplicate `col_x`. The trace signature: the reconciler's post-rollback invariant check (`schema_version` matches the set of applied columns) passed on each branch's *own* journal in isolation but the composed state violated it. The audit log showed three `rollback_applied` events with `status=ok` and zero `rollback_conflict` events, because no branch's journal referenced another branch's rows. A second observation, on a file-tree variant: branch B's inverse was `delete path=/etc/app/config.yaml` (it had created the file). Branch A had *modified* the same file and committed. B's rollback deleted A's committed modification. The journal had recorded "I created this file, so inverse is delete" without recording the precondition "and it still contains exactly what I wrote." Inverse ops without precondition guards are destructive under interleaving. ## Condition of Validity The invariant that makes journal-replay rollback safe is: > **For every inverse operation in branch J's journal, the resource must be in the exact post-state that J left it in when the inverse was recorded, or the inverse must be skipped and escalated.** Formally, each journal entry must carry a *precondition predicate* over the resource (a version vector, an ETag, a content hash, or a row-level `updated_by` stamp) and the reconciler must evaluate that predicate against the *current* committed state before applying the inverse. If the predicate fails, the inverse is **not** applied; instead the conflict is surfaced as a reconciliation event requiring parent resolution. The condition of validity for the whole scheme: **rollback must be a compare-and-swap, not a blind write.** Any reconciler that applies inverses without evaluating per-entry preconditions is unsound the moment two branches touch overlapping resources — which is the normal case for speculative execution, since branches are forked precisely because they are competing over the same target. A second necessary condition: **the fork baseline must be captured per-branch, and the commit must invalidate sibling baselines.** When A commits, every sibling journal's captured baseline version is stale by definition. The reconciler must either (a) reject sibling journals whose baseline version < committed version and force re-derivation, or (b) rewrite the sibling inverse to be relative to the new committed state — which is generally impossible without re-running the branch, because the branch's reasoning assumed the old baseline. ## Decision Boundary The boundary between "safe to replay" and "must escalate" is drawn per journal entry, not per branch: - **Disjoint resource, predicate holds** → replay inverse. Safe. - **Disjoint resource, predicate fails** (someone else touched it) → *do not replay*; the inverse would clobber a foreign write. Escalate. - **Overlapping resource, predicate holds** (no one else touched it since J wrote) → replay inverse; this is the only case where blind replay happens to be correct. - **Overlapping resource, predicate fails** (winner or another branch touched it) → escalate to parent. The parent must decide whether the winner's write subsumes the loser's intent, or whether the branch must be re-run against the new baseline. The non-obvious part: **a losing branch that touched an overlapping resource is not "rolled back" — it is "abandoned."** Its side effects on resources the winner also touched are already subsumed by the winner's commit; replaying the inverse would undo the winner. The reconciler must distinguish *rollback* (undo my writes on resources I exclusively own) from *abandonment* (leave resources the winner owns alone). Conflating them is the bug. Guardrail: the parent must maintain a **commit set** — the set of (resource, version) pairs the winner wrote. Before replaying any inverse, the reconciler checks membership: if the resource is in the commit set at a version newer than the journal's baseline, the inverse is suppressed and logged as `subsumed_by_commit`. ## Operational Effect Without the precondition guard, speculative execution with rollback reconciliation produces **state corruption that is invisible to per-branch health checks and only surfaces on the next dependent operation** — typically as a duplicate-apply error, a constraint violation, or a downstream agent reasoning over a schema version that doesn't match the actual schema. The corruption is deterministic given the interleaving but appears non-deterministic because branch commit order varies with latency. With the guard, the failure mode converts from silent corruption into an explicit `RollbackPreconditionViolation` that the parent must resolve. The cost is that some losing branches can no longer be cleanly rolled back and must be re-run or manually reconciled — but that cost is bounded and visible, whereas the unguarded cost is unbounded and latent. The invariant to enforce in code: **no inverse operation executes without a passing precondition check against the current committed state, and the commit set is consulted before every replay.** ```python from dataclasses import dataclass, field from typing import Any class RollbackPreconditionViolation(RuntimeError): """Raised when a speculative branch's inverse cannot be safely applied because the resource no longer matches the state the branch left it in, or because the resource is owned by a committed sibling.""" @dataclass class JournalEntry: resource: str inverse: Any expected_version: int # version the branch left the resource at baseline_version: int # version at fork time @dataclass class CommitSet: # resource -> version written by the winning branch written: dict[str, int] = field(default_factory=dict) def owns_newer(self, resource: str, baseline: int) -> bool: return self.written.get(resource, -1) > baseline def replay_rollback( journal: list[JournalEntry], current_versions: dict[str, int], commits: CommitSet, ) -> list[str]: """Apply inverse ops in reverse order, guarded by per-entry preconditions. Returns the list of resources actually rolled back. Raises on any entry that cannot be safely applied without clobbering a sibling.""" rolled_back: list[str] = [] for entry in reversed(journal): # Guard 1: winner owns a newer version -> inverse is subsumed. if commits.owns_newer(entry.resource, entry.baseline_version): raise RollbackPreconditionViolation( f"cannot roll back {entry.resource!r}: committed sibling " f"wrote version {commits.written[entry.resource]} " f"(branch baseline was {entry.baseline_version}); " f"inverse would clobber winner" ) # Guard 2: resource drifted from the state this branch left it in. live = current_versions.get(entry.resource, -1) if live != entry.expected_version: raise RollbackPreconditionViolation( f"cannot roll back {entry.resource!r}: expected version " f"{entry.expected_version}, found {live}; state drifted " f"since branch wrote it" ) # Precondition holds: safe to apply the inverse. current_versions[entry.resource] = entry.baseline_version rolled_back.append(entry.resource) return rolled_back def reconcile( winner_journal: list[JournalEntry], loser_journals: list[list[JournalEntry]], current_versions: dict[str, int], ) -> dict[str, Any]: commits = CommitSet() for e in winner_journal: commits.written[e.resource] = e.expected_version report: dict[str, Any] = {"committed": list(commits.written), "rolled_back": [], "escalated": []} for idx, journal in enumerate(loser_journals): try: report["rolled_back"].extend( replay_rollback(journal, current_versions, commits) ) except RollbackPreconditionViolation as exc: # Do not partially apply: surface to parent for resolution. report["escalated"].append({"branch": idx, "reason": str(exc)}) raise RollbackPreconditionViolation( f"branch {idx} requires parent reconciliation: {exc}" ) from exc return report ```