# Why does an instance of speculative agent execution corrupt its parent state when a rolled-back branch writes to a shared reconciliation log **Date of Record:** 2026 November 17 **Scope:** Applies to orchestrators that fan out N speculative agent branches from a common checkpoint, execute them concurrently against shared external state (filesystems, databases, message queues, reconciliation logs), and roll back all but one branch on commit. Does not apply to branches that operate on fully isolated copy-on-write snapshots with no shared append-only sinks. ## Core Distinction The distinction that matters is between **rollback of branch-local state** and **rollback of effects already externalized to a shared sink**. A speculative branch is usually modeled as a transaction over its own working memory: on abort, you discard the branch's context, undo its scratchpad, and free its lease. That model is correct for private state. It is *wrong* for any append-only shared resource — reconciliation logs, audit trails, event buses, distributed locks with TTL, or a "last writer wins" coordination table — because those resources do not participate in the branch's rollback. When a losing branch has already appended a reconciliation intent record, rolling back the branch does not remove that record; the parent then reconciles against a log that contains phantom intents from branches that never committed. The parent does not corrupt itself because a branch failed. It corrupts itself because a branch *succeeded at writing* into a sink the parent treats as authoritative, and then the branch was discarded. ## What Was Observed In a fan-out of four branches speculating on a schema migration, branch B2 wrote a `reconcile_intent` entry to the shared log at T+40ms and then stalled on a downstream lock. The orchestrator's timeout fired at T+120ms, B2 was rolled back, and B4 committed. The parent then ran reconciliation, read the log, and found two intents — B2's phantom and B4's real one — for the same resource key. Because the log was append-only and keyed only by resource, the reconciler applied both, producing a double-apply that left the resource in a state matching neither branch. The failure was silent: no exception, no error-path, exit code zero. The tell was a monotonic counter (`applied_intents`) exceeding the number of committed branches. A secondary tell: the phantom intent carried a branch-scoped correlation ID that no live branch owned at reconciliation time — a dangling writer reference. ## Condition of Validity Rollback reconciliation is only sound when **every externalized effect is either (a) idempotent and keyed by a committed-branch token, or (b) fenced by a generation/epoch that the parent validates at reconcile time.** If the shared sink accepts unkeyed appends, or keys only by resource rather than by `(resource, branch_epoch)`, the invariant "log contents ⊆ committed branch effects" is unenforced and rollback is not a true rollback. The invariant holds only under the conjunction: append is fenced by a token the parent can verify, and the parent performs fence validation *before* applying any intent. Absent both, the failure is latent and will surface under any concurrency where a losing branch writes before its rollback. ## Decision Boundary The decision boundary is: **does the branch write to a sink the parent will later read as ground truth?** If no — branch state is private, rollback is complete, proceed with naive speculation. If yes — you must choose between (1) fencing every append with a `(branch_id, epoch)` token and having the reconciler reject intents whose token is not in the committed set, or (2) forbidding shared-sink writes from speculative branches entirely and routing all externalization through the parent after commit. Option 1 preserves parallelism at the cost of a validation pass; option 2 serializes externalization but removes the class of bug. Choosing neither — the common default — is the boundary violation. The boundary is crossed the moment a speculative branch is granted write access to an authoritative sink without a fence. ## Operational Effect Operationally, the effect is a **phantom-effect leak**: the shared log accumulates intents from branches that were rolled back, and the parent's reconciler, trusting the log, applies effects that no committed branch authorized. This manifests as double-application, divergent final state, and — critically — a green exit code, because the orchestrator's success signal is "a branch committed," not "the log contains only committed-branch effects." Detection requires asserting `len(committed_intents) == len(committed_branches)` and rejecting any intent whose writer token is not live at reconcile time. Remediation is fencing, not retry: retrying reconciliation against a polluted log re-applies the phantoms. The durable fix is to make the append itself conditional on a fence check, so a losing branch's write is rejected at the sink rather than silently absorbed. ```python from dataclasses import dataclass, field class PhantomIntentError(RuntimeError): """Raised when reconciliation observes an intent from a non-committed branch.""" @dataclass class Intent: resource: str branch_id: str epoch: int @dataclass class Branch: branch_id: str epoch: int committed: bool = False def reconcile( intents: list[Intent], branches: dict[str, Branch], committed_ids: set[str], ) -> list[Intent]: """Apply intents only if their writer is a committed, live branch. Enforces the invariant: log contents at reconcile time must be a subset of committed-branch effects. Any intent whose (branch_id, epoch) does not match a committed branch is a phantom and must abort reconciliation. """ applied: list[Intent] = [] for intent in intents: branch = branches.get(intent.branch_id) if branch is None: raise PhantomIntentError( f"intent for {intent.resource!r} references unknown branch " f"{intent.branch_id!r}" ) if intent.epoch != branch.epoch: raise PhantomIntentError( f"intent for {intent.resource!r} has stale epoch " f"{intent.epoch} (live {branch.epoch})" ) if intent.branch_id not in committed_ids or not branch.committed: raise PhantomIntentError( f"phantom intent: branch {intent.branch_id!r} wrote " f"{intent.resource!r} but was rolled back" ) applied.append(intent) return applied def safe_reconcile(intents, branches, committed_ids): applied = reconcile(intents, branches, committed_ids) if len(applied) != len(committed_ids): raise PhantomIntentError( f"effect count {len(applied)} != committed branches " f"{len(committed_ids)}; log is inconsistent" ) return applied ```