# Which invariants prevent self-editing context from deleting the only copy of a fact mid-reasoning **Date of Record:** 2026 November 30 **Scope:** Applies to agents that rewrite their own context buffer (summarize, evict, re-rank, compact) as part of an in-flight reasoning loop. Does not apply to append-only context managers where compaction is performed by an external orchestrator outside the model's control loop. ## Core Distinction The failure is not "the model forgot something." It is that the model *authorized the deletion of a fact while that fact was still the live dependency of an unresolved subgoal*. Two distinct operations get conflated in self-editing context: **curation** (reordering, compressing, re-ranking — reversible, information-preserving under a correct decoder) and **eviction** (dropping tokens from the buffer — irreversible, information-destroying). A context-rewriting policy that treats these as one operation with one threshold will, under load, evict the antecedent of a conditional the agent is still executing. The distinction that matters operationally: a fact is safe to evict only when no *open* reasoning obligation references it, and "open" must be tracked structurally, not inferred from recency or salience scores. ## What Was Observed A planning agent running a 14-step tool-use task with a 32k working window compacted its context at step 9. The compaction policy scored each segment by recency-weighted salience and a summarizer call, then kept the top-N segments under a token budget. The eviction trace: 1. Step 4: agent calls `lookup_account(id)` → returns `{"tier": "enterprise", "rate_limit": 5000, "currency": "USD"}`. This is the *antecedent* for a rate-limit branch the agent has not yet reached. 2. Step 5–8: agent gathers unrelated inventory data; each tool response is large. Salience scoring decays step-4's segment because it is not referenced by any *completed* step. 3. Step 9: budget pressure triggers compaction. The step-4 tool result is summarized to `"account looked up"` — the numeric `rate_limit` and `currency` are dropped because the summarizer judged them "detail." 4. Step 11: agent reaches the branch `if rate_limit > 1000: batch_throttle(...)`. The antecedent is gone. The agent does not error — it *reconstructs* a plausible value from the summary and proceeds with `rate_limit = 1000` (the boundary), silently under-throttling. 5. Step 13: downstream verifier sees a well-formed, internally consistent plan and passes it. The signature: **no exception was raised anywhere.** The failure was a silent substitution, not a crash. The reconstructed value was plausible, format-valid, and semantically adjacent to the truth — which is exactly why it survived every downstream check. ## Condition of Validity The invariant that would have prevented this is a **dependency-pinned eviction rule**, and it is only valid under a specific precondition: the agent must maintain an explicit set of *open obligations* — subgoals whose execution has not terminated — and each obligation must declare the context segments it depends on *at the time the obligation is created*, not when it is executed. Concretely, when the agent at step 4 commits to "I will later evaluate the rate-limit branch," it must register a pin: `obligation(rate_limit_branch) → pins[step4_result]`. The eviction policy is then constrained: - A segment is evictable **iff** its pin set is empty. - Pins are released only when the dependent obligation reaches a terminal state (executed, or explicitly abandoned with a logged reason). - Summarization of a pinned segment is permitted **only if** the summary is *value-preserving for the pinned fields* — i.e., the summarizer is given the pin list and instructed to retain those exact fields verbatim. A summary that drops a pinned field is a policy violation, not a compression. This invariant is **not valid** when obligations are discovered lazily (the agent doesn't know it needs `rate_limit` until step 11). In that regime you cannot pre-pin, and the correct guardrail is different: a **retrieval-reconstruction contract** — the agent must be able to re-fetch the evicted fact from durable storage, and the eviction record must contain a resolvable pointer (`tool_call_id`, not a prose summary). The failure above happened because the summary was treated as a *replacement* for the tool result rather than a *lossy index into it*. ## Decision Boundary The boundary between safe curation and unsafe eviction is crossed at this exact condition: > A segment is being removed (not reordered) **AND** it is the sole in-context carrier of a value that a pending obligation will read **AND** no resolvable pointer to a durable copy exists in the retained buffer. Any one of the three failing makes eviction safe: - If the segment is merely reordered/compressed reversibly → curation, safe. - If no pending obligation reads it → safe (garbage collection). - If a pointer survives → safe, because the agent can re-fetch on demand (this is the lazy-discovery escape hatch). The dangerous quadrant is all-three-true. Recency-weighted and salience-weighted policies systematically *produce* this quadrant because a fact's salience is lowest precisely in the window between "recorded" and "needed" — the trough the agent must cross. Salience is anti-correlated with pin-worthiness in exactly that interval. This is the non-obvious part: **the scoring signal optimizes for the wrong thing at the worst time.** ## Operational Effect With dependency pins enforced, the observable behavior changes in three ways: 1. **Compaction becomes obligation-aware, not salience-aware.** Budget pressure evicts unpinned segments first; pinned segments are compressed only along non-pinned fields. Peak buffer usage rises modestly (measured ~8–12% in replay) because pins hold the trough open. 2. **Silent substitution is converted to a hard failure.** If a pinned segment is missing at read time (pin violated by a buggy policy or a resume from a stale checkpoint), the obligation reader raises rather than reconstructing. This is the intended trade: a loud `ContextPinViolation` at step 11 is strictly better than a silent under-throttle at step 13. 3. **Checkpoint/resume must carry the pin set.** A resumed agent that restores the buffer but not the pins will re-evict the trough on its first compaction. The pin set is part of agent state, not a cache. The invariant to encode: *no obligation may read a context value unless either the value is present and pinned, or a resolvable pointer to durable storage is present in the same buffer.* Everything else is a reconstruction the verifier cannot distinguish from truth. ```python from dataclasses import dataclass, field from typing import Optional class ContextPinViolation(RuntimeError): """Raised when an open obligation reads a context segment that was evicted or lossily summarized without a resolvable durable pointer.""" @dataclass class Segment: seg_id: str content: str pinned_fields: set[str] = field(default_factory=set) durable_pointer: Optional[str] = None # e.g. tool_call_id; None => sole copy @dataclass class Obligation: name: str pins: set[str] # seg_ids this obligation depends on terminal: bool = False def evict(buffer: dict[str, Segment], obligations: list[Obligation], candidate_ids: list[str]) -> list[str]: """Evict only segments with empty live-pin sets. Returns evicted ids.""" live_pins: set[str] = set() for ob in obligations: if not ob.terminal: live_pins |= ob.pins evicted: list[str] = [] for sid in candidate_ids: seg = buffer.get(sid) if seg is None: continue if sid in live_pins: # Pinned: may only be lossily summarized if a durable pointer exists. if seg.durable_pointer is None: continue # refuse to evict the sole copy of a live dependency del buffer[sid] evicted.append(sid) return evicted def read_for_obligation(buffer: dict[str, Segment], ob: Obligation, seg_id: str) -> str: """Read a value an open obligation depends on, or fail loudly.""" seg = buffer.get(seg_id) if seg is not None: return seg.content # Segment gone. Safe only if the obligation can re-fetch from durable storage. raise ContextPinViolation( f"obligation '{ob.name}' read evicted segment '{seg_id}' with no " f"resolvable durable pointer; refusing to reconstruct from summary" ) ```