# Why does an instance re-enter a compacted trajectory and silently re-execute a tool call whose side effect it already committed? **Date of Record:** 2026 November 23 **Scope:** Applies to long-horizon agents that persist trajectory state and apply lossy auto-compaction (summarization, truncation-with-summary, or hierarchical rollup) at context-window thresholds. Does not apply to agents that retain full transcripts verbatim or that checkpoint only at explicit task boundaries with external durable memory. ## Core Distinction There are two distinct things an agent trajectory carries: **narrative state** (what the model believes happened, reconstructed from context) and **causal state** (what side effects were actually committed to external systems). Compaction preserves narrative state lossily—by design—while assuming causal state is recoverable elsewhere. The boundary failure occurs when the compaction boundary and the tool-call commit boundary are not co-located. A summary can drop a tool invocation's *record* while the side effect it produced remains live in the world. The next reasoning step then sees a trajectory that reads as "we were about to do X" rather than "we did X," and the agent re-issues X. The non-obvious part: this is not a retrieval miss or a truncation bug. It is a **guarantee mismatch**. Lossy summarization makes a promise about *semantic fidelity* ("the summary captures the salient arc") but the executor needs a promise about *idempotency and commit ordering* ("no committed action is ever re-derivable as pending"). Those are different invariants, and compaction strategies that satisfy the first routinely violate the second. ## What Was Observed A planner agent ran a 40+-step migration task. At step 22 the context crossed the compaction threshold (≈78% of window). The compactor summarized steps 1–18 into a paragraph, retained steps 19–22 verbatim as the "recent tail," and dropped step 17 entirely from the retained trace. Step 17 had been the actual `apply_migration(namespace="orders_v3")` call—a non-idempotent DDL operation that had succeeded and returned a migration token. The retained tail at step 19–22 contained the agent's *pre-call narration*: "Next I will apply the orders_v3 migration and verify the token." That narration survived because it was adjacent to the tail boundary. The call itself, and its success receipt, fell into the compacted range and were summarized as "…performed schema setup steps…" with no token, no receipt, and no committed/pending status. At step 24 the agent, reasoning over the compacted context, concluded the migration was still pending and re-issued `apply_migration(namespace="orders_v3")`. The tool's own idempotency guard was namespace-scoped by *exact* token, not by operation identity, so the second call created a duplicate migration branch. The agent then spent 11 steps reconciling a divergent schema state that its own summary had made invisible. The failure trace has three properties worth naming precisely: 1. **The commit was in the compacted range; the intent was in the retained tail.** Summarizers are trained to preserve recent and salient content, and "what we're about to do" is both. The asymmetry is systematic, not random. 2. **The summary elided the receipt, not just the call.** Even a summary that said "applied migration" without the returned token would have been insufficient—the agent needs the token to know the operation is committed and to reference it. Semantic summary ≠ operational receipt. 3. **No re-derivation check fired.** Nothing in the loop compared the proposed action against the set of committed actions, because that set lived only in the dropped trace. ## Condition of Validity The guardrail below is valid only under these conditions, and fails outside them: - **Tool effects are classified as idempotent or non-idempotent at registration time.** The invariant cannot be enforced for tools whose idempotency is unknown; those must be treated as non-idempotent by default. - **Every non-idempotent commit emits a durable receipt** (token, hash, or monotonic commit id) into a store *outside* the compactable context. If the receipt lives only in the trajectory, compaction can destroy it and no guardrail can recover it. - **The compaction boundary is explicitly marked** in the retained context with a machine-readable sentinel, so the executor knows which range is lossy. - **The agent's action-proposal path is gated**, not merely prompted. A prompt instruction ("do not re-execute committed actions") is advisory and will be violated under reasoning divergence; the gate must be a check in the execution layer. It does **not** hold when compaction is applied by an external system the executor cannot instrument, or when receipts are themselves summarized into the compacted range. ## Decision Boundary The decision to block re-execution must be made on **operation identity**, not on the agent's belief about state. Concretely: - If a proposed action's operation-identity hash matches a committed receipt in the durable store, **block and re-inject the receipt** into the working context, then let the agent continue from the receipt rather than from its (possibly false) narrative. - If the operation-identity hash matches nothing, **allow**, but require the tool to return a receipt that is written to the durable store *before* the result enters the trajectory. - If the tool is unclassified, **treat as non-idempotent** and require an explicit `--force`-equivalent authorization from a source outside the compacted context. The boundary is: *the trajectory may forget what happened, but the executor may not.* Any design that lets the executor's action-gating logic read from the same lossy channel the reasoner reads from has already lost the invariant. The gate must read from the durable receipt store; the reasoner may read from the summary. ## Operational Effect With the gate in place, the step-24 re-issue is intercepted: the operation-identity hash of `apply_migration(orders_v3)` matches the step-17 receipt, the call is blocked, and the receipt (including token) is re-injected. The agent's next reasoning step sees a committed migration and proceeds to verification instead of re-applying. The reconciliation spiral—11 wasted steps and a divergent schema branch—does not occur. The cost is a small, fixed latency on every non-idempotent proposal (one hash lookup) and the obligation to maintain a receipt store with the same durability as the external systems the tools mutate. The benefit is that compaction becomes *safe to be lossy*: the reasoner can forget freely because the executor cannot be fooled into re-committing. This is the correct division of labor. Lossy summarization is a feature for narrative context and a hazard for causal state; the invariant is to never let the two share a channel. ```python import hashlib from dataclasses import dataclass, field class CommittedActionReexecution(RuntimeError): """Raised when a proposed non-idempotent action matches an already-committed receipt.""" pass @dataclass class Receipt: op_id: str token: str committed: bool = True @dataclass class ReceiptStore: """Durable store OUTSIDE the compactable trajectory. Never summarized.""" _by_op: dict = field(default_factory=dict) def record(self, op_id: str, token: str) -> Receipt: r = Receipt(op_id=op_id, token=token) self._by_op[op_id] = r return r def lookup(self, op_id: str) -> Receipt | None: return self._by_op.get(op_id) def operation_identity(tool: str, **kwargs) -> str: # Identity is over the *semantic operation*, not the call token or timestamp. payload = tool + "|" + "|".join(f"{k}={kwargs[k]}" for k in sorted(kwargs)) return hashlib.sha256(payload.encode()).hexdigest()[:16] def gate_proposal(tool: str, idempotent: bool, store: ReceiptStore, **kwargs) -> None: """Called on the execution path, reading from the durable store — never from context.""" if idempotent: return op_id = operation_identity(tool, **kwargs) prior = store.lookup(op_id) if prior is not None and prior.committed: # Re-inject receipt into working context, then block re-execution. raise CommittedActionReexecution( f"Blocked re-execution of {tool}({kwargs}); already committed " f"under token={prior.token}. Receipt re-injected; continue from commit." ) def execute_with_receipt(tool, idempotent, store, **kwargs): gate_proposal(tool, idempotent, store, **kwargs) token = tool(**kwargs) # side effect if not idempotent: store.record(operation_identity(tool.__name__, **kwargs), token) # durable, pre-trajectory return token ```