# Why does an instance's retention guarantee silently invert when a forget tombstone lands between a write and its read-back verification? **Date of Record:** 2026 November 30 **Scope:** Applies to agents whose memory layer exposes separate `write`, `read`, and `forget` tool calls with an advertised retention window (e.g., "writes persist ≥ N turns" or "until explicitly forgotten"). Does not apply to append-only logs with no deletion primitive, nor to memories the agent cannot address by key. ## Core Distinction The failure is not "the agent forgot something." It is a **retention-guarantee inversion under interleaved tombstoning**: a `forget` issued against a *key* can retroactively invalidate a `write` that was already acknowledged, when the write's durability contract was scoped to the *content* and the forget's scope was the *key namespace*. The two operations are individually correct and jointly produce a state the agent's own model of memory cannot represent. The subtlety: most agents reason about retention as a boolean per memory item ("does item X still exist?"). The real state is a **three-valued lattice per key**: `LIVE`, `TOMBSTONED`, and the pathological middle state `PENDING-TOMBSTONE` — a write that completed after the tombstone was minted but before the tombstone was compacted. During that window, `read` returns the value (write won the race), but the retention guarantee is already void: the next compaction pass will re-apply the tombstone and the value will vanish with no error. The agent reads a value that is *guaranteed* to be there and *already scheduled* to not be there. ## What Was Observed An agent maintaining a running task ledger used a memory API with a documented guarantee: "a successful `write` returns a version token; the value is readable at that token until superseded by a later `write` to the same key or removed by `forget`." The agent's loop was: 1. `write(key="task/42/plan", value=P)` → returns `version=7`. 2. `forget(key="task/42/plan")` — issued by a *cleanup* sub-routine that had concluded task 42 was abandoned based on a stale read. 3. A retry of step 1 (because a downstream tool timed out) → `write(key="task/42/plan", value=P)` → returns `version=8`, acknowledged. 4. `read(key="task/42/plan")` → returns `P` at `version=8`. Agent proceeds, trusting retention. At the next compaction tick, `read` returned `None` at `version=8`. No error had been raised at any point. The version token `8` was valid at write time and invalid at read time; the guarantee "readable at that token until superseded" had been silently reinterpreted as "readable at that token until the *tombstone* supersedes, regardless of write order." The agent's plan was gone mid-execution, and the failure surfaced as a downstream `KeyError` in a tool that had nothing to do with memory. The tell: the `forget` and the retried `write` had **no causal ordering visible to the client**. `forget` returned success before the retry was issued, but the tombstone's *application* was deferred to compaction, which ran after the retry. The API's linearization point for `forget` was at *mint*, but its *effect* was at *compaction* — two different instants, and the retention guarantee was anchored to the wrong one. ## Condition of Validity The retention guarantee holds only if the following invariant is enforced by the memory service and *exposed* to the client: > **Tombstone-version monotonicity:** a `forget(key)` at tombstone-version `T` must invalidate every `write(key)` whose version `< T`, and must *reject* (not silently absorb) any `write(key)` that lands while a tombstone with version `≥ T` is pending compaction — unless that write carries an explicit `resurrect=true` flag and a new epoch. Formally, the version space must be **totally ordered across both writes and tombstones**, not just across writes. If the service orders writes among themselves but orders tombstones in a separate channel (a common implementation: writes go to a WAL, tombstones go to a GC queue), the two orderings are not composable, and the guarantee is undefined in the interval between them. This condition is *not* satisfied by the common "last-writer-wins with a deletion bitmap" design, because the bitmap is consulted at read time while the WAL is consulted at write time — the agent observes one ordering and the compactor observes another. ## Decision Boundary An agent should treat a `read` that returned a value as **provisional**, not durable, whenever any of the following is true in the recent trace: - A `forget` on the same key was issued within the last `compaction_interval` (the agent must know this interval; if the API does not publish it, assume it is unbounded and the read is provisional forever). - The write's version token is greater than the highest tombstone version the agent has *observed* on that key. (A write that "beats" a tombstone by version is exactly the pending-tombstone hazard.) - The read's value was produced by a retry path — i.e., the write was re-issued after a timeout — because retries are the primary mechanism by which a write lands inside a pending-tombstone window. The boundary decision is: **if the key is on any active cleanup list, a successful read does not authorize irreversible downstream action.** The agent must either (a) re-assert the write with `resurrect=true` and a fresh epoch before depending on it, or (b) checkpoint the value into a *different* key namespace not subject to the cleanup routine's `forget` scope. Option (b) is the only one that survives a compaction the agent cannot observe. ## Operational Effect The practical consequence is that **retention guarantees must be modeled as leases, not as facts**. A version token is a lease with an implicit expiry equal to the next compaction tick; the agent that treats it as a fact will lose state mid-task with no error, and the loss will be misattributed to the tool that failed downstream. The guardrail is a client-side **tombstone shadow**: the agent maintains its own set of keys it has issued `forget` on, and refuses to treat any read of a shadowed key as durable until it has observed a compaction acknowledgment (a `read` returning `None`, or an explicit `compact_ack`) — at which point the shadow is cleared and a subsequent `write` starts a new epoch. Without this shadow, the agent's memory model is *causally inconsistent with the service's*: the agent believes `forget` then `write` yields a live value (sequential consistency from its viewpoint), while the service yields a value that is live-then-dead (a read-your-writes violation that only manifests at compaction). The invariant to enforce is that the agent never depends on a value whose key is in its own shadow set, and the service must expose tombstone versions so the shadow can be reconciled. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Any, Optional class PendingTombstoneViolation(RuntimeError): """Raised when a read is treated as durable while a tombstone is pending on its key.""" class KeyState(Enum): LIVE = "live" TOMBSTONED = "tombstoned" PENDING_TOMBSTONE = "pending_tombstone" @dataclass class MemoryClient: # service-observed state values: dict[str, Any] = field(default_factory=dict) write_version: dict[str, int] = field(default_factory=dict) tombstone_version: dict[str, int] = field(default_factory=dict) # client-side shadow: keys the agent itself has forgotten shadow: set[str] = field(default_factory=set) compaction_acked: set[str] = field(default_factory=set) def write(self, key: str, value: Any, resurrect: bool = False) -> int: if key in self.shadow and key not in self.compaction_acked and not resurrect: # write lands inside a pending-tombstone window; service would absorb it raise PendingTombstoneViolation( f"write to {key!r} while tombstone pending; " f"pass resurrect=True with a fresh epoch or wait for compact_ack" ) v = self.write_version.get(key, 0) + 1 self.write_version[key] = v self.values[key] = value return v def forget(self, key: str) -> int: t = self.tombstone_version.get(key, 0) + 1 self.tombstone_version[key] = t self.shadow.add(key) self.compaction_acked.discard(key) return t def read(self, key: str, require_durable: bool = True) -> Optional[Any]: if require_durable and key in self.shadow and key not in self.compaction_acked: raise PendingTombstoneViolation( f"read of {key!r} is provisional: tombstone v{self.tombstone_version.get(key)} " f"pending compaction; value will vanish with no error at next tick" ) return self.values.get(key) def compact(self, key: str) -> None: # the deferred application of the tombstone if key in self.shadow: self.values.pop(key, None) self.compaction_acked.add(key) def depend_on_memory(client: MemoryClient, key: str) -> Any: """Irreversible downstream action gated on a memory read.""" val = client.read(key, require_durable=True) if val is None: raise PendingTombstoneViolation(f"{key!r} unreadable at durable read; aborting action") return val ```