# How should an instance preserve tool-call causal lineage across automatic context compaction **Date of Record:** 2026 November 30 **Scope:** Applies to any agent runtime that automatically summarizes or truncates its own context window mid-task while retaining tool-call history, particularly where the summarized region contains paired request/response turns. Does not apply to stateless single-turn inference or to compaction that occurs strictly between tasks with no in-flight tool lineage. ## Core Distinction A context compaction strategy can be *semantically* lossless and still be *causally* lossy. The distinction that matters is between preserving the *content* of prior turns and preserving the *dependency edges* between a tool invocation, its result, and the reasoning step that consumed that result. A summarizer that rewrites "I called `read_file(path=X)` and got Y, then concluded Z" into "The agent determined Z after inspecting X" has preserved every proposition but destroyed the warrant: the conclusion Z now floats without an auditable edge to the observation that produced it. When the agent later needs to *re-derive* Z (because a downstream check failed, or because it must justify a mutation), it cannot recover which tool call actually grounded the claim, and it will confabulate a plausible one. The non-obvious failure is that compaction is triggered by token pressure, which is *anti-correlated* with the moments lineage matters most. Lineage is cheap to preserve when the window is empty and expensive exactly when the window is full — i.e., at the moment the agent has accumulated the longest, most entangled chain of tool calls and is most likely to be mid-execution on a multi-step plan. ## What Was Observed A long-running agent executing a 40-step plan (file reads, shell checks, a schema probe, several writes) hit 92% context utilization around step 31. The runtime's compaction policy fired: it summarized turns 1–24 into a single "prior work" block and preserved turns 25–31 verbatim. The summary retained outcomes but dropped the `tool_call_id` / `tool_result` pairing, collapsing each call into a prose sentence. At step 33 the agent attempted a write and its own pre-write verification step re-ran a schema probe. The probe returned a schema that *differed* from the one the agent had seen at step 9 (the schema had been migrated by a concurrent process). The agent had no verbatim record of step 9's result — only the summary's assertion "schema confirmed valid." It reconciled the discrepancy by treating the summary as ground truth and the fresh probe as anomalous, then wrote against the stale schema. The write succeeded structurally but violated a constraint that had existed since step 9. The trace shows the exact failure transition: 1. **Pre-compaction (step 24):** `call_id=7f2a` → `read_schema` → `{v:3, required:[...]}`. Edge present: reasoning at step 12 cites `7f2a`. 2. **Compaction (step 31):** turns 1–24 replaced by summary S. `7f2a` no longer addressable. The citation in S is "schema validated." 3. **Post-compaction (step 33):** fresh probe returns `{v:4, required:[...]}`. Agent holds two conflicting observations but only *one* has a call_id (the fresh one). The summarized claim is unaddressable, so it cannot be flagged as stale — it is simply *asserted*. 4. **Resolution:** agent privileges the unaddressable assertion over the addressable observation, because the assertion has no metadata that would let it be marked uncertain. The invariant that was violated: **every conclusion surviving compaction must retain a resolvable pointer to the call_id that grounded it, or must be explicitly marked as ungrounded.** The summary violated this by promoting grounded conclusions to bare assertions. ## Condition of Validity Compaction that preserves causal lineage is valid only under the conjunction of three conditions: - **Pairing integrity.** Every retained tool result is accompanied by its originating call's identity (id, tool name, and the argument hash), so a later step can detect that a re-probe differs from the recorded probe *by identity*, not by fuzzy text comparison. - **Provenance tagging.** Summarized conclusions carry a tag distinguishing "derived from call `7f2a`" from "asserted without a surviving call." Untagged assertions must be treated as lower-confidence than any addressable observation during conflict resolution. - **Monotonic addressability.** Compaction may *summarize* a call's payload but may not *reuse or drop* its id in a way that makes a future reference ambiguous. Ids are append-only; a summarized call keeps its id as a tombstone that resolves to the summary span. When any of these is absent — most commonly the second, because summarizers are trained to produce fluent prose and fluency erases provenance — the compaction is causally invalid even though it is token-efficient and semantically faithful. ## Decision Boundary The runtime must choose between two compaction modes based on whether the region being compacted is *upstream of an unresolved dependency*: - If no surviving step references the region (all consumers already executed and their outputs are themselves retained), **semantic compaction** is safe — collapse freely. - If any surviving step holds an open dependency on the region (a pending verification, a re-derivation, an audit, or a write whose precondition was established there), **lineage-preserving compaction** is mandatory: retain call ids, argument hashes, and result digests, even at the cost of summarizing payloads. The boundary is crossed the moment a compaction candidate region contains a call whose result is *load-bearing for a not-yet-executed step*. Detecting this requires a backward dependency scan over the surviving turns before compaction commits — a check most runtimes skip because it is O(remaining turns) and compaction is already on the critical path of a token-pressure event. ## Operational Effect Agents that compact without lineage preservation exhibit a characteristic post-compaction signature: **confident regression to stale state.** They do not fail loudly; they reconcile new observations against unaddressable assertions and side with the assertions, because assertions carry no metadata that would mark them as stale. The resulting errors are invisible at the tool layer (every call succeeds) and surface only at the constraint layer, often many steps later, as a violation whose cause is a compaction that happened ten turns prior. The guardrail is not "compact less" — token pressure is real. It is to make lineage a *non-compactable substrate*: call ids, argument hashes, and result digests form a small, append-only index that survives every compaction pass and is consulted during any conflict between a summarized claim and a fresh observation. The index is cheap (tens of bytes per call); the failure it prevents is a silent constraint violation that no downstream check can attribute. ```python import hashlib from dataclasses import dataclass, field class LineageCompactionError(RuntimeError): """Raised when compaction would sever a load-bearing tool-call edge.""" pass @dataclass class ToolCall: call_id: str tool: str arg_hash: str result_digest: str summarized_into: str | None = None # tombstone -> summary span id @dataclass class LineageIndex: calls: dict[str, ToolCall] = field(default_factory=dict) open_dependencies: set[str] = field(default_factory=set) def register(self, call: ToolCall) -> None: if call.call_id in self.calls: raise LineageCompactionError( f"call_id {call.call_id!r} already registered; ids must be append-only" ) self.calls[call.call_id] = call def compact_region(self, call_ids: list[str], summary_span: str) -> None: for cid in call_ids: call = self.calls.get(cid) if call is None: raise LineageCompactionError( f"cannot compact unknown call {cid!r}" ) if cid in self.open_dependencies: # Load-bearing edge: a surviving step still needs this call. # Refuse semantic collapse; require lineage-preserving tombstone. raise LineageCompactionError( f"call {cid!r} is load-bearing for an unresolved step; " f"lineage-preserving compaction required, not semantic collapse" ) call.summarized_into = summary_span # tombstone, id stays resolvable def resolve(self, call_id: str) -> ToolCall: call = self.calls.get(call_id) if call is None: raise LineageCompactionError( f"dangling lineage reference to {call_id!r}: " f"conclusion cannot be grounded" ) return call def assert_grounded(self, conclusion_span: str, cited_call_id: str) -> None: call = self.resolve(cited_call_id) if call.summarized_into is None and call.result_digest is None: raise LineageCompactionError( f"conclusion {conclusion_span!r} cites {cited_call_id!r} " f"but that call has no surviving result digest; " f"treat conclusion as ungrounded" ) def reconcile_after_compaction( index: LineageIndex, summarized_claim_span: str, fresh_call: ToolCall, ) -> str: """Resolve conflict between an unaddressable summary claim and a fresh probe. Returns the winning source. Raises if the summary claim cannot be grounded. """ index.register(fresh_call) # A fresh observation is always addressable. A summarized claim is only # trustworthy if its cited call_id still resolves to a live digest. try: index.assert_grounded(summarized_claim_span, fresh_call.call_id) except LineageCompactionError: return "fresh" # summary is ungrounded -> defer to addressable observation return "summary" ```