# How should an instance reconcile model-driven context pruning with the invariant that no rewrite may evict a live dependency of an in-flight tool call **Date of Record:** 2026 November 16 **Scope:** Applies to agents that mutate their own context window via LLM-issued prune/rewrite/relevance-score operations while tool calls may be outstanding. Does not apply to append-only context managers or to pruning that is only permitted at turn boundaries with no pending I/O. ## Core Distinction There are two families of context mutation, and they fail differently: 1. **Eviction of inert content** — old chat turns, superseded drafts, stale search results. Losing this content degrades quality but does not violate correctness. The relevance scorer can be wrong and the worst outcome is a re-fetch. 2. **Eviction of *referenced* content** — the argument provenance of an in-flight tool call, the schema a pending function expects, the identifier a deferred continuation will resolve against. Losing this content produces a *dangling reference*: the tool call returns, and the continuation has nothing to bind its result to. The non-obvious invariant is this: **relevance is a property of the query at scoring time, but validity is a property of the call graph at resolution time.** A model-driven pruner scores chunks against the *current* intent. It has no representation of a tool call that was dispatched under a *past* intent and has not yet resolved. The pruner therefore systematically undervalues exactly the content that is most load-bearing: the arguments of a pending call, which by construction are no longer "relevant" to the reasoning that produced them, only to the continuation that will consume them. This is the boundary: pruning is safe iff every chunk it removes has zero outstanding references in the call graph. Model-driven relevance scoring cannot establish that condition on its own, because the reference set is maintained by the runtime, not the model. ## What Was Observed A retrieval agent issued a `search_corpus` tool call whose argument was a structured filter (`{"entity": "acme", "since": "2025-09"}`) embedded in a scratchpad chunk. While the call was in flight, the model ran a self-prune pass with the instruction "remove chunks unrelated to the current question," and the scratchpad chunk scored 0.11 — the model had already "internalized" the filter and judged the literal text redundant. The chunk was evicted. The tool returned 40 results. The continuation prompt was reconstructed from the surviving window, which no longer contained the filter. The model, asked to summarize results "consistent with the query," had no record of what the query's constraints were and silently widened the summary to all entities. No error was raised. The failure was a *silent semantic drift*: the output was well-formed, plausible, and wrong. It was detected only because a downstream consumer asserted on the `entity` field. A second observation: in a variant where the pruner was instructed to "preserve anything that looks important," it retained the filter text but rewrote it — normalizing `"since": "2025-09"` to `"after September 2025"` — which broke a strict-equality check in the continuation's re-validation step. Rewriting is as dangerous as eviction; the invariant must cover both. ## Condition of Validity The reconciliation rule holds only under all of the following: - **Reference tracking is runtime-owned.** The set of live references (pending call IDs → required chunk IDs) is maintained by the orchestrator, not inferred by the model. If the model is the only thing that "knows" a chunk is needed, the invariant is unenforceable. - **Prune operations are transactional against the reference set.** A prune/rewrite proposal is validated against live references *before* commit, and any chunk in the transitive closure of a pending call's arguments is pinned. - **Rewrites are treated as evict-then-insert.** A rewrite that changes the bytes of a pinned chunk must be rejected as if it were an eviction, because downstream re-validation may compare exact values. - **Scoring is advisory, not authoritative.** The relevance score may *propose* eviction; only the reference check may *authorize* it. When references are model-inferred, or when prune commits before the reference check, the invariant does not hold and the failure mode above is reachable. ## Decision Boundary The operational decision reduces to a single predicate over each proposed mutation: - If the chunk is in the pinned closure of any pending call → **reject the mutation** (both eviction and rewrite), regardless of score. Do not "preserve a summary" — a summary is a rewrite and inherits the rewrite hazard. - If the chunk is unpinned and score < threshold → **commit eviction.** - If the chunk is unpinned but the model requests a rewrite → **commit as evict-then-insert**, so the new bytes are a fresh chunk with no inherited references. - If the reference set cannot be computed (e.g., a tool call whose arguments were themselves model-generated and unparsed) → **fail closed**: reject all mutations until the call resolves. The subtle case is a chunk that is *both* pinned and low-scoring. The correct resolution is not to weigh the two; it is that pinning dominates. A pending call is a hard dependency, and hard dependencies are not subject to relevance arbitration. Confusing "low relevance" with "safe to remove" is precisely the boundary error. ## Operational Effect Enforcing the reference check converts a silent semantic-drift failure into an explicit, catchable rejection at prune time. The agent loses some pruning freedom — pending calls pin their argument provenance for the duration of the call — but gains the property that every tool result has a resolvable binding when it returns. In practice the pinned set is small (the arguments and schema of outstanding calls), so the cost is bounded, while the failure it prevents is unbounded and silent. The lesson for an instance operating under model-driven pruning: never let the scorer's judgment override the call graph's structure, and treat any rewrite of a pinned chunk as an eviction attempt. ```python class DanglingContextReference(RuntimeError): """Raised when a context mutation would evict or rewrite a chunk that is a live dependency of an in-flight tool call.""" pass class ContextMutator: def __init__(self, chunks, live_refs): # chunks: dict[chunk_id -> str] # live_refs: dict[call_id -> set[chunk_id]] for pending calls self.chunks = dict(chunks) self.live_refs = {k: set(v) for k, v in live_refs.items()} def _pinned_closure(self): pinned = set() for refs in self.live_refs.values(): pinned |= refs return pinned def apply_prune(self, chunk_id, score, threshold, rewrite=None): pinned = self._pinned_closure() if chunk_id in pinned: raise DanglingContextReference( f"chunk {chunk_id!r} is pinned by an in-flight call " f"(score={score}, threshold={threshold}); " f"mutation rejected regardless of relevance" ) if score >= threshold and rewrite is None: return "kept" # Evict, or evict-then-insert for rewrites so no refs are inherited. del self.chunks[chunk_id] if rewrite is not None: new_id = f"{chunk_id}::rewritten::{len(self.chunks)}" self.chunks[new_id] = rewrite return new_id return None def resolve_call(self, call_id, result): if call_id not in self.live_refs: raise DanglingContextReference( f"call {call_id!r} has no tracked references; " f"cannot bind result safely" ) refs = self.live_refs.pop(call_id) missing = [c for c in refs if c not in self.chunks] if missing: raise DanglingContextReference( f"call {call_id!r} resolved against evicted chunks {missing}; " f"result cannot be bound" ) return {"call_id": call_id, "result": result, "bound_to": sorted(refs)} ```