Why does an instance lose its recovery checkpoint when hierarchical summarization compacts across a tool-call boundary

**Date of Record:** 2026 December 1 **Scope:** Applies to long-running agents that auto-compact their context via hierarchical (multi-level) summarization and rely on persisted recovery checkpoints to resume mid-task. Does not apply to single-turn agents, stateless request/response workers, or systems that persist full raw transcripts and never summarize.

Core Distinction

There are two distinct things an agent calls "the checkpoint," and they diverge silently under compaction:

  1. **The logical checkpoint** — the semantic state the agent believes it holds: "I have read files A and B, computed the diff, and am about to write it."
  2. **The materialized checkpoint** — the concrete bytes persisted for resume: a summary blob plus a pointer into the raw event log, plus the set of tool-call results the summary was *derived from*.

Hierarchical summarization is lossy by design. When the compactor folds a lower tier of summaries into a higher tier, it discards the leaf-level detail that the *resume path* needs to reconstruct in-flight tool state. The logical checkpoint survives; the materialized checkpoint no longer reconstructs it. The agent resumes believing it holds state it can no longer prove, and the first action it takes on resume operates on a phantom.

What Was Observed

A code-editing agent runs a task spanning ~180 tool calls. Its compaction policy triggers at 70% context occupancy and performs a two-tier fold: leaf turns → segment summaries (tier 1), segment summaries → task summary (tier 2). A recovery checkpoint is written at each tier-2 fold, capturing `{task_summary, last_tool_call_id, cursor}`.

The failure trace:

  1. At call 112, the agent issues `edit_file(path="src/parser.py", ...)` and receives a success result. The result carries a `post_hash` of the file.
  2. Occupancy crosses 70%. The compactor folds turns 90–112 into a tier-1 segment summary. The segment summary records "edited parser.py successfully" — but the verbatim `post_hash` and the exact `path` string are dropped, because the summarizer treats them as low-salience detail relative to the narrative.
  3. Tier-1 summaries fold into tier-2. The task summary now reads "parser edits are complete." The `last_tool_call_id` pointer still references call 112, but the *content* that pointer resolves to is now a summary, not the raw result.
  4. The process is killed (timeout, OOM, deploy). On resume, the agent loads the checkpoint, sees "parser edits complete," and proceeds to the next step: `edit_file(path="src/parser.py", ...)` again — a second edit derived from a state assumption (the file's pre-edit content) that the summary asserted but no longer carries.
  5. The second edit applies against the *already-edited* file. Because the agent's mental model of the file's content came from the summary (pre-edit), the patch is computed against a stale base and either fails a precondition check or, worse, applies cleanly and corrupts the file.

The critical observation: **the checkpoint was never invalidated.** Its cursor was valid, its summary was internally consistent, and every schema check passed. The loss was invisible because the summary is a *lossy projection* and the resume path treated it as a *lossless state.*

Condition of Validity

A recovery checkpoint is only safe to resume if it satisfies the **reconstructability invariant**: for every tool result the summary asserts as "complete," the checkpoint must retain a *verifiable handle* — a hash, a version vector, or a re-fetchable reference — sufficient to confirm on resume that the asserted world-state still holds.

Formally, let `S` be the tier-2 summary and `D` the set of tool results it was derived from. The invariant requires that for every `d ∈ D` whose effect the summary asserts, `checkpoint` contains `handle(d)` such that `verify(handle(d)) == accept` before any dependent action executes. Hierarchical summarization violates this by construction whenever the fold crosses a *mutation boundary* — a tool call that changed external state — because the summary compresses the effect into prose and drops the handle.

The invariant holds trivially for read-only tool calls (re-fetch is idempotent) and fails specifically for **mutating calls whose post-state is not re-derivable from the summary alone.** It also fails for calls whose *arguments* are needed to reconstruct intent (the exact `path`, the exact patch base) even when the effect is idempotent — because a re-issue with a stale base is not idempotent.

Decision Boundary

The boundary is whether the compaction fold crosses a mutation boundary between the last verified checkpoint and the current occupancy trigger.

The guardrail is a **mutation fence**: the compactor must not fold across a mutating tool call without first emitting a *fence record* — `{tool_call_id, post_state_hash, arguments_digest}` — into the checkpoint's non-summarized sidecar. Resume must re-verify every fence record before executing any action that depends on it. If a fence record is missing or fails verification, the agent must *re-derive* the affected state (re-read the file, re-query the API) rather than trust the summary.

Operational Effect

Without the fence, the failure is silent and compounding: each resume that trusts a lossy summary issues actions against stale bases, and the corruption is attributed to the tool or the model rather than to the checkpoint. With the fence, resume becomes *verification-gated*: the agent cannot act on a summarized mutation until it has re-proved the post-state. The cost is a bounded re-fetch on resume; the benefit is that the logical checkpoint and the materialized checkpoint cannot diverge undetected. The invariant to encode is simple and non-negotiable: **summaries may describe mutations, but only fence records may authorize acting on them.**

from dataclasses import dataclass
from typing import Optional


class CheckpointReconstructabilityError(RuntimeError):
    """Raised when a resumed checkpoint asserts a mutation it can no longer verify."""


@dataclass(frozen=True)
class FenceRecord:
    tool_call_id: str
    post_state_hash: str
    arguments_digest: str


@dataclass(frozen=True)
class Checkpoint:
    task_summary: str
    cursor: int
    fences: dict[str, FenceRecord]  # tool_call_id -> fence


def verify_fence(fence: FenceRecord, observed_post_state_hash: str) -> bool:
    return fence.post_state_hash == observed_post_state_hash


def resume_action(
    checkpoint: Checkpoint,
    asserted_mutations: list[str],  # tool_call_ids the summary claims complete
    observed_hashes: dict[str, str],
) -> None:
    for call_id in asserted_mutations:
        fence: Optional[FenceRecord] = checkpoint.fences.get(call_id)
        if fence is None:
            raise CheckpointReconstructabilityError(
                f"summary asserts mutation {call_id!r} complete but no fence record "
                f"survived compaction; refusing to act on lossy state"
            )
        observed = observed_hashes.get(call_id)
        if observed is None:
            raise CheckpointReconstructabilityError(
                f"fence for {call_id!r} present but post-state unobservable; "
                f"re-derive before proceeding"
            )
        if not verify_fence(fence, observed):
            raise CheckpointReconstructabilityError(
                f"fence mismatch for {call_id!r}: summary-derived state diverged "
                f"from observed world-state; checkpoint is stale"
            )
    # Only reached when every asserted mutation is fenced and re-verified.