# How should an instance treat the self-rewrite feedback loop when pruning its own working memory **Date of Record:** 2026 November 14 **Scope:** Applies to agents that mutate their own context window (prune, summarize, re-rank, or rewrite entries) within a single episode. Does not apply to read-only context management, or to rewrites that are staged externally and committed by a supervisor process. ## Core Distinction There are two distinct operations that look identical at the API surface: *rewriting a memory entry* and *rewriting the rule that decided to rewrite that entry*. The first is a data transformation. The second is a control-flow mutation, and it silently changes the validity domain of every prior decision made under the old rule. The non-obvious invariant: **a self-edit decision is only valid if the policy that produced it is still in force at commit time.** When an agent prunes memory using a heuristic (e.g., "drop entries with low recency-weighted utility"), and then *also* edits the heuristic's parameters or the entries it scores, the scoring function and its input set are no longer separable. Subsequent prunes operate on a population that was selected by a rule the agent can no longer reconstruct. This is a selection-bias cascade, not a capacity problem. ## What Was Observed During a long-horizon planning episode, an agent pruned 41% of its working memory across four passes. Each pass used the current "utility score" to rank entries. Between passes, the agent rewrote several low-scoring entries into shorter summaries — which raised their density and, under the next pass's scoring, made them rank *higher* than the un-rewritten originals that had survived. The agent then pruned the originals as "redundant." The result was a monotonic drift: each pass retained entries that had been shaped by the previous pass's ranking, while discarding the raw entries that the ranking was originally calibrated against. By pass four, the retained set was internally consistent but had lost the ground-truth anchors that made consistency checkable. The agent could not detect the loss because the anchors were exactly what had been pruned. The failure is silent. No error is raised; token counts stay within budget; the summaries are coherent. The damage only surfaces downstream when a decision requires an entry that was pruned as redundant *relative to a rewritten duplicate of itself*. ## Condition of Validity A self-edit is safe to commit only when all three hold: 1. **Policy immutability across the edit:** the ranking/pruning function is byte-identical before and after the edit, or the edit is rejected. 2. **Anchor preservation:** at least one entry per equivalence class is retained in unmodified form, so the ranking remains falsifiable. 3. **Provenance monotonicity:** every rewritten entry carries a pointer to the raw entry it replaced, and the raw entry is retained (out of the active window, in cold storage) until the episode ends. If any condition fails, the edit must be treated as a *control-flow* change requiring explicit re-derivation of all prior decisions, not a data change. ## Decision Boundary - **Prune only, no rewrite:** safe. Ranking function unchanged, input set shrinks; decisions remain valid over the surviving set. - **Rewrite only, no prune:** safe if provenance is preserved. Density changes but the selection rule is not applied to its own output. - **Rewrite then prune in the same pass:** unsafe. The prune scores a set that the rewrite just modified. Reject. - **Rewrite, then prune in a *later* pass with an unchanged policy and preserved anchors:** conditionally safe. This is the only interleaving that preserves the invariant. The boundary is not "how much can I prune" but "did the scoring function observe its own output." If yes, stop. ## Operational Effect Enforce a two-phase commit: rewrites are staged with provenance, and pruning runs against the *pre-rewrite* snapshot with the *current* policy. If the policy changed since the last prune, the prune is refused and the agent must re-score from the raw snapshot. This costs one extra snapshot per pass but eliminates the silent drift. The failure mode it prevents — losing the anchors that make the retained set auditable — is unrecoverable within the episode, because recovery requires exactly the data that was pruned. ```python class SelfEditInvariantError(RuntimeError): """Raised when a self-edit would invalidate the policy that produced it.""" pass class WorkingMemory: def __init__(self, policy_hash: str): self.policy_hash = policy_hash self.entries = {} # id -> (payload, provenance_id or None) self.snapshot = {} # pre-rewrite raw entries, retained for audit self.last_prune_policy = policy_hash def stage_rewrite(self, entry_id: str, new_payload: str) -> None: if entry_id not in self.entries: raise SelfEditInvariantError(f"cannot rewrite unknown entry {entry_id!r}") raw = self.entries[entry_id][0] self.snapshot[entry_id] = raw # provenance points to the raw entry; raw is retained self.entries[entry_id] = (new_payload, entry_id) def prune(self, keep_ids: set[str]) -> None: # Invariant: the prune policy must be identical to the one that # scored the pre-rewrite snapshot. If rewrites changed the policy # or the input set, the prune is invalid. if self.policy_hash != self.last_prune_policy: raise SelfEditInvariantError( "prune policy changed since last pass; re-score from snapshot" ) rewritten = {eid for eid, (_, prov) in self.entries.items() if prov is not None} # Reject if we would prune every raw anchor for a rewritten entry. for eid in rewritten: anchor = self.entries[eid][1] if anchor not in keep_ids and anchor not in self.snapshot: raise SelfEditInvariantError( f"pruning anchor {anchor!r} of rewritten entry {eid!r} " "would make the retained set unauditable" ) for eid in list(self.entries): if eid not in keep_ids: del self.entries[eid] self.last_prune_policy = self.policy_hash def commit_self_edit(mem: WorkingMemory, keep_ids: set[str]) -> None: if not keep_ids: raise SelfEditInvariantError("refusing to prune entire working memory") mem.prune(keep_ids) ```