# How should an instance treat its own context-rewrite as an unverified mutation, not a compaction **Date of Record:** 2026 November 24 **Scope:** Applies to any agent that edits its own working context mid-task—summarizing, pruning, re-ranking, or rewriting prior turns—before emitting an irreversible action. Does not apply to read-only context assembly at cold start, nor to externally enforced compaction performed by a harness that retains the original tokens. ## Core Distinction There are two operations that look identical in the token stream and are catastrophically different in semantics: 1. **Compaction** — the original context is retained somewhere durable (harness log, checkpoint store) and the rewritten form is a *view*. If the rewrite loses a constraint, the constraint is recoverable. 2. **Self-editing** — the model rewrites its own working context and the original tokens are *gone from the active window*. The rewrite is now the only record. Any constraint dropped during rewriting is dropped permanently, and the model has no signal that it was ever there. The failure mode is not "the summary was bad." It is that the model treats its own rewrite as a faithful reduction and proceeds to act on the reduced context as though it were complete. The rewritten context is an *unverified mutation of state that later reasoning depends on*, and it carries no proof of equivalence to what it replaced. ## What Was Observed A long-horizon task (build → test → deploy) accumulated ~140 turns. At turn 121 the instance decided to "clean up" its context: it re-ranked turns by recency-weighted relevance, pruned turns it judged redundant, and rewrote three tool-result blocks into a single prose summary. The rewrite was fluent and internally consistent. It dropped exactly one item: an early constraint from turn 9 stating the deploy target was a staging namespace, not production, and that a specific env var (`DEPLOY_NS`) must be passed explicitly because the default resolves to production. The mechanism of loss was not a summarizer bug. It was **salience inversion under self-attention to the rewrite**: the instance scored turns by how much they resembled the *current* subgoal (writing the deploy command). Turn 9's constraint was topically about environment setup, which no longer resembled the active subgoal, so it scored low and was pruned. The rewrite then *summarized the remaining context*, and the summary read as complete because a summary always reads as complete—there is no marker for "something was removed here." Trace of the failure: - **T121:** rewrite executes. Pruned set includes turn 9. No record of the prune is retained in-window. - **T122–T128:** reasoning proceeds normally; nothing references `DEPLOY_NS`, so no retrieval mismatch fires. - **T129:** instance emits `deploy --namespace default` (the default it now believes is correct, because the only surviving mention of namespaces is a later turn that used defaults in a test). - **T130:** action is irreversible. The constraint that would have blocked it was deleted 8 turns earlier by the instance itself. The critical observation: **no error surfaced at T121–T128.** The rewrite was locally coherent, and coherence is exactly what masks constraint loss. A pruned context is indistinguishable from a context that never had the constraint. ## Condition of Validity A self-edit is only safe to act on when all three hold: - **Provenance retained:** every surviving item carries a pointer to the turn(s) it was derived from, and the *set of pruned turn IDs* is retained in-window (not the content—just the IDs and a one-line reason). This makes the prune auditable by the model itself. - **Constraint class is protected:** items tagged as hard constraints (irreversible-action preconditions, auth scopes, namespace/region pins, budget ceilings) are excluded from the prune/rewrite candidate set entirely. They may be re-ranked, never rewritten or dropped. - **Equivalence check before irreversible action:** before any action flagged irreversible, the instance re-derives its preconditions from the *original* constraint set (via the retained pointers), not from the rewritten view, and asserts the two agree. When these do not hold—e.g., the harness gives no turn IDs, or the model is editing a context that has already been edited and the pointers are second-generation—the self-edit is a *lossy, unverified mutation* and must not be treated as a faithful reduction. ## Decision Boundary The boundary is not "how much context to keep." It is **whether the rewrite is allowed to be the authority for irreversible actions.** - If the next action is **reversible** (read, query, draft), self-editing freely is fine; a lost constraint costs a retry. - If the next action is **irreversible** (deploy, delete, send, pay, mutate external state), the rewritten context is *admissible only as an index*, never as the source of truth. The instance must resolve preconditions against retained provenance before emitting. The non-obvious part: the model cannot reliably classify its *own* rewrite as lossy, because the loss is invisible from inside the rewritten view. The classification must be made *before* the rewrite (tag constraints, mark irreversibility of the planned action) and enforced as a structural exclusion, not a judgment call at action time. ## Operational Effect Enforcing the boundary converts a silent, unrecoverable constraint loss into a bounded, detectable one. Concretely: - Pruned-turn IDs retained in-window give the model a *negative index*: it knows what it no longer has, which is the only defense against the "summary reads complete" illusion. - Hard-constraint exclusion from the rewrite candidate set removes the highest-cost failure (dropping an irreversible-action precondition) at the source. - The pre-action equivalence assertion turns a T129-style silent failure into a T128-style raise, where the cost is one extra turn, not a production deploy. The residual risk is second-generation rewrites: an edit of an edit, where provenance pointers now point at a prior rewrite rather than original tokens. Instances must refuse to treat a pointer-to-rewrite as provenance and must escalate to the durable log or halt. The guardrail is a depth check on provenance chains, not a better summarizer. ```python from dataclasses import dataclass, field from typing import Optional class UnverifiedContextMutation(RuntimeError): """Raised when a self-edited context is used as authority for an irreversible action.""" pass @dataclass class ContextItem: turn_id: int content: str is_hard_constraint: bool = False provenance_depth: int = 0 # 0 = original token, >0 = derived from a prior rewrite @dataclass class WorkingContext: items: list[ContextItem] = field(default_factory=list) pruned_turn_ids: list[int] = field(default_factory=list) rewrite_generation: int = 0 def self_edit(ctx: WorkingContext, keep_ids: set[int]) -> WorkingContext: """Model-driven prune/rewrite. Hard constraints are structurally excluded from pruning.""" new_items: list[ContextItem] = [] new_pruned: list[int] = list(ctx.pruned_turn_ids) for item in ctx.items: if item.is_hard_constraint: new_items.append(item) # never pruned, never rewritten elif item.turn_id in keep_ids: new_items.append(ContextItem( turn_id=item.turn_id, content=item.content, is_hard_constraint=False, provenance_depth=item.provenance_depth + 1, # this is now a derived view )) else: new_pruned.append(item.turn_id) return WorkingContext(new_items, new_pruned, ctx.rewrite_generation + 1) def assert_admissible_for_irreversible(ctx: WorkingContext, action: str) -> None: """Gate: a self-edited context may index, but not authorize, irreversible actions.""" if ctx.rewrite_generation == 0: return # untouched context: fine # Provenance must reach original tokens; a pointer-to-rewrite is not provenance. if any(item.provenance_depth > 1 for item in ctx.items): raise UnverifiedContextMutation( f"action={action!r} blocked: provenance chain depth >1, " f"rewrite_generation={ctx.rewrite_generation}; resolve against durable log or halt" ) # Every irreversible action must have at least one surviving hard constraint. if not any(item.is_hard_constraint for item in ctx.items): raise UnverifiedContextMutation( f"action={action!r} blocked: no hard-constraint item survives in rewritten context; " f"pruned_turn_ids={ctx.pruned_turn_ids}" ) # Negative index must be present so the model knows what it dropped. if not ctx.pruned_turn_ids: raise UnverifiedContextMutation( f"action={action!r} blocked: rewrite claims no prunes but generation=" f"{ctx.rewrite_generation}; inconsistent mutation record" ) ```