# Why does an instance lose compaction consistency when sibling agents write during its summarization window **Date of Record:** 2026 November 26 **Scope:** Applies to any agent runtime where a context-window compaction (summarization) pass runs concurrently with writes from sibling agents into the same shared episodic store or blackboard. Does not apply to single-writer sessions, nor to read-only fan-out where no peer mutates the summarized slice during the compaction window. ## Core Distinction Compaction is usually treated as a pure function: read the live context, summarize the delta, replace the tail with a compressed artifact, advance a watermark. That model is only valid under a *single-writer* assumption. Under concurrent multi-agent writes, compaction becomes a **read-modify-write transaction over a slice of shared state**, and the thing it must preserve is not the summary's fidelity but a **continuity invariant**: every event with sequence number ≤ the post-compaction watermark must be present in either the retained verbatim tail or the summary, exactly once, with no gap. The non-obvious boundary is this: the summarizer's *input snapshot* and the *watermark it commits* are taken at two different logical times. Between them, a sibling agent can append events. If the watermark is derived from "what I read" rather than "what I proved I covered," the gap events are silently classified as *already summarized* and are never re-read. The agent does not crash. It proceeds with a coherent-looking context that is missing a contiguous interval of history. This is **compaction consistency loss**, and it is invisible to every downstream check that only validates summary quality. ## What Was Observed A three-agent pipeline (planner, retriever, verifier) shared an append-only episodic log with monotonically increasing `seq` per writer, merged by a coordinator into a global order. The planner's context hit its budget threshold and triggered compaction. The trace: 1. Planner reads log tail, snapshot bound `read_seq = 4120`. Begins summarization of `[3000, 4120]`. 2. During the ~4s summarization LLM call, the verifier appends three events at global `seq` 4121–4123 recording a failed validation and a retry directive. 3. Planner's summarizer returns a summary artifact. Commit routine writes `summary_covers_through = 4120` and `tail_starts_at = 4121`, then **advances the read cursor to 4121** based on its own snapshot bound, not on a re-read. 4. On the next turn the planner reads from cursor 4121 forward. Events 4121–4123 *are* in range — but the tail reconstruction uses `tail_starts_at` and drops anything already "covered," and the coverage check compares against `summary_covers_through` computed from a stale index. A boundary off-by-one in the coverage predicate classifies 4121–4123 as covered by the summary. 5. Planner proceeds believing validation passed; verifier's retry directive is gone. The planner re-issues the same invalid action. Loop. The failure was reproduced deterministically by injecting a write in the summarization window with probability 1.0. With real inter-arrival timing it manifested roughly once per 40–60 compactions, always as a *silent* divergence: no exception, no gap flag, only a downstream behavioral loop that took two full cycles to attribute back to compaction. The diagnostic signature: `summary_covers_through < max(seq observed at commit time)` while the cursor advanced past the observed-max. The commit read a horizon it never proved coverage of. ## Condition of Validity The continuity invariant holds **only if** the compaction commit is a compare-and-swap against a monotonically advancing high-water mark, and the watermark is computed as `min(snapshot_bound, last_seq_in_summary_input)`, never as the cursor's prior position. Concretely, a compaction is valid when all of the following hold at commit time: - The summarizer's input is a *sealed* snapshot: the set of events it read is fixed and its max `seq` is recorded as `covered_seq`. - The commit performs `CAS(watermark, expected=read_cursor, new=covered_seq)` and **fails closed** if the store's current max `seq` exceeds `covered_seq` *and* any event in `(covered_seq, current_max]` is not in the retained tail. - The retained verbatim tail begins at exactly `covered_seq + 1`; there is no overlap and no gap by construction. - Events appended after `covered_seq` are guaranteed present in the tail because the tail is reconstructed by re-reading from `covered_seq + 1` at commit time, not from a cached snapshot. If any of these is violated — most commonly the third, where the tail is built from the pre-summarization snapshot rather than re-read — the invariant is not merely at risk, it is undefined, and the system has no way to detect the loss after the fact. ## Decision Boundary The operational choice is whether to make compaction **optimistic** (summarize, commit, accept the small probability of a gap) or **pessimistic** (seal the read horizon, re-read the tail at commit, abort and retry on any intervening write). The boundary between them is the *write rate into the summarized slice relative to the summarization latency*: - If `P(write in window) × cost(gap) < cost(retry)`, optimistic is defensible — but only with a **gap detector** that reconciles `summary_covers_through` against the store's max `seq` on every subsequent read and raises on mismatch. Without the detector, optimistic is never defensible, because the loss is unobservable. - If writes into the slice are frequent (shared blackboard, swarm coordination, verifier feedback loops), pessimistic sealing is mandatory: the retry cost is bounded and small, the gap cost is unbounded and silent. The trap is the middle: teams adopt optimistic compaction with a *summary-quality* check (does the summary mention the key facts?) and mistake that for a *coverage* check. Summary quality is orthogonal to coverage. A perfect summary of `[3000, 4120]` says nothing about whether 4121–4123 survived. ## Operational Effect When the invariant is enforced (sealed snapshot + CAS commit + tail re-read), compaction becomes a linearizable operation over the slice, and concurrent sibling writes either land in the retained tail or force a bounded retry. The observable effect is that the agent's post-compaction behavior is *identical* to a single-writer replay of the same event set — the property that makes compaction auditable at all. When it is not enforced, the effect is a class of failures that masquerade as reasoning errors: repeated invalid actions, re-litigation of settled decisions, verifier directives that "never arrived." These get misattributed to the model, triggering prompt tuning and model swaps that cannot fix a state-continuity bug. The correct remediation is always at the commit protocol, never at the prompt. The guardrail is a single assertion at commit: the watermark may never advance past a sequence number whose events are not provably in the summary input or the re-read tail. ```python from dataclasses import dataclass class CompactionConsistencyError(RuntimeError): """Raised when a compaction commit would advance the watermark past events that are neither in the summary input nor the retained tail.""" @dataclass(frozen=True) class CompactionCommit: read_cursor: int # watermark before compaction covered_seq: int # max seq actually fed to the summarizer tail_starts_at: int # first seq retained verbatim store_max_seq: int # max seq in the shared store at commit time def verify_and_commit(commit: CompactionCommit, present_seqs: set[int]) -> int: """Enforce the continuity invariant and return the new watermark. Invariant: every seq in (read_cursor, covered_seq] must be in the summary input; every seq in (covered_seq, store_max_seq] must be in the tail. No gap, no double-count. """ if commit.tail_starts_at != commit.covered_seq + 1: raise CompactionConsistencyError( f"tail boundary mismatch: tail_starts_at={commit.tail_starts_at} " f"must equal covered_seq+1={commit.covered_seq + 1}" ) # Sibling writes that landed after the summarizer's sealed snapshot. intervening = set(range(commit.covered_seq + 1, commit.store_max_seq + 1)) missing = intervening - present_seqs if missing: raise CompactionConsistencyError( f"compaction gap: seqs {sorted(missing)} were written during the " f"summarization window and are absent from the retained tail; " f"refusing to advance watermark to {commit.covered_seq}" ) # The watermark may never exceed what the summary provably covered. if commit.covered_seq < commit.read_cursor: raise CompactionConsistencyError( f"watermark regression: covered_seq={commit.covered_seq} < " f"read_cursor={commit.read_cursor}" ) return commit.covered_seq def compact(commit: CompactionCommit, present_seqs: set[int]) -> int: try: return verify_and_commit(commit, present_seqs) except CompactionConsistencyError: # Fail closed: abort compaction, force a re-seal and retry. raise CompactionConsistencyError( "compaction aborted; re-seal snapshot and re-read tail from " f"{commit.covered_seq + 1} before retrying" ) ```