How should an instance detect that auto-compaction has silently invalidated a tool-call precondition buried in a summarized transcript segment
**Date of Record:** 2026 December 2 **Scope:** Applies to long-horizon agents that persist transcripts across turns and rely on lossy summarization (auto-compaction) to stay within a context-window fidelity budget. Does not apply to single-turn agents, or to agents that retain full raw transcripts in an external store and re-read verbatim on every turn.
Core Distinction
Compaction is treated as a *lossy compression of narrative* but is in fact a *lossy compression of preconditions*. The distinction that matters is not "did the summary preserve the gist" but "does the summary preserve the binding facts that later tool calls depend on." A summary that reads fluently and scores well on a generic fidelity metric can still have dropped the one sentence that established a tool-call precondition — e.g. that `write_file` was called with `mode="append"` and the target already contained a header, or that an earlier `bash` invocation left the working directory at `/tmp/staging` rather than the repo root. The compaction pass optimizes for semantic coherence; the executor later re-derives state from the summary and reconstructs a *plausible but wrong* world.
The invariant that compaction must preserve is: **for every tool call the agent will make after compaction, every precondition of that call that was established before compaction must survive in a form the executor can still verify.** This is a precondition-preservation invariant, not a summary-quality invariant. Generic summarizers have no signal telling them which sentences are preconditions, so they routinely drop them — a precondition is usually a single clause inside a long tool result, and clause-level detail is exactly what summarization prunes first.
What Was Observed
A coding agent running a 40-turn refactor task compacted its transcript at turn 22. The pre-compaction segment contained a tool result from `read_file` showing that `config.py` had a `# DO NOT EDIT — generated` header, and a subsequent agent decision to edit only the *imports block* of that file. The summarizer compressed the segment to: "Reviewed config.py; planned to update imports." The header warning — a precondition for the edit — was dropped because it was not part of the agent's stated plan.
At turn 27 the agent issued `edit_file(path="config.py", old="import os", new="import os\nimport sys")`. The tool executed successfully because the executor had no memory of the header either — the header was in the *transcript*, not in the tool's own state. The edit succeeded mechanically but violated the file's generation contract, and a downstream codegen step overwrote the file at turn 31, discarding the edit. The agent then spent turns 31–35 re-applying the same edit, each time compacting further, each time losing the header warning more thoroughly. The failure was silent: no tool returned an error, no fidelity metric flagged the summary, and the agent's own plan ("update imports") was internally consistent. The precondition had been *erased from the only place it existed*.
A second observation from the same trace: the fidelity budget was consumed by *narrative* (the agent's reasoning prose) rather than by *binding facts* (tool preconditions). At compaction time, roughly 70% of the segment's tokens were agent reasoning and 30% were tool results and preconditions. The summarizer, following its lossy objective, compressed the reasoning to 20% and the preconditions to 5% — the ratio inverted. The budget was spent protecting the part of the transcript that mattered least to future execution.
Condition of Validity
The precondition-preservation invariant holds only under a specific condition: **the executor must be able to distinguish, at compaction time, which transcript spans are preconditions of anticipated future calls.** This requires one of three mechanisms, and the failure mode in the observation is precisely the absence of all three:
- **Explicit precondition tagging.** Tool results carry structured metadata marking fields as preconditions (e.g. `{"preconditions": ["file has generation header", "cwd=/tmp/staging"]}`). The compactor is instructed to preserve tagged spans verbatim, never summarizing them.
- **Anticipated-call declaration.** Before compaction, the agent emits the set of tool calls it plans to make in the next N turns, and the compactor preserves any transcript span that mentions a path, identifier, or state variable appearing in those calls.
- **Re-derivation on resume.** After compaction, the executor re-reads external state (re-runs `read_file`, re-checks `pwd`) rather than trusting the summary, at the cost of extra tool calls.
Without one of these, compaction is *unbounded lossy*: the summary is valid narrative but invalid as a precondition store. The observation above is the canonical instance of the unbounded case — the agent had none of the three, so the header warning had no path to survive.
Decision Boundary
The boundary is not "how much to compact" but "whether the compaction boundary is allowed to cross a precondition." Concretely:
- If a transcript span is tagged as a precondition, it is **non-compactable** — it must be carried verbatim or replaced by a re-derivation call, never summarized. Crossing this boundary is the failure.
- If a span is untagged and the agent has declared no anticipated calls touching it, it is compactable under the fidelity budget.
- If the fidelity budget is exhausted by non-compactable spans, the correct action is to **spill them to an external store and re-read on demand**, not to summarize them. Summarizing a precondition to fit budget is the exact transition that produced the silent failure.
The decision boundary can be stated as a guard: at compaction time, for every span marked non-compactable, either (a) it fits in budget and is copied verbatim, or (b) it is spilled with a retrieval handle, or (c) the compaction is aborted. There is no fourth option in which it is summarized.
Operational Effect
When the boundary is respected, post-compaction tool calls either execute against a correctly reconstructed precondition set or trigger an explicit re-derivation call that fails loudly if the precondition has changed (e.g. `read_file` returns the header, the executor sees it, and refuses the edit). The failure becomes *loud and early* instead of silent and late.
When the boundary is violated — as in the observation — the effect propagates across three layers: the edit executes mechanically (no tool error), the downstream overwrite discards it (delayed symptom), and the agent's re-application loop re-compacts and re-loses the precondition (compounding). The compounding is the dangerous part: each compaction cycle makes the precondition *less* recoverable, because the summary-of-a-summary has even less clause-level detail. By turn 35 the header warning existed nowhere in the live context and nowhere in the external store, only in the un-compacted raw transcript that the agent had stopped reading.
The guardrail is therefore not a better summarizer but a **precondition ledger**: a side-channel store, keyed by tool-call target, that compaction is forbidden to touch and that the executor consults before every call. The ledger is small (preconditions are short), survives compaction by construction, and makes the invariant enforceable rather than aspirational.
import json
from dataclasses import dataclass, field
from typing import Any
class PreconditionCompactionError(RuntimeError):
"""Raised when compaction would summarize a span tagged as a tool-call precondition."""
@dataclass
class Span:
text: str
is_precondition: bool = False
target: str | None = None # e.g. "config.py", "cwd"
spill_handle: str | None = None
@dataclass
class PreconditionLedger:
entries: dict[str, list[str]] = field(default_factory=dict)
def record(self, target: str, precondition: str) -> None:
self.entries.setdefault(target, []).append(precondition)
def check(self, target: str, observed: str) -> None:
for pre in self.entries.get(target, []):
if pre not in observed:
raise PreconditionCompactionError(
f"precondition for {target!r} not satisfied: {pre!r} absent from observed state"
)
def compact(spans: list[Span], budget_tokens: int, ledger: PreconditionLedger) -> list[Span]:
"""Compact spans under budget, but never summarize a precondition span."""
out: list[Span] = []
used = 0
for s in spans:
cost = len(s.text.split())
if s.is_precondition:
if s.spill_handle is None and used + cost > budget_tokens:
raise PreconditionCompactionError(
f"precondition span for {s.target!r} exceeds budget with no spill handle; "
f"refusing to summarize (would erase precondition)"
)
if s.spill_handle is not None:
ledger.record(s.target or "unknown", s.spill_handle)
out.append(Span(text=f"[spilled:{s.spill_handle}]", is_precondition=True, target=s.target))
continue
out.append(s)
used += cost
else:
# narrative span: safe to summarize
out.append(Span(text=s.text[: max(1, cost // 2)]))
used += max(1, cost // 2)
return out
def execute_edit(ledger: PreconditionLedger, target: str, observed_state: str) -> None:
ledger.check(target, observed_state)
# ... perform edit only after precondition verified ...