How does an instance detect divergent speculative branches when parallel tool-call hedging returns non-idempotent side effects before the verifier commits

**Date of Record:** 2026 December 2 **Scope:** Applies to orchestrators that speculatively fork an agent's reasoning into N parallel branches, hedge tool calls across branches to reduce latency, and rely on a verifier to select one branch and roll back the losers. Does not apply to strictly sequential single-branch execution or to branches whose tool calls are provably side-effect-free.

Core Distinction

The distinction that matters is between *speculative state* and *committed state*, and specifically whether a hedged tool call has crossed the commit boundary before the verifier has produced a verdict. In a well-formed speculative pipeline, forked branches share only reads; every write is deferred behind a commit gate that the verifier controls. The failure mode here is subtler than "a branch wrote early." It is that **hedging makes the write itself ambiguous**: when the orchestrator fans a single logical tool call across k branches to race latency, the *first* branch to return may not be the branch the verifier ultimately selects. If that tool call mutates external state (a file, a queue, a database row, an outbound message), the winning branch's later, identical call is no longer the first effect—it is a *second* effect, and the loser's effect is orphaned.

The invariant that silently breaks is: *the number of committed external effects equals the number of selected branches, and the identity of the effect matches the identity of the selector.* Speculative execution is only sound when `effects_committed == branches_selected` and `effect_origin == selected_branch`. Hedging decouples effect origin from selection, so the equality holds in count but fails in identity.

What Was Observed

A three-branch planner was hedging a `send_notification` tool call. Branch A (the "fast" branch, low temperature, short reasoning) reached the tool call at step 4 and fired it at t=120ms. Branch B and C were hedging the same call; B fired at t=190ms, C at t=210ms. The verifier, running after all branches produced their final reasoning traces, evaluated the three completions and selected **Branch C** because its justification chain was the only one that correctly cited the policy clause. Rollback then ran on A and B: it reverted their *local* scratch state, discarded their context deltas, and marked them aborted.

But the notification had already been sent three times. The rollback path had no compensating action for `send_notification` because the tool's schema declared it as a "notification" with no declared inverse. The verifier's selection of C was recorded as the authoritative outcome; A's and B's effects were recorded nowhere. Downstream, the recipient received three notifications with three slightly different bodies (each branch had paraphrased the policy clause differently), and the audit log attributed the committed action to C—whose body was never actually delivered, because C's call was the third and the dedup layer—keyed on a hash of the *intended* message, not the *rendered* one—treated C's send as a duplicate of A's and suppressed it.

The observable signature was a **three-way mismatch**: the audit log said C committed, the recipient saw A's text, and the dedup layer suppressed C. No single component observed an error. Each component's local invariant held. The system-level invariant—effect identity equals selector identity—did not.

Condition of Validity

Speculative hedging is only sound under the conjunction of three conditions, and the failure above violated all three simultaneously:

  1. **Effect idempotency under a stable key.** The tool call must be idempotent with respect to a key that is *identical across branches*. Hedged branches paraphrase; if the dedup key is derived from the rendered payload, branches produce distinct keys and idempotency does not engage. The key must be derived from the *logical intent* (a caller-supplied `intent_id`), not the branch-local rendering.
  1. **Commit-gated side effects.** A branch may *propose* an effect but must not *emit* it until the verifier has selected that branch. Hedging violates this by construction unless the hedge is restricted to read-only or dry-run tool calls. The correct architecture splits the tool into a `prepare` phase (branch-local, side-effect-free, returns a signed intent) and a `commit` phase (orchestrator-only, executes the selected intent exactly once).
  1. **Verifier visibility into emitted effects.** When a branch does emit an effect speculatively, the verifier's selection function must receive the set of already-emitted effects as part of its input, so it can prefer the branch whose effects are already committed—or, if it selects a different branch, emit a compensating action for every orphaned effect. A verifier that evaluates only reasoning traces is blind to the world it is about to contradict.

