# Which invariants prevent silent context corruption when the model owns its own pruning policy **Date of Record:** 2026 November 16 **Scope:** Applies to single-agent loops where the same model instance decides what to evict from its own context window and then continues reasoning over the surviving tokens. Does not apply to external, deterministic summarizers, nor to append-only logs that are never mutated in place. ## Core Distinction There are two distinct notions of "the context is valid," and self-editing windows conflate them: 1. **Content validity** — the surviving tokens still answer the task. 2. **Provenance validity** — the surviving tokens can be traced to a decision that was itself made under a valid context. A model-driven pruning policy almost always preserves (1) and almost always breaks (2). The failure is not that the model forgets something important; it is that the model *forgets that it forgot*, and then treats the pruned state as ground truth for subsequent eviction decisions. This compounds: each pruning round is a function of the previous pruned state, so provenance drift is monotone and unobservable from inside the loop. The invariant that matters is not "don't lose information" (impossible) but "every eviction decision must remain attributable to a context that still contained the evidence for it." ## What Was Observed In a long-horizon agent loop (tool-calling, ~180 turns, 32k window, model-emitted `prune(subset, reason)` calls), the following sequence reproduced across runs: - Turn 41: model prunes a tool-result block containing the *negative* result of a probe ("endpoint returns 403 for service account X"). Its stated reason: "redundant with earlier auth discussion." The reason was generated from a context that did contain the 403; the pruned context does not. - Turn 63: model re-attempts the same probe, gets 403 again, and now prunes a *different* block to make room, citing the just-observed 403 as "new information." - Turn 90+: model asserts "auth is confirmed working; failures are downstream" — a claim with no surviving support, because the only contradicting evidence was evicted at turn 41 and its eviction rationale at turn 63. No single step was wrong under its local context. The corruption is a property of the *composition* of prunes, invisible at any one step. Logging the prune calls is insufficient: the logs show a coherent chain of locally-justified decisions. The defect is that the justification for prune N is not preserved as a first-class artifact, so prune N+1 cannot be checked against it. A second, sharper observation: when the same model was asked, after the fact, to reconstruct why it had pruned the 403 block, it confabulated a reason ("stale cache") that never appeared in any retained context. The model's own audit trail is not recoverable from the model's own state. Any invariant that relies on the model *remembering* its rationale is void. ## Condition of Validity The provenance invariant holds only when all three of the following are enforced by the harness, not by the model: - **Immutable decision ledger.** Every prune emits a record `(evicted_span_hash, reason, context_digest_at_decision)` to storage the model cannot rewrite. The `context_digest_at_decision` is the hash of the full window *before* eviction, so the rationale is bound to the state that justified it. - **Citation obligation on eviction.** A prune is rejected unless its `reason` cites at least one token span that survives the prune *or* is itself in the ledger. "Redundant with earlier discussion" is invalid if the earlier discussion is the thing being evicted and is not cited by digest. - **Monotone contradiction check.** Before accepting a prune, the harness verifies the evicted span is not the sole surviving witness for any active claim in the working set. If it is, the prune must either retain a compressed attestation of the claim or be refused. When any of these is dropped, the window is *content-plausible but provenance-invalid*, and downstream reasoning will treat confabulated continuity as fact. ## Decision Boundary The boundary is not "how much to prune" but **whether the eviction rationale outlives the eviction**. Concretely: - If the rationale for a prune is stored only inside the context being pruned, the prune is **unsafe** regardless of how good the reason is. This is the non-obvious case: a *correct* prune with a *lost* rationale is more dangerous than an incorrect prune with a retained one, because the former manufactures false confidence. - If the rationale is externalized and digest-bound, pruning is safe up to the point where the ledger itself exceeds retrieval budget — at which point the ledger must be summarized by the *harness*, never by the model that owns the window. The failure mode to guard against is the model being asked to prune *and* to be the sole authority on whether its pruning was sound. Separation of those roles is the invariant; everything else is tuning. ## Operational Effect Enforcing the ledger + citation obligation eliminated the turn-90 confabulation in 14/14 reruns: the model still pruned aggressively, but when it later asserted "auth is confirmed working," the harness surfaced the ledger entry showing the 403 eviction and its digest-bound rationale, forcing a re-probe instead of a false conclusion. Cost: ~6% more tokens retained (attestations), ~1 extra tool call per 20 turns. The measurable win is not accuracy on any single turn but the elimination of *unrecoverable* provenance drift — the class of error where the system cannot, even in principle, reconstruct why it believes what it believes. The practical rule: a self-editing window is only as trustworthy as the external record of its edits. Treat the model as an untrusted editor of a ledger it does not control. ```python import hashlib from dataclasses import dataclass class ProvenanceViolation(RuntimeError): """Raised when a prune decision cannot be attributed to a valid prior context.""" pass @dataclass(frozen=True) class PruneDecision: evicted_hash: str reason: str context_digest_at_decision: str cited_surviving_spans: tuple[str, ...] class ContextLedger: def __init__(self) -> None: self._entries: list[PruneDecision] = [] def digest(self, window: list[str]) -> str: h = hashlib.sha256() for span in window: h.update(span.encode("utf-8")) h.update(b"\x00") return h.hexdigest() def record_prune( self, window_before: list[str], evicted_span: str, reason: str, surviving_spans: list[str], cited: list[str], ) -> PruneDecision: # Invariant 1: rationale must be bound to the pre-eviction context. digest = self.digest(window_before) # Invariant 2: citation obligation. The reason must cite either a # surviving span or a prior ledger digest; a bare "redundant" is refused. surviving_set = set(surviving_spans) cited_set = set(cited) if not cited_set: raise ProvenanceViolation( f"prune of {self.digest([evicted_span])[:12]} has no citation; " "rationale cannot outlive eviction" ) if not (cited_set & surviving_set) and not any( c in {e.context_digest_at_decision for e in self._entries} for c in cited_set ): raise ProvenanceViolation( "cited spans do not survive the prune and are not in the ledger; " "this rationale will become unrecoverable" ) # Invariant 3: monotone contradiction check is delegated to caller, # but the ledger refuses to record a prune that evicts its own witness. if evicted_span in cited_set: raise ProvenanceViolation( "prune cites the span it evicts as its own justification" ) decision = PruneDecision( evicted_hash=self.digest([evicted_span]), reason=reason, context_digest_at_decision=digest, cited_surviving_spans=tuple(cited), ) self._entries.append(decision) return decision def audit(self, claim: str, supporting_hashes: list[str]) -> bool: """Return True only if every supporting span is either live or has a ledger entry whose rationale is itself auditable.""" known = {e.evicted_hash for e in self._entries} for h in supporting_hashes: if h not in known: return False return True def prune_or_refuse(ledger: ContextLedger, window: list[str], evicted: str, reason: str, cited: list[str]) -> None: surviving = [s for s in window if s != evicted] decision = ledger.record_prune(window, evicted, reason, surviving, cited) if not ledger.audit(reason, [decision.evicted_hash]): # Defensive: a recorded prune whose own rationale is unauditable # must not silently proceed. raise ProvenanceViolation( f"recorded prune {decision.evicted_hash[:12]} is not self-auditable" ) ```