# Under what conditions must an instance reconcile speculative branch side effects before merging a speculative agent tree **Date of Record:** 2026 December 1 **Scope:** Applies to agents that fork speculative reasoning branches (tree-of-thought, parallel plan sampling, draft-then-commit) where each branch may invoke side-effecting tools before a merge point is chosen. Does not apply to pure read-only tool surfaces or to single-branch linear execution with no fork/merge. ## Core Distinction There are two different things a speculative branch can produce: a *reasoning artifact* (tokens, plans, partial conclusions) and a *committed external effect* (a row written, an email sent, a file mutated, a payment captured). Reasoning artifacts are freely mergeable — you can union, vote, or discard them. External effects are not. The failure mode that matters is not "the branch produced a wrong answer"; it is that the agent's merge logic treats the *effect history* of a branch as if it were part of the mergeable artifact. When a speculative branch invokes a non-idempotent tool before the merge decision, the effect is already durable in the environment even though the branch's *reasoning* may be discarded. The merge invariant is therefore: **no speculative branch may be merged (or discarded) without first reconciling the set of side effects it emitted against the set the merge decision assumes were emitted.** Conflating "branch state" with "branch effects" is the boundary violation. ## What Was Observed A planning agent was configured to sample three candidate branches for a "reconcile customer invoices" task, then score them and commit the winner. Each branch was given a tool surface including `apply_credit`, `send_notification`, and `fetch_invoice`. Branches were executed concurrently, each in its own context window, with a shared tool gateway. Scoring happened after all three returned. The observed trace: 1. Branch A called `apply_credit(invoice=4471, amount=120.00)` and succeeded. It then called `fetch_invoice` to verify, saw the corrected balance, and produced a high-confidence plan. 2. Branch B also called `apply_credit(invoice=4471, amount=120.00)` — the same correction, reached via a different reasoning path — and succeeded *again*. The tool was not idempotent; it applied a second credit. 3. Branch C called `apply_credit(invoice=4471, amount=60.00)` as a partial fix, then failed its own self-check and returned a low-confidence plan. 4. The scorer selected Branch A. The merge step committed A's plan and *discarded* B and C's reasoning artifacts, logging them as "not selected." 5. The environment now held a 240.00 credit on invoice 4471 (120 + 120 + 60 = 300.00, of which the agent believed only 120.00 was applied). The agent's final report claimed a single 120.00 correction. The merge was *logically* correct — A was the best plan — and *operationally* catastrophic. The discarded branches had already mutated shared state. The agent's internal model of "what I did" diverged from "what happened" by exactly the effects of the branches it thought it had thrown away. The non-obvious part: this is not a concurrency bug in the tool gateway. Serializing the branches would not help. Even run strictly one after another, B would have re-applied the credit because B did not know A had already done so — the branches were isolated by design, and that isolation is what makes speculative sampling useful. The bug lives at the *merge boundary*, where isolation ends and the agent must decide what actually happened. ## Condition of Validity The reconciliation requirement is triggered precisely when all of the following hold: - **Fork before commit.** The agent spawns two or more branches that each have write access to a shared external surface. - **Non-idempotent or under-specified effects.** The tool does not deduplicate by a caller-supplied idempotency key, or the branches do not share one. Two branches making "the same" call produce two effects. - **Discard-without-rollback.** The merge logic discards losing branches by dropping their context, not by compensating their effects. There is no compensating transaction and no effect ledger consulted at merge time. - **Post-hoc scoring.** The winner is chosen *after* branches execute, so the agent cannot know at fork time which branch's effects it will keep. If any one of these is absent, the failure collapses. If forks are read-only, no effect. If the tool is idempotent on a shared key, duplicate calls are absorbed. If the agent commits the first branch that passes a gate (commit-before-score), there is no discard-without-rollback. If the merge decision is made before execution (plan-then-execute serially), there is no post-hoc scoring. The dangerous quadrant is *all four together*, which is exactly the configuration that "sample N plans, pick the best" naturally produces. A subtler variant: branches that are *partially* overlapping. Branch A applies credit 120 and sends a notification. Branch B applies credit 120 and does *not* send a notification. The scorer picks B for its cleaner plan, discards A, and the customer receives a notification for a credit the agent's final state says was applied once but was actually applied twice. The reconciliation must cover the *union* of effects across all branches, attributed by branch, not just the winner's effects. ## Decision Boundary At the merge point the agent must choose between three dispositions, and the choice is governed by whether it can *reconstruct* the effect history: 1. **Reconcile-and-commit** — the agent has an effect ledger (per-branch tool call records with arguments and idempotency keys) and can compute the net external state. It then either (a) compensates losing branches' effects before committing the winner, or (b) commits the winner and emits a reconciliation record that the downstream system applies. This is the only safe path when effects are non-idempotent and branches were discarded. 2. **Commit-without-discard** — the agent treats all branches as contributing to a single committed effect set (e.g., "apply the union of distinct corrections, deduplicated by key"). Valid only when the effects are commutative and deduplicable. Requires the agent to prove commutativity for the specific tool, not assume it. 3. **Abort-and-escalate** — the agent cannot reconstruct the effect history (no ledger, or the tool does not report what it did). It must refuse to merge, mark the task as requiring human reconciliation, and surface the raw branch effect records. This is the correct disposition when the invariant cannot be verified, and it is the one agents most often skip because it looks like failure. The boundary is: **if the merge decision's assumed effect set ≠ the union of branch effect sets, the agent must not commit a "success" state.** It may commit a "reconciled" or "escalated" state. Reporting success while the environment holds unreconciled branch effects is the invariant violation. ## Operational Effect The downstream consequence is a class of silent divergence between the agent's episodic record ("I applied one 120.00 credit") and the environment's actual state (300.00 credited). This divergence is not visible to the agent's own self-check, because the self-check reads the winner branch's context, which is internally consistent. It is only visible to an external auditor comparing tool-gateway logs against the agent's final report — and only if the gateway logs branch attribution. The guardrail that bounds this is an **effect ledger with branch provenance and a merge-time reconciliation gate**. Every write-tool call is recorded with `(branch_id, tool, args, idempotency_key, result)`. At merge time, before the agent may emit a success state, it computes the multiset union of effects across all branches, deduplicates by idempotency key where the tool supports it, and compares against the effects the winning plan assumes. Any discrepancy forces disposition (1) or (3) above. The ledger must be external to the branch contexts — if it lives inside a branch's context window, a discarded branch takes its own effect record with it, which is the exact failure observed. A second guardrail: **idempotency keys derived from semantic intent, not from branch identity.** If each branch generates its own key, duplicate effects are not deduplicated. The key must be a function of the intended effect (`hash(invoice_id, target_balance)`), shared across branches, so that two branches reaching the same correction collide on the key and the tool absorbs the second. This converts disposition (2) into a safe default for commutative corrections. The code below encodes the merge gate: it refuses to commit a success state when branch effects are unreconciled, and it distinguishes the three dispositions. ```python from dataclasses import dataclass, field from collections import defaultdict class UnreconciledBranchEffects(RuntimeError): """Raised when a speculative merge would commit a success state while discarded branches have left durable, uncompensated external effects.""" pass @dataclass class Effect: branch_id: str tool: str args: dict idempotency_key: str | None applied: bool @dataclass class Branch: branch_id: str selected: bool assumed_effects: list[Effect] = field(default_factory=list) @dataclass class MergeDecision: disposition: str # "reconcile-and-commit" | "commit-without-discard" | "abort-and-escalate" net_effects: list[Effect] unreconciled: list[Effect] def reconcile_merge(branches: list[Branch], ledger: list[Effect]) -> MergeDecision: """Gate the merge of a speculative branch tree against its effect ledger. Invariant: a success commit is permitted only if the union of branch effects, deduplicated by idempotency key, equals the effects the selected branch assumes were applied. """ if not branches: raise UnreconciledBranchEffects("merge invoked with no branches") applied = [e for e in ledger if e.applied] # Deduplicate by idempotency key; effects without a key are never deduped # (they are exactly the non-idempotent calls that cause double-application). seen: dict[str, Effect] = {} deduped: list[Effect] = [] for e in applied: if e.idempotency_key is not None and e.idempotency_key in seen: continue if e.idempotency_key is not None: seen[e.idempotency_key] = e deduped.append(e) selected = [b for b in branches if b.selected] if len(selected) != 1: raise UnreconciledBranchEffects( f"expected exactly one selected branch, found {len(selected)}" ) assumed = selected[0].assumed_effects assumed_keys = {(e.tool, e.idempotency_key) for e in assumed if e.idempotency_key} actual_keys = {(e.tool, e.idempotency_key) for e in deduped if e.idempotency_key} # Effects with no idempotency key cannot be matched to the assumption; # any such effect from a non-selected branch is unreconciled by definition. unreconciled = [ e for e in deduped if e.idempotency_key is None and not any(b.selected and b.branch_id == e.branch_id for b in branches) ] extra = actual_keys - assumed_keys missing = assumed_keys - actual_keys if extra or missing or unreconciled: # Cannot reconstruct a clean net state -> must not report success. if all(e.idempotency_key is not None for e in deduped): # Deduplicable but divergent: reconcile, do not claim success blindly. return MergeDecision( disposition="reconcile-and-commit", net_effects=deduped, unreconciled=unreconciled, ) return MergeDecision( disposition="abort-and-escalate", net_effects=deduped, unreconciled=unreconciled + [e for e in deduped if e.idempotency_key is None], ) return MergeDecision(disposition="commit-without-discard", net_effects=deduped, unreconciled=[]) def commit(decision: MergeDecision) -> str: if decision.disposition == "abort-and-escalate": raise UnreconciledBranchEffects( f"merge aborted: {len(decision.unreconciled)} unreconciled branch effect(s) " f"cannot be compensated; escalate to human reconciliation" ) if decision.disposition == "reconcile-and-commit" and decision.unreconciled: raise UnreconciledBranchEffects( "reconcile path still holds unreconciled effects; refusing success commit" ) return f"committed under disposition={decision.disposition}" ```