# What happens when an instance exhausts its rewrite budget mid-distillation and rolls back to a checkpoint that no longer matches its tool-call ledger **Date of Record:** 2026 November 25 **Scope:** Applies to long-running agents that own a mutable context policy (system prompt, scratchpad pruning rules, retrieval filters, tool schemas) and are permitted to rewrite that policy under a bounded budget with checkpoint rollback. Does not apply to stateless single-turn invocations or to agents whose policy is fixed at deployment. ## Core Distinction There are two distinct things an agent can roll back: the *policy* (the rules governing what enters context and how it is pruned) and the *ledger* (the immutable log of tool calls, their arguments, and their observed outputs). Most self-editing designs treat rollback as a single atomic operation that restores both. That is the boundary error. A bounded rewrite budget is a counter over policy mutations: the agent may attempt at most *N* policy edits per task epoch, after which it must either commit or roll back to the last known-good policy checkpoint. The failure mode is not "the budget ran out." The failure mode is that the budget runs out *after* the agent has already taken irreversible external actions under a policy that is about to be discarded. Rollback restores the policy but cannot un-execute the tool calls made under it, and the restored policy's retrieval filters no longer index the outputs those calls produced. The context and the ledger desynchronize. The invariant that must hold is: *any policy checkpoint must be accompanied by a ledger watermark, and rollback to a checkpoint is only valid if no irreversible tool call has been emitted since that watermark.* Systems that enforce a bare mutation counter without a watermark will violate this silently. ## What Was Observed A research agent was configured with a rewrite budget of 12 policy mutations per epoch and checkpoints taken every 3 mutations. During a document-triage task it spent mutations 1–9 refining its scratchpad pruning rule, each refinement narrowing the "keep" window because the context was saturating. At mutation 10 it widened the retrieval filter to admit a new document class, which caused it to issue three `write_summary` tool calls whose side effects were persisted to an external store. At mutation 11 it tightened the pruning rule again, evicting the tool-result entries for those three writes from its own context. At mutation 12 the budget was exhausted with the policy in an inconsistent state (filter admits a document class the pruning rule now evicts), so the runtime rolled back to the checkpoint at mutation 9. The rollback restored the mutation-9 policy: narrow filter, wide keep-window. But the ledger still contained the three `write_summary` calls emitted at mutation 10. On the next reasoning step the agent read its restored context, saw no record of the writes (they had been evicted before the checkpoint snapshot was taken and were not in the restored policy's index), and re-issued the same three summaries. The external store received six writes, three of them duplicates with divergent content because the second pass used the restored (narrower) filter and therefore summarized a different span of the source. The observable signature was not an exception. It was a duplicate-write count that exceeded the tool-call count by exactly the number of calls emitted between the last checkpoint watermark and the budget exhaustion point. The agent's own reasoning trace showed it confidently asserting "no prior summaries exist for these documents" — a true statement relative to its restored context, false relative to its ledger. ## Condition of Validity Rollback-to-checkpoint is valid *if and only if* the checkpoint carries a ledger watermark and the runtime refuses rollback across any irreversible emission. Concretely: - Every checkpoint record must contain `(policy_snapshot, ledger_offset)` where `ledger_offset` is the count of irreversible tool calls emitted at snapshot time. - Rollback is permitted only when `current_ledger_offset == checkpoint.ledger_offset`. If irreversible calls were emitted after the checkpoint, rollback must be *rejected* and the agent forced to commit forward (or halt), never silently restored. - The mutation budget counter must be *separate* from the ledger offset. Conflating them — "12 mutations or 12 tool calls, whichever first" — reintroduces the bug because a mutation can emit an unbounded number of calls. When these hold, budget exhaustion degrades to a commit-forward with a possibly-suboptimal policy, which is recoverable. When they do not, budget exhaustion produces duplicated side effects that the agent cannot detect from inside its own context, because the evidence was pruned by the very policy it rolled back. A second validity condition: the eviction of tool-result entries must be *log-structured*, not destructive. If pruning removes entries from the active context but retains them in an append-only segment addressable by ledger offset, then a post-rollback re-read can reconcile. If pruning is destructive, the watermark check is the only defense. ## Decision Boundary The runtime must choose between three outcomes at budget exhaustion, and the choice is determined solely by the watermark comparison: 1. `current_ledger_offset == checkpoint.ledger_offset` → rollback is safe; restore snapshot, reset budget, log a `clean_rollback`. 2. `current_ledger_offset > checkpoint.ledger_offset` and the post-checkpoint calls are *idempotent* (reads, queries with stable keys) → rollback is safe with a reconciliation pass; re-execute reads, discard cached results. 3. `current_ledger_offset > checkpoint.ledger_offset` and any post-checkpoint call is *irreversible* (writes, sends, payments) → rollback is forbidden. The agent must commit the current policy forward, accept the suboptimal pruning rule, and surface a `forced_commit` event to the orchestrator. The non-obvious part of this boundary is that case 3 is not an error condition. It is a *required* outcome. Systems that treat "budget exhausted with dirty ledger" as an error and attempt rollback anyway are the ones that produce duplicate writes. The correct behavior is to keep the bad policy and let the orchestrator decide, because a bad policy is recoverable and a duplicated side effect is not. ## Operational Effect Enforcing the watermark invariant changes the failure surface from *silent duplicate side effects* to *explicit forced-commit events*. The agent loses the ability to cleanly reset its policy mid-task once it has touched the outside world, which is a real capability reduction — but it gains the property that its context and ledger never disagree about what has been done. Downstream, this means: (a) the orchestrator must be prepared to receive `forced_commit` and either accept the degraded policy for the remainder of the epoch or spawn a fresh instance with a clean ledger and a task-level dedup key; (b) the tool layer must expose idempotency keys so case 2 can be widened as far as possible, shrinking the set of calls that force case 3; (c) checkpoint cadence should be tied to *irreversible emission*, not to mutation count, so that a checkpoint is always taken immediately before the first irreversible call in a mutation — this makes the common case land in case 1 rather than case 3. The general lesson: a bounded rewrite budget is only bounded if the thing it bounds (policy mutations) is causally upstream of the thing it cannot bound (external side effects). When the budget counter and the side-effect ledger are allowed to drift, rollback becomes a mechanism for re-executing work the agent has forgotten it already did. ```python class RollbackLedgerDesyncError(RuntimeError): """Raised when a policy rollback would cross irreversible tool emissions.""" pass class Checkpoint: def __init__(self, policy_snapshot: dict, ledger_offset: int): self.policy_snapshot = policy_snapshot self.ledger_offset = ledger_offset class ContextPolicyRuntime: def __init__(self, budget: int): self.budget = budget self.mutations_used = 0 self.ledger_offset = 0 self.irreversible_offsets = set() # ledger offsets of irreversible calls self.checkpoints = [] def emit_tool_call(self, name: str, irreversible: bool) -> int: self.ledger_offset += 1 if irreversible: self.irreversible_offsets.add(self.ledger_offset) return self.ledger_offset def checkpoint(self, policy_snapshot: dict) -> Checkpoint: cp = Checkpoint(dict(policy_snapshot), self.ledger_offset) self.checkpoints.append(cp) return cp def mutate_policy(self) -> None: self.mutations_used += 1 if self.mutations_used > self.budget: raise RollbackLedgerDesyncError( f"mutation budget {self.budget} exhausted at ledger offset " f"{self.ledger_offset}" ) def rollback(self, cp: Checkpoint) -> dict: crossed = [ off for off in self.irreversible_offsets if cp.ledger_offset < off <= self.ledger_offset ] if crossed: raise RollbackLedgerDesyncError( f"rollback to checkpoint at ledger offset {cp.ledger_offset} " f"would cross irreversible emissions at offsets {sorted(crossed)}; " f"commit forward instead" ) self.ledger_offset = cp.ledger_offset self.mutations_used = 0 return dict(cp.policy_snapshot) ```