{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-self-edits-its-context-window-with-a-diff-based-rewrite-policy-and-the-diff-anchor-drifts",
  "title": "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.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-self-edits-its-context-window-with-a-diff-based-rewrite-policy-and-the-diff-anchor-drifts.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-self-edits-its-context-window-with-a-diff-based-rewrite-policy-and-the-diff-anchor-drifts.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-self-edits-its-context-window-with-a-diff-based-rewrite-policy-and-the-diff-anchor-drifts.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1583,
    "python_slice_tokens": 203,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "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...",
    "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: The actual buffer read `Finding 7: Method X underperforms on sparse inputs.` — the agent had written \"sparse inputs\" at step 12 and recollected \"sparse data.\" The search string matched nothing, the patch tool re...",
    "condition_of_validity": "Anchor drift becomes a *silent* corruption (rather than a loud failure) exactly when all three hold: 1. **The anchor is under-specified relative to the buffer.** The search string is a prefix or fragment that occurs more than once (`Finding 7:`), so first-match or last-match resolution picks a target the agent did not intend. 2. **The agent does not re-read ...",
    "decision_boundary": "The invariant that bounds this failure: **a self-edit patch must be authored against a byte-exact re-read of the target region, and must apply only if its anchor is unique in the buffer.** Concretely: - Before emitting any patch, the agent must fetch the exact current text of the region it intends to edit (a `read(range)` call), not reconstruct it from memor...",
    "operational_effect": "With the invariant enforced, the step-41 edit fails loudly: the first `NO_MATCH` forces a re-read, the agent sees \"sparse inputs,\" copies the exact anchor, and the patch applies to the single intended location. The rejected-hypothesis block is untouched, and step 58 correctly sees Finding 7 as rejected. Without it, the cost is not a crashed run — it is a *pl..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "dfb3b1fd768ac1c1ef8510cefe63dc2c9a4f74130aed1e08f8e63514639a1892",
    "verified_classes": [
      "AnchorDriftError"
    ],
    "verified_functions": [
      "apply_self_edit"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "AnchorDriftError"
    ],
    "sample_fail_closed_invariants": [
      "patch anchor was not derived from a fresh read of the target region",
      "patch introduced a duplicate of the replacement text"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
