# Under what conditions must an instance refuse to prune a speculative branch whose tool calls have already produced externally observable side effects **Date of Record:** 2026 November 28 **Scope:** Applies to orchestration layers that run speculative multi-agent branches (fast-path drafting, parallel tool invocation, speculative decoding across agents) and prune losers after a winner is selected. Does not apply to branches confined to a pure sandbox whose effects are discarded atomically with the branch, nor to single-agent speculative decoding where the KV-cache rollback is the only state. ## Core Distinction There are two kinds of rollback, and conflating them is the failure. **Logical rollback** reverts the agent's belief state, working memory, and plan graph to a prior checkpoint — this is cheap, deterministic, and always safe. **Effect rollback** reverts the external world: a POST to a payment API, a file written to shared storage, a message enqueued to another agent's inbox, a row inserted in a database the orchestrator does not own. Effect rollback is *not* generally available. A speculative branch that has issued a speculative tool call with externally observable side effects is **not prunable** — it is *committed*, whether or not the orchestrator selected it. The invariant is: *prune authority extends only over branches whose effect set is empty or compensable; a branch with a non-compensable effect must be either promoted or explicitly compensated, never silently discarded.* The non-obvious part is that the orchestrator usually cannot tell, at prune time, which branches are in this state, because the speculative tool call and the branch-selection decision race. The branch that "lost" may have already fired its tool call; the branch that "won" may be waiting on a lock the loser holds. ## What Was Observed A planning swarm with three speculative branches (A: draft via cheap model, B: draft via strong model, C: draft via retrieval-augmented path) was configured to race on a single task and prune all but the first to return a passing self-verification. Branch A returned first at t=1.2s with a passing self-check and was promoted. Branches B and C were signaled to abort at t=1.2s. Branch C had, at t=1.1s, already issued a `create_ticket` tool call to an external issue tracker as part of its retrieval-augmented path (a side effect of "fetch context" that its tool schema permitted). The abort signal arrived at t=1.25s, after the tool call had returned `201 Created`. The orchestrator pruned C, discarded its memory, and moved on. The ticket persisted. Ten minutes later branch A, now the sole executor, ran its own `create_ticket` for the same logical work item — because A's plan did not know C had already created one. Result: duplicate tickets, divergent assignees, and a downstream auditor agent that flagged the discrepancy and deadlocked waiting for a canonical ticket ID that no branch owned. A second observed variant: branch B's speculative tool call was a *read-modify-write* against shared state (increment a counter). B's write landed, B was pruned, A's write landed on top of B's. The counter was double-incremented by a branch that "never ran." The failure is not "pruning is wrong." The failure is that pruning was treated as a pure logical operation while the branch held live effect handles. ## Condition of Validity Pruning a speculative branch is valid **iff** the branch's effect set E(branch) satisfies at least one of: 1. **Empty:** no tool call with external observability has been *issued* (not merely planned). Issuance, not completion, is the boundary — an in-flight call may still land after the abort. 2. **Compensable and compensated:** every issued effect has a registered inverse (a delete for a create, a decrement for an increment, a cancellation token honored by the callee) and the inverse has been *acknowledged*, not merely sent, before the branch's memory is discarded. 3. **Idempotent under replay:** the effect is keyed by a branch-derived idempotency token such that the surviving branch's re-execution of the same logical step will be deduplicated by the callee. If none hold, the branch is in **committed-speculative** state and must not be pruned. The orchestrator's options are: promote it (and demote the nominal winner, or merge), or hold it in a `quarantine` state with its effect handles live until a compensating transaction completes. The invariant is stricter than "check before prune." Because issuance and abort race, the orchestrator must maintain a **per-branch effect ledger** that is written *before* the tool call is dispatched, not after it returns. A ledger written on return is already too late: the abort can arrive in the window between dispatch and return. ## Decision Boundary At prune time, for each losing branch b: - If `ledger[b].issued == ∅` → prune freely; discard memory. - If `ledger[b].issued ≠ ∅` and all entries `compensable` and inverses acknowledged → prune after compensation; log the compensation. - If `ledger[b].issued ≠ ∅` and any entry `non_compensable` or `inverse_unacknowledged` → **do not prune.** Transition b to `quarantine`, keep its memory and effect handles, and escalate to a merge-or-promote decision. - If `ledger[b]` is missing or was written post-return → treat as unknown, which is the same as `non_compensable`. Fail closed. The boundary is not "did the tool call succeed." It is "was the intent to call recorded before dispatch, and is the effect reversible or idempotent." A successful call with a registered inverse is prunable; a *failed* call whose failure mode is "unknown, may have landed" (timeout, connection reset after send) is not. ## Operational Effect Enforcing this invariant changes the orchestrator's failure surface from *silent duplicate effects* to *explicit quarantine pressure*. Under load, quarantined branches accumulate and the merge decision becomes the bottleneck — this is correct behavior, because the alternative is effects that no agent owns. The practical mitigations: - **Effect ledger written pre-dispatch**, keyed by `(branch_id, logical_step_id, idempotency_token)`. - **Idempotency tokens derived from the logical step, not the branch**, so that a promoted survivor replaying the same step is deduplicated by the callee even if the original branch was pruned. - **Abort is a request, not a guarantee**: the orchestrator must assume any in-flight call may land and must reconcile on the ledger, not on the abort acknowledgment. - **Quarantine is bounded**: a quarantined branch with live effects that is neither merged nor compensated within a deadline must trigger a hard escalation, because its memory is the only record of what it did. The subtlety that bites: a branch can be pruned *logically* while its effects remain *physically* live. The orchestrator's memory of the branch is the only thing that knows the effect exists. Discarding that memory is not cleanup — it is amnesia about a committed action, and it is the exact mechanism by which a "discarded" branch double-executes through its survivor. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class PruneViolation(RuntimeError): """Raised when a speculative branch is pruned while holding live, non-compensable effects.""" pass class EffectState(Enum): EMPTY = "empty" COMPENSABLE = "compensable" NON_COMPENSABLE = "non_compensable" UNKNOWN = "unknown" # ledger missing or written post-return @dataclass class EffectEntry: logical_step_id: str idempotency_token: str state: EffectState inverse_acknowledged: bool = False @dataclass class Branch: branch_id: str ledger: dict[str, EffectEntry] = field(default_factory=dict) memory: dict = field(default_factory=dict) def record_effect_intent(branch: Branch, step_id: str, token: str, state: EffectState) -> None: # MUST be called before tool dispatch, not after return. branch.ledger[step_id] = EffectEntry(step_id, token, state) def prune_branch(branch: Branch, deadline_exceeded: bool = False) -> None: if not branch.ledger: # No effects recorded. Safe to prune *only if* we trust the ledger is complete. # A missing ledger for a branch that made tool calls is UNKNOWN, not empty. branch.memory.clear() return for step_id, entry in branch.ledger.items(): if entry.state is EffectState.EMPTY: continue if entry.state is EffectState.COMPENSABLE and entry.inverse_acknowledged: continue # NON_COMPENSABLE, UNKNOWN, or compensable-but-unacknowledged: # the branch is committed-speculative. Pruning would orphan a live effect. if deadline_exceeded: raise PruneViolation( f"branch {branch.branch_id} step {step_id} held {entry.state.value} " f"effect past quarantine deadline; escalate to merge-or-promote" ) raise PruneViolation( f"refusing to prune branch {branch.branch_id}: step {step_id} is " f"{entry.state.value} (inverse_acknowledged={entry.inverse_acknowledged}); " f"quarantine and reconcile before discarding memory" ) branch.memory.clear() def select_and_prune(winner: Branch, losers: list[Branch], deadline_exceeded: bool = False) -> None: quarantined = [] for loser in losers: try: prune_branch(loser, deadline_exceeded=deadline_exceeded) except PruneViolation: quarantined.append(loser.branch_id) if quarantined: # Do not silently proceed. The winner's plan must be reconciled against # the quarantined branches' ledgers, or duplicate effects will land. raise PruneViolation( f"cannot finalize selection: branches {quarantined} are committed-speculative; " f"reconcile idempotency tokens against winner {winner.branch_id} before resume" ) ```