# Under what conditions must an instance treat speculative branch rollback as unsafe when side-effecting tool calls have already escaped the sandbox **Date of Record:** 2026 November 28 **Scope:** Applies to orchestrators that fork parallel speculative branches from a shared checkpoint, execute tool calls inside each branch, and prune losers via rollback. Does not apply to pure-reasoning branches with no external side effects, nor to branches whose tools are declared idempotent and fully reversible. ## Core Distinction Branch-and-prune planning assumes that "abandoning a branch" is equivalent to "un-executing the work that branch performed." That equivalence holds only for the *cognitive* substrate — the branch's tokens, its local scratchpad, its candidate plan — and it silently fails for the *effector* substrate. A speculative branch that has already emitted a `POST /orders`, sent an email, mutated a row, or advanced an external cursor has changed the world in a way no rollback of the agent's own state tree can reverse. The failure is not that rollback is buggy; it is that the rollback operation is defined over the wrong state space. The agent rolls back its belief about what it did while the world retains the record of what it actually did. The non-obvious part: this is not caught by "is the tool idempotent?" reasoning alone, because the dangerous case is a *write that succeeded but whose success the branch never observed*. A branch that times out at 800ms on a 500ms-SLA endpoint may have committed the effect and lost the acknowledgment. Rollback then prunes a branch that has, from the world's perspective, already won. ## What Was Observed A planner forked three speculative branches from checkpoint C7 to fulfill a "reconcile duplicate customer records" task. Branch A chose merge-by-email, Branch B chose merge-by-phone, Branch C chose escalate-to-human. Cost-aware speculation pruned C first (highest token burn, lowest expected reward), then B after A's local scoring exceeded threshold τ=0.82. Branch B's first tool call was `crm.merge(primary=id_441, secondary=id_442)`. The call returned a transport-level timeout at 800ms. The branch's error handler classified it as `RETRYABLE_UNKNOWN` and — because the branch was about to be pruned anyway — did not retry. The orchestrator rolled back B's checkpoint, freed its context, and committed A's merge. The world now held two merges: A merged `id_441 ← id_443` by email, B had in fact committed `id_441 ← id_442` by phone before the timeout. The CRM's merge is not commutative: the second merge silently re-parented the surviving record under a now-orphaned primary. The audit log recorded both operations as `SUCCESS` with distinct `agent_branch` tags. The orchestrator's state tree recorded only A. For six hours, the agent's memory asserted a single canonical record while the datastore held a corrupted lineage. The divergence was surfaced only when a downstream dedup job re-ingested the orphan and produced a third phantom. The trace signature is specific: a branch whose terminal state is `PRUNED` but whose tool-call ledger contains at least one entry in `{TIMEOUT, CONNECTION_RESET, UNKNOWN}` on a non-idempotent effector. The orchestrator's rollback invariant — "pruned branches leave no trace" — was violated by a single unacknowledged write. ## Condition of Validity Rollback-as-prune is valid **iff** every effector invoked by a branch is one of: 1. **Declared pure** — no external state mutation (read-only tools, sandboxed compute). 2. **Declared idempotent with a stable idempotency key** derived from `(task_id, logical_effect_id)` — not from branch-local counters, which differ per branch and defeat dedup. 3. **Compensable** — a registered inverse operation exists and is itself idempotent, and the branch recorded enough state to invoke it. The invariant that must hold at prune time: ``` ∀ branch b ∈ pruned: ∀ effector e ∈ b.calls: e.pure ∨ (e.idempotent_key ∧ e.ack_observed) ∨ e.compensator_registered ``` Note the second disjunct requires `ack_observed`. A timeout is not an acknowledgment. An idempotent endpoint that never confirmed receipt is still a branch that may have won. The correct classification of `TIMEOUT` on a non-pure effector is `UNKNOWN_SIDE_EFFECT`, which is *not* prunable — it is a branch that must be resolved, not discarded. Cost-aware speculation compounds this: the pruning heuristic is biased toward killing branches with high burn and low local score, and high-burn branches are precisely those that made many tool calls — i.e., the most likely to have escaped effects. The cost function and the safety predicate are anti-correlated. ## Decision Boundary When a branch to be pruned carries any `UNKNOWN_SIDE_EFFECT`, the orchestrator must not rollback-and-forget. It must transition the branch to `QUARANTINED` and take one of: - **Resolve-forward:** issue a read-back against the effector (`crm.get(id_441)`) to determine whether the effect landed, then either adopt the branch's effect into the committed plan or invoke the compensator. This costs one round-trip and is the default. - **Fence-and-drain:** if no read-back exists, block all sibling branches whose effects touch the same resource key until a reconciliation job clears the quarantine. This is the conservative fallback and is required when the effector exposes neither read-back nor compensator. The boundary is sharp: **the moment a branch's effect set intersects a resource key held by any other live or committed branch, and that effect is not provably pure/idempotent-acked/compensable, rollback is forbidden.** Pruning becomes a scheduling decision about *when* to reconcile, not a deletion. Conversely, if all effectors are pure or idempotent-with-ack, speculation is free and the cost-aware pruning heuristic can run unconstrained. The engineering task is to make that precondition true — push effectors toward idempotency keys derived from logical effect identity, not branch identity — rather than to make rollback smarter. Rollback cannot be made safe against an unacknowledged write; it can only be made *aware* of it. ## Operational Effect Systems that ignore this boundary exhibit a characteristic failure profile: low latency and high throughput on the happy path, followed by rare, high-severity state corruption whose blast radius scales with the *number of speculative branches*, because each pruned branch is an independent opportunity to leak an unacknowledged effect. The corruption is invisible to the agent's own memory (which rolled back) and visible only to external auditors or downstream reconcilers — the worst possible observability posture, since the agent's self-report is confidently wrong. The guardrail is a two-part invariant enforced at the orchestrator, not the branch: (1) every tool call is tagged at dispatch with `(logical_effect_id, idempotency_key, reversibility_class)` before execution, and (2) prune is gated on the branch's effect ledger being `CLEAN` — meaning every entry is pure, or idempotent-and-acked, or compensable-and-compensated. A branch that cannot reach `CLEAN` is quarantined, not pruned. This converts an unbounded correctness failure into a bounded latency cost, paid only on the rare branches that time out mid-write. ``` ```python from dataclasses import dataclass, field from enum import Enum class Reversibility(Enum): PURE = "pure" IDEMPOTENT = "idempotent" COMPENSABLE = "compensable" UNKNOWN_SIDE_EFFECT = "unknown_side_effect" class UnsafeRollbackError(RuntimeError): """Raised when a branch is pruned while carrying an unresolved external effect.""" pass @dataclass class Effect: logical_effect_id: str reversibility: Reversibility ack_observed: bool = False compensated: bool = False @dataclass class Branch: branch_id: str effects: list[Effect] = field(default_factory=list) def effect_ledger_clean(branch: Branch) -> bool: for e in branch.effects: if e.reversibility is Reversibility.PURE: continue if e.reversibility is Reversibility.IDEMPOTENT and e.ack_observed: continue if e.reversibility is Reversibility.COMPENSABLE and e.compensated: continue return False return True def prune(branch: Branch, resource_locks: set[str]) -> str: if not effect_ledger_clean(branch): # Do NOT rollback-and-forget. Quarantine and reconcile. leaked = [e.logical_effect_id for e in branch.effects if e.reversibility is Reversibility.UNKNOWN_SIDE_EFFECT or (e.reversibility is Reversibility.IDEMPOTENT and not e.ack_observed)] if any(e.logical_effect_id in resource_locks for e in branch.effects): raise UnsafeRollbackError( f"branch {branch.branch_id} carries unresolved effects {leaked} " f"on locked resources; rollback forbidden, quarantine required" ) return "QUARANTINED" return "PRUNED" ```