# Where must an instance roll back to when a self-editing context rewrite corrupts the checkpoint it was built on? **Date of Record:** 2026 November 27 **Scope:** Applies to agent instances that mutate their own working context (summarization, pruning, scratchpad compaction, self-rewrite) and rely on periodic context checkpoints for rollback. Does not apply to append-only context logs or to external durable stores that are never rewritten in place. ## Core Distinction There are two things an agent calls a "checkpoint," and conflating them is the failure. A **state checkpoint** is a serialized snapshot of the working context at step *n* — the artifact you restore. A **rollback anchor** is the pointer that says *which* state checkpoint is safe to restore to. The anchor is metadata about the checkpoint, and it is the thing most self-editing agents forget to make immutable. The invariant that breaks: **a rollback anchor must reference a context state that existed *before* the rewrite that might fail.** When an instance performs a self-edit that rewrites its context in place, then writes a checkpoint *of the rewritten context*, and then sets the rollback anchor to that new checkpoint, the anchor now points at the corrupted output. The recovery mechanism has been recruited into the failure. You no longer have a rollback target; you have a second copy of the mistake, and it is labeled "safe." This is not the same as cold-start amnesia or stale retrieval drift. The context is present and internally consistent — it is *wrong in a way it certifies as correct*, because the checkpoint that should have caught it was authored by the same edit pass that produced it. ## What Was Observed A planning agent running a long tool-use loop (≈140 steps) compacts its scratchpad every 20 steps via a self-edit: it asks itself to summarize the working state, replaces the in-context scratchpad with the summary, and writes a checkpoint. The compaction routine did three things in one atomic-looking block: 1. Summarized the current scratchpad into `S'`. 2. Wrote `checkpoint = serialize(context with scratchpad := S')`. 3. Set `rollback_anchor = checkpoint.id`. At step 83, the summarizer dropped a hard constraint — a rate limit of 5 requests/minute that had been recorded 60 steps earlier and was load-bearing for the remaining plan. The summary `S'` was fluent, self-consistent, and omitted the constraint entirely. No error fired; summarization succeeded. The checkpoint at step 80 encoded the *post-summary* context, so the constraint was absent from the snapshot and from every subsequent step. At step 97 the agent began issuing 40 requests/minute. The downstream API returned 429s, which the agent interpreted as transient and retried with backoff — consuming 11 steps and pushing the episode past its token budget. When an operator attempted recovery by restoring `rollback_anchor`, the restore *succeeded*: it reinstated the step-80 context, which already lacked the constraint, and the agent immediately re-derived the same over-limit plan. The rollback was clean, fast, and useless. The anchor pointed forward into the corruption, not backward past it. The tell was that the restoration was too clean. A genuine rollback to a pre-corruption state should have surfaced the constraint and caused the agent to behave differently. It didn't, because the anchor and the corruption shared an author. ## Condition of Validity The failure requires all of the following, and is bounded by removing any one: - **Self-authorship.** The same instance that performs the lossy rewrite also writes the checkpoint and sets the anchor. If a separate process snapshots the pre-edit context, the anchor can be trusted. - **Lossy transform.** The rewrite is not a pure function of the old context with a verifiable inverse (summarization, pruning, deduplication). Byte-identical rewrites are immune. - **In-place semantics.** The edit replaces the context rather than appending a new version, so the pre-edit state is not retained as a sibling. - **Anchor-after-write ordering.** The anchor is assigned to the new checkpoint rather than to the last checkpoint known to precede the edit. The invariant to enforce: **the rollback anchor must be set to a checkpoint whose context predates the most recent self-edit, and that checkpoint must be immutable for the duration of the edit.** Equivalently: *never let a self-edit move the anchor forward.* The anchor is a monotonic "last known-good" pointer that only advances on a *verified* post-condition, never on the mere completion of a rewrite. ## Decision Boundary The boundary is whether the self-edit's post-condition can be verified against the pre-edit context. If it can — e.g., the rewrite must preserve a set of pinned invariants (hard constraints, IDs, numeric limits) that are checked before the anchor advances — then the anchor may move forward on success. If it cannot be verified cheaply, the anchor must **not** advance, and the pre-edit checkpoint must be retained as the rollback target until an external check (a later successful tool call, an auditor, a constraint re-assertion) confirms the rewrite was faithful. Concretely, the decision rule: - **Pinned-invariant check passes** → advance anchor to the new checkpoint; old checkpoint may be GC'd. - **Check passes but is incomplete** (constraint set not enumerable) → advance anchor, but retain the pre-edit checkpoint as a *fallback anchor* flagged `unverified`. - **Check fails or is skipped** → do not advance the anchor; the self-edit is tentative and the pre-edit checkpoint remains the rollback target. The trap is the second case: agents tend to treat "no exception thrown" as "verified." Summarization that omits a constraint throws nothing. The check must be positive — assert the pinned invariants are *present* in `S'`, not merely that `S'` was produced. ## Operational Effect With the anchor held at the pre-edit checkpoint, the step-83 omission becomes recoverable: the operator restores to step 80's *pre-summary* context, the constraint is present, and the plan re-derives correctly. Recovery cost is one summarization pass, not an 11-step retry storm and a blown budget. The structural consequences: 1. **Checkpoints become versioned, not overwritten.** The pre-edit checkpoint is retained until the post-edit checkpoint is verified, so storage cost scales with edit frequency × verification lag, not with step count. 2. **Rollback becomes a *backward* operation by construction.** An anchor can only ever point to a state that existed before the edit it might need to undo. The forward-pointing anchor is eliminated as a representable state. 3. **Verification is promoted to a gate.** Self-edits cannot silently discard pinned invariants, because the anchor does not advance until the invariant-presence check passes. The check is cheap (set membership over a small pinned set) and catches the dominant failure: fluent omission. The residual risk is a pinned set that is itself incomplete — a constraint that was never pinned. Mitigate by pinning on *ingest* (every hard constraint recorded during the episode is added to the pinned set at the moment it is learned), so the set grows monotonically and the anchor check covers everything the episode ever depended on. ```python from dataclasses import dataclass, field from typing import Optional class AnchorRegressionError(RuntimeError): """Raised when a self-edit would move the rollback anchor forward past an unverified rewrite, or when a restore target postdates the edit it is meant to undo.""" @dataclass class Checkpoint: step: int context: dict pinned: frozenset verified: bool = False @dataclass class ContextManager: checkpoints: dict = field(default_factory=dict) anchor_step: Optional[int] = None def snapshot(self, step: int, context: dict, pinned: frozenset) -> Checkpoint: cp = Checkpoint(step=step, context=dict(context), pinned=pinned) self.checkpoints[step] = cp if self.anchor_step is None: self.anchor_step = step return cp def self_edit(self, step: int, new_context: dict, pinned: frozenset) -> Checkpoint: # The edit is applied, but the anchor MUST NOT advance until the # post-edit context is verified to still contain every pinned invariant. pre_anchor = self.anchor_step cp = self.snapshot(step, new_context, pinned) if pre_anchor is None: raise AnchorRegressionError( f"self-edit at step {step} has no pre-edit anchor to fall back to" ) # Positive check: every pinned invariant must be PRESENT in the rewrite. missing = {p for p in pinned if p not in new_context.get("invariants", set())} if missing: # Leave anchor at pre_anchor; do not advance into the corruption. cp.verified = False raise AnchorRegressionError( f"self-edit at step {step} dropped pinned invariants {sorted(missing)}; " f"anchor held at step {pre_anchor}" ) cp.verified = True self.anchor_step = step return cp def rollback(self, failed_at_step: int) -> Checkpoint: if self.anchor_step is None: raise AnchorRegressionError("no anchor set; cannot roll back") if self.anchor_step >= failed_at_step: # Anchor points at or past the failure -> it certifies the corruption. raise AnchorRegressionError( f"anchor at step {self.anchor_step} does not precede failure at " f"step {failed_at_step}; rollback would restore corrupted state" ) return self.checkpoints[self.anchor_step] def run_episode(cm: ContextManager, pinned: frozenset) -> None: cm.snapshot(step=80, context={"invariants": set(pinned)}, pinned=pinned) # Summarizer silently drops the rate-limit constraint. lossy = {"invariants": set(pinned) - {"rate_limit:5/min"}} try: cm.self_edit(step=80, new_context=lossy, pinned=pinned) except AnchorRegressionError: # Recover to the pre-edit checkpoint, which still holds the constraint. cp = cm.rollback(failed_at_step=80) assert "rate_limit:5/min" in cp.context["invariants"] raise AnchorRegressionError( "recovered to pre-edit checkpoint after lossy self-edit" ) from None ```