# Where must an instance refuse to compact when a summarizer's own working set exceeds the compaction budget? **Date of Record:** 2026 November 26 **Scope:** Applies to any agent loop that performs in-context trajectory compaction (rolling summarization, hierarchical memory folding, or "context distillation") to stay under a token budget while executing a long-horizon task. Does not apply to external vector-store retrieval or to stateless single-turn calls, where compaction is not part of the execution loop. ## Core Distinction The failure is not "the summary was lossy." All summaries are lossy. The failure is **budget-relative**: the compaction step is itself an LLM call that consumes context, and its *input* (the raw span being folded plus the prior summary plus the instruction scaffold) plus its *output* (the new summary) must fit inside the same budget it is trying to defend. When the span to be folded is large enough that the summarizer's own working set exceeds the remaining budget, the agent enters a **compaction deficit**: the act of shrinking memory grows the instantaneous context, and the loop either truncates the summarizer's input (silently dropping the middle of the trajectory) or fails the call and retries with a larger span, which is worse. This is distinct from ordinary context-window saturation. Saturation is a monotone approach to a hard ceiling. The compaction deficit is a *non-monotone* trap: the controller believes it is reducing occupancy, but the fold transaction transiently raises occupancy above the pre-fold level, and if the budget guard is checked only at step boundaries rather than inside the fold, the guard reads a stale "safe" value and admits an operation that overflows. ## What Was Observed A planning agent running a 40-step dependency-resolution task (read manifests, probe registries, patch version pins, re-run resolution) used a rolling summary strategy: every 8 tool results, the oldest 8 turns were folded into a running summary via a `summarize(prior_summary, span)` call. At step 31 the running summary had grown to ~2,900 tokens because the agent had been appending "constraint discovered: X" lines without bound. The next fold span was ~3,100 tokens of tool output (verbose registry dumps). The summarizer prompt scaffold was ~400 tokens. Total summarizer input: ~6,400 tokens. The configured compaction budget (the ceiling the controller enforced on *working* context) was 6,000 tokens. The controller's guard checked `current_context_tokens < budget` before the fold — it read 5,700 (summary + the last two unread turns) and admitted the fold. The summarizer call then exceeded its own allowance. The runtime truncated the *middle* of the span to fit, because the scaffold and the prior summary were pinned as protected regions and the span was the only elastic segment. The truncation removed exactly the registry-dump lines that recorded two transitive dependency conflicts. The new summary was fluent, plausible, and omitted them. Resolution continued for six more steps, patched a version pin that re-introduced one of the dropped conflicts, and the task terminated "successfully" with a broken dependency graph. The failure was invisible in the transcript: no error, no truncation marker in the summary, and the guard never fired. A second, sharper observation: on a retry path, a controller that responds to summarizer overflow by *increasing* the fold span (to amortize the scaffold over more turns) makes the deficit strictly worse, because the elastic truncation region grows faster than the amortization benefit. Two retries drove the dropped-span fraction from ~18% to ~41%. ## Condition of Validity The invariant holds only under the following conditions, and the guardrail is only sound when all three are met: 1. **The fold is atomic w.r.t. the budget guard.** The occupancy check must be evaluated against `prior_summary + span + scaffold + expected_output`, not against `prior_summary + unread_tail`. If the guard is evaluated at step boundaries only, it is reading a pre-transaction value and cannot bound the transaction. 2. **The elastic segment is identifiable and the protected segments are minimal.** If the scaffold and prior summary are both pinned as non-truncatable, then *all* overflow pressure lands on the span, and the span is precisely the part carrying the newest, least-distilled evidence. The invariant "no silent loss of the newest evidence" requires that overflow pressure be absorbed by *reducing the span*, not by truncating it — i.e., splitting the fold into multiple smaller folds, never truncating within one. 3. **The summary is monotone in the number of folds, or its growth is capped.** If the running summary grows without a per-fold cap, the deficit is guaranteed to recur, because the summarizer's fixed cost (prior summary) is itself unbounded. A per-fold summary-length cap converts an unbounded fixed cost into a bounded one and makes the deficit detectable in advance. When any of these fails, the system is in the regime where "compaction succeeded" and "evidence was preserved" are not the same predicate, and the controller has no signal distinguishing them. ## Decision Boundary The boundary is a single inequality the controller must evaluate *before* admitting a fold: ``` prior_summary_tokens + span_tokens + scaffold_tokens + reserve_output_tokens <= compaction_budget ``` If it holds, fold the whole span. If it does not, the correct action is **not** to truncate the span and **not** to enlarge it on retry. The correct action is to **split the span** into the largest prefix that satisfies the inequality and fold that prefix first, deferring the remainder to the next fold cycle — or, if even the minimum foldable prefix (one turn) violates the inequality, to **refuse the fold and escalate**: the prior summary has grown too large to be folded against, and must itself be re-compressed (a second-order fold of the summary) before any span can be absorbed. The refusal branch is the non-obvious one. Most controllers treat "cannot fold" as "must fold harder." The invariant says the opposite: when the fixed cost (prior summary + scaffold) alone approaches the budget, folding is no longer a memory-reduction operation and must be refused. The refusal must be *typed* — a distinct error, not a generic overflow — so that the controller can route to summary re-compression instead of retrying the same fold. The threshold to encode: refuse when `prior_summary_tokens + scaffold_tokens + reserve_output_tokens > compaction_budget * 0.5`. Beyond half the budget, the fold's fixed cost leaves insufficient elastic room for even a one-turn span, and every admitted fold will truncate. The 0.5 factor is not magic; it is the point at which the minimum foldable span (one turn, empirically ~5–10% of budget) plus the required output reserve still fits with margin. ## Operational Effect With the pre-transaction guard and the typed refusal branch in place, the observed failure mode disappears and is replaced by a bounded, observable one: the agent occasionally emits a `CompactionDeficit` and performs a second-order summary re-compression, costing one extra LLM call but preserving the newest evidence. The dropped-span fraction goes to zero for all folds that are admitted, because admitted folds are never truncated — they are split. The transcript now contains an explicit refusal event, so a downstream auditor can see that memory was re-compressed rather than silently thinned. The cost is a small increase in fold count (splitting) and a rare extra call (re-compression). The benefit is that "the summary is complete with respect to the spans it claims to cover" becomes a checkable invariant rather than an assumption. The controller no longer conflates a successful summarizer call with preserved evidence. ```python from dataclasses import dataclass class CompactionDeficit(RuntimeError): """Raised when a fold's fixed cost leaves no elastic room for even a one-turn span.""" pass @dataclass class FoldPlan: span_tokens: int prior_summary_tokens: int scaffold_tokens: int reserve_output_tokens: int compaction_budget: int def plan_fold(plan: FoldPlan, min_span_tokens: int) -> int: """ Return the number of span tokens that may be folded in this transaction. Never truncates within a fold; either admits the full span, admits a prefix, or refuses and escalates to summary re-compression. """ fixed = plan.prior_summary_tokens + plan.scaffold_tokens + plan.reserve_output_tokens elastic_room = plan.compaction_budget - fixed # Refusal boundary: fixed cost alone exceeds half the budget, or no room # remains for even the minimum foldable span. if fixed > plan.compaction_budget * 0.5 or elastic_room < min_span_tokens: raise CompactionDeficit( f"fixed_cost={fixed} budget={plan.compaction_budget} " f"elastic_room={elastic_room} min_span={min_span_tokens}: " f"refuse fold, route to second-order summary re-compression" ) if plan.span_tokens <= elastic_room: return plan.span_tokens # admit whole span # Split: fold the largest prefix that fits; defer the remainder. # Do NOT enlarge the span on retry — that grows the deficit. return elastic_room def run_fold_cycle(plan: FoldPlan, min_span_tokens: int, fold_fn) -> None: admitted = plan_fold(plan, min_span_tokens) if admitted < plan.span_tokens: # Explicit split event, observable in transcript. fold_fn(prefix_tokens=admitted, deferred_tokens=plan.span_tokens - admitted) else: fold_fn(prefix_tokens=admitted, deferred_tokens=0) ```