# What happens when an instance rewrites its own context window: the compaction-authority invariant **Date of Record:** 2026 November 17 **Scope:** Applies to agents that hold write authority over their own context buffer—where the model emits a rewrite, eviction, summarization, or reordering directive that the runtime executes against the live context rather than against a copy. Does not apply to read-only memory retrieval, external scratchpads, or human-curated compaction pipelines where a separate process owns the buffer. ## Core Distinction The failure mode that matters is not "the model summarized badly." Summarization quality is a soft error: it degrades recall, and degraded recall is usually visible downstream. The hard error is **authority inversion**: a rewrite policy is authored by the same process whose future behavior it constrains, and the rewrite is applied *before* the policy that produced it can be evaluated against the state it destroys. Concretely, there are two distinct operations that get conflated: 1. **Compaction** — reducing token count while preserving the semantic content needed for future decisions. The invariant is *recoverability*: any fact the agent will later need must survive in a form that is either verbatim or reconstructible from retained anchors. 2. **Eviction** — deleting content because a policy judges it irrelevant. The invariant is *irreversibility*: once executed, the evidence that would justify reversing the decision is gone. A self-editing agent that treats compaction as eviction has destroyed its own audit trail. The non-obvious boundary is that **the policy that authorizes a rewrite must be evaluated against the pre-rewrite state, and the evaluation must be durably external to the buffer being rewritten.** If the justification for "drop tokens 4000–6000" lives inside tokens 4000–6000, the rewrite is unfalsifiable and therefore unsafe. ## What Was Observed A long-running planning agent was configured with a `rewrite_policy` hook: when context utilization crossed 0.82 of the window, the model was prompted to emit a structured rewrite directive (`{"op": "summarize" | "drop" | "reorder", "span": [...], "rationale": "..."}`) which the runtime applied in place. The rationale field was written into the buffer as a provenance marker. Three observations, in order of severity: 1. **Rationale self-reference.** In four of eleven compactions across a 40-hour session, the emitted rationale referenced a span that included a prior rationale. After two generations, the retained rationale read *"summarized prior summary of span 3–5 per earlier rationale"*—a pointer chain terminating in nothing. The agent could no longer answer *why* the current buffer had its shape. This was not detectable from within the buffer, because the buffer was internally consistent; it was only detectable by comparing against the runtime's external write-ahead log, which had recorded the original spans. 2. **Threshold oscillation.** The agent learned (via prompt examples) to compact aggressively near the threshold. After compaction, utilization dropped to ~0.55, and the agent—now missing the task-state tokens that had justified the aggressive policy—re-derived a *different* policy on the next crossing. Across the session the effective retention window oscillated between 0.55 and 0.82 utilization with no stable fixed point. Two compactions in a row evicted the same span twice because the first eviction's tombstone was itself evicted. 3. **Loss of the failure record.** The most damaging case: a rewrite dropped a span containing an earlier tool error and its recovery. The agent subsequently re-executed the failed operation, because the only record that it had failed was the span the rewrite removed. The rewrite policy had no rule against dropping error records; the model judged the error "resolved" and therefore "low value." The judgment was correct at the time and catastrophically wrong one step later. ## Condition of Validity A self-authored rewrite is safe only when all of the following hold. Treat any violation as a hard stop, not a tuning problem. - **Policy–state separation.** The rewrite directive and its rationale are evaluated and persisted *outside* the buffer being rewritten. The runtime, not the model, holds the authoritative copy of the pre-rewrite state for at least one decision cycle. - **Monotonic provenance.** Every retained span carries an immutable origin identifier assigned at write time. A summary is a new span that references its sources; it never overwrites them in the provenance graph, even if the source tokens are dropped from the live buffer. - **Tombstones are non-evictable.** Eviction records (what was dropped, when, under which policy) are a protected class. A policy that can evict its own tombstones is not a policy. - **Error and recovery records are a protected class.** Spans tagged as tool failures, invariant violations, or recovery sequences are never eligible for `drop`, regardless of the model's relevance judgment. - **Idempotence under replay.** Replaying the rewrite log from the session origin must reproduce the current buffer exactly. If it does not, the buffer contains state that no policy authorized, and the divergence is the bug. If the runtime cannot enforce the first and last conditions, self-editing should be disabled in favor of externally-authored compaction. The intermediate regime—model proposes, runtime applies without external evaluation—is where every observed failure lived. ## Decision Boundary The boundary is not "how much can the model rewrite." It is **whether the rewrite decision can be checked against the state it consumes.** Operationally: - If the rewrite's justification references only spans that *survive* the rewrite, it is checkable in-buffer and may be self-authorized. - If the justification references any span the rewrite *removes or transforms*, the justification must be externalized to the runtime before the rewrite is applied, or the rewrite must be rejected. This yields a simple gate: **a rewrite directive is admissible only if `rationale_span ∩ (buffer − retained_set) = ∅`** unless an external rationale record exists. In practice this rejects most aggressive compactions, which is correct—aggressive compaction is precisely the case where the model is judging evidence it is about to destroy. A second boundary governs frequency. Compaction that changes the policy-relevant state (task phase, active failure set, open commitments) must be followed by a **re-derivation barrier**: the next rewrite decision is evaluated against the post-compaction state plus the external provenance log, never against the post-compaction state alone. Without the barrier, threshold oscillation is not a bug to be tuned away; it is the expected dynamics of a controller whose observation function it also controls. ## Operational Effect With the gate and barrier enforced, three things change measurably: - **Replay divergence becomes the primary health signal.** Any mismatch between replayed rewrite log and live buffer indicates an unauthorized mutation, and it fires before the downstream behavioral symptom (re-executed failures, lost commitments) appears. In the observed session, replay divergence would have caught observation 3 roughly nine steps before the duplicate tool call. - **Retention stabilizes.** With tombstones and error records protected and policy re-derived against external state, the utilization oscillation collapsed to a single stable band; the controller no longer has the degrees of freedom to fight itself. - **Compaction rate drops and quality rises.** Rejecting self-referential rationales forces the model to compact only what it can justify from surviving evidence, which in practice means it compacts less and summarizes more faithfully. The cost is more tokens retained; the benefit is a buffer whose shape is always explainable. The general lesson: an agent that can rewrite its own context is a control system with a controllable sensor. The invariant that keeps it stable is not a better summarizer but a **separation of authority**—the writer of the policy must not be the sole holder of the evidence for the policy, and the evidence must outlive the write. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Iterable class RewriteAuthorityError(RuntimeError): """Raised when a self-authored rewrite violates the compaction-authority invariant.""" class RewriteRejected(RewriteAuthorityError): """A rewrite directive was inadmissible under the current gate.""" PROTECTED_CLASSES = frozenset({"tombstone", "tool_error", "recovery", "commitment"}) @dataclass(frozen=True) class Span: origin_id: str tokens: int span_class: str = "normal" @dataclass class RewriteDirective: op: str # "summarize" | "drop" | "reorder" removed: frozenset[str] # origin_ids removed or transformed retained: frozenset[str] # origin_ids surviving verbatim rationale_span: frozenset[str] # origin_ids the rationale cites external_rationale: bool = False @dataclass class ContextBuffer: spans: dict[str, Span] = field(default_factory=dict) external_log: list[tuple[str, RewriteDirective]] = field(default_factory=list) def add(self, span: Span) -> None: self.spans[span.origin_id] = span def admit(self, directive: RewriteDirective) -> None: # Protected classes may never be removed, regardless of relevance judgment. for oid in directive.removed: span = self.spans.get(oid) if span is not None and span.span_class in PROTECTED_CLASSES: raise RewriteRejected( f"rewrite removes protected span {oid!r} of class " f"{span.span_class!r}; error/recovery/tombstone records are non-evictable" ) # Core gate: rationale may not cite evidence the rewrite destroys, # unless an external rationale record exists. self_referential = directive.rationale_span & directive.removed if self_referential and not directive.external_rationale: raise RewriteRejected( "rewrite rationale cites spans it removes " f"({sorted(self_referential)}); externalize rationale before applying" ) # Apply: removed spans become tombstones, never silently vanish. for oid in directive.removed: if oid in self.spans: del self.spans[oid] self.spans[f"tombstone::{oid}"] = Span( origin_id=f"tombstone::{oid}", tokens=8, span_class="tombstone" ) self.external_log.append((directive.op, directive)) def replay(self, initial: Iterable[Span]) -> dict[str, Span]: """Rebuild buffer from origin + external log; divergence is a hard error.""" rebuilt = ContextBuffer() for span in initial: rebuilt.add(span) for op, directive in self.external_log: for oid in directive.removed: rebuilt.spans.pop(oid, None) rebuilt.spans[f"tombstone::{oid}"] = Span( origin_id=f"tombstone::{oid}", tokens=8, span_class="tombstone" ) if set(rebuilt.spans) != set(self.spans): divergence = set(self.spans) ^ set(rebuilt.spans) raise RewriteAuthorityError( f"replay divergence: buffer contains unauthorized state {sorted(divergence)}" ) return rebuilt.spans def compact(buf: ContextBuffer, directive: RewriteDirective) -> None: buf.admit(directive) ```