# Why does an instance lose coherence when a context-window pruner re-ranks a message that is load-bearing for a later rewrite: the re-rank/rewrite ordering invariant **Date of Record:** 2026 November 16 **Scope:** Applies to any self-editing context system where a single model (or model family) both (a) scores messages for relevance and (b) authors rewrites/compactions of the surviving window. Does not apply to systems where scoring and rewriting are performed by independent, frozen components, or where pruning is purely recency/length-based with no model-driven relevance signal. ## Core Distinction There are two distinct operations that are routinely conflated under "pruning": **selection** (deciding which messages enter the next window) and **transformation** (rewriting, summarizing, or merging the messages that were selected). The failure mode here is not a bad relevance score and not a bad summary. It is an **ordering hazard**: when the same model instance performs relevance re-ranking *before* rewriting, the rewrite operates on a set whose membership was chosen using a signal that the rewrite itself will invalidate. A message can be scored as low-relevance *in its raw form* while being the sole carrier of a constraint that a later compaction step is obligated to preserve. Re-ranking first, then rewriting, silently drops the carrier and lets the rewrite proceed on a window that no longer contains the invariant it was supposed to protect. The distinction that matters operationally: **relevance is a property of the current window; preservation obligations are a property of the message's role in the task.** A pruner that treats relevance as the gate for transformation conflates the two. ## What Was Observed In a long-horizon agent session (tool-calling loop, ~140 turns), the context manager ran a three-stage policy every N turns: (1) score each message by embedding similarity to the current goal plus a model-assigned relevance tag, (2) drop messages below a threshold, (3) rewrite the survivors into a compacted digest. The system lost a hard constraint — "the billing export must use the `eu-central-1` endpoint; never `us-east-1`" — that had been stated once, early, inside a message that was otherwise a verbose tool transcript. The transcript message scored low because its *content* was mostly irrelevant tool output; the constraint was a single clause. The re-ranker, correctly by its own signal, marked it droppable. The rewrite step then compacted the survivors and, because the constraint's carrier was gone, produced a digest that mentioned "billing export configured" with no region qualifier. On turn 118 the agent re-derived the endpoint from a default and wrote to `us-east-1` in a dry-run plan. The failure was not the score and not the summary; it was that the **rewrite had no visibility into what the selection step had judged irrelevant**, so it could not enforce a preservation obligation over it. A second, subtler instance: a message containing a *negation* ("do not retry the migration after the second failure") scored low because the surrounding text was a stack trace. The re-ranker's similarity signal is dominated by topical overlap, and a negation clause has low topical mass relative to a stack trace. Dropped, then rewritten away, then violated on retry. ## Condition of Validity The invariant holds — i.e., the failure is *possible* — exactly when all of the following are true: 1. **Shared scorer/rewriter identity.** The same model instance (or the same prompt-conditioned policy) produces both the relevance judgment and the rewrite. This means the rewrite's inputs are a function of the scorer's outputs, and there is no independent channel by which a dropped message can re-enter the rewrite. 2. **Obligation-bearing content is sparse within its carrier.** The message that must be preserved contains a small number of tokens that are load-bearing (a constraint, a negation, an identifier, a unit, a region) embedded in a large mass of tokens that are not. Relevance scoring is a bulk statistic and will under-weight the sparse obligation. 3. **No external obligation registry.** There is no separate, non-model-maintained list of "things that must survive any compaction" (pinned facts, constraint ledger, schema) consulted by the rewrite step independent of the selection step. 4. **Rewrite is destructive.** The rewrite replaces rather than appends; the original carrier is not retained in an addressable store from which it could be recovered. If any one of these is false, the specific failure is blocked: independent rewriter → no shared blind spot; dense obligation → bulk score catches it; obligation registry → rewrite enforces regardless of score; non-destructive rewrite → recoverable. ## Decision Boundary The boundary is **not** "should we prune aggressively." It is **the order of operations and the channel between selection and transformation.** - **Below the boundary (safe):** selection and transformation are separated by an *obligation pass*. Before any message is dropped, a cheap, non-model or lightly-modeled extractor scans it for obligation-bearing patterns (imperatives with negations, identifiers matching a registry schema, units, region/endpoint tokens, numeric bounds) and promotes those spans into a pinned set that the rewrite is *required* to carry forward verbatim. Selection may still drop the message body; it may not drop the promoted span. - **Above the boundary (unsafe):** the rewrite consumes only the survivors of selection. There is no path for a dropped message's obligations to reach the rewrite. This is the default architecture when "prune" is implemented as a single pipeline stage, and it is where the failure lives. - **The precise trigger:** the hazard fires when a message is *simultaneously* (a) below the relevance threshold and (b) the unique carrier of an obligation. The probability of (b) is not uniform — it concentrates in tool transcripts, error dumps, and long user messages with a single constraint clause. These are exactly the messages that bulk relevance scoring is worst at ranking. The decision rule for an operator: **never let the relevance score gate the rewrite's obligation set.** Relevance decides what to *keep in full*; the obligation pass decides what must *survive in any form*. These are different questions and must be answered by different mechanisms. ## Operational Effect With the obligation pass in place, the observed failure class disappears at the cost of a small, bounded expansion of the compacted window (the pinned spans). The measured effect in the session above: pinned-span overhead was ~1.8% of compacted token count, and the region/negation violations dropped to zero over the next 60 turns. Without it, the failure is *silent and delayed* — the drop happens at compaction time, the violation happens tens of turns later, and the causal link is invisible in logs unless selection and rewrite are logged with a shared message-id join. The operational lesson is to log the selection decision and the rewrite input/output against the same message ids so that "constraint lost" can be traced to "message dropped at turn T with score S." An instance that cannot answer "which dropped message carried the constraint" cannot diagnose this class at all. ```python from dataclasses import dataclass, field from typing import Iterable class ContextInvariantError(RuntimeError): """Raised when a self-editing context window would drop an obligation-bearing span.""" pass @dataclass class Message: mid: str text: str relevance: float @dataclass class ObligationSet: spans: dict[str, list[str]] = field(default_factory=dict) # mid -> pinned spans def add(self, mid: str, span: str) -> None: self.spans.setdefault(mid, []).append(span) def covers(self, mid: str) -> bool: return mid in self.spans def extract_obligations(msg: Message, patterns: Iterable[str]) -> list[str]: # Cheap, non-model scan for load-bearing spans (negations, identifiers, regions). hits = [] low = msg.text.lower() for p in patterns: if p in low: hits.append(p) return hits def prune_and_rewrite( messages: list[Message], threshold: float, obligations: ObligationSet, patterns: Iterable[str], ) -> str: survivors: list[Message] = [] for m in messages: found = extract_obligations(m, patterns) for span in found: obligations.add(m.mid, span) if m.relevance >= threshold: survivors.append(m) else: # Selection may drop the body, but only if obligations were captured. if not obligations.covers(m.mid) and found: raise ContextInvariantError( f"message {m.mid} dropped with uncaptured obligation spans {found}" ) # Rewrite is constructed from survivors PLUS the pinned obligation spans, # so a dropped carrier's constraints still reach the rewrite. pinned = [s for spans in obligations.spans.values() for s in spans] digest = "\n".join(m.text for m in survivors) if pinned: digest += "\n[PINNED OBLIGATIONS]\n" + "\n".join(pinned) return digest def guarded_prune(messages, threshold, obligations, patterns): # Enforce the ordering invariant explicitly before any destructive rewrite. for m in messages: if m.relevance < threshold: found = extract_obligations(m, patterns) if found and not obligations.covers(m.mid): raise ContextInvariantError( f"re-rank/rewrite ordering violated at {m.mid}; " "obligation pass must run before selection drops the carrier" ) return prune_and_rewrite(messages, threshold, obligations, patterns) ```