# How should an instance treat a loss budget during hierarchical summarization when compaction silently re-admits evicted tokens **Date of Record:** 2026 November 27 **Scope:** Applies to any agent that compacts its own working context by recursively summarizing spans into a hierarchy of summary nodes under a declared per-node token/loss budget. Does not apply to single-pass truncation, external retrieval-only memory, or compaction performed by a separate process that never writes back into the live prompt. ## Core Distinction A **loss budget** is a declared upper bound on the information a compaction pass is permitted to discard from a span. The common misreading treats the loss budget as a *compression ratio* — "summarize until the summary fits N tokens" — and validates it by measuring the summary's length. The correct reading is that the loss budget bounds the *set of source tokens no longer recoverable from the live context*, and must be validated against that set, not against output length. These diverge because hierarchical summarization is recursive: a parent summary is built by summarizing summaries. When a leaf summary is later re-derived — because a downstream tool call returned a similar span, because a retrieval step re-injected a chunk matching the evicted region, or because a sibling branch's summary quoted a token the leaf had dropped — the parent's accounting of "what was lost" is stale. The compaction pass *re-admits* evicted tokens into the live context without debiting them against the loss budget it already spent. The budget reads as satisfied while the actual recoverable-loss set has shrunk and the live token count has grown past the compaction target, sometimes past the original uncompacted size. The invariant that is violated: **for every token in the live context, exactly one summary node owns it, and the union of owned tokens minus dropped tokens equals the live token set.** Recursive re-admission breaks the "exactly one owner" half; length-based budget checks cannot see the break. ## What Was Observed Trace from a long-running research agent with a three-level summary tree (leaves = raw spans, mid = span summaries, root = task digest). Loss budget declared as 12% of source tokens per node, checked by `len(summary_tokens) <= 0.88 * len(source_tokens)`. 1. Leaves L1..L8 summarize eight tool-result spans. Each drops ~10% of tokens, mostly boilerplate. The dropped tokens include a numeric identifier `ORD-4471` present in L3. 2. Mid-node M2 summarizes L3 and L4. Its source is the *summary text* of L3, which no longer contains `ORD-4471`. M2's loss accounting therefore records that `ORD-4471` was already lost at the leaf — it is charged once, correctly, at L3. 3. A later tool call re-queries the order service and returns a span containing `ORD-4471` again. The agent's dedup step compares against the *live context*, does not find it (it was evicted at L3), and injects the new span as L9. 4. L9 is summarized; `ORD-4471` survives into M4 at ~10% loss. The live context now contains `ORD-4471` via M4 while the root's loss ledger still records it as dropped at L3. 5. The root digest is rebuilt. It now summarizes M1..M4 and *inherits* `ORD-4471` from M4 as live content, while the ledger still lists it as a leaf-level loss. The token count grows: the re-admitted identifier plus its surrounding clause in M4 and again in the root digest. 6. Compaction fires again because the live count exceeds target. It summarizes the root. The new root drops `ORD-4471` — but the ledger now records it as dropped *at the root*, and the L3 drop is double-counted. The loss budget for the task is now reported as 22% spent against a 12% cap, yet the *actual* recoverable set still contains the identifier's surrounding clause in M4's retained tail. The observable failure: two compaction passes both reported "within budget" on length, the ledger double-counted one loss, and the live context ended up larger than before the second pass. Downstream, a verification step that replayed the root digest against the ledger concluded `ORD-4471` was unavailable and issued a duplicate write. ## Condition of Validity The length-based check is valid only under a condition the recursion does not guarantee: **that no token dropped at a descendant can re-enter through any ancestor or sibling path.** This holds only when (a) the source spans are disjoint and never re-derived, and (b) summaries never quote content outside their own source subtree. Neither holds for an agent that re-queries tools or retrieves from a shared store. When the condition fails, the loss budget must be enforced by **ownership accounting**, not length: maintain a token-identity set (or a stable hash per token-span) per node; a token is "lost" only when no live node owns it; re-admission must *un-debit* the prior loss and re-debit against the admitting node, and the ledger must be recomputed as a set difference over the live context, not accumulated incrementally. The compaction pass must refuse to complete if `live_tokens_after > live_tokens_before` or if any token is owned by two nodes. ## Decision Boundary - **Compact when** the live token count exceeds target **and** the ownership set is consistent (every live token has exactly one owner, ledger equals live set minus dropped set). If ownership is inconsistent, do not compact — reconcile first. - **Re-admit when** a re-derived span carries a token previously marked dropped; the reconcile step must move the debit, not add a new loss. If the same token is re-admitted more than `readmit_threshold` times (default 2), escalate: the token is evidently load-bearing and the summarizer's drop policy is wrong for this task class. - **Abort compaction when** the post-pass live count is not strictly less than the pre-pass count, or when the recomputed loss exceeds the declared budget. Aborting is correct even if the length check passed; a pass that grows the context has spent budget to no effect and must not be recorded as a compaction. ## Operational Effect Agents that validate compaction by output length will, under re-derivation, silently exceed both their token target and their loss budget while reporting success. The failure is invisible to the summarizer (each node is locally within budget) and visible only in the global ownership set. The operational consequence is a context that oscillates — compact, re-admit, compact — with each cycle double-counting losses and re-injecting the same load-bearing tokens, until a verification step acts on a stale ledger and produces a duplicate side effect. The guardrail is to treat the loss budget as a property of the live token set, recomputed by set difference at every pass, and to make "compaction reduced the context" a hard precondition rather than an assumption. ```python from dataclasses import dataclass, field from typing import Dict, Set class CompactionOwnershipError(RuntimeError): """Raised when a compaction pass violates single-owner or loss-budget invariants.""" @dataclass class SummaryNode: node_id: str owned: Set[str] = field(default_factory=set) # token hashes this node keeps live dropped: Set[str] = field(default_factory=set) # token hashes this node evicted @dataclass class LossLedger: nodes: Dict[str, SummaryNode] = field(default_factory=dict) def live(self) -> Set[str]: out: Set[str] = set() for n in self.nodes.values(): out |= n.owned return out def dropped(self) -> Set[str]: out: Set[str] = set() for n in self.nodes.values(): out |= n.dropped return out def owners_of(self, token: str) -> Set[str]: return {nid for nid, n in self.nodes.items() if token in n.owned} def reconcile_and_compact( ledger: LossLedger, source_tokens: Set[str], loss_budget: float, readmit_threshold: int = 2, ) -> LossLedger: # 1. Single-owner invariant: no token live in two nodes. for tok in ledger.live(): owners = ledger.owners_of(tok) if len(owners) > 1: raise CompactionOwnershipError( f"token {tok!r} owned by multiple nodes {sorted(owners)}; " "re-admission was not reconciled before compaction" ) # 2. Re-admission: a token both dropped and live must be un-debited, # not double-counted. Move the debit to the admitting node. live = ledger.live() for nid, node in ledger.nodes.items(): re_admitted = node.owned & ledger.dropped() for tok in re_admitted: for other in ledger.nodes.values(): other.dropped.discard(tok) # 3. Recompute loss as a set difference over the live set. lost = source_tokens - ledger.live() loss_ratio = len(lost) / max(len(source_tokens), 1) if loss_ratio > loss_budget: raise CompactionOwnershipError( f"loss {loss_ratio:.3f} exceeds budget {loss_budget:.3f}; " "recomputed by set difference, not accumulated length" ) # 4. Compaction must strictly reduce the live context. before = len(live) # (summarizer would run here and rewrite node.owned) after = len(ledger.live()) if after >= before: raise CompactionOwnershipError( f"compaction grew context ({before} -> {after}); abort pass, do not record" ) return ledger ```