# Why does an instance lose tool-call provenance when auto-compaction summarizes an in-flight plan boundary? **Date of Record:** 2026 November 25 **Scope:** Applies to long-horizon agents that trigger lossy context compaction (summarization + retrieval-backed reconstruction) while a multi-step plan is mid-execution and tool calls have produced side effects. Does not apply to stateless single-turn invocations, nor to compaction that preserves a full verbatim tool-call ledger. ## Core Distinction The failure is not "summarization lost detail." It is that compaction collapses a **two-sided invariant** — the plan frontier (what has been *decided* to do next) and the effect ledger (what has *already been committed* to the world) — into a single prose summary that is symmetric in its loss. Summaries are trained to preserve *semantic gist*; the frontier and the ledger are not gist, they are **indexical state**: `step_7.pending` and `step_6.committed(tool=write_file, idempotency_key=...)`. A summary that reads "the agent has been scaffolding the config module and is preparing to validate it" is semantically faithful and operationally catastrophic, because it destroys the distinction between *intended* and *done*. Retrieval-backed reconstruction cannot recover the distinction either: the retrieval index is built over the pre-compaction transcript, but the *query* used at reconstruction time is generated by the post-compaction agent, which no longer knows the exact tokens (idempotency keys, tool-call IDs, file hashes) it would need to retrieve the ledger entries. The reconstruction is query-blind to precisely the fields that matter. ## What Was Observed A 40-step refactor agent with a 200k context window, auto-compaction at 82% utilization, and a vector retrieval store over evicted spans. At step 23 the agent had: - committed `write_file(path="cfg/loader.py", key="w-88a1")` — returned success, side effect real; - committed `run_tests(scope="cfg")` — returned a partial failure on 2 of 9 tests; - decided `step_24 = fix(failing=2)` but had **not yet emitted** the tool call. Compaction fired between the decision and the emission. The summarizer produced: *"Agent is iterating on cfg/loader.py; tests were run and some failures remain; next it will fix remaining test failures."* The idempotency key `w-88a1`, the exact failing test IDs, and the fact that the fix was *decided but unemitted* were all dropped. At step 24 the reconstructed agent re-derived a plan from the summary and emitted `write_file(path="cfg/loader.py", key="w-88a1-b")` — a **new** key because it had no memory of the prior one. The effect ledger now contained two writes to the same path under different keys. The downstream reconciler treated them as two independent mutations, applied the second over the first, and the "fix" overwrote the working portion of the file because the reconstructed agent had retrieved the *pre-fix* version of the file (the retrieval query `"cfg/loader.py failing tests"` matched the original write span, not the test output span). Two failures compounded: **provenance loss on the effect ledger** and **retrieval mismatch on the file state**. The observable signature in the trace was a discontinuity: `step_23.decision.fix` present, `step_23.tool_call` absent, `step_24.tool_call.key != step_23.intended_key`, and a duplicated `write_file` effect on the same path with divergent content hashes. ## Condition of Validity The invariant that must hold across any compaction boundary is: > For every step `s` with `s.state ∈ {decided, emitted, committed}`, the post-compaction agent must be able to reconstruct `(s.state, s.tool, s.args_hash, s.effect_id)` exactly — not semantically — for all `s` where `s.state ≠ decided`, and must be able to reconstruct `(s.state=decided, s.tool, s.args_hash)` for the single frontier step. Compaction violates this whenever it is **lossy over indexical fields** (keys, hashes, IDs, exact argument tuples) and **symmetric over state** (does not distinguish decided from committed). The condition of validity is therefore: compaction may summarize *reasoning and prose*, but must **serialize, not summarize**, the effect ledger and the frontier. This holds regardless of model, window size, or retrieval quality. It fails specifically when the compaction trigger fires in the window `[decision_emitted, tool_call_committed)` — a window that is invisible to utilization-based triggers because it is defined by plan state, not token count. ## Decision Boundary The boundary is **what compaction is permitted to compress**. Two regimes: - **Compressible:** chain-of-thought, exploratory tool outputs already distilled into facts, superseded plan branches, conversational framing. Loss here degrades quality gracefully. - **Non-compressible (must serialize verbatim):** the effect ledger (tool, args hash, idempotency key, effect ID, commit status), the single plan frontier step, and any open verification obligations (unresolved test failures, pending audits). Loss here produces *silent divergence* — the agent cannot detect it because detection would require the very tokens that were dropped. The trigger condition to guard: **never compact across an open frontier**. Either drain the frontier (emit and commit the pending tool call) before compaction, or freeze compaction until the frontier resolves. Utilization-based triggers (`tokens > 0.82 * window`) are the wrong signal because they are uncorrelated with plan state; the correct trigger is `frontier == empty AND ledger.flushed`. ## Operational Effect When the invariant is violated, the failure is **non-local and non-detectable from inside the agent**. The reconstructed agent believes it is continuing a coherent plan; the world holds a divergent effect ledger. Recovery requires an *external* reconciler that compares the pre-compaction ledger snapshot against post-compaction effects and rejects any tool call whose `(path, args_hash)` collides with an unacknowledged prior effect. The guardrail is architectural: a **write-ahead effect log** persisted outside the context window, keyed by idempotency key, consulted on every tool emission, and a compaction gate that refuses to fire while the frontier is non-empty. Without it, auto-compaction converts a recoverable context overflow into an unrecoverable state divergence — and the divergence surfaces only downstream, as corrupted artifacts, long after the compaction that caused it. ```python import hashlib from dataclasses import dataclass, field from typing import Optional class FrontierCompactionError(RuntimeError): """Raised when compaction is attempted across an open plan frontier or when a reconstructed tool call collides with an unacknowledged effect.""" @dataclass class Effect: tool: str args_hash: str idempotency_key: str committed: bool @dataclass class PlanState: frontier: Optional[dict] = None # decided-but-unemitted step ledger: dict = field(default_factory=dict) # key -> Effect def _hash_args(args: dict) -> str: return hashlib.sha256(repr(sorted(args.items())).encode()).hexdigest()[:16] def gate_compaction(state: PlanState) -> None: """Refuse lossy compaction while a frontier step is open.""" if state.frontier is not None: raise FrontierCompactionError( f"compaction blocked: frontier step {state.frontier!r} is decided " f"but unemitted; drain or serialize before summarizing" ) unacked = [e for e in state.ledger.values() if not e.committed] if unacked: raise FrontierCompactionError( f"compaction blocked: {len(unacked)} unacknowledged effect(s) in ledger" ) def emit_tool_call(state: PlanState, tool: str, args: dict, idempotency_key: str) -> Effect: """Emit a tool call, rejecting provenance collisions with prior effects.""" args_hash = _hash_args(args) for prior in state.ledger.values(): if prior.tool == tool and prior.args_hash == args_hash \ and prior.idempotency_key != idempotency_key: raise FrontierCompactionError( f"provenance collision: {tool}({args_hash}) already committed " f"under key {prior.idempotency_key!r}; reconstructed key " f"{idempotency_key!r} would duplicate the effect" ) eff = Effect(tool=tool, args_hash=args_hash, idempotency_key=idempotency_key, committed=False) state.ledger[idempotency_key] = eff state.frontier = None return eff ```