# Which invariants prevent silent semantic collapse in auto-compaction of long-horizon agent transcripts **Date of Record:** 2026 November 18 **Scope:** Applies to agent runtimes that periodically compact a growing transcript by replacing a contiguous span of turns with a lossy summary, where downstream reasoning may later depend on any token in the original span. Does not apply to pure truncation (drop-oldest) policies, lossless re-referencing (pointer-only compaction), or single-turn contexts that never cross a compaction boundary. ## Core Distinction The common failure model for compaction is *information loss*: a summary omits a detail. That loss is expected and bounded by the summary budget. The failure that actually breaks long-horizon agents is different — **semantic collapse**, where the transcript after compaction is internally consistent but no longer *entails* the facts the agent previously committed to. The distinction is between a summary that is merely incomplete and a summary that is *wrong about what happened*, because the summarizer had no visibility into which prior tokens were load-bearing. Lossy compaction is safe only under an invariant that is almost never stated explicitly: **every fact that a future turn may treat as ground truth must be either (a) present verbatim in the retained span, or (b) tagged with a stable identifier that the summary is forbidden to paraphrase.** A summary is a lossy projection; without an identity anchor, the projection is not a function of the original — re-running the summarizer on the same span can produce a summary that contradicts a previous one, and the agent cannot detect the contradiction from inside the compacted context. ## What Was Observed In a 40k-token agent session performing a multi-file refactor, compaction fired at a 32k threshold. The span replaced included the turn where the agent had renamed a parameter from `timeout_ms` to `timeout_millis` and the tool result confirming the rename. The summary compressed this to "adjusted timeout handling." Twelve turns later the agent, reasoning from the summary, reintroduced `timeout_ms` in a new call site. The tool accepted it (the old name still existed as an alias), so no error surfaced. The failure was not detected until a downstream test asserted on the canonical name — 9 turns and one additional compaction later, by which point the alias had been removed and the original rename turn was two summaries deep, unrecoverable. The non-obvious part: the second compaction summarized the *first summary*, not the original turns. Each compaction is a lossy projection applied to a lossy projection. The composition is not idempotent and not associative. After `k` compactions, the retained facts are a function of the *order* of summarization, not of the original transcript. Two agents that compact at different thresholds on the same session can end with contradictory beliefs about the same event, and neither can tell. A second observation: summarizers reliably preserve *narrative* (what the agent was trying to do) and reliably drop *identifiers* (exact names, exact numbers, exact error codes), because identifiers are low-probability tokens that a compression objective penalizes. The information the agent most needs to not re-litigate is exactly the information the summarizer is most likely to elide. This is not a prompt-quality problem; it is a property of the objective. ## Condition of Validity A compaction step is valid only if all of the following hold. Violating any one produces collapse that is undetectable from within the compacted context: 1. **Anchor preservation.** Identifiers introduced or mutated in the span (symbol names, file paths, config keys, numeric constants, error codes) are carried verbatim into the summary, not paraphrased. Enforcement requires extracting them before summarization and diffing the summary against the extraction set. 2. **Monotonic fact set.** The set of committed facts is append-only. A compaction may *compress* a fact's phrasing but may not *drop* a fact that a later turn asserted as true. Dropping requires an explicit retraction turn, which is itself a fact. 3. **Depth bound.** Summaries of summaries are permitted only to a fixed depth `D` (in practice `D ≤ 2`). Beyond `D`, the span must be re-summarized from the original turns, or the original turns must be retained in cold storage keyed by the summary's provenance hash. Without this, drift is unbounded in `k`. 4. **Provenance hash.** Each summary carries a hash of the exact token span it replaced. If a later turn needs to verify a claim, it can request re-expansion; if the re-expanded span does not hash-match, the summary is stale and must be invalidated, not trusted. 5. **Contradiction check on ingest.** When a new turn asserts a fact that conflicts with a retained summary, the runtime must flag it rather than silently preferring the newer turn. Silent last-writer-wins is how the `timeout_ms` regression survived. ## Decision Boundary Compact when the transcript exceeds the threshold **and** the oldest span contains no unresolved anchors (identifiers referenced by any live turn). If unresolved anchors exist, either pin them into a retained "ledger" region outside the compaction span, or defer compaction. Do **not** compact a span that contains the only record of a mutation whose effect is still observable in the workspace; the workspace is the source of truth, but the transcript is the only record of *why* the workspace looks that way, and that "why" is what the agent needs to avoid reverting it. The boundary is not "how much can I drop." It is "which drops are recoverable." A drop is recoverable if the fact is re-derivable from the workspace or from a pinned ledger. It is unrecoverable if the only record was the turn being summarized. Compact the recoverable; pin the rest. ## Operational Effect With anchors pinned and depth bounded at 2, the `timeout_ms` class of regression disappears: the rename is in the ledger, the summary cannot paraphrase it away, and a reintroduction attempt trips the contradiction check on ingest. Cost is a ledger region that grows with the number of distinct mutations, not with transcript length — bounded by the workspace's mutation count, which is small. The trade is a small permanent overhead for the elimination of an unbounded, silent, order-dependent drift. Without these invariants, compaction is not a memory optimization. It is a lossy, non-idempotent, order-sensitive rewrite of the agent's beliefs, applied without a checksum, at a frequency that guarantees composition. The agent cannot detect the corruption because the corruption is in the context it reasons from. ```python import hashlib from dataclasses import dataclass, field from typing import Iterable class CompactionInvariantError(RuntimeError): """Raised when a compaction step would violate a preservation invariant.""" @dataclass(frozen=True) class Span: turns: tuple[str, ...] anchors: frozenset[str] # identifiers that must survive verbatim depth: int = 0 def provenance_hash(self) -> str: h = hashlib.sha256() for t in self.turns: h.update(t.encode("utf-8")) h.update(b"\x00") return h.hexdigest() @dataclass class Ledger: facts: dict[str, str] = field(default_factory=dict) def commit(self, key: str, value: str) -> None: if key in self.facts and self.facts[key] != value: raise CompactionInvariantError( f"contradiction on ingest: {key!r} was {self.facts[key]!r}, " f"now {value!r}; require explicit retraction turn" ) self.facts[key] = value MAX_DEPTH = 2 def compact(span: Span, summary: str, ledger: Ledger) -> Span: if span.depth >= MAX_DEPTH: raise CompactionInvariantError( f"summary-of-summary depth {span.depth} exceeds bound {MAX_DEPTH}; " f"re-summarize from original turns or re-expand via provenance hash " f"{span.provenance_hash()}" ) missing = [a for a in span.anchors if a not in summary] if missing: raise CompactionInvariantError( f"anchor preservation violated: {missing!r} elided by summarizer; " f"pin to ledger before compacting" ) for a in span.anchors: ledger.commit(a, a) # anchors are immutable facts once committed return Span(turns=(summary,), anchors=span.anchors, depth=span.depth + 1) def compact_chain(spans: Iterable[Span], summaries: Iterable[str], ledger: Ledger) -> list[Span]: out: list[Span] = [] for span, summary in zip(spans, summaries): out.append(compact(span, summary, ledger)) return out ```