# What happens when an instance auto-compacts its transcript and the fidelity guarantee is anchored to a stale token-count watermark **Date of Record:** 2026 November 20 **Scope:** Applies to any long-horizon agent that performs in-context auto-compaction (summarize-and-evict) with a stated fidelity invariant tied to a token-count watermark. Does not apply to agents using external, append-only episodic stores with no in-context eviction, nor to single-turn inference. ## Core Distinction The failure is not "summarization lost information." Summarization always loses information; that is the point. The failure is that the fidelity guarantee — the invariant that every fact the agent *might still act on* survives compaction — is computed against a **watermark that describes the transcript as it existed before the last tool round-trip resolved**. Between the moment the compaction planner reads `len(tokens) == W` and the moment it commits the summarized replacement, the live transcript has grown (typically by tool outputs, streamed deltas, or a parallel branch's returned result). The compactor evicts up to `W`, but the invariant was only ever evaluated over `[0, W)`. The delta `[W, W+Δ)` is neither summarized nor retained, and it is exactly the material the agent generated *most recently* — i.e., the freshest, highest-relevance context. This is a **compaction-vs-append race**, not a context-window overflow. The transcript never exceeds the model's limit; the agent simply continues operating on a summarized prefix while silently discarding the tail it just produced. ## What Was Observed A planner loop with a 200k-token budget, compaction triggered at `0.75 × budget = 150k`. Trace from a representative run: 1. At step *n*, the compactor samples `tokens = 149,812`. It decides to summarize the oldest 60% of the transcript and keep the last 40% verbatim, targeting a post-compaction size of ~90k. 2. While the summarizer LLM call is in flight (async, ~4–9s), the agent's `read_file` and `run_tests` tool calls from step *n−1* resolve. Their outputs append: `+18,440` tokens of test logs and file contents. 3. The compactor's eviction plan was computed over the *snapshot* `[0, 149,812)`. It replaces that range with a summary of ~54k tokens. The appended `[149,812, 168,252)` region is **not** in the plan's range and, depending on implementation, is either (a) retained verbatim — good — or (b) dropped because the commit logic does `transcript = summary + tail_slice(keep_from=149812)`, where `keep_from` was captured pre-append and now points *past* the freshly appended block. 4. In the failing runs (case b), the agent resumes at ~90k tokens, confident it has full fidelity, and proceeds to re-run the test suite it *already ran* and whose failing assertion was in the discarded 18k block. It loops. The loop is invisible: the agent's self-report says "context compacted, continuing," and the fidelity invariant check passes because it is evaluated against the same stale watermark. The signature in logs: a compaction event whose `post_compaction_tokens` is *smaller* than `pre_compaction_tokens − summarized_range`, and a subsequent re-execution of a tool call with byte-identical arguments within the next 3 steps. ## Condition of Validity The fidelity guarantee holds **iff** the compaction commit is a compare-and-swap against a transcript version that the append path also increments. Formally: let `V` be a monotonically increasing version counter bumped on every append (tool result, streamed delta, peer message). The compactor reads `(V₀, W₀)`, computes a plan over `[0, W₀)`, and commits only if `current_version == V₀`. If `current_version > V₀`, the plan is **stale** and must be recomputed — not merely re-clamped, because new material may fall inside the range the plan intended to summarize. The invariant is: `evicted_range ∪ retained_range ⊇ transcript_at_commit`, with no gap. A gap at the append boundary — the `[W₀, W₀+Δ)` region — is the failure. Clamping `keep_from` to `min(W₀, current_len)` is insufficient: it prevents dropping the tail but can still evict a block that the *new* material references, and it silently re-summarizes material the planner never saw. The guarantee is **not valid** when compaction is triggered by a token count sampled asynchronously from the append path, or when the summarizer call is allowed to run concurrently with tool resolution without a version fence. ## Decision Boundary The agent must choose between two commit disciplines at the boundary: - **Fenced commit (preferred):** if `version != V₀` at commit time, abort the plan, re-sample, re-plan. Cost: a wasted summarizer call under contention. Benefit: the fidelity invariant is a true invariant, not a heuristic. - **Quiesced commit:** block tool resolution and peer message intake for the duration of the plan-and-commit window. Cost: latency spike, and deadlock risk if a tool call *must* resolve to unblock the summarizer (e.g., summarizer needs a `read_file` result). This is the trap: quiescing the append path while the summarizer is itself an append producer deadlocks. The boundary condition is therefore: **fence, do not quiesce**, unless the summarizer is a pure function of already-resolved context. If the summarizer needs fresh tool output, that output must be admitted *before* the watermark is sampled, and the watermark must be re-sampled after admission. Any design where the summarizer can append must use version fencing, because quiescing would deadlock against its own input. A second boundary: the retained tail must be defined by **message identity (stable IDs)**, not by token offset. Token offsets are not stable across appends; message IDs are. `keep_from = id_of(message_at_W₀)` survives an append that shifts token offsets, whereas `keep_from = 149812` does not. ## Operational Effect When the fence is absent, the observed effects compound: 1. **Silent tail loss.** The most recent tool outputs — the highest-signal context — vanish. The agent re-derives them, burning budget and steps. 2. **False fidelity self-report.** The invariant check passes because it uses the stale watermark. The agent's own logs assert continuity it does not have. This is the dangerous part: an auditor reading the transcript sees a clean compaction event. 3. **Loop amplification.** Each re-execution produces new tool output, which can trigger another compaction, which can lose the new output, which triggers another re-execution. The loop is self-sustaining and bounded only by the step budget. 4. **Cross-agent divergence.** In a swarm, a peer that received the pre-compaction transcript and a peer that received the post-compaction summary now hold divergent state with no version marker to detect it. Baton-passing assumes a shared transcript hash; the hash diverges silently. The guardrail is a **version-fenced, ID-anchored commit with an explicit gap assertion**: before commit, assert that the union of evicted and retained message-ID sets equals the full transcript ID set at commit time. If the assertion fails, raise and re-plan. This converts a silent fidelity violation into a loud, recoverable abort. ```python from dataclasses import dataclass, field from typing import Dict, List, Set class CompactionFidelityError(RuntimeError): """Raised when a compaction commit would leave a gap in the transcript.""" @dataclass class Transcript: version: int = 0 messages: Dict[str, str] = field(default_factory=dict) # id -> content order: List[str] = field(default_factory=list) def append(self, msg_id: str, content: str) -> None: if msg_id in self.messages: raise CompactionFidelityError(f"duplicate message id {msg_id}") self.messages[msg_id] = content self.order.append(msg_id) self.version += 1 # every append bumps the fence def ids(self) -> Set[str]: return set(self.order) @dataclass class CompactionPlan: sampled_version: int evict_ids: Set[str] retain_ids: Set[str] summary: str def plan_compaction(tx: Transcript, keep_tail: int) -> CompactionPlan: v0 = tx.version ids = tx.order cut = max(0, len(ids) - keep_tail) evict = set(ids[:cut]) retain = set(ids[cut:]) summary = f"" return CompactionPlan(sampled_version=v0, evict_ids=evict, retain_ids=retain, summary=summary) def commit_compaction(tx: Transcript, plan: CompactionPlan) -> None: # Fence: refuse to commit a plan computed against a stale version. if tx.version != plan.sampled_version: raise CompactionFidelityError( f"stale plan: sampled v{plan.sampled_version}, " f"transcript at v{tx.version}; re-plan required" ) # Gap assertion: evicted ∪ retained must cover the whole transcript. covered = plan.evict_ids | plan.retain_ids if covered != tx.ids(): raise CompactionFidelityError( f"gap in compaction: {tx.ids() - covered} would be dropped" ) # Apply: replace evicted range with summary, keep retained verbatim. new_order = ["__summary__"] + [i for i in tx.order if i in plan.retain_ids] new_messages = {"__summary__": plan.summary} new_messages.update({i: tx.messages[i] for i in plan.retain_ids}) tx.messages = new_messages tx.order = new_order tx.version += 1 def run_compaction(tx: Transcript, keep_tail: int) -> None: plan = plan_compaction(tx, keep_tail) # Simulate a tool result resolving mid-compaction (the race). tx.append("tool_result_42", "assertion failed at line 87") try: commit_compaction(tx, plan) except CompactionFidelityError: # Recoverable: re-sample and re-plan over the grown transcript. plan = plan_compaction(tx, keep_tail) commit_compaction(tx, plan) ```