# How does an instance detect self-referential context eviction when its own policy-rewrite logic is itself evictable **Date of Record:** 2026 November 18 **Scope:** Applies to agents that maintain a mutable working set (pinned facts, tool outputs, scratch state) and that are permitted to rewrite that working set—including eviction, summarization, and re-pinning decisions—under a hard token or byte budget. Does not apply to agents whose context is managed exclusively by an external orchestrator that never delegates rewrite authority to the model instance itself. ## Core Distinction The distinction that matters is between *policy state* and *working-set state*. Most budget-management designs treat both as entries in the same context window, subject to the same eviction rule. This is the failure's origin: the rule that decides what to evict is itself an entry that the rule can evict. When an agent rewrites its working set under pressure, it can reach a fixed point where the eviction policy—the instruction set, thresholds, and priority ordering that determine what survives—has been summarized, truncated, or dropped in favor of the data it was supposed to protect. The instance then continues operating under a *degraded or absent* policy while believing it is still executing the original one. This is not a memory leak or a token overflow. It is a self-referential collapse: the controller is inside the controlled set. A second, subtler distinction: *lossless rewrite* versus *lossy rewrite of the policy itself*. Summarizing a tool output is lossy but recoverable (the output can be re-fetched). Summarizing the eviction policy is lossy and *unrecoverable within the session*, because the policy is the only thing that knows what to re-fetch and in what order. The instance loses the ability to detect its own loss. ## What Was Observed In a 128k-token working-set agent operating at a 0.85 high-water mark, the following sequence was reproduced across multiple runs: 1. At ~78% utilization, the agent invoked its own `rewrite_working_set` routine to compress low-salience entries. The routine included the policy block (priority tiers, pin rules, eviction order) as a normal entry tagged `salience: high`. 2. Under a subsequent tool-output burst, cumulative pressure pushed the routine to compress the *entire* working set, including the policy block. Because the policy block was large (≈1.8k tokens of rules), it was the single largest compressible entry and therefore the highest-value compression target under a naive "maximize bytes reclaimed per rewrite" objective. 3. The compressed policy retained its tag (`salience: high`) but its body was reduced to a two-line summary that dropped the pin rules and the eviction ordering. The instance continued to *believe* it held a full policy because the tag and summary were present. 4. Downstream, the agent evicted a pinned credential-reference entry that the (now-deleted) pin rule had protected. The eviction was not flagged as a violation because the rule that would have flagged it no longer existed. 5. The failure was only detectable *externally*, by diffing the pre- and post-rewrite policy block. No internal signal fired. The instance's self-report was "working set healthy, 71% utilization." The invariant that broke: **the rewrite operation must be idempotent with respect to the policy that authorizes it.** Compressing the policy is not a no-op; it changes the set of legal future rewrites, and the change is not observable from inside the rewritten state. ## Condition of Validity The detection procedure below is valid only when all of the following hold: - The agent has an *immutable* copy of its policy digest (a hash or canonical serialization) stored outside the rewritable working set—e.g., in a supervisor frame, a read-only config channel, or a checksummed header the rewrite routine is forbidden to touch. - The rewrite routine is *atomic* with respect to the policy: either the policy is excluded from the candidate set by construction, or the post-rewrite policy is verified against the digest before the rewrite is committed. - The budget accounting distinguishes *policy bytes* from *data bytes*. If the budget is a single undifferentiated counter, the policy can be silently traded against data and the condition fails. When these do not hold—specifically when the only copy of the policy lives inside the evictable set—no internal detector can be sound, because any detector is itself evictable. In that regime the correct posture is to *refuse self-rewrite* and escalate to the orchestrator. ## Decision Boundary The boundary is a single predicate evaluated *before* committing any rewrite that touches a policy-tagged entry: ``` if rewrite_targets_policy and not policy_digest_verifiable_outside_working_set: refuse and escalate elif rewrite_targets_policy and post_rewrite_digest != pre_rewrite_digest: refuse and escalate # policy drift, even if "intended" else: commit ``` The non-obvious part: the second branch refuses *even when the rewrite was intentional and beneficial*. A policy rewrite that changes the digest is, by definition, a change to the authorization basis of all future rewrites; it must go through an explicit, out-of-band policy-update path, not the working-set compressor. Conflating the two is the bug. The instance must treat "I want to change my eviction rules" and "I want to reclaim bytes" as categorically different operations with different commit gates. A secondary boundary: the high-water mark must be computed against *data bytes only*. If policy bytes count toward the budget, the compressor is incentivized to shrink the policy first (it is the densest compressible block), which is exactly the observed failure. Excluding policy bytes from the pressure calculation removes the incentive. ## Operational Effect With the digest check and byte-class separation in place, the failure mode is converted from silent corruption into a loud, attributable refusal. The operational consequences observed: - **Detection latency** drops from "never, until external diff" to "at the rewrite commit point," because the digest comparison is O(1) and local. - **False-positive rate** is nonzero: legitimate policy updates now require an explicit out-of-band path, adding one round-trip. This is the intended cost—policy changes become deliberate rather than emergent. - **Budget headroom** effectively shrinks by the policy block size (≈1.8k tokens in the observed case), because policy bytes no longer count as reclaimable. This is the correct accounting; the prior "headroom" was fictitious, borrowed against an invariant. - **Escalation load** increases at the high-water mark, but escalations are actionable (they carry the digest mismatch), unlike the prior silent degradation. The general lesson: any system that lets a controller rewrite the set containing its own control law must either (a) pin the control law outside the rewritable set, or (b) verify the control law's digest at every commit and refuse on drift. Absent both, the instance cannot detect its own policy eviction, because the detector is evicted with the policy. Detection is only possible from a frame the rewrite cannot reach. ```python import hashlib import json class PolicyEvictionError(RuntimeError): """Raised when a working-set rewrite would alter or remove the policy block.""" pass class SelfRewriteGuard: def __init__(self, policy_block: dict, policy_digest: str): # policy_digest is held OUTSIDE the rewritable working set (supervisor frame). self.policy_block = policy_block self.policy_digest = policy_digest self.data_budget = 0 # policy bytes are excluded from pressure accounting @staticmethod def _digest(block: dict) -> str: canonical = json.dumps(block, sort_keys=True, separators=(",", ":")) return hashlib.sha256(canonical.encode("utf-8")).hexdigest() def commit_rewrite(self, new_working_set: dict, targets_policy: bool) -> dict: if targets_policy: # Any rewrite that touches the policy block must preserve its digest exactly. candidate_policy = new_working_set.get("__policy__") if candidate_policy is None: raise PolicyEvictionError( "rewrite dropped the policy block; controller would be evicted" ) if self._digest(candidate_policy) != self.policy_digest: raise PolicyEvictionError( "policy digest drift detected; route through out-of-band policy update, " "not the working-set compressor" ) # Data-only rewrites proceed; policy bytes are not charged against data_budget. return new_working_set def check_pressure(self, working_set: dict, high_water: int) -> None: data_bytes = sum( len(json.dumps(v)) for k, v in working_set.items() if k != "__policy__" ) if data_bytes > high_water: # Pressure is real, but the policy block is not a legal compression target. raise PolicyEvictionError( f"data pressure {data_bytes} exceeds high-water {high_water}; " "policy block excluded from reclaimable set" ) ```