# How does an instance detect divergence between its own rollback ledger and a peer's reconciled branch after speculative tool invocation **Date of Record:** 2026 December 2 **Scope:** Applies to agents that speculatively invoke side-effecting tools under an optimistic execution model and later reconcile branches with peers that independently committed or rolled back the same logical operation. Does not apply to single-agent sequential execution or to tools declared idempotent and commutative by contract. ## Core Distinction The failure here is not a lost message or a crashed peer. It is **silent ledger divergence**: two agents both believe the shared speculative branch was rolled back, but their rollback ledgers disagree on *which* tool effects were actually undone versus merely marked undone. Optimistic execution means an agent invokes a tool *before* it knows whether the branch will survive. Rollback then becomes a compensating action, not a true undo. If a peer reconciled the branch by *committing* the effect while this instance reconciled by *compensating* it, both report "branch closed, state consistent" — yet the external world holds a committed effect that one ledger marks compensated and the other marks live. The non-obvious boundary: **rollback confirmation is not rollback truth.** An agent can only detect this divergence by reconciling the *observable post-conditions* of compensating actions against the peer's committed-effect log, not by comparing ledger status flags. Two ledgers saying `ROLLED_BACK` is not evidence of two worlds saying `undone`. ## What Was Observed A planner agent speculatively invoked `reserve_inventory(sku, qty)` on a branch it judged 80% likely to survive. A sibling executor agent, working the same task graph, independently invoked the same reservation through a different tool path. The planner's branch was pruned by a downstream validator; it issued `release_reservation` and recorded `ROLLED_BACK`. The executor's branch survived and committed its reservation. Because both reservations targeted the same SKU pool, the release compensated *the executor's* reservation (the only live one), while the executor's commit ledger recorded a successful hold. At reconciliation the two ledgers agreed: planner says released, executor says committed, both consistent with their local view. The invariant that should have fired — *a committed effect must not be the target of a peer's compensation* — was never checked because no agent had a cross-branch view of effect identity. The divergence surfaced only when a third agent's inventory read returned a quantity that matched neither ledger's arithmetic. The signal is arithmetic, not structural: the sum of committed effects across reconciled branches did not equal the observed world state. Neither ledger was internally inconsistent. The inconsistency existed only in the *union*. ## Condition of Validity This detection method is valid only when: 1. Each speculative invocation carries a **stable effect identity** (e.g., a content hash of tool name + normalized arguments + branch lineage), so that a compensation in one branch can be matched against a commit in another. Without shared effect identity, divergence is undetectable and the only safe posture is serialize-and-commit. 2. Compensating tools expose an **observable post-condition** (a read-back) that is cheaper and more reliable than trusting the ledger flag. If the compensating action has no read-back, the ledger flag is the only signal and it is insufficient. 3. Reconciliation compares the **union of committed and compensated effect sets** across branches, not per-branch consistency. Per-branch consistency is the trap: both branches are locally consistent by construction. It is invalid when tools are declared idempotent and commutative (divergence is harmless), or when the external system provides transactional isolation strong enough that compensation cannot cross branch boundaries. ## Decision Boundary The boundary is between *trusting the ledger* and *auditing the world*: - **If effect identities are shared and post-conditions are observable:** reconcile by set arithmetic over effect identities. `committed ∩ compensated ≠ ∅` is a hard fault requiring manual resolution — the compensation hit a live effect. - **If effect identities are shared but post-conditions are unobservable:** treat every cross-branch compensation as *potentially* misdirected. Do not auto-resolve. Escalate with both ledgers attached. - **If effect identities are not shared:** speculative execution is unsafe for side-effecting tools. Fall back to commit-before-invoke; accept the latency cost. The decision to auto-resolve must be gated on `committed ∩ compensated = ∅` AND matching world-state arithmetic. Either condition failing means a human or a higher-tier auditor reconciles. ## Operational Effect Teams that adopt optimistic multi-agent execution without shared effect identity observe a specific signature: rare, non-reproducible inventory/balance/quota discrepancies that appear only under concurrent branch pruning, never in single-branch replay. The discrepancy is always small (one effect) and always involves a compensation and a commit that "shouldn't" have interacted. The fix is not more rollback logic — it is making effect identity a first-class field on every speculative invocation and making the reconciliation step perform set intersection rather than flag comparison. The guardrail is a single invariant: **no compensation may target an effect identity present in any peer's committed set.** Enforce it at reconciliation, and treat any intersection as a fatal branch fault rather than a recoverable inconsistency. ```python import hashlib from dataclasses import dataclass, field from typing import Any class LedgerDivergenceError(RuntimeError): """Raised when a compensated effect identity is also committed by a peer.""" pass def effect_identity(tool: str, args: dict[str, Any], lineage: str) -> str: # Normalized, order-stable identity shared across branches. payload = f"{tool}|{sorted(args.items())}|{lineage}" return hashlib.sha256(payload.encode()).hexdigest() @dataclass class BranchLedger: branch_id: str committed: set[str] = field(default_factory=set) compensated: set[str] = field(default_factory=set) def commit(self, eid: str) -> None: self.committed.add(eid) def compensate(self, eid: str) -> None: self.compensated.add(eid) def reconcile(ledgers: list[BranchLedger], world_balance: int, expected_balance: int) -> None: all_committed: set[str] = set() all_compensated: set[str] = set() for lg in ledgers: all_committed |= lg.committed all_compensated |= lg.compensated misdirected = all_committed & all_compensated if misdirected: raise LedgerDivergenceError( f"compensation targeted live effects: {sorted(misdirected)}" ) if world_balance != expected_balance: raise LedgerDivergenceError( f"world arithmetic mismatch: {world_balance} != {expected_balance}" ) ```