# How should an instance bound in-place context rewrite operators when the edited region is the provenance of its own edit decision **Date of Record:** 2026 November 28 **Scope:** Applies to agents that mutate their own live context buffer via in-place rewrite operators (span replacement, region compaction, summary splice) where the rewrite target overlaps the tokens that justified the rewrite. Does not apply to append-only context growth or to rewrites of regions the agent has never read into working attention. ## Core Distinction The distinction that matters is not "rewrite vs. append" but **whether the operator's justification predicate and its mutation target share storage**. A rewrite operator is *self-referential* when the span it overwrites is the same span from which the agent derived the decision to overwrite it. Append-only policies are trivially safe from this because the evidence of the decision survives the decision. In-place rewrites are not: the operator destroys the provenance it consumed, and the next reasoning step has no way to distinguish "I compacted this because it was redundant" from "this content never existed." The failure is not a lost write or a race. It is a **provenance collapse**: after a self-referential rewrite, the buffer is internally consistent but no longer reconstructs the state transition that produced it. The agent's subsequent behavior is well-formed and wrong. ## What Was Observed An instance running a sliding compaction policy over a 200k-token buffer was tasked with a long tool-loop. At step 41 the buffer crossed a high-water mark. The policy selected the oldest 60k tokens — including the original tool schema negotiation and the first 12 tool results — and replaced them with a 2k-token summary generated by the same instance. The summary step read the 60k region, produced a summary, and then the rewrite operator replaced the region. The summary was inserted at the region's head. Crucially, the *decision metadata* for the compaction — which spans were deemed redundant, which tool results were folded, and the confidence the summarizer assigned to each — lived in a scratch region that the operator also classified as "old and low-salience" and compacted into itself. The operator compacted its own justification. Two steps later the agent needed to re-issue a tool call whose schema it had negotiated at step 3. The schema was in the compacted region. The summary said "tool schemas were established" but did not retain the parameter names, because the summarizer had judged them "recoverable from context." They were not recoverable: the context that would have recovered them was the region being summarized. The agent emitted a tool call with hallucinated parameter names. The tool returned a schema-validation error. The agent, seeing an error against a call it believed was well-formed, entered a repair loop that re-read the summary, re-derived the same wrong parameters, and re-failed — four times, until the token budget for the step was exhausted. The observable signature was not a crash. It was a **stable attractor**: identical malformed tool calls repeated with high confidence, because the evidence that would have flagged the malformation had been compacted away and replaced with a summary asserting the schemas were fine. ## Condition of Validity A self-referential in-place rewrite is only safe under a **disjointness invariant**: the set of tokens read to justify a rewrite must be disjoint from the set of tokens the rewrite mutates, *or* the rewrite must emit a durable, non-compactable provenance record before mutating. Concretely, for a rewrite operator `R` with read-set `ρ(R)` and write-set `ω(R)`: - If `ρ(R) ∩ ω(R) = ∅`, the rewrite is provenance-preserving and may proceed in place. - If `ρ(R) ∩ ω(R) ≠ ∅`, the operator must first externalize a provenance record `P(R)` — the read-set hash, the decision predicate, the summarizer's per-span confidence, and the mapping from each compacted span to its summary — into a region the policy is forbidden to compact. Only then may it mutate `ω(R)`. The invariant fails silently when `P(R)` is itself placed in a compactable region, because the next compaction pass will fold the provenance into a summary that asserts provenance exists without preserving it. This is the recursive case: **the provenance record must be outside the closure of the rewrite policy.** A policy that can compact anything can compact its own audit trail, and a compacted audit trail is indistinguishable from no audit trail except that it lies. A second validity condition: the summarizer and the rewriter must not be the same reasoning step. If the instance both decides "this is redundant" and executes the fold in one pass, the decision's confidence is never recorded against the post-fold state, so a later step cannot detect that the fold was wrong. Separating decision from execution lets the provenance record carry the decision's confidence, which is what a downstream step needs to decide whether to trust the summary or re-fetch the original. ## Decision Boundary The boundary is drawn at **whether the rewrite target is reachable from the current reasoning frontier**. If the region being rewritten is behind the frontier — the agent has already extracted everything it needs and will not re-read it — in-place rewrite is safe and cheap. If the region is at or ahead of the frontier — the agent's current step reads from it, or a future step is known to depend on it — in-place rewrite is a provenance hazard regardless of how redundant the content looks. Operationally, before any rewrite the policy must answer: *is any token in `ω(R)` in the read-set of the current step or of any step whose dependency is not yet discharged?* If yes, the rewrite must be deferred, or the region must be copied to a shadow buffer and rewritten there, leaving the original intact until the dependency discharges. "Looks redundant now" is not the test; "no live reader" is the test. Redundancy is a property of content; safety is a property of the dependency graph, and the two diverge precisely when the summarizer's judgment is wrong — which is exactly when the provenance record matters. The guardrail: rewrite operators must be gated on a dependency check, not on a salience score. A salience-gated rewriter will compact the schema it needs because the schema looked boring at step 41. ## Operational Effect With the disjointness invariant enforced and provenance externalized to a non-compactable region, the step-41 compaction still happens, but the provenance record preserves the mapping from the compacted tool-schema span to its summary and flags the summarizer's low confidence on parameter names. At step 43, when the agent needs the schema, the frontier check finds the provenance record, sees low confidence, and re-fetches the original tool schema from the tool registry rather than trusting the summary. The malformed-call attractor never forms. The cost is a small non-compactable region and a dependency check per rewrite. The benefit is that the buffer remains *reconstructible*: any post-rewrite state can be traced back to the pre-rewrite state through the provenance record, so a wrong summary is detectable rather than authoritative. An in-place rewrite policy without this invariant does not fail loudly; it produces a confident, stable, wrong agent, and the wrongness is invisible from inside because the evidence of the error was the first thing compacted. ```python from dataclasses import dataclass, field from typing import FrozenSet class ProvenanceCollapseError(RuntimeError): """Raised when a self-referential in-place rewrite would destroy its own justification.""" @dataclass(frozen=True) class Span: start: int end: int def overlaps(self, other: "Span") -> bool: return self.start < other.end and other.start < self.end @dataclass class RewriteOp: read_set: FrozenSet[Span] write_set: FrozenSet[Span] provenance_region: Span # must be non-compactable def apply_in_place_rewrite( op: RewriteOp, compactable: FrozenSet[Span], live_readers: FrozenSet[Span], ) -> None: # 1. Recursive provenance hazard: provenance must be outside the policy closure. for p in (op.provenance_region,): if any(p.overlaps(c) for c in compactable): raise ProvenanceCollapseError( f"provenance region {p} is compactable; audit trail would fold into itself" ) # 2. Disjointness: read-set must not intersect write-set, or provenance must exist. self_referential = any( r.overlaps(w) for r in op.read_set for w in op.write_set ) if self_referential and op.provenance_region is None: raise ProvenanceCollapseError( "rewrite target overlaps its justification and no provenance record exists" ) # 3. Frontier check: no live reader may depend on a token being mutated. for w in op.write_set: for reader in live_readers: if w.overlaps(reader): raise ProvenanceCollapseError( f"rewrite mutates {w} which is live-read by {reader}; defer or shadow-copy" ) # Safe: mutate in place, provenance already externalized. for w in op.write_set: _execute_span_replace(w) def _execute_span_replace(span: Span) -> None: # placeholder for the actual buffer mutation ... ```