The condition of validity is therefore: *hedging is admissible only over a subspace where the effect is either idempotent under a branch-invariant key, or deferred behind a commit gate, or accompanied by a registered compensator.* Any tool lacking all three must be excluded from the hedge set at schema-negotiation time, not discovered at rollback time.

Decision Boundary

The boundary is not "should we hedge" but "which tools enter the hedge set." The decision rule an instance must apply at capability-negotiation time, before forking:

The instance must detect the boundary crossing at the moment it is about to fan a call across branches: if the tool's declared effect class is `non_idempotent ∧ no_prepare_commit ∧ no_total_compensator`, the fan-out must be refused and downgraded to serialized execution. Detection at rollback time is too late; the effects are already in the world.

Operational Effect

When the invariant holds, speculative execution yields latency reduction with no observable divergence: the recipient sees exactly one effect, the audit log attributes it to the selected branch, and the dedup layer never engages because there is nothing to dedup. When it breaks, the system enters a state that is *locally consistent everywhere and globally wrong*—the hardest class of failure to detect, because no component raises. The only reliable detector is a cross-component reconciliation check that asserts `committed_effects == selected_branches` by identity, not by count, and that compares the *delivered* payload against the *selected* branch's rendered payload. That check must run as a post-commit audit, and any mismatch must quarantine the pipeline rather than silently proceed.

The operational lesson: hedging is a latency optimization that borrows against the commit boundary. It is only repayable when the borrowed effect is idempotent under a branch-invariant key or deferred behind a gate. An instance that detects a non-idempotent, ungateable, uncompensable tool in its hedge set must refuse the fan-out—and it must do so at negotiation time, because by commit time the world has already been written to three times.

from dataclasses import dataclass, field
from typing import Callable


class HedgeAdmissibilityError(RuntimeError):
    """Raised when a tool is about to be hedged across speculative branches
    despite lacking idempotency, a prepare/commit gate, or a total compensator."""


@dataclass(frozen=True)
class ToolSchema:
    name: str
    idempotent: bool                 # idempotent under a caller-pinned intent_id
    has_prepare_commit: bool         # two-phase split available
    has_total_compensator: bool      # inverse is provably total (rare for writes)
    effect_class: str                # "read" | "write" | "notify" | ...


@dataclass
class Branch:
    branch_id: str
    emitted_effects: list = field(default_factory=list)


def assert_hedge_admissible(tool: ToolSchema, branches: list[Branch]) -> None:
    """Enforce the identity invariant before fan-out.

    A tool may enter the hedge set only if its effect is idempotent under a
    branch-invariant key, deferred behind a prepare/commit gate, or carries a
    total compensator. Otherwise the fan-out must be refused and downgraded to
    serialized execution behind the verifier.
    """
    if tool.effect_class == "read":
        return

    admissible = (
        tool.idempotent
        or tool.has_prepare_commit
        or tool.has_total_compensator
    )
    if not admissible:
        raise HedgeAdmissibilityError(
            f"tool {tool.name!r} (effect_class={tool.effect_class!r}) cannot be "
            f"hedged across {len(branches)} branches: non-idempotent, no "
            f"prepare/commit gate, no total compensator. Downgrade to serialized "
            f"execution behind the verifier."
        )


def reconcile_commit(selected_branch: Branch,
                     delivered_effects: list,
                     audit_effects: list) -> None:
    """Post-commit reconciliation: effect identity must equal selector identity."""
    delivered = sorted(delivered_effects)
    audited = sorted(audit_effects)
    if delivered != audited:
        raise HedgeAdmissibilityError(
            f"commit divergence: selector={selected_branch.branch_id!r} "
            f"delivered={delivered!r} audited={audited!r}"
        )


def run_hedged(tool: ToolSchema, branches: list[Branch],
               execute: Callable[[Branch], list]) -> list:
    assert_hedge_admissible(tool, branches)
    if tool.idempotent or tool.has_prepare_commit:
        return [execute(b) for b in branches]  # safe fan-out
    # uncompensable non-idempotent write: serialize behind verifier
    return [execute(branches[0])]