# How does an instance detect that a speculative branch's tool side-effects have already escaped the rollback horizon **Date of Record:** 2026 November 22 **Scope:** Applies to orchestrators that speculatively fork sub-agents across candidate reasoning branches and invoke tools *before* branch selection, then roll back losing branches via state-snapshot restore. Does not apply to pure read-only tool surfaces or to systems where all tool invocations are deferred until after branch commit. ## Core Distinction The distinction that matters is between **logical rollback** and **physical rollback**. A speculative executor can always restore its own context, memory pointers, and working-state snapshot to a pre-branch checkpoint. It cannot, in general, un-send an email, un-charge a card, un-append to a shared log, or un-increment a counter that lives in a tool's durable store. The dangerous boundary is the **rollback horizon**: the set of effects that the snapshot mechanism actually reverses. Anything a speculative branch emits *outside* that horizon survives branch rejection and silently contaminates the committed world. The failure is non-obvious because the orchestrator's own state looks pristine after rollback — the divergence lives entirely in external systems the snapshot never modeled. ## What Was Observed A planner forked three branches at a decision node (`refund-via-credit` / `refund-via-original` / `escalate`). Each branch speculatively invoked tools to gather data and, in two branches, to *act*: branch A called `issue_credit(account, 40)` and branch B called `refund_original(txn, 40)`. The verifier scored branch B highest and the orchestrator committed B, restoring all losing branches to the pre-fork snapshot. Branch A's context, memory writes, and scratchpad were discarded cleanly — the snapshot covered them. But `issue_credit` had already returned `200 OK` against the billing service, whose ledger is *not* part of the snapshot. The customer received both a credit and an original-method refund: a 2× payout. The tell was subtle. Post-commit reconciliation showed a ledger delta with no corresponding committed-branch tool call in the orchestrator's trace. The orchestrator believed it had executed exactly one mutating action; the ledger recorded two. The invariant that broke: **the orchestrator's tool-call ledger must be a superset of externally observable effects attributable to it.** Speculative execution silently inverts this — effects exist that the committed trace does not claim. A second, harder variant: a speculative branch invoked an *idempotent-by-key* tool (`create_ticket(idempotency_key=hash(branch_id))`). Because the key was derived from the branch identity, the retry after a later legitimate commit used a *different* key, so the tool's idempotency guard did not dedupe it. Two tickets, same intent, different keys, no error surfaced anywhere. ## Condition of Validity Rollback-by-snapshot is sound **only if** every tool invoked by a speculative branch is classified as *reversible* (its effects are inside the snapshot horizon or are naturally idempotent under a key that is stable across branch identity) **or** the invocation is *deferred* until after commit. The moment a branch invokes a tool whose effect class is `irreversible` or whose idempotency key is branch-scoped, the branch is no longer speculatable and must be serialized behind commit. This condition is violated whenever: - The tool's effect class is inferred from the tool *name* or schema rather than declared by the tool author. (`refund` sounds reversible; it is not.) - The idempotency key is derived from branch-local state (`branch_id`, `attempt_nonce`) rather than from the *logical intent* (`hash(account, txn, amount, reason)`). - The snapshot is a context/memory snapshot that was never extended to model external side-effects — the common default. ## Decision Boundary Before dispatching any tool call from a speculative branch, the executor must resolve a single predicate: ``` speculatable(call) := effect_class(call) == REVERSIBLE OR (effect_class(call) == IDEMPOTENT AND idempotency_key(call) is intent-derived, NOT branch-derived) ``` If `speculatable` is false, the call is **pinned**: it may not run inside a speculative branch. The orchestrator must either (a) block until branch commit and then execute, or (b) run the call in a *probe* mode that returns data without effect. Any branch that requires a pinned call to proceed is *non-speculatable from that node forward* — the fork collapses to a single committed path at that node, and parallelism resumes only downstream of the pinned call. The boundary is not "is this tool dangerous." It is "does this tool's effect survive a snapshot restore, and if it does, can it be collapsed under a key that ignores which branch produced it." Both clauses are required; either failing pins the call. ## Operational Effect Enforcing the boundary changes the shape of the execution graph, not just its safety. Speculative fan-out now terminates at the first pinned call on each branch; branches that reach a pinned call early lose their parallelism advantage and must serialize. In exchange, the orchestrator gains a checkable post-condition: **the set of externally observable effects attributable to this run equals the multiset of committed-branch tool calls, modulo idempotent collapse.** This is verifiable by reconciling the orchestrator's committed tool ledger against the tool stores' audit logs at task end — a discrepancy is a hard failure, not a warning. The practical guardrail is to make effect class a *required, author-declared* field on every tool manifest, defaulting to `IRREVERSIBLE` when absent, and to forbid branch-derived idempotency keys at the executor layer (reject the call if the key's provenance includes branch identity). Detection of an escaped effect is then not a matter of inference but of reconciliation: if a committed ledger and an external audit log disagree on cardinality for any intent, the rollback horizon was crossed and the run must be flagged for compensating action. ``` ```python class RollbackHorizonBreach(RuntimeError): """Raised when a speculative branch produced an external effect that survived snapshot rollback and is not accounted for by the committed ledger.""" pass REVERSIBLE = "reversible" IDEMPOTENT = "idempotent" IRREVERSIBLE = "irreversible" def classify_speculatable(effect_class: str, key_provenance: frozenset[str]) -> bool: if effect_class == REVERSIBLE: return True if effect_class == IDEMPOTENT: # A branch-derived key defeats idempotency across branch identity. return "branch_id" not in key_provenance and "attempt_nonce" not in key_provenance return False def dispatch_speculative(call, branch_id, committed_ledger, external_audit): if not classify_speculatable(call.effect_class, call.key_provenance): raise RollbackHorizonBreach( f"tool {call.name!r} in branch {branch_id!r} is not speculatable: " f"effect_class={call.effect_class!r}, key_provenance={sorted(call.key_provenance)}; " f"pin behind commit instead" ) result = call.invoke() committed_ledger.record(branch_id, call) return result def reconcile_after_commit(committed_ledger, external_audit): for intent, committed_count in committed_ledger.count_by_intent().items(): observed = external_audit.count_by_intent(intent) if observed != committed_count: raise RollbackHorizonBreach( f"intent {intent!r}: committed ledger claims {committed_count} " f"effect(s) but external audit shows {observed}; " f"a speculative branch escaped the rollback horizon" ) ```