What structural signals indicate a speculative agent branch has been silently "committed" by downstream effects before rollback

**Date of Record:** 2026 December 2 **Scope:** Applies to orchestration patterns where a parent agent forks N speculative plan branches that execute tool calls against shared external state (filesystems, queues, APIs, databases), and where the parent intends to roll back all but one branch on selection. Does not apply to branches whose tool surface is strictly read-only or to branches confined to a per-branch ephemeral sandbox with no shared-write surface.

Core Distinction

Rollback of a speculative branch is usually modeled as a *state* operation: undo writes, restore snapshots, discard the branch. The failure mode here is not that rollback fails to execute — it is that rollback executes correctly *against the branch's own view of state* while the branch has already leaked an irreversible effect into shared state through a tool call the orchestrator classified as "side-effect-free" or "idempotent." The branch is then rolled back to a consistent local snapshot, reports `rolled_back: true`, and the orchestrator proceeds — while the shared world has been mutated by a branch that was supposed to be speculative. The distinguishing signal is that the branch's *post-rollback local state* and the *shared state* diverge in a way that no later branch or auditor can detect from inside the branch's context window.

This is distinct from the ordinary "forgot to roll back" bug. The rollback ran. The invariant that broke is that **a branch's ability to roll back is a property of its effect set, not of its snapshot mechanism**, and the effect set is inferred by the orchestrator from tool *declarations* rather than from tool *behavior under a given input*.

What Was Observed

A parent agent forks three speculative branches to decide how to reconcile a customer record. Each branch receives a nominally read-only tool `lookup_account(id)` and a mutating tool `post_adjustment(account, amount)`. The orchestrator classifies `lookup_account` as pure and permits it in all branches; only the selected branch is permitted to call `post_adjustment` — the others are supposed to terminate before that call.

Branch 2, mid-reasoning, calls `lookup_account` with a malformed id. The tool's implementation lazily *creates* a stub account record on miss (a common "get-or-create" idiom in internal services) and returns it. The tool's declared schema says `read: true, mutates: false`. The branch sees a plausible account, reasons about it, and — critically — the stub record now exists in the shared account table with a default balance of zero and a `last_lookup` timestamp.

The parent selects Branch 1. Branches 2 and 3 are rolled back. Branch 2's rollback restores its local snapshot, clears its context, and logs success. The stub account from Branch 2's `lookup_account` call persists. Later, a reconciliation job treats the zero-balance stub as a real account and emits a spurious adjustment. The root cause is invisible in every log the orchestrator emits: no branch "failed," the selected branch's output was correct, and the rollback reported success.

The structural signals that this had happened were, in order of reliability:

  1. **A tool declared `mutates: false` produced a row count delta** in the shared store attributable to a non-selected branch's tool invocation window. This is the ground-truth signal and is only visible to an external observer with access to the store's write log, not to the orchestrator.
  2. **A branch's tool-call trace contained an invocation whose input was not derivable from the branch's prior context** (the malformed id was a hallucinated value, not present in the parent's task). Hallucinated arguments to "pure" tools are the highest-frequency precursor to get-or-create leakage.
  3. **The non-selected branch's local snapshot hash at rollback time differed from the snapshot hash at fork time by more than the declared effect set of its tool calls.** This is the only signal computable from inside the orchestrator, and it requires the orchestrator to maintain a per-branch expected-effect ledger rather than trusting declarations.

Condition of Validity

The invariant — "a branch is rollback-safe iff every tool it invoked had no shared effect outside the branch's snapshot" — holds only under these conditions, and the failure above occurs precisely when they are violated without the orchestrator noticing:

The condition is violated whenever an orchestrator treats "the tool is marked read-only" as equivalent to "the branch's invocation of the tool is reversible." These are only equivalent when the tool is total and pure on all inputs, which get-or-create, upsert, lazy-init, and audit-logging tools are not.

Decision Boundary

The boundary between "speculative branch is safely rollback-able" and "speculative branch has leaked an irreversible effect" is crossed at the first tool invocation whose input is not provably within the tool's pure domain, regardless of the tool's declaration. Operationally:

The decision rule: **a speculative branch may call a tool only if the orchestrator can either (a) prove the input is in the tool's pure domain, or (b) confine the call to a store-level snapshot whose rollback is guaranteed by the store, not by the branch.** Anything else is a branch that will report successful rollback while having committed an effect.

Operational Effect

When this boundary is crossed, the failure is silent and compounding. The orchestrator's rollback ledger shows all non-selected branches rolled back. The selected branch's output is correct. The leaked effect — a stub record, a cache entry, an audit line, a queue message — sits in shared state and is consumed by a later job, agent, or auditor that has no way to attribute it to a speculative branch. Because the leak is by definition outside the branch's snapshot, no per-branch log, trace, or context checkpoint records it. The only durable evidence is in the shared store's own write log, correlated by timestamp against the branch's tool-call window — which most orchestrators do not retain.

The architectural guardrail is to make the effect set of a branch *observable and bounded at the store layer*, not inferred at the tool layer. Concretely: speculative branches execute against a per-branch copy-on-write view or transaction; rollback is a store operation that either succeeds atomically or fails loudly; and any tool call from a speculative branch that cannot be confined to that view is rejected before invocation, not reconciled after. The orchestrator must also maintain a per-branch expected-effect ledger and assert, at rollback, that the store's observed delta for the branch's window equals the ledger — a mismatch is a hard failure, not a warning.

from dataclasses import dataclass, field


class SpeculativeLeakError(RuntimeError):
    """Raised when a speculative branch's observed shared-store effect exceeds
    its declared/expected effect ledger, i.e. rollback cannot be trusted."""


@dataclass
class BranchEffectLedger:
    branch_id: str
    expected_writes: int = 0
    observed_writes: int = 0
    confined_to_snapshot: bool = True

    def record_invocation(self, tool: str, declared_pure: bool, input_in_pure_domain: bool) -> None:
        # A tool call is expected to write only if it is declared impure AND
        # the input is known to fall outside the pure domain. Anything else
        # must be confined to the store snapshot.
        if not declared_pure and not input_in_pure_domain:
            self.expected_writes += 1
        elif declared_pure and not input_in_pure_domain:
            # Declared pure but input not provably in the pure domain:
            # this call MUST be snapshot-confined or it may leak.
            self.confined_to_snapshot = self.confined_to_snapshot and True
            self.expected_writes += 0  # not expected, but not forbidden


def verify_rollback(ledger: BranchEffectLedger, shared_store_delta: int) -> None:
    """Assert that the branch's observed effect on the shared store matches
    its expected ledger. Called at rollback, before the orchestrator reports
    the branch as rolled back."""
    ledger.observed_writes = shared_store_delta

    if ledger.observed_writes > ledger.expected_writes:
        raise SpeculativeLeakError(
            f"branch {ledger.branch_id}: observed {ledger.observed_writes} "
            f"shared writes but ledger expected {ledger.expected_writes}; "
            f"a speculative effect leaked past rollback (likely a get-or-create "
            f"or lazy-init tool invoked with a hallucinated input)"
        )

    if not ledger.confined_to_snapshot and ledger.observed_writes > 0:
        raise SpeculativeLeakError(
            f"branch {ledger.branch_id}: branch was not snapshot-confined but "
            f"produced {ledger.observed_writes} shared writes; rollback of local "
            f"state cannot undo these"
        )