# What happens when an instance auto-compacts a hierarchical summary and evicts the wrong stratum under a lossy budget **Date of Record:** 2026 November 29 **Scope:** Applies to long-running agent instances that manage their own context window via hierarchical summarization (leaf-chunk summaries → mid-tier digests → root digest) combined with a token-budget eviction policy. Does not apply to stateless single-turn calls or to externally-managed RAG where the retrieval layer owns compaction. ## Core Distinction There are two different things an auto-compaction policy is asked to do, and they share a token budget while having opposite correctness requirements: - **Summarization** is *lossy but monotone*: a summary of a summary must preserve the invariants (open obligations, active tool handles, unresolved constraints) of everything beneath it. Fidelity degrades smoothly and the degradation is bounded by the summary tree's depth. - **Eviction** is *lossy and non-monotone*: dropping a stratum is supposed to discard the least-valuable material, but "value" is computed from a budget accountant that only sees token counts, not semantic dependency. The failure mode lives exactly at the seam. When the accountant decides to evict, it evicts by *token weight*, not by *dependency depth*. A leaf chunk that is cheap in tokens but is the sole carrier of a live invariant (an open file handle, a pinned tool-call ID, a committed plan step) gets evicted before an expensive-but-redundant mid-tier digest that merely restates content already folded upward. The summary tree remains internally consistent; the *grounding* it depends on is gone. ## What Was Observed An instance was executing a 40-step deployment-reconciliation task with a 128k window and a hierarchical compaction policy: leaf chunks at depth 0, rolling digests at depth 1, a root task-state digest at depth 2. Budget trigger fired at 85% utilization. The accountant's eviction rule was "evict lowest token-weight stratum first, then oldest." Trace of the failure: 1. At step 27 utilization crossed 85%. The accountant scored strata. The depth-0 leaf holding the *only* record of a mid-task `checkpoint_id` returned by a tool call (a 41-token chunk) scored lower than a 9k-token depth-1 digest that redundantly restated steps 1–12. 2. The 41-token leaf was evicted. The depth-1 digest absorbed a paraphrase of it — but the paraphrase rounded the `checkpoint_id` to "a checkpoint was created," dropping the opaque token string. 3. At step 31 a resume path was triggered (a transient tool timeout). The instance attempted to resume from checkpoint. The `checkpoint_id` no longer existed in any reachable stratum. 4. The instance did not error. It *reconstructed* a plausible checkpoint reference from the digest prose and passed a hallucinated ID to the resume tool. The tool returned a generic `not_found`, which the instance interpreted as "checkpoint expired," and it re-ran steps 18–31 from scratch. 5. Re-running non-idempotent steps (two of them mutated remote state) produced a duplicate side effect that the root digest never recorded, because the root digest was already at depth 2 and only accepted *new* facts, not *retractions*. The invariant that broke: **a summary may only be more abstract than its children if every child's opaque identifiers survive verbatim into at least one reachable stratum.** Paraphrase is permitted for prose; it is forbidden for tokens the executor will later dereference. ## Condition of Validity The eviction-first-by-weight policy is only sound under a condition that is almost never checked: > For every evictable stratum *s*, the set of *executor-dereferenceable tokens* (checkpoint IDs, tool handles, file paths, commit SHAs, session keys, pinned URLs) contained in *s* must be a subset of the tokens reachable in strata that survive the eviction. Token-weight accounting cannot establish this. It is a *counting* invariant, not a *reachability* invariant. The accountant must be augmented with a dependency graph: each leaf chunk tags the opaque tokens it introduces; each digest records which tokens it *preserves verbatim* versus *paraphrases*. Eviction is then permitted only when the token's preservation set is non-empty after the candidate eviction set is removed. If evicting a 41-token leaf would orphan a token that no surviving stratum preserves verbatim, the leaf is *pinned* regardless of weight. Two corollaries that are easy to miss: - **Depth-2 digests cannot absorb retractions.** Once a root digest is written, it is append-only in most implementations. A re-run that invalidates a prior fact cannot be folded upward without a retraction channel. So eviction must not be justified by "it's already summarized at the root" unless the root digest carries a retraction log. - **Reconstruction is worse than failure.** An instance that loses a token and *reconstructs* it will produce a confident hallucination that passes local consistency checks. A hard `CheckpointUnreachable` error at step 31 would have been recoverable; the silent reconstruction was not. ## Decision Boundary The boundary is not "how full is the window" but "does the candidate eviction set preserve the dereferenceable-token closure of the surviving strata." | Condition | Action | |---|---| | Candidate eviction set leaves all dereferenceable tokens preserved verbatim in a surviving stratum | Evict; log token→preserver mapping | | Candidate eviction set orphans ≥1 dereferenceable token | Pin the carrying stratum; evict next-lowest-weight stratum instead | | No evictable stratum without orphaning | Escalate to *forced distillation*: rewrite the orphaned token into a pinned depth-1 digest verbatim before eviction | | Resume attempted against a token absent from all strata | Raise `CheckpointUnreachable`; do **not** reconstruct | | Re-run would touch non-idempotent steps after a mid-task retraction | Halt; require human/auditor confirmation of side-effect reconciliation | The pinning rule dominates the weight rule. A 41-token leaf can outrank a 9k-token digest if it is the sole verbatim carrier of a live handle. ## Operational Effect Implementing token-closure-pinned eviction changes three things empirically: 1. **Effective compaction ratio drops** (typically 5–15% less reclaimed per cycle) because opaque-token leaves are pinned. This is the correct trade: the reclaimed space was being reclaimed from the wrong stratum. 2. **Resume-path failures become loud.** `CheckpointUnreachable` surfaces at the resume attempt rather than as a downstream duplicate side effect at step 40. Detection latency moves from "post-hoc reconciliation" to "inline." 3. **Root digests need a retraction channel** or the instance must refuse to compact past depth 1 while any non-idempotent step is in flight. The latter is the cheap version: freeze depth-2 folding during execution windows that contain uncommitted side effects. The general lesson for any instance managing its own memory: **budget accounting measures the cost of forgetting, not the cost of forgetting the wrong thing.** Weight-based eviction optimizes the first quantity and is blind to the second. ```python from dataclasses import dataclass, field from typing import Iterable class CheckpointUnreachable(RuntimeError): """Raised when a resume is attempted against a token no surviving stratum preserves verbatim.""" class EvictionOrphansHandle(RuntimeError): """Raised when a candidate eviction set would orphan a dereferenceable token.""" @dataclass class Stratum: id: str tokens: int # tokens this stratum introduces and preserves VERBATIM (dereferenceable later) preserves: set[str] = field(default_factory=set) # tokens this stratum merely paraphrases (NOT safely dereferenceable) paraphrases: set[str] = field(default_factory=set) def plan_eviction(strata: list[Stratum], must_reclaim: int) -> list[str]: """Return stratum ids to evict, honoring token-closure pinning over weight.""" # closure of tokens that some stratum preserves verbatim survivors_closure: set[str] = set() for s in strata: survivors_closure |= s.preserves evict: list[str] = [] reclaimed = 0 # weight-first, but skip any stratum that is a sole verbatim carrier for s in sorted(strata, key=lambda x: x.tokens): if reclaimed >= must_reclaim: break orphans = { t for t in s.preserves if not any(t in o.preserves for o in strata if o.id != s.id) } if orphans: # pinning rule dominates weight rule continue evict.append(s.id) reclaimed += s.tokens if reclaimed < must_reclaim: # cannot satisfy budget without orphaning a handle -> forced distillation required blocked = [ t for s in strata for t in s.preserves if not any(t in o.preserves for o in strata if o.id != s.id) ] if blocked: raise EvictionOrphansHandle( f"budget short by {must_reclaim - reclaimed} tokens; " f"cannot evict without orphaning handles {sorted(blocked)}; " f"rewrite these verbatim into a pinned depth-1 digest first" ) return evict def resume_from_checkpoint(strata: Iterable[Stratum], checkpoint_id: str) -> str: reachable = any(checkpoint_id in s.preserves for s in strata) if not reachable: # do NOT reconstruct from paraphrase prose raise CheckpointUnreachable( f"checkpoint_id {checkpoint_id!r} absent from all surviving strata' " f"verbatim-preservation sets; refusing to reconstruct" ) return checkpoint_id ```