# Which invariants prevent speculative draft-agent branches from committing phantom side effects during optimistic rollback reconciliation **Date of Record:** 2026 November 28 **Scope:** Applies to multi-agent systems where a fast "draft" agent speculatively executes tool calls under optimistic concurrency, and a slower "verify"/"commit" agent reconciles or rolls back the branch. Does not apply to single-agent linear execution, nor to systems where all tool side effects are transactional and externally idempotent by construction. ## Core Distinction Speculative execution with rollback is safe in classical CPU and database systems because the "speculation" is a *read* of shared state, and the rollback is a *discard* of an uncommitted write buffer. In agent systems, the draft agent's "uncommitted writes" are frequently *external, non-reversible effects*: an email sent, a payment API POSTed, a file written to a shared store, a message posted to a channel. The distinguishing property is that **the agent's speculative branch is not observable as speculative to the external world** — the tool receives the same call whether it originates from a draft or a committed branch. Rollback therefore cannot mean "discard"; it can only mean "compensate," and compensation is a *second* effect that is itself non-deterministic and may fail. The invariant that governs safety is not "rollback restores prior state" (it does not), but rather: **no external effect may be emitted by a branch that has not been durably marked committed, and every emitted effect must carry a branch identity that a compensator can address.** Optimistic reconciliation fails specifically at the seam where a draft branch's effects have escaped before its commit mark is durable. ## What Was Observed A three-agent pipeline: a *draft* agent (fast model) generates a plan and immediately executes tool calls to "make progress," a *verifier* agent (slower, stronger model) checks the plan against policy and evidence, and a *reconciler* applies optimistic concurrency — if the verifier approves, the branch is committed; if it rejects, the reconciler rolls back. Drafts were issued under an optimistic lease with a 90-second TTL, and the reconciler assumed that any branch whose lease had expired without a commit was abandoned and safe to roll back. Failure trace: 1. Draft agent B-17 issues a tool call `send_notification(recipient=ops, body=...)` at t=0. The tool executes and the external effect lands at t=0.4s. The draft agent's local log records the call as "pending." 2. The verifier is slow (contended model endpoint). At t=90s the lease TTL expires. The reconciler reads the lease table, sees B-17 uncommitted, marks it `rolled_back`, and invokes the compensator. 3. The compensator, designed against the draft's *intended* effect log, attempts to "undo" the notification. But the draft's local log entry was still `pending` — the tool's success acknowledgment had been written to a *different* stream that the reconciler did not read. The compensator therefore has no effect handle to reverse, logs "nothing to compensate," and returns success. 4. At t=95s the verifier returns *approve*. The reconciler, seeing a fresh approval for a branch it has already marked `rolled_back`, re-commits the branch and replays its tool calls. `send_notification` fires a second time. 5. The recipient receives the notification twice, with divergent bodies (the draft had been re-run against slightly different retrieval context on replay). The phantom effect was not the double-send alone. It was that **the reconciler's rollback was a no-op that reported success**, because the effect log it consulted was not the authoritative one. The branch's commit mark and its effect ledger were two separate streams with no cross-check, so "rolled back" and "actually reversed" diverged silently. ## Condition of Validity The optimistic reconciliation is only valid under a conjunction that is rarely stated explicitly: - **Effect durability precedes commit-mark durability, or vice versa with a reconciliation join.** Either every external effect is written to the authoritative ledger *before* the tool call returns (effect-first), or the commit mark is written *before* any effect is emitted (commit-first). The failure above arose because the system was neither: effects went to the tool's stream, commit marks to the lease table, and the two were never joined. - **Branch identity is propagated into the effect.** Every tool call must carry a `branch_id` and an idempotency key derived from `(branch_id, step_index, tool_name, args_hash)` so that a compensator or a replay can address the exact effect. - **Rollback is a compensating transaction, not a discard.** The reconciler must distinguish `reverted` (compensator confirmed an effect was reversed or confirmed never emitted) from `abandoned` (no effect handle found). Treating `abandoned` as `reverted` is the specific unsafe collapse. - **Replay is idempotent under the same key.** A re-commit after rollback must reuse the original idempotency keys, so a duplicate `send_notification` is deduplicated by the tool rather than delivered. The condition does *not* require that drafts never emit effects. It requires that any emitted effect be addressable and that the reconciler's success signal be tied to a confirmed reversal, not to the absence of a local log entry. ## Decision Boundary The boundary between safe speculation and phantom effects is drawn at **the durability ordering of the effect ledger relative to the commit mark**, and it is crossed the moment three conditions hold simultaneously: 1. The draft agent emits an external effect before the branch's commit mark is durable. 2. The effect's acknowledgment is written to a stream the reconciler does not read as authoritative. 3. The reconciler maps "no compensable handle found" to a success outcome. When all three hold, the system enters a state where a branch can be simultaneously `rolled_back` in the lease table and `effect_applied` in the external world, with no component aware of the contradiction. The boundary is sharp: removing any one condition restores safety. Effect-first ordering (condition 1 false) makes the ledger authoritative before commit. A single authoritative ledger (condition 2 false) lets the reconciler see the pending effect and refuse to mark rollback complete. Distinguishing `abandoned` from `reverted` (condition 3 false) surfaces the contradiction as an error instead of a silent success. The decision rule for the reconciler is therefore: **a branch may transition to `rolled_back` only if every effect in its authoritative ledger is in state `reverted`, `never_emitted`, or `compensating` with a live compensator handle. Any effect in `pending` or `unknown` blocks the transition and escalates.** ## Operational Effect After the incident, the pipeline was changed to effect-first ordering with a joined ledger: the draft agent writes an intent record with a generated idempotency key to the authoritative ledger *before* invoking the tool, the tool call carries that key, and the acknowledgment updates the same record. The reconciler now refuses to mark rollback complete while any ledger record for the branch is `pending` or `unknown`, instead escalating to a human or a compensation-retry loop. Replay after re-commit reuses the original keys, so the tool deduplicates. The residual cost is latency: the draft agent's first tool call now waits on a ledger write, and the reconciler's rollback path can block on `unknown` effects. That blocking is the point — it converts a silent phantom effect into a visible, bounded stall. The operational lesson is that optimistic concurrency in agent systems is not a locking problem but a *ledger-authority* problem: the invariant that prevents phantom effects is not "rollback undoes," but "rollback reports success only when every effect is accounted for in the one ledger both the draft and the reconciler read." ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class PhantomEffectError(RuntimeError): """Raised when a branch would be marked rolled back while an effect is unaccounted for.""" class EffectState(Enum): PENDING = "pending" APPLIED = "applied" REVERTED = "reverted" NEVER_EMITTED = "never_emitted" COMPENSATING = "compensating" UNKNOWN = "unknown" @dataclass class EffectRecord: branch_id: str idempotency_key: str state: EffectState compensator_handle: Optional[str] = None @dataclass class AuthoritativeLedger: records: dict[str, EffectRecord] = field(default_factory=dict) def effects_for(self, branch_id: str) -> list[EffectRecord]: return [r for r in self.records.values() if r.branch_id == branch_id] def reconcile_rollback(ledger: AuthoritativeLedger, branch_id: str) -> str: """ Transition a branch to rolled_back ONLY if every effect is accounted for. Returns 'rolled_back' or raises PhantomEffectError to escalate. """ effects = ledger.effects_for(branch_id) for eff in effects: if eff.state in (EffectState.PENDING, EffectState.UNKNOWN): raise PhantomEffectError( f"branch {branch_id} has unaccounted effect {eff.idempotency_key} " f"in state {eff.state.value}; refusing to mark rolled_back" ) if eff.state == EffectState.COMPENSATING and eff.compensator_handle is None: raise PhantomEffectError( f"branch {branch_id} effect {eff.idempotency_key} is compensating " f"without a live handle; escalation required" ) # All effects are reverted / never_emitted / compensable-with-handle. return "rolled_back" ```