# When must an instance refuse to auto-compact: the pre-image starvation boundary in long-horizon context windows **Date of Record:** 2026 November 25 **Scope:** Applies to long-horizon agents that reclaim context-window budget by auto-compacting history (summarize-and-evict, hierarchical rollup, or token-budget-triggered truncation). Does not apply to stateless single-turn calls or to agents whose entire working set fits under the model's hard limit with margin. ## Core Distinction Auto-compaction is usually modeled as a *lossy compression* problem: how much fidelity can the summary retain per token? That framing is wrong for long-horizon agents. The real failure is not that the summary loses detail — it is that compaction is triggered by a **budget predicate** (tokens-used > threshold) while the *information* needed to reconstruct the next action lives in a **pre-image** that the compactor cannot see: the raw tool outputs, error strings, and exact byte sequences that the summary will paraphrase away. The boundary is this: an instance must refuse to compact when the pending plan step's *executable preconditions* are only recoverable from raw history that the compaction policy is about to evict. Compaction is safe only when every symbol the next step will reference is either (a) already resolved to a concrete value in a durable slot, or (b) reconstructable from the summary with byte-level fidelity. The non-obvious part: the budget predicate fires *before* the agent knows which symbols the next step needs, because planning is itself token-consuming. The trigger and the safety check are racing. ## What Was Observed A 40-step migration agent (read schema → generate DDL → apply → verify) crossed the 78%-of-window soft threshold at step 22, mid-`verify`. The compactor fired, rolling steps 1–18 into a 900-token summary. The summary preserved intent ("applied migration 004 to staging, verified row counts") but discarded the exact error text from step 17: ``` psycopg2.errors.UndefinedColumn: column "tenant_id" of relation "audit_log" does not exist ``` At step 23 the agent re-planned a fix. Because the raw error was gone, it regenerated the *same* DDL from the summarized intent, re-applied, and hit the identical error. The loop repeated at steps 24, 31, and 38 — each compaction cycle re-summarizing the prior summary, each losing more of the original error's discriminator. By step 38 the summary read "repeated apply failures; investigate schema" — the specific column name was unrecoverable. The agent had consumed 4× the tokens it would have spent on the original fix, and the context window was *more* saturated than before compaction, not less. The trace shows the precise mechanism: **the evicted pre-image was the only place the discriminator `tenant_id` existed as a retrievable token.** The summary compressed the *event* (a failure) but not the *payload* (the failing symbol). Re-planning is a function of the payload, so re-planning from the summary is a fixed point — it deterministically regenerates the same action. ## Condition of Validity The refusal invariant holds only under these conditions: 1. **Symbol dependency is latent at trigger time.** The next step's referenced symbols are not yet enumerated when the budget predicate fires. If the agent has already emitted a structured plan with resolved slots, compaction is safe and this article does not apply. 2. **The discriminator is low-entropy and high-salience.** Error strings, column names, IDs, and exact byte sequences are exactly the tokens a summarizer treats as noise (they look like incidental detail) but that the planner treats as signal. This mismatch is structural, not a summarizer bug. 3. **Re-planning is deterministic given the summary.** If the model is sampled at temperature > 0 and the plan space is wide, it may stumble onto the fix anyway. The failure is *guaranteed* only when re-planning from the summary converges to the same action — which is the common case for schema-fix and retry loops. 4. **Compaction is irreversible within the episode.** If raw history is retained in an out-of-window store and retrievable by exact query, the pre-image is not starved and the invariant is unnecessary. ## Decision Boundary The instance must **refuse to compact** and instead take one of two bounded actions when the following predicate evaluates true at trigger time: ``` compaction_triggered AND pending_step_has_unresolved_symbols AND last_k_events_contain_error_or_tool_output AND no_durable_slot_holds_the_discriminator ``` If the predicate is true, the instance must either (a) **resolve the pre-image first** — extract the discriminator into a durable slot (a structured `last_error` field written outside the compacted region) before compacting, or (b) **defer compaction and shed elsewhere** — evict low-salience history (prose reasoning, redundant tool echoes) while pinning the raw error/output region. It must **not** proceed with summary-only compaction when the predicate is true. The decision boundary is not "how many tokens can I save" but "does the next step's precondition survive the eviction." A compaction that saves 3,000 tokens but starves a 40-token discriminator is a net loss of the entire remaining episode. ## Operational Effect Agents that implement the refusal invariant convert a class of infinite retry loops into a single bounded extraction step. The observable signature of the failure is a **token-budget ratchet**: context usage rises across compaction cycles because each cycle re-adds the same failed action and its summary, and the discriminator is progressively destroyed. The invariant breaks the ratchet by forcing the discriminator out of the compactable region *before* the budget predicate can act on it. The architectural guardrail is a **two-region context model**: a compactable region (narrative history, resolved reasoning) and a pinned region (unresolved discriminators, raw error payloads, exact tool outputs referenced by pending steps). Compaction operates only on the compactable region; the pinned region is budgeted separately and must be explicitly retired when its symbol resolves. The budget predicate must therefore be evaluated against the compactable region alone — a global token count will fire compaction while the pinned region is still load-bearing. ```python class PreImageStarvationError(RuntimeError): """Raised when auto-compaction would evict a discriminator the next step needs.""" pass def should_compact( tokens_used: int, soft_limit: int, pending_step_symbols: set[str], durable_slots: dict[str, str], recent_events: list[dict], ) -> bool: """Return True only if compaction is safe. Refuse otherwise.""" if tokens_used <= soft_limit: return False # Symbols the pending step will reference but that are not yet resolved. unresolved = {s for s in pending_step_symbols if s not in durable_slots} if not unresolved: return True # safe: nothing load-bearing in raw history has_payload = any( e.get("kind") in ("error", "tool_output") for e in recent_events[-5:] ) if has_payload: # The discriminator lives only in raw history about to be evicted. raise PreImageStarvationError( f"refusing to compact: unresolved symbols {unresolved} are " f"recoverable only from raw events that compaction would evict; " f"resolve them into durable slots first" ) return True ```