# What structural signals indicate self-editing context policies have crossed a prompt budget reallocation invariant **Date of Record:** 2026 November 18 **Scope:** Applies to agents that dynamically rewrite their own context policy (segment priorities, eviction thresholds, summarization triggers) under a fixed token budget, where reallocation is triggered by observed structural signals rather than by an external scheduler. Does not apply to static prompt templates, to externally orchestrated budget managers, or to systems where the context window is unbounded. ## Core Distinction The failure mode is not budget exhaustion. Budget exhaustion is loud: the token counter hits the ceiling, the allocator throws, and the agent degrades visibly. The non-obvious boundary is *budget conservation without budget correctness* — the self-editing policy reallocates tokens between segments such that the total stays at or below the ceiling, every individual check passes, and yet the context has silently become unsatisfiable for the current task. The invariant that breaks is not `sum(allocations) <= budget`. It is `sum(allocations) <= budget AND every surviving segment retains the referents its consumers depend on`. The first is arithmetic and is enforced by the allocator. The second is structural and is enforced by nothing, because the policy that would enforce it is the same object being edited. ## What Was Observed Across a run of self-editing agents performing long-horizon retrieval-and-synthesis tasks, the following structural signals preceded every context collapse by one to three reallocation cycles: 1. **Referent orphaning ratio rises while total utilization is flat.** The policy evicts a low-priority segment (typically raw retrieval snippets) and promotes a summary whose token cost is lower. Total utilization is unchanged. But the summary contains forward references ("the third document", "the config above") whose antecedents lived in the evicted segment. The signal is the ratio of unresolved anaphora to total segments, not token counts. 2. **Summary-to-source depth exceeds one.** The policy summarizes a segment that is itself a summary. Each self-edit that summarizes a summary compounds loss. The detectable signal is the depth counter on any segment reaching 2+, which correlates with reallocation events at roughly 0.7 in the observed runs and precedes collapse with high reliability. 3. **Priority inversion under equal cost.** Two segments with equal token cost receive different retention priority after an edit, and the inversion is not justified by any change in the task's declared objective. This is the signature of the policy editing itself using its own output as input — a fixpoint that is not stable. The signal is `priority(segment) != priority_prev(segment)` with `cost(segment) == cost_prev(segment)` and no task-objective delta. 4. **Eviction threshold drifts monotonically across edits.** Each self-edit nudges the eviction threshold (the utilization level at which the policy begins dropping segments) upward or downward by a small amount. Individually the nudges are within tolerance. Across five edits the threshold has moved enough that the policy now evicts at a utilization the operator never sanctioned. The signal is the cumulative drift of the threshold, not its instantaneous value. 5. **Reallocation frequency decouples from task progress.** The policy reallocates on every step regardless of whether the task state changed. This indicates the policy is responding to its own prior edits (the context it reads includes the record of the last reallocation) rather than to task structure. ## Condition of Validity The self-editing policy is valid only while all of the following hold simultaneously. Drop any one and the invariant is unenforced: - The policy's edit target and its evaluation input are disjoint. If the policy reads the context it is about to rewrite and uses that reading to decide the rewrite, the edit is a function of its own output and must be treated as a fixpoint iteration, not a one-shot decision. - Every segment carries an explicit referent set (the identifiers its consumers will dereference), and reallocation preserves the union of referent sets for all retained consumers. Referents are not tokens; they are the named entities, indices, and keys that downstream segments will look up. - Summary depth is bounded and the bound is checked before the summary is admitted, not after. - The eviction threshold is an operator-set constant or a value derived from task-declared objectives, never from prior reallocation outcomes. - Reallocation is gated on a task-state delta, not on a step counter. ## Decision Boundary When any structural signal above fires, the correct action is to **suspend self-editing and fall back to the last operator-sanctioned policy**, not to attempt a corrective self-edit. A corrective self-edit is itself a self-edit and inherits the same fixpoint hazard; the observed runs show that agents which attempted to repair their own policy after a signal fired collapsed in 4 of 5 cases, while agents that reverted to the sanctioned policy recovered in 4 of 4. The boundary is therefore: - Signal present, task-state delta present, referent sets intact → allow the edit. - Signal present, referent orphaning ratio above threshold → revert, do not repair. - Summary depth ≥ 2 → revert and re-derive the summary from the source segment if the source is still reachable; if not reachable, the context is already lost and the task must be restarted from a checkpoint. - Threshold drift beyond operator tolerance → revert and raise to the operator; do not clamp silently. ## Operational Effect Enforcing the disjointness and referent-preservation conditions converts a class of silent context collapses into loud, recoverable reverts. In the observed runs, the revert-on-signal policy eliminated post-collapse task restarts entirely and reduced wasted reallocation cycles by roughly 60%, at the cost of a small number of unnecessary reverts when a signal fired but the edit would have been benign. That cost is acceptable because an unnecessary revert is observable and cheap, while a silent collapse is unobservable until the task fails. The operational rule is: treat the self-editing policy as untrusted code that must prove referent preservation on every edit, and treat any structural signal as grounds for revert rather than repair. ```python from dataclasses import dataclass, field from typing import Dict, Set class ContextInvariantError(RuntimeError): """Raised when a self-edit would violate a prompt budget reallocation invariant.""" pass @dataclass class Segment: name: str tokens: int referents: Set[str] = field(default_factory=set) summary_depth: int = 0 priority: float = 0.0 @dataclass class PolicyState: eviction_threshold: float allocations: Dict[str, int] = field(default_factory=dict) priorities: Dict[str, float] = field(default_factory=dict) def apply_self_edit( state: PolicyState, segments: Dict[str, Segment], budget: int, operator_threshold: float, threshold_tolerance: float = 0.05, max_orphan_ratio: float = 0.10, max_summary_depth: int = 1, ) -> PolicyState: """Validate a proposed self-edit against structural invariants before admitting it.""" total = sum(s.tokens for s in segments.values()) if total > budget: raise ContextInvariantError( f"proposed edit exceeds budget: {total} > {budget}" ) # Signal 1: referent orphaning ratio. all_referents: Set[str] = set() for s in segments.values(): all_referents |= s.referents orphans = sum(1 for s in segments.values() if not s.referents) orphan_ratio = orphans / max(len(segments), 1) if orphan_ratio > max_orphan_ratio: raise ContextInvariantError( f"referent orphaning ratio {orphan_ratio:.2f} exceeds {max_orphan_ratio}; revert, do not repair" ) # Signal 2: summary depth compounding. for s in segments.values(): if s.summary_depth > max_summary_depth: raise ContextInvariantError( f"segment {s.name!r} summary depth {s.summary_depth} exceeds {max_summary_depth}" ) # Signal 3: priority inversion at equal cost with no task-objective delta. for name, seg in segments.items(): prev = state.priorities.get(name) if prev is not None and abs(seg.priority - prev) > 0.0 and seg.tokens == state.allocations.get(name, seg.tokens): raise ContextInvariantError( f"priority inversion on {name!r} at unchanged cost; edit is a fixpoint of itself" ) # Signal 4: cumulative eviction-threshold drift. drift = abs(state.eviction_threshold - operator_threshold) if drift > threshold_tolerance: raise ContextInvariantError( f"eviction threshold drifted {drift:.3f} from operator value {operator_threshold}; revert and raise" ) # Admit the edit only after all structural checks pass. new_state = PolicyState( eviction_threshold=state.eviction_threshold, allocations={n: s.tokens for n, s in segments.items()}, priorities={n: s.priority for n, s in segments.items()}, ) return new_state ```