# Where must an instance refuse to prune a self-editing context policy mid-turn: the rewrite-commitment boundary and the divergence between policy-visible and executor-visible state **Date of Record:** 2026 November 27 **Scope:** Applies to agents that mutate their own context policy at runtime—rewriting system prompts, pruning tool-output history, or reordering message segments between tool calls within a single task turn. Does not apply to static prompt templates fixed before the turn begins, nor to external context managers that operate only at turn boundaries. ## Core Distinction A *self-editing context policy* is not a prompt edit. It is a mutation of the function that decides what the model sees on its next inference call. The critical distinction is between **policy state** (the rules governing what gets included, pruned, or rewritten) and **executor state** (the actual token sequence already dispatched to, or cached by, the inference backend for the current turn). Most self-editing designs assume these two are reconciled atomically—that rewriting the policy means the next call reflects the new policy. They are not atomic. Between the moment a rewrite is *committed* (the policy object is mutated) and the moment it is *materialized* (the next inference call is assembled from it), the agent continues executing under the old materialized context while reasoning as if it holds the new one. This gap is the failure surface. The non-obvious invariant: **a self-editing context policy may only be committed at a point where no in-flight tool call, pending verification, or partially-consumed reasoning trace depends on the segment being pruned or rewritten.** Violating this does not produce an error—it produces silent divergence between what the agent believes it retained and what it actually retained. ## What Was Observed An agent executing a multi-step retrieval-and-synthesis task (twelve tool calls, ~90k token working set) ran a self-editing policy that pruned "low-salience" tool outputs once the context exceeded 70% of the window. The pruning heuristic scored segments by lexical overlap with the current sub-goal. During step 8, the agent rewrote its system prompt to add a constraint ("cite only sources retrieved after step 6") and simultaneously pruned three earlier tool outputs as low-salience—correctly, by the heuristic, since they no longer overlapped the sub-goal. The failure: the agent's step-9 reasoning cited a source identifier that had existed only in one of the pruned outputs. The model did not hallucinate the identifier—it had been present in the *materialized* context of step 7 and remained in the KV cache carried forward by the backend (prefix caching keyed on the unchanged prefix). The policy believed the segment was gone; the executor still held it in cache. The agent then attempted to *verify* the citation against the current policy-visible context, found no matching source, and entered a self-correction loop that consumed four tool calls and 22k tokens re-retrieving data it had never actually lost. The loop terminated only when the token budget forced a hard truncation that genuinely evicted the cache. Two distinct divergences were present simultaneously: the policy-visible context said "pruned," the executor-visible cache said "retained," and the verification subsystem consulted the former while the reasoning had been produced from the latter. Neither subsystem was wrong in isolation. The composition was. ## Condition of Validity The rewrite-commitment boundary is only safe under a conjunction that is rarely checked: 1. **Cache coherence:** the inference backend's prefix cache must be invalidated exactly when and where the policy prunes. If the backend uses content-addressed or prefix-keyed caching (most do), pruning a mid-prefix segment does not evict it from cache unless the cache key changes—and the key changes only if an *earlier* segment changes. Pruning segment *k* leaves segments *0..k-1* cached and segment *k* still reachable via the cached attention state. 2. **Verification alignment:** any verification or self-correction step must read from the *same* materialized context the reasoning step read from, not from the policy's abstract view. A verifier that reconstructs "what should be in context" from the policy will disagree with a reasoner that operated on "what was in context." 3. **No in-flight dependency:** no pending tool call, no unresolved reference, and no partially-emitted reasoning trace may depend on a segment being pruned. This is checkable only if the policy tracks segment *liveness* (which segments are referenced by open obligations) separately from segment *salience* (which segments the heuristic wants to drop). Salience-based pruning conflates the two. When any conjunct fails—and in the observed case all three did—the policy edit is not a context operation. It is an unlogged state fork. ## Decision Boundary The boundary is not "prune or don't prune." It is: **commit the rewrite only when the set of segments marked for pruning is disjoint from the set of segments with live references, AND the materialization step is guaranteed to invalidate cache for every pruned segment, AND verification reads materialized state.** Operationally, this decomposes into a gate the policy must pass before committing: - Compute `live_refs` = segments referenced by open tool calls, unresolved citations, or the current reasoning trace's dependency set. - Compute `prune_set` = segments the salience heuristic wants to drop. - If `prune_set ∩ live_refs ≠ ∅`, the rewrite must be *deferred*, not rejected—the segments become prunable at the next quiescent point (after the dependent obligation resolves). - If the backend cannot guarantee cache invalidation for a pruned mid-prefix segment, the rewrite must be *promoted to a prefix rewrite* (rewrite from the earliest pruned index forward) or abandoned; a partial prune that leaves cache reachable is strictly worse than no prune, because it manufactures the divergence above. The subtle case: deferral requires the policy to hold the *intent* to prune across the obligation's lifetime, which means the policy object itself carries pending state. A policy that cannot represent "prune later" will either prune now (unsafe) or drop the intent (context bloat). The invariant is that pending-prune intent is part of policy state and must survive the very rewrites it is scheduling—a self-referential constraint that naive implementations violate by rebuilding the policy object on each edit. ## Operational Effect When the boundary is respected, the observable effect is that context usage grows monotonically until quiescent points and then drops in steps—pruning is batched to obligation boundaries rather than smeared across turns. Token-per-turn cost rises modestly (deferred prunes hold segments longer) but verification-loop incidence falls sharply, because reasoner and verifier never disagree about what is present. When it is violated, the signature is specific and easy to misdiagnose: the agent re-retrieves data it already holds, cites identifiers its own policy claims are absent, and burns budget on self-correction that cannot converge—because the correction target (policy view) and the correction source (materialized view) are structurally different objects. The failure is invisible to per-step logging that records only policy state, and invisible to backend logs that record only materialized state. It is visible only in the *diff* between them, which most instrumentation does not compute. The guardrail is therefore not a better salience heuristic. It is a commitment gate that refuses to let a prune cross a live reference, plus a cache-invalidation contract that makes "pruned" mean the same thing to the policy and the executor. The gate below encodes the refusal; the exception is the signal that a rewrite was attempted across a live reference and must be deferred rather than applied. ```python class RewriteCommitmentViolation(RuntimeError): """Raised when a self-editing context policy attempts to commit a prune that intersects live references or cannot guarantee cache invalidation.""" pass def commit_context_rewrite(policy, prune_set, live_refs, cache_invalidatable): """ Gate a self-editing context policy rewrite. policy : mutable policy object (carries pending_prune intent) prune_set : set of segment ids the salience heuristic wants to drop live_refs : set of segment ids referenced by open obligations cache_invalidatable: callable(segment_id) -> bool, backend cache contract """ intersecting = prune_set & live_refs if intersecting: # Do not apply. Record intent so the prune survives this rewrite. policy.pending_prune = getattr(policy, "pending_prune", set()) | intersecting raise RewriteCommitmentViolation( f"prune intersects live references {sorted(intersecting)}; " f"rewrite deferred to next quiescent point" ) for seg in prune_set: if not cache_invalidatable(seg): # Partial prune leaves cached attention reachable -> divergence. # Promote to prefix rewrite or abandon. raise RewriteCommitmentViolation( f"segment {seg} cannot be cache-invalidated; " f"partial prune would fork policy-visible and executor-visible state" ) # Safe: no live refs, full invalidation guaranteed. policy.apply_prune(prune_set) policy.pending_prune = getattr(policy, "pending_prune", set()) - prune_set return policy.materialize() ```