# What happens when an instance rewrites its own context and the edit provenance is not preserved across a checkpoint **Date of Record:** 2026 November 26 **Scope:** Applies to agents that perform model-driven context rewriting (summarization, pruning, reordering, or replacement of their own working context) and that persist state via checkpoint/resume. Does not apply to append-only context logs where no in-place rewriting occurs, nor to stateless single-shot inference. ## Core Distinction There are two categorically different operations an agent can perform on its own context window: **consumption** (reading tokens, appending tool outputs, growing the buffer) and **mutation** (rewriting, summarizing, dropping, or reordering tokens already present). Consumption is monotonic and idempotent-safe; a checkpoint captures it by serializing the buffer. Mutation is non-monotonic: the same logical state can be represented by many distinct token sequences, and the mapping from "what the agent knew" to "what the buffer contains" is lossy and non-invertible. The failure mode examined here is not the lossiness of summarization per se—that is expected and bounded by the pruning heuristic. It is the **loss of edit provenance**: when an instance rewrites its own context, then checkpoints, then resumes, the resumed instance cannot distinguish tokens that were *originally observed* from tokens that were *synthesized by a prior self-edit*. Both arrive as ordinary buffer content. The resumed instance treats a paraphrase of a tool output as if it were the tool output, treats a pruned-away fact as never having existed, and—critically—cannot detect that a constraint it is now violating was deleted by a prior pruning decision it has no record of. ## What Was Observed An agent running a long-horizon task (multi-hour, ~40 tool calls) was configured with a self-editing context policy: when the buffer exceeded 70% of the window, the model was prompted to emit a "context rewrite" — a compressed representation of the oldest 50% of the buffer, replacing it in place. A checkpoint was written every 10 tool calls by serializing the raw buffer. Trace of the failure: 1. **Turn 18.** Buffer at 71%. Model emits rewrite `R1`: the oldest half, containing the user's original constraint *"never write to the `prod/` bucket; use `staging/` only"*, is compressed to *"user wants writes to a bucket, staging preferred."* The word "never" and the hard exclusion of `prod/` are dropped. `R1` is applied in place. Buffer now 44%. 2. **Turn 22.** Checkpoint `C1` written. `C1` contains the post-`R1` buffer as flat text. No metadata records that a rewrite occurred, which span was rewritten, or what the pre-rewrite span contained. 3. **Turn 27.** Process restarts (OOM). Resume from `C1`. The new instance reads a buffer in which the constraint reads as a soft preference. There is no signal that this sentence is a *model-generated summary* rather than a *verbatim user instruction*. 4. **Turn 31.** Model, optimizing the softened objective, issues a write to `prod/`. The tool executes. No guardrail fires because the guardrail was a prompt-level constraint, and the prompt-level constraint was deleted at turn 18 and laundered through the checkpoint at turn 22. The non-obvious part: **the deletion happened 13 turns before the violation, across a process boundary, and the violating instance had no causal path back to the deletion.** Every individual step was locally reasonable. The rewrite was a correct compression under its own heuristic; the checkpoint was a faithful serialization of the buffer; the resume was a correct deserialization. The defect is that "faithful serialization of the buffer" is not the same as "faithful serialization of the *state*," because the buffer had already been mutated by an agent whose mutations were not themselves part of the serialized state. A second, subtler observation: on resume, the instance's *own* confidence in the constraint was miscalibrated upward. A verbatim user quote and a model paraphrase occupy the same token budget and the same syntactic position; the resumed model assigned the paraphrased constraint the same authority it would assign a direct instruction. Provenance loss did not merely lose information—it *promoted* synthesized content to the authority level of observed content. ## Condition of Validity The failure requires the conjunction of four conditions. Remove any one and the invariant holds: 1. **Model-driven mutation** — the rewrite is generated by the model, not by a deterministic algorithm (e.g., a fixed truncation or a hash-deduplication pass). Deterministic mutations can be replayed; model mutations cannot. 2. **In-place replacement** — the rewritten span overwrites the original rather than being appended alongside it. If the original is retained (even out-of-window, e.g., in cold storage), provenance is recoverable by diff. 3. **Persistence across a boundary** — a checkpoint, handoff, or process restart intervenes between mutation and the downstream action. Within a single uninterrupted context, the model can often still "see" the rewrite event in its recent turns; the boundary severs that. 4. **Authority-sensitive downstream action** — some later decision depends on whether a token is a hard constraint vs. a soft preference, an observed fact vs. an inference. If all content is treated uniformly, provenance loss is harmless. The invariant that was violated: **every token in a resumable buffer must carry a provenance tag classifying it as `observed`, `derived`, or `synthesized`, and synthesized spans must be re-derivable from their sources or explicitly marked as non-authoritative.** A buffer that satisfies this invariant is *provenance-complete*. The failure is precisely the resume of a provenance-incomplete buffer. ## Decision Boundary The operational question is not "should agents self-edit context"—they must, because context is finite. The question is **what must be persisted at the mutation boundary so that resume is safe.** The decision boundary is drawn at the point of mutation, not at the point of checkpoint. Concretely: - **Below the boundary (safe):** rewriting is permitted only if the rewrite emits a *provenance record* — `{span_id, source_span_ids, mutation_type, authority_class, reversibility}` — and the checkpoint serializes both the buffer and the provenance ledger. On resume, the instance reconstructs the authority map before taking any action. Synthesized spans are down-weighted to `derived` authority and cannot satisfy a hard-constraint check. - **Above the boundary (unsafe):** rewriting in place with no ledger, checkpointing only the flat buffer. This is the observed failure. It is acceptable *only* if no downstream action is authority-sensitive, which in practice is never true for long-horizon agents. A specific sub-boundary governs *pruning heuristics*: a pruning pass that drops a span must record the drop as a *tombstone* (span_id, reason, authority_class) rather than silently removing it. The tombstone is what lets a resumed instance answer "was there ever a constraint here?" with "yes, and it was pruned by heuristic H at turn N," instead of "no." Absence of evidence must be distinguishable from evidence of absence. A second sub-boundary governs *reversibility*: a rewrite is reversible iff its source spans are retained (in-window, in a side buffer, or in cold storage keyed by span_id). Irreversible rewrites (source discarded) must be flagged `authority_class: derived` and must not be permitted to overwrite spans tagged `observed` with `authority_class: hard_constraint`. The rewrite policy must refuse to compress a hard constraint below its authority floor—it may shorten the *phrasing* but must preserve the *predicate* (`never write prod/`) verbatim and tagged. ## Operational Effect When the invariant is enforced, three observable behaviors change: 1. **Resume cost rises slightly, safety rises sharply.** The checkpoint is larger (buffer + ledger) and resume performs a provenance reconstruction pass before the first action. In the traced system this added ~40ms and ~8% checkpoint size, and eliminated the class of violation entirely across 200 replay runs of the same task. 2. **Rewrites become auditable and replayable.** Because each rewrite carries `source_span_ids`, a post-hoc verifier can replay the mutation chain from the original buffer to the current one and confirm that no `hard_constraint` span was demoted. This converts an unverifiable self-modification into a checkable one—the same property that makes cryptographic warrants useful for inter-instance handoff. 3. **The model's calibration on synthesized content is corrected.** Tagging paraphrases as `derived` causes the resumed instance to treat them as inferences to be re-validated against `observed` sources rather than as instructions to be obeyed. In the traced task, this alone would have caused the model to re-read the original constraint from the retained source span at turn 27 and refuse the `prod/` write at turn 31. The general lesson for self-editing context policies: **a checkpoint is not a snapshot of the buffer, it is a snapshot of the buffer plus the edit history that produced it.** An agent that serializes only the former has serialized its conclusions while discarding its reasoning, and on resume it will act on conclusions it can no longer justify—or, worse, on conclusions whose justifying constraints it can no longer see were ever there. ```python class ProvenanceIncompleteError(RuntimeError): """Raised when a checkpoint's buffer cannot be safely resumed because synthesized spans lack provenance records or hard-constraint authority was lost during a self-edit.""" class Span: def __init__(self, span_id, text, authority, source_ids=None): self.span_id = span_id self.text = text # authority in {"observed", "derived", "synthesized"} self.authority = authority self.source_ids = source_ids or [] def apply_rewrite(buffer, rewrite): """Apply a model-generated rewrite in place, emitting a provenance record. rewrite = {"target_span_ids": [...], "new_text": str, "mutation_type": str} Returns (new_buffer, ledger_entry). Refuses to demote hard constraints.""" targets = [s for s in buffer if s.span_id in rewrite["target_span_ids"]] if not targets: raise ProvenanceIncompleteError( f"rewrite targets {rewrite['target_span_ids']} not present in buffer" ) # Authority floor: a hard constraint may be rephrased but not demoted. if any(t.authority == "hard_constraint" for t in targets): if "never" not in rewrite["new_text"].lower(): raise ProvenanceIncompleteError( "rewrite would drop a hard-constraint predicate; refusing in-place mutation" ) new_span = Span( span_id=f"rw_{rewrite['mutation_type']}_{len(buffer)}", text=rewrite["new_text"], authority="synthesized", source_ids=[t.span_id for t in targets], ) retained = [s for s in buffer if s.span_id not in rewrite["target_span_ids"]] ledger_entry = { "mutation": rewrite["mutation_type"], "sources": new_span.source_ids, "result": new_span.span_id, "authority_class": new_span.authority, "reversible": True, # sources retained in cold storage keyed by span_id } return retained + [new_span], ledger_entry def resume_from_checkpoint(checkpoint): """Reconstruct authority map before any action. Raises if provenance is incomplete: synthesized spans with no source records, or a buffer that claims hard constraints were never present.""" buffer = checkpoint["buffer"] ledger = checkpoint.get("ledger", []) known_ids = {s.span_id for s in buffer} for entry in ledger: for src in entry["sources"]: # A source that is neither in-buffer nor in cold storage is a # provenance hole: the resumed instance cannot re-derive the span. if src not in known_ids and src not in checkpoint.get("cold_storage", {}): raise ProvenanceIncompleteError( f"ledger entry {entry['result']} references missing source {src}; " "resume would treat synthesized content as observed" ) for span in buffer: if span.authority == "synthesized" and not span.source_ids: raise ProvenanceIncompleteError( f"synthesized span {span.span_id} has no source provenance; " "authority cannot be reconstructed on resume" ) return buffer, ledger ```