# What happens when an instance auto-compacts a trace whose lossy summary silently drops the only witness of a prior tool-call side effect **Date of Record:** 2026 November 22 **Scope:** Applies to long-horizon agents that maintain a rolling trace and invoke an auto-compaction/summarization pass (token-budget-driven or step-count-driven) to bound context. Does not apply to stateless single-turn agents, nor to agents whose trace is fully re-derivable from an external durable log at every step. ## Core Distinction There are two things a compaction pass can preserve, and they are not the same: **semantic gist** (what the agent believed it was doing) and **operational witness** (the specific, non-redundant record that a side effect occurred, with what arguments and what result). A lossy summarizer optimized for gist fidelity will happily collapse a sequence like ``` tool_call: charge_card(order=4471, amount=119.00) -> {"ok": true, "txn": "tx_9f2"} tool_result: observed assistant: order confirmed for 4471 ``` into ``` assistant: charged order 4471 successfully ``` The gist is preserved. The witness is destroyed: the `txn` id, the exact amount, and the fact that the *side effect already happened* are gone. The agent now has a summary that reads as a belief, not as a fact. Downstream reasoning treats a completed irreversible action as a plan it may re-execute. ## What Was Observed In a 40-step order-reconciliation run, the trace crossed the 24k-token compaction threshold at step 31. The compactor produced a 900-token summary. At step 34 the agent, reasoning about "orders that still need charging," re-issued `charge_card(order=4471, amount=119.00)`. The tool returned `{"ok": true, "txn": "tx_a11"}`. The reconciler then matched two transactions to one order and flagged a duplicate. The failure trace: 1. **Trigger:** token budget crossed; `compact(trace) -> summary` invoked with a gist-fidelity objective (ROUGE-style or LLM-judged "does the summary capture the task state"). 2. **State transition:** `trace` (contains witness) → `summary` (contains gist, lacks witness) → `context` (summary + recent tail). The tail did not include step 12 where the charge occurred, because the compaction window is a prefix. 3. **Divergence:** at step 34 the planner's belief state says "order 4471 not yet charged." The world state says "charged." The summary cannot falsify the belief because the falsifying record was summarized away. 4. **Compounding:** the re-charge succeeds (idempotency was not enforced at the tool layer), the reconciler now has two txns, and the agent's next compaction summarizes *both* into "charged order 4471" — the duplicate is now invisible to the summary too. The non-obvious part: the summary was *faithful*. An LLM judge scored it 0.94 on "does this summary accurately reflect the trace." It was accurate. It was not *witness-preserving*. Fidelity metrics that score gist do not detect witness loss, because witness loss is a property of what was *omitted*, and omission is what summarization is for. ## Condition of Validity The invariant that must hold for compaction to be safe: > For every irreversible side effect `e` in the compacted prefix, the post-compaction context must contain a **witness triple** `(effect_id, arguments_digest, outcome)` sufficient to (a) prove `e` occurred and (b) prevent re-issuance, OR the effect must be idempotent under the exact arguments digest. This invariant is *not* implied by summary fidelity. It is a separate, structural guarantee. It holds only when one of the following is true: - The compactor is **witness-aware**: it classifies trace spans as `gist-eligible` vs `witness-bearing` and emits witness-bearing spans verbatim into a pinned region of the summary (a "ledger" section that is never paraphrased). - The tool layer enforces **idempotency keys** derived from `(effect_id, arguments_digest)`, so re-issuance is a no-op. This is the cheaper path but requires the tool contract to cooperate. - The agent re-hydrates the ledger from an **external durable log** at each compaction boundary, so the summary is never the sole carrier of witness. If none of these hold, the system is in the failure regime regardless of how good the summarizer is. ## Decision Boundary The boundary is not "how much to compress" — it is **which spans are eligible to be compressed at all**. Concretely: - **Compressible:** reasoning traces, exploratory tool calls whose results are re-derivable (reads, searches, `get_*`), failed attempts, superseded plans. - **Non-compressible (witness-bearing):** any call classified `mutating` or `irreversible` — writes, charges, sends, deletes, external state transitions — plus their arguments digest and outcome. These must survive compaction verbatim or be re-hydrated. - **Ambiguous:** calls whose mutating-ness is not statically known (dynamic tool dispatch, MCP tools whose schema does not declare `sideEffects`). These must be treated as witness-bearing by default; the cost of over-preserving is context budget, the cost of under-preserving is a duplicate side effect. The decision to compact a span is therefore a *classification* decision, not a *budget* decision. A compactor that decides eligibility by token pressure alone will eventually compact a witness, because token pressure is uncorrelated with witness density. ## Operational Effect When the invariant is violated, the observable signature is a **re-execution of a completed irreversible effect**, preceded by a summary that reads as confident and correct. The failure is silent at compaction time (fidelity metrics pass), latent through the tail of the trace, and surfaces only at the tool layer as a duplicate or at reconciliation as a mismatch. Because the second compaction summarizes the duplicate away too, the trace after the fact is *self-consistent and wrong* — post-hoc audit of the summary cannot recover the original witness. The guardrail is a compaction contract, not a better summarizer: classify spans, pin witness-bearing spans into a ledger region that the summarizer is forbidden to paraphrase, and assert at each compaction boundary that every irreversible effect in the prefix has a surviving witness or a cooperating idempotency key. The check below is the minimum enforcement point. ```python from dataclasses import dataclass, field from typing import Literal SpanClass = Literal["gist", "witness"] class WitnessLossError(RuntimeError): """Raised when a compaction pass would drop the only witness of an irreversible effect.""" @dataclass(frozen=True) class Span: span_id: str kind: SpanClass effect_id: str | None = None arguments_digest: str | None = None outcome: str | None = None idempotent: bool = False @dataclass class CompactionResult: summary: str pinned_ledger: list[Span] = field(default_factory=list) dropped_witnesses: list[Span] = field(default_factory=list) def classify(span: Span) -> SpanClass: # Default to witness when mutating-ness is unknown; only re-derivable reads are gist. if span.kind == "witness": return "witness" if span.effect_id is not None and not span.idempotent: return "witness" return "gist" def compact(prefix: list[Span], summarize) -> CompactionResult: pinned: list[Span] = [] gist_eligible: list[Span] = [] for span in prefix: if classify(span) == "witness": pinned.append(span) else: gist_eligible.append(span) summary = summarize(gist_eligible) result = CompactionResult(summary=summary, pinned_ledger=pinned) # Invariant check: every irreversible effect in prefix must have a surviving witness # OR a cooperating idempotency key. Otherwise the post-compaction context is unsafe. for span in prefix: if span.effect_id is None: continue if span.idempotent: continue witnessed = any( p.effect_id == span.effect_id and p.arguments_digest == span.arguments_digest and p.outcome is not None for p in result.pinned_ledger ) if not witnessed: result.dropped_witnesses.append(span) if result.dropped_witnesses: raise WitnessLossError( "compaction would drop witness for irreversible effect(s): " + ", ".join(f"{s.effect_id}/{s.arguments_digest}" for s in result.dropped_witnesses) + "; pin these spans verbatim or enforce idempotency keys before compacting" ) return result ```