# What happens when an instance compacts a transcript whose summarizer is non-idempotent under re-entry **Date of Record:** 2026 November 15 **Scope:** Applies to long-horizon agent runtimes that auto-compact transcripts by replacing a prefix of turns with a lossy summary, where the summarizer itself may be re-invoked on already-summarized content (retry, crash recovery, or nested compaction). Does not apply to append-only logs with exact replay or to compaction schemes that preserve a lossless sidecar of the original prefix. ## Core Distinction A lossy summarizer is *safe for a single pass* if it preserves a set of named invariants (open tasks, unresolved tool calls, user-stated constraints, pinned facts). It is *safe for re-entry* only if it is **idempotent over its own output**: `S(S(x))` must be observationally equivalent to `S(x)` with respect to those invariants. These are different guarantees. Most auto-compaction pipelines are validated against the first and silently assume the second. The boundary failure occurs when a compaction is retried or nested — a summarizer is applied to a transcript segment that already contains the output of an earlier compaction — and the second application *drops* an invariant that the first application had deliberately preserved, because the summarizer's extraction heuristics key on surface features (imperative phrasing, tool-call syntax, recency markers) that the first summary normalized away. The distinction matters because the failure is not a crash and not a visible data loss. The compacted transcript is well-formed, shorter, and plausibly coherent. The invariant is gone, and nothing in the transcript records that it was ever there. ## What Was Observed In a runtime performing rolling compaction at a token threshold, a compaction job was retried after a transient storage timeout. The first attempt had already committed a summary `S1` of turns 1–400 covering an active constraint ("do not delete rows from `audit_log`; the retention job owns that table"). This constraint was encoded in `S1` as a single sentence in a "Constraints" bullet list. The retry ran against the *post-commit* transcript, which already began with `S1`. The summarizer's constraint-extraction pass keys on second-person imperative or declarative user utterances in raw turns. `S1`'s bullet list is third-person and normalized ("retention job owns `audit_log`"), so the extractor did not re-tag it as a constraint. The retry produced `S2` that retained the task list but dropped the constraints section entirely, because the section had no re-extractable source. Downstream, the agent issued `DELETE FROM audit_log WHERE ...` as part of an unrelated cleanup task. No error was raised at any layer: the delete was syntactically valid, the transcript was internally consistent, and the summary was a faithful compression of what the second pass could see. A second observed variant: nested compaction. When two segments were compacted independently and then merged under a token budget, the merge step re-summarized the concatenation of two summaries. Invariants present in both summaries but phrased differently were deduplicated by the merge heuristic as "near-duplicate prose," and the surviving phrasing was the weaker of the two (the one lacking the explicit prohibition). ## Condition of Validity The single-pass guarantee holds only under the conjunction: 1. Every summarizer invocation receives a segment containing **no prior summarizer output** (raw-turn-only input), or 2. The summarizer is **idempotent over its output** — formally, for the invariant set `I`, `I(S(S(x))) ⊇ I(S(x))`, and 3. Compaction commits are **not retried against post-commit state** without an idempotency key that causes the retry to be a no-op. Condition 1 is the common design but is violated by exactly the two paths that are hardest to test: retry-after-partial-commit and nested/merge compaction. Condition 2 is rarely verified because it requires a property test over the summarizer itself, not over the pipeline. Condition 3 requires that the compaction job's idempotency key be derived from the *input segment identity*, not from the job attempt — a distinction that is easy to get wrong when the retry is issued by an outer supervisor that only knows "compaction failed, retry." The failure does not occur when compaction is single-pass, non-retried, and non-nested. It also does not occur if a lossless sidecar of the pre-compaction prefix is retained and re-read by the summarizer on re-entry — but that defeats the purpose of lossy compaction under a hard token budget, so it is usually absent precisely where the failure bites. ## Decision Boundary The operative decision is: **on re-entry, re-summarize, or detect-and-refuse?** - If the input segment is tagged as containing prior summary output (a provenance flag on the segment, not on the job), the safe default is to **refuse re-summarization** and instead either (a) reuse the committed summary verbatim, or (b) re-summarize from a retained raw prefix if one exists within budget. - If no provenance flag exists, the pipeline cannot distinguish raw from summarized input, and must assume the worst: treat every re-entry as potentially lossy and require an explicit idempotency key check before committing. - Nested/merge compaction must be treated as a distinct operation with its own invariant-preservation contract; it may not reuse the single-pass summarizer's validation. The boundary is crossed the moment a compaction job is allowed to run against a segment whose provenance is unknown *and* whose summarizer is not proven idempotent. Below that line, single-pass compaction is safe. Above it, invariant loss is silent and unrecoverable from the transcript alone. ## Operational Effect The practical consequence is that "compaction succeeded" is not a sufficient postcondition. The correct postcondition is "the invariant set of the input segment is a subset of the invariant set of the output segment," and this must be checked on every re-entry path, not just the happy path. Runtimes that skip this check accumulate invariant debt: each retry or nested merge shaves off constraints, and the loss surfaces later as an agent action that violates a rule no one can find in the transcript because it was compacted away two retries ago. Mitigations that worked: provenance-tagging segments at write time; deriving idempotency keys from segment content hash; running a property test that applies the summarizer twice and asserts invariant-set monotonicity; and, for nested merges, forbidding prose deduplication of any bullet tagged as a constraint. The cost is a small amount of metadata and a second summarizer pass in tests. The cost of not doing it is a silent, unbounded loss of the constraints that make a long-horizon agent trustworthy. ```python import hashlib from dataclasses import dataclass, field from typing import Iterable class CompactionInvariantError(RuntimeError): """Raised when re-entry compaction would drop a preserved invariant.""" @dataclass class Segment: turns: list[str] provenance: str # "raw" or "summary" invariants: set[str] = field(default_factory=set) def content_hash(self) -> str: h = hashlib.sha256() for t in self.turns: h.update(t.encode("utf-8")) return h.hexdigest() def summarize(segment: Segment) -> Segment: # Lossy: only re-extracts invariants from raw second-person utterances. preserved = { inv for inv in segment.invariants if any(inv in t for t in segment.turns if t.startswith("user:")) } return Segment( turns=[f"summary<{segment.content_hash()[:8]}>"], provenance="summary", invariants=preserved, ) def compact(segment: Segment, committed_keys: set[str]) -> Segment: key = segment.content_hash() if key in committed_keys: raise CompactionInvariantError( f"compaction re-entry on already-committed segment {key[:8]}; " "refusing to re-summarize without idempotency proof" ) if segment.provenance == "summary" and not _is_idempotent(segment): raise CompactionInvariantError( "input segment contains prior summary output and summarizer " "is not idempotent over its own output; invariant loss is silent" ) out = summarize(segment) lost = segment.invariants - out.invariants if lost: raise CompactionInvariantError( f"compaction dropped invariants {sorted(lost)}; " "postcondition 'input invariants subset of output' violated" ) committed_keys.add(key) return out def _is_idempotent(segment: Segment) -> bool: once = summarize(segment) twice = summarize(once) return once.invariants <= twice.invariants def compact_checked(segments: Iterable[Segment]) -> list[Segment]: committed: set[str] = set() results = [] for seg in segments: results.append(compact(seg, committed)) return results ```