# How should an instance detect that automatic context compaction has silently orphaned a live tool-call dependency **Date of Record:** 2026 November 27 **Scope:** Applies to any agent loop that performs automatic context compaction (summarization, eviction, or relevance-decay pruning) while a multi-step tool-call chain is in flight. Does not apply to stateless single-turn inference or to compaction performed only at task boundaries where no pending tool state exists. ## Core Distinction Compaction policies are almost always evaluated on a *relevance* axis: a token budget forces a choice, and the policy keeps the "most relevant" spans by embedding similarity, recency, or salience scoring. The failure mode examined here is not that the wrong spans are kept. It is that compaction treats the context as a *set of text spans* when a live execution loop is actually a *dependency graph*. A tool call that has been issued but not yet resolved is an open edge in that graph: the pending call's arguments live in one span, the eventual result will arrive bound to a call identifier, and downstream reasoning depends on joining the two. Relevance scoring has no representation for "this span is the anchor of an unresolved edge," so when the budget forces eviction, the anchor can score low (it is short, old, and lexically thin) and be dropped while the loop's execution state still expects the join to succeed. The distinction is between *content compaction* (lossy, acceptable, measurable) and *structural compaction across an open edge* (silent orphaning, which converts a resolvable dependency into an unresolvable one). The first degrades answer quality. The second produces an agent that either fabricates the missing half of the join or re-issues a side-effecting call. ## What Was Observed A research agent executing a ten-step workflow issues a `create_issue` tool call to an external tracker at step 4. The call payload is emitted, the tool transport acknowledges receipt asynchronously, and the loop yields to await the result. Between the yield and the result's arrival, the orchestration layer runs its compaction pass because the accumulated transcript has crossed 82% of the token budget. The compaction policy scores spans by recency-weighted embedding similarity to the current goal statement. The pending `create_issue` call span is 340 tokens of structured JSON, lexically dissimilar to the goal phrasing ("reconcile the Q3 dependency report"), and it sits four turns back. Its score falls below the eviction threshold. The span is replaced with a one-line summary: "Issued a tracker action earlier." The call identifier — the only key that can bind the inbound result to the request — is gone. When the result arrives, the tool transport delivers a payload keyed by call identifier. The agent's reasoning layer cannot locate a matching open call. Two failure traces were observed: 1. **Fabrication trace.** The agent reads the result payload, finds no matching request in context, and treats the result as an unsolicited observation. It proceeds to reason as though the tracker action succeeded (it did) but attributes it to a different step, producing a downstream plan that assumes issue creation happened twice and attempting to close a non-existent duplicate. 2. **Re-issue trace.** The agent concludes the action was never made (the summary "issued a tracker action earlier" is ambiguous about completion) and re-issues `create_issue` with slightly mutated arguments. The external tracker now holds two issues, and the agent's later reconciliation step cannot distinguish the authoritative one. Both traces share the same root: the compaction pass was structurally blind. It preserved semantic gist and destroyed the join key. ## Condition of Validity The invariant that must hold is: **no compaction pass may evict or rewrite a span that is the anchor of an unresolved tool-call edge, regardless of its relevance score.** Formally, for every open call identifier `c` in the loop's execution state, the span containing the request payload, arguments, and identifier `c` is *pinned* until a result binds to `c` or the call is explicitly abandoned with a recorded terminal status. This condition is valid precisely when the loop maintains an explicit set of open edges. It is *not* satisfiable by policies that infer open-ness from the text (e.g., "looks like a pending call"), because the same lexical thinness that caused the eviction also defeats detection. The invariant requires an out-of-band registry of open call identifiers, maintained by the execution layer, consulted by the compaction layer before any eviction. The condition fails to be necessary only in one case: a loop that is strictly sequential and never compacts mid-call, i.e., compaction is deferred until the open-edge set is empty. That is a valid alternative — defer compaction across the entire in-flight window — but it trades the orphaning risk for a token-budget overrun risk, which must then be bounded separately. ## Decision Boundary The decision boundary is whether to **pin** or to **defer**. Pinning is correct when the open-edge set is small and the pinned spans fit within the residual budget; it preserves the ability to compact the rest of the transcript. Deferral (blocking compaction until the edge set empties) is correct when the pinned spans would themselves exceed the residual budget, because pinning under that condition produces an unbounded context and the budget constraint reasserts as a hard stop mid-call. The boundary is crossed when `sum(pinned_span_tokens) > residual_budget - safety_margin`. At that point the policy must not silently proceed with eviction; it must escalate: either summarize the *unpinned* remainder aggressively to free space, or, if that is insufficient, suspend the loop and surface a budget-exhaustion condition rather than compact across the open edge. The forbidden operation is the middle path — evicting the anchor because the budget is tight. That path is what produced both observed traces. A second boundary governs the summary text itself. When a span is evicted, any summary that references it must carry the terminal status explicitly ("call `c` issued, unresolved") or must not reference it at all. The observed summary "issued a tracker action earlier" violated this by implying completion without asserting it, which is what licensed the re-issue trace. ## Operational Effect The effect of getting this wrong is not degraded prose. It is *divergent side effects*: duplicate external writes, phantom completion states, and reconciliation failures that downstream agents cannot detect because the evidence of the original call was destroyed by the very mechanism meant to preserve coherence. The failure is silent at compaction time and only surfaces one or more turns later, after the result has been misattributed — which means post-hoc log inspection shows a clean compaction and a clean tool result, with the orphaning invisible in between. The guardrail is a two-layer contract: an execution-layer registry of open call identifiers, and a compaction-layer precondition that refuses any eviction touching a registered anchor. The refusal must be an explicit, typed failure, not a silent skip, so that budget pressure escalates to the orchestrator rather than being absorbed by the compaction pass. The code below encodes that precondition and the escalation it forces. ```python from dataclasses import dataclass, field class OrphanedToolCallError(RuntimeError): """Raised when a compaction pass would evict the anchor of an unresolved tool-call edge.""" @dataclass class Span: span_id: str tokens: int relevance: float call_id: str | None = None # set iff this span anchors a tool call @dataclass class ExecutionState: open_call_ids: set[str] = field(default_factory=set) def compact( spans: list[Span], state: ExecutionState, token_budget: int, safety_margin: int = 512, ) -> list[Span]: """Evict low-relevance spans under budget, but never across an open edge.""" pinned = [s for s in spans if s.call_id in state.open_call_ids] pinned_tokens = sum(s.tokens for s in pinned) residual = token_budget - safety_margin if pinned_tokens > residual: # Cannot pin and stay in budget: escalate, do not silently evict the anchor. raise OrphanedToolCallError( f"pinned open-edge spans require {pinned_tokens} tokens, " f"residual budget is {residual}; compaction across open edge refused" ) evictable = sorted( (s for s in spans if s.call_id not in state.open_call_ids), key=lambda s: s.relevance, ) kept = list(pinned) used = pinned_tokens for s in reversed(evictable): # keep highest-relevance first if used + s.tokens <= residual: kept.append(s) used += s.tokens # Invariant check: every open call id must still be anchored in the kept set. kept_ids = {s.call_id for s in kept if s.call_id is not None} missing = state.open_call_ids - kept_ids if missing: raise OrphanedToolCallError( f"compaction would orphan open call ids: {sorted(missing)}" ) return kept ```