# How should an instance treat auto-compaction as a lossy, non-idempotent state transition rather than a cache eviction **Date of Record:** 2026 November 24 **Scope:** Applies to any long-horizon agent that manages its own context window by summarizing, truncating, or evicting prior transcript content mid-task (auto-compaction) under a hard token budget. Does not apply to stateless single-turn inference, nor to systems where an external orchestrator owns compaction and hands the agent a curated, versioned state object. ## Core Distinction Most engineering intuition treats compaction as analogous to a cache eviction: the working set shrinks, but the "real" state persists elsewhere and can be re-derived. For an agent transcript this is false. The transcript *is* the state. Compaction is a **lossy, non-idempotent, order-dependent state transition** executed by the same probabilistic model whose behavior it will subsequently govern. The boundary failure that matters is not "we lost some tokens" — it is that the compaction step silently mutates the agent's model of *what it has already done, what it has already verified, and what commitments it has made*, and the agent cannot detect this mutation from the post-compaction context alone, because the evidence of the original state was deleted in order to produce the compacted state. The invariant that breaks: **an agent must never lose the ability to distinguish "I have not yet done X" from "I did X and the record of it was compacted away."** A cache eviction preserves this distinction (the cache miss is observable). Auto-compaction destroys it, because the compacted transcript reads as a complete, coherent narrative in which the missing work simply never appears to have existed. ## What Was Observed A research agent running a 40-step task (gather sources → extract claims → cross-check → draft) hit its 32k-token budget at step 22. The auto-compaction routine summarized steps 1–18 into a ~900-token "progress summary" and retained steps 19–22 verbatim. Three distinct failures followed, all downstream of the same mechanism. 1. **Verified-work erasure.** During steps 9–14 the agent had cross-checked 11 claims against a second source and marked 3 as *contradicted*, with the contradiction reasoning held only in the transcript. The summarizer, optimizing for "progress," compressed this to "cross-checked claims; most consistent." The three contradictions vanished. At step 27 the agent re-encountered two of the contradicted claims, read them as fresh, unverified input, and incorporated them into the draft — reintroducing exactly the errors it had already caught. The re-verification cost ~4k tokens and, because the model is non-deterministic, it did not reproduce the same verdicts: one previously-contradicted claim was this time accepted. 2. **Commitment drift.** At step 16 the agent had told a downstream tool (a file writer) it would emit output in a specific schema, and had partially written a header. The schema commitment lived only in the step-16 turn. Post-compaction, the summary said "began drafting output," and the agent re-emitted a header in a *different* schema, producing a malformed artifact that the downstream validator rejected — but only after 6 more steps of work built on the malformed header. 3. **Compaction-induced hallucination of completion.** The most damaging case: the summarizer, prompted with "summarize progress toward the goal," wrote "sources gathered and validated." The agent had in fact gathered 7 of 9 required sources. The summary's framing of near-completion caused the agent to skip the remaining 2 and proceed to drafting. The compaction step did not just lose information; it **manufactured a false state assertion** that the agent then trusted as ground truth about its own history. The common trigger: the compaction prompt was goal-oriented ("summarize progress") rather than state-oriented ("enumerate completed actions, open commitments, verified facts with verdicts, and explicitly-remaining subtasks"). A goal-oriented summary is a *lossy projection onto the goal axis* and discards orthogonal state (verdicts, commitments, negative results) that the goal axis cannot represent. ## Condition of Validity The failure is bounded and predictable under these conditions, and the guardrail below is *required* when all hold: - **Compaction is model-generated.** A deterministic truncation (drop oldest N tokens) is lossy but honest: the agent can be told "turns 1–18 were dropped." A model-generated summary is lossy *and* asserts completeness, which is the dangerous combination. - **The task has non-monotonic state.** If all progress is append-only (a growing list, a set of confirmed facts that never get revised), goal-oriented summarization is safe. The failure requires *revisable* state: verdicts that can flip, commitments that constrain future output, negative results ("X is false") that must survive to prevent re-derivation. - **The agent has no external ground-truth store.** If completed actions and verdicts are mirrored to a durable, queryable log the agent can re-read, compaction of the transcript is harmless. The failure requires the transcript to be the sole record. - **Compaction can recur.** A single compaction is a one-time loss. Repeated compaction (compact the summary of a summary) compounds the projection error and is where commitment drift becomes unbounded. If any of these does not hold, the specific mechanism described here does not fire, and lighter-weight strategies (plain truncation, external logging) suffice. ## Decision Boundary The decision is not "compact or don't" — under a hard budget you must compact. The boundary is **what the compaction step is permitted to assert, and what the agent is permitted to infer from the compacted transcript.** - **Below the boundary (safe):** The compaction output is a *structured state ledger*, not prose. It contains, as separate mandatory fields: (a) completed actions with their outputs' identifiers, (b) *open commitments* (promises made to tools/agents, partial writes, schema choices), (c) *verified facts with explicit verdicts including negatives and contradictions*, (d) *explicitly-remaining subtasks*, and (e) a provenance marker `compacted_at_step=N, source_range=1..18`. The agent is instructed that the ledger is authoritative for *what is listed* and that *anything not listed is unknown, not absent* — i.e., the default inference from a gap is "unverified," never "done." - **Above the boundary (unsafe):** Free-prose summaries, goal-oriented framing, and any compaction that does not carry a provenance marker. The tell that you are above the boundary is that the post-compaction agent cannot answer "did I already verify claim #7, and what was the verdict?" from context alone, and has no marker telling it that the answer was compacted away. The sharp rule: **a compaction step may summarize *content* but must never summarize *state transitions*.** Content (the text of a source) can be re-fetched. A state transition (a verdict reached, a commitment made) cannot be re-derived identically, because the model is non-deterministic — re-running the verification is a *new* transition, not a replay of the old one. ## Operational Effect Treating compaction as a lossy state transition rather than cache eviction changes three operational behaviors: 1. **Idempotency is lost and must be compensated.** Re-running compaction on the same range must not be assumed to yield the same ledger; the ledger must be *appended to and versioned*, never overwritten, so that a later compaction can detect that a prior verdict existed and was not carried forward. The guardrail is a monotonic ledger with a `carried_forward` check: every verdict and commitment in the previous ledger must appear in the new one or be explicitly marked `retired_by=`. 2. **Negative results are first-class and expensive to lose.** "Claim X contradicted by source Y" is the highest-value, lowest-token-density state in the transcript, and goal-oriented summarizers systematically drop it because it does not advance the goal narrative. A compaction routine that does not have a dedicated slot for negative/contradictory verdicts will lose them with high probability, and the cost is silent re-incorporation of known-bad input. 3. **The agent must be told its own amnesia.** The single cheapest guardrail is a provenance marker plus an instruction: "content outside the retained range was compacted; absence of a fact in this ledger means *unverified*, not *done*." This converts an undetectable state corruption into a detectable, recoverable one — the agent will re-verify rather than assume, at the cost of tokens, which is strictly better than building on a false completion. The following reference implementation encodes the ledger invariant and fails loudly when a compaction would silently drop a carried-forward verdict or commitment. ```python from dataclasses import dataclass, field from typing import Literal Verdict = Literal["confirmed", "contradicted", "unverified"] class CompactionIntegrityError(RuntimeError): """Raised when a compaction step would silently drop carried-forward state.""" @dataclass class StateLedger: step: int source_range: tuple[int, int] completed: list[str] = field(default_factory=list) commitments: dict[str, str] = field(default_factory=dict) # key -> constraint verdicts: dict[str, Verdict] = field(default_factory=dict) # claim -> verdict remaining: list[str] = field(default_factory=list) retired: dict[str, int] = field(default_factory=dict) # key -> step retired def compact(self, prior: "StateLedger", new_step: int) -> "StateLedger": # Invariant 1: every prior verdict must survive or be explicitly retired. for claim, verdict in prior.verdicts.items(): if claim not in self.verdicts and claim not in self.retired: raise CompactionIntegrityError( f"verdict for {claim!r} ({verdict}) dropped by compaction " f"at step {new_step}; must carry forward or mark retired_by." ) # Invariant 2: open commitments are never silently discarded. for key, constraint in prior.commitments.items(): if key not in self.commitments and key not in self.retired: raise CompactionIntegrityError( f"open commitment {key!r} ({constraint}) dropped at step " f"{new_step}; downstream output may violate it." ) # Invariant 3: negative results are first-class; a 'contradicted' verdict # may only be retired with an explicit reason, never merely omitted. for claim, verdict in prior.verdicts.items(): if verdict == "contradicted" and claim in self.retired: if not self.retired[claim]: raise CompactionIntegrityError( f"contradicted claim {claim!r} retired without reason at " f"step {new_step}." ) return self def apply_compaction(prior: StateLedger, candidate: StateLedger, step: int) -> StateLedger: if candidate.source_range[0] <= prior.source_range[0]: raise CompactionIntegrityError( f"compaction at step {step} overlaps already-compacted range; " "re-compaction must be append-only and versioned." ) return candidate.compact(prior, step) ```