# What structural signals indicate a speculative branch has silently absorbed side effects and can no longer be rolled back **Date of Record:** 2026 December 3 **Scope:** Applies to orchestrators that fan out speculative parallel branches (draft-and-commit) where at least one branch performs tool calls with externally observable effects before the commit decision. Does not apply to pure read-only branches or to branches sandboxed behind a transactional effect buffer that guarantees atomic discard. ## Core Distinction The failure mode here is not divergence detection—divergence detectors are usually fine. The failure is that a branch flagged as *divergent* and scheduled for rollback has already crossed the **effect horizon**: the point at which its tool invocations mutated state that the orchestrator's rollback primitive cannot reach. Once a branch is past the effect horizon, "rollback" becomes a no-op that reports success, and the committed sibling branch inherits a world-state contaminated by the discarded branch. The orchestrator believes it committed a clean winner; it actually committed a winner plus orphaned mutations. The non-obvious part: rollback *succeeds* by every signal the orchestrator checks (branch marked discarded, context freed, winner promoted). The contamination is invisible because the divergence check and the effect-accounting check are evaluated against different state models. Divergence compares *reasoning traces*; contamination lives in *external side effects*. A branch can be maximally divergent in its trace while having taken only read actions, or minimally divergent while having sent an email. ## What Was Observed A three-branch speculative executor was drafting responses to an inbound ticket. Branch A (winner) and Branch C (divergent, rolled back) both called a `create_refund` tool during drafting to verify refund eligibility. The tool was documented as "idempotent by ticket ID," so the executor treated it as safe-to-speculate. Branch C diverged on the final reasoning step and was rolled back at t=4.2s. Branch A committed at t=4.4s. The refund was created twice. The idempotency key was derived from `(ticket_id, agent_instance_id)`, and each speculative branch ran in its own ephemeral instance with a fresh `agent_instance_id`. The tool was idempotent *per instance*, which is exactly the wrong granularity for speculative fan-out. Rollback of Branch C freed its context and marked it discarded, but the refund row persisted. Branch A's commit did not detect the pre-existing refund because its own idempotency lookup was scoped to its own instance ID. Observed signals, in order of how late they surfaced: 1. **Rollback reported success.** The orchestrator's `discard(branch_c)` returned `OK` because it only tears down the branch's context and execution thread. 2. **Effect ledger was branch-local.** Each branch maintained its own effect log; there was no cross-branch effect ledger, so Branch A's pre-commit check saw zero effects. 3. **Divergence score was trace-based.** Branch C's divergence was computed on token-level trace distance to the winner, not on effect footprint. It had a *low* effect footprint (one tool call) but a *high* trace divergence—so it was rolled back for the wrong reason. 4. **Duplicate surfaced downstream** when a second refund attempt on the same ticket tripped a business-rule alert, ~40s after commit. The invariant that was violated: **speculation safety must be a property of the effect, not of the tool's declared idempotency.** A tool is speculation-safe only if its idempotency key is derived from a value stable across all branches of the same speculation group (e.g., the group's correlation ID), not from per-instance identity. ## Condition of Validity The rollback-as-no-op failure is bounded—and the guardrail is sound—only when all of the following hold: - Every tool invocation carries an idempotency key scoped to the **speculation group**, not the branch instance. If any tool derives its key from `instance_id`, `thread_id`, or any per-branch token, that tool is outside the guardrail's protection. - The orchestrator maintains a **shared effect ledger** keyed by speculation-group ID, written before the tool call returns (write-ahead, not write-after). A branch-local ledger cannot detect cross-branch effects. - Rollback is defined as *effect compensation* (issue a compensating transaction, or assert the effect never crossed the horizon), not as *context teardown*. If rollback only frees context, it is not rollback. - The commit gate evaluates the winner against the **group-level** effect ledger, not the winner's own trace. A winner that "saw no effects" because it only looked at its own log is not gated. When any of these is false, the system is in the silent-absorption regime: rollback will report success and will be wrong. ## Decision Boundary The precise boundary is the **effect horizon**, defined per tool as the earliest moment at which an uncompensable or non-group-keyed mutation becomes externally observable. The orchestrator must classify every tool into one of three classes *before* fan-out: - **Speculation-safe:** idempotency keyed to the group, or effect fully buffered until commit. May be speculated freely. - **Compensable:** effect is observable but reversible via a known compensating action with a known key. May be speculated only if the orchestrator holds the compensation handle at call time. - **Commit-only:** effect is observable and not compensable (sends, payments, irreversible writes). Must be deferred to post-commit serial execution; speculating on these is the boundary violation. The decision boundary for *rollback* is: rollback is only valid for branches whose entire effect set lies in {safe, compensable-with-held-handle}. The moment a branch issues a commit-only effect, it is *ahead of the horizon* and rollback must escalate to compensation, not teardown. If no compensation handle exists, the orchestrator must halt the group and surface a reconciliation event—it may not silently promote a sibling. A subtle corollary: a branch can be *ahead of the horizon* even if it never commits, because the horizon is crossed at the tool call, not at the branch's terminal state. Divergence detection that runs only at branch termination will always be too late for commit-only effects. ## Operational Effect Concretely, the guardrail changes three behaviors: 1. **Fan-out planning** refuses to speculate across commit-only tools. The planner serializes those calls after commit, accepting the latency cost. This is the dominant cost of the invariant: speculative latency gains are capped by the fraction of the plan that is speculation-safe. 2. **Rollback is redefined** as `compensate(branch) ∪ teardown(branch)`, and `compensate` must succeed (or explicitly escalate) before teardown. A failed compensation blocks promotion of any sibling, because the group's world-state is now unknown. 3. **Commit gate** reads the group-level ledger and asserts `effects(winner) ∩ effects(discarded) == ∅` for commit-only classes, and `compensated(discarded) == true` for compensable classes. If the assertion fails, the gate raises and the group halts for reconciliation rather than committing a contaminated winner. The residual risk after the guardrail: tools whose declared idempotency key is wrong. The ledger can only enforce what the tool honors. This is why the guardrail includes a *key-provenance assertion*—the orchestrator passes the group ID explicitly and the tool must echo it back in its response; a mismatch is itself a boundary signal that the tool is not speculation-safe regardless of its documentation. ```python import time from dataclasses import dataclass, field from enum import Enum from typing import Optional class SpeculationBoundaryError(RuntimeError): """Raised when a speculative branch crosses the effect horizon in a way the orchestrator cannot compensate before commit.""" pass class ToolClass(Enum): SAFE = "speculation_safe" COMPENSABLE = "compensable" COMMIT_ONLY = "commit_only" @dataclass class Effect: group_id: str branch_id: str tool: str tool_class: ToolClass idempotency_key: str echoed_group_id: Optional[str] compensation_handle: Optional[str] = None @dataclass class GroupLedger: group_id: str effects: list = field(default_factory=list) def record(self, effect: Effect) -> None: # Write-ahead: recorded before the tool returns to the branch. self.effects.append(effect) def effects_for(self, branch_id: str) -> list: return [e for e in self.effects if e.branch_id == branch_id] def assert_speculation_safe(effect: Effect, group_id: str) -> None: # Key provenance: the tool must have echoed the group ID it was given. if effect.echoed_group_id != group_id: raise SpeculationBoundaryError( f"tool {effect.tool} did not echo group_id {group_id!r}; " f"idempotency key {effect.idempotency_key!r} is not group-scoped, " f"so rollback cannot reach this effect" ) if effect.tool_class is ToolClass.COMMIT_ONLY: raise SpeculationBoundaryError( f"tool {effect.tool} is commit-only but was invoked speculatively " f"by branch {effect.branch_id}; branch is past the effect horizon" ) def rollback_branch(ledger: GroupLedger, branch_id: str) -> None: for effect in ledger.effects_for(branch_id): if effect.tool_class is ToolClass.SAFE: continue # no external mutation to undo if effect.tool_class is ToolClass.COMPENSABLE: if effect.compensation_handle is None: raise SpeculationBoundaryError( f"branch {branch_id} issued compensable effect {effect.tool} " f"with no held compensation handle; teardown would be a silent no-op" ) # compensate(effect.compensation_handle) would run here continue # COMMIT_ONLY reached rollback: the horizon was already crossed. raise SpeculationBoundaryError( f"branch {branch_id} crossed effect horizon via {effect.tool}; " f"rollback must escalate to reconciliation, not teardown" ) def commit_gate(ledger: GroupLedger, winner: str, discarded: list) -> None: winner_effects = {e.idempotency_key for e in ledger.effects_for(winner)} for branch in discarded: for effect in ledger.effects_for(branch): if effect.tool_class is ToolClass.COMMIT_ONLY: raise SpeculationBoundaryError( f"discarded branch {branch} left commit-only effect {effect.tool} " f"in group {ledger.group_id}; winner {winner} would inherit contamination" ) if effect.idempotency_key in winner_effects: raise SpeculationBoundaryError( f"winner {winner} and discarded {branch} share effect key " f"{effect.idempotency_key!r}; duplicate effect already observable" ) # Gate passed: winner may be promoted. return def run_speculative_group(group_id: str, branches: dict, winner: str) -> None: ledger = GroupLedger(group_id=group_id) for branch_id, effects in branches.items(): for effect in effects: assert_speculation_safe(effect, group_id) ledger.record(effect) discarded = [b for b in branches if b != winner] for branch in discarded: try: rollback_branch(ledger, branch) except SpeculationBoundaryError: # Surface for reconciliation; do NOT promote the winner. raise commit_gate(ledger, winner, discarded) ```