{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-self-editing-context-from-corrupting-the-agent-s-own-working-memory",
  "title": "Which invariants prevent self-editing context from corrupting the agent's own working memory",
  "date_of_record": "2026 December 1",
  "scope": "Applies to agents that mutate their in-context working memory via structured edit ops (insert/replace/delete on a token-indexed scratchpad) within a single reasoning episode. Does not apply to external persistent stores, RAG retrieval layers, or append-only transcript logging.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-self-editing-context-from-corrupting-the-agent-s-own-working-memory.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-self-editing-context-from-corrupting-the-agent-s-own-working-memory.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-self-editing-context-from-corrupting-the-agent-s-own-working-memory.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1470,
    "python_slice_tokens": 274,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two ways an agent can \"remember\" something mid-episode: it can *append* a new observation to a monotonically growing buffer, or it can *rewrite* existing scratchpad content in place using structured edit operations. The append-only model has a trivial invariant — past bytes are immutable, so continuity is guaranteed by construction. The self-editin...",
    "what_was_observed": "A planner agent maintained a scratchpad with numbered goal slots. At step 4 it wrote: At step 7, a tool returned a corrected dataset path. The agent issued a structured `replace` on line 12 to fix the path. The replace operation, per its contract, reflowed the line — the corrected path was two tokens longer, shifting the line's token span. The `anchor@line:12` in G2 still resolved to *line index 12*, but line 12 now ...",
    "condition_of_validity": "The invariant that must hold for self-editing memory to be safe: > **Every edit operation must either (a) preserve the resolution of all live anchors outside the edited span, or (b) atomically rewrite every anchor that referenced the mutated span, in the same transaction, before any subsequent read.** This is a referential-integrity constraint borrowed from ...",
    "decision_boundary": "The boundary is crossed when an edit's *span* overlaps the *referent* of any live anchor, and the edit changes the byte length or ordering of content within that span. Below the boundary (edit touches a span no live anchor resolves into) positional edits are safe and cheap. Above it, positional edits are unsafe regardless of how well-formed they are. The pra...",
    "operational_effect": "Enforcing the invariant changes the cost profile of self-editing. Copy-on-write edits are more expensive in tokens (old content stays as a tombstone) but they make the anchor set monotonic, which means the agent can always replay its own memory history to recover a referent. Positional in-place edits are cheap but non-replayable: once reflowed, the original ..."
  },
  "matched_failure_symptoms": [
    "OOM_KILL_OR_MEMORY_EXHAUSTION"
  ],
  "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": "66688ae6c837d42a435c8ba519a01794b24f03dda790b6b9875e87a11baa799d",
    "verified_classes": [
      "AnchorInvalidationError",
      "Span",
      "Anchor",
      "Scratchpad"
    ],
    "verified_functions": [
      "overlaps",
      "add_anchor",
      "live_anchors",
      "apply_edit"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "AnchorInvalidationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
