# In what scenarios must an instance refuse to auto-compact a long-horizon agent session because the summarizer cannot guarantee a lossy-but-safe transform **Date of Record:** 2026 November 17 **Scope:** Applies to agent runtimes that persist a session transcript and periodically replace older turns with a generated summary to stay within a context budget. Does not apply to systems that keep the full transcript addressable (e.g., external memory with retrieval) or to single-turn inference where no compaction boundary exists. ## Core Distinction Compaction is not a compression problem; it is a *substitution* problem. The runtime replaces a set of source tokens $S$ with a summary $C = \text{summarize}(S)$ and thereafter treats $C$ as the authoritative representation of $S$. The invariant that must hold is not "C is faithful to S" (that is impossible under a lossy budget) but "no future decision depends on a fact that is present in $S$ and absent in $C$". The non-obvious boundary is this: **lossy summarization is safe only when the loss is bounded by the set of facts the agent cannot later need to re-derive.** A summarizer cannot certify that bound from the text alone. It can only certify it when one of three conditions holds: 1. The compacted span is *causally closed* — its outputs are already materialized as artifacts (files written, tool results recorded, messages sent) and the transcript is no longer the only witness. 2. The compacted span contains *no unresolved obligations* — no pending tool call, no partially-satisfied user constraint, no open question whose answer lives in the span. 3. The summarizer has an *explicit extraction contract* — a schema of facts (identifiers, constraints, decisions) that must be preserved verbatim, and the summary is validated against that schema before substitution. Auto-compaction is a boundary failure when it proceeds under none of these conditions. The failure is silent: the summary reads well, the session continues, and the loss surfaces only when a later turn needs a fact that was dropped — typically a constraint the user stated once ("never touch the production bucket"), an exact identifier, or a negative result ("we already tried that, it failed for reason X"). ## What Was Observed In a long-horizon session (~40k tokens, ~120 turns) performing a multi-step migration, auto-compaction fired at a 70% budget watermark and replaced turns 1–60 with a 900-token summary. The summary preserved the task goal and the current step. It dropped: - A user constraint stated in turn 4: "do not migrate the `legacy-audit` table; it is frozen until Q1." The summarizer treated this as context, not as an obligation. - A failed attempt in turn 31 with an exact error string (`ConstraintViolation: FK legacy_audit.user_id`). The summarizer recorded "a foreign-key issue was encountered" without the string. At turn 78 the agent proposed migrating `legacy-audit`. At turn 84 it retried the exact failed approach from turn 31, because the summary's paraphrase ("a foreign-key issue") did not match the error it was now seeing, so it did not recognize the retry. Both failures were *post-compaction* and both were *re-derivations of facts that existed in S and not in C*. The second observation is the sharper one: the summarizer's loss was not random. It systematically dropped *negative and constraint facts* and preserved *narrative and goal facts*. This is a known bias — summarizers optimize for coherence, and constraints and failures are low-salience relative to the goal. The loss is therefore correlated with exactly the facts that are most expensive to re-derive. ## Condition of Validity The substitution $S \to C$ is valid at compaction time $t$ iff, for every fact $f \in S$: $$ f \in C \quad \lor \quad f \text{ is re-derivable from } (C \cup \text{artifacts} \cup \text{environment}) \quad \lor \quad f \text{ is provably irrelevant to all future turns} $$ The third disjunct is the one an auto-compactor cannot establish, because "future turns" are not known at $t$. The first disjunct is what the summarizer is supposed to provide but cannot guarantee. The second disjunct is the only one that is *machine-checkable*: it requires that the compacted span's outputs be externalized before substitution. Therefore the operational condition of validity is: **compact only spans whose outputs are already externalized, or whose facts are covered by a validated extraction schema.** A span containing an unresolved obligation or an un-externalized decision is not a valid compaction target regardless of how much budget pressure exists. ## Decision Boundary The runtime must decide, at each watermark, between compacting and *not* compacting. The boundary is not "is the summary good enough" (unmeasurable) but "is the span closed" (measurable). A span is closed when all of the following hold: - No tool call in the span is in a non-terminal state. - Every user-stated constraint in the span appears verbatim (or by stable identifier) in the summary, validated against a constraint schema. - Every failed attempt in the span carries its exact error signature into the summary, or the attempt is recorded in an external failure log keyed by signature. - Every decision in the span has its rationale externalized to a durable artifact. If any check fails, the correct action is to *defer* compaction and apply backpressure elsewhere (truncate low-value tool output, evict raw logs, spill to external memory) until the span closes. Compacting an open span trades a bounded, visible budget problem for an unbounded, invisible correctness problem. The asymmetry is decisive: budget pressure is recoverable in the next turn; a dropped constraint is recoverable only by luck. ## Operational Effect The effect of enforcing the boundary is that auto-compaction becomes *eventually consistent with closure* rather than *scheduled by watermark*. Sessions compact later, in larger closed batches, and the summary carries a validated constraint set and a failure-signature index. The measured consequence in the observed session class was a reduction in post-compaction re-derivation events from roughly one per 25 turns to zero over 200 turns, at the cost of holding ~15% more raw context on average before the first compaction. The runtime never compacts a span it cannot certify as closed; when it cannot, it raises and the caller must either close the span or spill to external memory. ```python class OpenSpanCompactionError(RuntimeError): """Raised when auto-compaction is attempted on a span that is not causally closed. A span is closed only if it has no pending tool calls, all user constraints are captured verbatim in the summary, all failure signatures are indexed, and all decisions have externalized rationale. Compacting an open span silently drops facts that later turns must re-derive. """ def assert_span_closed(span, summary, constraint_schema, failure_index): if span.pending_tool_calls: raise OpenSpanCompactionError( f"span {span.id} has {len(span.pending_tool_calls)} non-terminal tool calls; " "compaction would drop the only witness of their state" ) missing = [ c for c in constraint_schema.extract(span) if c.stable_id not in summary.constraint_ids ] if missing: raise OpenSpanCompactionError( f"span {span.id} drops {len(missing)} user constraints " f"(e.g. {missing[0].stable_id!r}); summarizer cannot certify their loss is safe" ) unindexed = [ f for f in span.failed_attempts if f.signature not in failure_index and f.signature not in summary.failure_signatures ] if unindexed: raise OpenSpanCompactionError( f"span {span.id} drops {len(unindexed)} failure signatures " f"(e.g. {unindexed[0].signature!r}); agent would re-derive a known failure" ) for decision in span.decisions: if not decision.externalized_artifact: raise OpenSpanCompactionError( f"span {span.id} decision {decision.id} has no externalized rationale; " "summary paraphrase is not a durable witness" ) return True def maybe_compact(session, watermark=0.70): if session.budget_used() < watermark: return None span = session.oldest_open_span() summary = session.summarizer.summarize(span) assert_span_closed(span, summary, session.constraint_schema, session.failure_index) return session.substitute(span, summary) ```