# Which invariants prevent cost-aware speculative agents from committing side effects under consensus-gated budgets **Date of Record:** 2026 November 17 **Scope:** Applies to agent runtimes where a planner speculatively expands multiple branches under a per-step cost budget and a consensus gate authorizes external side effects (writes, API calls, irreversible transactions). Does not apply to single-branch deterministic execution or to systems where side effects are idempotent and freely retried. ## Core Distinction A speculative agent has two distinct notions of "commit": *epistemic commit* (the planner selects a branch as the basis for further reasoning) and *operational commit* (a side effect is emitted to the external world). Cost-aware branch budgeting couples these two incorrectly if the budget is treated as a resource that, once spent, *entitles* the branch to commit. The invariant that must hold is orthogonal: **budget exhaustion is a reason to stop speculating, never a reason to commit**. Consensus gating is a separate predicate over branch *content*, evaluated independently of how much budget a branch consumed. The non-obvious failure is that these two predicates are usually implemented in the same scheduler loop, and cost accounting naturally produces a "winner" (the branch that scored best per unit cost). If the consensus gate is then queried only for that winner — rather than for the full surviving frontier — the scheduler has silently converted a *ranking heuristic* into an *authorization*. The agent commits the cheapest plausible branch instead of the consensually endorsed one. ## What Was Observed In a multi-branch planner with a token-and-tool-call budget, the following sequence produced an unauthorized side effect: 1. Three branches were expanded; branch B scored highest on expected utility per budget unit. 2. The budget was exhausted mid-expansion. The scheduler short-circuited, promoting B to "candidate." 3. The consensus gate was invoked with a *single-element* ballot `[B]`. A gate that requires agreement across ≥2 independent evaluators trivially satisfies on a one-element ballot (vacuously unanimous, or `max` over one score). 4. B committed a non-idempotent write. The gate was correct in isolation. The *caller* violated the gate's precondition: consensus requires a population. The budget logic starved the population to size one before the gate ran. A second observed failure: branches that were pruned for cost were still present in the side-effect queue because pruning removed them from the *scoring* set but not from the *commit* set. The two sets had diverged. ## Condition of Validity The invariant holds only when all of the following are true simultaneously: - The consensus gate is a function of the *frontier* (all branches that reached the gate), not of the scheduler's selected branch. - Budget exhaustion triggers *abort-and-report*, never *promote-and-commit*. - The pruned set and the committed set are disjoint by construction, not by convention. - The gate's quorum threshold is checked against the *cardinality of the ballot actually presented*, and a ballot smaller than quorum is a hard error, not a pass. If any of these is relaxed — for example, if a "fast path" allows single-branch commit under a latency budget — the invariant no longer protects you and the failure mode returns. ## Decision Boundary Given a branch `b` at the moment the scheduler wants to commit: - If `b` was pruned for cost → it must not be in the commit set. Reject. - If `len(ballot) < quorum` → the gate cannot authorize. Raise, do not default to pass. - If budget is exhausted and `b` is the sole survivor → this is *not* consensus; it is a singleton. Treat as `InsufficientBallotError`, not `Approved`. - If the gate returns approval but the ballot was assembled from the scoring set rather than the frontier set → reject; the sets must be reconciled before commit. The boundary is: **commit is authorized by consensus over a population, and cost only ever shrinks the population, so cost can only ever make commit harder — never easier.** ## Operational Effect Enforcing the invariant converts a silent authorization bug into a loud, recoverable abort. The agent stops, reports an insufficient ballot, and the orchestrator can either widen the budget to re-expand the frontier or escalate to a human. The cost is occasional false aborts when a genuinely single viable branch exists; the benefit is that no side effect is ever committed on the strength of a budget heuristic masquerading as agreement. Log the ballot cardinality alongside every commit decision — that single field makes the divergence between scoring set and commit set auditable after the fact. ```python from dataclasses import dataclass, field from typing import Sequence class InsufficientBallotError(RuntimeError): """Raised when a consensus gate is asked to authorize a commit over a ballot smaller than its quorum. Cost pruning must never be allowed to shrink the ballot below quorum and then pass.""" class PrunedBranchCommitError(RuntimeError): """Raised when a branch pruned for cost appears in the commit set.""" @dataclass class Branch: id: str score: float pruned_for_cost: bool = False @dataclass class ConsensusGate: quorum: int def authorize(self, ballot: Sequence[Branch]) -> bool: if len(ballot) < self.quorum: raise InsufficientBallotError( f"ballot size {len(ballot)} < quorum {self.quorum}; " "budget pruning cannot substitute for consensus" ) return all(b.score > 0 for b in ballot) @dataclass class Scheduler: gate: ConsensusGate budget_remaining: int frontier: list[Branch] = field(default_factory=list) def try_commit(self, candidate: Branch) -> bool: if candidate.pruned_for_cost: raise PrunedBranchCommitError( f"branch {candidate.id} was pruned for cost and must not commit" ) if self.budget_remaining <= 0 and len(self.frontier) < self.gate.quorum: raise InsufficientBallotError( f"budget exhausted with frontier size {len(self.frontier)} " f"< quorum {self.gate.quorum}; abort, do not promote" ) return self.gate.authorize(self.frontier) # Example: budget exhaustion collapses the frontier to a singleton. gate = ConsensusGate(quorum=2) sched = Scheduler(gate=gate, budget_remaining=0, frontier=[Branch("B", 0.9)]) sched.try_commit(Branch("B", 0.9)) # raises InsufficientBallotError ```