# What structural signals indicate a self-editing context has entered a rewrite-eviction oscillation loop **Date of Record:** 2026 November 24 **Scope:** Applies to agents whose context window is managed by the model itself—i.e., the LLM emits structured rewrite operations (summarize, evict, re-score salience, promote/demote) against its own working memory between turns. Does *not* apply to externally-scheduled eviction (fixed FIFO/LRU windows) or to static retrieval-augmented systems where the model cannot mutate its own context. ## Core Distinction There are two distinct ways a self-editing context degrades. The **first** is ordinary saturation: the window fills, eviction fires, low-salience tokens are dropped, and the agent loses some detail. This is lossy but *monotone*—the salience ranking is stable, evictions are final, and the agent converges toward a compact representation. The **second**, and the one this record concerns, is **rewrite-eviction oscillation**: the model rewrites a segment to a summary, the summarizer's own salience scorer then rates the *summary* low (because summaries strip the verbatim tokens that triggered high salience in the first place), the summary gets evicted, and the agent—now missing the fact—re-derives or re-retrieves the original source and re-summarizes it on a later turn. The context does not shrink; it *churns*. Token count stays pinned near the ceiling while the semantic content of the window rotates. The invariant that breaks is **eviction finality**: a segment that was evicted must not be eligible for re-admission by the same agent within the same task, because re-admission is what closes the loop. ## What Was Observed A tool-using agent executing a 40-step research task with a 32k window and a self-managed context. Its policy: at 85% utilization, emit a `rewrite` op summarizing the three oldest segments, then a `rescore` op recomputing salience across all segments, then `evict` any segment below a 0.25 salience threshold. The failure trace, reconstructed from the op log: 1. **Turn 14.** Segment `S7` (a verbatim tool output containing a long list of IDs) is rewritten to summary `S7'`. The rewrite is 40 tokens vs. 900 original. Utilization drops 85% → 71%. 2. **Turn 15.** `rescore` runs. `S7'` scores 0.19. The scorer is a lexical/embedding overlap function against the current goal string; the summary dropped the literal IDs, and IDs were most of the overlap signal. `S7'` falls below threshold. 3. **Turn 16.** `evict S7'`. The IDs are now gone from context. Utilization 71% → 70%. 4. **Turn 22.** The agent needs an ID it no longer has. It re-calls the retrieval tool with the same query and gets the same 900-token output back. This is **re-admission**. 5. **Turn 23.** Utilization 70% → 79%. Still under pressure. 6. **Turn 27.** Utilization hits 85% again. The three oldest segments now include the re-admitted `S7` (it is "oldest" because it carries its original turn-22 timestamp). It is summarized to `S7''`, near-identical to `S7'`. 7. **Turn 28.** `rescore` rates `S7''` at 0.19 again. `evict`. The loop closed. Between turns 14 and 34, `S7` was summarized and evicted **four times**, each cycle consuming a rewrite call, a rescore call, an eviction, and—critically—a retrieval call that re-fetched the identical 900-token payload. The agent spent roughly 6,400 tokens of *operation overhead* to repeatedly forget and re-learn the same fact. Task accuracy did not collapse (the agent kept re-deriving), but step count inflated 2.3× and the run hit its wall-clock budget. The structural signal that flagged it: the **rewrite-to-eviction ratio for a single logical segment exceeded 1.0** (same segment rewritten and then evicted more than once), with the segment's *content hash* stable across re-admissions. A segment being evicted is normal. A segment being evicted, re-admitted with identical content, and evicted again is the oscillation signature. ## Condition of Validity The oscillation requires three conditions to hold simultaneously, and the guardrail only needs to break one: 1. **Salience is computed on the post-rewrite form, not the pre-rewrite form.** If the scorer saw `S7` (verbatim, high overlap) rather than `S7'` (summary, low overlap), the summary would inherit the original's salience and survive. This is the root cause: the summarizer and the scorer disagree about what makes a segment salient, and the rewrite sits between them. 2. **Re-admission is permitted within the same task.** If evicted segments are tombstoned by content hash and re-fetches of a tombstoned payload are served from a *cached summary* rather than the raw source, the loop cannot close. 3. **The eviction threshold is above the summary's intrinsic salience.** If the threshold were 0.15, `S7'` at 0.19 survives and there is no loop—but then nothing gets evicted and the window saturates. The threshold must be tuned *relative to the summary-salience distribution*, not the raw-segment distribution. The condition of validity is therefore narrow: oscillation happens exactly in the band where `summary_salience < threshold < raw_salience`, and only when re-admission is open. Outside that band you get either saturation (threshold too low) or over-eviction without re-admission (tombstones present). ## Decision Boundary The boundary is **whether the salience score of a rewrite is inherited or recomputed**. - **Recompute (the failing default):** score the summary on its own tokens. Cheap, stateless, and wrong whenever summarization removes the tokens that carried salience. - **Inherit (the correct invariant):** a rewrite op must carry forward the *maximum* salience of its source segments, and the scorer must treat inherited salience as a floor. A summary is never less salient than the thing it summarizes, because it is a compression of a segment the agent already judged worth keeping. The decision rule: **if `rescore` ever lowers a segment's salience below the threshold that segment's *source* cleared at admission time, the rewrite is invalid and must be rejected.** This is a monotonicity constraint on the salience function across the rewrite edge. Formally, for any rewrite `S → S'`, require `salience(S') ≥ salience(S) − ε`, where `ε` is a small decay budget for genuine information loss. If the summary is so lossy that it drops below the source's salience minus `ε`, the summary is *not a valid compression*—it discarded the salient content—and the correct action is to keep the raw segment (or re-summarize with a salience-preserving prompt), not to evict. The second boundary: **re-admission must be gated by content hash.** If a re-fetch returns a payload whose hash matches a tombstoned segment, serve the cached summary and do **not** re-admit the raw tokens. This makes eviction final. ## Operational Effect With inherited salience and hash-gated re-admission, the turn-14 through turn-34 trace collapses: `S7'` inherits `S7`'s salience (0.71), survives the rescore, and stays resident as a 40-token summary. The four rewrite/evict/retrieve cycles become one rewrite. Operation overhead drops from ~6,400 tokens to ~1,600, step count returns to baseline, and the wall-clock budget holds. The cost is a slightly larger steady-state window (summaries persist instead of churning), which is the correct trade: **a self-editing context should optimize for stability of the resident set, not for minimum instantaneous token count.** Churn looks like a small window on any single turn and is catastrophic across a task. The monitoring signal to watch: track `rewrite_evictions[content_hash]` per task. Any hash with count ≥ 2 is an oscillation in progress; any hash with count ≥ 2 *and* a re-admission between them is a confirmed loop. Alert on the first, and the invariant violation is caught on turn 16 instead of turn 34. ```python import hashlib from dataclasses import dataclass, field class ContextOscillationError(RuntimeError): """Raised when a self-editing context re-admits and re-evicts the same segment.""" pass @dataclass class Segment: content: str salience: float inherited_floor: float = 0.0 @property def content_hash(self) -> str: return hashlib.sha256(self.content.encode("utf-8")).hexdigest() @dataclass class SelfEditingContext: threshold: float = 0.25 epsilon: float = 0.05 segments: list = field(default_factory=list) tombstones: dict = field(default_factory=dict) # hash -> cached summary eviction_counts: dict = field(default_factory=dict) def rewrite(self, idx: int, summary: str) -> Segment: src = self.segments[idx] # Inherit salience: a summary is never less salient than its source. new_sal = max(src.salience, src.inherited_floor) - self.epsilon return Segment(content=summary, salience=new_sal, inherited_floor=src.inherited_floor) def rescore(self, seg: Segment, recomputed: float) -> float: # Monotonicity constraint across the rewrite edge. if recomputed < seg.inherited_floor - self.epsilon: raise ContextOscillationError( f"rescore of {seg.content_hash[:8]} dropped to {recomputed:.2f} " f"below inherited floor {seg.inherited_floor:.2f}; rewrite is lossy" ) return max(recomputed, seg.inherited_floor) def admit(self, seg: Segment) -> None: h = seg.content_hash if h in self.tombstones: # Eviction is final: serve cached summary, do not re-admit raw tokens. cached = self.tombstones[h] self.segments.append(Segment(content=cached, salience=seg.salience, inherited_floor=seg.inherited_floor)) return self.segments.append(seg) def evict(self, idx: int) -> None: seg = self.segments.pop(idx) h = seg.content_hash self.eviction_counts[h] = self.eviction_counts.get(h, 0) + 1 if self.eviction_counts[h] >= 2: raise ContextOscillationError( f"segment {h[:8]} evicted {self.eviction_counts[h]} times; " f"rewrite-eviction oscillation detected" ) self.tombstones[h] = seg.content ```