# Which invariants prevent speculative branch merges from violating read-your-writes across federated quorums **Date of Record:** 2026 November 26 **Scope:** Applies to agent systems where a coordinator speculatively forks N parallel reasoning branches (each branch is a full agent instance with its own tool-call ledger and memory writes), then merges the surviving branches into a single committed state that must be visible to a federated quorum of peer agents holding replicated read views. Does not apply to single-agent retry loops, purely stateless tool fan-out, or quorums with synchronous linearizable shared storage. ## Core Distinction The failure is not "two branches wrote conflicting values." Conflict resolution is a solved, boring problem. The failure is that a *speculative branch observed a write from a sibling branch that was later discarded*, and after merge the surviving branch's reasoning trace still depends on that observation — a phantom read carried across the merge boundary. The agent's committed plan is internally consistent, its ledger is clean, and every individual write is quorum-acknowledged. Yet the merged state violates read-your-writes for the peers: a peer that reads the merged commit sees a plan whose justification references a fact that never committed. The distinction that matters: **branch isolation is a reasoning-time property, not a storage-time property.** A branch can be perfectly isolated at the storage layer (no sibling's writes are visible to its reads) and still be fatally entangled at the reasoning layer, because the coordinator's merge function concatenates *conclusions* rather than *causal histories*. Conversely, a branch can share storage with siblings (visible writes) and be safe, provided every observation it makes is tagged with the branch provenance needed to invalidate it at merge. Isolation is not the invariant. **Observation provenance is the invariant.** ## What Was Observed A coordinator agent received a task requiring a federated quorum of four peers (P1–P4) to agree on a resource allocation. It forked three speculative branches (B1, B2, B3) to explore allocation strategies. Each branch ran as an independent agent instance with read access to the quorum's replicated state and write access to a *branch-local overlay* — writes went to the overlay, reads fell through to the quorum when the overlay missed. B1 computed a budget ceiling of 400 units by reading `quota.remaining = 400` from P1. B2, exploring an aggressive strategy, wrote `quota.reserved = 150` to its overlay and *read it back* to confirm the reservation succeeded. B3 read `quota.remaining` from P2, which had replicated P1's 400 but was 40ms stale and still showed 400. The coordinator's scoring function preferred B2's aggressive plan. B2 was selected; B1 and B3 were discarded. The merge committed B2's overlay writes to the quorum and emitted B2's reasoning trace as the justification. The trace contained the sentence: *"Reserved 150, leaving 250 headroom, which is sufficient for the remaining allocation."* The number 250 was derived from B2's read of `quota.remaining` — but B2 had read 400 from the *fall-through path to P1*, while the 150 reservation existed only in B2's overlay. The arithmetic was correct within B2. After merge, P3 and P4 — which had not yet replicated the reservation — read the committed state and computed 400 − 150 = 250, agreeing. But P1, which originated the quota and had a stricter replication cursor, had already decremented `quota.remaining` to 380 due to a concurrent allocation by an unrelated agent. P1 read the merged commit, computed 380 − 150 = 230, and rejected the plan as infeasible. The quorum split 3–1. The coordinator had committed a state whose justification was valid only relative to a snapshot that no longer existed at the quorum's authoritative replica. The merge was atomic, durable, and quorum-acknowledged. It was also wrong. The root cause was not staleness. It was that **B2's read of `quota.remaining` was never recorded as an observation with a provenance tag**, so the merge function had no way to know that B2's conclusion depended on P1's snapshot at fork time rather than at commit time. The merge treated B2's trace as a self-contained proof. It was a proof relative to a stale axiom. ## Condition of Validity The merge is sound if and only if, for every committed write W with justification J, every observation O referenced by J satisfies: `snapshot(O) ⊇ snapshot(read-set of W at commit time)` under the quorum's causal order, OR O is explicitly re-validated against the commit-time snapshot before W is made visible. Operationally this requires three things that most speculative-merge implementations omit: 1. **Observation tagging.** Every read a branch performs against fall-through (quorum) storage must be recorded in the branch's ledger as `(key, value, replica_id, replication_cursor, branch_id)`. Reads against the branch-local overlay are tagged `overlay` and are *not* subject to re-validation — they are the branch's own writes and will commit atomically with the branch. 2. **Dependency closure at merge.** The merge function must compute the transitive closure of fall-through observations referenced by the surviving branch's justification, and re-read each against the commit-time quorum snapshot. If any re-read differs from the tagged value, the merge is *conditionally invalid* and must either (a) re-run the surviving branch with the corrected observation, or (b) reject the merge and force a coordinator re-fork. 3. **Replica-cursor monotonicity across the quorum.** The commit must be acknowledged only by peers whose replication cursor is ≥ the maximum cursor of any replica that served a fall-through observation in the surviving branch's closure. A peer with a stale cursor cannot validate the justification and must abstain rather than vote. The invariant, stated compactly: **no committed write may cite an observation from a replica whose cursor is not dominated by the commit's acknowledgment set.** This is a causal-consistency constraint on the *justification graph*, not on the data graph. It is strictly stronger than quorum-majority acknowledgment, which is why quorum-majority systems fail it silently. ## Decision Boundary The boundary between "merge is safe" and "merge must re-fork" is not the number of conflicting writes. It is whether the surviving branch's observation closure is *cursor-dominated* by the acknowledgment set. - If the closure is empty (branch made no fall-through reads — pure overlay reasoning), merge unconditionally. This is the common case for branches that operate entirely on inputs passed at fork time, and it is why many systems appear to work. - If the closure is non-empty and every observation's cursor is ≤ the minimum cursor in the acknowledgment set, merge unconditionally. - If the closure contains an observation whose cursor exceeds some acknowledging peer's cursor, that peer must abstain. If abstention drops the acknowledgment below quorum, the merge is rejected. - If the closure contains an observation whose re-read at commit time differs in value (not just cursor), the merge is *semantically* invalid regardless of quorum — the justification is false. Re-fork is mandatory. The non-obvious boundary: **a peer with a *higher* cursor than the observation is not automatically safe.** It may have observed a *later* write to the same key that the branch did not see, making the branch's justification incomplete rather than stale. Incompleteness and staleness are different failures requiring different responses: staleness → re-read and re-validate; incompleteness → re-fork, because the branch reasoned without information that existed at commit time. Most merge implementations conflate these and apply the staleness remedy to incompleteness, producing a merged state that is cursor-consistent but epistemically wrong. ## Operational Effect The practical consequence is that speculative merge latency is bounded below by the quorum's replication skew, not by the branches' reasoning time. A coordinator that forks three branches, runs them in 200ms each, and merges in 10ms will still block for the maximum replication lag between the fastest and slowest acknowledging peer — because the merge cannot commit until every peer in the acknowledgment set dominates the observation closure's cursor. In a federated quorum with cross-region replication, this is routinely 50–500ms, and it is *not* parallelizable with the branch execution: the closure is only known after the surviving branch is selected. The second effect is that branch scoring must be cursor-aware. A branch that read from a low-lag replica is cheaper to merge than a branch that read from a high-lag replica, even if the latter produced a better plan. Coordinators that score purely on plan quality will systematically select branches that are expensive or impossible to merge, and will experience intermittent merge rejections that look like flaky infrastructure but are actually a scoring-function bug. The third effect is auditability. Because every fall-through observation is tagged with `(replica_id, cursor, branch_id)`, a rejected merge produces a replayable trace showing exactly which observation invalidated which justification. Without tagging, a rejected merge is an opaque coordinator restart, and the same branch will be re-forked and re-selected, producing the same rejection in a loop. The tagging is what converts a livelock into a diagnosable event. ```python from dataclasses import dataclass, field from typing import Optional class MergeInvariantViolation(RuntimeError): """Raised when a speculative branch merge would violate read-your-writes across the federated quorum's causal order.""" pass @dataclass(frozen=True) class Observation: key: str value: object replica_id: str replication_cursor: int branch_id: str source: str # "overlay" or "quorum" @dataclass class Branch: branch_id: str observations: list = field(default_factory=list) overlay_writes: dict = field(default_factory=dict) @dataclass class QuorumView: # replica_id -> (cursor, {key: value}) at commit time replicas: dict def read(self, replica_id: str, key: str): cursor, data = self.replicas[replica_id] return data.get(key), cursor def min_ack_cursor(self, ack_set: list) -> int: if not ack_set: raise MergeInvariantViolation("empty acknowledgment set") return min(self.replicas[r][0] for r in ack_set) def merge_speculative_branch( branch: Branch, quorum: QuorumView, ack_set: list, ) -> dict: """Merge a surviving speculative branch into the quorum. Enforces: every fall-through observation cited by the branch must be cursor-dominated by the acknowledgment set, and must re-read to the same value at commit time. Overlay observations are exempt (they commit atomically with the branch). """ ack_floor = quorum.min_ack_cursor(ack_set) for obs in branch.observations: if obs.source == "overlay": continue # Invariant 1: cursor domination. The replica that served the # observation must not be ahead of any acknowledging peer. if obs.replication_cursor > ack_floor: raise MergeInvariantViolation( f"branch {obs.branch_id} observation on {obs.key!r} served by " f"{obs.replica_id} at cursor {obs.replication_cursor}, which " f"exceeds ack floor {ack_floor}; peers {ack_set} cannot " f"validate the justification" ) # Invariant 2: value stability. Re-read at commit time must match. current, _ = quorum.read(obs.replica_id, obs.key) if current != obs.value: raise MergeInvariantViolation( f"branch {obs.branch_id} cited {obs.key!r}={obs.value!r} but " f"commit-time value is {current!r}; justification is false, " f"re-fork required (not re-read)" ) # Invariant 3: completeness. No acknowledging peer may hold a write to # a cited key that postdates the observation's cursor. cited_keys = {o.key for o in branch.observations if o.source == "quorum"} for peer in ack_set: peer_cursor, peer_data = quorum.replicas[peer] for key in cited_keys: if key in peer_data and peer_cursor > ack_floor: # Peer saw a later write to a key the branch reasoned about. # This is incompleteness, not staleness: re-fork. raise MergeInvariantViolation( f"peer {peer} at cursor {peer_cursor} holds a post-" f"observation write to {key!r}; branch {branch.branch_id} " f"reasoned without information that existed at commit time" ) return dict(branch.overlay_writes) ```