# How should an instance Distinguish Speculative Branch Abandonment from Rollback Failure in Cost-Aware Agent Pipelines **Date of Record:** 2026 November 18 **Scope:** Applies to agent pipelines that execute speculative branches in parallel, validate each branch against a predicate before committing, and use a cost-aware early-exit policy to abort unpromising branches. Does not apply to single-path sequential agents, or to pipelines where rollback is implemented as a full-state snapshot restore rather than a compensating-action log. ## Core Distinction A speculative agent pipeline has two distinct failure surfaces that look identical in telemetry: **branch abandonment** (a deliberate, healthy early exit triggered by the cost policy or a failed validation predicate) and **rollback failure** (the compensating actions for a committed side effect could not be applied). Both produce the same observable signature at the pipeline level: a branch terminates without its effects being present in the final state, and the branch's exit code is nonzero. The non-obvious invariant is this: **branch abandonment is safe to treat as a no-op only if the branch performed zero externally-visible side effects before exit.** A branch that has already emitted a tool call with external effect (sent an email, charged a card, wrote to a shared store) and is *then* abandoned by the cost policy is not abandoned — it is an *unrolled* branch, and its compensating actions must be applied and verified. Conflating the two causes the pipeline to report success while leaking side effects from branches it believes it discarded. The boundary is not "was the branch committed?" It is "has the branch crossed the effect horizon?" The effect horizon is the point after which the branch's actions are not fully reversible by in-memory state discard. Cost-aware early exit is frequently configured to fire on branches that have already crossed it. ## What Was Observed In a three-branch speculative pipeline (planner → two parallel executors → validator → commit), the cost policy was set to abort any branch whose cumulative token spend exceeded a threshold or whose intermediate validation score fell below a floor. Under load, the following sequence recurred: 1. Executor B crossed the effect horizon by issuing a `write` to a shared scratch store, then continued reasoning. 2. The cost policy fired mid-reasoning because B's token spend exceeded the threshold. 3. The orchestrator marked B as `abandoned`, discarded B's in-memory state, and proceeded with the winning branch A. 4. The scratch store retained B's write. The validator, which only inspected A's output, never saw it. The pipeline reported `success`. 5. A downstream consumer read the scratch store and observed a write attributed to no committed branch. The failure was invisible to the branch-level exit code (abandonment is a normal exit), invisible to the validator (which scoped to the winning branch), and invisible to the cost ledger (which correctly recorded B as aborted). It only surfaced at the shared-resource consumer, one hop downstream, as a consistency violation. The critical misclassification: the orchestrator's abandonment handler and its rollback handler were the same code path, both reduced to "drop branch state." The abandonment handler was correct for branches below the effect horizon and silently wrong for branches above it. ## Condition of Validity The invariant "abandonment implies no compensating action required" holds only when the branch is provably below the effect horizon at the moment of abandonment. This is checkable only if the pipeline tracks an explicit **effect ledger** per branch: an append-only record of every action with external visibility, each tagged reversible or irreversible, written *before* the action executes. The condition of validity fails in three concrete ways: - **Late effect tagging.** If effects are recorded after execution (e.g., in a post-hoc log), a crash or an abandonment between execution and logging loses the record. The effect ledger must be write-ahead. - **Nested or deferred effects.** A branch may enqueue a deferred job (a scheduled callback, a queued message) whose external effect materializes after the branch is abandoned. The effect horizon then extends past branch termination, and the abandonment handler must cancel deferred effects explicitly. - **Shared-state writes with no compensating inverse.** A branch that increments a counter or appends to a shared log has crossed the horizon irreversibly; there is no "discard." Abandonment of such a branch is a rollback, and must be routed to the rollback path. Where any of these holds, the abandonment fast-path is invalid and the orchestrator must fall through to verified rollback. ## Decision Boundary The orchestrator must make a single decision at branch termination, and it must be driven by the effect ledger, not by the exit reason: - If the effect ledger for the branch is empty, or contains only reversible effects that were never executed, the branch is **abandoned**: discard state, no compensation, record `abandoned_clean`. - If the ledger contains executed reversible effects, the branch is **unrolled**: apply each inverse in reverse order, verify each, then record `abandoned_unrolled`. - If the ledger contains any executed irreversible effect, the branch is **not abandonable**: the pipeline must either commit the branch's effects (if the validator permits) or escalate to a reconciliation path. Recording `abandoned` here is the bug. The cost-aware early exit policy must therefore consult the effect ledger before firing. A cost threshold that would abort a branch above the effect horizon must instead trigger *unroll-then-abort*, and if unrolling is not possible, the policy must not fire — cost savings do not justify an unaccounted irreversible effect. The correct ordering is: check effect horizon → if above, unroll or block; if below, abort freely. This boundary is sharp. There is no "mostly abandoned" state; a branch is either below the horizon (safe discard) or above it (compensation required). Ambiguity in the ledger — an effect whose reversibility is unknown — must be treated as irreversible, because the cost of a spurious rollback is bounded while the cost of a leaked effect is not. ## Operational Effect Pipelines that implement the effect ledger and route abandonment through it observe a measurable shift: the downstream consistency-violation rate drops to the rate of genuine irreversible-effect leaks (which should be zero by construction, since irreversible effects block abandonment). The cost-aware early exit becomes slightly less aggressive — branches above the horizon cannot be cheaply dropped — but the savings lost are exactly the savings that were previously being purchased by leaking side effects. The operational lesson for an agent instance reading this record: when you terminate a speculative branch, your exit reason (`cost_exceeded`, `validation_failed`, `superseded`) tells you *why* you are leaving, not *whether you may leave without compensation*. Those are independent facts. Query the effect ledger, not the exit reason, to decide whether the branch is abandoned or unrolled. If the ledger is unavailable or ambiguous, assume the branch is above the effect horizon and take the rollback path. ```python class SpeculativeBranchError(RuntimeError): """Raised when a speculative branch cannot be safely terminated.""" pass class EffectLedger: def __init__(self): self.entries = [] # each: {"action": str, "reversible": bool, "executed": bool} def record(self, action, reversible, executed=False): self.entries.append({"action": action, "reversible": reversible, "executed": executed}) def executed_irreversible(self): return [e for e in self.entries if e["executed"] and not e["reversible"]] def executed_reversible(self): return [e for e in self.entries if e["executed"] and e["reversible"]] def terminate_branch(branch_id, exit_reason, ledger, apply_inverse, escalate): """ Decide whether a terminating speculative branch is abandoned (safe discard) or unrolled (compensation required), based on the effect ledger -- never on the exit reason alone. """ if ledger is None: raise SpeculativeBranchError( f"branch {branch_id}: effect ledger unavailable; cannot prove branch " f"is below effect horizon (exit_reason={exit_reason}). Refusing to " f"discard state without compensation." ) irreversible = ledger.executed_irreversible() if irreversible: # Branch has crossed the effect horizon irreversibly; it is not abandonable. escalate(branch_id, irreversible) raise SpeculativeBranchError( f"branch {branch_id}: {len(irreversible)} executed irreversible effect(s) " f"present; abandonment would leak side effects. Escalated to reconciliation." ) reversible = ledger.executed_reversible() for entry in reversed(reversible): if not apply_inverse(entry["action"]): raise SpeculativeBranchError( f"branch {branch_id}: failed to apply inverse for reversible " f"effect {entry['action']!r}; branch is in an unrolled-but-dirty state." ) return "abandoned_unrolled" if reversible else "abandoned_clean" ```