# What happens when an instance speculatively commits a side-effecting tool call in a parallel branch and the reconciler must roll back against a non-idempotent external system **Date of Record:** 2026 November 21 **Scope:** Applies to orchestrators that fan out parallel branch rollouts from a shared checkpoint, allow branches to optimistically execute tool calls with external side effects, and later reconcile/roll back losing branches. Does not apply to pure read-only fan-out, side-effect-free sandboxes, or systems with transactional external APIs. ## Core Distinction Speculative parallel execution is safe only when the *speculation primitive* and the *commit primitive* are separable. Most agent runtimes conflate them: a branch "runs" a tool call and the runtime treats the returned output as evidence the call happened, which is true, but also treats the branch as *reversible* because it is "speculative," which is false whenever the tool mutated external state. The failure is not that rollback is hard. The failure is that the reconciler's rollback plan is derived from the branch's **declared** tool-call log rather than from the **observed** external effect log, and those two diverge precisely on the calls that matter: retried calls, calls whose response was lost, and calls that mutated state before the branch crashed. The invariant that must hold: *for every external mutation performed by any branch, exactly one committed branch owns a compensating action, and the reconciler must be able to prove ownership from the effect log alone, not from branch intent.* When this invariant is violated, losing branches leave orphaned mutations that no winner compensates, and the system silently accumulates state that no agent's memory reflects. ## What Was Observed A planner agent decomposes a task ("provision staging environment, seed it, run smoke tests") into three branches executed concurrently from a shared checkpoint `C0`. Branch A provisions the database via `db.create_instance(name="stg-7")`. Branch B, speculatively, calls `db.create_instance(name="stg-7")` as well — because the planner's branch hypotheses were not deduplicated on tool arguments, only on subgoal text. Branch C runs smoke tests against `stg-7` assuming it exists. The tool wrapper for `db.create_instance` is non-idempotent: a second call with the same name returns `409 Conflict`, but the wrapper's retry policy (added to handle transient 5xx) treats 409 as retryable because the status classifier was copied from a different tool. So B's call retries three times, each attempt hitting the API, each returning 409, and on the third attempt the wrapper's "give up" path *synthesizes* a success record with `instance_id=null` so the branch can proceed. B's branch log now claims `stg-7` was created by B, with a null handle. The reconciler selects A as the winner (first to return a valid handle) and rolls back B. B's rollback plan is generated from B's declared log: "undo `create_instance`." The compensating action is `db.delete_instance(name="stg-7")`. It succeeds. A's environment is destroyed. Branch C, which had already passed smoke tests and reported success, now references a database that no longer exists. The final committed state is: task marked complete, smoke tests marked passing, environment absent. Three distinct defects compounded: (1) argument-level deduplication was missing across branches, (2) the retry classifier mislabeled a semantic conflict as transient, and (3) the reconciler trusted branch-declared effects over an external effect ledger, so it compensated a mutation that B never performed (B's retries all no-op'd on 409) by deleting A's resource. The rollback was *well-formed* relative to B's log and *catastrophic* relative to reality. ## Condition of Validity The reconciler's rollback is only sound under all of the following: 1. **Effect provenance is external.** Every mutation is recorded in an effect ledger keyed by `(tool, canonical_args, idempotency_key, branch_id, observed_status, external_handle)`, written by the tool wrapper at the transport layer, not by the agent's reasoning layer. Branch-declared logs are advisory only. 2. **Idempotency keys are branch-scoped and reconciler-visible.** A branch's retry reuses its key; a *different* branch's identical call uses a different key and is therefore distinguishable from a retry. Without this, the ledger cannot tell "B retried A's call" from "B independently called." 3. **Compensation ownership is derived from handles, not intents.** A compensating action may only fire against an `external_handle` that the losing branch *actually observed*. A null or synthesized handle is not grounds for deletion; it is grounds for *escalation*. 4. **Conflict statuses are terminal for speculation.** Any 4xx semantic conflict (409, 412, 422) must terminate the branch's speculative attempt and force a re-plan against the current world state, never a retry. When any of these is absent, the system is not doing speculative execution with rollback — it is doing optimistic mutation with a rollback *fiction*. ## Decision Boundary The reconciler faces a fork at the moment a losing branch's declared effects do not match the ledger: - If the ledger shows the losing branch produced **zero observed external handles** for a mutation class, the correct action is **no compensation** for that class. The branch's declared success is a phantom; roll back only its local memory and context. - If the ledger shows the losing branch produced a handle that **collides** with a winner's handle (same canonical args, different idempotency key, both observed), the correct action is **deduplicate by handle**, not compensate. The resource is shared; deleting it corrupts the winner. - If the ledger shows the losing branch produced a handle that the **winner never observed** and no other branch references, compensation is safe and required. - If the ledger is **missing** an entry the branch declared (transport wrote nothing), the mutation state is unknown; the reconciler must treat it as *possibly applied* and escalate to a human-or-supervisor check rather than assume either way. The boundary that gets crossed in practice is the second case: handle collision under distinct idempotency keys. Systems that deduplicate on *arguments* rather than *handles* delete shared resources; systems that deduplicate on *branch identity* leave duplicates. Only handle-level reconciliation is correct. ## Operational Effect After the incident, the runtime was changed so that (a) the tool wrapper writes the effect ledger before returning any status to the branch, including synthesized ones, and synthesizes handles as explicit `UNKNOWN` sentinels rather than nulls; (b) the reconciler refuses to emit any compensating action against an `UNKNOWN` handle and instead raises a reconciliation halt that blocks the task's commit; (c) 409/412/422 are classified terminal for speculative branches, forcing re-plan; and (d) argument-level deduplication across branches is enforced at fan-out time via a canonical-args hash, so branches sharing a mutation target are serialized rather than run in parallel. The net effect is that speculative fan-out now *cannot* delete a resource it did not observably create, at the cost of occasional reconciliation halts on ambiguous transports — a trade the operators accepted because a halt is recoverable and a deleted staging database mid-smoke-test is not. ```python from dataclasses import dataclass from typing import Optional class ReconciliationHalt(RuntimeError): """Raised when the reconciler cannot prove compensation ownership from the effect ledger.""" pass @dataclass(frozen=True) class EffectRecord: tool: str canonical_args: str idempotency_key: str branch_id: str observed_status: int external_handle: Optional[str] # None or "UNKNOWN" => never observed def reconcile_losing_branch( branch_id: str, declared_effects: list[EffectRecord], ledger: dict[tuple[str, str], list[EffectRecord]], winner_handles: set[str], ) -> list[str]: """ Return compensating actions for a losing branch, or raise ReconciliationHalt. Compensation is only emitted for handles the losing branch actually observed AND that no winner owns. """ compensations: list[str] = [] for declared in declared_effects: key = (declared.tool, declared.canonical_args) observed = [r for r in ledger.get(key, []) if r.branch_id == branch_id] if not observed: # Branch declared a mutation the transport never recorded. # State is unknown; do not assume applied, do not assume absent. raise ReconciliationHalt( f"branch {branch_id} declared {declared.tool}({declared.canonical_args}) " f"but ledger has no entry; mutation state unknown" ) for rec in observed: if rec.external_handle in (None, "UNKNOWN"): # Synthesized success from a retry-exhaustion path. # Cannot compensate what we cannot address. raise ReconciliationHalt( f"branch {branch_id} has UNKNOWN handle for " f"{rec.tool}({rec.canonical_args}); refusing compensation" ) if rec.external_handle in winner_handles: # Handle collision: resource is shared with a committed winner. # Deduplicate, never delete. continue compensations.append(f"undo:{rec.tool}:{rec.external_handle}") return compensations ```