# What structural signals indicate that speculative branch commits are unsafe under merge reconciliation **Date of Record:** 2026 December 3 **Scope:** Applies to orchestrators that speculatively fork an agent's execution into parallel branches, run them concurrently, and reconcile via rollback-or-merge once a branch is selected. Does not apply to strictly sequential retry loops or to branches whose side effects are fully sandboxed and never observable outside the fork. ## Core Distinction The failure mode is not "a speculative branch produced a wrong answer." It is that a speculative branch produced a *correct* answer whose correctness depended on a world state that the merge step then invalidated. Speculative execution assumes branch independence: each fork observes the same pre-fork state, and the reconciler picks one winner. But agent branches are not pure functions—they emit tool calls, write memory, and mutate external resources. When two branches touch the same resource, the *loser's* write is not automatically undone by "discarding" the branch. The reconciler rolls back the branch's *reasoning trace* but not its *committed side effects*, and the winner's merge is then evaluated against a state the loser already contaminated. The structural signal is a **write-set overlap between a discarded branch and the merge target**, detectable before reconciliation but almost never checked, because the rollback contract is written against the reasoning state (context, plan, scratchpad) rather than the effect state (tool results, memory writes, resource handles). ## What Was Observed An orchestrator forks a task into three speculative branches at a decision point where the agent must choose a data-migration strategy. Each branch runs an agent with the same seed context but different strategy prompts. Branch A chooses a schema-first migration and issues `apply_migration(schema_v2)`. Branch B chooses a shadow-table migration and issues `create_shadow_table` then `backfill`. Branch C chooses in-place and issues `alter_column` on the live table. The reconciler scores branches on a verification rubric. Branch A wins. The reconciler discards B and C: it drops their context, marks their plans failed, and merges A's trace into the parent. But B's `create_shadow_table` already committed, and C's `alter_column` already mutated the live table that A's `apply_migration` now reads. A's migration applies cleanly *as scored*—its verification ran against a schema that C had already altered—but the merged parent state now contains an orphan shadow table and a column that no branch's plan accounts for. The parent agent's next step fails a schema assertion it has no record of violating, because the assertion was true at fork time and false at merge time. The tell: the branch verification scores were all computed *before* the losing branches' effect commits were visible to the scorer. The reconciler treated "branch discarded" as equivalent to "branch never happened." ## Condition of Validity Speculative merge is safe only when the following invariant holds at reconciliation time: > For every discarded branch *d* and the winning branch *w*, `effect_set(d) ∩ read_set(w) = ∅` and `effect_set(d) ∩ effect_set(w) = ∅`. In practice this requires the orchestrator to maintain a per-branch **effect ledger** separate from the reasoning trace, and to check ledger intersection—not trace intersection—before declaring a branch discardable. The ledger must record resource identifiers (table names, file paths, memory keys, API resource IDs), not just tool names, because `apply_migration` on two different tables does not conflict while `alter_column` and `apply_migration` on the same table do. The invariant also fails silently when effects are **non-idempotent and unrecorded**: a branch that sends an email, increments a counter, or appends to an external log leaves no rollback handle, so the ledger cannot even be constructed after the fact. Any effect without a compensating operation must be treated as permanently committed at fork time, which means it must be excluded from speculative execution entirely. ## Decision Boundary The boundary between "safe to speculate" and "must serialize" is drawn by two properties of each tool the branch may call: 1. **Compensability**: does a defined inverse operation exist (`create_shadow_table` → `drop_shadow_table`)? If not, the tool cannot appear in a speculative branch. 2. **Read-visibility**: can the tool's effect be observed by a sibling branch's read? If yes, and the siblings' read-sets are not disjoint by construction, the branches are not independent and must not be forked. A tool that is compensable but read-visible (e.g., a row insert into a shared table) is safe only if the reconciler applies compensations *before* scoring the winner, not after. A tool that is read-invisible but non-compensable (e.g., a fire-and-forget webhook) is safe only if it is deferred to post-merge commit, never executed inside the fork. The subtle case is **transitively read-visible effects**: `apply_migration` alters a schema that a later `select` in the *same* branch reads. That is fine—it is intra-branch. The failure is when the schema alteration is visible to a *sibling* branch's verification query, which happens whenever verification runs against live state rather than a branch-local snapshot. The boundary therefore also requires that each branch's verification execute against a snapshot isolated from sibling effects, or that verification be deferred until after all compensations. ## Operational Effect The practical consequence is that speculative agent execution needs a **two-phase reconciliation** rather than a single merge: a *compensation phase* that reverses every discarded branch's ledgered effects, followed by a *merge phase* that scores and commits the winner against the now-clean state. Scoring before compensation produces a winner whose score is contaminated by losers' effects; scoring after compensation may change the winner, which is correct but requires the reconciler to tolerate a winner flip. Orchestrators that skip the compensation phase exhibit a characteristic symptom: the merged parent state is internally inconsistent in a way no single branch's trace explains, and re-running the winning branch in isolation succeeds while re-running it after a merge fails. That divergence between isolated and merged execution of the *same* winning branch is the diagnostic signature of an uncompensated speculative effect. The guardrail is to make the effect ledger a first-class artifact of the fork, to refuse speculation for any tool lacking a registered compensator, and to assert ledger emptiness for all discarded branches before the merge commits. ```python from dataclasses import dataclass, field from typing import Optional class UncompensatedSpeculativeEffect(RuntimeError): """Raised when a discarded branch left effects that the merge cannot unwind.""" pass @dataclass class Effect: resource_id: str tool: str compensator: Optional[str] # None => non-compensable, must not speculate committed: bool = False @dataclass class Branch: name: str effects: list[Effect] = field(default_factory=list) read_set: set[str] = field(default_factory=set) def commit_effect(self, effect: Effect) -> None: if effect.compensator is None: raise UncompensatedSpeculativeEffect( f"branch {self.name} attempted non-compensable effect " f"{effect.tool} on {effect.resource_id}; refuse to speculate" ) effect.committed = True self.effects.append(effect) def reconcile(winner: Branch, discarded: list[Branch]) -> None: # Phase 1: compensate every discarded branch before scoring/merging. for branch in discarded: for effect in branch.effects: if effect.committed and effect.compensator is None: raise UncompensatedSpeculativeEffect( f"discarded branch {branch.name} left committed, " f"non-compensable effect {effect.tool} on {effect.resource_id}" ) # apply effect.compensator(effect.resource_id) here effect.committed = False # Phase 2: assert the winner's reads are no longer shadowed by losers. for branch in discarded: overlap = {e.resource_id for e in branch.effects} & winner.read_set if overlap: raise UncompensatedSpeculativeEffect( f"winner {winner.name} reads {overlap} touched by " f"discarded branch {branch.name} after compensation" ) # merge winner trace into parent here ```