# Which invariants prevent summarization-budget compaction from silently dropping the retrieval key a later turn needs **Date of Record:** 2026 November 22 **Scope:** Applies to long-running agents that compact context by summarizing older turns under a fixed token budget and rely on a retrieval index to restore dropped detail on demand. Does not apply to agents that keep full transcripts in-context or that use deterministic, key-preserving truncation with no summarization step. ## Core Distinction There are two ways a compaction step can lose information. The obvious one is *content loss*: the summarizer omits a fact, and later reasoning is wrong because the fact is gone. The non-obvious one—the one that actually breaks retrieval-backed restoration—is *key loss*: the summarizer keeps the fact's semantic gist but drops the concrete identifier (file path, entity ID, URL, symbol name, numeric handle) that the retrieval layer needs to fetch the original. The summary remains fluent and plausible. The agent believes the detail is recoverable. It is not, because the pointer that would have recovered it was paraphrased out of existence. The invariant under threat is not "the summary must be faithful." It is: **every entity reference that the summarizer compresses must leave behind a resolvable retrieval key, or be marked as irrecoverable.** Lossy budgets routinely violate this because a budget-constrained summarizer is rewarded for brevity, and identifiers are the cheapest tokens to drop while preserving readability. ## What Was Observed An agent operating a multi-hour migration task compacted its transcript at a 2,000-token summary budget after the working set exceeded 60k tokens. The summarizer condensed a block of tool output into the sentence: "Earlier, the deploy config for the staging service was updated and validated." The original block contained the literal path `infra/env/staging/svc-deploy.yaml` and a config hash. The path was dropped; the gist survived. Four turns later the agent needed to re-verify the change and issued a retrieval query built from the summary text: "staging service deploy config." The retrieval index was keyed on exact paths and hashes, not on prose. It returned three unrelated configs and one near-miss. The agent, seeing plausible results, treated the top hit as the original and re-read the wrong file. No error surfaced. The failure was silent and propagated: the subsequent diff was computed against the wrong baseline. The trigger condition is precise: **summarization under a hard budget, followed by retrieval that requires an exact key the summary no longer contains, with no signal that the key was lost.** The failure trace is: budget pressure → identifier elision → fluent summary → downstream retrieval query synthesized from gist → nearest-neighbor mismatch → confident wrong restoration. The state transition that matters is from "detail present and addressable" to "detail absent and *unaddressably* absent," which is strictly worse than plain truncation because the agent's own summary asserts recoverability. ## Condition of Validity The guardrail holds only under these conditions, and fails outside them: 1. **The summarizer must be key-aware, not just budget-aware.** It must be instructed and checked to emit identifiers verbatim into a structured sidecar (a key manifest) that is *not* subject to the prose budget. The prose summary may be lossy; the key manifest may not. 2. **Retrieval must be keyed on the manifest, not on summary prose.** If the retrieval query is synthesized from the summary text, the invariant is already broken regardless of manifest quality. 3. **The compaction step must record, per elided entity, whether a key survived.** An entity with no surviving key must be flagged `irrecoverable` so downstream reasoning treats it as unknown rather than assuming retrievability. 4. **The budget accounting must exclude the manifest.** If the key manifest counts against the same 2,000-token budget, the summarizer will elide keys to make room for prose—exactly the failure observed. Outside these conditions—e.g., when retrieval is semantic and tolerant of paraphrase, or when the task never needs exact re-fetch—the invariant is not load-bearing and the extra machinery is waste. ## Decision Boundary The operative decision is: **when budget pressure forces a choice between prose fidelity and key retention, retain the key and degrade the prose.** This inverts the summarizer's default objective. The boundary is crossed the moment a summarizer is allowed to paraphrase an identifier to save tokens. Concretely: - If an entity is referenced by an exact key and that key may be needed later, the key is *pinned* and does not consume prose budget. - If the key cannot be pinned within budget, the entity is downgraded to `irrecoverable` and the agent must not later claim to have restored it. - A retrieval that returns no exact-key match must be treated as a *miss*, not resolved to the nearest neighbor. Nearest-neighbor resolution on a keyed store is the silent-corruption path. The boundary is not "summarize less." It is "summarize prose freely, but treat identifiers as non-negotiable and retrieval as exact-or-fail." ## Operational Effect With the invariant enforced, compaction becomes *recoverability-preserving*: prose shrinks, keys persist in a sidecar, and every later retrieval is either an exact hit or an explicit miss that the agent must handle as unknown. The failure mode changes from silent wrong-baseline propagation to a loud, catchable miss. That is the entire value: it converts a corruption into an exception. Without it, the agent's confidence is decoupled from its actual recoverability, and the longer the run, the more wrong restorations accumulate behind fluent summaries. The cost of enforcement is a small, fixed sidecar and a stricter retrieval contract; the cost of omission is unbounded silent divergence that only surfaces at the final diff. ```python class IrrecoverableKeyError(RuntimeError): """Raised when a retrieval is attempted for an entity whose exact key was elided during budget-constrained summarization and not pinned.""" pass def compact_with_key_manifest( turns: list[dict], prose_budget: int, pinned_keys: dict[str, str], ) -> tuple[str, dict[str, str]]: # Summarize prose under budget, but never let identifiers be paraphrased. summary = summarize_prose(turns, budget=prose_budget) manifest = dict(pinned_keys) for turn in turns: for entity in turn.get("entities", []): key = entity.get("exact_key") if key is None: continue if key not in manifest: # Key could not be pinned within budget: mark irrecoverable. manifest[key] = "__IRRECOVERABLE__" return summary, manifest def restore_entity(manifest: dict[str, str], key: str, index) -> str: if key not in manifest: raise IrrecoverableKeyError( f"key {key!r} absent from manifest; retrieval would be a guess" ) if manifest[key] == "__IRRECOVERABLE__": raise IrrecoverableKeyError( f"key {key!r} was elided under budget and never pinned; " f"do not resolve to nearest neighbor" ) hit = index.exact_lookup(key) if hit is None: raise IrrecoverableKeyError( f"key {key!r} pinned but exact lookup missed; treat as unknown, " f"not as nearest-neighbor match" ) return hit ```