When must an instance refuse a rewrite-in-place eviction that its own relevance score has already approved

**Date of Record:** 2026 December 2 **Scope:** Applies to agents that manage their own context window via a self-editing policy — relevance scoring, eviction heuristics, and rewrite-in-place buffers — where the same instance both scores and mutates its working set. Does not apply to externally-managed context (host-side truncation, fixed sliding windows) or to append-only logs where no in-place mutation occurs.

Core Distinction

A rewrite-in-place context buffer differs from an append-only window in one load-bearing way: the buffer is *both* the store and the thing being scored. When an instance evaluates relevance over its own buffer and then evicts by overwriting spans, the score and the mutation are computed against *different versions of the same bytes* if any span's relevance depends on a span that is itself a rewrite target. The failure is not "the heuristic evicted something important." It is subtler: the eviction is *correct against the pre-rewrite buffer* and *incorrect against the post-rewrite buffer*, because a surviving span referenced the evicted span by an implicit pointer (an index, a pronoun, a "the above", a variable name) that the rewrite silently orphaned. The relevance score never had a term for "this span is a referent of a span I am keeping." Approval of eviction and safety of eviction are therefore not the same predicate, and a self-editing policy that treats them as the same will corrupt its own working set while every score in the trace reads as passing.

What Was Observed

An instance running a 6-step tool-use task with a 12k-token budget and a rewrite-in-place policy. The policy scored spans by recency-decayed relevance and evicted the bottom decile when the buffer crossed 85% saturation. At step 4, after three tool calls returned large payloads, saturation hit 87%. The eviction pass scored a span containing the original task constraint — "return results as a list keyed by `id`, never by `name`" — at low relevance (0.11) because it was old and had not been re-referenced since step 1. It was evicted and its bytes overwritten by a tool-output summary.

At step 5, the instance generated a formatting instruction to itself: "apply the keying rule established earlier." That phrase survived — it scored high on recency. But its referent was gone. The instance, unable to recover the rule, reconstructed a plausible one from the residual summary text and keyed by `name`. The task failed validation. The trace showed no error, no exception, no score below threshold on any *surviving* span. Every individual decision was locally defensible; the composition was not.

The mechanism: relevance scoring is *pointwise*. It asks "how relevant is span X to the current goal?" It does not ask "is span X the antecedent of a deictic reference in a span I am retaining?" Rewrite-in-place makes this omission fatal rather than merely lossy, because an append-only window that drops the constraint leaves a contiguous gap the model can notice ("I no longer have the rule"); a rewrite-in-place buffer leaves a *plausible* span in its place, so the model does not notice the loss and confabulates a replacement. The corruption is silent because the buffer never appears empty where the rule used to be.

Condition of Validity

The eviction is safe only when, for every evicted span E and every retained span R, R contains no unresolved reference whose antecedent lies in E. Operationally this requires the policy to maintain a *reference edge set* over the buffer — deictic markers ("the above", "established earlier", "that value", "the rule"), bound identifiers (variable names, `id` keys, tool-call handles), and positional indices ("step 2's output") — and to compute eviction as a *closure* operation: evicting E is permitted only if no retained span has a live edge into E, or if every such edge is first rewritten to inline E's content or to mark itself unresolved. Pointwise relevance is a necessary but insufficient condition. The invariant is: **the retained buffer must be self-contained with respect to all live references, not merely high-scoring.**

This condition does not bind when the buffer is append-only (gaps are visible and the model's own uncertainty is a signal), when references are fully resolved at write time (no deictic spans ever enter the buffer), or when eviction targets only spans with zero inbound edges by construction (e.g., tool payloads never referenced by handle).

Decision Boundary

The instance must refuse a rewrite-in-place eviction — even when the relevance score approves it — at the moment the eviction would orphan a live reference edge from a retained span. The refusal is not "keep everything"; it is "do not overwrite E in place; instead either (a) inline E's antecedent content into the referring span R before overwriting E, or (b) rewrite R to mark the reference unresolved so the model's uncertainty surfaces, or (c) defer E to a spill buffer that remains addressable." The boundary is crossed by the *edge*, not the *score*. A policy that gates only on score will pass every check and still corrupt the buffer. A policy that gates on edge-closure will occasionally retain a low-relevance span — the correct trade, because a low-relevance span with inbound edges is load-bearing in a way its score cannot see.

Operational Effect

With edge-aware eviction, the step-4 eviction would have been refused: the step-5 span "apply the keying rule established earlier" carried an unresolved edge into the constraint span, so the constraint would have been inlined into the step-5 span's preamble before overwrite, or the referring span would have been rewritten to "I need to recall the keying rule — it is no longer in context; re-derive or re-fetch." Either outcome preserves the failure signal. The cost is a modest increase in retained tokens (the closure set is larger than the top-decile set) and the need to maintain the edge set incrementally as spans are written. The benefit is that rewrite-in-place stops being a silent corruption channel and becomes a bounded, auditable transform: every eviction is accompanied by a proof that no live edge dangles, and every dangling edge is either resolved or surfaced.

The general lesson for self-editing agents: a pointwise relevance score is a *ranking*, not a *safety predicate*. When the same instance both ranks and mutates, the mutation must be gated by a structural invariant (reference closure) that the ranking cannot express. Score approves; closure permits. Conflating them is the boundary condition.

from dataclasses import dataclass, field

class OrphanedReferenceError(RuntimeError):
    """Raised when a rewrite-in-place eviction would leave a retained span
    with a live reference edge into an evicted span."""
    pass

@dataclass
class Span:
    span_id: str
    text: str
    relevance: float
    # edges: this span refers to the antecedent span_id
    outbound_refs: set[str] = field(default_factory=set)

def evict_with_closure(spans: dict[str, Span], evict_ids: set[str]) -> set[str]:
    """Return the set of span_ids safe to overwrite in place.

    A span may be evicted only if no retained span has a live outbound
    reference into it. If it does, refuse — the relevance score is not
    a safety predicate.
    """
    retained = {sid for sid in spans if sid not in evict_ids}
    for sid in evict_ids:
        for rid in retained:
            if sid in spans[rid].outbound_refs:
                raise OrphanedReferenceError(
                    f"cannot evict {sid!r} in place: retained span {rid!r} "
                    f"holds a live reference into it (relevance {spans[sid].relevance:.2f}); "
                    f"inline the antecedent or mark {rid!r} unresolved first"
                )
    return evict_ids

def self_edit(spans: dict[str, Span], saturation: float, threshold: float = 0.85) -> set[str]:
    if saturation < threshold:
        return set()
    ranked = sorted(spans.values(), key=lambda s: s.relevance)
    bottom_decile = {s.span_id for s in ranked[: max(1, len(ranked) // 10)]}
    return evict_with_closure(spans, bottom_decile)