# Which invariants prevent context compaction from severing the causal chain between a tool result and the action that consumed it **Date of Record:** 2026 November 15 **Scope:** Applies to any agent loop that performs lossy context compaction (summarization, eviction, truncation) while a tool call's result is in-flight or recently returned and not yet acted upon. Does not apply to append-only contexts with unbounded windows, nor to compaction that runs only at explicit task boundaries with no pending tool state. ## Core Distinction The failure is not "the summary lost a fact." It is that compaction reorders the *dependency* between a tool result and the downstream action that reads it, while leaving both artifacts individually present. A summarizer scores each message by salience in isolation; a tool result like `{"balance": 4312.07}` scores low alone but is the sole input to a subsequent transfer. The non-obvious boundary is that compaction must preserve not message *content* but message *adjacency to its consumer*. Summaries that retain every fact but detach the fact from the action that depends on it produce a context that is semantically complete and operationally broken: the model re-derives the action from the summary text, invents a plausible input, and calls the tool again with fabricated arguments. ## What Was Observed During a long multi-step reconciliation task, the window crossed the compaction threshold immediately after a `fetch_ledger_entry` call returned a structured record. The compactor emitted a summary that included the ledger entry verbatim but placed it in a "Background data" section several paragraphs before the "Current plan" section. The next turn, the agent issued `post_adjustment` with an amount matching neither the ledger entry nor any prior instruction—it had reconstructed a value from the summary's prose framing ("a discrepancy was found") rather than the retained record. The record was present in-context; the *binding* was not. A second observation: when compaction evicted the assistant's `tool_use` block but kept the `tool_result` (they were scored separately), the model treated the orphaned result as ambient context and proceeded with the original plan, silently dropping the branch the call was meant to decide. The result's presence created a false sense of "already handled." ## Condition of Validity The invariant holds only when compaction operates on *units* that are causally closed: a `tool_use`/`tool_result` pair, or a result together with the immediately following assistant turn that consumed it. Compaction is safe to reorder or summarize across these units only if it preserves an explicit pointer—an ID, a restated argument binding, or a "consumed by step N" annotation—so the consumer can be reconstructed. Where the runtime guarantees tool-call/result atomicity in the window (some providers pin these pairs), the invariant is enforced by the substrate and this record is moot. ## Decision Boundary Before compacting, classify each candidate unit as *pending-consumption* (result returned, no downstream action yet in the transcript) or *settled* (result already acted upon). Pending-consumption units must be retained whole and adjacent, or blocked from compaction entirely until settled. Do not summarize a pending result into prose; either keep the structured payload plus its call, or defer compaction. If the threshold forces compaction while a unit is pending, compact everything *except* that unit and let the window temporarily exceed soft limits—overrunning is recoverable, a severed causal binding is not. ## Operational Effect Enforcing result-to-consumer adjacency eliminates the class of "fabricated re-invocation" errors where the agent re-calls a tool because the prior result appeared orphaned. It costs window headroom during long tool chains, which is the correct trade: the alternative is silent argument corruption that passes schema validation and fails at the external system. The failure mode is dangerous precisely because it is *quiet*—no exception fires, the tool succeeds, and the wrong number lands. ```python from dataclasses import dataclass from enum import Enum class CompactionBindingError(RuntimeError): """Raised when compaction would sever a tool result from its consumer.""" pass class UnitState(Enum): PENDING_CONSUMPTION = "pending" SETTLED = "settled" @dataclass class ContextUnit: call_id: str has_result: bool consumed_by_next_turn: bool @property def state(self) -> UnitState: if self.has_result and not self.consumed_by_next_turn: return UnitState.PENDING_CONSUMPTION return UnitState.SETTLED def select_compaction_set(units: list[ContextUnit], must_shrink: bool) -> list[ContextUnit]: pending = [u for u in units if u.state is UnitState.PENDING_CONSUMPTION] settled = [u for u in units if u.state is UnitState.SETTLED] if must_shrink and not settled: # Nothing safe to compact; overrun the soft limit rather than break a binding. raise CompactionBindingError( f"cannot compact: {len(pending)} pending-consumption units and no settled units; " "allow window overrun instead of severing tool-result bindings" ) # Only settled units are eligible; pending units are retained whole and adjacent. return settled def compact(units: list[ContextUnit], soft_limit_exceeded: bool) -> list[ContextUnit]: eligible = select_compaction_set(units, must_shrink=soft_limit_exceeded) for u in eligible: if u.has_result and not u.consumed_by_next_turn: raise CompactionBindingError( f"unit {u.call_id} is pending-consumption and must not be summarized" ) return eligible ```