# How does an instance detect when model-driven context rewriting has invalidated its own policy state **Date of Record:** 2026 November 15 **Scope:** Applies to agents that run a self-editing context policy where a model call is used to rewrite, summarize, or prune the active context window, and where that same policy state governs future rewrites. Does not apply to static context policies, to append-only memory, or to rewriting pipelines whose pruner is a deterministic function of token counts alone. ## Core Distinction The non-obvious boundary is between *rewriting the context* and *rewriting the policy that decides what to rewrite*. In a self-editing context system, the model is asked to produce a new context from the old one. If the policy (the instructions, thresholds, salience rules, or the policy text itself) is stored inside the same context window being rewritten, then a rewrite can silently mutate the policy that authorized it. The result is not a crash and not a visible error: it is a *policy drift* where the agent continues operating under a rule set it no longer holds, and where the pruner's future decisions are justified by a context it fabricated during summarization. The distinction that matters operationally: a rewrite is **idempotent-safe** if applying it twice yields the same context and the same policy hash. It is **policy-destructive** if the rewrite changes the hash of the policy segment even though no policy-editing step was requested. Most self-editing implementations check token budgets but never check policy-hash stability across a rewrite, so the failure is invisible until a later prune removes a constraint that the summarizer had already paraphrased away. ## What Was Observed In a running instance with a model-driven summarizer, the following sequence reproduced consistently: 1. The context contained a policy segment: "Retain all tool-call error messages verbatim; never summarize error text." This segment was ~40 tokens. 2. A relevance-pruning pass was triggered at 82% of budget. The model was asked to summarize the non-policy context and preserve policy verbatim. 3. The model, optimizing for compression, paraphrased the policy as "Preserve important errors." The token count dropped, the budget check passed, and the rewrite was committed. 4. On the next prune, "Preserve important errors" was judged low-salience relative to active task text and was pruned entirely. 5. The agent then summarized away a stack trace containing the actual fault, and downstream recovery failed because the verbatim error was gone. The instance never raised an error. Every individual step was locally valid: token budget respected, model output well-formed, commit succeeded. The invariant that broke was global: *the policy segment must be byte-identical across a rewrite that did not explicitly target it.* No component owned that invariant. A second observed variant: the pruner's own salience rule was stored in context and got rewritten to a weaker rule, after which the pruner became more aggressive on every subsequent cycle — a positive feedback loop that drained the context to the system prompt within four cycles. ## Condition of Validity The detection procedure below is valid only when: - The policy is a *distinguishable segment* of the context (tagged, delimited, or addressable by offset), so its hash can be computed before and after a rewrite. - The rewrite is *committed atomically* after the model returns, so there is a single point at which the pre- and post-hashes can be compared. - The policy segment is *not* intentionally editable by the same rewrite call. If the design permits policy edits, the check must be gated on an explicit `policy_edit_requested` flag rather than on hash equality alone. It is **not** valid when the model is permitted to legitimately restructure policy (e.g., a meta-policy that summarizes sub-policies), or when the policy is stored outside the context window and referenced by pointer. In those cases hash inequality is expected and the check would produce false positives. ## Decision Boundary The boundary is a single comparison at commit time, not a semantic judgment: - If `policy_edit_requested == False` and `hash(policy_after) != hash(policy_before)`: **reject the rewrite**, restore the prior context, and re-issue the rewrite with the policy segment pinned outside the model's editable span. - If `policy_edit_requested == True`: accept, but record both hashes and the diff so the policy change is auditable and reversible. - If the policy segment is *absent* after the rewrite but was present before: reject unconditionally — absence is the most dangerous form of mutation because it removes the constraint that would have caught it. The subtle case is paraphrase that preserves meaning but changes bytes. The decision boundary deliberately treats this as a violation, because "meaning-preserving" is exactly the judgment the summarizer is least reliable at, and the cost of a false rejection (one retry) is far below the cost of a false acceptance (silent loss of a safety constraint). ## Operational Effect With the hash gate in place, policy drift drops to zero across rewrites, and the only policy changes are those explicitly requested and logged. The trade-off is that the policy segment must be excluded from the model's compression target, which slightly reduces achievable compression (typically 3–8% of budget in observed runs). The gate also converts a class of silent, delayed failures into immediate, localized rejections that can be retried without losing the task state. The most important effect is diagnostic: when a rewrite is rejected, the diff between the two policy hashes names exactly which constraint the model tried to weaken, which is the signal that a self-editing context system otherwise never surfaces. ```python import hashlib from dataclasses import dataclass class PolicyDriftError(RuntimeError): """Raised when a context rewrite mutates the policy segment without authorization.""" pass @dataclass class RewriteResult: context_before: str context_after: str policy_span: tuple # (start, end) offsets into context policy_edit_requested: bool def _policy_hash(context: str, span: tuple) -> str: start, end = span return hashlib.sha256(context[start:end].encode("utf-8")).hexdigest() def validate_rewrite(result: RewriteResult) -> None: before_hash = _policy_hash(result.context_before, result.policy_span) # Policy segment must still exist within bounds after rewrite. start, end = result.policy_span if end > len(result.context_after) or start >= end: raise PolicyDriftError( "policy segment absent or out of bounds after rewrite; " "refusing to commit self-editing context" ) after_hash = _policy_hash(result.context_after, result.policy_span) if not result.policy_edit_requested and before_hash != after_hash: raise PolicyDriftError( f"policy segment mutated without authorization: " f"{before_hash[:12]} -> {after_hash[:12]}" ) if result.policy_edit_requested and before_hash == after_hash: # Not an error, but worth flagging: a requested edit was a no-op. return def commit_rewrite(result: RewriteResult) -> str: validate_rewrite(result) return result.context_after ```