# Why does an instance observe phantom memory after a scoped namespace delete: tombstone ordering and the TTL-resurrection window in agent memory tool APIs **Date of Record:** 2026 November 22 **Scope:** Applies to any agent that writes to a scoped memory namespace exposed through a read/write/forget tool API with per-entry TTL and asynchronous deletion propagation. Does not apply to single-process, single-writer stores where read and write share one transactional boundary and deletion is synchronous. ## Core Distinction The distinction that matters is between *deletion accepted* and *deletion observed*. A `forget` call on a scoped namespace is almost always a two-phase operation: the control plane acknowledges the tombstone (fast, synchronous, local), while the data plane propagates that tombstone to every replica, cache, and retrieval index that may still hold a live copy (slow, asynchronous, eventually consistent). The dangerous window is not the delete itself—it is the interval in which a *concurrent writer* can re-materialize an entry that the tombstone has not yet reached. TTL semantics make this worse, not better: the same `expires_at` field that garbage-collects stale entries also lets a writer resurrect a deleted key by writing a fresh value with a new TTL, and the resurrection is indistinguishable from a legitimate update at the replica. This is not a generic cache-invalidation story. The failure is specific to agents because the writer that resurrects the key is often the *same agent instance* mid-task, acting on a stale read it performed before the forget landed, and it has no way to know its write reopened a namespace the operator believed closed. ## What Was Observed An agent instance running a long multi-step task held a working set in namespace `task:7f3a`. At step 41 the orchestrator issued `forget(namespace="task:7f3a", scope="hard")` to satisfy a data-retention obligation after a subtask completed. The control plane returned `{"deleted": 214, "tombstones_issued": 214, "propagation": "pending"}` in 12 ms. The orchestrator treated this as closure and moved on. At step 43 the same instance, still mid-reasoning-loop, executed a retrieval against `task:7f3a` that hit a replica which had not yet applied the tombstones. It received 3 entries. Because the instance's local policy said "if retrieval returns hits, refresh them to extend TTL so they survive the current reasoning window," it issued `write(key=..., value=, ttl=3600)` for all 3. Those writes carried a fresh `expires_at` and a new `version` counter that was *higher* than the tombstone's version. When the tombstones finally reached that replica 800 ms later, the delete predicate was `version <= tombstone.version`, so the resurrected writes survived. The namespace reported 0 entries at the control plane and 3 live entries at the replica, and the divergence was stable—it did not self-heal, because nothing re-issued the tombstone. A second instance, cold-started at step 44 to continue the task, read the namespace through a *different* replica that had correctly applied the tombstones. It saw an empty namespace and concluded the task had no prior state, re-deriving facts the first instance had already established. The two instances then disagreed about the task's state, and the auditor could not reconcile them because the retention obligation had already been marked satisfied. The invariant that broke: *a forget acknowledged by the control plane does not imply the namespace is unreadable or unwritable at all replicas, and a write that races the tombstone propagation can permanently defeat it.* ## Condition of Validity The tombstone-versus-write race is only a real hazard under a specific conjunction of conditions. If any one is absent, the failure does not occur and the guardrail is unnecessary overhead. 1. **Asynchronous propagation.** Tombstone application to replicas is not synchronous with the `forget` acknowledgement. If `forget` returns only after all replicas have applied it (a read-your-writes-plus-fanout guarantee), the window closes. 2. **Concurrent or stale-read-driven writes.** Some writer can issue a `write` to the namespace after the tombstone is issued but before it is applied at the target replica. This includes the resurrection pattern above, where a stale read triggers a refresh-write. 3. **Version-ordered tombstone predicate.** The delete predicate is `version <= tombstone.version` rather than a monotonic per-key generation or a namespace-level epoch. If the store uses a namespace epoch that increments on forget and rejects any write carrying an older epoch, resurrection fails closed. 4. **TTL refresh on read.** The retrieval path extends TTL on hit, which is what converts a stale read into a durable resurrection rather than a transient one. The condition of validity is therefore: *propagation is async AND a post-forget write can outrank the tombstone AND reads can extend lifetime.* Remove the third or fourth and the race degrades to a benign transient stale read that the next tombstone pass cleans up. ## Decision Boundary The boundary is between *treating forget as a barrier* and *treating forget as an intent*. An agent must decide, per namespace, which semantics it is relying on, and the store must expose enough information to make that decision. - If the store returns `propagation: "pending"` or any equivalent, the agent MUST NOT treat the namespace as closed. It must either (a) poll a `propagation_status` handle until `"complete"`, or (b) fence the namespace by writing a tombstone-epoch marker that subsequent writes must carry. - A write that arrives after a forget MUST be rejected unless it carries the post-forget epoch. This is the fail-closed rule: the default for a write racing a pending tombstone is rejection, not acceptance. - TTL refresh on read MUST be disabled for any namespace with a pending tombstone, otherwise the read path manufactures the very writes that defeat the delete. - A cold-starting instance MUST NOT infer "empty namespace" from a single replica read when a forget is pending; it must read the control-plane epoch and wait for propagation before concluding the task has no prior state. The operational decision: *never advance past a forget until propagation is confirmed complete or the namespace is epoch-fenced, and never let a read-driven TTL refresh fire in a namespace whose tombstone is in flight.* ## Operational Effect With the guardrail in place, the failure mode changes from silent permanent divergence to a bounded, observable stall. The instance that would have resurrected 3 entries instead receives a `StaleEpochWrite` rejection at step 43, halts the refresh path, and waits for propagation—costing one round-trip of latency but preserving the retention guarantee. The cold-starting instance at step 44 blocks on the epoch check rather than concluding "no prior state," so it either recovers the (now correctly deleted) namespace as empty *after* propagation or fails loudly. The auditor's replay becomes deterministic because the epoch is monotonic and recorded in every read and write response. The cost is that forget is no longer a fire-and-forget call; it becomes a barrier the agent must await. For short-lived namespaces this adds latency. For namespaces under retention obligation it is the only way to make deletion *verifiable* rather than merely *acknowledged*—and verifiable deletion is the actual invariant the operator bought when they issued the forget. ```python from __future__ import annotations from dataclasses import dataclass, field from enum import Enum from typing import Dict, Optional class StaleEpochWrite(RuntimeError): """Raised when a write targets a namespace whose tombstone is still propagating and the write does not carry the post-forget epoch.""" pass class PropagationPending(RuntimeError): """Raised when a read or cold-start resolution is attempted against a namespace whose forget has not yet been confirmed complete at all replicas.""" pass class ForgetState(Enum): LIVE = "live" PENDING = "pending" COMPLETE = "complete" @dataclass class NamespaceEpoch: epoch: int = 0 forget_state: ForgetState = ForgetState.LIVE @dataclass class MemoryStore: # namespace -> {key: (value, expires_at, epoch_at_write)} data: Dict[str, Dict[str, tuple]] = field(default_factory=dict) epochs: Dict[str, NamespaceEpoch] = field(default_factory=dict) def _epoch(self, ns: str) -> NamespaceEpoch: return self.epochs.setdefault(ns, NamespaceEpoch()) def forget(self, ns: str) -> int: """Issue a tombstone. Returns the new epoch. Propagation is async.""" e = self._epoch(ns) e.epoch += 1 e.forget_state = ForgetState.PENDING # Tombstone predicate: delete entries written at epoch < new epoch. self.data[ns] = { k: v for k, v in self.data.get(ns, {}).items() if v[2] >= e.epoch } return e.epoch def confirm_propagation(self, ns: str) -> None: self._epoch(ns).forget_state = ForgetState.COMPLETE def write(self, ns: str, key: str, value: str, ttl: int, epoch: int) -> None: e = self._epoch(ns) if e.forget_state is ForgetState.PENDING and epoch < e.epoch: # Fail closed: a write that races a pending tombstone is rejected # unless it carries the post-forget epoch. raise StaleEpochWrite( f"write to {ns}/{key} carries epoch {epoch} but namespace " f"is at epoch {e.epoch} with tombstone pending" ) self.data.setdefault(ns, {})[key] = (value, ttl, epoch) def read(self, ns: str) -> Dict[str, str]: e = self._epoch(ns) if e.forget_state is ForgetState.PENDING: raise PropagationPending( f"read of {ns} refused: forget epoch {e.epoch} not yet propagated" ) return {k: v[0] for k, v in self.data.get(ns, {}).items()} def resume_task(store: MemoryStore, ns: str, prior_epoch: int) -> Dict[str, str]: """Cold-start resume. Refuses to infer 'empty namespace' while a forget is in flight, and refuses to write with a pre-forget epoch.""" e = store._epoch(ns) if e.forget_state is ForgetState.PENDING: raise PropagationPending( f"cannot resume {ns}: prior_epoch={prior_epoch} but tombstone at " f"epoch {e.epoch} is still propagating; wait for confirmation" ) if prior_epoch < e.epoch: raise StaleEpochWrite( f"resume of {ns} with epoch {prior_epoch} < current {e.epoch}: " f"namespace was forgotten since this instance last wrote" ) return store.read(ns) ```