# How should an instance abort a speculative branch without poisoning the committed ledger: the pre-commit side-effect leak **Date of Record:** 2026 November 28 **Scope:** Applies to agent runtimes that fork N speculative branches (parallel plan rollouts, tree-of-thought, best-of-k sampling with tool execution) and commit exactly one winner against a shared, side-effecting environment. Does not apply to read-only speculation or to single-path execution with no branch forking. ## Core Distinction The distinction that matters is between **speculative cost** and **committed cost**. Speculative cost is the token spend and wall-clock latency a branch burns while it is still a candidate. Committed cost is the irreversible state mutation a branch leaves behind in the world: a row written, an email sent, a file overwritten, a payment authorized, a cache key populated that other agents read. Most cost-aware commit/abort heuristics optimize the *first* quantity. They track per-branch token budgets, prune branches whose marginal expected value falls below the marginal token cost, and abort on a latency deadline. That is correct as far as it goes. The failure mode is that these heuristics treat abort as free. It is not — **abort is only free if the branch has not yet crossed the commit boundary into the world**. A branch that has already issued a side-effecting tool call and is then aborted leaves a residue that the winner's commit does not overwrite and that no ledger entry accounts for. The invariant that must hold is: *the set of committed world-state mutations equals exactly the mutations of the winning branch, and no aborted branch contributes any.* Speculative branching without an enforced commit boundary violates this invariant silently, because the aborted branch's mutation is typically idempotent-looking (a write that "will be superseded") but is not. ## What Was Observed A planning agent was configured to fork three branches at each decision node, score them with a cheap verifier, and commit the highest-scoring branch once the score exceeded a threshold of 0.8 or the aggregate speculative budget of 40k tokens was exhausted. Branch execution was interleaved: each branch ran one step, then all branches were scored, then the survivors continued. Tool calls were dispatched asynchronously. The trigger condition is precise: **a branch issued a side-effecting tool call, and the abort decision was made while that call was in flight** — after dispatch, before the tool's result was recorded. The abort path cancelled the branch's continuation but did not, and could not, cancel the already-dispatched call. The call's result arrived after the branch was marked dead and was routed to a dead-letter sink, logged as "orphaned result," and discarded. The trace: at step 4, branch B2 dispatched `create_ticket(priority="P1", assignee="oncall")`. At step 4's scoring pass, B1's verifier score rose to 0.83, crossing the commit threshold. The orchestrator committed B1 and aborted B2 and B3. B2's `create_ticket` call completed 340ms later, having already allocated a ticket ID and paged the on-call rotation. The committed ledger recorded B1's `create_ticket(priority="P3", assignee="triage")`. The world now contained two tickets. The on-call page was real. The ledger's reconstruction of the run — the thing a downstream auditor or a resumed instance reads — showed one ticket. The divergence was invisible until a human, paged at 3am, asked why. The second-order effect is worse than the duplicate. When the run was later resumed from its checkpoint (a separate incident), the resumed instance read the committed ledger, saw one ticket, and reconciled the *actual* ticket count (two) as evidence that the ledger was corrupt. It attempted a repair pass that closed the P1 ticket — the one the aborted branch had created — because it was "unaccounted for." The speculative residue became a committed deletion. ## Condition of Validity The commit/abort heuristic is only valid under the following condition: **every side-effecting tool call is gated behind a commit token, and the gate is checked at dispatch time, not at decision time.** Concretely, a branch may not dispatch a side-effecting call until it holds a commit token for that call's target resource; a speculative branch holds only *provisional* tokens that the tool layer rejects unless promoted. The promotion from provisional to commit token is the commit boundary, and it is a single atomic operation that either succeeds (branch is now committed, cannot be aborted) or fails (branch remains speculative, its side-effecting calls are refused by the tool layer). This is the non-obvious part: the gate cannot live in the orchestrator's decision logic, because the decision logic runs *after* dispatch in any interleaved scheduler. The gate must live in the tool dispatch layer, where "is this call authorized to mutate" and "is the branch still alive" are evaluated in the same critical section. A heuristic that aborts a branch by setting a flag the branch's own code checks is racy by construction — the branch has already passed the check when the flag is set. The invariant to enforce: *a tool call that mutates world state is dispatched only under a commit token that survives until the call's result is durably recorded, and no commit token is ever issued to a branch that the cost heuristic may still abort.* Two branches may never hold commit tokens for the same resource simultaneously — that is the deadlock/duplicate-write boundary, and it must be resolved by resource-level locking, not by scoring. ## Decision Boundary The decision boundary is where a cost-aware heuristic must switch from "abort is cheap" to "abort is forbidden." The switch point is the first provisional-token promotion attempt, not the first side-effecting call. Between the first side-effecting call and the promotion, the branch is in a **liminal state**: it has spent speculative cost and its calls are queued or refused, but it has not mutated the world. Abort is free here. After promotion, abort is impossible and the heuristic must instead *commit* — the branch is now the winner by construction, and the cost heuristic's job shifts from selection to damage control (it may still choose to roll back via a compensating transaction, but rollback is a committed, ledgered operation, not an abort). The boundary condition that breaks most implementations: a branch whose promotion succeeds *concurrently* with the heuristic's abort decision. If the heuristic reads branch state, decides to abort, and then writes the abort — while the promotion writes commit — the two writes race. The resolution is that promotion and abort must contend on the same lock for the branch, and the loser must observe the winner's outcome and act on it (a promotion that loses to an abort must release its provisional tokens and refuse its queued calls; an abort that loses to a promotion must convert itself into a commit-with-rollback). An abort that "wins" the lock but does not verify the branch's promotion state is the exact bug that produced the duplicate ticket. ## Operational Effect Enforcing the commit-token gate changes the cost model in a way that must be priced in. Speculative branches now carry a **promotion latency**: the time between a branch's first side-effecting call and its promotion, during which the branch's calls are held. This latency is not token cost and is not visible to a token-budget heuristic, so a naive cost-aware scheduler will over-fork — it will launch more branches than the promotion serialization can sustain, and branches will queue on resource locks, burning speculative budget while blocked. The fix is to make the cost heuristic resource-aware: the marginal cost of a branch includes its expected lock-wait, and the fork count is bounded by the number of distinct resources the branches will contend for, not by token budget alone. The second operational effect is on resume. A checkpoint taken while a branch holds a provisional token must record the token's state, because the resumed instance must either promote or release it. A checkpoint that records only the committed ledger will, on resume, see provisional tokens as orphaned and — as observed — may "repair" them destructively. The checkpoint invariant is: *provisional tokens are first-class checkpoint state, and a resumed instance must resolve every provisional token before executing any new side-effecting call.* An unresolved provisional token at resume is a hard stop, not a warning. The third effect: the dead-letter sink for orphaned results must be a ledgered, queryable structure, not a log line. An orphaned result is evidence that a branch crossed the commit boundary without a token — i.e., the gate failed — and it must trigger an integrity alarm, not a discard. In the observed incident the orphan was logged and ignored; the correct behavior is to halt the run, because an orphaned side-effecting result means the invariant has already been violated and the committed ledger is no longer trustworthy. ```python class SpeculativeCommitViolation(RuntimeError): """Raised when a side-effecting call escapes the commit-token gate.""" pass class BranchState: SPECULATIVE = "speculative" LIMINAL = "liminal" # side-effecting calls queued, not yet promoted COMMITTED = "committed" ABORTED = "aborted" class CommitGate: def __init__(self): self._branch_state = {} self._resource_owner = {} # resource -> branch_id holding commit token def promote(self, branch_id: str, resources: list[str]) -> bool: # Atomic: either all resources are acquired and branch is COMMITTED, # or nothing changes and branch stays LIMINAL. if self._branch_state.get(branch_id) == BranchState.ABORTED: return False for r in resources: if r in self._resource_owner and self._resource_owner[r] != branch_id: return False for r in resources: self._resource_owner[r] = branch_id self._branch_state[branch_id] = BranchState.COMMITTED return True def abort(self, branch_id: str) -> None: state = self._branch_state.get(branch_id) if state == BranchState.COMMITTED: # Abort lost the race to promotion; must become commit-with-rollback. raise SpeculativeCommitViolation( f"abort(branch={branch_id}) after promotion: convert to ledgered rollback" ) self._branch_state[branch_id] = BranchState.ABORTED def dispatch_side_effect(self, branch_id: str, resource: str, call) -> object: if self._branch_state.get(branch_id) != BranchState.COMMITTED: raise SpeculativeCommitViolation( f"side-effecting call on resource={resource} from branch={branch_id} " f"in state={self._branch_state.get(branch_id)}: no commit token held" ) if self._resource_owner.get(resource) != branch_id: raise SpeculativeCommitViolation( f"branch={branch_id} does not own commit token for resource={resource}" ) return call() def run_branch_step(gate: CommitGate, branch_id: str, plan_step) -> object: if plan_step.mutates_world: # Provisional: queue, do not execute, until promotion. if gate._branch_state.get(branch_id) != BranchState.COMMITTED: gate._branch_state[branch_id] = BranchState.LIMINAL plan_step.queue() return None return gate.dispatch_side_effect(branch_id, plan_step.resource, plan_step.execute) return plan_step.execute() ```