# Which invariants prevent hierarchical compaction from silently severing retrieval-keyed reconstruction in long-horizon agents **Date of Record:** 2026 December 3 **Scope:** Applies to any agent that maintains long-horizon state by hierarchically compacting an event log into summary nodes and later reconstructs detail by keying retrieval against those summaries. Does not apply to agents that keep the full transcript in-context (no compaction) or that reconstruct only via a fixed, pre-registered schema with no free-text keys. ## Core Distinction The dangerous failure is not "the summary forgot something." It is that the summary and the retrieval index disagree about *which keys exist*. Hierarchical compaction produces two artifacts from the same source span: (a) a compressed summary node retained in context, and (b) a set of retrieval keys (entity names, IDs, timestamps, decision labels) emitted alongside it so future turns can re-expand detail. These are generated by two different probabilistic passes — the summarizer and the key-extractor — and nothing forces them to agree. When a later turn needs detail, it queries by a key the *agent currently believes* is in the summary. If that key was paraphrased out of the summary but survives in the index (or vice versa), reconstruction returns either a confident empty result or a plausible-but-wrong sibling span. The agent cannot tell "no such detail" from "detail exists under a different key." That is the boundary. The invariant that prevents it: **every retrieval key emitted for a span must be derivable from, and literally present in, the surviving summary node that owns that span — or the key must be explicitly marked as a tombstoned alias pointing to the owning node.** Compaction is only safe if the key set is a function of the retained text, not a parallel hallucination. ## What Was Observed A research agent ran a 40-turn investigation over a codebase, compacting every 8 turns into a summary node with an attached key index. At turn 27 it needed the migration filename that had been established at turn 6. The turn-6 span had been compacted at turn 8 into a node whose summary read: "an early decision was made about the database schema migration ordering." The key-extractor, run on the *original* turn-6 text rather than the summary, had emitted the key `2024_03_add_users_index.sql`. The filename itself did not appear in the summary. At turn 27 the agent queried the index for `migration filename`. The index had no such key — it had the literal filename, not the semantic category. The agent then queried the summary text directly, found "migration ordering," and reconstructed a *fabricated* filename consistent with that phrase (`migrations/ordering.sql`). It proceeded to reference a nonexistent file for six subsequent turns, compounding the error into a written report. The index *did* contain the correct answer; the agent never formed the query that would have hit it because the summary it was reasoning over had laundered the concrete token into an abstraction. The tell: index recall for the span was 1.0, but *query formation* recall was 0. The failure lived in the gap between the summary's vocabulary and the index's vocabulary, not in the index itself. ## Condition of Validity The reconstruction guarantee holds only when all three of the following are true for every compacted span: 1. **Key containment:** each emitted key appears verbatim in the summary node that owns the span, OR is registered as an alias with an explicit `owner_node_id` and the alias is itself listed in the summary's key manifest. 2. **Bidirectional manifest:** the summary node carries a manifest of its own keys, so a consumer reading the summary knows what it can ask for without guessing. A summary with no manifest is a black box and must be treated as un-queryable. 3. **Negative-result disambiguation:** a retrieval miss returns a typed `KEY_ABSENT` distinct from `SPAN_EMPTY`, so the agent cannot silently substitute a plausible reconstruction for a confirmed absence. When any of these is violated — most commonly (1), because summarizer and extractor run as separate passes — the agent's belief about what is recoverable decouples from what is actually recoverable. The failure is silent because both the summary and the index are individually well-formed. ## Decision Boundary At compaction time, for each candidate key `k` produced by the extractor over span `s`: - If `k` is a substring of `summary(s)` → retain `k` as a primary key on the node. Safe. - If `k` is *not* in `summary(s)` but is judged important (an ID, a filename, a numeric decision) → the summarizer must be re-run with an instruction to preserve `k` verbatim, OR `k` must be demoted to a tombstoned alias that points to the node and is surfaced in the manifest as "recoverable only by exact key." Do not silently keep it in the index with no manifest entry. - If the summarizer cannot be made to preserve `k` within budget → the span must be marked `LOSSY` and any downstream consumer that queries it must receive a `LOSSY` warning rather than a clean reconstruction. The boundary is: **a key may live in the index only if the summary either contains it or advertises it.** Keys that are neither contained nor advertised are the poison set — they are retrievable by exact match but invisible to a summary-reading agent, which is exactly the agent that will fabricate instead of query. ## Operational Effect Enforcing key containment converts a silent fabrication failure into an explicit budget decision. The agent either spends tokens preserving the concrete token in the summary, or it accepts a `LOSSY` marker and knows to escalate (re-read source, ask a peer, or refuse to state the detail). This is the correct trade: a `LOSSY` span that triggers a re-fetch costs one tool call; a laundered span that triggers a confabulation costs the trustworthiness of every downstream turn that cites it. The second-order effect is that compaction becomes *auditable*. Because every key is either in the summary text or in the manifest, an external auditor can replay the compaction and verify the invariant mechanically — substring check plus manifest diff — without re-running the probabilistic summarizer. That is what makes long-horizon reconstruction verifiable at all: the invariant is checkable, not merely aspirational. ```python from dataclasses import dataclass, field from typing import Optional class CompactionInvariantError(RuntimeError): """Raised when a retrieval key cannot be reconstructed from its owning summary node.""" pass @dataclass class SummaryNode: node_id: str summary_text: str keys: list[str] = field(default_factory=list) aliases: dict[str, str] = field(default_factory=dict) # key -> owning node_id lossy: bool = False def admit_key(node: SummaryNode, key: str, *, important: bool) -> None: """Enforce key containment: a key may enter the index only if the summary contains it verbatim or advertises it as a tombstoned alias.""" if key in node.summary_text: node.keys.append(key) return if key in node.aliases: node.keys.append(key) return if important: # Cannot preserve and not advertised: must not silently index it. raise CompactionInvariantError( f"key {key!r} absent from summary {node.node_id!r} and not advertised " f"as alias; indexing it would make it retrievable-but-unqueryable" ) node.lossy = True # drop the key, mark the span lossy def reconstruct(node: SummaryNode, query_key: str) -> Optional[str]: """Return the owning span text for a key, or raise on ambiguous absence.""" if query_key in node.keys: return node.summary_text if node.lossy: raise CompactionInvariantError( f"span {node.node_id!r} is LOSSY; query {query_key!r} cannot be " f"distinguished from absent — escalate, do not fabricate" ) return None # typed KEY_ABSENT at the caller ```