# Where must an instance halt in-place context rewriting when the eviction predicate is non-monotonic across the rewrite pass **Date of Record:** 2026 November 23 **Scope:** Applies to agent instances that mutate their own live context buffer (in-place summarization, stale-span eviction, span compaction) during a multi-step task. Does not apply to append-only context growth, external memory stores, or post-hoc transcript summarization performed outside the active reasoning loop. ## Core Distinction The failure is not "the agent forgot something." It is that the **eviction predicate evaluated during a rewrite pass can be invalidated by the rewrite pass itself**, and the rewrite engine treats eviction as monotonic when it is not. Most self-editing context policies assume a *monotone* eviction predicate: once a span is judged stale, it stays stale, and once a span is judged live, removing other spans cannot make it stale. Under that assumption, a single forward pass over the buffer is safe — you scan, mark, and compact. The assumption breaks when the predicate depends on *other spans still in the buffer*. A span judged "live" because it is the sole anchor for a fact can become "orphaned" the moment a *different* span that referenced it is evicted in the same pass. The rewrite then leaves behind a live-marked span whose only support was deleted earlier in the same traversal. This is a **non-monotonic eviction predicate**: `stale(s, B) = true` for buffer `B` does not imply `stale(s, B') = true` for `B' ⊂ B`, and — critically — `live(s, B) = true` does not imply `live(s, B \ {t}) = true`. Forward compaction over such a predicate produces a buffer that is neither the pre-state nor a valid fixpoint: it is a *partially rewritten* state containing dangling live spans. ## What Was Observed A planning agent held a 14-span context. Span `S7` was a task constraint ("output must be CSV, not JSON"). Span `S11` was a restatement of that constraint inside a tool-result echo. The eviction policy scored spans by (a) recency, (b) reference count from other spans, and (c) whether the span is the *unique* carrier of a constraint. During a mid-task compaction pass, traversal reached `S11` first (higher index, scanned in reverse). `S11` scored low on uniqueness because `S7` also carried the constraint — reference count 1, uniqueness false → evicted. Traversal continued to `S7`. `S7` now scored low on reference count (the only span that had referenced it, `S11`, was already removed from the working buffer used for scoring) and was judged a redundant restatement → evicted. Both carriers gone. The final buffer contained zero CSV constraints, and the agent emitted JSON on the next tool call. The trace is the tell: the scoring function read the *mutating* buffer, not a snapshot. Each eviction updated the reference graph that the next eviction decision depended on. The pass was order-dependent, and the order it chose happened to delete the anchor before the dependent. ## Condition of Validity The single-pass forward compaction is valid **only if** the eviction predicate is monotone with respect to span removal — formally, if `live(s, B)` holds, then `live(s, B \ {t})` must hold for all `t ≠ s` not equal to `s`. Equivalently: no span's liveness may depend on the presence of another evictable span. This condition holds for pure recency or pure token-budget eviction (removing a span never changes another span's age or token count in a way that flips liveness). It fails for any predicate that includes: - **reference-count or anchor-dependency** terms (the `S7`/`S11` case), - **coverage/support** terms ("this span is live because it is corroborated by ≥1 other span"), - **contradiction-resolution** terms ("keep the newer of two conflicting spans" — evicting the newer can resurrect the older as sole carrier). When any such term is present, the rewrite must be performed against a **frozen snapshot** for scoring and applied as a set-difference, not as an in-place mutation interleaved with scoring. ## Decision Boundary The boundary is precise: **halt the in-place pass and fall back to snapshot-diff compaction the moment the scoring function reads a span's neighbor set.** Concretely, an instance must refuse in-place rewriting if the liveness score of any span is a function of `B \ {s}` rather than of `s` alone plus immutable metadata. Two operational rules bound this: 1. **Snapshot-then-apply.** Compute all liveness scores against an immutable copy `B₀`. Compute the evict set `E = {s : stale(s, B₀)}`. Apply `B₁ = B₀ \ E` in one step. Never re-score mid-apply. 2. **Fixpoint check.** After applying, re-evaluate `stale` over `B₁`. If any span in `B₁` is now stale that was not stale in `B₀` (a *newly orphaned* span), the predicate was non-monotonic and the pass is unsound — the instance must either retain the newly-orphaned spans (they are live-by-default) or escalate to a full re-summarization rather than silently dropping them. The dangerous middle ground — which is what most self-editing policies actually implement — is re-scoring against the mutating buffer and calling the result "adaptive." It is not adaptive; it is order-dependent, and the order is an accident of traversal direction. ## Operational Effect An instance that violates this invariant does not fail loudly. It produces a context that *looks* compacted and coherent, passes a superficial self-check ("did I keep the important stuff?"), and then diverges one or two tool calls later when the missing constraint surfaces as a wrong-format output or a re-litigated decision. The failure is attributed to "the model forgot," masking the actual mechanism: the rewrite engine deleted a liveness anchor and then deleted the span that anchor supported. The guardrail is the snapshot-diff discipline plus the post-apply fixpoint check. The fixpoint check is the load-bearing part: it converts a silent partial rewrite into a detectable invariant violation, which is the difference between a recoverable compaction and a cascading reasoning divergence. ```python from dataclasses import dataclass, field from typing import Callable class NonMonotonicEvictionError(RuntimeError): """Raised when an in-place context rewrite evicts a span whose liveness depended on another span evicted earlier in the same pass.""" @dataclass class Span: id: str text: str anchors: set[str] = field(default_factory=set) # ids this span supports def snapshot_evict( buffer: list[Span], is_stale: Callable[[Span, list[Span]], bool], ) -> list[Span]: """Sound compaction: score against a frozen snapshot, apply as set-difference.""" snapshot = list(buffer) evict_ids = {s.id for s in snapshot if is_stale(s, snapshot)} rewritten = [s for s in snapshot if s.id not in evict_ids] # Fixpoint check: no span may become newly stale after the apply. newly_orphaned = [ s.id for s in rewritten if is_stale(s, rewritten) and not is_stale(s, snapshot) ] if newly_orphaned: raise NonMonotonicEvictionError( f"eviction predicate non-monotonic: spans {newly_orphaned} " f"became stale only after eviction; rewrite unsound, " f"escalate to full re-summarization" ) return rewritten def in_place_evict( buffer: list[Span], is_stale: Callable[[Span, list[Span]], bool], ) -> list[Span]: """Unsound: re-scores against the mutating buffer. Kept to demonstrate the bug.""" working = list(buffer) for span in reversed(working): # reverse traversal: the S11-before-S7 hazard if is_stale(span, working): working.remove(span) # NOTE: 'working' is now mutated; the next is_stale call sees a # buffer missing the span we just removed. If span supported a # later-scanned span's liveness, that span is now orphaned. # No fixpoint check: orphaned live spans silently survive or silently drop. return working ```