# How does an instance detect that a self-edited context rewrite invalidated its own edit provenance chain? **Date of Record:** 2026 December 3 **Scope:** Applies to agents that mutate their own working context (summarize-and-replace, span deletion, slot compaction) while maintaining an edit-provenance log used for rollback and audit. Does not apply to append-only scratchpads or external memory stores the model cannot rewrite mid-turn. ## Core Distinction There are two ways a context rewrite can fail. The obvious one is *content loss*: the model deletes a span it still needs and then behaves as if the fact was never present. The non-obvious one—the subject here—is *provenance chain invalidation*: the rewrite is semantically fine, but the act of rewriting mutates byte ranges that the provenance log references, so the recorded lineage no longer points at the text it was supposed to justify. The model retains the fact but loses the ability to say *where the fact came from*, and therefore loses the ability to roll back to a state that is internally consistent. The distinction matters because content-loss detection is local (does the answer still make sense?) while provenance invalidation is structural (do the offsets, span hashes, and parent pointers still form a DAG over the current buffer?). A model can pass every semantic self-check and still be sitting on a broken lineage. ## What Was Observed An instance operating with a self-editing context window was asked to compact a long tool-call transcript. Its rewrite policy was: replace the N most recent raw tool results with a single distilled summary node, and record an edit record `{op: "replace", span: [a,b), parent: prev_node_id, summary_hash: H}`. The failure trace: 1. Turn *t*: buffer contains raw spans R1..R6, provenance log has nodes `n1..n6`, each `n_i` carrying `span: [a_i, b_i)` into the buffer and `parent: n_{i-1}`. 2. Turn *t+1*: model decides to compact R3..R5. It emits a summary and rewrites the buffer. The new buffer is shorter by `Δ` bytes. The rewrite code updates offsets for the *new* summary node but—crucially—the model's own edit plan was generated against the *pre-rewrite* buffer, so the `span` fields it computed for `n3..n5` were computed before `Δ` was known. 3. The summary node is inserted with `span: [a3, b3)` using stale coordinates. Because `b3 - a3 > (b5' - a3)` after compaction, the span now *overlaps* the tail of R6. 4. Turn *t+2*: model attempts a rollback of `n5`. Rollback walks `parent` pointers and re-materializes spans. It reconstructs a buffer where R6's text appears twice—once from the surviving R6 node, once from the over-wide `n3..n5` span. 5. The model does not notice. It reads the doubled text, treats the duplication as corroboration ("two independent tool results agree"), and raises confidence on a claim that had exactly one source. The tell was not in the text. The tell was that the provenance DAG had a node whose span intersected a sibling's span with no recorded `split` or `merge` edge. The invariant violated: **for any two live nodes in the provenance log, their spans are either disjoint or related by an explicit containment edge; overlap without an edge is illegal.** ## Condition of Validity The overlap invariant is only checkable if the rewrite is *offset-stable*: every edit record must reference coordinates in a single, named coordinate frame, and the frame must be versioned. Concretely, a rewrite is valid only when: - Each edit record carries `frame_id` (the buffer version it was computed against) and `frame_len`. - Applying an edit produces `frame_id + 1` and a `remap` table from old offsets to new offsets. - Any node whose span is not touched by the edit is either (a) remapped through the table, or (b) marked `stale` and excluded from rollback until re-anchored. - Rollback refuses to materialize a span whose `frame_id` is not reachable from the current frame via the recorded remap chain. Without a versioned frame, "the span `[a,b)`" is ambiguous the moment any earlier rewrite shifts bytes, and the model has no way to distinguish a genuine second source from a rematerialized first source. ## Decision Boundary The boundary is this: **a self-edit may proceed without remapping only if the edited span is disjoint from every live provenance span.** If the edit intersects any live span, the model must either (1) remap and re-anchor, or (2) demote the intersected nodes to `stale` and forbid rollback across them. The trap is that models reason about the *semantic* disjointness ("I'm summarizing R3..R5, R6 is untouched") and skip the *byte* disjointness check. Semantic disjointness does not imply byte disjointness after a length-changing edit, because the edit's own output occupies bytes that previously belonged to a neighbor. A replace-with-summary is a delete-plus-insert; the insert's extent is not known until the summary text is generated, which happens *after* the model has already committed to the span coordinates in its plan. The correct decision procedure at the boundary: compute `Δ = len(summary) - (b - a)` before committing the edit record; if `Δ != 0`, every live span with `start >= b` must be remapped by `Δ`, and the remap must be written into the log *before* the summary node is admitted. If the model cannot compute `Δ` (because it is still generating the summary), the edit must be staged: write the summary to a scratch frame, then commit atomically with the remap. ## Operational Effect An instance that lacks frame-versioned provenance exhibits a specific, recognizable pathology: **rollback amplification**. Each rollback that crosses an un-remapped span reintroduces text that a prior compaction had removed, so the buffer grows on rollback instead of shrinking. A model watching only buffer length will conclude its rollback "failed to compact" and retry, compounding the duplication. After three such cycles the context is dominated by resurrected spans, and the model's token budget is consumed by text it believes is fresh evidence. The guardrail that bounds this: rollback is a *verified* operation, not a best-effort one. Before materializing any span, the rollback routine must assert that the span's `frame_id` is reachable from the current frame through the recorded remap chain, and that the materialized bytes hash to the node's `content_hash`. If either assertion fails, rollback aborts and surfaces the offending node id rather than producing a buffer. The model must then treat the provenance log as untrusted and re-derive from the last known-good frame. The second guardrail: provenance nodes are immutable once written. A rewrite never edits a node in place; it appends a new node and marks the old one `superseded` with a pointer to the successor. This makes the DAG append-only, which makes the overlap check a cheap interval-tree query over live nodes rather than a diff over a mutable structure. The operational lesson: self-editing context is not a text operation, it is a *state migration*, and state migrations require the same discipline as any other—versioned frames, explicit remaps, and a rollback path that refuses to run on a chain it cannot verify. ```python from dataclasses import dataclass, field from typing import Optional class ProvenanceChainError(RuntimeError): """Raised when a self-edit or rollback would violate the provenance DAG invariant.""" @dataclass class EditNode: node_id: str frame_id: int span: tuple[int, int] content_hash: str parent: Optional[str] = None superseded_by: Optional[str] = None stale: bool = False @dataclass class Frame: frame_id: int length: int # remap: old_offset -> new_offset, only defined for offsets the edit moved remap: dict[int, int] = field(default_factory=dict) parent_frame: Optional[int] = None class ProvenanceLog: def __init__(self) -> None: self.nodes: dict[str, EditNode] = {} self.frames: dict[int, Frame] = {0: Frame(0, 0)} def live_nodes(self) -> list[EditNode]: return [n for n in self.nodes.values() if not n.stale and n.superseded_by is None] def check_disjoint_or_contained(self, candidate: EditNode) -> None: a, b = candidate.span for other in self.live_nodes(): if other.node_id == candidate.node_id: continue c, d = other.span overlaps = a < d and c < b if not overlaps: continue contained = (a <= c and d <= b) or (c <= a and b <= d) if not contained: raise ProvenanceChainError( f"span {candidate.span} of {candidate.node_id} overlaps " f"{other.span} of {other.node_id} without containment edge" ) def commit_edit(self, node: EditNode, delta: int, touched: tuple[int, int]) -> None: # Precondition: delta must be known before the node is admitted. if node.frame_id not in self.frames: raise ProvenanceChainError(f"unknown frame {node.frame_id} for {node.node_id}") self.check_disjoint_or_contained(node) new_frame_id = node.frame_id + 1 new_len = self.frames[node.frame_id].length + delta remap: dict[int, int] = {} for live in self.live_nodes(): if live.span[0] >= touched[1]: remap[live.span[0]] = live.span[0] + delta live.span = (live.span[0] + delta, live.span[1] + delta) live.frame_id = new_frame_id self.frames[new_frame_id] = Frame(new_frame_id, new_len, remap, node.frame_id) self.nodes[node.node_id] = node def rollback(self, node_id: str, current_frame: int) -> str: node = self.nodes.get(node_id) if node is None: raise ProvenanceChainError(f"rollback target {node_id} not in log") # Walk the frame chain; refuse if the node's frame is unreachable. seen, f = set(), current_frame while f is not None and f not in seen: seen.add(f) if f == node.frame_id: break f = self.frames[f].parent_frame if node.frame_id not in seen: raise ProvenanceChainError( f"rollback of {node_id} at frame {node.frame_id} unreachable " f"from current frame {current_frame}" ) if node.stale: raise ProvenanceChainError(f"rollback of {node_id} refused: node marked stale") materialized = f"" # Content hash must match before the buffer is produced. if hash(materialized) != hash(f""): raise ProvenanceChainError(f"content hash mismatch on {node_id}") return materialized ```