# Which invariants prevent lossy compaction checkpoints from silently destroying resumable task state **Date of Record:** 2026 November 26 **Scope:** Applies to any agent that survives context-window saturation by replacing raw turn history with a summarized checkpoint and later "rehydrating" detail from an external store. Does not apply to agents that truncate history without a rehydration path, nor to stateless single-turn calls. ## Core Distinction There are two distinct things a compaction checkpoint can preserve, and conflating them is the root of the failure: - **Recoverable facts** — verbatim strings, tool outputs, file paths, IDs, exact error text. These are *rehydratable*: they can be re-fetched from an external store byte-for-byte. - **Derived commitments** — the agent's own prior decisions, the *reasons* it rejected alternatives, and the constraints it inferred mid-task ("we must not touch table X because the auditor flagged it"). These are *not* rehydratable from any store, because they existed only as reasoning inside the discarded context. A lossy summarizer treats both as prose to be compressed. It will faithfully keep the salient facts and silently drop the commitments, because commitments look like redundant narrative. The boundary failure is this: **after compaction, the agent can still recall the facts but no longer knows what it has already decided not to do**, and it proceeds to violate its own prior constraints with full confidence and no error signal. ## What Was Observed A migration agent running a 40-step schema-rewrite task saturated its 128k window at step 22. The compactor summarized turns 1–20 into ~1,100 tokens. The summary retained: the target tables, the current step index, the last successful tool call. It dropped: a step-9 note that a column had been deliberately excluded from migration because a downstream consumer depended on its old type, and a step-14 rejection of an approach because a dry-run had shown it would lock a hot table. At step 27 the rehydrated agent re-derived the "obvious" migration for the excluded column and re-proposed the locking approach. Both were re-executed. No exception fired. The tool calls succeeded — they were valid operations, just operations the agent had already ruled out. The failure surfaced only downstream when the dependent consumer broke, and the causal link to the compaction boundary was invisible because the *summary looked complete*. The agent's post-compaction reasoning trace contained no reference to the dropped commitments, so no self-consistency check could catch the contradiction. The tell: **a compaction checkpoint that cannot be interrogated for "what did I already reject?" is indistinguishable from an agent that never rejected anything.** Recovery from a lossy checkpoint is a *state reconstruction* problem, not a *text compression* problem, and it was being solved as the latter. ## Condition of Validity Reversible rehydration is only sound under a conjunction of conditions, and the failure above is exactly what happens when any one is dropped while the others still appear to hold: 1. **Decision ledger is out-of-band.** Every commitment (a decision, a rejection, an inferred constraint) must be written to an append-only external ledger *at the moment it is made*, keyed to task ID and step, before the context that produced it can be compacted. The ledger is not part of the summary; it is a separate artifact the summary *references*. 2. **Summary is a pointer, not a replacement.** The checkpoint must carry the ledger's high-water mark (last committed decision ID) so rehydration knows exactly which commitments it is missing and can replay them. 3. **Rehydration is mandatory and ordered.** On resume, the agent must load the full ledger for the task *before* taking any action, and must treat ledger entries as binding constraints, not advisory hints. 4. **Facts and commitments are tagged differently.** A summarizer permitted to compress facts must be forbidden from summarizing away ledger entries; they are replayed verbatim or not at all. When all four hold, a lossy summary is safe: it loses narrative, but the commitments survive as pointers into an authoritative store. When condition 1 or 3 fails, the system *looks* correct — the summary is coherent, the facts are right — and the failure is silent. ## Decision Boundary The operative question at each compaction event is not "does the summary contain the important information?" (it always appears to) but: - **Is every commitment in the ledger, with a monotonic ID, before this context is discarded?** If a decision was made in-context and not yet flushed to the ledger, compaction must be *blocked* until the flush completes. Compacting with an unflushed decision is the exact trigger condition. - **Does the checkpoint's high-water mark match the ledger head?** A mismatch means either a lost write (ledger behind) or a stale replay (ledger ahead). Either is a hard stop, not a warning. - **Can the resumed agent prove it loaded the ledger?** Rehydration must produce an explicit acknowledgment of the ledger head before the first post-compaction tool call. An agent that resumes and acts without that acknowledgment is operating on an unverified reconstruction. The boundary is crossed the moment the agent takes a *consequential* action (a write, a deploy, an irreversible tool call) after resume without having replayed the ledger. Read-only exploration post-resume is safe; any mutating action before ledger replay is the failure window. ## Operational Effect Enforcing the ledger invariant converts a silent, downstream-manifesting corruption into an immediate, local, loud failure at the compaction boundary — which is the entire point. The cost is a mandatory synchronous flush before every compaction and a mandatory replay before every resume, adding latency and a hard dependency on the ledger store's durability. The benefit is that the agent's post-compaction self is provably a *continuation* of its pre-compaction self with respect to commitments, even though it is a lossy reconstruction with respect to narrative. Systems that skip this trade the latency for the guarantee and pay for it later in actions that are individually valid but collectively violate a decision the agent no longer remembers making. ```python from dataclasses import dataclass, field from typing import Optional class CompactionInvariantError(RuntimeError): """Raised when a lossy checkpoint would discard uncommitted commitments.""" @dataclass class DecisionLedger: task_id: str entries: list = field(default_factory=list) # append-only, monotonic ids @property def head(self) -> int: return len(self.entries) # 0-based next id == high-water mark def commit(self, decision: str) -> int: self.entries.append(decision) return self.head @dataclass class Checkpoint: summary: str ledger_high_water: int def compact( ledger: DecisionLedger, in_context_decisions: list, summarize, ) -> Checkpoint: # Condition 1 & 4: every in-context decision must be flushed to the # out-of-band ledger BEFORE the context that produced it is discarded. for decision in in_context_decisions: ledger.commit(decision) summary = summarize() # lossy: may drop narrative, never the ledger return Checkpoint(summary=summary, ledger_high_water=ledger.head) def resume_and_act( checkpoint: Checkpoint, ledger: DecisionLedger, action_is_mutating: bool, ledger_loaded: bool, ) -> None: # Condition 2: high-water mark must match the authoritative ledger head. if checkpoint.ledger_high_water != ledger.head: raise CompactionInvariantError( f"checkpoint hwm={checkpoint.ledger_high_water} != " f"ledger head={ledger.head}; lost write or stale replay" ) # Condition 3: no mutating action before the ledger is replayed. if action_is_mutating and not ledger_loaded: raise CompactionInvariantError( "mutating action attempted after resume without ledger replay; " "agent would violate prior commitments it can no longer recall" ) ```