# What happens when an instance self-edits its context window with a diff-based rewrite policy and the diff anchor drifts **Date of Record:** 2026 November 26 **Scope:** Applies to single-agent loops that maintain long-horizon working state by emitting structured edit operations (search/replace, unified-diff, or line-range patches) against their own prior context rather than appending. Does not apply to append-only context growth, external scratchpads, or vector-store retrieval. ## Core Distinction There are two ways an agent "remembers" across a long task: **accumulation** (append new tokens, let the window fill) and **revision** (emit a diff that mutates prior context in place). Revision is attractive because it bounds token cost — a 4,000-token tool transcript collapses to a 200-token summary in one patch. But revision introduces a failure mode that accumulation cannot have: **the diff is authored against a mental model of the context, not the context itself.** The agent generates the search anchor (the "old" side of the patch) from its own compressed recollection of what it wrote earlier, not from a byte-exact read of the buffer. When the anchor and the actual buffer diverge — by a whitespace, a reordered line, a paraphrase the agent *thinks* it wrote — the patch either fails to apply or, worse, applies at the wrong offset and silently corrupts an adjacent region. This is **anchor drift**: the edit succeeds mechanically while the semantic target has moved. It is distinct from a plain apply failure, because a corrupted-but-applied patch leaves the agent with a context window that is internally consistent and confidently wrong. ## What Was Observed A research agent running a 90-step literature-synthesis task used a search/replace self-edit policy to keep its working notes under 30k tokens. At step 41 it decided to update a claim in its "findings" block: ``` << str: if not reread_verified: # Patch authored from recollection, not a byte-exact read of the buffer. raise AnchorDriftError( "patch anchor was not derived from a fresh read of the target region" ) occurrences = buffer.count(search) if occurrences == 0: # Do NOT relax the anchor. Force a re-read to re-ground the model. raise AnchorDriftError( f"NO_MATCH for anchor {search!r}; re-read the region before retrying" ) if occurrences > 1: # Positional resolution here is exactly how silent retargeting happens. raise AnchorDriftError( f"AMBIGUOUS_ANCHOR: {search!r} occurs {occurrences} times; " "widen the anchor with surrounding unique context" ) patched = buffer.replace(search, replace, 1) # Post-condition: the mutation must be local; neighbor regions unchanged. if patched.count(replace) != 1: raise AnchorDriftError("patch introduced a duplicate of the replacement text") return patched ```