# In what scenarios must an instance treat speculative multi-agent branch rollback as unsafe when tool side effects are non-idempotent and the compensating log is lossy **Date of Record:** 2026 November 27 **Scope:** Applies to any orchestrator that forks speculative agent branches (parallel hypothesis execution) and commits the winning branch while rolling back losers by replaying a compensation log. Does not apply to pure-read tool graphs, or to systems where every tool call is provably idempotent under a stable idempotency key. ## Core Distinction Speculative execution in agent systems is usually justified by an analogy to CPU branch prediction: losers are "discarded" and the machine state is restored. That analogy fails at the tool boundary. A CPU rollback restores a register file; an agent rollback must *undo externally observed actions* — an email sent, a payment authorized, a row inserted into a third-party SaaS, a file written to a shared bucket. The distinction that matters operationally is between **state that the orchestrator owns** (context window, scratchpad, internal scratch state — freely discardable) and **state the orchestrator merely caused** (side effects in tools the orchestrator does not control — discardable only if a compensation exists, is ordered correctly, and itself succeeds). The non-obvious boundary: rollback is *safe* only if the compensation log is a total, ordered, and durable record of every externally visible effect produced by the losing branch. In practice the log is **lossy** in three specific ways — tool calls that succeeded but whose response was truncated/lost, calls that timed out after the remote committed, and calls made by sub-agents the parent never observed. When the log is lossy, "rollback" silently becomes "partial rollback," and the loser's effects leak into the committed world. ## What Was Observed A planning agent forks three speculative branches to satisfy a request ("provision access for the new contractor"). Branch A calls `iam.grant_role`, then `notify.send_email`. Branch B calls `iam.grant_role` with a different scope. Branch C calls `billing.create_seat`. The verifier selects Branch A; B and C are rolled back by replaying their compensation logs. Two leaks appeared: 1. **Timeout-after-commit.** Branch C's `billing.create_seat` request timed out at the 8s client deadline. The orchestrator logged the call as `FAILED` and recorded *no* compensation (you cannot compensate a call you believe failed). The remote had in fact committed at t=6.1s and the response was lost in transit. Rollback of C therefore did nothing, and a seat was billed. The log was not wrong about what it *saw*; it was wrong about what *happened*. 2. **Unobserved sub-agent effect.** Branch B delegated to a child agent that called `iam.grant_role` and `audit.write`. The parent's compensation log captured only the delegation call, not the child's tool calls, because the child's tool trace was streamed to the child's own span and never folded into the parent's compensation ledger. Rolling back B revoked nothing; the grant from the child persisted alongside Branch A's grant, producing a privilege union neither branch intended. The failure trace for case 1 is the important one: ``` t=0.0 branch C: tool_call create_seat (idem_key=K) t=6.1 remote: COMMIT seat t=8.0 client: deadline exceeded -> status=TIMEOUT t=8.0 orchestrator: log(status=TIMEOUT, compensation=NONE) t=9.4 verifier: select A t=9.4 rollback(C): replay [] -> no-op INVARIANT VIOLATED: committed_world ⊋ {effects of A} ``` The invariant that broke is the one speculative execution depends on: **committed_world == effects(winner) ∪ effects(compensated losers)**, with the second set empty by construction. It was not empty. ## Condition of Validity Rollback-on-divergence is valid only under the conjunction of four conditions. Each is individually checkable; the failure occurs when any is assumed rather than verified. - **C1 — Effect totality:** every externally visible effect of a branch is represented in its compensation ledger. Fails when a branch delegates to a sub-agent whose tool calls are not merged into the parent ledger. - **C2 — Outcome certainty:** a tool call is recorded as `FAILED` (no compensation needed) only if the client can prove non-commit. A bare timeout proves nothing; it is `UNKNOWN`, not `FAILED`. - **C3 — Compensation soundness:** each compensation is itself idempotent and can be retried without new effects, and compensations run in reverse causal order. - **C4 — Divergence isolation:** no two branches write the same external key without a shared idempotency namespace, so a loser's compensation cannot revoke a winner's effect. The observed incident violated C1 (case 2) and C2 (case 1). C4 is the quieter hazard: if A and B both call `grant_role(user=u, role=r)` with the same idempotency key, B's rollback revokes the grant A depends on — rollback of the loser deletes the winner's effect. Without a per-branch key namespace, compensation is not branch-local. ## Decision Boundary The operational rule is a tri-state classification of every tool outcome, not a binary: - `COMMITTED` — response received, effect known. Compensable. - `FAILED` — proven non-commit (connection refused before send, explicit 4xx rejection). No compensation. - `UNKNOWN` — timeout, connection reset mid-flight, truncated response, 5xx after request body sent. **Must be treated as possibly-committed.** The decision boundary: **speculative rollback may only proceed for a branch whose ledger contains zero `UNKNOWN` outcomes and whose ledger is proven total (C1).** If either fails, the branch is not rolled back — it is *quarantined*: its effects are treated as committed, the branch is excluded from the winner selection as a candidate but its effects are surfaced to the verifier as a conflict, and reconciliation is escalated (reconcile-by-query against the remote, or human/auditor resolution). You do not get to pretend the branch didn't happen. This inverts the usual default. The naive orchestrator optimizes for "clean" rollback and maps `UNKNOWN → FAILED` to keep the ledger simple. The correct default maps `UNKNOWN → possibly-committed` and pays the reconciliation cost, because the alternative is silent divergence between the orchestrator's model of the world and the world. ## Operational Effect Three concrete consequences for any implementation: 1. **Compensation ledgers must be built from the transport layer, not the tool-call intent.** Record the request as `PENDING` *before* dispatch, and only resolve to `COMMITTED`/`FAILED` on an authoritative signal. A `PENDING` entry at rollback time is an `UNKNOWN` and blocks silent rollback. 2. **Sub-agent traces must be folded into the parent ledger at delegation boundaries**, or the parent must refuse to speculatively fork a branch that delegates to a child it cannot observe. Unobserved delegation is an un-compensable effect by definition. 3. **Idempotency keys must be branch-scoped** (`{branch_id}:{logical_op}`), so a loser's compensation is a no-op against a winner's identical logical operation. The cost is real: some fraction of speculative branches become non-rollbackable and must be reconciled rather than discarded, which erodes the throughput benefit of speculation precisely in the high-side-effect regime where speculation is most tempting. That erosion is the correct price. Speculation is a bet that losers are free to discard; when the ledger is lossy, that bet is unhedged, and the loss surfaces later as duplicated grants, double-billed seats, and audit trails that disagree with reality. ``` ```python class UnsafeRollbackError(RuntimeError): """Raised when a speculative branch cannot be safely rolled back.""" pass COMMITTED, FAILED, UNKNOWN = "COMMITTED", "FAILED", "UNKNOWN" def classify_outcome(transport_status: str, response_received: bool) -> str: if response_received and transport_status == "OK": return COMMITTED if transport_status in ("CONN_REFUSED", "REJECTED_4XX"): return FAILED # timeout, reset mid-flight, truncated body, 5xx-after-send return UNKNOWN def rollback_branch(branch_id: str, ledger: list[dict], ledger_is_total: bool) -> list[str]: """Attempt to compensate a losing speculative branch. ledger entries: {"op", "idem_key", "transport_status", "response_received"} """ if not ledger_is_total: # C1 violated: sub-agent effects may be missing from the ledger. raise UnsafeRollbackError( f"branch {branch_id}: ledger not proven total (unobserved delegation); " f"quarantine and reconcile instead of rollback" ) compensations = [] for entry in reversed(ledger): # reverse causal order (C3) outcome = classify_outcome(entry["transport_status"], entry["response_received"]) if outcome == UNKNOWN: # C2 violated: timeout may have committed remotely. raise UnsafeRollbackError( f"branch {branch_id}: op {entry['op']} (key={entry['idem_key']}) " f"has UNKNOWN outcome; effect may be committed, cannot silently discard" ) if outcome == COMMITTED: compensations.append(entry["idem_key"]) for key in compensations: # branch-scoped key (C4): compensation is a no-op against other branches _issue_compensation(key) return compensations def _issue_compensation(key: str) -> None: ... ```