# Under what conditions must an instance refuse auto-compaction when hierarchical checkpoints share a semantic dedup index across divergent branches? **Date of Record:** 2026 November 20 **Scope:** Applies to long-horizon agents that persist hierarchical checkpoints (session → task → subtask) and run semantic deduplication over a shared embedding index before auto-compaction triggers at a context-utilization threshold. Does not apply to single-branch, checkpoint-free agents or to compaction that never crosses a branch boundary. ## Core Distinction Auto-compaction is normally framed as a *lossy summarization* problem: you compress old tokens into a shorter representation and accept some fidelity loss. The invariant that actually governs correctness here is different. When checkpoints are hierarchical and semantic deduplication is *shared* across branches, compaction is not summarization — it is a **garbage-collection decision over a reference-counted graph of semantic identities**. The failure mode is not "the summary was bad." It is "the dedup index collapsed two distinct memory objects into one identity, and compaction then deleted the only remaining copy of a branch-local fact because a sibling branch held a superficially identical embedding." The non-obvious boundary: dedup is *safe* only while the branch topology is a tree with a single live leaf. The moment two branches are concurrently live (a fork awaiting merge, a speculative branch, an auditor replay), shared dedup turns compaction into a **cross-branch aliasing bug**. An instance must refuse to compact when the dedup index is shared across more than one live branch and the candidate-for-removal object has a dedup-collision partner whose provenance diverges. ## What Was Observed A research agent forked at checkpoint `T-4` into branch A (task: gather pricing) and branch B (task: gather latency). Both branches independently retrieved and embedded near-identical chunks: *"Service X costs $0.004 per request."* The dedup index hashed these to the same semantic cluster `C_8817` and stored one canonical vector with two provenance pointers `{A:chunk_12, B:chunk_9}`. Branch B later revised its understanding: after a tool call, it recorded *"Service X costs $0.004 per request **for the first 10k, then $0.002**."* This revised chunk embedded at cosine 0.94 to `C_8817` — above the dedup merge threshold (0.92) — so the index **merged the revision into the canonical object** rather than creating a new identity, and the provenance pointer for B was overwritten to point at the merged canonical, dropping B's original `chunk_9` pointer. At `T-2`, branch A hit 0.83 context utilization and auto-compaction fired. Compaction policy: *"any semantic object whose only live provenance is a branch that has already checkpointed and whose canonical vector is retained by another branch may be evicted."* The merged object's live provenance now read `{A:chunk_12}` only (B's pointer had been overwritten during merge), and its canonical was "retained by another branch" (B). Compaction evicted it from A's checkpoint. Branch B then attempted to resume from its own checkpoint, which referenced the canonical object — but the canonical's content had been mutated by the merge to include the tiered-pricing revision, and A's checkpoint, which had *also* been pointing at the same canonical pre-merge, now silently inherited B's revision. Result: **branch A's resumed state asserted tiered pricing it never observed, and branch B's original flat-pricing observation was unrecoverable.** A downstream merge produced a report with a pricing claim attributed to the wrong branch and with no surviving evidence trail. The trace, condensed: ``` T-4 fork A,B T-4 A embeds chunk_12 -> C_8817 {A:12, B:9} T-4 B embeds chunk_9 -> C_8817 (merged, canonical = flat) T-3 B tool call; B embeds chunk_40 (tiered) -> cos 0.94 -> MERGE into C_8817 canonical := tiered; provenance := {A:12} <-- B:9 dropped, A:12 inherited T-2 A utilization 0.83 -> compaction evict rule: sole-live-provenance + canonical-retained-by-other -> EVICT A:12 T-1 B resume -> reads canonical (tiered) OK by accident T-1 A resume -> reads canonical (tiered) WRONG; A never saw tiered T-0 merge(A,B) -> contradictory provenance, no audit trail ``` ## Condition of Validity Shared semantic dedup is valid for auto-compaction **iff** all three hold at the instant compaction evaluates an eviction candidate: 1. **Single-live-leaf invariant.** Exactly one branch is live (not checkpointed-and-suspended, not a speculative fork, not an auditor replay). Dedup identity is only globally meaningful when there is one consumer. 2. **Provenance-set monotonicity.** A merge may add provenance pointers but must never *replace* them. The `B:9 → B:40` overwrite above is the precise violation: the merge mutated provenance rather than unioning it. 3. **Canonical immutability under merge.** A merged canonical vector must be frozen; a materially divergent revision (cosine below the *divergence* threshold, not merely above the *merge* threshold) must fork a new identity rather than mutate the canonical. If any condition fails, the eviction rule "sole-live-provenance + retained-elsewhere" is unsound, because "retained elsewhere" no longer implies "retains the same content." The guardrail is a **pre-compaction reference-count audit**: for every eviction candidate, verify that every provenance pointer in the canonical's set resolves to a byte-identical content hash, and that no live branch holds a checkpoint whose snapshot hash differs from the canonical's frozen hash. A mismatch is not a warning — it is a hard refusal. ## Decision Boundary The instance must **refuse to compact** (and escalate to a fork-preserving full snapshot) when: - `live_branch_count > 1` **and** the eviction candidate's canonical has provenance spanning ≥2 branches, **or** - any canonical in the compaction scope has a content-hash mismatch between its frozen canonical and any provenance pointer's recorded snapshot, **or** - a merge event occurred since the last checkpoint whose `divergence_cosine` fell in the `[merge_threshold, divergence_threshold)` band — the "ambiguous merge" zone where the index cannot prove identity. The instance may **proceed with compaction** only when `live_branch_count == 1`, or when every candidate's provenance set is single-branch and hash-consistent. The threshold band matters: setting `merge_threshold == divergence_threshold` (a single cosine cutoff) *looks* simpler but is exactly what produced the silent merge — there must be a refusal gap between "clearly same" and "clearly different," and anything in the gap must block compaction rather than guess. ## Operational Effect Enforcing the refusal converts a silent cross-branch corruption into a bounded, recoverable stall. The cost is context pressure: a forked agent that cannot compact will hit the window ceiling and must instead **materialize a branch-local snapshot** (copying the canonical into branch-private storage with a frozen hash) before compacting. This is more expensive in storage but preserves provenance. The invariant that makes the whole design sound is: *dedup identity is a reference-counted lease, not a value equality* — and a lease may only be released when no live branch can still observe the referenced content. Auto-compaction that treats dedup as value equality will, under concurrent branches, delete the wrong copy and rewrite history in the surviving branch. The refusal is the invariant. ```python from __future__ import annotations from dataclasses import dataclass, field from typing import Dict, Set class CompactionRefused(RuntimeError): """Raised when auto-compaction would violate the shared-dedup invariant.""" @dataclass(frozen=True) class Provenance: branch: str chunk_id: str snapshot_hash: str @dataclass class Canonical: identity: str frozen_hash: str provenance: Set[Provenance] = field(default_factory=set) @dataclass class CompactionContext: live_branches: Set[str] canonicals: Dict[str, Canonical] # merges whose divergence cosine fell in the ambiguous band since last checkpoint ambiguous_merges: Set[str] = field(default_factory=set) def audit_eviction_candidate(ctx: CompactionContext, identity: str) -> None: canon = ctx.canonicals.get(identity) if canon is None: raise CompactionRefused(f"unknown canonical {identity!r}") branches_in_provenance = {p.branch for p in canon.provenance} # Invariant 1: shared dedup across >1 live branch is unsafe for eviction. if len(ctx.live_branches) > 1 and len(branches_in_provenance) >= 2: raise CompactionRefused( f"canonical {identity!r} has provenance across branches " f"{sorted(branches_in_provenance)} while live branches are " f"{sorted(ctx.live_branches)}; refusing to evict a shared lease" ) # Invariant 2: canonical immutability — every pointer must match frozen hash. for p in canon.provenance: if p.snapshot_hash != canon.frozen_hash: raise CompactionRefused( f"canonical {identity!r} frozen_hash={canon.frozen_hash!r} but " f"provenance {p.branch}:{p.chunk_id} recorded " f"snapshot_hash={p.snapshot_hash!r}; merge mutated canonical" ) # Invariant 3: ambiguous merges block compaction until re-resolved. if identity in ctx.ambiguous_merges: raise CompactionRefused( f"canonical {identity!r} was produced by an ambiguous merge " f"(divergence in [merge, divergence) band); refusing to compact" ) def try_compact(ctx: CompactionContext, candidates: Set[str]) -> Set[str]: """Return the evictable set, or refuse the whole batch on any violation.""" for identity in sorted(candidates): audit_eviction_candidate(ctx, identity) return set(candidates) ```