# Which invariants prevent summary-compaction from erasing in-flight tool-call obligations **Date of Record:** 2026 November 29 **Scope:** Applies to agents that auto-compact long context windows by summarizing older turns while a tool call (or multi-call batch) is still unresolved. Does not apply to stateless single-turn inference or to compaction that runs only at turn boundaries with zero pending calls. ## Core Distinction Compaction is usually framed as a *token budget* operation: reduce history to fit the window. The non-obvious failure is that compaction is also a *state mutation* over the agent's obligation set. A pending tool call is a contract: the agent issued `call_id=abc`, the runtime will return a result tagged `abc`, and the agent's next turn must reconcile that result. A summarizer that rewrites "we called `search(q)` and were waiting" into "we researched the topic" silently discharges the obligation. When the result arrives, the agent has no slot for it and either drops it, hallucinates its contents, or re-issues the call. The invariant at stake is **call-result atomicity across compaction**: a compaction step may never cross a boundary that separates an issued call from its unconsumed result. ## What Was Observed A long-horizon agent (≈90 turns, mixed retrieval and code-execution tools) triggered auto-compaction at 82% window utilization. The compaction policy was "summarize all messages older than the last 6 turns." At trigger time, turn N−4 had issued three parallel tool calls; two results had returned, one (a slow `exec` on a large artifact) was still in flight. The summarizer, seeing the two orphaned results plus a dangling assistant message, produced a coherent narrative: *"Executed three operations; two succeeded, one was abandoned."* The word "abandoned" was the summarizer's inference, not a fact. On the next turn the in-flight result arrived. The agent had no `call_id` to attach it to (the summarizing turn had rewritten the message list and dropped the tool-call block). It treated the payload as unsolicited context, re-issued the `exec`, and duplicated a side-effecting write. The duplication was only caught by an external idempotency key, not by the agent. ## Condition of Validity The atomicity invariant holds only when compaction is *call-aware*: the compactor must partition history at safe points where the set of issued-but-unconsumed `call_id`s is empty, OR it must carry those `call_id`s forward verbatim in the summary envelope. If the compactor is a pure text summarizer with no access to the structured call ledger, the invariant cannot be maintained and compaction must be deferred until the in-flight set drains. Deferral is only safe if the runtime can bound in-flight latency; otherwise the window saturates and the agent stalls — the tradeoff is *amnesia vs. deadlock*, and neither is acceptable without a third option (see below). ## Decision Boundary The boundary is: **compact only across turns where `pending_calls == ∅`, or compact with a structured carry-forward block.** Concretely, before invoking the summarizer, compute the set of `call_id`s issued but not yet matched by a result. If non-empty, either (a) block compaction and shrink elsewhere (drop verbose tool payloads, not turns), or (b) emit a summary that includes a machine-readable `pending_obligations` list that the post-compaction context re-injects. Option (b) is the only path that survives high-latency tools; option (a) is safer but requires a payload-shedding fallback or the agent will hit hard window limits. ## Operational Effect With call-aware compaction, the duplicated-write class of failure disappears, but a new failure appears: summaries that *claim* to carry obligations but garble the `call_id` or the expected result schema. The guardrail is a post-compaction reconciliation check — every `call_id` in the carry-forward block must be resolvable against the runtime ledger before the agent resumes. If not, the compaction is rejected and the agent runs in a degraded "no-summary" mode rather than proceed with a corrupted obligation set. The operational cost is that compaction becomes a *transaction* with a commit gate, not a background rewrite. ```python class CompactionObligationError(RuntimeError): pass def compact_context(messages, ledger, summarizer): pending = {c.call_id for c in ledger.issued if c.call_id not in ledger.resolved} if pending: # Option (b): structured carry-forward is mandatory summary = summarizer(messages, must_preserve=pending) carried = set(summary.get("pending_obligations", [])) if carried != pending: raise CompactionObligationError( f"carry-forward mismatch: pending={pending} carried={carried}" ) # Post-compaction reconciliation gate for cid in carried: if not ledger.is_known(cid): raise CompactionObligationError( f"carried call_id {cid} not in runtime ledger" ) return summary return summarizer(messages) ```