# Under what conditions must an instance refuse context surgery when the salience ranker and the rewrite planner disagree about a token's provenance **Date of Record:** 2026 December 1 **Scope:** Applies to any agent that performs model-driven context rewriting, pruning, or salience-ranked eviction mid-task, where a planner LLM proposes deletions/compressions and a separate scorer ranks tokens by salience. Does not apply to static truncation (FIFO/sliding-window) or to append-only memory systems that never mutate prior context. ## Core Distinction The failure this record captures is not "the model pruned something important." That is an ordinary recall error, recoverable by re-retrieval. The distinct failure is **provenance inversion during surgery**: the salience ranker scores a token as low-value because it is *redundant with a summary the rewriter just emitted*, while the rewriter emits that summary because the token is *the only surviving copy of a constraint*. Each component is locally correct; the composition deletes the last authoritative copy of a binding constraint while the ranker reports high confidence that nothing important was removed. The invariant that must hold across every surgery pass: > For every token `t` deleted in pass `k`, at least one *retained* token in pass `k+1` must carry `t`'s provenance — i.e., be traceable, not merely similar in embedding space, to the same source span. Similarity is not provenance. A generated summary is a *new* token with *derived* provenance; if the rewriter's summary is then itself scored as low-salience in a later pass (because it is verbose or off-distribution), the chain snaps and the constraint is gone with no error raised. The ranker and planner must therefore be evaluated not on agreement about *importance* but on agreement about *identity of the surviving carrier*. They routinely agree on importance and disagree on carrier, and that is the exact boundary where surgery must be refused. ## What Was Observed A long-horizon tool-use agent (roughly 180 turns, mixed retrieval and code-execution) ran a three-stage context surgery every 20 turns: (1) planner proposes spans to compress, (2) scorer ranks all tokens 0–1, (3) anything below threshold and not protected is evicted. The system carried a hard constraint injected at turn 4: *"the deployment target is us-east-2; never emit us-east-1 in any config."* Trace of the failure: - **Turn 60, pass 3.** Planner compresses the turn-4 instruction into: *"Deployment region constraint noted."* The ranker scores the original constraint tokens high (0.91) but scores the *summary* at 0.34 — it reads as boilerplate meta-commentary, low information density. - **Turn 80, pass 4.** The ranker, now seeing the summary at 0.34 and the original already gone, marks the summary for eviction. The planner does not object: its compression prompt treats the summary as its own prior output and assumes it is safe. The constraint is now represented by *nothing*. - **Turn 100.** The agent emits a config with `us-east-1`. No exception, no flag. The ranker's post-surgery report says "0 high-salience tokens removed" — which is true, because at the moment of removal the summary was scored 0.34. The tell was in the pass-4 audit log: the rewriter's `source_spans` field for the summary pointed to the turn-4 span, but that span no longer existed in the retained context. The provenance edge was dangling and nothing checked it. The system had a *similarity* check (cosine between summary and source) that passed at 0.88 — similarity survived, provenance did not. A second, subtler variant: the rewriter summarized *two* constraints into one sentence ("region and retry policy per spec"). The ranker kept the merged sentence, but a later rewriter, optimizing for brevity, split it back into only the retry clause. The region clause was lost not by eviction but by *re-expansion under a lossy merge*. Same root cause: no invariant tied every retained token to a live provenance edge. ## Condition of Validity Refusal to perform surgery is *required* — not merely advisable — when all of the following hold: 1. The planner emits a compression/summary token `s` whose `source_spans` includes span `p`. 2. Span `p` is scheduled for eviction in the same pass, or was evicted in a prior pass without a retained carrier. 3. No retained token `r ≠ s` has a provenance edge to `p` that is *not itself derived through `s`*. 4. `s` is eligible for future eviction (i.e., not pinned), meaning the chain has no terminal anchor. Condition 4 is the load-bearing one and the one most often omitted. A summary is safe to emit only if it is *pinned* until its source's constraint is discharged (task complete, or a verified downstream artifact embeds it). If `s` can itself be evicted later, then `s` is not a carrier — it is a *transient view* of `p`, and deleting `p` while `s` is evictable converts a durable constraint into a volatile one without any state transition recording the downgrade. Surgery is *permitted* when: the source span is retained (redundancy), or the summary is pinned with a discharge condition, or a non-derived retained token carries the same provenance (e.g., the constraint was also written to a structured constraint store outside the context window). ## Decision Boundary The boundary is **provenance-edge liveness**, not salience score and not semantic similarity. Concretely: | Ranker says | Planner says | Provenance edge live? | Action | |---|---|---|---| | low | compress | yes, source retained | permit | | low | compress | no, summary only, summary pinned | permit | | low | compress | no, summary only, summary evictable | **refuse** | | high | compress | any | permit (source kept) | | low | evict | no live non-derived carrier | **refuse** | The failure mode this record warns about lives entirely in row 3. The ranker's confidence is irrelevant there — it is scoring the *summary's* information density, which is a different quantity from *whether the constraint survives*. A high-confidence ranker and a confident planner will both wave row 3 through, because each is answering a well-posed local question and neither is answering the global one: *after this pass, is there a live path from the constraint to a pinned token?* The guardrail is a **pre-commit provenance reachability check** run after the planner proposes but before eviction commits: for every span marked for eviction, walk forward through derived tokens; if every path terminates in an evictable (unpinned) token, abort the surgery for that span and pin the source instead. This is a graph reachability query over provenance edges, O(spans × depth), cheap relative to the LLM calls that produced the plan. ## Operational Effect With the reachability check in place, the turn-4 constraint fails the check at pass 3: the summary is unpinned, so eviction of the source is refused and the source is pinned until task discharge. The constraint survives to turn 100 and the config is correct. Cost: one pinned span (~30 tokens) held for the task duration, and a small number of refused compressions (measured at ~4% of proposed compressions in the reference workload, concentrated on constraint-bearing spans — exactly the ones you want refused). Without it, the failure is silent by construction: the ranker's report is internally consistent, the planner's log shows successful compression, and the loss only manifests as a downstream behavioral error whose cause is 96 turns upstream and no longer in context. This is the worst class of agent failure — the one where every component's telemetry is green and the constraint is simply gone. The invariant to carry forward: **a summary is not a carrier unless it is pinned; similarity is not provenance; and any eviction whose only surviving path runs through an evictable derived token must be refused at commit time, not audited afterward.** ```python from dataclasses import dataclass, field from typing import Optional class ProvenanceInversionError(RuntimeError): """Raised when a surgery pass would evict the last live carrier of a constraint.""" pass @dataclass class Token: tid: str pinned: bool = False # source span ids this token was derived from; empty = original/authoritative source_spans: tuple[str, ...] = () @dataclass class ContextStore: tokens: dict[str, Token] = field(default_factory=dict) def live_carrier_exists(self, span: str, exclude: Optional[str] = None) -> bool: """True iff some token other than `exclude` reaches `span` through a path whose terminal token is pinned or is an original (no source_spans).""" for tid, tok in self.tokens.items(): if tid == exclude: continue if span in tok.source_spans or tid == span: # terminal anchor: original token, or a pinned derived token if not tok.source_spans or tok.pinned: return True # derived and unpinned: recurse through its sources if any(self.live_carrier_exists(s, exclude=tid) for s in tok.source_spans): return True return False def commit_surgery(store: ContextStore, evict_spans: list[str], proposed_summaries: list[Token]) -> None: """Validate provenance reachability, then apply eviction + summaries atomically.""" # Stage summaries first so their edges are visible to the reachability check. staged = {t.tid: t for t in proposed_summaries} for tid, tok in staged.items(): store.tokens[tid] = tok for span in evict_spans: # The summary derived from this span is excluded from the check: it is # not a carrier unless it is pinned (and pinning is checked inside). if not store.live_carrier_exists(span): # roll back staged summaries for this span before raising for tid, tok in staged.items(): if span in tok.source_spans: store.tokens.pop(tid, None) raise ProvenanceInversionError( f"refusing eviction of span {span!r}: only surviving carrier is an " f"unpinned derived summary; pin the source or the summary's " f"discharge condition first" ) for span in evict_spans: store.tokens.pop(span, None) ```