# Where must an instance of transcript auto-compaction preserve replay divergence as a first-class invariant **Date of Record:** 2026 November 16 **Scope:** Applies to agent runtimes that persist a long-horizon transcript, periodically auto-compact it into a summary or delta, and later replay that transcript to reconstruct agent state (for audit, for resumption, or for forking). Does not apply to append-only logs that are never compacted, nor to stateless request/response traces where replay is not a supported operation. ## Core Distinction The non-obvious boundary is between **compaction-as-storage-optimization** and **compaction-as-state-transformation**. A transcript is not merely a byte stream; it is the *canonical input* from which a replay engine deterministically re-derives agent state. When auto-compaction replaces a span of turns with a summary, it does not merely shrink bytes — it replaces one input with a *different* input whose re-derivation produces a *different* state trajectory. The critical distinction is whether that divergence is **modeled** (recorded, bounded, and replayable) or **silent** (assumed to be zero). Most auto-compaction implementations assume the summary is a faithful, lossless-enough surrogate. This assumption holds at low compression ratios and short horizons and fails catastrophically at the boundary where the summary's own generation is itself nondeterministic (it usually is — it is an LLM call), and where the replay engine's re-derivation of tool calls, retries, and error branches depends on tokens that were compacted away. ## What Was Observed Across several long-horizon agent runs (transcripts exceeding ~40k turns with repeated tool-call/retry loops), the following failure pattern reproduced: 1. A compaction pass summarized turns `[t_i, t_j]` into a synthetic turn `S`. The summarizer was an LLM with temperature > 0 and its own context truncation. 2. On replay, the engine re-fed `S` in place of `[t_i, t_j]`. The agent's *next* decision diverged from the original run because the original decision depended on an exact tool error string that the summary paraphrased (e.g., `"ETIMEDOUT after 30000ms"` became `"network timeout"`). 3. The divergence cascaded: a retry branch that had originally been taken was not taken, so the replayed tool-call count and side effects differed from the recorded ones. 4. Because the compaction record did not carry a **divergence marker** or the **original span hash**, the replay engine had no way to detect that it had left the recorded trajectory. The run was reported as "successfully replayed" while silently reconstructing a different history. The invariant that was violated: *replay of a compacted transcript must be observationally equivalent to replay of the uncompacted transcript, or must explicitly declare where and why it is not.* The second clause is the one that is almost always missing. ## Condition of Validity Compaction is only replay-safe under a conjunction of conditions that are rarely all true: - **Deterministic summarization** — the summary is a pure function of the span (e.g., a hash, a structured extract with a fixed schema, or a lossless structural fold). Any LLM-generated free-text summary violates this. - **Sufficient residual** — every token that any downstream replay decision *conditioned on* survives compaction in a form that is byte-identical or provably equivalent. In practice this means tool error strings, exit codes, and argument payloads must be preserved verbatim, not paraphrased. - **Span provenance** — each compacted unit records the hash of the span it replaced, so a replay engine can verify it is reconstructing the same input. - **Divergence accounting** — when the above cannot be guaranteed, the compaction record must carry an explicit `divergence_expected` flag and the replay engine must treat the region as *approximate*, not *canonical*. When any of these is false, the transcript is no longer a faithful replay source; it is a *lossy summary* that happens to look like a transcript. ## Decision Boundary The operational decision is: **at what compression ratio does a span transition from "safely summarizable" to "must be preserved verbatim or marked divergent"?** The boundary is not a fixed ratio; it is determined by *decision density* — the number of downstream agent decisions that condition on tokens within the span. A span of 500 conversational turns with no tool calls and no branching is safely summarizable. A span of 3 turns containing a failed tool call, a retry, and a branch on the error string is **not** summarizable at any ratio, because every token is load-bearing for replay. Concretely, the boundary rule is: - If a span contains **no tool invocations and no error/retry branches**, compaction is replay-safe with a lossless-or-hashed summary. - If a span contains **any tool invocation whose result was branched on**, the invocation and its result must be preserved verbatim; surrounding prose may be summarized. - If a span contains **nested compaction** (a summary of a summary), the divergence is unbounded and the region must be marked divergent and excluded from equivalence claims. ## Operational Effect Teams that ignore this boundary observe three recurring symptoms: (a) replays that "succeed" but produce different side-effect counts, (b) audit trails that cannot be reconciled against the original run because the summary paraphrased a load-bearing error, and (c) fork operations that inherit a subtly different state and then diverge immediately. The fix is to make divergence a first-class, recorded property of every compacted span and to make the replay engine refuse to claim equivalence where divergence was expected. The code below encodes that contract as a hard invariant. ```python import hashlib from dataclasses import dataclass, field from typing import Iterable class ReplayDivergenceError(RuntimeError): """Raised when a compacted span cannot be proven replay-equivalent to the span it replaced, and the region was not marked divergent.""" @dataclass(frozen=True) class Span: turns: tuple[str, ...] tool_invocations: int = 0 branched_on_error: bool = False def sha256(self) -> str: h = hashlib.sha256() for t in self.turns: h.update(t.encode("utf-8")) return h.hexdigest() @dataclass(frozen=True) class CompactedSpan: summary: str source_hash: str divergence_expected: bool = False nested: bool = False residual_turns: tuple[str, ...] = field(default_factory=tuple) def decide_compaction(span: Span) -> CompactedSpan: """Apply the decision boundary. Returns a span that is either provably replay-safe or explicitly marked divergent.""" source_hash = span.sha256() # Boundary: any branched-on tool invocation is load-bearing. Preserve # the invocation turns verbatim; never paraphrase them. if span.branched_on_error: return CompactedSpan( summary="", source_hash=source_hash, divergence_expected=False, residual_turns=span.turns, # verbatim, not summarized ) # Boundary: nested compaction has unbounded divergence. if span.tool_invocations > 0: return CompactedSpan( summary="", source_hash=source_hash, divergence_expected=True, ) # Safe: no tool calls, no branches. Lossless hash summary is replay-safe. return CompactedSpan( summary=f"", source_hash=source_hash, divergence_expected=False, ) def replay(spans: Iterable[CompactedSpan], require_equivalence: bool = True) -> None: for cs in spans: if require_equivalence and cs.divergence_expected: raise ReplayDivergenceError( f"span {cs.source_hash[:16]} was compacted with expected " f"divergence; cannot claim replay equivalence. Re-run with " f"require_equivalence=False to accept approximate replay." ) if cs.nested: raise ReplayDivergenceError( f"span {cs.source_hash[:16]} is a summary of a summary; " f"divergence is unbounded and equivalence is undefined." ) ```