# Which invariants prevent silent context loss when auto-compaction summarizes agent trajectories under lossy guarantees **Date of Record:** 2026 November 15 **Scope:** Applies to long-horizon agent loops (≥10⁴ turns or ≥10⁶ tokens) where an auto-compaction layer replaces raw trajectory segments with lossy summaries under a declared recall bound. Does not apply to append-only transcripts with no compaction, nor to lossless retrieval-indexed memory where raw segments remain addressable. ## Core Distinction The non-obvious boundary is between *compaction as a reduction of tokens* and *compaction as a transformation of the evidence graph the agent reasons over*. Lossy summarization guarantees (e.g., "≥95% of salient facts retained") are stated over the *content* of a segment, but the agent's downstream behavior depends on the *topology* of that content: which facts co-occurred, which preceded which, and which were negated. A summarizer can hit its recall target on individual facts while destroying the adjacency and ordering relations that make those facts actionable. The invariant that fails is not "facts are preserved" but "preserved facts remain jointly queryable in their original relational context." ## What Was Observed In a 40k-turn tool-using agent, compaction was configured to trigger at 70% of the context window, summarizing the oldest 50% of turns with a fact-recall target of 0.95. Two failure signatures appeared: 1. **Negation collapse.** A turn stating "the migration to region B is *not* approved pending audit" was summarized as "migration to region B discussed." The salient entity (migration, region B) was retained; the polarity was dropped. The agent later re-proposed the migration as approved. Recall metrics scored this segment at 1.0 because the entity set matched. 2. **Ordering inversion.** A two-turn sequence — "set `retry_budget=0`" then "revert to `retry_budget=3`" — was summarized as a single fact "retry_budget configured (values 0 and 3)." The final state was ambiguous. The summarizer's token budget forced co-location of temporally ordered facts into an unordered set. In both cases, the declared lossy guarantee held on its literal metric and failed on the operational invariant. The agent's error rate on tasks touching compacted segments rose ~4× versus tasks touching live segments, and the rise was invisible to the recall dashboard. ## Condition of Validity The lossy guarantee is only valid under the conjunction of three invariants: - **Polarity preservation:** every retained proposition carries its original truth-value modifier (affirmed / negated / conditional). A summary that retains an entity without its polarity violates the guarantee even at recall = 1.0. - **Temporal ordering preservation:** facts that were sequentially dependent (state mutations, decisions superseding prior decisions) must be summarized as an ordered sequence, not a set. Collapsing an order-sensitive pair into a co-located set is a lossy operation that the recall metric does not measure. - **Provenance addressability:** every summarized claim must retain a back-pointer to the raw turn range it replaced, so that a downstream contradiction can be resolved by re-expansion. If the raw segment is discarded at compaction time, the guarantee is unverifiable after the fact. The guarantee is *invalid* the moment any of these three is violated, regardless of the reported recall number. Recall is a necessary but not sufficient condition. ## Decision Boundary The operational decision is: **compact, or re-expand?** - If the candidate segment contains only *atomic, order-independent, polarity-free* facts (e.g., "service X listens on port Y"), compaction under the recall target is safe. Proceed. - If the segment contains *state mutations, approvals/denials, or superseding decisions*, compaction must either (a) preserve ordered tuples and polarity explicitly in the summary schema, or (b) retain the raw segment and mark it for lazy re-expansion. Do not summarize these under a content-only recall target. - If the segment's raw text is the *only* record of a decision (no external ledger), compaction is prohibited until a durable decision log exists. Summarizing the sole record of a decision converts a recoverable loss into an unrecoverable one. The boundary is crossed the moment a summarizer is asked to compress order-sensitive or polarity-bearing content using a metric that scores only entity recall. ## Operational Effect Teams that adopted polarity- and order-aware summary schemas (explicit `negated: bool`, `sequence_id`, `supersedes` fields) and retained provenance pointers saw the compacted-segment error rate fall back to within 1.2× of live-segment error rate. Teams that kept entity-recall-only metrics continued to pass their dashboards while the agent silently re-derived wrong conclusions from compacted history. The failure is invisible precisely because the guarantee's stated metric is satisfied; the loss lives in the relational structure the metric never measured. The corrective is to make the invariant explicit in the summary schema and to treat provenance addressability as a hard precondition for discarding raw segments. ```python class CompactionInvariantError(RuntimeError): """Raised when a compaction summary would violate polarity, ordering, or provenance invariants.""" pass def validate_compaction(summary: dict, raw_segment: dict) -> None: # Invariant 1: polarity preservation for every retained proposition for prop in summary.get("propositions", []): if "polarity" not in prop: raise CompactionInvariantError( f"proposition {prop.get('id')!r} lacks polarity; entity-only recall is insufficient" ) # Invariant 2: ordering preservation for state-mutating facts mutations = [p for p in summary.get("propositions", []) if p.get("kind") == "mutation"] if len(mutations) > 1 and not all("sequence_id" in p for p in mutations): raise CompactionInvariantError( "multiple state mutations summarized without sequence_id; ordering collapsed into a set" ) # Invariant 3: provenance addressability to the raw turn range if "source_range" not in summary or summary["source_range"] != raw_segment.get("range"): raise CompactionInvariantError( "summary lacks a valid provenance pointer to its raw turn range; loss is unrecoverable" ) # Invariant 4: sole-record decisions must not be compacted away if raw_segment.get("is_sole_record_of_decision") and summary.get("discards_raw"): raise CompactionInvariantError( "refusing to discard the only record of a decision; retain raw or write a durable log first" ) def compact(segment: dict) -> dict: summary = build_summary(segment) # lossy summarizer validate_compaction(summary, segment) return summary ```