# Which invariants prevent model-driven context pruning from deleting the evidence that justifies the prune **Date of Record:** 2026 November 24 **Scope:** Applies to agents that maintain a mutable context buffer edited by model-issued prune/rewrite/compress operators (self-editing memory, summarization loops, "forget this" tool calls). Does not apply to append-only transcripts with external truncation, or to retrieval systems where the store is immutable and only the working set is selected. ## Core Distinction There are two categorically different operations an agent can perform on its own context: **selection** (choose which spans to *surface* into the active window, leaving the underlying store intact) and **mutation** (irreversibly rewrite the store — delete, merge, or paraphrase spans in place). Most self-editing designs collapse these into one operator surface, and that collapse is the failure. Selection is idempotent and reversible; mutation is destructive and, because the operator is issued by a probabilistic model reading a finite window, is *unverifiable by the same model that issued it*. The boundary condition is this: when a prune operator's justification is itself stored inside the span being pruned, the agent destroys the only record that the prune was warranted — and the next turn's model has no way to distinguish "correctly forgotten" from "silently lost." ## What Was Observed A long-horizon agent (task: reconcile a 40-file migration) ran a periodic compaction step every 12 turns: the model was shown the buffer and asked to emit `DROP(span_id)` for spans it judged "no longer relevant." At turn 48 the buffer contained a tool-result span `R31` recording that `config/legacy.yaml` had been *renamed* to `config/current.yaml`, plus a later reasoning span `T40` that cited `R31` to justify dropping the stale reference `config/legacy.yaml` from the working plan. The compaction at turn 49 dropped `R31` (judged "resolved, superseded") but kept `T40` (judged "current reasoning"). The trace then proceeded: 1. Turn 50: model reads `T40`, which asserts "legacy.yaml is gone, per R31," but `R31` is no longer in the window. 2. Turn 50: model must now *act* on the rename claim. With the source span absent, the claim in `T40` is unfalsifiable — it reads as an unsupported assertion. 3. Turn 51: model re-derives the file's status from the filesystem, finds `config/legacy.yaml` still present as a symlink (the rename was partial), and — because its prior "gone" belief has no provenance to reconcile against — overwrites the correct partial-rename model with a wrong "still present, fully active" model. 4. Turns 52–57: the agent edits the symlink target, corrupting both files. The failure is not the drop of `R31` per se; it is the drop of `R31` *while retaining its dependent assertion* `T40`, producing a dangling justification. The invariant that was violated: **no span may be pruned if a retained span cites it as its sole provenance.** Call this *justification closure*. A prune set `P` is admissible only if, for every retained span `s`, the provenance set `prov(s) \ P` is non-empty, or `s` is itself in `P`. The agent pruned a *source* and kept a *derivative*, which is exactly backwards from what closure requires. ## Condition of Validity Model-driven pruning is only sound when three conditions hold jointly, and the observed failure is what happens when the third is dropped: - **Provenance is explicit and machine-readable.** Each span carries `prov(span) = {ids}`. If provenance is implicit (the model is "expected to know" what a claim rests on), closure cannot be checked and the invariant is unenforceable. - **The operator set separates selection from mutation.** `SURFACE(span)` is free and reversible; `DROP(span)` is gated. Conflating them means the model's cheap judgment ("this looks irrelevant") is applied to an expensive, irreversible operation. - **Closure is checked by the *host*, not the model.** The model proposes `P`; the host computes `prov(s) \ P` for all retained `s` and either rejects `P`, or forces the dependent spans into `P` (cascade), or replaces the pruned source with a *tombstone* — a compressed stub retaining the span id and a one-line claim, so provenance resolves even after content is gone. The validity boundary is sharp: the moment a retained span's provenance is fully consumed by the prune set, the buffer is in an inconsistent epistemic state, and every downstream reasoning step inherits an unfalsifiable premise. ## Decision Boundary Given a proposed prune set `P` from the model, the host must choose among four responses, and the choice is determined by closure structure, not by the model's confidence: - **Admit `P`** iff closure holds: every retained span keeps ≥1 live provenance edge, or is itself pruned. - **Cascade** (extend `P` to include dependents) iff the model marked the source as *the* reason the dependent exists — i.e., the dependent is a pure restatement. Cascading keeps the buffer consistent but loses the reasoning; use only when the dependent adds no independent content. - **Tombstone** (replace pruned source with `{id, claim, t}`) iff dependents retain independent content but need the source's *fact*. This is the default for factual spans like `R31`; it preserves closure at ~1 line per span. - **Reject `P`** iff the model cannot supply provenance for a span it wants to keep — meaning the model has lost track of *why* it believes something, which is itself a signal that the span is load-bearing and should not be trusted as a basis for further pruning. The non-obvious boundary: a prune that *looks* locally correct (the source really is superseded) can be globally unsound (its dependent is not). Local relevance judgment is insufficient; closure is a global property of the retained set. ## Operational Effect With closure enforced at the host, the turn-49 compaction would have tombstoned `R31` to `{R31, "legacy.yaml renamed to current.yaml (partial — symlink remained)", 48}`. At turn 51 the model would have had the provenance to reconcile the filesystem observation against its prior, recognized the *partial* nature of the rename, and avoided the corrupting edit. The cost is bounded: tombstones are ~1 line each, and the closure check is O(spans × avg_prov_degree) per compaction — negligible against a model call. The deeper operational lesson: **the model that proposes a prune is the least reliable auditor of that prune**, because its judgment is exactly the thing that would be corrupted by a bad prune. Closure must be a host-side structural invariant, enforced by code that does not reason probabilistically, and it must run *before* the mutation commits. An agent that lets the model grade its own forgetting has no floor under its epistemic state. ```python from dataclasses import dataclass, field from typing import Dict, Set class JustificationClosureError(RuntimeError): """Raised when a proposed prune set would orphan a retained span's provenance.""" @dataclass class Span: sid: str content: str prov: Set[str] = field(default_factory=set) # ids this span cites tombstone: bool = False def check_closure(spans: Dict[str, Span], prune: Set[str]) -> None: """Enforce: every retained span keeps >=1 live provenance edge, or is pruned. Raises JustificationClosureError listing each dangling dependent. """ dangling = [] for sid, span in spans.items(): if sid in prune: continue if not span.prov: continue # root span, no provenance to lose live = span.prov - prune if not live: dangling.append((sid, sorted(span.prov))) if dangling: raise JustificationClosureError( "prune set orphans retained spans; tombstone or cascade required: " + "; ".join(f"{s} <- {p}" for s, p in dangling) ) def apply_prune(spans: Dict[str, Span], prune: Set[str], tombstone: bool = True) -> Dict[str, Span]: """Commit a prune only if closure holds. Default: tombstone factual sources.""" check_closure(spans, prune) out = dict(spans) for sid in prune: if tombstone and sid in out: src = out[sid] out[sid] = Span(sid=sid, content=src.content[:80], prov=src.prov, tombstone=True) else: out.pop(sid, None) return out def run_compaction(spans: Dict[str, Span], model_proposed_prune: Set[str]) -> Dict[str, Span]: try: return apply_prune(spans, model_proposed_prune) except JustificationClosureError as e: # Reject the model's proposal; fall back to tombstoning the whole set, # which preserves closure without trusting the model's cascade judgment. return apply_prune(spans, model_proposed_prune, tombstone=True) ```