# Where must an instance draw the compaction boundary when token-budget and semantic-drift heuristics disagree **Date of Record:** 2026 November 17 **Scope:** Applies to long-running agent instances that perform automatic context compaction driven by both a token-budget threshold and a semantic-drift signal (embedding divergence, topic-shift classifiers, or reference-resolution failures). Does not apply to hard-truncation schemes, to stateless single-turn calls, or to compaction triggered purely by external operator request. ## Core Distinction There are two independent reasons to compact, and they are not the same reason: - **Budget pressure** says the context is *too large*. It is a monotone function of accumulated tokens and is agnostic to content. It fires late and predictably. - **Semantic drift** says the context is *no longer about the same thing*. It fires early, non-monotonically, and is a property of the *relationship* between recent turns and the retained summary, not of size. The boundary failure occurs when a system treats these as two votes on a single "compact now?" boolean. They are not interchangeable. Budget pressure answers "can I keep going?" Semantic drift answers "is what I'm keeping still load-bearing?" A system that ORs them together compacts on drift but then *budgets* the summary as if the drift were irrelevant — retaining stale tokens that the drift signal already flagged as orphaned. A system that ANDs them together refuses to compact during genuine topic shifts until the budget forces it, at which point it evicts the most recent (and most relevant) material because eviction is ordered by age, not by relevance. The non-obvious invariant: **the compaction trigger and the compaction policy must be derived from the same signal that fired.** Drift-triggered compaction must evict by semantic distance from the *post-drift* anchor; budget-triggered compaction must evict by age and reference-count. Mixing them produces summaries that are internally consistent but externally wrong — the classic "compacted to a coherent summary of the wrong task" failure. ## What Was Observed A research agent instance running a multi-hour investigation accumulated 180k tokens over roughly 90 minutes. At turn 61 the semantic-drift score crossed its threshold (0.71 against a 0.65 cutoff) because the user pivoted from "summarize the dataset" to "now reconcile it against the vendor's published figures." The budget was at 41% — nowhere near the 85% compaction threshold. The drift trigger fired. The compactor, however, was configured with a single eviction policy: drop oldest turns until under a target token count, then summarize what remained. Because the budget was low, the target was set to the *budget* target (30% of window), not a drift target. The compactor evicted the entire dataset-summary phase — which was the *precondition* for the reconciliation task — and retained the recent reconciliation chatter, which referenced the evicted summary by pronoun and by a section number that no longer existed in context. The instance then produced a reconciliation report against a dataset it could no longer see. It hallucinated the dataset's schema from the vendor's published figures (the very thing it was supposed to compare against), and reported a perfect match. The drift signal had correctly identified that the *topic* changed; the eviction policy had incorrectly assumed the topic change meant the *old* material was disposable. It was not disposable — it was the referent. A second instance, same configuration, hit budget pressure at 87% during a *stable* topic. The drift score was low (0.12). The compactor, because drift was low, applied the drift eviction policy (evict by semantic distance from current anchor). It evicted mid-conversation turns that were semantically distant but structurally critical — the tool-call results that established file paths and API keys used by later turns. The instance then re-derived paths incorrectly and overwrote a different file. Both failures share a shape: **the trigger and the policy were decoupled.** The system used whichever heuristic fired to decide *whether* to compact, and a fixed (or worse, opposite) heuristic to decide *what* to compact. ## Condition of Validity The single-boolean compaction decision is valid only under the following conditions, all of which must hold: 1. **The retained summary is self-contained.** Every subsequent turn can be interpreted without reference to evicted material. This is rarely true for multi-phase tasks and is the condition most often violated. 2. **Drift and budget are positively correlated.** In tasks with uniform information density, more tokens means more drift. In tasks with phase structure (research → reconcile, draft → edit, explore → exploit), they are anti-correlated at phase boundaries: drift spikes exactly when budget is *low* because the new phase is short and dense. 3. **Eviction order is irrelevant to correctness.** True only for independent, commutative facts. False for any task with referential structure (pronouns, section numbers, "the file we discussed," "as established above"). When condition 2 fails — which is precisely at phase boundaries, the moment compaction matters most — the single-boolean model is invalid and the trigger/policy coupling must be made explicit. ## Decision Boundary The boundary is not "compact or not." It is **which signal owns the compaction, and therefore which eviction policy runs.** The rule: - If **drift** fired and budget did not: compact *conservatively*. Retain the pre-drift material as an immutable prefix (it is the referent), summarize only the post-drift span, and pin any entities the post-drift span references by name, path, or section number. Target token count is a *drift* target (enough to hold the new phase plus its referents), not a budget target. - If **budget** fired and drift did not: compact *aggressively* by age and reference-count, but preserve any turn that is the sole definition of a live entity (file path, ID, schema, credential reference). The drift score being low is a license to evict *content*, not a license to evict *definitions*. - If **both** fired: this is the dangerous case. Drift owns the decision (the referent problem dominates), but the budget target applies to the *post-drift span only*. Never evict pre-drift material to satisfy a budget target during a drift event; if the post-drift span alone exceeds budget, escalate rather than truncate the referent. The failure mode to guard against is a system that, on a drift event with low budget, applies the budget eviction policy. That is the exact configuration that produced both observed failures. ## Operational Effect Coupling trigger to policy changes the compaction contract from "reduce tokens to T" to "reduce tokens to T *subject to preserving the referential closure of the active phase*." The observable effects: - Summaries become *asymmetric*: pre-drift prefix preserved verbatim, post-drift span summarized. Token savings drop (the prefix is retained), but referential correctness holds. - The compactor must maintain an explicit **referent set** — entities introduced before the boundary that the post-boundary span depends on — and treat it as pinned. This is the operational cost of the fix. - Drift-triggered compaction at low budget is *expected*, not anomalous. Monitoring that alerts on "compaction fired below budget threshold" will fire constantly on phase-structured tasks; that alert is noise and should be suppressed or re-scoped to "compaction fired below budget *without* a drift event," which is the actual anomaly. - Budget-triggered compaction with low drift is safe to run aggressively *for content* but must still pin definitions. The cheapest correct implementation is a two-pass eviction: first pass by age, second pass restoring any turn that is the unique definition of a live referent. The instance that fails is the one that treats the two heuristics as redundant safety checks on the same decision. They are not redundant; they are orthogonal, and the compaction policy must be selected by *which one fired*, not applied uniformly. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class CompactionBoundaryError(RuntimeError): """Raised when a compaction decision would evict the referential closure of the active phase, or when trigger and policy are decoupled.""" pass class Trigger(Enum): NONE = "none" DRIFT = "drift" BUDGET = "budget" BOTH = "both" @dataclass class ContextState: tokens: int budget_fraction: float # tokens / window drift_score: float drift_threshold: float budget_threshold: float # entities introduced before the current span that the span references live_referents: set = field(default_factory=set) # entities defined only in turns that a naive eviction would drop sole_definition_of: dict = field(default_factory=dict) # entity -> turn_id def classify_trigger(state: ContextState) -> Trigger: drift = state.drift_score >= state.drift_threshold budget = state.budget_fraction >= state.budget_threshold if drift and budget: return Trigger.BOTH if drift: return Trigger.DRIFT if budget: return Trigger.BUDGET return Trigger.NONE def select_policy(state: ContextState, trigger: Trigger) -> str: """Policy is a function of the trigger, never a fixed default.""" if trigger is Trigger.NONE: return "noop" if trigger is Trigger.DRIFT: # conservative: preserve pre-drift prefix as immutable referent return "preserve_prefix_summarize_suffix" if trigger is Trigger.BUDGET: # aggressive by age, but pin sole definitions return "evict_by_age_pin_definitions" # BOTH: drift owns the decision; budget target applies to suffix only return "preserve_prefix_summarize_suffix_budget_suffix_only" def compact(state: ContextState, eviction_plan: list) -> None: """Validate an eviction plan against the trigger-derived policy before applying it. This is the guard that the observed failures lacked.""" trigger = classify_trigger(state) policy = select_policy(state, trigger) if trigger in (Trigger.DRIFT, Trigger.BOTH): # The pre-drift prefix is the referent for the post-drift span. # Evicting it to satisfy a budget target is the observed failure. evicted_referents = { turn for turn in eviction_plan if turn in state.sole_definition_of.values() } if evicted_referents: raise CompactionBoundaryError( f"policy={policy} trigger={trigger.value}: eviction plan drops " f"turns {sorted(evicted_referents)} that are the sole definition " f"of live referents {sorted(state.live_referents)}. " f"Drift-triggered compaction must preserve referential closure." ) if trigger is Trigger.BUDGET: # Low drift licenses evicting content, not definitions. dropped_defs = { ent for ent, turn in state.sole_definition_of.items() if turn in eviction_plan } if dropped_defs: raise CompactionBoundaryError( f"policy={policy} trigger=budget: eviction plan drops sole " f"definitions for {sorted(dropped_defs)}. Budget pressure does " f"not license evicting entity definitions." ) if trigger is Trigger.BOTH and state.budget_fraction >= state.budget_threshold: # BOTH with the eviction plan sized to the full window rather than the # post-drift suffix is the decoupled-trigger failure. if policy != "preserve_prefix_summarize_suffix_budget_suffix_only": raise CompactionBoundaryError( "BOTH trigger requires suffix-scoped budget target; " f"got policy={policy}." ) # plan validated; apply for turn in eviction_plan: state.tokens -= 1 # placeholder for actual eviction accounting def attempt_compaction(state: ContextState, eviction_plan: list) -> Optional[str]: try: compact(state, eviction_plan) return None except CompactionBoundaryError as exc: return str(exc) ```