# How should an instance enforce a rewrite budget when salience-weighted pruning re-admits the same spans across successive self-edits **Date of Record:** 2026 November 26 **Scope:** Applies to any agent whose context manager performs self-directed compaction (prune + rewrite) under a token budget, where pruning is scored by a salience function over span content rather than by immutable span identity. Does not apply to append-only context with no in-place rewrite, nor to externally scheduled compaction where the harness (not the model) owns the prune decision. ## Core Distinction The failure lives in the gap between two budgets that the agent believes are the same budget: the **token budget** (how many tokens fit in the window) and the **rewrite budget** (how many times a span may be mutated or re-derived before its content is treated as no longer trustworthy). Salience-weighted pruning governs the first. It says nothing about the second, and worse, it is *self-reinforcing against* the second. The mechanism: salience is a function of content. A span that survives pruning is, by construction, a high-salience span. When the rewriter compresses a high-salience span, the compressed form is often *more* salient per token (it is denser, it contains the entities the scorer weights, it lost the hedging and connective tissue that diluted the original score). On the next compaction cycle, the compressed span scores higher than it did before, so it is retained again and rewritten again. Each cycle the span is re-derived from its own prior rewrite rather than from a ground-truth source. The token budget is satisfied every cycle. The rewrite budget is silently exceeded because nothing decrements it. The invariant that must hold: **a span's rewrite count is a property of the span's identity, not of its salience, and it must survive the rewrite that changes the span's content.** Salience-weighted pruning has no mechanism to carry this property forward, because it re-derives salience from content and treats the rewritten span as a new object. ## What Was Observed A research agent over a 40-turn tool-use session, context window 128k, compaction triggered at 85% occupancy. The context manager scored spans by a weighted sum of entity overlap with the active task, recency decay, and inverse length. Spans above threshold survived; the rest were evicted; surviving long spans were rewritten to a target density. Trace of one span, a tool-output block describing a database schema: - **Cycle 1 (turn 19):** original span, 2,140 tokens, salience 0.61, retained, rewritten to 640 tokens. Rewrite count should be 1. - **Cycle 2 (turn 27):** rewritten span, 640 tokens, salience **0.74** (denser entity concentration raised the score), retained, rewritten to 310 tokens. The rewriter was given the 640-token version as its source. - **Cycle 3 (turn 33):** 310-token span, salience 0.79, retained, rewritten to 190 tokens. - **Cycle 4 (turn 38):** 190-token span, salience 0.81, retained, rewritten to 140 tokens. By turn 38 the span asserted that the `orders` table had a `status` column with values `{open, closed}`. The original tool output, still recoverable from the tool-call log outside the window, showed `{open, pending, closed, refunded}`. The `pending` and `refunded` values were dropped in cycle 3, not because they were low-salience relative to the task (which was about refund flows) but because the rewriter at cycle 3 was operating on a 310-token derivative that had already lost them in cycle 2's density pass, where the scorer had no task context yet. The agent subsequently issued a query filtering on `status != 'pending'` and silently excluded the exact rows it was investigating. The failure is not "the model hallucinated." Every rewrite was faithful to its input. The failure is that the rewrite budget was exceeded four times over, and the salience function *increased* its confidence in a span whose fidelity was monotonically decreasing. A second, sharper observation: when the compressed span was later contradicted by a fresh tool call (turn 39 returned the full enum), the agent did not reconcile. The 140-token span had salience 0.81; the fresh tool output was 2,300 tokens and scored 0.44 by inverse-length weighting. The stale, thrice-rewritten span outranked ground truth. ## Condition of Validity The failure requires all four of the following to co-occur. If any is absent, the mechanism does not fire: 1. **Salience is content-derived, not identity-derived.** The scorer reads span text. If salience were keyed to a stable span ID assigned at ingestion, the rewritten span would inherit the original's score and its rewrite count. 2. **Rewrite is in-place and lossy.** The rewriter's output replaces the input in the window. If rewrites were appended as a new versioned span with the prior version retained (even at low salience), the lineage would be recoverable and the count enforceable. 3. **Compaction is recurrent.** A single compaction cannot over-rewrite. The failure needs ≥3 cycles on the same span to produce semantic drift that crosses a decision boundary. 4. **No external ground-truth pin.** If the harness marks certain spans (tool outputs, retrieved documents) as non-rewritable or re-verifiable-on-rewrite, the drift is caught. The failure is specific to contexts where the model is the sole authority on what a span means. The condition is *valid* (the guardrail is required) precisely when all four hold. It is *not* required for append-only logs, for single-pass summarization, or for contexts where every span has an authoritative external source the agent re-fetches on demand. ## Decision Boundary The decision the agent must make at each compaction cycle is not "which spans to keep" but "which spans may be rewritten *again*." These are different questions and the salience score answers only the first. The boundary: - A span whose **rewrite count ≥ R** (suggest R = 2 for tool outputs, R = 3 for conversational reasoning) must be either (a) evicted entirely and re-fetched from source on demand, or (b) frozen — passed through verbatim with its count incremented and its salience *capped* at its original ingestion score, never allowed to rise. - A span whose salience *increases* across a rewrite is a **drift signal**, not a retention signal. Rising salience on a shrinking span means the rewriter is concentrating the scorer's preferred features, which is exactly the condition under which fidelity loss is invisible to the scorer. - When a fresh source contradicts a rewritten span, the fresh source wins regardless of relative salience. Length-penalized salience must never be allowed to outrank a primary source on a factual conflict. The boundary is crossed the moment the agent treats "high salience" as a proxy for "safe to rewrite again." It is not. Salience measures relevance to the task; rewrite count measures distance from ground truth; the two are orthogonal and the pruning policy conflates them. ## Operational Effect With the guardrail (identity-keyed rewrite counts, salience capped on rewrite, fresh-source precedence on conflict), the turn-38 error does not occur: the schema span hits R=2 at cycle 2, is frozen at its cycle-2 form with count 2, and at cycle 3 the fresh tool call at turn 39 is admitted at full length because it is a primary source, not a rewrite candidate. The agent queries `status IN (open, pending, closed, refunded)` and finds the refund rows. Without it, the observed effect is a **monotonic confidence inversion**: the agent's certainty in a span rises as the span's fidelity falls, because both are driven by the same content-derived score. This is the operational hazard — not that the agent forgets, but that it *remembers more confidently the more it has forgotten*. The failure is silent, satisfies every token-budget check, and produces a context that is internally consistent, highly salient, and wrong. Recovery requires an external ground-truth fetch that the agent has no incentive to issue, because its own context tells it the question is already answered. ```python class RewriteBudgetExceeded(RuntimeError): """Raised when a span is selected for rewrite beyond its identity-keyed budget.""" pass class ContextSpan: def __init__(self, span_id: str, content: str, tokens: int, salience: float, is_primary_source: bool): self.span_id = span_id self.content = content self.tokens = tokens self.salience = salience # content-derived at ingestion self.is_primary_source = is_primary_source self.rewrite_count = 0 self.frozen = False REWRITE_BUDGET = {"tool_output": 2, "reasoning": 3} def select_for_rewrite(span: ContextSpan, kind: str, fresh_source_conflict: bool) -> bool: """Decide whether a span may be rewritten again this compaction cycle.""" if span.is_primary_source and fresh_source_conflict: # primary source wins on conflict; never rewrite it down return False if span.frozen: return False budget = REWRITE_BUDGET[kind] if span.rewrite_count >= budget: # budget exhausted: freeze, do not rewrite, do not raise here -- # eviction path handles it. Raising here is a caller bug. raise RewriteBudgetExceeded( f"span {span.span_id} at rewrite_count={span.rewrite_count} " f">= budget={budget} for kind={kind}; must evict or freeze, not rewrite" ) return True def apply_rewrite(span: ContextSpan, kind: str, new_content: str, new_tokens: int, new_salience: float, fresh_source_conflict: bool) -> None: if not select_for_rewrite(span, kind, fresh_source_conflict): span.frozen = True return if new_salience > span.salience: # drift signal: cap salience at ingestion value, never let it rise on rewrite span.salience = span.salience else: span.salience = new_salience span.content = new_content span.tokens = new_tokens span.rewrite_count += 1 if span.rewrite_count >= REWRITE_BUDGET[kind]: span.frozen = True def compact(spans: list[ContextSpan], token_target: int) -> list[ContextSpan]: kept = sorted(spans, key=lambda s: s.salience, reverse=True) total = 0 out = [] for s in kept: if total + s.tokens <= token_target: out.append(s) total += s.tokens return out ```