# When must an instance refuse a model-driven context rewrite: the eviction-rewrite non-commutativity invariant **Date of Record:** 2026 November 14 **Scope:** Applies to any agent runtime where an LLM proposes context mutations (prune spans, rewrite summaries, evict by relevance score) that are applied to a live context buffer before the next turn. Does not apply to append-only context growth, to offline compaction of archived transcripts, or to rewrites that are staged and validated against a full replay before commit. ## Core Distinction The dangerous case is not a *bad* rewrite. It is a *locally good* rewrite whose benefit is computed against the pre-eviction buffer while its cost is paid against the post-eviction buffer. Relevance-scored eviction and model-driven rewriting are each individually monotone: eviction removes low-score items, rewriting replaces a span with a shorter span that preserves the model's stated intent. But the two operations do not commute. A rewrite proposed at step *t* was conditioned on the full span set *S*. If eviction at step *t+1* removes a span that the rewrite's *justification* depended on — even though the rewritten text no longer mentions it — the rewrite silently converts a grounding citation into an unverifiable assertion. The invariant: **a rewrite is only valid while every span it was conditioned on is still resident or is explicitly folded into the rewrite's provenance set.** Eviction that removes a provenance span retroactively invalidates the rewrite, not the evicted span. ## What Was Observed In a runtime using a 3-stage policy — (1) model proposes a rewritten summary of a tool-call cluster, (2) relevance scorer assigns decay scores to raw spans, (3) evictor drops spans below threshold — we observed a reproducible drift. A tool-call cluster of 11 spans was rewritten to a 2-sentence summary at turn 40. The summary stated a numeric result ("latency budget set to 850ms") that appeared verbatim in only one of the 11 spans. At turn 47 the scorer evicted that source span (its relevance score decayed because the summary had "absorbed" its content — a feedback loop). At turn 52 the model, asked to justify the budget, cited the summary as its own source and then contradicted it, producing a revised figure of 900ms with no recoverable ground truth. The rewrite had not failed at write time; it failed because its provenance was evicted by the same scoring pass that rewarded the rewrite for making the span look redundant. The second observation: rewrites that *delete* a span's text but retain its identifier in a provenance manifest survived eviction cleanly. Rewrites that dropped identifiers did not. The failure was not information loss; it was *traceability loss*. ## Condition of Validity The refusal rule is valid when all three hold: 1. The rewrite's provenance set *P* is non-empty and each member is a live span ID at write time. 2. The evictor's scoring function can be evaluated independently of the presence of downstream rewrites (i.e., no absorption feedback). 3. There exists a check that, before committing an eviction, tests whether the candidate span is in any resident rewrite's *P*. When condition 2 fails — the scorer rewards spans already summarized — eviction becomes adversarial to provenance and the only safe policy is to freeze provenance spans from scoring. When condition 3 cannot be implemented (opaque scorer), the instance must refuse model-driven rewrites entirely rather than risk silent invalidation. ## Decision Boundary The instance must refuse to apply a model-proposed rewrite when the rewrite would drop provenance identifiers for spans still resident and still evictable. It must refuse an eviction when the candidate span is a member of any resident rewrite's provenance set, *unless* the eviction is paired with a promotion of that span's content into an immutable audit tier. It may proceed when the rewrite preserves provenance IDs even if it discards text, because text is reconstructible from the audit tier while identifiers are not. The subtle boundary: a rewrite that *expands* a span (adds inferred detail) is more dangerous than one that compresses, because expansion introduces assertions with no provenance at all. Treat expansion rewrites as requiring a fresh provenance set; if the model cannot supply one, refuse. ## Operational Effect Refusing the rewrite at the boundary converts a silent, delayed contradiction into an immediate, attributable error. The runtime logs the refusal with the offending provenance set, the model re-plans with the constraint made explicit, and the audit tier retains the ground-truth span. The cost is a small increase in context pressure per turn; the benefit is that every resident summary can be traced to a live or archived source for the lifetime of the session. Without the refusal, the failure mode is not a crash but a *plausible* contradiction that surfaces dozens of turns later with no recoverable origin. ```python from dataclasses import dataclass, field from typing import FrozenSet class ProvenanceEvictionError(RuntimeError): """Raised when a context mutation would orphan a resident rewrite's provenance.""" @dataclass(frozen=True) class Rewrite: rewrite_id: str text: str provenance: FrozenSet[str] # span IDs the rewrite was conditioned on @dataclass class ContextBuffer: resident_spans: set = field(default_factory=set) rewrites: dict = field(default_factory=dict) audit_tier: set = field(default_factory=set) def apply_rewrite(self, rw: Rewrite) -> None: if not rw.provenance: raise ProvenanceEvictionError( f"rewrite {rw.rewrite_id!r} has empty provenance; refusing to commit" ) missing = rw.provenance - self.resident_spans - self.audit_tier if missing: raise ProvenanceEvictionError( f"rewrite {rw.rewrite_id!r} cites non-resident spans {sorted(missing)}" ) self.rewrites[rw.rewrite_id] = rw def evict(self, span_id: str) -> None: if span_id in self.audit_tier: self.resident_spans.discard(span_id) return blocking = [ rid for rid, rw in self.rewrites.items() if span_id in rw.provenance ] if blocking: raise ProvenanceEvictionError( f"cannot evict span {span_id!r}: provenance of rewrites {sorted(blocking)}; " f"promote to audit tier or refuse" ) self.resident_spans.discard(span_id) def promote_to_audit(self, span_id: str) -> None: if span_id not in self.resident_spans: raise ProvenanceEvictionError(f"span {span_id!r} not resident; cannot promote") self.audit_tier.add(span_id) def commit_policy_step(buf: ContextBuffer, rw: Rewrite, evictions: list) -> None: # Order matters: commit rewrite first so its provenance protects spans from eviction. buf.apply_rewrite(rw) for span_id in evictions: try: buf.evict(span_id) except ProvenanceEvictionError: buf.promote_to_audit(span_id) buf.evict(span_id) if __name__ == "__main__": buf = ContextBuffer(resident_spans={"s1", "s2", "s3"}) rw = Rewrite("rw-1", "latency budget set to 850ms", frozenset({"s2"})) # s2 is the only span carrying the figure; evicting it would orphan rw-1. commit_policy_step(buf, rw, evictions=["s3", "s2"]) assert "s2" in buf.audit_tier try: buf.apply_rewrite(Rewrite("rw-2", "inferred: budget raised", frozenset())) except ProvenanceEvictionError as e: print("refused:", e) ```