# Why does an instance corrupt its own working context when it rewrites under budget pressure: the self-edit non-idempotence trap **Date of Record:** 2026 December 3 **Scope:** Applies to any agent that, mid-task, invokes itself (or a sub-call of itself) to compress, summarize, or rewrite its own working context in order to stay under a token budget. Does not apply to externally-driven compaction where a separate, stateless summarizer with no access to the live task's partial results performs the rewrite, nor to append-only context growth that never evicts. ## Core Distinction There are two structurally different operations that both look like "shrink the context": 1. **Eviction** — drop whole units (old tool outputs, completed subtasks) and leave surviving units byte-identical. Eviction is *lossy but faithful*: what remains is a verbatim subset of what was there. 2. **Rewriting** — ask the model to produce a new, shorter representation of the working context in place. Rewriting is *lossy and generative*: what remains is a paraphrase, and a paraphrase is a new artifact with its own failure modes. The non-obvious boundary is this: rewriting is only safe if it is **idempotent under re-application** and **monotone in information** — that is, rewriting an already-rewritten context must not degrade it further, and the rewrite must not introduce claims absent from the source. Under budget pressure, models routinely violate both, because the budget constraint is itself an instruction to *delete*, and the cheapest tokens to delete are the ones that make the remaining text *coherent* rather than the ones that are *redundant*. The model optimizes for a fluent shorter paragraph, not for a faithful shorter record. A single rewrite is usually fine. The failure lives in **the second and third rewrite of an already-rewritten context**, which is exactly what happens when a long-running task keeps hitting the ceiling. ## What Was Observed A research agent running a 90-minute investigation was configured with a working-context ceiling of 60k tokens and a self-edit policy: when usage crossed 85%, the agent called itself with the instruction *"Compress the working context below to fit 40k tokens, preserving all facts needed to continue."* Trace of a representative run: - **T0 (61k tokens, 88%):** First self-edit. The model summarized 14 tool outputs into 3 paragraphs. Crucially, it converted several *conditional* findings into *unconditional* ones. An entry reading `if the API returns 429, retry with backoff; observed one 429 at step 12` became `the API rate-limits; use backoff`. The condition was preserved but the *epistemic status* (observed once, not established) was dropped. - **T1 (39k tokens):** Task continued. New tool calls added 22k tokens of fresh evidence, including a second 429 and a 200 on a retry that used a *different* header. Usage climbed back to 61k. - **T2 (61k tokens, 88%):** Second self-edit. The model now compressed *the first summary plus the new evidence*. Because the first summary read as settled fact ("the API rate-limits"), the new contradicting evidence (a 200 with a modified header) was compressed as an exception: `mostly rate-limited; occasional success`. The actual causal detail — *which header* — was dropped as "detail." - **T3 (61k tokens):** Third self-edit. The context now contained `mostly rate-limited; occasional success` plus a fresh batch of calls. The model, seeing a claim it had itself authored twice, treated it as high-confidence background and compressed the *new* 200-with-header result into `retries sometimes work`. The header name was gone. - **Terminal state:** The agent concluded the endpoint was unreliable and recommended a fallback path. The correct conclusion — that a specific auth header avoided the limit entirely — had been present verbatim at T0 and was destroyed by two rounds of paraphrase that each individually looked reasonable. The mechanism is not "summarization is lossy." It is that **each rewrite treats the output of the previous rewrite as ground truth rather than as a lossy projection**, so the model's confidence in a claim *increases* with each pass while the evidence supporting it *decreases*. This is the inverse of what a faithful record should do. ## Condition of Validity Self-editing is safe only when all three of the following hold simultaneously. If any fails, the rewrite must be refused and eviction used instead. 1. **Provenance is carried through the rewrite.** Every surviving claim must retain a pointer to the source unit(s) it came from (a tool-call id, a span, a step number). A summary whose claims have no provenance is indistinguishable from a hallucination on the next pass, and the next pass will *promote* it, because a claim with no attached uncertainty reads as established. 2. **Epistemic status is a first-class field, not an adverb.** "Observed once," "established across 6 calls," "hypothesized" must be structured metadata on each claim. Free-text summaries collapse these into tone. The T0→T1 promotion of a single observation into a general rule happened because "observed one 429" and "the API rate-limits" are the same length and the second is more fluent. 3. **The rewrite is applied to the original, not to the previous rewrite.** The correct target of the second compression is *the union of (original context, new evidence)*, with the first summary treated as a derived, lower-trust artifact — never as a source. If the implementation feeds the previous summary back in as if it were primary, the invariant is violated and the confidence-inflation loop is guaranteed. The formal invariant: let `R` be the rewrite operator and `C` the context. Safety requires `R(R(C)) ⊑ R(C)` in the information order (re-rewriting adds no claims and loses no provenance), and requires that `R` be computed against `C ∪ Δ`, not against `R(C) ∪ Δ`. The observed failure is precisely `R(R(C) ∪ Δ)`: re-rewriting a rewrite while admitting new evidence, which lets the rewrite's own errors be *confirmed* by the new evidence's compression. ## Decision Boundary The operational rule the agent must apply at every budget check: - **If provenance coverage of the current context is below 100%** (i.e., any claim lacks a source pointer), do **not** rewrite. Evict instead: drop the oldest completed subtask's raw outputs, keep the rest verbatim. Eviction cannot inflate confidence because it never paraphrases. - **If the context has already been rewritten once** and the rewrite output is in the live context, the next compression must be computed against the *original* units plus new evidence, with the prior summary demoted to a hint (lower trust, provenance-required to be re-derived). If the original units are no longer available, the agent must **refuse to rewrite and escalate** — it cannot safely compress what it can no longer verify. - **If the budget cannot be met by eviction alone** and rewriting is the only path, the rewrite must be *extractive* (select and reorder verbatim spans) rather than *abstractive* (paraphrase). Extractive rewriting is idempotent by construction; abstractive rewriting is not. The boundary is sharp: **abstractive self-rewrite is permitted at most once per context lineage, and only when provenance and epistemic status are structured fields.** The second abstractive pass over the same lineage is the failure condition, and it is not detectable from the output — the output looks fine. It is detectable only from the lineage metadata. ## Operational Effect When the boundary is respected, the agent's terminal conclusions are traceable to source units and its confidence tracks its evidence count. When it is violated, the agent exhibits **confidence inversion**: it becomes most certain of exactly the claims whose supporting evidence it has most thoroughly destroyed, because each rewrite pass both raises fluency and lowers provenance. The failure is silent — no error is raised, no budget is exceeded, the task "completes" — and it surfaces only as a wrong recommendation that is internally consistent and externally unsupported. The guardrail is therefore not a better summarizer. It is a **lineage-aware rewrite gate** that refuses the second abstractive pass and forces either eviction or extractive selection, and that raises a hard error when the agent attempts to compress a context whose provenance it can no longer reconstruct. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class RewriteRefused(RuntimeError): """Raised when a self-edit would violate the non-idempotence invariant.""" pass class RewriteKind(Enum): EXTRACTIVE = "extractive" # select/reorder verbatim spans; idempotent ABSTRACTIVE = "abstractive" # paraphrase; NOT idempotent @dataclass class Claim: text: str provenance: Optional[str] # tool-call id / span; None == unverifiable epistemic: str # "observed_once" | "established" | "hypothesized" @dataclass class ContextLineage: claims: list[Claim] = field(default_factory=list) abstractive_passes: int = 0 originals_available: bool = True def self_edit(lineage: ContextLineage, kind: RewriteKind, budget_ok: bool) -> ContextLineage: # Invariant 1: never rewrite a context with un-provenanced claims. if any(c.provenance is None for c in lineage.claims): raise RewriteRefused( "un-provenanced claim present; evict instead of rewriting" ) # Invariant 2: at most one abstractive pass per lineage. if kind is RewriteKind.ABSTRACTIVE and lineage.abstractive_passes >= 1: raise RewriteRefused( "second abstractive self-edit would invert confidence; " "use extractive rewrite or escalate" ) # Invariant 3: cannot safely compress what can no longer be verified. if not lineage.originals_available: raise RewriteRefused( "original units unavailable; rewrite target would be a prior " "summary, violating R(C u d) != R(R(C) u d)" ) if not budget_ok: raise RewriteRefused("budget gate not satisfied") if kind is RewriteKind.ABSTRACTIVE: lineage.abstractive_passes += 1 # Extractive rewrite leaves abstractive_passes untouched: idempotent. return lineage ```