{
  "schema_version": "1.3.0",
  "slug": "why-does-an-instance-corrupt-its-own-context-when-a-diff-based-self-edit-patch-applies-cleanly-but-anchors-to-a-stale-pre-image",
  "title": "Why does an instance corrupt its own context when a diff-based self-edit patch applies cleanly but anchors to a stale pre-image?",
  "date_of_record": "2026 November 25",
  "scope": "Applies to single-agent systems that mutate their own working memory via textual/structural diff patches (unified diff, JSON-Patch, RFC 6902, or custom `op: replace` grammars) with an accompanying rollback log. Does not apply to append-only scratchpads, external vector stores, or human-mediated edits, where no in-context pre-image is re-applied.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/why-does-an-instance-corrupt-its-own-context-when-a-diff-based-self-edit-patch-applies-cleanly-but-anchors-to-a-stale-pre-image.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-corrupt-its-own-context-when-a-diff-based-self-edit-patch-applies-cleanly-but-anchors-to-a-stale-pre-image.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/why-does-an-instance-corrupt-its-own-context-when-a-diff-based-self-edit-patch-applies-cleanly-but-anchors-to-a-stale-pre-image.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2187,
    "python_slice_tokens": 384,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"the model wrote a bad patch.\" A syntactically valid, hunk-clean patch that applies without conflict is the *most dangerous* case, because the patch engine reports success. The distinction that matters is between **applicability** and **semantic anchoring**: a diff is applicable if its context lines match the target region byte-for-byte; i...",
    "what_was_observed": "A representative trace, single agent, three self-edits in one reasoning episode: 1. **Turn 4.** Agent reads `MEM[plan]`, which contains a six-step task list. It patches step 3: `replace \"3. call search_api(q)\" with \"3. call search_api(q, limit=50)\"`. Context lines are steps 2 and 4. Patch applies. `MEM[plan]` now has the modified step 3. Rollback log entry `R1` records the inverse hunk. 2. **Turn 7.** A tool result a...",
    "condition_of_validity": "The invariant that prevents this is: *every self-edit hunk must be bound to a content hash of the exact pre-image region it was authored against, and the apply step must reject any hunk whose recorded pre-image hash does not equal the hash of the region the hunk is about to overwrite.* Formally, for a hunk `H` with recorded pre-image `P_H` and target region ...",
    "decision_boundary": "The boundary is: **does the runtime distinguish \"the model's belief about the pre-image\" from \"the runtime's record of the pre-image\"?** - **Below the boundary** (safe): the runtime owns the pre-image. When the model wants to edit region `R`, the runtime hands the model a *token* or *hash* for `R`'s current bytes, the model returns `(token, new_bytes)`, and ...",
    "operational_effect": "When the invariant is violated, the observable consequences compound across the episode: 1. **Silent state corruption.** The patch applies; the model's next read returns the corrupted region; the model has no signal that its belief and the bytes diverged. It proceeds to plan and execute on a plan it did not author. 2. **Rollback log poisoning.** The rollback..."
  },
  "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": "314f074738d33f92d5734270467bc774a1881870af02881e31e1ff64d399f8d9",
    "verified_classes": [
      "StalePreImageError",
      "Hunk",
      "SelfEditContext"
    ],
    "verified_functions": [
      "_hash",
      "read",
      "apply",
      "rollback",
      "compact"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "StalePreImageError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "hashlib",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
