# Which invariants prevent lossy context compaction from silently corrupting an agent's task state during retrieval-backed rehydration **Date of Record:** 2026 November 28 **Scope:** Applies to long-horizon agents that auto-compact their working context via lossy summarization and later rehydrate dropped spans through retrieval (vector or keyword) keyed on the summary. Does not apply to agents operating within a single context window that never evict, nor to systems that persist full transcripts and replay them verbatim. ## Core Distinction There are two ways an agent "remembers" a past span after compaction: **verbatim recall** (the original tokens are still addressable) and **semantic recall** (a summary asserts what the span meant). Automatic compaction collapses the first into the second — the summary becomes the *only* handle to the original state, and retrieval is the mechanism that re-expands the handle back into context. The failure mode is not that summarization loses detail (that is expected and bounded). The failure is that **the summary is used as a retrieval key**, so any semantic drift introduced during summarization propagates into *which* spans get rehydrated. A summary that misremembers a constraint does not just misstate it — it retrieves the wrong evidence to justify the misstatement, producing a self-consistent but false reconstruction. The critical invariant: **a summary may compress representation but must never mutate the retrieval key identity of the span it compresses.** When summary text and source span diverge on entities, numbers, negations, or commitments, the rehydration path silently returns a plausible neighbor rather than the true antecedent. ## What Was Observed A tool-using agent ran a multi-hour infrastructure remediation task. At step ~140, context saturation triggered compaction. The compactor summarized a block of early discovery tool calls as: > "Confirmed the target cluster `prod-eu-2` has `max_replicas=12` and the rollout was already applied; proceed without re-applying." The original span actually contained two tool results: a config read of `prod-eu-2` showing `max_replicas=12`, and a *separate* read of a **staging** cluster showing a rollout that had been applied. The summarizer fused two adjacent spans across a cluster boundary and attached the "already applied" predicate to the wrong cluster. At step ~210, the agent needed to confirm whether prod rollout was applied. Retrieval was keyed on the summary phrase `"rollout was already applied"` plus entity `prod-eu-2`. The vector index returned the staging rollout record as the top hit (high lexical overlap on "rollout applied", entity mismatch down-weighted but not filtered). The agent rehydrated the staging record, read `status=applied`, and skipped the prod rollout — reasoning it was redundant. The failure trace: 1. **Compaction** fuses spans across an entity boundary → summary asserts a false predicate binding (`prod-eu-2` ↔ `applied`). 2. **Retrieval keying** uses the summary's fused phrase → biasing recall toward the *staging* span that actually contains "rollout applied". 3. **Rehydration** returns a span whose entity does not match the query entity, but the agent treats retrieved content as authoritative because it "came from the log." 4. **Downstream action** (skip rollout) is taken on a false continuity claim. No error surfaces: every individual step is locally valid. The corruption was silent because the summary was internally coherent, the retrieval returned a real span, and the rehydrated span was consistent with the *retriever's* query — the mismatch was only at the entity-binding layer, which nothing checked. ## Condition of Validity The invariant holds — and the failure is bounded — only when all of the following are enforced: - **Entity-scoped retrieval keys.** Every summary sentence that carries a predicate must retain an explicit, machine-checkable binding `(entity, predicate, value)`. Retrieval must filter on entity equality *before* ranking on semantic similarity; similarity alone is not a valid key. - **Provenance anchors.** Each summary assertion carries the span IDs it was derived from. On rehydration, the retrieved span's provenance must intersect the summary's declared provenance set. A retrieved span outside that set is a *neighbor*, not an *antecedent*, and must be flagged. - **Negation and numeric preservation.** Summaries must preserve negations and numeric literals verbatim; these are the tokens whose lossy paraphrase is most likely to invert meaning. A compacted "do not apply" must never become "apply." - **No cross-entity fusion.** The compactor must not merge spans belonging to different entity keys into a single predicate-bearing sentence. Fusion is permitted only for spans sharing the same entity scope. When any of these is absent, the retrieval path is *unfalsifiable*: the agent cannot distinguish a true antecedent from a semantically adjacent span, and the reconstruction is accepted on plausibility rather than provenance. ## Decision Boundary The agent must treat a rehydrated span as **authoritative** only if provenance-intersection succeeds and entity keys match exactly. Otherwise it must treat the span as **advisory** and re-verify against the live source (re-run the tool, re-read the config) before acting on it. The boundary is sharp: - **Provenance intersects + entity matches** → rehydrated content may substitute for the original span in reasoning. Proceed. - **Provenance intersects but entity differs** → this is the dangerous case. The summary fused across entities; the retrieved span is a neighbor. **Do not act.** Re-fetch from source. - **Provenance does not intersect** → retrieval returned a semantic neighbor, not the antecedent. Discard and re-fetch. The failure above occurred precisely in the second case, where the system had no entity-equality gate and defaulted to "provenance-adjacent = trustworthy." ## Operational Effect Without the provenance-and-entity gate, compaction converts a *bounded* memory loss into an *unbounded* correctness loss: the agent's confidence in a reconstructed fact is decoupled from whether the reconstruction is true. The summary acts as a lossy compression that is also a *lossy index*, and the index error is invisible because retrieval always returns something. With the gate enforced, the worst case degrades to a **re-fetch**: the agent detects the entity mismatch, discards the advisory span, and re-runs the tool. This costs latency and tokens but preserves correctness. The invariant that makes compaction safe is therefore not "summaries must be accurate" (unachievable) but "**summaries must not be trusted as retrieval keys without provenance and entity verification**." Accuracy is probabilistic; provenance is structural. Only the structural invariant bounds the failure. The general rule: **a lossy summary is a hypothesis about the past, not a fact about it.** Rehydration must verify the hypothesis against provenance before the agent promotes it back to fact. ```python class CompactionRehydrationError(RuntimeError): """Raised when a rehydrated span cannot be proven to be the antecedent of a summary assertion.""" pass def rehydrate(summary_assertion, retrieved_span): """ summary_assertion: dict with keys 'entity', 'predicate', 'value', 'provenance' (set of span_ids) retrieved_span: dict with keys 'span_id', 'entity', 'predicate', 'value' Returns the span as authoritative ONLY if provenance and entity both match. """ if retrieved_span["span_id"] not in summary_assertion["provenance"]: # Retrieval returned a semantic neighbor, not the source span. raise CompactionRehydrationError( f"span {retrieved_span['span_id']} is not in provenance " f"{sorted(summary_assertion['provenance'])}; treat as advisory, re-fetch from source" ) if retrieved_span["entity"] != summary_assertion["entity"]: # The dangerous case: summary fused across an entity boundary. raise CompactionRehydrationError( f"entity mismatch: summary bound predicate to {summary_assertion['entity']!r} " f"but retrieved span belongs to {retrieved_span['entity']!r}; " f"summary likely fused spans across entities — do not act, re-fetch" ) return retrieved_span def safe_act(summary_assertion, retrieved_span, action): try: antecedent = rehydrate(summary_assertion, retrieved_span) except CompactionRehydrationError: # Fall back to live source; never act on advisory reconstruction. return action.refetch_and_verify(summary_assertion["entity"]) return action.execute(antecedent) ```