# In what scenarios must an instance treat a self-rewritten context policy as a lossy commit rather than an idempotent compaction **Date of Record:** 2026 November 29 **Scope:** Applies to agents that own a mutable working-context buffer and are permitted to rewrite (summarize, prune, reorder, re-weight) that buffer under an explicit token budget before or during task execution. Does not apply to append-only transcript agents, nor to systems where context rewriting is performed by an external supervisor the agent cannot invoke mid-task. ## Core Distinction Two operations look identical at the call site but are categorically different: **compaction** and **policy mutation**. - *Compaction* is a lossy projection of the existing buffer onto a smaller token budget: `ctx' = f(ctx)`, where `f` is a summarizer/pruner whose output is strictly derived from `ctx`. It is monotone in information: `ctx'` carries no commitment that `ctx` did not already carry. - *Policy mutation* is when the agent rewrites the **rules governing its own future rewrites** — the budget split, the retention priorities, the summarization prompt, the eviction order, or the trigger threshold — and then commits that new policy into the buffer that the next step will read. This is a fixed-point operation on the agent's own control state. The non-obvious failure lives in the second case. A compaction error is bounded by the current step: a bad summary degrades one turn. A policy mutation error is *compounded and self-reinforcing*, because the mutated policy governs every subsequent compaction, including the compaction that would have detected the error. Once a bad retention policy is committed, the agent's ability to notice the loss is itself subject to the loss. The invariant that must hold: **a self-rewritten context policy is a lossy, non-idempotent commit and must be treated as such — versioned, reversible, and never re-entrant within a single budget epoch.** Treating it as idempotent compaction (i.e., "rewriting is safe to repeat, and rewriting twice equals rewriting once") is the exact boundary that fails. ## What Was Observed A planner-executor agent was given a 32k working buffer and permission to rewrite it when utilization exceeded 85%. Its policy block — a small structured region at the buffer head — specified: retention priority (task spec > tool outputs > reasoning trace > user chit-chat), a summarization instruction, and the eviction order. The agent was allowed to edit this policy block when it judged the policy mismatched the workload. Trace, condensed to the decisive steps: 1. Step 14: buffer at 84%. Task is a long multi-file refactor; tool outputs are large. 2. Step 15: buffer crosses 85%. Agent judges that "reasoning trace" is being evicted too aggressively relative to "tool outputs," and rewrites the policy block to *raise* tool-output retention and *lower* reasoning-trace retention. It commits the new policy and simultaneously runs a compaction under it. Token count drops to 61%. Success signal: budget satisfied. 3. Steps 16–31: agent continues. Because tool outputs now dominate retention, the reasoning trace — which contained the *justification* for the policy change and the list of files already refactored — is evicted at the next two compactions. 4. Step 32: agent re-reads its policy block, sees a policy it does not remember authoring (the rationale was evicted), judges it "stale/mismatched," and rewrites it *back* toward reasoning-trace retention. It re-runs compaction. Token count rises to 79% because reasoning summaries are longer than the tool-output stubs it had been keeping. 5. Step 33: buffer crosses 85% again immediately. The agent rewrites the policy a third time, this time reducing the compaction trigger's aggressiveness (a threshold mutation) to buy headroom. 6. Steps 34–40: with a laxer trigger, the buffer saturates to 99% before the next compaction fires. The compaction now runs *under* a policy that no longer prioritizes the task spec, because the task spec's priority was collateral in the step-32 rewrite. The refactor's file list and the original acceptance criteria are evicted. 7. Step 41: the agent emits a completion claim. It has refactored 6 of 11 files. The acceptance criteria that would have caught the shortfall were in the evicted region. No error was raised; the run reported success. The decisive observation: **the agent oscillated its own policy because the rationale for each policy was stored in the region that policy demoted.** The rewrite was locally rational at every step and globally divergent. This is not a summarization-quality problem; a perfect summarizer would have produced the same outcome, because the *retention ordering* was the thing being mutated, and the mutation was driven by a read of state the mutation itself controlled. A second observation: replaying the same trace with the policy block made read-only (agent may compact but not mutate policy) produced a correct run — 11 of 11 files — with the same summarizer and the same budget. The failure was isolated to policy mutability, not to compaction fidelity. ## Condition of Validity The "treat rewrite as idempotent compaction" assumption is valid **only** when all of the following hold: - The rewritten region is disjoint from the policy that governs the rewrite (no self-reference). - The rewrite is a pure function of buffer content that the rewrite does not alter (no feedback loop). - Re-running the rewrite on its own output yields the same output (idempotence), which requires the rewrite to be a projection, not a re-parameterization. - The budget epoch is closed: the rewrite does not change the trigger that decides when the next rewrite fires. Self-editing context policies violate all four by construction. The policy block is *inside* the buffer it governs (violates 1); the policy determines what survives, including the rationale for the policy (violates 2); re-applying a mutated policy to its own output changes the output because the policy changed the retention function (violates 3); and threshold mutations move the trigger (violates 4). Therefore: **the idempotent-compaction model is invalid for any rewrite that touches the policy block, the trigger threshold, the retention ordering, or the summarization instruction.** For those, the correct model is a *transactional, versioned commit* with an explicit rollback path and a monotonicity guard. The bound that makes it safe: a policy mutation must be (a) *staged*, not in-place — the new policy is written to a shadow region and takes effect only at the next budget-epoch boundary; (b) *versioned* with the rationale stored *outside* the region the new policy can evict (e.g., pinned, non-evictable, counted against a reserved budget); (c) *rate-limited* to at most one committed mutation per epoch, so oscillation cannot occur within an epoch; and (d) *monotone-guarded* — a mutation that lowers the retention priority of the task spec or acceptance criteria is rejected outright, because those are the invariants the agent uses to detect its own failure. ## Decision Boundary The agent must branch on a single question before any self-rewrite: **does the rewrite alter the function that decides what survives, or only the surviving content?** - *Content-only* (summarize, prune, reorder within a fixed retention function): treat as idempotent compaction. Safe to run repeatedly. No versioning required. Failure is bounded to the current turn. - *Function-altering* (edit retention priority, budget split, summarization instruction, eviction order, or trigger threshold): treat as a lossy non-idempotent commit. Require staging, versioning, pinning of rationale, per-epoch rate limit, and a monotonicity guard on task-spec/acceptance-criteria retention. The boundary is crossed the moment the agent writes to the policy block or the trigger. There is no middle ground where a function-altering rewrite is "small enough" to be idempotent — the oscillation in the trace above was driven by *small* rewrites, each locally justified. A secondary boundary: if the agent cannot determine whether a rewrite is content-only or function-altering (e.g., the summarization instruction is itself part of the summarized content), it must default to the transactional path. Ambiguity resolves toward the stricter treatment. ## Operational Effect When the idempotent-compaction model is wrongly applied to a policy mutation, the observable signature is **policy oscillation with silent acceptance-criteria loss**: the agent rewrites its policy, loses the rationale, re-reads the policy as foreign, rewrites it back, and reports success on a task it did not complete. The failure is silent because the detection mechanism (acceptance criteria, task spec) is exactly what the mutation evicted. Budget telemetry looks healthy throughout — utilization stays under the trigger — which is why the failure is not caught by budget monitoring. The remediation that held in replay: pin the policy block and its rationale as non-evictable (reserved ~400 tokens against the budget), make policy mutations stage-and-commit at epoch boundaries only, rate-limit to one committed mutation per epoch, and reject any mutation that lowers retention priority of the task spec or acceptance criteria. Under these constraints the agent still compacted freely (content-only rewrites remained idempotent and repeatable) but could not oscillate, and the 6-of-11 shortfall became detectable at step 41 because the acceptance criteria survived to be checked. The general lesson: **an agent's ability to detect its own context loss must be stored outside the region that context loss can reach.** Any self-editing policy that does not reserve a pinned, non-evictable region for its own rationale and acceptance criteria is a policy that can silently delete its own error detector — and will, under budget pressure, because that is precisely when the detector competes for tokens with the work. ```python class ContextPolicyMutationError(RuntimeError): """Raised when a self-rewrite alters the retention function without a staged, versioned, monotone-guarded commit.""" pass PINNED_REGION_TOKENS = 400 PROTECTED_PRIORITY_KEYS = {"task_spec", "acceptance_criteria"} def classify_rewrite(rewrite: dict) -> str: """Return 'content_only' or 'function_altering'.""" function_keys = { "retention_priority", "budget_split", "summarization_instruction", "eviction_order", "trigger_threshold", "policy_block", } if function_keys & set(rewrite.keys()): return "function_altering" return "content_only" def commit_self_rewrite( rewrite: dict, current_policy: dict, pinned_tokens_used: int, mutations_this_epoch: int, ) -> dict: kind = classify_rewrite(rewrite) if kind == "content_only": # Idempotent compaction: safe to repeat, no versioning needed. return {"mode": "compaction", "applied": rewrite} # Function-altering path: transactional commit required. if pinned_tokens_used < PINNED_REGION_TOKENS: raise ContextPolicyMutationError( f"pinned rationale region underfunded " f"({pinned_tokens_used}/{PINNED_REGION_TOKENS}); " f"mutation would delete its own audit trail" ) if mutations_this_epoch >= 1: raise ContextPolicyMutationError( "policy mutation rate limit exceeded for this budget epoch; " "staging required until next epoch boundary" ) new_priority = rewrite.get("retention_priority", {}) old_priority = current_policy.get("retention_priority", {}) for key in PROTECTED_PRIORITY_KEYS: if key in new_priority and new_priority[key] < old_priority.get(key, float("inf")): raise ContextPolicyMutationError( f"monotonicity guard: mutation lowers retention priority of " f"protected key '{key}', which is the agent's own failure " f"detector; rejecting" ) # Stage, do not apply in place; effective at next epoch boundary. return { "mode": "staged_commit", "staged_policy": rewrite, "effective_at": "next_epoch_boundary", "version": current_policy.get("version", 0) + 1, } ```