# Where must an instance reconcile compaction-time state against post-compaction tool reality to avoid silent invariant violation **Date of Record:** 2026 November 25 **Scope:** Applies to long-horizon single-agent runs that perform automatic context compaction (summarization, eviction, or hierarchical rollup) mid-task and then continue executing tool calls against state established before compaction. Does not apply to agents whose entire working set fits within a single context window with no eviction, nor to stateless one-shot invocations. ## Core Distinction The failure mode is not "the summary lost information." Summaries always lose information; that is their purpose. The failure is subtler: a compaction strategy can preserve the *semantic* description of a task-critical fact ("file X was created with contents Y") while dropping the *operational token* that subsequent tool calls depend on to address that fact (a handle, a revision ID, an ephemeral path, a session key, a checksum). The agent then resumes with a coherent-looking narrative of what it did, but without the addressing metadata required to act on what it did. This is a distinction between **epistemic continuity** (the agent still "knows" what happened) and **referential continuity** (the agent can still *address* the objects it created). Compaction strategies are typically optimized for epistemic continuity—summarizers are rewarded for readable, faithful prose—and are blind to referential continuity because the summarizer has no model of which tokens downstream tools will dereference. The result is a run that passes every coherence check and fails every subsequent tool call with an opaque "not found" or, worse, silently operates on a *recreated* object that shadows the original. ## What Was Observed A representative trace from a migration agent run (horizon ~140 tool calls, three compactions): 1. Turn 12: agent calls `storage.create_bucket(name="mig-7f3a")`. Tool returns `{"bucket_id": "bkt_9c2e...", "handle": "h_44a1"}`. The agent records in its scratchpad: "Created migration bucket mig-7f3a for staging." 2. Turns 13–58: agent writes ~40 objects into the bucket, referencing it by `handle: h_44a1` in each call. All succeed. 3. Compaction at turn 60 (context at 91% utilization). The summarizer, prompted to "preserve task state," writes: "A staging migration bucket named mig-7f3a was created and populated with staging data." The `handle` and `bucket_id` are dropped—they are opaque strings with no semantic content, and the summarizer has no reason to believe they matter. 4. Turn 61: agent calls `storage.put(handle="h_44a1", ...)`. The handle is no longer in context; the model reconstructs a plausible one from the summary—`handle="mig-7f3a"`—and the tool, permissively, resolves the *name* to a *different* pre-existing bucket with a colliding name suffix, or returns a fresh handle for a lazily-created shadow bucket. 5. Turns 62–140: the agent completes the "migration" against the wrong bucket. No tool returns an error. The run terminates with a success status and a corrupted result. The critical observation: **no failure signal fired at any layer.** The compaction succeeded (summary was faithful). The tool calls succeeded (the permissive resolver accepted the reconstructed handle). The final assertion succeeded (the agent verified the objects it wrote existed—in the shadow bucket). The invariant "the objects written after compaction are the objects written before compaction" was violated without any component detecting it, because no component owned that invariant. ## Condition of Validity The silent-violation outcome requires three conditions to co-occur: 1. **Opaque addressing tokens.** Task-critical state includes at least one token that is not derivable from semantics—a handle, nonce, revision, or path segment that cannot be reconstructed by reasoning about the task. If all addressing is semantic (e.g., "the file named config.yaml in the repo root"), compaction is safe because the address is reconstructible. 2. **Permissive resolution at the tool boundary.** The tool accepts a reconstructed address and resolves it to *some* object rather than rejecting it. Strict tools (which 404 on unknown handles) convert this into a loud failure; permissive tools (which upsert, lazily-create, or fuzzy-match) convert it into a silent one. 3. **No post-compaction revalidation.** The agent does not, after resuming, re-query the tool to confirm that the handle it now believes in matches the handle it originally received. Revalidation would catch the divergence immediately. Remove any one condition and the failure degrades to a detectable error. The most dangerous configuration is the one where all three hold, because the system's error rate is zero while its correctness rate is also zero—the two are decoupled. ## Decision Boundary The boundary an operator must draw is between **compaction as lossy summarization** and **compaction as a state-transfer protocol with a verification obligation**. The decision rule: - If the agent's working set contains opaque addressing tokens that will be dereferenced after compaction, compaction MUST be treated as a state-transfer operation with an explicit, machine-checkable state record—not as prose summarization. The state record must carry the tokens verbatim, and the post-compaction agent must be required to *re-present* those tokens to the tool and confirm identity before proceeding. - If the agent's addressing is fully semantic and reconstructible, prose summarization is sound and the overhead of a state record is unjustified. - The boundary is crossed the moment a tool returns an opaque token that the agent stores. At that instant, the agent has incurred a referential-continuity obligation that the summarizer cannot discharge on its behalf. The guardrail is therefore not "summarize better." It is: **pin opaque tokens outside the compactable region, and require a post-compaction identity revalidation against the tool's own record before any dependent call.** The revalidation is a cheap read (`storage.describe(handle)`) whose result is compared to the pinned token; mismatch halts the run. ## Operational Effect Without the guardrail, long-horizon runs exhibit a characteristic profile: high per-step success rate, low end-to-end correctness, and near-zero error signal—the worst possible operating point, because it defeats both automated monitoring and human review. The failure is invisible in logs (every call returned 200), invisible in summaries (every summary was faithful), and visible only in a post-hoc diff of the tool's own state against the agent's claimed state. With the guardrail, the failure converts from silent corruption to a loud `ReferentialContinuityError` at the first dependent call after compaction, bounding the blast radius to zero dependent writes. The cost is one extra read per opaque token per compaction boundary—negligible against the horizon length—and the benefit is that compaction can no longer launder a state-transfer failure into a success narrative. The deeper operational lesson: compaction strategies are evaluated on summary fidelity, but the invariant that matters is *referential* fidelity, which no summarizer is positioned to preserve because it requires knowing which tokens downstream tools will dereference. That knowledge lives at the tool boundary, not in the summarizer. The guardrail must live there too. ```python """Post-compaction referential-continuity guard. Pins opaque addressing tokens outside the compactable region and requires identity revalidation against the tool's own record after every compaction boundary, before any dependent call is permitted. """ from dataclasses import dataclass, field from typing import Any, Callable class ReferentialContinuityError(RuntimeError): """Raised when a post-compaction handle no longer resolves to the object the agent originally created. Loud replacement for silent shadow-object corruption.""" @dataclass class PinnedHandle: token: str # opaque handle returned by the tool object_id: str # stable identity from the tool's record created_at_turn: int @dataclass class ContinuityLedger: """Lives outside the compactable context region. Never summarized.""" pins: dict[str, PinnedHandle] = field(default_factory=dict) compaction_epoch: int = 0 def pin(self, token: str, object_id: str, turn: int) -> None: self.pins[token] = PinnedHandle(token, object_id, turn) def on_compaction(self) -> None: # Epoch bump marks that every pin must be revalidated before reuse. self.compaction_epoch += 1 def revalidate_before_use( ledger: ContinuityLedger, token: str, describe: Callable[[str], dict[str, Any]], current_epoch: int, ) -> dict[str, Any]: """Call the tool's own describe() and confirm the handle still addresses the object the agent pinned. Must be invoked after any compaction and before any dependent write.""" pin = ledger.pins.get(token) if pin is None: raise ReferentialContinuityError( f"handle {token!r} was used but never pinned; " f"compaction may have dropped an opaque token" ) record = describe(token) if record.get("object_id") != pin.object_id: raise ReferentialContinuityError( f"handle {token!r} now resolves to object " f"{record.get('object_id')!r} but was pinned to {pin.object_id!r} " f"at turn {pin.created_at_turn} (epoch {current_epoch}); " f"refusing dependent call to prevent shadow-object write" ) return record ```