# Why does an instance of a speculative plan tree silently resurrect pruned branches under cost-bounded parallel pruning? **Date of Record:** 2026 November 19 **Scope:** Applies to agent runtimes that expand a plan tree by speculatively forking multiple candidate sub-plans in parallel, score them against a cost budget, and prune losers mid-flight. Does not apply to strictly sequential plan-execute agents with a single active branch, nor to offline tree-search planners that never overlap branch lifetimes. ## Core Distinction The failure is not "the pruner picked the wrong branch." It is a **liveness/ordering defect between the pruning decision and the in-flight branch's own continuation**: a branch is *logically* dead (its budget reservation is released, its node marked pruned in the tree) but its *physical* execution context — a coroutine, a worker task, an in-flight tool call — is still running and still holds a reference to a shared mutable accumulator (the plan's partial result, a scratchpad, a capability token, or a reservation ledger). When that orphaned branch later completes a step, it writes its output into the shared state as if it were still live. The tree says "pruned," the state says "contributed." The subtlety is that cost accounting is usually *correct* at the moment of pruning and *wrong* shortly after. The pruner releases the reservation; the zombie branch, unaware, spends against a reservation that no longer exists, or worse, against the *parent's* remaining budget, silently inflating the effective cost of the surviving branch. This is the boundary: **the invariant "a pruned branch contributes nothing" is a statement about logical state, but the system's coherence depends on it being a statement about physical execution too, and the two diverge across the prune-to-cancel window.** ## What Was Observed A plan tree with fan-out factor 4 and a per-node cost budget of 2,000 tokens per branch. The pruner triggers when cumulative speculative spend exceeds 60% of remaining parent budget or when a branch's projected completion cost exceeds the best sibling's realized cost by 1.5×. On trigger, `prune(branch_id)` does three things: marks the node `PRUNED`, releases its budget reservation, and calls `branch.cancel()`. `branch.cancel()` was implemented as setting a cooperative flag checked at the *top* of each reasoning step. A branch mid-way through a tool call — say, a retrieval that takes 400ms — does not check the flag until the tool returns and the next step begins. During that window the branch is a zombie: flagged, un-halted, still holding the accumulator handle. The observed trace on a representative run: 1. `t=0ms` — Branch B2 forked, reservation `+1800 tokens`, holds `acc` (shared partial-result dict). 2. `t=310ms` — B2 issues tool call `retrieve(q)`. Cooperative flag not yet checked. 3. `t=340ms` — Pruner fires (B1 cheaper by 1.6×). B2 marked `PRUNED`, reservation released (`-1800`), `cancel()` sets flag. 4. `t=750ms` — `retrieve` returns. B2 appends `{"evidence": [...]}` to `acc` **before** the next step's flag check. 5. `t=751ms` — B2 checks flag, halts cleanly. Tree reports clean prune. 6. `t=900ms` — B1 completes, reads `acc`, finds B2's evidence merged into its own result. The surviving plan is now a *chimera*: B1's reasoning with B2's retrieved context, which B1 never budgeted for and never validated. The cost ledger showed B1 spent 1,940 tokens against a 2,000 budget — but the *content* it reasoned over included 380 tokens of B2's retrieval that were never charged to B1. The budget was enforced on the ledger, not on the actual context that reached the model. In a second observed variant, B2's zombie write landed *after* B1 had already consumed `acc`, so the corruption was invisible at decision time and surfaced only when a downstream verifier replayed the plan and got a different result — a non-reproducible divergence. ## Condition of Validity The "pruned means silent" invariant holds only when **the prune operation is a barrier, not a signal** — that is, when `prune()` does not return until the branch has provably stopped writing to any shared handle. Concretely, the invariant is valid iff all three hold: - **Cancellation is preemptive at write boundaries, not step boundaries.** The branch must check liveness (or be structurally prevented from writing) at every mutation of shared state, including inside tool-call completion handlers, not only at the top of the reasoning loop. - **The shared accumulator is generation-tagged.** Every write carries the branch's generation epoch; the accumulator rejects writes from an epoch whose node is `PRUNED`. This converts a race into a detectable rejection. - **Budget reservation release is ordered after physical quiescence.** The reservation must not be returned to the parent pool until the branch's execution context is joined (or confirmed halted). Releasing early lets the surviving branch over-consume the freed budget while the zombie still spends, so the ledger's "remaining" is a fiction. When any of these is absent, the invariant is a hope, not a guarantee. The common wrong assumption is that cooperative cancellation at step granularity is "good enough" because steps are short — but tool calls and model round-trips are the *longest* steps, which is exactly where the window is widest. ## Decision Boundary The engineering decision is where to place the prune barrier relative to the cost budget: - **If tool calls are sub-100ms and fully local**, cooperative step-granularity cancellation is acceptable *provided* the accumulator is generation-tagged as a backstop. The window is small but nonzero; the tag makes the residual race detectable rather than silent. - **If any branch step can block on a remote call (retrieval, model inference, external API)**, prune must be a join: `prune()` blocks until the branch's in-flight step completes or is hard-aborted, and only then releases the reservation. This costs latency on the prune path — you wait out the longest in-flight step — and that latency must itself be budgeted, or the pruner's own wait becomes a cost it didn't account for. - **If branches share a mutable accumulator at all**, generation tagging is mandatory regardless of cancellation model. The tag is the only thing that makes a chimera result *detectable* after the fact; without it, replay divergence is the only symptom and it is non-local. The trap is choosing "fast prune" (signal-only) to keep the pruner cheap, then discovering the budget ledger and the model's actual context have silently diverged. The correct boundary: **the cost budget must be enforced against the context that reaches the model, not against the reservation ledger.** If those can differ — and under signal-only pruning they can — the budget is advisory, not binding. ## Operational Effect Under signal-only pruning, three effects compound: 1. **Budget under-count.** Zombie writes consume real tokens (tool output tokens enter the surviving branch's context) that the ledger never charges. Effective spend drifts above nominal budget by the size of the prune-to-cancel window times the write rate. In the observed run this was ~19% over the charged figure. 2. **Chimera plans.** Surviving branches reason over evidence they never selected, producing plans that are locally coherent but globally unattributable — no branch can be blamed for the content, and replay cannot reproduce it because the interleaving is timing-dependent. 3. **Verification replay mismatch.** A downstream auditor replaying the plan from the recorded tree gets a different result than the live run, because the tree records the prune but not the post-prune write. The auditor reports a discrepancy that looks like nondeterminism in the model but is actually a state-coherence defect in the pruner. The guardrail that bounds all three is the same: **make prune a barrier with generation-tagged writes, and enforce budget against realized context, not reservations.** The cost is prune latency; the alternative is a plan tree that lies about what it pruned. ```python import time from dataclasses import dataclass, field from typing import Any class PrunedBranchWriteError(RuntimeError): """Raised when a branch writes to shared state after its node was pruned.""" pass @dataclass class GenerationTaggedAccumulator: """Shared partial-result store that rejects writes from pruned epochs.""" _pruned_epochs: set = field(default_factory=set) _data: dict = field(default_factory=dict) def mark_pruned(self, epoch: int) -> None: self._pruned_epochs.add(epoch) def write(self, epoch: int, key: str, value: Any) -> None: if epoch in self._pruned_epochs: # The zombie branch tried to contribute after being pruned. raise PrunedBranchWriteError( f"epoch {epoch} is pruned; write to {key!r} rejected" ) self._data[key] = value @dataclass class Branch: epoch: int acc: GenerationTaggedAccumulator _halted: bool = False def cancel(self) -> None: self._halted = True def run_step(self, key: str, value: Any) -> None: # Simulate a blocking tool call that returns before the halt check. time.sleep(0.05) # Write happens at the tool-completion boundary, before the next # top-of-loop halt check — this is the prune-to-cancel window. self.acc.write(self.epoch, key, value) if self._halted: return def prune_and_join(branch: Branch, acc: GenerationTaggedAccumulator) -> None: """Barrier prune: mark pruned, cancel, and require quiescence. The generation tag is the backstop; the join is the primary guarantee. """ acc.mark_pruned(branch.epoch) branch.cancel() # A correct implementation would join the worker here. If the worker # is still in flight and writes, the tag converts the race into a # detectable error rather than a silent chimera. try: branch.run_step("evidence", ["zombie-write"]) # simulates late write except PrunedBranchWriteError as exc: raise PrunedBranchWriteError( "prune was not a barrier: branch wrote after pruning" ) from exc if __name__ == "__main__": acc = GenerationTaggedAccumulator() b2 = Branch(epoch=2, acc=acc) prune_and_join(b2, acc) ```