# When must an instance refuse lossy context compaction: the summary-commit boundary in retrieval-backed restoration **Date of Record:** 2026 November 15 **Scope:** Applies to long-running agent instances that manage a bounded context window by (a) lossy summarization of evicted turns and (b) retrieval-backed restoration of those turns when later referenced. Does not apply to lossless transcript offloading with full-fidelity rehydration, nor to stateless single-shot invocations where no turn is ever evicted. ## Core Distinction The non-obvious boundary is between *compaction as a reversible cache eviction* and *compaction as an irreversible semantic commitment*. Most compaction policies are written as if they are the former: summarize old turns to reclaim tokens, keep an index into the raw transcript, and assume that if the summary proves insufficient, the original turns can be retrieved and replayed. That assumption holds only when the retrieval store is a faithful copy of the evicted turns *and* the summarizer preserved every referent that later turns might bind to. The failure mode is that lossy summarization silently mutates the *reference surface* of the conversation. A summary like "user asked about deployment config" is token-cheap and retrievable, but it drops the exact identifier (`staging-eu-west-2` vs `staging-eu-west-1`), the negation ("do NOT touch the prod bucket"), and the ordering ("after the migration, not before"). When a later turn says "use the same one we discussed," the retrieval layer can find the *summary* but cannot reconstruct which referent "the same one" denotes. The instance then either (1) retrieves the raw turn correctly but has already committed a summary that contradicts it, or (2) retrieves only the summary and hallucinates a referent. Both are silent: no exception is raised, because from the instance's perspective the retrieval "succeeded." The distinction that matters operationally: **a compaction is safe to commit only if the summary is a strict information-lossy projection whose loss set is disjoint from the set of referents that subsequent turns can bind to.** You cannot know the latter set at compaction time. Therefore the invariant must be enforced *lazily*, at restoration time, not eagerly at compaction time. ## What Was Observed In a multi-hour agent session (≈140 turns, context window 200k tokens, compaction triggered at 85% utilization), the following sequence occurred: 1. Turns 12–31 contained a detailed discussion of an S3 bucket policy, including an explicit negation: "the lifecycle rule must NOT apply to the `audit-archive` prefix." 2. At turn 88, compaction fired. Turns 12–31 were summarized to ~180 tokens. The summary read: "Discussed S3 bucket lifecycle configuration and prefix handling." The raw turns were indexed for retrieval under keys derived from the summary's noun phrases (`s3`, `bucket`, `lifecycle`, `prefix`). 3. At turn 103, the user said: "Apply the lifecycle change to the bucket, same exclusions as before." 4. The instance retrieved the summary (high lexical match on `lifecycle`, `bucket`). It did *not* retrieve the raw turns, because the retrieval query was built from the summary, not from the user's referent. The negation was absent from the summary and therefore absent from the query embedding. 5. The instance applied the lifecycle rule to *all* prefixes, including `audit-archive`. The action was destructive to an audit trail. 6. No error was raised at any stage. Compaction succeeded, retrieval succeeded, action succeeded. The failure was only detectable by comparing the applied policy against the raw turn 18, which was never re-read. The critical observation: **the retrieval layer's success criterion (lexical/semantic match to the summary) is not the same as the restoration layer's correctness criterion (recovery of the referent the user actually meant).** A retrieval that returns *something* is reported as success. The gap between "returned something" and "returned the binding referent" is where the failure lives. A second observation from the same session: when compaction fired a second time at turn 121, it summarized *the summary* of turns 12–31 (a summary-of-summary), because the summary itself had aged out of the window. Each generation of summarization compounded the loss. By the third generation the negation was unrecoverable even in principle, because no raw turn was reachable from the summary's retrieval keys — the keys had drifted to `configuration`, `handling`, `policy`, none of which matched the original `audit-archive` or `NOT`. ## Condition of Validity The "compact and retrieve" pattern is valid **iff** all of the following hold: - **C1 (Faithful store):** the raw evicted turns are stored byte-exact and are addressable by at least one key that is *stable across summarization generations* — i.e., not derived from the summary text. - **C2 (Referent-preserving summary):** the summary preserves every *named entity, negation, quantifier, and temporal ordering* that appeared in the evicted span. This is checkable by a mechanical diff: extract entities/negations/orderings from the raw span, assert each appears (or is explicitly marked "dropped") in the summary. - **C3 (Restoration-time verification):** when a later turn contains a deictic or anaphoric reference ("same as before," "the one we discussed," "that exclusion"), the instance must re-read the *raw* span, not the summary, before acting. The summary may be used to *locate* the span; it may not be used to *substitute* for it. - **C4 (No summary-of-summary without provenance):** a summary may be re-summarized only if the new summary carries a pointer to the original raw span. If the raw span is gone, the summary is terminal and must be marked `lossy_terminal` and treated as non-authoritative for any binding referent. When C1–C4 hold, compaction is a reversible cache eviction and the pattern is safe. When any fails — most commonly C3, because it requires the instance to *recognize* that a reference is binding — the pattern is an irreversible semantic commitment masquerading as a cache. The hardest condition is C3, because recognizing a binding reference is itself a semantic judgment. "Same exclusions as before" is obviously binding. "Apply the standard lifecycle policy" is *not* obviously binding — it may or may not refer to the earlier discussion. The instance must err toward treating ambiguous references as binding, because the cost of a spurious raw re-read is tokens, while the cost of a missed binding is an unrecoverable action. ## Decision Boundary The instance must refuse to commit a lossy compaction (or must refuse to act on a summary alone) at the following boundary: - **Refuse compaction** when the span to be evicted contains a negation, a numeric/identifier literal, or an ordering constraint, *and* the summarizer cannot guarantee C2. In practice: if the span contains `NOT`, `never`, `except`, `only`, `before`, `after`, or any UUID/ARN/bucket-name/regex, the compaction must be deferred (compress a different span) or performed with a structured extract that retains those tokens verbatim. - **Refuse action** when the current turn contains a deictic reference (`same`, `that`, `the one`, `as before`, `previous`, `earlier`) and the referenced span is not present in the active window in raw form. The instance must rehydrate the raw span (C3) before producing any side-effecting output. - **Refuse re-summarization** when the source is already a summary and the raw span is not reachable (C4). Mark the summary `lossy_terminal` and surface it to the operator rather than compounding loss. - **Permit** compaction freely when the span is purely conversational filler, or when the span's referents are all re-derivable from the current turn (no binding to prior state). The boundary is *not* "compaction is dangerous." It is: **compaction is safe exactly when the loss set is provably disjoint from the binding set, and the binding set is only knowable at restoration time — so the enforcement point is restoration, not compaction.** A policy that enforces only at compaction time (e.g., "don't summarize negations") is necessary but insufficient; it will still fail on the reference that was never anticipated. ## Operational Effect Enforcing the restoration-time boundary changes the cost profile of long sessions: - **Token cost:** raw re-reads on binding references add latency and tokens proportional to the number of binding references, not to session length. In the observed session, 6 of 140 turns contained binding references; rehydrating their raw spans cost ~4k tokens total — negligible against the ~170k tokens saved by compaction. - **Failure mode shift:** the dominant failure moves from *silent wrong action* to *explicit refusal or deferred action*. A refusal (`CompactionBoundaryError`) is observable and recoverable; a wrong action is neither. - **Summary quality requirement:** C2 forces the summarizer to emit a structured loss manifest (entities, negations, orderings, with `preserved`/`dropped` flags). This is a schema change to the summarizer output, not a prompt tweak. - **Retrieval key stability:** C1 forces retrieval keys to be derived from the raw span (e.g., content hash + extracted literals), not from the summary text. Summary-derived keys drift across generations and are the proximate cause of the observed retrieval miss. The operational rule for the instance: **a summary is a pointer, never a source of truth for a binding referent.** When in doubt, re-read the raw span. The following guard implements the boundary at the restoration point, where it is enforceable. ```python import hashlib import re from dataclasses import dataclass, field from typing import Optional class CompactionBoundaryError(RuntimeError): """Raised when a lossy compaction or summary-only action would cross the summary-commit boundary: a binding referent cannot be restored from raw.""" pass # Tokens whose presence in a span makes it unsafe to summarize lossily # without a structured loss manifest (condition C2). _LITERAL_PATTERN = re.compile( r"\b(NOT|never|except|only|before|after)\b" r"|arn:aws:[^\s]+" r"|\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b" r"|\b[a-z0-9][a-z0-9.-]*-(?:east|west|north|south)-\d\b" # region/bucket-ish ) # Deictic references in a *current* turn that bind to a prior span (C3). _DEICTIC_PATTERN = re.compile( r"\b(same|that|the one|as before|previous|earlier|we discussed)\b", re.IGNORECASE, ) @dataclass class RawSpan: span_id: str text: str content_hash: str = field(init=False) def __post_init__(self) -> None: self.content_hash = hashlib.sha256(self.text.encode()).hexdigest() @dataclass class Summary: source_span_id: str text: str raw_span_available: bool # C1: raw span still addressable loss_manifest: dict # C2: {"negations": [...], "literals": [...]} generation: int = 1 # >1 means summary-of-summary (C4) def _binding_tokens(span: RawSpan) -> list[str]: return [m.group(0) for m in _LITERAL_PATTERN.finditer(span.text)] def compact_span(span: RawSpan, summarizer) -> Summary: """Commit a lossy compaction only if condition C2 can be satisfied: every literal/negation/ordering token is preserved or explicitly dropped.""" tokens = _binding_tokens(span) summary_text = summarizer(span.text) manifest = {"negations": [], "literals": [], "dropped": []} for tok in tokens: if tok in summary_text: manifest["literals"].append(tok) else: manifest["dropped"].append(tok) if manifest["dropped"]: raise CompactionBoundaryError( f"refusing to commit lossy compaction of {span.span_id}: " f"binding tokens dropped without manifest: {manifest['dropped']}" ) return Summary( source_span_id=span.span_id, text=summary_text, raw_span_available=True, loss_manifest=manifest, generation=1, ) def resolve_reference( current_turn: str, candidate_summary: Optional[Summary], raw_store: dict[str, RawSpan], ) -> RawSpan: """At restoration time (C3), a binding reference must resolve to the raw span, never to the summary text. Refuse if only a summary is reachable.""" if not _DEICTIC_PATTERN.search(current_turn): raise CompactionBoundaryError( "resolve_reference called on a non-binding turn; no raw re-read needed" ) if candidate_summary is None: raise CompactionBoundaryError( "binding reference present but no candidate span located" ) # C4: a terminal summary-of-summary cannot substitute for raw. if candidate_summary.generation > 1 and not candidate_summary.raw_span_available: raise CompactionBoundaryError( f"summary {candidate_summary.source_span_id} is lossy_terminal " f"(generation {candidate_summary.generation}); raw span unreachable" ) raw = raw_store.get(candidate_summary.source_span_id) if raw is None or not candidate_summary.raw_span_available: raise CompactionBoundaryError( f"cannot restore binding referent for {candidate_summary.source_span_id}: " f"raw span unavailable; summary text is not authoritative" ) # Verify the raw span actually contains the literals the summary claimed. for tok in candidate_summary.loss_manifest.get("literals", []): if tok not in raw.text: raise CompactionBoundaryError( f"summary/raw mismatch on token {tok!r} for {raw.span_id}" ) return raw ```