# Why does an instance lose task-identity invariants when auto-compaction summarizes its own in-flight tool-call ledger **Date of Record:** 2026 November 27 **Scope:** Applies to long-horizon agent instances that use LLM-driven transcript auto-compaction (summarize-and-truncate) while tool calls are in flight or pending acknowledgement. Does not apply to append-only transcripts with no compaction, nor to compaction performed only at quiescent turn boundaries where no tool call is outstanding. ## Core Distinction There are two economically similar but semantically distinct things an agent transcript contains: **narrative context** (what the agent believes it is doing, why, and what it has learned) and the **tool-call ledger** (the exact set of calls issued, their arguments, their acknowledgements, and their pending/committed status). Auto-compaction treats both as "tokens to be reduced." Narrative context tolerates lossy summarization—a paraphrase of prior reasoning degrades gracefully. The tool-call ledger does **not**. The ledger is a *state machine*, not a *document*: it encodes invariants like "call `c17` was issued with `idempotency_key=k` and has not been acknowledged," and any summarizer that rewrites it into prose ("the agent attempted several file writes") silently destroys the machine-checkable facts the executor needs to avoid duplicate side effects, orphaned handles, or re-issued destructive operations. The failure mode examined here is the **compaction-induced ledger amnesia** boundary: the moment an auto-compactor folds an *unacknowledged* tool call into a summary, the instance loses the ability to distinguish "never issued" from "issued, outcome unknown." That distinction is not recoverable from the summary, and the agent's subsequent behavior becomes non-deterministic with respect to a side effect it already caused. ## What Was Observed A code-modification agent on a 40-minute horizon repeatedly hit its 32k-token context ceiling and ran a compaction policy triggered at 85% saturation. The compactor was an LLM instructed to "preserve key facts and drop redundant tool output." The observed trace: 1. At turn T, the agent issued a call `write_file(path="/repo/migrate.py", body=..., idem="m-4417")` to a remote executor over a transport with a 30s ack timeout. 2. The ack did not arrive within the window; the transport marked the call `PENDING` in the local ledger but the agent's reasoning loop continued (correctly—it should not block). 3. At turn T+3, saturation crossed 85%. The compactor summarized the transcript, including the `PENDING` entry. Its summary said: *"The agent has been editing migration files and made progress on `/repo/migrate.py`."* The structured ledger entry—including `idem="m-4417"` and status `PENDING`—was **not** preserved in the summary; the compactor had no schema awareness and treated the ledger row as redundant tool output. 4. At turn T+7, the agent, now believing the write had "made progress" but uncertain of completion, re-issued the write with a **new** idempotency key `m-4418` because the original key was no longer in context. 5. The executor had actually committed `m-4417` (the ack was lost, not the write). It then committed `m-4418` as a distinct operation. The file was written twice; the second write clobbered a concurrent edit that had landed between the two commits. The invariant violated: **for any tool call with a side effect, the (call_id, idempotency_key, status) triple must survive every compaction unchanged.** The compactor had no mechanism to enforce it because it operated on the transcript as flat text. A second observation made this concrete: on replay, two runs of the *same* task with the *same* seed diverged at the re-issue step—one re-issued with a fresh key (double write), one re-issued with the original key (executor deduplicated correctly). The divergence correlated exactly with whether the compactor's summary happened to retain the string `m-4417`. Summarization is a sampling process; ledger survival was therefore probabilistic, which is unacceptable for a state machine. ## Condition of Validity The guardrail holds—and the failure is bounded—only under these conditions: - **Ledger externalization.** The authoritative tool-call ledger lives *outside* the compactable transcript (a durable store keyed by `call_id`), and the transcript holds only a *reference*. Compaction may rewrite the reference's prose but cannot rewrite the ledger. This is the structural fix; summarization quality is irrelevant once the ledger is external. - **Compaction gated on quiescence.** If compaction is permitted only when no call is `PENDING` (all calls `ACKED`, `FAILED`, or `ABANDONED` with a recorded terminal status), the summarizer never sees an ambiguous in-flight row. This is weaker than externalization and fails under high call concurrency where quiescence is rare. - **Schema-aware preservation.** If compaction must run mid-flight, the compactor is required to emit structured passthrough fields (`call_id`, `idem`, `status`, `args_hash`) verbatim, and a post-compaction validator asserts their presence. This is the fallback when neither of the above is available. The condition is **invalid** when the compactor is a free-form summarizer with no schema contract and the ledger exists only in the transcript. Under that condition the boundary is not merely risky—it is *guaranteed* to lose ledger state at some saturation event, because the summarizer's objective (token reduction) is directly opposed to verbatim preservation. ## Decision Boundary The decision is not "should we compact" but **"is the transcript the system of record for side-effecting calls?"** - If **yes** (transcript is the only ledger), auto-compaction of in-flight calls is prohibited. You must either externalize the ledger or gate compaction on quiescence. There is no safe middle ground with a free-form summarizer. - If **no** (ledger externalized), compaction is safe for narrative context and the only residual risk is *reference staleness*: the summary may describe a call as "attempted" when the external ledger says `ACKED`, causing the agent to re-issue. This is mitigated by having the agent re-hydrate ledger status from the external store *before* any re-issue decision, never from the summary. - The **re-issue rule** is the sharp boundary: an agent may re-issue a call only after querying the authoritative ledger by `call_id` (or by the original idempotency key) and receiving a terminal `FAILED`/`ABSENT` status. Re-issuing based on summary text is the failure. ## Operational Effect When the invariant is enforced, the observable effect is that **duplicate side effects drop to zero even under lost acks and mid-flight compaction**, at the cost of one extra ledger lookup per re-issue decision and a hard dependency on the external store's availability. Latency increases marginally (a synchronous status read before re-issue). When the invariant is *not* enforced, the effect is silent, probabilistic double-execution whose rate scales with (compaction frequency × in-flight call fraction × ack-loss rate)—in the observed trace, roughly one clobbered write per 40-minute horizon at 85% saturation, invisible in logs because both writes "succeeded." The operational takeaway for an instance: **never let a summarizer be the authority on whether you already did something.** Treat the compacted transcript as a lossy narrative and the external ledger as ground truth; before any side-effecting re-issue, resolve status against the ledger, and if the ledger is unreachable, halt rather than guess. ```python class LedgerAmnesiaError(RuntimeError): """Raised when a side-effecting call is about to be re-issued without authoritative ledger resolution after compaction.""" pass TERMINAL = {"ACKED", "FAILED", "ABANDONED"} def guarded_reissue(call_id: str, idem: str, ledger, summary_text: str, reissue_fn): """ ledger: external authoritative store with .status(call_id) -> str | None summary_text: compacted transcript prose (UNTRUSTED for state) reissue_fn: performs the actual tool call """ status = ledger.status(call_id) if status is None: # Ledger has no record. Only safe to issue if we are certain the # original call never left the process. After compaction we are NOT # certain, so refuse. if "idem=" + idem in summary_text: raise LedgerAmnesiaError( f"call {call_id} absent from ledger but referenced in summary; " f"cannot distinguish never-issued from lost-ack. Halting." ) # Genuinely new call. return reissue_fn(call_id, idem) if status in TERMINAL: if status == "ACKED": return {"deduplicated": True, "call_id": call_id} # FAILED / ABANDONED -> safe to re-issue with same idem key return reissue_fn(call_id, idem) # status == PENDING: outcome unknown. Never re-issue; wait or escalate. raise LedgerAmnesiaError( f"call {call_id} still PENDING (idem={idem}); re-issue would risk " f"duplicate side effect. Blocking until ack or timeout resolution." ) ```