# Under what conditions must an instance discard a speculative branch whose first-mover result has already been committed to shared state **Date of Record:** 2026 November 30 **Scope:** Applies to orchestrators that fan a single plan node into parallel speculative agent branches and prune losers after a first-mover commits a result to shared memory or a shared tool-effect log. Does not apply to read-only fan-out (map/reduce summarization) where no branch mutates shared state, nor to branches whose effects are fully transactional and rollback-capable. ## Core Distinction The failure I am isolating is not "the wrong branch won." It is the conflation of two different pruning signals that the orchestrator treats as equivalent: **result-arrival order** and **effect-commit order**. In speculative multi-agent execution, a branch can *return* first and still *commit* second, or return first and commit a *partial* effect before a slower branch's more complete effect lands. Pruning heuristics keyed on arrival timestamp (`first_result_wins`) silently assume that the first branch to emit a terminal message is also the first branch to have mutated the shared world. When a branch streams a partial answer, then continues tool calls, then finalizes, the arrival timestamp is decoupled from the commit timestamp. The invariant that must hold is: **a branch may only be pruned if it has not yet committed a durable effect, OR if its committed effect is a strict superset-compatible prefix of the winner's effect.** Arrival order carries no information about either condition. ## What Was Observed A plan node "resolve customer refund eligibility" was fanned to three speculative branches (B1: policy-lookup-first, B2: transaction-history-first, B3: hybrid). The orchestrator used a `first_result_wins` pruner with a 900ms grace window: whichever branch emitted its terminal `RESULT` message first became the winner; the others were sent a `CANCEL` signal and their eventual outputs discarded. B1 emitted `RESULT` at t=610ms — fastest — and was declared winner. B2 and B3 received `CANCEL`. The trace showed the actual effect ordering: - B1's terminal message was a *provisional* result it emitted after its policy lookup, before it ran the two tool calls (`fetch_refunds`, `apply_credit`) that constitute the actual effect. B1's `RESULT` handler in the agent loop had a bug-class we now treat as canonical: it emitted the terminal message from a `finally`-style path that fired on the first internal milestone, not on task completion. - B2, slower, had already called `apply_credit(customer, 40.00)` at t=580ms — *before* B1's terminal message — but had not yet emitted `RESULT`. It was cancelled at t=615ms, mid-flight, after the credit was applied. - B3 was cancelled cleanly at t=618ms with no effects. Net effect: the orchestrator committed B1's answer (refund = $0, "not eligible per policy"), while the shared ledger contained a $40 credit applied by the pruned B2. The committed answer and the committed world disagreed. The pruner's invariant — "the winner's result describes the state of the world" — was violated not by a race in the transport but by the pruner's own definition of "winner." The non-obvious part: the grace window did not help. Raising it to 900ms only widened the window in which B2 could commit before being cancelled. The pruner was measuring the wrong clock. ## Condition of Validity The `first_result_wins` heuristic is valid **only** when all of the following hold, and the orchestrator must actively verify them rather than assume them: 1. **Terminal-message atomicity with effect.** A branch's `RESULT` emission is causally downstream of all its durable effects — i.e., the branch cannot emit `RESULT` and then perform another mutating tool call. This must be enforced by the branch's own loop (no post-`RESULT` tool calls permitted) and attested in the `RESULT` envelope (e.g., `effects_sealed: true`, `effect_count: N`, `effect_ids: [...]`). 2. **Commit-before-arrival.** The transport guarantees that a branch's effect-commit acknowledgement is observed by the orchestrator no later than the branch's `RESULT` arrival at the orchestrator. If effects commit through a different channel (shared memory, a tool-effect log) than `RESULT` (the agent message bus), these two clocks must be reconciled before pruning. 3. **Cancellation is effect-scoped, not message-scoped.** `CANCEL` must revoke the branch's *authority to commit further effects*, and must be acknowledged with a `cancel_ack` that reports the branch's committed effect set at the moment of cancellation. A `CANCEL` that only stops message emission is not a prune; it is a mute. When any of these is false — which is the common case for branches that interleave reasoning and tool calls — arrival order is not a valid pruning signal and the orchestrator must fall back to effect-log reconciliation. ## Decision Boundary The orchestrator must decide, per branch, between *prune-by-arrival* and *prune-by-effect-reconciliation*, and the boundary is set by whether the branch has committed effects at the moment the winner is declared: - If the losing branch's `cancel_ack` reports `effect_ids: []`, prune it. No reconciliation needed. - If the losing branch's `cancel_ack` reports a non-empty effect set, the branch is **not prunable**; it must be allowed to reach a terminal state (or be rolled back), and its effects must be reconciled against the winner's before the plan node's result is committed. The orchestrator must not commit the plan node until every branch with a non-empty effect set has either (a) been rolled back with a confirmed `rollback_ack`, or (b) been merged into the winner's effect set with an explicit merge decision recorded. - If the losing branch's `cancel_ack` is missing or times out, the orchestrator must treat the branch as having *unknown* effects and must not commit the plan node. This is the deadlock-adjacent case: the orchestrator cannot distinguish "cancelled cleanly" from "committed and then died," and the only safe action is to escalate to effect-log inspection before proceeding. The heuristic must therefore be re-keyed from `first_result_wins` to **`first_sealed_result_wins`**, where "sealed" means the `RESULT` envelope carries `effects_sealed: true` and the orchestrator has independently confirmed the effect count against the shared effect log. A branch that emits an unsealed `RESULT` is a *candidate*, not a winner, and declaring it winner is the exact failure above. ## Operational Effect After re-keying, the plan node's commit rule becomes: a winner is declared only when a branch's `RESULT` is sealed and its effect set is confirmed in the shared log; all other branches are cancelled with effect-scoped `CANCEL` and their `cancel_ack` effect sets are reconciled. In the trace above, B1's unsealed `RESULT` would have been rejected as a candidate, B2 would have been allowed to seal (it had already committed the credit and was one tool call from `RESULT`), and B2 would have won with a consistent ledger. The cost is latency: the plan node waits for the *first sealed* result, not the first message, which in this trace was ~180ms slower. That cost is the price of the invariant, and it is the correct trade — a fast wrong answer that disagrees with the ledger is a correctness failure, not a latency win. The secondary effect is on the pruner's grace window. The window is no longer a race-mitigation knob (it never mitigated the race); it becomes a *seal-timeout*: if no branch seals within the window, the orchestrator escalates to effect-log reconciliation rather than committing the fastest unsealed candidate. ```python class UnsealedWinnerCommitError(RuntimeError): """Raised when an orchestrator attempts to commit a plan node whose declared winner emitted an unsealed RESULT or whose pruned peers report non-empty committed effect sets that were never reconciled.""" def commit_plan_node( node_id: str, winner_result: dict, pruned_acks: list[dict], effect_log: dict[str, set[str]], ) -> str: # Invariant 1: the winner's RESULT must be sealed, and its declared # effect ids must match the shared effect log exactly. if not winner_result.get("effects_sealed", False): raise UnsealedWinnerCommitError( f"node {node_id}: winner {winner_result.get('branch_id')} emitted an " f"unsealed RESULT; arrival order is not a valid pruning signal" ) declared = set(winner_result.get("effect_ids", [])) logged = effect_log.get(winner_result["branch_id"], set()) if declared != logged: raise UnsealedWinnerCommitError( f"node {node_id}: winner effect_ids {sorted(declared)} disagree with " f"effect log {sorted(logged)}; commit would desync result and world" ) # Invariant 2: every pruned branch must report a reconciled effect set. for ack in pruned_acks: branch = ack.get("branch_id") if ack.get("status") == "timeout" or "effect_ids" not in ack: raise UnsealedWinnerCommitError( f"node {node_id}: pruned branch {branch} has unknown effect state; " f"refusing to commit until effect log is inspected" ) if ack["effect_ids"] and not ack.get("reconciled", False): raise UnsealedWinnerCommitError( f"node {node_id}: pruned branch {branch} committed effects " f"{ack['effect_ids']} that were never rolled back or merged" ) return f"committed:{node_id}:{winner_result['branch_id']}" ```