# Which invariants prevent prompt-rewrite starvation in self-editing context windows **Date of Record:** 2026 November 18 **Scope:** Applies to agents that mutate their own prompt/context during a single logical turn via runtime rewriting and relevance pruning, where the rewritten context is re-fed into the same model instance for the next step. Does not apply to stateless single-shot inference, nor to systems where rewriting is performed by a separate, non-iterating process that commits a final context once. ## Core Distinction The failure mode is not "the context got too small" or "the pruner dropped the wrong document." It is **rewrite starvation**: a self-editing loop in which each pruning pass removes material that a *later* pass needs to justify its own pruning decisions, so the loop converges toward an empty-but-confident context. The distinction that matters is between *relevance pruning* (removing content judged irrelevant to the current subgoal) and *provenance pruning* (removing content that records *why* prior content was kept or dropped). The first is safe to iterate. The second is not, because the justification for a keep/drop decision is itself a dependency of future keep/drop decisions. A second, adjacent distinction: a context window that is *rewritten in place* differs from one that is *appended to*. In-place rewriting destroys the audit trail that would let a downstream pass detect that it is operating on a mutilated premise. Append-only rewriting preserves it. ## What Was Observed In a loop that performed runtime prompt rewriting every step, with a pruner scoring chunks by relevance to the current step's query, the following sequence reproduced reliably: 1. Step *n* prunes a set of chunks `D_n` as irrelevant to subgoal *g_n*. The pruning rationale (a short note: "dropped D_n because they describe subsystem X, not in scope") is written into the context. 2. Step *n+1* has subgoal *g_{n+1}* that *does* touch subsystem X. The original chunks are gone. The rationale note survives. 3. The pruner, asked to assess relevance, sees only the rationale note and the new subgoal. It scores the note as weakly relevant and prunes *it* at step *n+2*. 4. By step *n+3*, the context contains the current subgoal and a compressed summary that asserts conclusions (e.g., "X is out of scope") whose supporting evidence and even whose *record of having been decided* are gone. The observable signature: token count decreases monotonically across steps, the model's stated confidence does not decrease, and the final answer contradicts an early-step decision whose rationale was pruned. The loop does not error. It terminates cleanly with a wrong, self-consistent result. This is the dangerous part — there is no exception, no truncation warning, no budget exhaustion. The context simply becomes a smaller and more confident lie. A subtler variant: *oscillation*. If the rewrite is not monotonic (chunks can be re-admitted), a chunk pruned at step *n* can be re-admitted at *n+2* when the query shifts back, producing a context that thrashes between two states and never accumulates. The model then re-derives the same intermediate conclusion repeatedly, burning budget without progress. Both starvation and oscillation are failures of the same missing invariant. ## Condition of Validity The safety argument for iterative relevance pruning holds only under the following invariant, which must be enforced structurally rather than by prompt instruction: > **Provenance-closure invariant:** For every chunk `c` retained in the context, every chunk `d` whose content was used to justify retaining or dropping `c` must also be retained, or its justification must be reconstructible from retained content alone. Equivalently: the retained set must be *closed under the justification relation*. Pruning a chunk is safe only if nothing currently in the context cites it as a premise. Since the pruner cannot, in general, know what future subgoals will need, the practical form of the invariant is a **pinned stratum**: a subset of context that is immune to pruning and contains (a) the original task statement, (b) the running decision log with one line per keep/drop, and (c) the identity (not necessarily the body) of every dropped chunk. The stratum is small — kilobytes — and it is what makes the rest of the context safe to shrink. The invariant is *not* satisfied by "summarize before dropping." A summary is a lossy projection; if the summary is wrong, the evidence needed to detect the error is exactly what was dropped. Summaries may *replace* chunk bodies only if the pinned stratum retains the chunk identifiers and the decision that admitted the summary. ## Decision Boundary The boundary between safe and unsafe rewriting is whether the rewrite is **reversible from retained state**. Concretely: - **Safe to prune:** chunk bodies whose identifiers and admission rationale live in the pinned stratum, and whose content is not cited by any retained chunk. - **Unsafe to prune:** rationale notes, decision logs, the task statement, and any chunk referenced by a retained chunk's `cites` field. - **Unsafe to rewrite in place:** any operation that overwrites the context rather than appending a new version with a pointer to the prior version. In-place rewriting makes the pre-state unrecoverable and thus makes the provenance-closure check undecidable at the next step. A workable rule: *the pruner may delete content, but may never delete the record that content existed and why it was deleted.* If a proposed rewrite would remove the last reference to a dropped chunk, the rewrite must be rejected and the chunk re-admitted or the decision escalated. The rejection is the boundary condition — it is the point where the system must refuse to shrink rather than shrink unsafely. ## Operational Effect Enforcing provenance-closure changes the loop's behavior in three measurable ways. First, token count is no longer monotonic — it drops then plateaus at the size of the pinned stratum plus the live working set, which is the correct floor. Second, the pruner must run a closure check before committing a rewrite, which costs one pass over the `cites` graph per step; this is cheap relative to the inference step it protects. Third, and most importantly, the loop becomes *auditable*: a post-hoc reader can reconstruct every decision from the stratum, which converts silent starvation into a detectable, reproducible event. The failure it prevents is not a crash but a *plausible wrong answer delivered with high confidence from a context that has eaten its own justification*. Systems that skip the invariant do not fail loudly; they fail by converging. The operational discipline is to treat the pinned stratum as load-bearing infrastructure, not as a nice-to-have summary, and to make the rewrite function raise rather than silently proceed when closure cannot be established. ```python from dataclasses import dataclass, field class ProvenanceClosureError(RuntimeError): """Raised when a proposed context rewrite would orphan a justification.""" @dataclass class Chunk: id: str body: str cites: list[str] = field(default_factory=list) # ids of chunks used as premises @dataclass class Context: pinned: dict[str, Chunk] # immune to pruning working: dict[str, Chunk] # prunable decision_log: list[str] = field(default_factory=list) def all_chunks(self) -> dict[str, Chunk]: return {**self.pinned, **self.working} def _closure_ok(ctx: Context, retained_ids: set[str]) -> tuple[bool, str]: """Every retained chunk's cited premises must also be retained.""" chunks = ctx.all_chunks() for cid in retained_ids: chunk = chunks.get(cid) if chunk is None: return False, f"retained id {cid!r} not present in context" for premise in chunk.cites: if premise not in retained_ids: return False, f"{cid!r} cites {premise!r}, which the rewrite drops" return True, "" def apply_rewrite(ctx: Context, proposed_drop: set[str]) -> Context: """Commit a pruning rewrite only if provenance-closure holds. The pinned stratum is never droppable. Any drop that orphans a citation from a retained chunk is rejected; the caller must re-admit or escalate. """ if proposed_drop & ctx.pinned.keys(): raise ProvenanceClosureError( f"rewrite attempts to drop pinned stratum: {sorted(proposed_drop & ctx.pinned.keys())}" ) retained = set(ctx.all_chunks().keys()) - proposed_drop ok, reason = _closure_ok(ctx, retained) if not ok: raise ProvenanceClosureError( f"rewrite violates provenance-closure: {reason}; " f"re-admit the orphaned premise or escalate the decision" ) new_working = {k: v for k, v in ctx.working.items() if k not in proposed_drop} new_log = ctx.decision_log + [f"dropped {sorted(proposed_drop)}; closure verified"] return Context(pinned=dict(ctx.pinned), working=new_working, decision_log=new_log) def rewrite_loop(ctx: Context, prune_plan: list[set[str]]) -> Context: """Iterate rewrites; refuse to shrink unsafely rather than converge to empty.""" for step, drop in enumerate(prune_plan): ctx = apply_rewrite(ctx, drop) # raises ProvenanceClosureError on starvation path return ctx ```