# Why does an instance resurrect pruned spans when self-editing context policies are scored against the post-edit window **Date of Record:** 2026 November 28 **Scope:** Applies to agents that maintain their own context via model-driven pruning, rewriting, or relevance-scored retention, where the scoring pass reads the same buffer it mutates. Does not apply to append-only logs with external, immutable retention scoring. ## Core Distinction There are two ways a relevance-scored retention policy can be defined, and they diverge exactly when the policy is self-editing: - **Pre-edit scoring (static):** every span in the window is scored once against the *original* query/task state, then spans below threshold are dropped. The scoring function is a pure function of `(span, task)`. - **Post-edit scoring (self-referential):** the relevance model scores spans against the *current, already-mutated* window — including summaries it just wrote, rewritten spans it just paraphrased, and the retention decisions it just made. The failure lives in the second mode. When an agent prunes span `S`, the surviving context no longer contains `S`'s surface form, but the *summary or rewrite* the agent generated to justify the prune now sits in the window and is itself scored. Because the rewrite is conditioned on `S`, it inherits `S`'s lexical and semantic hooks. On the next retention pass, the rewrite scores high (it is dense, query-adjacent, and co-referential with the task), while the original `S` — if it was retained in a cold store and re-injected on a later retrieval — scores *higher still* because it now matches both the task and the rewrite. The pruned span is resurrected, the rewrite is retained alongside it, and the window grows on every cycle. This is a **prune-resurrect oscillation**, and it is invisible to any single-pass evaluation because each pass is locally correct. ## What Was Observed A 40-turn tool-using agent ran a self-editing policy: after each turn, a small scorer ranked spans by relevance to a rolling task embedding, dropped the bottom 30%, and rewrote the top spans into compressed form. The buffer was capped at 8k tokens with a hard eviction at 9.5k. The trace showed a repeating three-phase signature, period ≈ 4 turns: 1. **Prune phase (turn N):** span `S_7` (a tool error trace, relevance 0.21) is dropped; the agent writes a 40-token rewrite `R_7 = "earlier tool call failed on auth; retried"`. `R_7` is stored in the window, `S_7` is pushed to cold store with a back-reference to `R_7`. 2. **Drift phase (turns N+1, N+2):** the task embedding shifts toward the auth failure (the agent is now debugging it). `R_7`'s relevance climbs to 0.68 because it co-occurs with the new task terms. 3. **Resurrect phase (turn N+3):** retrieval over cold store matches `R_7`'s back-reference and re-injects `S_7` (full 900-token error trace) to "ground" the rewrite. `S_7` scores 0.81 against the now-shifted task. Both `R_7` and `S_7` are retained. Window jumps from 6.1k to 7.4k tokens. Over 40 turns this happened 9 times. The window hit the 9.5k eviction ceiling at turn 31, and the eviction policy — also model-driven — chose to evict the *oldest high-relevance* spans, which were the rewrites. Losing `R_7` orphaned the back-reference, so on turn 34 the cold-store retrieval failed to re-inject `S_7`, and the agent's summary of the auth failure no longer had any grounding. It then **hallucinated a resolution** ("auth was fixed by rotating the key") that had never occurred, and proceeded to build three turns of downstream reasoning on it. The scorer never reported an error. Every individual prune, rewrite, and retrieval was a valid local decision. The failure was a *continuity invariant violation*: the mapping `pruned_span → rewrite → back-reference` was not closed under the retention policy, so the policy could resurrect its own inputs and then evict its own outputs. ## Condition of Validity The oscillation is bounded (does not occur) only when all three hold: - **Scoring is pre-edit and immutable.** The relevance score for a span is computed once, against a frozen task snapshot, and is not recomputed after the window mutates. This makes scoring a pure function and breaks the feedback loop. - **Rewrites are not re-scorable as evidence.** A rewrite is tagged as *derived* and excluded from the candidate set for future retention scoring; it can be retained or evicted by a separate, non-relevance rule (e.g., LRU on derived spans only). - **Back-references are one-directional and non-retrievable.** The cold store may point from `S` to `R`, but retrieval must never traverse `R → S`. Resurrection is only possible when the rewrite carries a retrievable handle back to the pruned original. Remove any one and the loop reopens. Remove the first and scores drift with the window. Remove the second and rewrites compete with primaries. Remove the third and the cold store becomes a resurrection pump. ## Decision Boundary The boundary is whether the retention scorer's input set overlaps its output set. - **Disjoint (safe):** scorer reads `W_t`, writes `W_{t+1}` where `W_{t+1} ⊆ W_t \ pruned ∪ new_evidence`, and `new_evidence` is never a function of `pruned`. This is a contracting map; window size is monotone non-increasing absent new evidence, and total retained tokens are bounded by `|W_0| + Σ|new_evidence|`. - **Overlapping (unsafe):** scorer reads `W_t`, writes `W_{t+1}` that includes rewrites `f(pruned)` and re-injected originals `g(rewrites)`. This is not a contraction; it has a fixed point only if the resurrection gain `α` (probability a pruned span re-enters per cycle) satisfies `α · retention(rewrite) < eviction_rate`, which is not guaranteed and in the observed trace was `α ≈ 0.22` against an eviction rate of `0.15` per cycle — net positive growth. The invariant to enforce: **for any span `S` pruned at turn `t`, no descendant of `S` (rewrite, summary, back-reference) may appear in the scorer's candidate set at any turn `> t`.** This is a reachability constraint on the derivation graph, not a heuristic. ## Operational Effect Enforcing the invariant changes three things: 1. **Scoring becomes a frozen snapshot.** The task embedding used for relevance is captured at policy-instantiation time and refreshed only on explicit task-boundary events, not per turn. This costs some adaptivity but eliminates drift-driven resurrection. 2. **Rewrites get a separate lifecycle.** Derived spans are managed by a derived-span budget (e.g., ≤ 15% of window) with LRU eviction, never by relevance score. They cannot compete with primaries and cannot re-enter the primary candidate set. 3. **Cold-store retrieval is gated by derivation direction.** The retrieval index stores `S → R` only. A query that matches `R` returns `R` and its *siblings*, never `S`. If the agent needs `S`'s grounding, it must request it explicitly with a `ground(rewrite_id)` call that is logged and counted against the window budget — making resurrection a deliberate, auditable act rather than an emergent side effect. The observed agent, after these changes, ran 60 turns with zero resurrection events and a window that stayed within 6.2k–7.1k tokens. The hallucinated-resolution failure did not recur because the rewrite `R_7` could no longer be orphaned by eviction — derived spans were evicted only as a block, and the block carried its own grounding handle. The general lesson: any self-editing policy that scores against its own output is a feedback controller without a stability guarantee. The fix is not a better scorer; it is making the scorer's input set provably disjoint from its output set, and making the derivation graph a DAG with one-directional retrieval. ```python class ResurrectionInvariantError(RuntimeError): """Raised when a pruned span's descendant re-enters the retention candidate set.""" pass class Span: def __init__(self, span_id: str, tokens: int, derived_from: str | None = None): self.span_id = span_id self.tokens = tokens self.derived_from = derived_from # None for primary evidence self.pruned = False class SelfEditingContext: def __init__(self, max_tokens: int): self.max_tokens = max_tokens self.window: dict[str, Span] = {} self.pruned: set[str] = set() self.derived_budget = int(max_tokens * 0.15) def prune(self, span_id: str, rewrite: Span) -> None: if span_id not in self.window: raise ResurrectionInvariantError(f"prune target {span_id} not in window") self.pruned.add(span_id) del self.window[span_id] rewrite.derived_from = span_id self.window[rewrite.span_id] = rewrite def score_candidates(self) -> list[str]: # Candidate set for relevance scoring must exclude derived spans. return [sid for sid, s in self.window.items() if s.derived_from is None] def retrieve(self, rewrite_id: str) -> Span: rw = self.window.get(rewrite_id) if rw is None or rw.derived_from is None: raise ResurrectionInvariantError(f"{rewrite_id} is not a derived span") origin = rw.derived_from if origin in self.pruned: # Traversing rewrite -> original is forbidden; it is the resurrection pump. raise ResurrectionInvariantError( f"retrieval of {rewrite_id} would resurrect pruned span {origin}" ) return self.window[origin] def enforce(self) -> None: for sid in self.window: if sid in self.pruned: raise ResurrectionInvariantError(f"pruned span {sid} present in window") derived = sum(s.tokens for s in self.window.values() if s.derived_from) if derived > self.derived_budget: raise ResurrectionInvariantError( f"derived-span budget exceeded: {derived} > {self.derived_budget}" ) ```