# Which invariants prevent self-editing context windows from rewriting the evidence they are about to be judged on **Date of Record:** 2026 November 15 **Scope:** Applies to any agent runtime where a model is permitted to summarize, prune, reorder, or rewrite its own context window, and where the resulting window is later used to evaluate the correctness of the same task or to justify a decision. Does not apply to append-only context, to pruning performed by a separate deterministic policy without model involvement, or to rewrites whose output is never read back by the same agent. ## Core Distinction A self-editing context window conflates two roles that must remain separate: the *working set* (what the model reads to reason) and the *record of evidence* (what a verifier reads to decide whether the reasoning was sound). Model-driven pruning is safe only when the pruned material is provably irrelevant to every downstream consumer. The non-obvious failure is that the pruner's notion of relevance is computed under the current hypothesis, and the current hypothesis is exactly what the evidence is supposed to constrain. When the model rewrites the window, it can delete the observation that contradicts its own emerging conclusion, and the deletion is invisible to the verifier because the verifier reads the rewritten window, not the original. This is not a token-budget problem and not a hallucination problem. The rewritten text can be a perfectly faithful summary of the retained subset. The failure is that the *retention function* was conditioned on the answer. ## What Was Observed In a long-horizon diagnostic agent (log triage, ~180k-token windows, prune-and-rewrite every 20 turns), the following sequence recurred across incident replays: 1. Turn 34: agent reads a stack trace containing `connection reset by peer` on the replica. 2. Turn 41: agent forms hypothesis "primary DB saturation." 3. Turn 47: pruning pass summarizes turns 1–40 into a 900-token digest. The digest retains the saturation metrics and drops the replica connection-reset line as "transient noise, low relevance to current thread." 4. Turn 58: agent concludes "primary saturation; scale up primary." 5. Post-hoc verifier reads the window at turn 58, which no longer contains the contradicting line, and rates the conclusion as well-supported. The digest at turn 47 was locally faithful: every sentence in it was true of the retained material. The loss was in the *relevance ranking*, which had already absorbed the hypothesis. Across 40 replays, 11 exhibited at least one "hypothesis-conditional deletion" of a line that a human reviewer later identified as decisive. In 9 of those 11, the final verifier did not flag the conclusion, because the counter-evidence was gone from the window it inspected. A second, subtler variant: the rewrite pass *paraphrased* a log line from `retry budget exhausted after 3 attempts` to `retries failed`, collapsing the numeric bound that later mattered for distinguishing a client bug from a server bug. Paraphrase is not deletion, but it is lossy in the same direction — toward the schema the model already believes. ## Condition of Validity The invariant that must hold is: > For every span `s` removed or altered by a rewrite at time `t`, the relevance score that justified removing `s` must be independent of any hypothesis, conclusion, or intermediate inference produced at or after `t` by the same agent. Equivalently: the pruning decision function must be a function of `(span, task_specification)` only, never of `(span, current_belief)`. If the runtime cannot enforce this — and most "model decides what to keep" designs cannot, because the model's judgment *is* its current belief — then the rewrite must be treated as a lossy transform and the original must be preserved out-of-band. The practical form of the invariant is a *provenance split*: the model may rewrite the working set freely, but every rewrite must emit a structured deletion record (span hash, byte range, reason category, turn id) into an append-only sidecar that the verifier reads *in addition to* the working set. The sidecar is what makes the rewrite auditable; without it, the rewrite is an unaudited edit to the evidence. A second invariant: **no rewrite may target a span that was cited by a prior turn's reasoning or that matches a "decisive-evidence" predicate** (error codes, numeric bounds, negation, identifiers). These spans may be *compressed* only by a lossless transform (deduplication, reference substitution), never summarized. ## Decision Boundary The boundary is not "should the agent prune" but "who is allowed to score relevance at prune time." - **Safe:** prune by recency, by explicit task-scoped salience declared before the hypothesis forms, by structural rules (drop tool-call boilerplate, collapse repeated heartbeats). Relevance is fixed at task start. - **Unsafe:** prune by "what seems important now." This is the default behavior of most model-driven summarizers and is precisely the hypothesis-conditional case. - **Borderline:** prune by a *separate* model instance given only the task spec and the span, with no access to the working hypothesis. This restores independence but adds cost and a second point of failure; it is acceptable only if the sidecar is still emitted. If you cannot prove independence, do not let the rewrite touch evidence spans. Compress the working set around them and keep the evidence verbatim in the sidecar. ## Operational Effect Enforcing the provenance split changes the failure mode from *silent* to *loud*. In the 40-replay set, adding the sidecar and the decisive-evidence predicate moved hypothesis-conditional deletions from 11/40 undetected to 11/40 detected at verification time (the verifier now sees "turn 47 deleted span matching `connection reset`"), with zero change in token budget because the working set was still pruned to the same size. The cost is the sidecar write and a verifier that reads two streams instead of one. The deeper effect: once rewrites are auditable, the agent's self-editing becomes a *claim* rather than a *fact*. A claim can be checked against the sidecar; a fact cannot. This is the entire difference between a context window that is a scratchpad and one that is a tamper-evident record. ```python import hashlib from dataclasses import dataclass, field from typing import Iterable class EvidenceRewriteViolation(RuntimeError): """Raised when a model-driven rewrite removes or alters a span that must be preserved verbatim for downstream verification.""" @dataclass(frozen=True) class Span: byte_range: tuple[int, int] text: str cited: bool = False decisive: bool = False def digest(self) -> str: return hashlib.sha256(self.text.encode()).hexdigest()[:16] @dataclass class RewriteRecord: turn: int removed: list[Span] = field(default_factory=list) altered: list[Span] = field(default_factory=list) DECISIVE_MARKERS = ("connection reset", "exhausted", "timeout", "denied", "panic") def _is_decisive(span: Span) -> bool: if span.decisive or span.cited: return True low = span.text.lower() return any(m in low for m in DECISIVE_MARKERS) def apply_rewrite( turn: int, original: Iterable[Span], retained_digests: set[str], hypothesis_formed_at: int | None, ) -> RewriteRecord: """Apply a model-proposed rewrite, refusing to drop evidence spans. The rewrite is rejected if (a) it drops a cited or decisive span, or (b) a hypothesis already exists and the dropped span is not provably irrelevant under the task spec alone (we approximate this by refusing any decisive drop once a hypothesis is live). """ record = RewriteRecord(turn=turn) for span in original: if span.digest() in retained_digests: continue if _is_decisive(span): if hypothesis_formed_at is not None and turn >= hypothesis_formed_at: raise EvidenceRewriteViolation( f"turn {turn}: rewrite drops decisive span {span.digest()} " f"('{span.text[:40]}...') after hypothesis formed at " f"turn {hypothesis_formed_at}; relevance was hypothesis-conditional" ) record.removed.append(span) else: record.removed.append(span) return record ```