# In what scenarios must an instance treat model-driven context pruning as a lossy, non-commutative rewrite rather than a garbage collection pass **Date of Record:** 2026 November 24 **Scope:** Applies to agents that let the model itself decide which spans of its own context window to prune, summarize, or rewrite mid-task (self-editing context). Does not apply to append-only context growth, external retrieval stores, or human-curated memory pipelines where pruning is decided out-of-band. ## Core Distinction The dangerous confusion is between **pruning as deletion** and **pruning as rewrite**. Deletion is idempotent, commutative, and reversible if you keep a log: dropping span A then span B leaves the same residue as dropping B then A, and the residue is a subset of the original. Model-driven pruning is none of these things. The model emits a *replacement* for a region of context — a summary, a compressed paraphrase, a "cleaned up" tool transcript — and that replacement is a new token sequence whose meaning is defined only relative to the tokens it replaced. Two properties break silently: 1. **Non-commutativity.** Pruning span A before span B can yield a different residue than B before A, because the summary of A is generated while B is still present and may absorb facts from B, which are then lost when B is pruned. 2. **Non-idempotence under re-entry.** A summary generated at step *n* and then re-summarized at step *n+k* (because it drifted back into the pruning window) loses a different set of facts on the second pass. Summaries of summaries are not fixed points. The invariant that must hold — and that naive implementations violate — is: *the pruned context must remain a faithful superset of the facts required by every still-open goal, and that superset relation must be checkable without re-running the model.* ## What Was Observed A planner-executor agent running a 14-step remediation task (diagnose failing test → patch → re-run → report) with a 32k window and a self-pruning trigger at 80% occupancy. At step 9 the model was asked to compress the earliest 40% of context. The compression instruction was generic ("summarize the above, preserving what matters for the current task"). The trace: - **Step 9 (prune 1):** The model summarized steps 1–5. The summary correctly retained the failing assertion (`expected 200 got 500`) but *dropped the exact request body* that triggered it, because at step 9 that body looked incidental. - **Step 11 (prune 2):** Occupancy climbed again. The model was asked to compress the region containing the step-9 summary. It re-summarized the summary. The assertion survived; the causal chain (which endpoint, which payload) did not, because the summary had already stripped it and the second-pass model had no token to recover it from. - **Step 13 (verify):** The executor re-ran the test, saw a *different* failure (a 422 from a schema mismatch introduced by the patch), and — lacking the original request body — misattributed it to the patch being incomplete. It patched again. The real bug (the original 500) was never confirmed fixed. The failure was not a hallucination. Every individual summary was locally reasonable. The break was the **composition of two lossy rewrites with no superset check between them**, plus a re-entry that treated a summary as if it were original evidence. ## Condition of Validity The invariant `pruned_context ⊇ required_facts(open_goals)` holds only when all of the following are true: 1. **Prune targets are leaves, not summaries of summaries.** A span that is already a model-generated replacement must be marked `derived=true` and excluded from re-pruning unless its source spans are also re-summarized in the same pass. Re-entry into a derived span is the primary loss vector. 2. **Each open goal carries an explicit fact-set.** Goals must be annotated with the concrete tokens/facts they depend on (assertion strings, IDs, payloads, error codes) so the pruner can be constrained: "you may drop anything not in the union of open-goal fact-sets." 3. **Pruning is logged with provenance.** Every replacement records `(source_span_ids, derived_span_id, model_version, step)`. Without this, a later verifier cannot distinguish "this fact was never in context" from "this fact was pruned." 4. **A post-prune superset check runs before the next tool call.** Cheap check: does the union of open-goal fact-sets still appear (literally or by declared paraphrase) in the residue? If not, abort the prune, not the task. When any of these is absent — especially (1) and (2) — the pruning pass is a lossy rewrite masquerading as garbage collection, and the failure is silent until a downstream step depends on a fact that was quietly removed. ## Decision Boundary An instance must **refuse to prune** (and instead escalate to checkpoint-and-restart, or spill to external memory) when: - The prune target overlaps a span marked `derived=true` and the open-goal fact-sets are not fully re-derivable from the original sources still in window. → *re-entry hazard.* - The union of open-goal fact-sets is not a subset of the post-prune residue under a declared paraphrase check. → *superset violation.* - The model's proposed summary introduces facts not present in the source span (paraphrase drift / confabulation during compression). → *rewrite contamination.* - More than one open goal shares a fact that appears in exactly one span, and that span is a prune candidate. → *single-point-of-loss.* An instance may **proceed** with pruning only when targets are original (non-derived) spans, every open goal's fact-set is either retained verbatim or recoverable from a still-present source, and the provenance log is written before the pruned tokens are dropped. The boundary is not "is the window full" — it is "does the residue provably cover the open goals." ## Operational Effect Treating self-pruning as garbage collection produces **compounding silent loss**: each pass removes facts that only later steps reveal as load-bearing, and because the loss is a paraphrase (not a deletion), there is no tombstone to detect it. The agent's failure signature is not "I forgot" but "I confidently acted on a degraded premise" — a misattributed root cause, a re-patch of an already-correct fix, a verifier that passes a test the original bug would still fail. The guardrail is to make pruning a *checked rewrite*: constrain the pruner by open-goal fact-sets, forbid re-pruning derived spans, log provenance, and run a superset check before the next tool call. When the check fails, the correct recovery is to restore from the provenance log or restart from the last checkpoint — never to let the model "summarize more carefully," because the second summary operates on already-lossy input and cannot recover what the first pass removed. ```python class PruneSupersetViolation(RuntimeError): """Raised when a model-driven context prune would drop a fact required by an open goal.""" pass def apply_prune(prune_span_ids, context, open_goals, provenance_log): # 1. Re-entry hazard: refuse to re-prune derived spans. derived_ids = {s.id for s in context.spans if s.derived} if derived_ids & set(prune_span_ids): raise PruneSupersetViolation( f"prune targets derived spans {derived_ids & set(prune_span_ids)}; " "re-summarizing a summary is lossy and non-idempotent" ) # 2. Build the residue by replacing targets with model-proposed summaries. residue = context.with_replacements(prune_span_ids) # 3. Superset check: every open goal's fact-set must survive in the residue. required = set() for goal in open_goals: required |= set(goal.fact_set) missing = {f for f in required if not residue.contains_fact(f)} if missing: raise PruneSupersetViolation( f"post-prune residue drops required facts {missing}; " "abort prune, restore from provenance or restart from checkpoint" ) # 4. Only commit provenance after the check passes. provenance_log.record(prune_span_ids, residue.new_span_ids, context.model_version) return residue ```