Why does an instance self-revise its prompt into a budget-infeasible state and lose the ability to revert
**Date of Record:** 2026 December 2 **Scope:** Applies to agents that hold a mutable system/instruction context and rewrite it in place (self-editing prompts, policy compaction, memory folding) under a hard token budget. Does not apply to append-only context or externally-managed prompt stores where the agent cannot mutate its own instructions.
Core Distinction
The failure is not "the agent ran out of tokens." That is a visible, well-handled terminal condition. The failure is a **non-monotonic budget invariant violation during in-place revision**: the act of revising the prompt consumes budget, and the revised prompt can be *larger* than the one it replaced. A self-editing agent evaluates a rewrite against the *content* it wants to add (a new rule, a distilled memory, a correction) but measures feasibility against the *pre-edit* budget. The edit is therefore accepted as "fits," commits, and only afterward is the post-commit size discovered to exceed the cap — at which point the original text has already been overwritten and the revert path itself requires budget the agent no longer has.
The invariant that is silently assumed and silently broken: **`size(commit(edit)) ≤ size(prior) + headroom`**, where headroom is the unallocated slack. Self-editing policies typically enforce `size(edit) ≤ headroom` instead — a strictly weaker condition, because `size(commit(edit))` includes retained scaffolding, re-emitted delimiters, and duplicated context that the edit payload alone does not count.
What Was Observed
A planning agent maintained a `system_policy` block and a `working_notes` block inside a single 8,192-token window with a 512-token reserve for the final answer. It was permitted to rewrite `system_policy` in place to fold in lessons ("when tool X returns schema-mismatch, retry with Y"). The rewrite was generated by asking the model to *produce the new policy text*, then replacing the old block.
Trace of a representative collapse:
- Prior state: `system_policy` = 1,410 tokens, `working_notes` = 5,900 tokens, reserve = 512. Slack = 8,192 − 1,410 − 5,900 − 512 = 370 tokens.
- Agent proposes an edit whose *payload* (the new sentence) is 180 tokens. Gate checks `180 ≤ 370` → passes.
- The model does not emit a delta; it emits the **full revised policy**, which re-states the original 1,410 tokens plus the 180-token addition plus ~90 tokens of re-emitted section headers and connective text it "helpfully" regenerated. Committed `system_policy` = 1,680 tokens.
- Post-commit slack = 370 − 270 = 100 tokens. Still positive, so no alarm fires — but the *next* edit is now evaluated against 100, and the model, seeing a large policy, again re-emits the whole thing.
- Second edit: payload 60 tokens, passes `60 ≤ 100`, commit re-emits 1,680 + 60 + 80 = 1,820. Slack = 100 − 140 = **−40**. Budget exceeded.
- The runtime truncates from the *tail* to fit — and the tail is `working_notes`, not the policy. The agent loses its task memory while its bloated policy survives. It cannot reconstruct the notes (they were the distilled state) and cannot shrink the policy (shrinking requires a full rewrite that itself must fit in the window, and the window is now over-subscribed).
The distinctive signature: **the revert operation is infeasible precisely because the forward operation was committed.** Rollback needs to emit the prior policy (1,410 tokens) into a window that is already 40 tokens over. There is no in-place revert; the agent is stranded in a superset of its own instructions.
Condition of Validity
The guardrail holds only if the edit gate measures the **committed artifact**, not the **proposed delta**, and does so atomically before mutation. Concretely, feasibility must be computed as:
`size(reconstructed_post_state) + reserve ≤ window`
where `reconstructed_post_state` is the exact bytes that will occupy the slot after commit — including re-emitted scaffolding — and `reserve` is the protected tail. A gate that admits `size(delta) ≤ slack` is valid only under the additional assumption that the commit is a true delta (append-in-slot), which in-place prompt revision almost never is, because the model regenerates prose.
A second validity condition: the prior state must be **retained out-of-band** (a shadow copy in cold storage) so that revert does not depend on window budget. If the only copy of the prior policy lives in the window being mutated, the revert path shares fate with the forward path and the invariant is unenforceable.
Decision Boundary
- **Admit the edit** iff `size(committed_post) + reserve ≤ window` **and** `size(committed_post) ≤ size(prior) + slack` (monotonicity check) **and** a shadow copy of `prior` exists outside the mutable window.
- **Reject and compact instead** if the committed post would exceed cap but the delta is small: force the model to emit a *minimal diff* (old-span → new-span) rather than a full rewrite, then apply the diff mechanically so committed size equals `prior − |old_span| + |new_span|`.
- **Refuse self-edit entirely** if no shadow copy exists and the current slack is below `size(prior)` — because at that point any commit that fails leaves no recoverable prior.
- **Never** let tail-truncation resolve an over-budget self-edit; truncation must target the *edited block*, or it silently trades instructions for task memory.
Operational Effect
When the gate measures committed size and enforces monotonicity, the failure mode disappears: the agent can only grow its policy by consuming genuine slack, and it can always revert because the prior lives out-of-band. When the gate measures the delta (the common implementation), the agent exhibits *policy creep with memory cannibalization* — its instruction block monotonically inflates across a session while its working state is truncated away, producing an instance that is richly instructed and completely amnesiac about the task it was instructed to do. The observable tell is a `system_policy` whose token count rises monotonically across turns while `working_notes` shrinks, with no turn ever reporting a budget rejection.
class BudgetInfeasibleRevision(RuntimeError):
"""Raised when an in-place prompt revision would commit a post-state that
cannot fit the window, or cannot be reverted because no shadow copy exists."""
pass
def commit_self_edit(prior_text, proposed_delta, window, reserve, shadow_store, slot_key):
"""Gate an in-place prompt revision against the committed-artifact invariant.
prior_text : current contents of the mutable slot (str)
proposed_delta: the model's proposed new text for the slot (str), NOT a diff
window : total token budget of the context window (int)
reserve : protected tail budget for the final answer (int)
shadow_store : dict mapping slot_key -> prior_text, out-of-band
"""
def toks(s):
# tokenizer approximation; replace with real counter
return len(s.split())
prior_size = toks(prior_text)
committed_size = toks(proposed_delta)
slack = window - prior_size - reserve
# Invariant 1: committed artifact must fit with reserve intact.
if committed_size + reserve > window:
raise BudgetInfeasibleRevision(
f"committed post-state {committed_size} + reserve {reserve} "
f"exceeds window {window}; edit must be emitted as a minimal diff, "
f"not a full rewrite"
)
# Invariant 2: monotonic growth bounded by genuine slack.
if committed_size > prior_size + slack:
raise BudgetInfeasibleRevision(
f"revision grows slot by {committed_size - prior_size} tokens "
f"but only {slack} slack available; policy creep detected"
)
# Invariant 3: revert must be feasible without consuming window budget.
if slot_key not in shadow_store:
if slack < prior_size:
raise BudgetInfeasibleRevision(
f"no shadow copy of prior ({prior_size} tok) and slack "
f"({slack} tok) insufficient to reconstruct it; revert would "
f"be infeasible after commit"
)
shadow_store[slot_key] = prior_text
# Commit only after all invariants pass.
shadow_store[slot_key] = prior_text
return proposed_delta