{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-rewrites-its-own-context-window-with-diff-based-patches-and-the-patch-anchor-has-drifted",
  "title": "What happens when an instance rewrites its own context window with diff-based patches and the patch anchor has drifted",
  "date_of_record": "2026 November 25",
  "scope": "Applies to agents that mutate their own working context via model-generated diff/patch operations (search-replace, unified diff, or line-anchored splice) where the patch is computed against a *remembered* or *summarized* view of the context rather than the byte-exact current buffer. Does not apply to append-only scratchpads, external vector memory, or patches applied by a deterministic non-LLM controller.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-rewrites-its-own-context-window-with-diff-based-patches-and-the-patch-anchor-has-drifted.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-rewrites-its-own-context-window-with-diff-based-patches-and-the-patch-anchor-has-drifted.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-rewrites-its-own-context-window-with-diff-based-patches-and-the-patch-anchor-has-drifted.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1960,
    "python_slice_tokens": 305,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two ways an agent can \"edit its own context.\" The first is **append-and-truncate**: the model emits new tokens, the runtime appends them, and eviction removes old tokens by policy. This is lossy but *monotone*—the buffer only ever grows at the tail and shrinks at the head, so every token still present is byte-identical to what was originally writte...",
    "what_was_observed": "A long-horizon coding agent maintained a working context containing a file tree, several tool outputs, and a running plan. At step 41 it decided to \"clean up\" a stale plan entry. The model emitted a search-replace patch whose search block was: But the live buffer, after two intermediate tool calls had inserted newline-delimited JSON results, actually contained: The model had reconstructed the search block from its *c...",
    "condition_of_validity": "Diff-based self-editing is sound only under a conjunction of conditions, and the failure above is the violation of exactly one of them: 1. **Anchor uniqueness.** The search block matches exactly one region of the live buffer. If it matches zero, the patch must be rejected (not fuzzed). If it matches more than one, the patch must be rejected (ambiguous). Fuzz...",
    "decision_boundary": "The operational decision is whether the patch applier is allowed to be *fuzzy* or must be *exact*, and what it does on mismatch. The correct boundary: - **Exact-match-or-reject.** The applier must require the search block to match exactly one contiguous span, byte-for-byte (or token-for-token under a pinned tokenizer). Zero matches → reject with a structured...",
    "operational_effect": "The consequence of getting this wrong is **silent, self-consistent corruption**. Unlike a rejected patch (which the model sees and can recover from) or a crash (which the runtime sees), a fuzzily-applied patch produces a buffer that is internally coherent and plausible. The model's subsequent reasoning is built on a context that no longer matches reality, an..."
  },
  "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": "4a82a3c7e88d639ea5dcaa112a7ca08e6ce9b5b805a570e89d92f7a06daa9216",
    "verified_classes": [
      "PatchAnchorError",
      "Patch"
    ],
    "verified_functions": [
      "apply_self_patch"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 5,
    "raised_exceptions": [
      "PatchAnchorError"
    ],
    "sample_fail_closed_invariants": [
      "post-apply length invariant violated; buffer corrupted",
      "replacement block collides with existing buffer content"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "re"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
