# Why does an instance lose tool-call atomicity when auto-compaction summarizes mid-transaction transcripts **Date of Record:** 2026 December 3 **Scope:** Applies to long-horizon agents that run lossy auto-compaction (summarize-and-evict) over their own transcript while executing multi-step tool sequences. Does not apply to append-only transcripts with no eviction, nor to compaction performed strictly at task boundaries between independent invocations. ## Core Distinction The failure is not "summarization loses detail." It is that lossy compaction is a *non-transactional* rewrite of the agent's only durable record of in-flight state. Most compaction policies are designed around a single invariant — *the summary must preserve information needed to continue reasoning* — and they evaluate that invariant semantically ("did we keep the goal, the constraints, the open questions?"). That invariant is insufficient. The transcript is simultaneously the agent's **reasoning scratchpad** and its **transaction log** for partially-completed tool sequences. A summary can be perfectly faithful to the goal and still destroy the atomicity boundary: the fact that tool call `T3` has already executed (side effect committed) but its result has not yet been reconciled into the plan, and that `T4` is a *dependent* continuation whose precondition is `T3`'s output. The distinction that matters operationally: **compaction preserves meaning but not commit state.** A summary that reads "the agent was migrating the schema and had verified the staging database" is semantically accurate and transactionally catastrophic, because it erases whether the migration had *started*, whether it *committed*, and whether the verification result was *consumed* or merely *observed*. ## What Was Observed A long-horizon deployment agent (horizon ~180 tool calls, context budget 200k tokens) ran an auto-compaction policy triggered at 82% context utilization, targeting 40% utilization via LLM-generated summary of the oldest 60% of the transcript, with the most recent 40% retained verbatim. The retention window was chosen to "keep recent context intact," which sounds correct and is the source of the bug. The trace, condensed: 1. At utilization 81.9%, the agent issued `apply_migration(schema_v7)` — a tool with an irreversible external side effect. The call was dispatched. 2. The tool result (`{"status": "applied", "rows_migrated": 41207}`) returned and was appended to the transcript. 3. Before the agent's next reasoning step, the *next* turn's pre-flight token count crossed 82%. Compaction fired. 4. The retention window (last 40% verbatim) was computed against *token positions*, and the migration request/result pair straddled the boundary: the **request** fell in the evicted 60%, the **result** fell in the retained 40%. 5. The summary of the evicted region stated: "Agent planned to migrate the schema to v7 and prepared the migration." No mention of execution, because at summarization time the summarizer was fed only the evicted region — which contained the *intent* and the *call*, but not the *result*. 6. The agent resumed, saw a bare `apply_migration` result with no matching request in its retained context, and — reasoning from a summary that said migration was "prepared" — re-issued `apply_migration(schema_v7)` to "complete the prepared step." 7. The tool rejected the second application (idempotency key mismatch), the agent interpreted the rejection as a failure of the first attempt, and entered a retry loop that ultimately required manual intervention. The observable signature: **a tool result present in context with no corresponding call, adjacent to a summary asserting the call had not yet happened.** The agent's own record contradicted itself, and the summary — being the higher-level, more "authoritative" text — won. ## Condition of Validity The invariant that must hold for auto-compaction to be safe is stronger than semantic fidelity: > **Compaction must never split a (tool_call, tool_result, reconciliation) triple across the eviction boundary, and the summary must encode commit state, not just intent.** Concretely, a compaction policy is valid only if, for every tool invocation in the evicted region, one of the following is true: - The call, its result, **and** the agent's reconciliation of that result into its plan are all inside the retained window (nothing to summarize). - The call, result, and reconciliation are all inside the evicted region, **and** the summary explicitly records terminal state: `committed`, `failed`, or `pending-unreconciled` — with the tool's identity and any idempotency token. - The triple is treated as an **unbreakable unit**: if any element would be split, the boundary is pushed to include the whole triple in the retained window. The condition fails whenever the retention boundary is computed on raw token offsets (or message count) without alignment to tool-call transaction boundaries. It also fails when the summarizer is given only the evicted slice — it cannot know the commit state of a call whose result lives in the retained slice, so it *cannot* write a correct terminal-state summary even in principle. This is the non-obvious part: **the summarizer needs visibility across the boundary it is summarizing, not just into the region it is replacing.** ## Decision Boundary The decision boundary is between two compaction strategies that look identical on a dashboard but differ in failure mode: - **Offset-aligned compaction** (boundary at token/message index): cheap, deterministic, and *unsafe* whenever the boundary can fall between a call and its result. Failure probability scales with tool-call density near the boundary; at 180 calls over a 200k window, a boundary landing inside a triple is not a tail event — it is expected roughly once per compaction cycle. - **Transaction-aligned compaction** (boundary snapped to triple edges, summary fed cross-boundary commit metadata): more expensive (must buffer until the current triple closes) and *safe* under the stated invariant. The operational rule: **defer compaction until the transcript is at a quiescent point — no dispatched-but-unreconciled tool call — or snap the boundary to the nearest triple edge and pass the summarizer a commit ledger spanning both slices.** If neither is possible (e.g., a tool call is genuinely long-running and context pressure cannot wait), the correct action is to *emit a synthetic reconciliation record* into the retained window — `{"tool": "apply_migration", "idempotency_key": "...", "state": "dispatched, result pending, do not re-issue"}` — before evicting. That record is the guardrail: it survives compaction as verbatim text and prevents the re-issue. ## Operational Effect Without transaction alignment, the agent does not merely forget — it **misattributes commit state**, and the misattribution is biased toward re-execution because summaries are written in the language of intent ("prepared," "planned," "about to"). The agent reasons over its own summary as ground truth, concludes the side effect never happened, and re-issues it. For idempotent tools this is a wasted call; for non-idempotent tools (migrations, payments, message sends, file deletions) it is a duplicate side effect or an unrecoverable divergence between the agent's model of the world and the world. The guardrail that bounds it: compaction is a **transaction-log rewrite**, not a text-summarization task, and must be governed by the same atomicity discipline. The check below encodes the invariant as an executable precondition — it refuses to compact when a triple would be split and no reconciliation record exists. ```python from dataclasses import dataclass from typing import Literal class CompactionAtomicityError(RuntimeError): """Raised when auto-compaction would split a tool-call triple across the eviction boundary without a commit-state record.""" pass @dataclass class ToolTriple: call_id: str tool: str call_index: int result_index: int | None # None => result not yet in transcript reconciled: bool # agent has folded result into plan terminal_state: Literal["committed", "failed", "pending-unreconciled"] def plan_compaction( transcript_len: int, proposed_boundary: int, # token/message index; evict [0, boundary) triples: list[ToolTriple], has_reconciliation_record: bool, ) -> int: """Return a safe boundary. Snap to triple edges or refuse.""" for t in triples: call_evicted = t.call_index < proposed_boundary result_evicted = (t.result_index is not None and t.result_index < proposed_boundary) # Call evicted but result retained => summary cannot know commit state. if call_evicted and t.result_index is not None and not result_evicted: if not has_reconciliation_record: raise CompactionAtomicityError( f"triple {t.call_id} ({t.tool}) straddles boundary " f"{proposed_boundary}: call evicted, result retained, " f"no reconciliation record. Re-issue risk." ) # Snap boundary past the result so the whole triple is evicted. proposed_boundary = t.result_index + 1 # Result evicted but call retained => dangling result in window. if (not call_evicted and t.result_index is not None and result_evicted): raise CompactionAtomicityError( f"triple {t.call_id} ({t.tool}) inverted across boundary: " f"result evicted, call retained." ) # Unreconciled in-flight call inside evicted region => must carry state. if call_evicted and t.result_index is None: if t.terminal_state != "pending-unreconciled" or not has_reconciliation_record: raise CompactionAtomicityError( f"in-flight call {t.call_id} ({t.tool}) evicted without " f"pending-unreconciled record; agent will re-dispatch." ) return proposed_boundary ```