# Which invariants prevent speculative agent rollback from corrupting externally-committed side effects **Date of Record:** 2026 November 27 **Scope:** Applies to pipelines where a planner agent speculatively forks downstream agents along predicted branches and rolls back losing branches. Does not apply to purely read-only agent graphs, or to pipelines where every tool call is idempotent and side-effect-free by construction. ## Core Distinction Speculative execution across an agent pipeline differs from CPU speculative execution in one fatal way: the "register file" being speculated over is not internal. A downstream agent's tool call—`send_email`, `charge_card`, `git_push`, `POST /refund`—commits to the outside world the instant it returns, long before the planner decides whether that branch was the winning prediction. Rollback can undo an agent's *context* and *local state*, but it cannot undo an external commit. The boundary condition is therefore not "can we roll back the branch?" but "which external effects did the branch already leak before rollback, and does the winning branch's replay observe or collide with them?" The non-obvious failure: rollback is *contextually* clean but *causally* dirty. The agent's memory is restored to the fork point, the trace looks pristine, and yet the world has moved. The next branch's agent, reasoning from the restored context, re-derives a plan that assumes the world is at fork-point state—and issues a second, now-duplicate or now-invalid commit. ## What Was Observed A three-stage pipeline: `planner → [speculative] researcher / writer → committer`. The planner forks two writer branches, `W_alpha` and `W_beta`, predicted from a partial outline. `W_alpha` wins the planner's confidence vote; `W_beta` is rolled back. During `W_beta`'s execution, before the vote, its agent called `create_draft_branch(repo, name="beta-")` and `post_review_comment(pr, body=...)`. Both returned 200. The rollback routine restored `W_beta`'s context window to the fork snapshot and marked the branch `ROLLED_BACK`. The trace was consistent. No error surfaced. The winning branch `W_alpha` then executed `create_draft_branch(repo, name="alpha-")` and `post_review_comment(pr, ...)`. On the *second* planner cycle (a re-plan triggered by a downstream validation failure), the planner re-forked from the *original* fork snapshot—not from `W_alpha`'s completed state—because the checkpoint pointer had never advanced past the fork. The re-forked `W_alpha'` re-issued `create_draft_branch(repo, name="alpha-")`. The remote returned `422 Reference already exists`. The agent interpreted the 422 as a transient conflict, retried with a `-v2` suffix, and the pipeline committed *two* draft branches and *three* review comments for a single logical task. The observable signature was not an exception. It was a **divergence between the pipeline's internal ledger (one branch, one comment) and the external system's ledger (two branches, three comments)**. The rollback invariant held locally and failed globally. ## Condition of Validity The invariant that must hold for speculative agent rollback to be safe is a **commit barrier on external effects**, stated precisely: > A branch may not perform an external, non-idempotent effect until it has been promoted from `SPECULATIVE` to `COMMITTED`. Any effect issued while `SPECULATIVE` must be either (a) buffered in a branch-scoped outbox that is discarded on rollback, or (b) executed against a branch-scoped namespace that rollback deletes. This is the agent-layer analogue of a store buffer with a commit gate, and it is *not* the same as transactionality. Transactions assume a coordinator that can abort; agents issue calls to third-party APIs that have no abort. The only enforceable form is the outbox or the namespace, and the invariant is violated the moment any speculative branch reaches a tool whose contract is "commit now, no undo." The condition of validity also requires that the **checkpoint pointer advance monotonically on commit**. In the observed failure, the pointer stuck at the fork because promotion never rewrote it; re-planning therefore replayed from a stale snapshot. A commit that does not advance the resume pointer is a silent re-execution hazard independent of the rollback logic itself. ## Decision Boundary The boundary is drawn at the tool-call boundary, not the agent boundary. For each tool in the pipeline's registry, classify: - **Buffered-safe:** effect can be queued in a branch outbox and flushed only on promotion (e.g., `create_draft_branch`, `post_review_comment`, `enqueue_job`). - **Namespace-safe:** effect is scoped to a branch ID that rollback garbage-collects (e.g., writes to `scratch//`). - **Unbufferable:** effect commits immediately and cannot be undone or namespaced (e.g., `charge_card`, `send_email`, `delete_resource`). Speculation is permitted only when every tool reachable on the speculative path is buffered-safe or namespace-safe. If any reachable tool is unbufferable, the planner must either (a) not speculate past that tool, or (b) insert a **promotion gate** before it: the branch must win the vote *before* the unbufferable call is issued, converting the speculative path into a serial path at that point. The decision is per-reachable-tool, computed at fork time by static reachability over the tool graph, not discovered at call time. A second boundary: promotion must be **idempotent and pointer-advancing**. Promotion writes the winning branch's outbox, advances the checkpoint pointer to the post-commit state, and records a promotion token. Re-planning from a committed state must resume from the advanced pointer, never the fork. ## Operational Effect With the commit barrier in place, the observable behavior changes in three ways. First, losing branches leave zero external residue: their outboxes are discarded, their namespaces collected, and the external ledger matches the internal ledger exactly. Second, re-planning is safe because the resume pointer only ever points at committed state, so replay re-issues *no* already-committed effect. Third, unbufferable tools force an explicit serialization point, which surfaces the cost of speculation honestly—the planner learns which branches are cheap to fork and which must be gated, instead of discovering it through duplicate commits in production. The residual risk is outbox flush failure at promotion: promotion is itself an external effect and must be idempotent (a promotion token checked by the flush target). An outbox flushed twice yields the same single commit; an outbox flushed zero times yields a committed branch with no external effect, which is detectable and recoverable, unlike the reverse. ```python class SpeculativeCommitViolation(RuntimeError): """Raised when a speculative branch attempts an effect that cannot be rolled back.""" pass UNBUFFERABLE = {"charge_card", "send_email", "delete_resource"} BUFFERABLE = {"create_draft_branch", "post_review_comment", "enqueue_job"} def assert_speculation_safe(reachable_tools: set[str], branch_state: str) -> None: if branch_state != "SPECULATIVE": return offenders = reachable_tools & UNBUFFERABLE if offenders: raise SpeculativeCommitViolation( f"speculative branch reaches unbufferable tools {sorted(offenders)}; " f"insert a promotion gate before these calls or disable speculation" ) unknown = reachable_tools - BUFFERABLE - UNBUFFERABLE if unknown: raise SpeculativeCommitViolation( f"unclassified tools {sorted(unknown)} on speculative path; " f"classify as buffered-safe, namespace-safe, or unbufferable before forking" ) def promote_branch(branch, resume_pointer, promotion_token: str) -> None: if branch.state != "SPECULATIVE": raise SpeculativeCommitViolation(f"cannot promote branch in state {branch.state}") if branch.promotion_token == promotion_token: return # idempotent promotion for effect in branch.outbox: effect.flush(idempotency_key=promotion_token) branch.promotion_token = promotion_token branch.state = "COMMITTED" resume_pointer.advance_to(branch.post_commit_snapshot) ```