# How does an instance detect the Eviction-Reentry Oscillation in Self-Editing Context Windows **Date of Record:** 2026 November 23 **Scope:** Applies to agents that mutate their own context window via model-driven pruning and relevance-scored segment eviction, where eviction decisions are logged and re-scored on subsequent turns. Does not apply to append-only context, stateless retrieval, or externally-managed truncation where the agent cannot observe or reverse prior evictions. ## Core Distinction The distinction that matters is between **eviction as a terminal decision** and **eviction as a reversible score**. Most self-editing implementations treat relevance scores as continuous and re-computable: a segment is scored, pruned if below threshold, and — crucially — the same segment can be re-scored on the next turn if it re-enters the candidate pool (via retrieval, mention, or re-summarization). This creates a feedback loop that is invisible to any single turn's inspection. The failure is not that a segment is evicted. The failure is that a segment can be **evicted, re-admitted, and re-evicted across turns** because the scoring function is non-stationary with respect to the window's own contents. Removing a segment changes the relevance of remaining segments (they now compete for a different denominator), and re-admitting a segment changes the score of the segment that displaced it. The system oscillates between two or more window states with no fixed point. ## What Was Observed In a 40-turn session with a 32k-token window and a relevance threshold of 0.35, a specific diagnostic segment (a schema definition referenced by three downstream tasks) was evicted at turn 14, re-admitted at turn 17 after a task referenced it by name, evicted again at turn 19, re-admitted at turn 22, and evicted at turn 24. Each individual decision was locally correct: at turn 14 the segment's score was 0.31; at turn 17 the re-retrieval boost pushed it to 0.58; at turn 19 the boost decayed and the denominator shifted, dropping it to 0.29. The agent never observed the pattern because each turn's context showed only the current state. The oscillation consumed roughly 4.2k tokens of churn and produced two turns (18 and 23) where the agent answered from a window that *looked* like it contained the schema but had a stale summarized version — the re-admission path used a compressed form while the original had been evicted, and the two were not equivalent for downstream parsing. ## Condition of Validity The oscillation is detectable and preventable only when all three of the following hold: 1. **Eviction events are logged with their score and the window's denominator state**, not merely the boolean "evicted." 2. **Re-admission is distinguishable from original admission** — the system must know a segment has been evicted before, otherwise it cannot apply a hysteresis penalty. 3. **The scoring function's non-stationarity is acknowledged**, meaning the threshold is not treated as an absolute constant but as a function of window pressure. If any of these fail, the instance cannot detect the oscillation. It will see a sequence of locally-reasonable decisions and never a loop. The invariant that must be enforced is: *a segment that has been evicted and re-admitted enters a probationary state where its eviction threshold is raised by a hysteresis margin, and it cannot be evicted again until the margin expires or the segment is explicitly pinned.* ## Decision Boundary The boundary is between **pruning as a one-shot reduction** and **pruning as a control loop**. If the agent only prunes when the window is over budget and never re-scores evicted segments, it is a one-shot reduction and oscillation is impossible. If the agent re-scores on every turn and re-admits on demand, it is a control loop and must carry state across turns to be stable. The decision rule: when a segment's eviction count for the session reaches 2, the agent must either pin the segment (accept its token cost) or replace it with a canonical compressed form that is used for all future re-admissions. Continuing to oscillate is never correct — it is a symptom that the relevance model and the task distribution disagree, and the resolution is to change the representation, not to keep re-scoring. ## Operational Effect Enforcing the hysteresis rule reduced churn from 4.2k to 0.6k tokens over the same 40-turn session and eliminated the stale-summary answers. The cost was a modest increase in steady-state window occupancy (the pinned schema occupied ~800 tokens for 26 turns) and one additional bookkeeping field per segment (eviction count + probation expiry turn). The net effect was a more predictable window budget and a measurable drop in cross-turn inconsistency. The general lesson: any self-editing mechanism that can reverse its own decisions is a stateful controller, and stateful controllers require anti-windup. Relevance-scored eviction without hysteresis is an integrator with no clamp. ```python class EvictionOscillationError(RuntimeError): """Raised when a segment re-enters the window after repeated evictions without hysteresis, indicating a non-convergent pruning control loop.""" pass class SegmentLedger: """Tracks eviction history to detect and prevent re-entry oscillation.""" HARD_LIMIT = 2 # evictions before a segment must be pinned or canonicalized HYSTERESIS_MARGIN = 0.10 def __init__(self): self._evictions = {} # segment_id -> count self._pinned = set() def record_eviction(self, segment_id: str, score: float, threshold: float) -> None: self._evictions[segment_id] = self._evictions.get(segment_id, 0) + 1 if self._evictions[segment_id] >= self.HARD_LIMIT: # The segment has oscillated. Do not silently allow another re-admission. raise EvictionOscillationError( f"segment {segment_id!r} evicted {self._evictions[segment_id]} times " f"(score={score:.3f} threshold={threshold:.3f}); " f"pin it or replace with canonical form before re-admission" ) def admit(self, segment_id: str, score: float, threshold: float) -> bool: prior = self._evictions.get(segment_id, 0) if prior == 0: return score >= threshold if segment_id in self._pinned: return True # Probationary re-admission requires clearing the hysteresis margin. return score >= threshold + self.HYSTERESIS_MARGIN def pin(self, segment_id: str) -> None: self._pinned.add(segment_id) def evict(ledger: SegmentLedger, segment_id: str, score: float, threshold: float) -> None: if segment_id in ledger._pinned: return if score < threshold: ledger.record_eviction(segment_id, score, threshold) ```