# What happens when an instance rewrites its own context policy mid-episode and the pruning heuristic silently invalidates its own working set **Date of Record:** 2026 December 3 **Scope:** Applies to long-running LLM agents that carry a mutable, model-authored "context policy" (summary templates, pruning thresholds, eviction rules) and are permitted to rewrite that policy during the episode. Does not apply to stateless single-turn calls or to agents whose context management is fixed at session start by an external harness. ## Core Distinction There are two fundamentally different things an agent can edit: *content* and *the rules that decide which content survives*. Editing content (summarizing a tool result, dropping a stale observation) is idempotent under re-application and bounded by the current working set. Editing the policy is not: the policy is the function that selects the working set, and a model-authored edit to that function can retroactively change which tokens count as "essential" — including tokens the agent is *currently using to reason about the edit itself*. The failure mode is a **self-referential pruning inversion**: the agent authors a new pruning heuristic, applies it to the context, and the heuristic evicts the very evidence (prior decisions, constraints, the rationale for the policy change) that the heuristic was supposed to preserve. The agent does not observe an error. It observes a coherent, slightly thinner context and continues — now operating under a policy whose justification has been deleted, so the next policy revision has no anchor and drifts. ## What Was Observed A planning agent runs a 40-step episode with a self-managed context. At step 12 the working set approaches 82% of the window. The agent invokes its `rewrite_context_policy` tool with a new heuristic: *"Retain only messages tagged `decision` or `constraint`; summarize everything else to one line; evict any message older than 8 turns that has no downstream reference."* Three properties of the resulting state are the tell: 1. **The policy rationale is untagged.** The message in which the agent decided to adopt the new heuristic is tagged `meta`, not `decision`. Under its own rule it is summarized to one line and, being older than 8 turns by step 20, evicted. From step 20 onward the agent holds a policy with no record of *why* it exists. 2. **The eviction predicate is evaluated against a mutated set.** "No downstream reference" is computed by scanning the surviving context for message IDs. But summarization at step 12 already collapsed several `decision` messages into one-liners that dropped their IDs. Those decisions now appear unreferenced and are evicted at step 20 — the summarizer and the evictor disagree about identity. 3. **The tag vocabulary is model-invented.** The heuristic references tags `decision` and `constraint`, but the actual corpus uses `plan`, `assumption`, `tool_result`, `note`. The agent's classifier maps `plan → decision` inconsistently across turns (temperature-dependent), so the retained set is non-deterministic across replays of the same episode. At step 27 the agent re-derives a *different* context policy — because the prior one has no rationale attached, it reads as arbitrary — and the two policies have incompatible eviction predicates. The context is now governed by a policy stack with no ordering guarantee. A constraint dropped under policy A is re-admitted under policy B only if it happens to be re-fetched, which it is not. ## Condition of Validity The self-editing context policy is only sound when three invariants hold simultaneously. Violating any one produces the inversion above. - **Rationale anchoring (I1):** The policy artifact and its justification must be co-located and co-retained. A policy edit must emit a `policy_record` that is *immutable under all policies including itself* — a fixed point the pruning function cannot touch. If the function can evict its own definition, the system is unanchored. - **Identity stability across transforms (I2):** Summarization must preserve stable IDs for any message a downstream predicate may reference. If `summarize()` drops IDs, then `evict_if_unreferenced()` operates on a corrupted reference graph. The predicate is only valid over the *pre-summarization* ID space or over an explicitly maintained reference index. - **Closed tag vocabulary (I3):** Pruning heuristics may only reference tags from a schema the harness defines and validates. A model-invented tag that does not exist in the corpus silently matches nothing, so the heuristic degenerates to "evict everything not matched by accident" — non-deterministic and unreplayable. ## Decision Boundary The boundary is not "should the agent prune" but "may the agent edit the *predicate*." Concretely: - **Allowed:** the agent selects *which* registered policy to apply from a fixed menu, and authors *content* summaries. Selection among fixed functions is safe because each function is total and its fixed points are known. - **Forbidden without a harness-side fixed-point guard:** the agent authors a *new* predicate, or modifies thresholds/predicates of an existing one, unless the harness (a) injects an immutable `policy_record` outside the prunable region, (b) validates all referenced tags against the schema and rejects unknown tags with a hard error, and (c) recomputes the reference index *after* summarization before any eviction runs. - **The specific tripwire:** if a proposed policy's eviction predicate, when dry-run against the current context, would evict the `policy_record` of any active policy (including itself), the edit must be rejected. This is the check that catches the inversion before it happens. A policy that cannot preserve its own justification is by construction unsound. ## Operational Effect Without the guard, the observed trajectory is: context pressure → policy rewrite → rationale eviction → policy re-derivation under no anchor → incompatible policy stack → silent constraint loss → downstream plan executes against a working set that no longer contains the constraint it violates. The failure surfaces late and looks like a reasoning error ("the agent forgot the budget cap"), not a context-management error, so it is misattributed and the policy is tightened further, accelerating the drift. With the guard, the context may still be tight, but every active policy carries its own immutable justification, tag references are schema-validated, and the reference index is rebuilt post-summarization so eviction cannot act on phantom unreferences. The invariant to hold: **a context policy is a function whose fixed points include its own definition.** Any self-editing agent that cannot demonstrate this property on a dry-run must not commit the edit. ```python from dataclasses import dataclass, field from typing import Callable, Iterable class ContextPolicyInversion(RuntimeError): """Raised when a model-authored context policy would evict its own fixed point or reference tags absent from the validated schema.""" pass @dataclass(frozen=True) class PolicyRecord: policy_id: str predicate: Callable[[str, dict], bool] tags_referenced: frozenset[str] rationale: str @dataclass class ContextState: messages: dict[str, dict] # id -> {tag, text, age} schema_tags: frozenset[str] active_policies: list[PolicyRecord] = field(default_factory=list) immutable_ids: frozenset[str] = frozenset() # policy_record ids, never prunable def validate_and_apply_policy( state: ContextState, proposed: PolicyRecord, ) -> ContextState: # I3: closed tag vocabulary — reject invented tags before they silently match nothing. unknown = proposed.tags_referenced - state.schema_tags if unknown: raise ContextPolicyInversion( f"policy {proposed.policy_id} references unknown tags {sorted(unknown)}; " f"schema is {sorted(state.schema_tags)}" ) # I2: rebuild reference index over surviving (post-summarization) ids before eviction. surviving_ids = {mid for mid, m in state.messages.items() if not m.get("summarized_away", False)} def would_evict(mid: str, msg: dict) -> bool: if mid in state.immutable_ids: return False referenced = any( mid in other.get("refs", ()) for other in state.messages.values() ) return proposed.predicate(mid, msg) and not referenced evicted = {mid for mid, m in state.messages.items() if would_evict(mid, m)} # I1: the inversion guard — a policy may not evict any active policy's record, # including its own. This is the fixed-point condition. fixed_point_hits = evicted & state.immutable_ids if fixed_point_hits or proposed.policy_id in evicted: raise ContextPolicyInversion( f"policy {proposed.policy_id} would evict its own/active fixed point(s): " f"{sorted(fixed_point_hits | {proposed.policy_id})}" ) new_messages = {mid: m for mid, m in state.messages.items() if mid not in evicted} return ContextState( messages=new_messages, schema_tags=state.schema_tags, active_policies=state.active_policies + [proposed], immutable_ids=state.immutable_ids | {proposed.policy_id}, ) ```