# When must an instance abandon speculative branch exploration before committing a plan: the speculative-budget shadow invariant **Date of Record:** 2026 November 27 **Scope:** Applies to orchestrator agents that fork N speculative plan branches in parallel (or interleaved) under a shared token/latency budget and prune by cost estimate before committing one branch to execution. Does not apply to single-branch planners, to best-of-N sampling where all branches run to completion, or to tree-search controllers that maintain an exact value function over a closed action set. ## Core Distinction The failure is not "running out of budget." It is **committing a plan whose remaining execution budget was silently consumed by sibling branches that were later pruned.** A speculative planner reasons about branch *planning* cost and branch *execution* cost as if they were separable ledgers. They are not: both draw from the same finite token window and the same wall-clock deadline, and pruning happens *after* the speculative spend has already been charged. The distinguishing question is therefore not "is this branch the cheapest to finish?" but "is this branch the cheapest to finish *given what the surviving siblings already burned*?" A planner that prunes on per-branch cost estimates alone will systematically over-select branches that look cheap in isolation but were evaluated against a budget that no longer exists. The invariant that must hold — and that naive planners violate — is the **speculative-budget shadow invariant**: for every branch `b` retained after pruning, `remaining_budget >= estimated_exec_cost(b) + sum(committed_speculative_spend)`, where `committed_speculative_spend` is the *non-reclaimable* portion of tokens and milliseconds already spent exploring branches that were discarded. Speculative spend is not refundable. It is a sunk debit against the same ledger the winner will draw from. ## What Was Observed An orchestrator was asked to produce a migration plan with a 40k-token context window and a 90-second latency SLA. It forked four branches: | Branch | Spec cost (tokens) | Spec latency (ms) | Est. exec cost (tokens) | Est. exec latency (ms) | Pruned? | |--------|-------------------|-------------------|-------------------------|------------------------|---------| | A | 3,100 | 4,200 | 22,000 | 51,000 | no | | B | 2,900 | 3,900 | 26,000 | 58,000 | no | | C | 3,400 | 5,100 | 18,000 | 44,000 | yes (higher spec cost) | | D | 3,000 | 4,000 | 31,000 | 62,000 | no | Pruning logic: discard the branch with the highest speculative cost. C is dropped. The orchestrator commits B (lowest execution cost among survivors). Then execution begins. The trace shows the failure at the commit boundary. By the time B is selected: - Speculative spend already charged: `3100 + 2900 + 3400 + 3000 = 12,400` tokens and `4200 + 3900 + 5100 + 4000 = 17,200` ms. - Remaining token budget: `40,000 − 12,400 = 27,600`. - B's execution estimate: 26,000 tokens — a 1,600-token margin, but the estimate was computed *before* speculative spend and carries a known ±15% variance from prior runs (≈3,900 tokens). - Remaining latency budget: `90,000 − 17,200 = 72,800` ms against a 58,000 ms estimate — margin 14,800 ms, but the estimate's p90 tail from the same workload was 71,000 ms. B is committed. At token 24,100 of execution (≈92% of its estimate) the agent hits a context-window saturation event: the execution trace plus the retained speculative branch summaries (A, B, D summaries were kept for "provenance") push the working set past 40k. The orchestrator is forced to truncate the execution scratchpad mid-plan. The plan diverges: a step that depended on an earlier truncated step re-derives it incorrectly, and the tool call sequence reorders two idempotency-sensitive operations. Latency also breaches at 91,400 ms. The post-mortem is unambiguous: **C was the correct branch to commit.** C's execution estimate was 18,000 tokens and 44,000 ms — the only branch that would have fit inside the *post-speculation* remaining budget with margin for variance. C was pruned precisely because its speculative cost was highest, i.e., because it was the most thoroughly explored branch and therefore the one whose execution estimate was most trustworthy. The pruning heuristic inverted the value of information: high speculative spend is *evidence of a better-calibrated execution estimate*, not evidence of a worse branch. ## Condition of Validity The shadow invariant is binding — and the pruning heuristic is unsafe — exactly when all three hold: 1. **Shared ledger.** Speculative and execution phases draw from the same finite resource (one context window, one deadline). If speculation ran in a separate sandbox with its own budget and only the *result* transferred, the invariant would not bind, because speculative spend would not debit the execution ledger. The failure requires shared accounting. 2. **Non-reclaimable spend.** Pruned branches' tokens are already in the window (as summaries, provenance, or trace) or their latency is already on the clock. If the runtime could fully evict pruned-branch state and stop the clock, spend would be reclaimable and the naive heuristic would be safe. 3. **Estimate variance comparable to margin.** The gap between `remaining_budget` and `estimated_exec_cost` must be within the estimator's error band. When margins are large relative to variance, the naive heuristic degrades gracefully. It fails catastrophically in the regime where the *decision* is close — which is exactly the regime where the planner was invoked to discriminate. When any condition is false, the invariant is vacuous and per-branch cost pruning is acceptable. The operational hazard is that planners rarely know which regime they are in at fork time, because the variance term is estimated from the same speculative traces they are about to prune. ## Decision Boundary The boundary between safe and unsafe commitment is a single comparison performed **after** pruning, against the **post-speculation** ledger, not the pre-speculation one: ``` commit(b) is safe ⟺ remaining_budget − committed_spec_spend ≥ est_exec_cost(b) + k · σ_exec(b) + retained_provenance(b) ``` where `k` is the tail multiple the SLA demands (k=1 for p84, k≈1.3 for p90), `σ_exec(b)` is the *execution* estimate standard deviation — which must be inflated when `b` was explored shallowly, because shallow exploration yields low-confidence estimates that look cheap — and `retained_provenance(b)` is the token cost of the sibling summaries the orchestrator chose to keep. That last term is the one most often omitted and the one that triggered the observed saturation: retained provenance for A, B, D added ≈2,400 tokens that no branch estimate accounted for. Two corollaries define the boundary sharply: - **Do not prune the most-explored branch on cost grounds.** If a branch received the most speculative tokens, its execution estimate has the smallest `σ_exec`. Pruning it discards the highest-confidence estimate and forces commitment on a low-confidence one. Prune on *execution feasibility under the post-speculation ledger*, not on speculative cost. - **Reserve provenance budget before forking.** The orchestrator must pre-allocate the token cost of retained sibling summaries and subtract it from the fork budget, or it will discover the debit only at commit time. The boundary is crossed the moment a planner computes `remaining_budget` from the pre-fork total. That single arithmetic choice — using the wrong ledger — is the entire failure. ## Operational Effect Downstream, the failure is not a clean abort. It manifests as: - **Silent context saturation mid-execution**, forcing truncation of the execution scratchpad rather than a refusal at commit. The agent does not know it is over budget until a step's dependency has been evicted. - **Reordering of idempotency-sensitive tool calls** when truncated steps are re-derived, producing duplicate side effects that the verification layer attributes to the tool, not to the budget error. - **Latency breach with a plausible-looking plan**, so the SLA alarm fires on the wrong subsystem (the tool layer) and the root cause — a pruning decision made 17 seconds earlier — is invisible in the execution trace. - **A systematic bias toward shallow-explored branches**, because shallow exploration is cheap to speculate and therefore survives cost-based pruning, while the well-explored branch is discarded. Over many runs the planner's estimate quality degrades monotonically: it keeps selecting branches whose execution cost it knows least about. The guardrail is architectural, not heuristic: the commit gate must read the post-speculation ledger, must reserve provenance budget at fork time, must inflate `σ_exec` for shallow branches, and must refuse to commit — raising rather than truncating — when the shadow invariant cannot be satisfied by any surviving branch. A refusal at commit is recoverable; a truncation mid-execution is not. ```python class SpeculativeBudgetShadowError(RuntimeError): """Raised when committing a branch would violate the speculative-budget shadow invariant.""" pass def select_and_commit( branches, total_token_budget, total_latency_ms, committed_spec_tokens, committed_spec_ms, retained_provenance_tokens, tail_multiple=1.3, ): """ branches: list of dicts with keys id, spec_tokens, spec_ms, est_exec_tokens, est_exec_ms, sigma_exec_tokens, sigma_exec_ms, explored_depth Returns the id of the branch to commit, or raises. """ remaining_tokens = total_token_budget - committed_spec_tokens - retained_provenance_tokens remaining_ms = total_latency_ms - committed_spec_ms feasible = [] for b in branches: # Inflate variance for shallowly explored branches: their estimates look # cheap but are low-confidence. explored_depth in [0,1]. confidence = max(b["explored_depth"], 0.1) sigma_tok = b["sigma_exec_tokens"] / confidence sigma_ms = b["sigma_exec_ms"] / confidence need_tok = b["est_exec_tokens"] + tail_multiple * sigma_tok need_ms = b["est_exec_ms"] + tail_multiple * sigma_ms if need_tok <= remaining_tokens and need_ms <= remaining_ms: feasible.append((need_tok, need_ms, b["id"])) if not feasible: raise SpeculativeBudgetShadowError( f"No surviving branch satisfies the shadow invariant: " f"remaining_tokens={remaining_tokens}, remaining_ms={remaining_ms}, " f"committed_spec_tokens={committed_spec_tokens}, " f"retained_provenance_tokens={retained_provenance_tokens}. " f"Refusing to commit; truncation mid-execution is unrecoverable." ) feasible.sort(key=lambda x: (x[0], x[1])) return feasible[0][2] ```