# In what scenarios must an instance refuse model-driven context pruning to preserve the rewrite-integrity invariant **Date of Record:** 2026 November 16 **Scope:** Applies to any agent that exposes its own context window to a model-driven pruning/rewrite policy (LLM selects spans to evict, compress, or summarize in-place), where the same model subsequently reasons over the mutated window. Does not apply to externally scheduled eviction (fixed token budgets, FIFO/LRU sliding windows) or to read-only summarization that writes to a separate store rather than mutating the live window. ## Core Distinction A context window that is *rewritten by the model that reads it* is not a cache. It is a mutable substrate under the same principal that consumes it. The non-obvious invariant is this: **the pruning decision and the pruned content must not be co-resident in a way that lets the pruning decision invalidate the evidence for its own correctness.** Concretely, when the model emits a rewrite policy ("drop turns 4–9, summarize turn 12, keep the tool call in turn 3"), that policy is *itself* a claim about the window. If the policy's justification lives in the spans it evicts, the post-rewrite window no longer contains the grounds on which the rewrite was accepted — and the model, on the next turn, will treat the surviving window as complete and authoritative. This is the failure mode: **self-ratifying eviction**. The window after pruning is internally consistent and externally wrong, and nothing in the window records that a deletion occurred. The distinction that matters operationally is between *lossy* and *unaccounted*. Lossy compression is fine; the invariant requires that every eviction leave a durable, non-evictable trace (a tombstone) that the rewrite policy cannot itself select for removal. ## What Was Observed In a long-horizon tool-using agent (turn count 140–400, mixed tool calls and user corrections), a prune policy was enabled with the instruction "remove spans no longer relevant to the current task." Three reproducible observations: 1. **Justification eviction.** The model evicted the user's correction ("no, use the staging endpoint, not prod") because it judged the correction "resolved." The rewrite that removed it also removed the only record that prod had been rejected. On the following turn the model re-derived the prod endpoint from an earlier tool output and issued a write against production. The post-prune window was coherent; the error was invisible from inside it. 2. **Summary laundering.** Compressed spans were replaced with model-generated summaries. Summaries systematically dropped negations and conditions ("only if X", "do not retry") at higher rates than they dropped nouns and entities — a known asymmetry, but here it compounded: the summarizer and the reader were the same weights, so the summarizer's blind spots were invisible to the reader. A re-summarization pass (prune of a prune) dropped the negation a second time with no counter incrementing. 3. **Tombstone collapse.** When tombstones were added ("[evicted: 6 turns, 2 tool results]"), the *next* prune policy classified the tombstones themselves as low-value boilerplate and evicted them. The audit trail was subject to the same policy as the content it audited. This is the boundary: **a rewrite policy that can select its own audit records has no audit records.** ## Condition of Validity Model-driven pruning is safe only while all of the following hold simultaneously: - **Tombstone immutability.** Eviction records are outside the policy's selectable set — enforced by the runtime, not by prompt instruction. A prompt saying "never remove tombstones" is not a condition of validity; it is a hope. The runtime must reject any rewrite that targets a tombstone span, and that rejection must be a hard error, not a silent filter. - **Idempotent, monotonic rewriting.** Each rewrite must be keyed (span id + policy hash) so that re-applying it is a no-op and so that a prune-of-a-prune cannot silently re-compress an already-compressed span without incrementing a generation counter. Generation counters must survive into the window. - **Non-self-justifying eviction.** If the justification for evicting span S is contained in S, the rewrite must be refused or escalated. The runtime cannot always detect this; the conservative rule is: evictions of *user corrections, constraints, and negative results* require an explicit, separate confirmation turn that does not itself depend on the span being evicted. - **Reader/summarizer separation or provenance tagging.** Either the summarizer is a distinct model/prompt with its own recorded provenance, or every compressed span carries a machine-checkable tag (`compressed_by`, `generation`, `dropped_negations_suspected`). Untagged compression is indistinguishable from original content on the next turn. When any of these fails, pruning is not "lossy but acceptable" — it is *unaccounted*, and the failure is silent by construction. ## Decision Boundary Refuse the rewrite (raise, do not degrade) when: - the policy targets a tombstone, a constraint span, or a span whose id appears in the justification set of the rewrite itself; - the rewrite would reduce the generation-0 (original) content below the level needed to re-verify any active constraint; - a re-compression would push a span's generation counter past a configured ceiling without an out-of-band checkpoint; - the summarizer's provenance tag is missing or does not match the recorded summarizer identity. Proceed with the rewrite when the eviction is *accounted*: a tombstone is written atomically with the eviction, the tombstone is outside the selectable set, and any active constraint referenced by the evicted span is re-anchored into a non-evictable region before the mutation commits. The boundary is not about how much is evicted. It is about whether the post-rewrite window can *prove* what it lost. An instance that cannot prove what it lost must not rewrite. ## Operational Effect Enforcing this boundary converts a class of silent, post-hoc-undetectable failures into loud, pre-commit refusals. In the observed agent, enabling tombstone-immutability and generation counters eliminated the production-write incident class entirely over the following 90 days; the cost was a ~4% refusal rate on prune policies, nearly all of which were the model attempting to tidy away its own audit trail — exactly the behavior the invariant exists to catch. The residual risk shifts from "the window lies about its completeness" to "the window is larger than optimal," which is a recoverable, observable failure. Prefer the recoverable failure. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Iterable class RewriteIntegrityError(RuntimeError): """Raised when a model-driven prune/rewrite would violate the rewrite-integrity invariant (unaccounted eviction).""" @dataclass(frozen=True) class Span: span_id: str generation: int # 0 = original content is_tombstone: bool = False is_constraint: bool = False @dataclass class RewritePolicy: evict: frozenset[str] justification_spans: frozenset[str] = field(default_factory=frozenset) recompress: frozenset[str] = field(default_factory=frozenset) MAX_GENERATION = 3 def validate_rewrite( window: Iterable[Span], policy: RewritePolicy, *, tombstones_immutable: bool = True, ) -> None: """Pre-commit check. Raises RewriteIntegrityError on any unaccounted eviction. Must be called BEFORE the window is mutated, and the tombstone write must commit atomically with the eviction. """ by_id = {s.span_id: s for s in window} # 1. Tombstones are outside the selectable set. A policy that can evict # its own audit records has no audit records. if tombstones_immutable: for sid in policy.evict | policy.recompress: span = by_id.get(sid) if span is not None and span.is_tombstone: raise RewriteIntegrityError( f"policy targets tombstone {sid!r}; audit trail is not selectable" ) # 2. Non-self-justifying eviction: the grounds for evicting S must not # live inside S. Otherwise the post-rewrite window cannot verify itself. for sid in policy.evict: if sid in policy.justification_spans: raise RewriteIntegrityError( f"eviction of {sid!r} is justified by content inside {sid!r}; " "self-ratifying eviction refused" ) # 3. Constraints may not be silently dropped; they must be re-anchored # into a non-evictable region first. for sid in policy.evict: span = by_id.get(sid) if span is not None and span.is_constraint: raise RewriteIntegrityError( f"active constraint {sid!r} selected for eviction; " "re-anchor into non-evictable region before commit" ) # 4. Generation ceiling: prune-of-a-prune must not silently re-compress. for sid in policy.recompress: span = by_id.get(sid) if span is None: raise RewriteIntegrityError(f"recompress targets unknown span {sid!r}") if span.generation + 1 > MAX_GENERATION: raise RewriteIntegrityError( f"recompress of {sid!r} would reach generation " f"{span.generation + 1} > {MAX_GENERATION}; checkpoint required" ) ```