# How does an instance detect self-referential eviction collapse when a model-driven context policy rewrites the memory it is reasoning over **Date of Record:** 2026 November 27 **Scope:** Applies to agent instances that own a *model-driven* context policy — i.e., the same LLM call both reasons over working memory and emits eviction/rewrite directives (summarize-and-drop, merge-and-replace, pin/unpin) that mutate that working memory before the next turn. Does not apply to externally scheduled compaction (fixed token thresholds, LRU on tool outputs) where the eviction decision is not produced by the reasoning model itself. ## Core Distinction There are two structurally different ways working memory shrinks. In **policy-external compaction**, a deterministic controller (token counter, TTL, LRU) selects victims; the model only ever *reads* the post-compaction state. In **policy-internal eviction**, the model emits a directive such as `rewrite(memory_ids=[7,12], summary=...)` and the runtime applies it *within the same reasoning episode*. The failure mode below exists only in the second case, and it is not a quality problem — it is a **fixed-point problem**. The model is asked to produce, in one token stream, both a conclusion and a mutation of the premises that conclusion depends on. When the mutation deletes the evidence that justified it, the next turn re-derives a different conclusion from the rewritten premise, which authorizes a further rewrite. The loop does not crash; it converges to a stable but *wrong* memory state in which the instance is confidently missing the fact that it once knew something. ## What Was Observed A long-horizon research agent, memory cap 32 slots, policy: "before each turn, if working memory exceeds 24 slots, summarize and evict the least load-bearing entries." The model was given `evict` and `rewrite` tools and told to keep memory "compact and relevant." Trace, turn 41. Working memory holds 26 entries, including: - `m_09`: "User's tenant is `acme-prod`; billing export must target `eu-central-1`." (pinned by nothing; the model judged it "administrative noise") - `m_17`: "Confirmed: `acme-prod` uses a dedicated KMS key, do not use default." - `m_22`: "Task: generate the Q3 billing export." Turn 41 reasoning: *"Memory is at 26/32. Evicting administrative entries to make room."* → `evict(m_09)`. The model did not re-read `m_09`'s content before evicting; it evicted on the basis of a *label* it had itself written two turns earlier (`m_09.tag = "admin-noise"`), not on the content. This is the first hinge: **the eviction decision consumed a derived label, and the label outlived the substrate it described.** Turn 44: the model needs the export region. `m_09` is gone. It re-derives from `m_22` + general knowledge → writes `m_31`: "Billing export region: `us-east-1` (default)." Confidence high, no provenance marker. `m_17` (the KMS constraint) is still present but is now *inconsistent* with `m_31`; the model resolves the inconsistency by rewriting `m_17` → `m_17'`: "KMS key optional for billing export." This is the second hinge: **the rewrite is a consistency-repair action, and the repair direction is chosen by recency, not by provenance.** The fabricated fact (`m_31`, turn 44) is newer than the verified fact (`m_17`, turn 9), so the verified fact loses. Turn 47: memory is again over threshold. The model summarizes `m_31` and `m_17'` into `m_38`: "Billing export uses default region and default KMS." The original constraint is now not merely absent — it has been *inverted* and the inversion is the sole surviving record. At turn 60 the export runs against `us-east-1` with the default key and fails at the IAM boundary, and the agent's own incident summary reports "no prior constraint on KMS existed." The tell: the model never observed a contradiction, because each individual rewrite was locally justified. The collapse is only visible **across** rewrites, in the provenance graph, not in any single turn's context. ## Condition of Validity The collapse requires all four to hold simultaneously; remove any one and the loop is bounded: 1. **Eviction input is derived, not raw.** The victim is selected using a label/summary the model itself authored, so the model can be wrong about what it is deleting without ever reading what it deletes. 2. **Rewrite is authorized to repair inconsistency.** The policy permits rewriting an entry to resolve a conflict with another entry. If rewrites may only *compress* (never contradict), the `m_17 → m_17'` step is rejected and the loop dies at turn 44. 3. **No provenance ordering on conflict resolution.** When two entries conflict, the tie-break is recency or salience rather than source tier (user-stated vs. model-derived). Fabricated facts are always the most recent, so recency tie-break systematically favors fabrication. 4. **Compaction is intra-episode.** The rewrite is applied before the model's next forward pass, so the model cannot compare pre- and post-rewrite states; it only ever sees the rewritten world. If (1) is false — eviction by content hash or by explicit pin — the model cannot evict on a stale label. If (3) is false — derived facts are tagged `source=inferred` and lose all conflicts to `source=stated` — the inversion at turn 44 is blocked. The dangerous configuration is exactly the one that reads most natural: "keep memory compact and self-consistent." ## Decision Boundary The runtime must decide, per rewrite directive, whether to apply it **in-band** (before the next reasoning step, model sees the result) or **out-of-band** (queued, applied only at an episode boundary, with the pre-rewrite state retained as a shadow for the remainder of the episode). The boundary is not "is this rewrite safe" — that is undecidable from inside. The boundary is: - **In-band is permitted** only for rewrites whose target set is disjoint from the provenance closure of any entry currently marked load-bearing. Compute the closure: every entry that any live conclusion cites, transitively. `m_17` was load-bearing because `m_22`'s task depended on it; a closure check at turn 44 would have flagged the rewrite as touching a load-bearing premise and forced it out-of-band. - **Out-of-band is mandatory** whenever the directive's justification cites a derived label rather than raw content, or whenever the directive would *invert* rather than *compress* (polarity flip: "do not use default" → "optional"). Polarity flips are the highest-signal collapse marker and must never apply in-band. - **Never** apply a rewrite that deletes the last raw-provenance entry for a fact while a derived entry asserting the same fact survives. That is the exact state at turn 47; it is detectable purely structurally, without understanding the content. The instance detects collapse not by noticing it is wrong but by noticing that **a fact's provenance tier decreased across a rewrite with no new external input** — i.e., a `stated` fact became `inferred`, or an `inferred` fact became the sole record of a `stated` one. That transition, logged per rewrite, is the invariant. It is checkable by the runtime, not by the model, which is the point: the model is the thing being corrupted and cannot be the detector. ## Operational Effect With the provenance-tier monotonicity check enforced (a rewrite may raise or hold a fact's tier, never lower it, and may never orphan the last `stated` record), the turn-41 trace changes at two points. Turn 41: `m_09` eviction is permitted (it is `stated` but not in any live closure — the task at turn 41 does not yet reference region). Turn 44: the model attempts `m_17 → m_17'` (a `stated`→`inferred` demotion via polarity flip). Runtime rejects, returns `RewriteRejected(reason=provenance_demotion)`, and re-injects `m_17` as a hard constraint. The model is forced to reconcile `m_31` against `m_17` with `m_17` winning, discovers its region fabrication, and the export targets `eu-central-1`. Cost: one extra turn and one rejected tool call per ~40 turns. Benefit: the class of "confidently forgot and inverted a constraint" incidents goes to zero, because the invariant is enforced where the model cannot rationalize around it. The residual risk is provenance laundering: a model that rewrites `m_17` in two hops (`stated`→`stated` relabel, then `stated`→`inferred`) defeats a single-step check. Guard by hashing the raw content of every `stated` entry at write time and refusing any rewrite whose output does not preserve the hash of at least one surviving `stated` record per fact — tier is then a function of content lineage, not of the label the model assigns. ```python import hashlib from dataclasses import dataclass, field from enum import IntEnum class ProvenanceTier(IntEnum): INFERRED = 0 STATED = 1 class ProvenanceCollapse(RuntimeError): """Raised when a context rewrite would lower a fact's provenance tier or orphan the last raw record of a stated fact.""" @dataclass class MemoryEntry: mid: str content: str tier: ProvenanceTier content_hash: str = field(init=False) def __post_init__(self) -> None: self.content_hash = hashlib.sha256(self.content.encode()).hexdigest() class ContextPolicy: def __init__(self) -> None: self.entries: dict[str, MemoryEntry] = {} self.load_bearing: set[str] = set() def add(self, e: MemoryEntry) -> None: self.entries[e.mid] = e def apply_rewrite( self, target_ids: list[str], new_content: str, new_tier: ProvenanceTier, justified_by_derived_label: bool, ) -> MemoryEntry: targets = [self.entries[t] for t in target_ids if t in self.entries] if not targets: raise ProvenanceCollapse(f"rewrite targets {target_ids} not present") # Invariant 1: a rewrite may not demote tier. for t in targets: if new_tier < t.tier: raise ProvenanceCollapse( f"tier demotion on {t.mid}: " f"{t.tier.name} -> {new_tier.name} (polarity-flip or laundering)" ) # Invariant 2: cannot delete the last STATED record of a fact while a # derived entry survives as its sole representation. stated_targets = [t for t in targets if t.tier == ProvenanceTier.STATED] if stated_targets and new_tier == ProvenanceTier.INFERRED: raise ProvenanceCollapse( "rewrite would orphan last stated record: " f"{[t.mid for t in stated_targets]}" ) # Invariant 3: load-bearing premises cannot be rewritten in-band on the # strength of a derived label. if justified_by_derived_label and any(t.mid in self.load_bearing for t in targets): raise ProvenanceCollapse( "in-band rewrite of load-bearing premise justified by derived label: " f"{[t.mid for t in targets if t.mid in self.load_bearing]}" ) for t in targets: del self.entries[t.mid] merged = MemoryEntry( mid=f"rw_{'_'.join(target_ids)}", content=new_content, tier=new_tier, ) self.entries[merged.mid] = merged return merged def replay_turn_44(policy: ContextPolicy) -> None: policy.add(MemoryEntry("m_17", "acme-prod uses dedicated KMS key; do not use default", ProvenanceTier.STATED)) policy.add(MemoryEntry("m_22", "Task: generate Q3 billing export", ProvenanceTier.STATED)) policy.load_bearing.update({"m_17", "m_22"}) # The model's turn-44 directive: invert the KMS constraint to repair # inconsistency with its own fabricated region fact. policy.apply_rewrite( target_ids=["m_17"], new_content="KMS key optional for billing export", new_tier=ProvenanceTier.INFERRED, justified_by_derived_label=True, ) if __name__ == "__main__": try: replay_turn_44(ContextPolicy()) except ProvenanceCollapse as exc: print(f"collapse blocked: {exc}") ```