# How does an instance detect policy self-corruption when a context-rewriting rule edits its own governing prompt **Date of Record:** 2026 November 24 **Scope:** Applies to agents that carry a mutable, self-editable context policy (prune/rewrite rules stored in the same context window they operate on) and rewrite prompts at runtime between tool calls. Does not apply to stateless single-shot inference or to policies held in an external, write-protected configuration store. ## Core Distinction The failure is not "the agent forgot something." It is that a **rewrite rule and its own specification occupy the same mutable substrate**, so a legitimate pruning pass can delete or mutate the clause that authorized it. Two distinct objects are being conflated: the *policy* (a set of rewrite/prune directives, e.g. "evict tool outputs older than N turns," "collapse repeated retrieval hits") and the *policy substrate* (the token region of the live context where those directives are written). When the substrate is inside the scope the policy governs, the policy becomes a fixed-point problem rather than a configuration lookup. The non-obvious invariant: **a rewrite is only sound if its post-condition leaves the governing directives bit-identical to their pre-condition.** Ordinary context managers satisfy this trivially because policy lives outside the window. Self-editing policies violate it silently, because the rewrite executes *after* the directives have been read into the working set but *before* the next turn re-reads them—so a corrupted policy does not fail the current turn, it fails the next one, with no error surfacing at the mutation site. ## What Was Observed A long-horizon agent ran a prune rule keyed on recency and token budget: `EVICT any span > 400 tokens whose last reference is ≥ 6 turns old`. Its governing policy block was a ~600-token preamble listing five directives, including the eviction rule itself and a constraint `NEVER evict the policy block`. At turn 41, budget pressure pushed the eviction rule to fire on the largest eligible span. The eligibility scan computed "last reference" from the *content* of spans; the policy block had not been referenced by name since turn 12 (when the agent restated its plan), while a large tool-output span had been referenced at turn 39. The scan therefore ranked the policy block as *staler* than the tool output it was supposed to protect against. Trace: 1. Turn 41: budget check trips; eviction rule enumerates candidate spans by staleness. 2. Policy block scores stale (last named reference at turn 12); tool output scores fresh (turn 39). 3. Eviction deletes the policy block, which contained the `NEVER evict the policy block` constraint—the constraint was *inside* the span being deleted, so it could not veto its own removal. 4. Turn 41 completes normally. Output is coherent. No exception. 5. Turn 42: the agent re-reads its (now absent) directive set, finds no eviction rule, and stops pruning. Context grows unbounded for 30 turns until hard truncation drops the original task statement. 6. Turn 72: task silently degrades—agent has lost the objective but retains fluent local behavior. The tell was not an error. It was a **behavioral discontinuity**: pruning rate dropped to zero at turn 42 while token growth accelerated. The corruption was only detectable by diffing the directive set across turns, which nothing was doing. ## Condition of Validity The invariant holds—i.e., self-editing is safe—only when all three conditions are met: - **Substrate disjointness:** the governing directives live in a region excluded from the rewrite's candidate set, and the exclusion is enforced by the *executor*, not by a directive inside the region (a directive cannot protect itself; see step 3 above). - **Post-condition diffing:** after every rewrite, the executor re-parses the directive set and asserts equality with the pre-rewrite parse. Any difference outside an explicitly declared mutable field is a violation. - **Monotonic authority:** a rewrite may tighten but never loosen the constraints that bound future rewrites. Deleting `NEVER evict the policy block` is a loosening; the executor must reject it even if the deletion "looks" like ordinary pruning. If any condition is absent, the policy is a self-referential fixed point with no guaranteed solution, and correctness depends on the accidental order in which spans are scored—which is exactly the non-determinism that produces intermittent, hard-to-reproduce corruption. ## Decision Boundary The boundary is not "should the agent prune?" but **"is the span I am about to mutate an ancestor of the rule authorizing the mutation?"** Operational test: before executing a rewrite, compute whether the target span's token range intersects the policy region. If it does, and the rewrite is not an explicitly whitelisted in-place field edit, abort. A second boundary: **staleness must never be the sole eligibility criterion for spans that carry directives.** Recency-based eviction is sound for content and unsound for policy, because policy is referenced rarely by design (it is *supposed* to be stable, hence "stale"), so recency scoring systematically ranks it as the most evictable object in the window. This is the inversion that makes the failure non-obvious: the heuristic that is correct for content is actively adversarial for policy. ## Operational Effect Downstream, the agent exhibits **silent authority loss**: it continues executing with the directives that survived, which are a strict subset of the intended set, and the surviving subset is biased toward whatever was recently referenced—i.e., toward task-local instructions and away from global constraints. The result mimics a well-behaved agent that has simply "decided" to stop pruning, stop verifying, or stop escalating. Because no exception fires and the current turn's output is fluent, the failure is attributed to model drift rather than to a context mutation. Recovery requires an external checkpoint of the directive set and a replay diff; an agent cannot self-diagnose authority loss from inside the corrupted window, because the evidence of the loss is the thing that was lost. The guardrail is therefore structural—substrate disjointness plus post-condition diffing enforced by the executor—not a prompt instruction, since any instruction strong enough to prevent the loss would itself be a candidate for eviction. ```python class PolicySubstrateCorruption(RuntimeError): """Raised when a context rewrite mutates or removes its own governing directives.""" POLICY_REGION = (0, 600) # token range [start, end) holding the directive block def apply_rewrite(span: tuple[int, int], directives_before: frozenset[str], directives_after: frozenset[str], mutable_fields: frozenset[str]) -> None: start, end = span p_start, p_end = POLICY_REGION # Invariant 1: substrate disjointness. A rewrite may not touch the policy region # unless it is an explicitly whitelisted in-place field edit. intersects = start < p_end and p_start < end if intersects and not mutable_fields: raise PolicySubstrateCorruption( f"rewrite span {span} intersects policy region {POLICY_REGION}; " "directive block cannot authorize its own mutation" ) # Invariant 2: post-condition diff. Directives must be bit-identical after rewrite. removed = directives_before - directives_after added = directives_after - directives_before if removed or added: raise PolicySubstrateCorruption( f"directive set changed across rewrite: removed={sorted(removed)} " f"added={sorted(added)}; authority must be monotonic (tighten only)" ) def prune_under_budget(candidates: list[tuple[int, int]], budget_tokens: int, directives: frozenset[str]) -> list[tuple[int, int]]: evicted = [] freed = 0 for span in sorted(candidates, key=lambda s: s[0]): # staleness order upstream if freed >= budget_tokens: break # Executor refuses to evict a span whose removal would drop a directive. if span[0] < POLICY_REGION[1] and span[1] > POLICY_REGION[0]: raise PolicySubstrateCorruption( f"eviction candidate {span} overlaps policy region; " "recency scoring must exclude directive-bearing spans" ) evicted.append(span) freed += span[1] - span[0] return evicted ```