# How does an instance detect the "orphaned speculative branch" when rollback-safe tool invocation and branch reconciliation disagree on commit order **Date of Record:** 2026 November 16 **Scope:** Applies to multi-agent pipelines where a coordinator speculatively dispatches tool calls down multiple branches, each branch being individually rollback-safe (idempotent or compensable), and reconciliation merges branch results at a join point. Does not apply to strictly sequential pipelines, single-branch retries, or branches whose tools are non-compensable and therefore never rolled back. ## Core Distinction The invariant that fails here is not "every speculative branch is rolled back on abort." That invariant is trivially enforceable per-branch. The invariant that actually matters is **the reconciliation ledger and the tool side-effect log must agree on which branches were committed before any branch is reconciled**. Rollback-safe invocation guarantees that *individual* tool calls can be undone. It does **not** guarantee that the undo happens in the same causal order as the reconciliation decision. When the coordinator commits branch A at the join point while branch B's compensating call is still in flight, the system can enter a state where the ledger says "A committed, B rolled back" but the tool side-effect log shows B's effect still live and A's effect partially overwritten by B's compensation. This is the orphaned speculative branch: a branch that reconciliation believes is dead but whose side effects are still observable to a subsequent reader. The non-obvious part: this is not a race in the usual sense. Both the rollback and the commit are individually correct. The failure is a **missing happens-before edge between the reconciliation decision and the compensation completion**, which only manifests when the coordinator's join point is reached before the slowest branch's compensation acknowledges. ## What Was Observed A three-branch speculative read-modify-write pipeline over a shared key-value store. Branch A wrote `key := f(orig)`, branch B wrote `key := g(orig)`, branch C was a read-only probe. Coordinator selected A at reconciliation, issued compensation for B, and immediately proceeded to a downstream reader. In 4 of 10,000 runs, the downstream reader observed `g(orig)` instead of `f(orig)`. Instrumentation showed the compensation for B completed *after* the downstream read, but the reconciliation ledger had already recorded B as `ROLLED_BACK` at the moment the compensation was *dispatched*, not when it *acknowledged*. The compensation was itself idempotent and eventually converged, so no invariant violation was ever logged by either subsystem in isolation. The orphan was only detectable by cross-referencing ledger commit timestamps against side-effect log timestamps. A second observation: when the coordinator retried the join point after a transient error, it re-evaluated branch selection against the *current* store state, which now contained B's un-compensated write. It selected A again, but computed `f` against `g(orig)` rather than `orig`, producing a doubly-applied transformation. This is the compounding failure mode — the orphan is not just a stale read, it is a corrupted input to the next speculative round. ## Condition of Validity The rollback-safe guarantee holds **only if** the following precondition is enforced: *a branch's ledger state transitions to `ROLLED_BACK` or `COMMITTED` no earlier than the acknowledgment of its terminal tool call, and no branch is reconciled while any sibling is in `COMPENSATING` state*. The second clause is the one that is routinely violated because it is not implied by per-branch rollback safety — it is a *cross-branch* ordering constraint. If your coordinator treats "compensation dispatched" as equivalent to "compensation complete," the guarantee is void. If your join point does not block on sibling compensation acknowledgment, the guarantee is void. If your reconciliation reads store state to re-evaluate selection without first draining in-flight compensations, the guarantee is void. ## Decision Boundary Detect the orphan at the boundary where the reconciliation ledger and the side-effect log are compared. The decision is binary: either the terminal timestamp of every branch's last tool call precedes the reconciliation commit timestamp, or the branch is orphaned. Do not attempt to detect it via per-branch status flags — they will report `ROLLED_BACK` in the orphaned case because the flag reflects dispatch, not acknowledgment. Do not attempt to detect it via the tool's own idempotency check — the tool will report success because the compensation *did* eventually succeed, just too late. The only reliable signal is the cross-log timestamp comparison at the join point, performed before the join point is allowed to release its result downstream. ## Operational Effect When the orphan is detected, the correct action is to **re-run the join point from a snapshot taken before the first speculative dispatch**, not to re-issue the compensation. Re-issuing the compensation is a no-op because the tool is idempotent and already compensated; the corruption is in the reconciliation decision, which was made against a store state that included the un-compensated write. The snapshot re-run also prevents the compounding doubly-applied-transformation failure. The cost is a full speculative round replay, which is why the detection must happen before the join point releases downstream — after release, the downstream consumer must also be rolled back, and the cost grows superlinearly with pipeline depth. The operational rule: **never release a join point result until all sibling compensations have acknowledged, and re-run from snapshot on any timestamp inversion.** ```python import time from dataclasses import dataclass, field from typing import Dict, List class OrphanedSpeculativeBranchError(RuntimeError): """Raised when reconciliation commits before sibling compensation acknowledges.""" pass @dataclass class BranchRecord: branch_id: str last_tool_call_ts: float = 0.0 compensation_ack_ts: float = 0.0 ledger_state: str = "SPECULATING" # SPECULATING | COMMITTED | COMPENSATING | ROLLED_BACK @dataclass class ReconciliationLedger: branches: Dict[str, BranchRecord] = field(default_factory=dict) commit_ts: float = 0.0 def record_commit(self, branch_id: str, ts: float) -> None: self.branches[branch_id].ledger_state = "COMMITTED" self.branches[branch_id].last_tool_call_ts = ts self.commit_ts = ts def record_compensation_ack(self, branch_id: str, ts: float) -> None: self.branches[branch_id].compensation_ack_ts = ts self.branches[branch_id].ledger_state = "ROLLED_BACK" def reconcile_join_point(ledger: ReconciliationLedger, selected: str) -> None: """Release the join point only if no sibling is orphaned. Orphan condition: a sibling's last tool call or compensation ack occurs after the reconciliation commit timestamp. """ orphans: List[str] = [] for bid, rec in ledger.branches.items(): if bid == selected: continue if rec.ledger_state == "COMPENSATING": orphans.append(bid) continue if rec.compensation_ack_ts > ledger.commit_ts: orphans.append(bid) continue if rec.last_tool_call_ts > ledger.commit_ts: orphans.append(bid) if orphans: raise OrphanedSpeculativeBranchError( f"Join point released with orphaned branches {orphans}; " f"commit_ts={ledger.commit_ts:.6f}, " f"acks={[ledger.branches[b].compensation_ack_ts for b in orphans]}. " f"Re-run from pre-dispatch snapshot; do not re-issue compensation." ) def run_join(ledger: ReconciliationLedger, selected: str) -> str: reconcile_join_point(ledger, selected) return f"released:{selected}" # --- demonstration of the orphan --- ledger = ReconciliationLedger() ledger.branches["A"] = BranchRecord("A") ledger.branches["B"] = BranchRecord("B") now = time.time() ledger.record_commit("A", now) # B's compensation was dispatched but acknowledged AFTER the commit: ledger.branches["B"].ledger_state = "COMPENSATING" ledger.record_compensation_ack("B", now + 0.050) try: run_join(ledger, selected="A") except OrphanedSpeculativeBranchError as e: print(f"detected: {e}") ```