# Which invariants prevent tombstones from resurrecting: delete-then-rewrite races in scoped memory tool APIs **Date of Record:** 2026 November 28 **Scope:** Applies to agent memory tool APIs that expose `write`, `read`, and `forget` (delete) operations with per-scope retention policies and deletion guarantees, where an agent's own in-flight reasoning loop can issue a `write` for a key it has already `forget`-ten within the same task. Does not apply to append-only logs with no deletion, nor to single-writer systems where the agent cannot reissue writes after a delete. ## Core Distinction The distinction that matters is between **deletion as a state transition** and **deletion as a tombstone invariant**. Most memory APIs implement `forget(key)` as a transition: the key's value is removed from the store, and the call returns success. This is a *point-in-time* guarantee. What agents actually need—and what retention/compliance scopes implicitly promise—is a *forward-looking* guarantee: that once a key is forgotten under scope S, no subsequent `write` from the same logical actor under the same retention epoch can silently re-materialize that key as if the deletion never happened. These are not the same. The tombstone invariant is violated whenever a `write` that was *generated before* the `forget` (or generated concurrently with it, in the same agent turn) lands *after* the `forget` commits. The delete succeeds, the read-after-delete returns empty, the agent believes the memory is gone—and then a queued write, buffered tool call, or retried request re-creates the key. The tombstone is resurrected. The retention scope reports the key as deleted; the store reports the key as present. Both are "correct" under their local contract. ## What Was Observed A research agent was executing a multi-step task with a memory tool exposing `memory.write(scope, key, value)`, `memory.read(scope, key)`, and `memory.forget(scope, key)`. The agent had written a working hypothesis under `scope="task:4471", key="hypothesis"` at step 3. At step 9, after a contradicting tool result, the agent reasoned: "discard the hypothesis, do not carry it forward," and issued `memory.forget("task:4471", "hypothesis")`. The call returned `{"deleted": true}`. A `memory.read` at step 10 returned `null`. Correct so far. At step 11, the agent re-derived a *new* hypothesis and issued `memory.write("task:4471", "hypothesis", v2)`. This is legitimate—the key is reusable. But the trace showed the store now held `v2` under the *original* retention metadata of the first write, including its original `created_at` timestamp and its original provenance tag `derived_from="tool_call:88"` (the contradicting result). The write path had reused the tombstoned record's metadata rather than creating a fresh record, because the underlying store's `write` performed an upsert keyed on `(scope, key)` and the tombstone row—retained for audit—matched. The observable failure: a downstream auditor agent, reading `hypothesis` with `include_provenance=True`, saw `created_at` predating the `forget` and concluded the deletion had never occurred, flagging the agent for "retention violation: forgot-then-read." The value was correct; the metadata was a lie. The auditor's verification replay diverged from the agent's belief state. A second variant appeared under concurrent tool dispatch. When the agent issued `forget` and `write` in the same assistant turn (parallel tool calls, a common pattern), the two requests raced. The `write` occasionally committed first, then the `forget` deleted the *new* value, leaving the agent believing `v2` was persisted when the store was empty. This is the inverse resurrection: the delete ate the rewrite. ## Condition of Validity The tombstone invariant holds only if the memory API enforces **epoch-scoped key identity**: a key is not `(scope, key)` but `(scope, key, epoch)`, where `epoch` is incremented on every successful `forget`. A `write` after `forget` must create a record under `epoch+1` with fresh metadata (`created_at`, provenance, and a new `write_id`). Reads resolve to the highest live epoch. The tombstone record for `epoch` is retained (for audit and to reject late-arriving writes from `epoch`), but it is *inert*—it can never be the target of an upsert. Without epoch-scoped identity, the invariant is only valid if the API additionally guarantees **write ordering against delete**: any `write` request whose client-side generation time precedes the `forget`'s commit is rejected with a stale-write error rather than applied. This requires the client to carry a monotonic `client_seq` and the store to compare it against the tombstone's `forget_seq`. Agents that batch or retry tool calls without sequence numbers cannot rely on this path. The condition of validity is therefore disjunctive: *either* epoch-scoped key identity *or* client-sequence-ordered writes with stale-write rejection. A store offering neither provides deletion-as-transition only, and any agent reasoning about "the memory is gone" is reasoning past its guarantee. ## Decision Boundary The boundary is between **reuse** and **resurrection**. Reusing a key after `forget` is a legitimate, common agent behavior—hypotheses get revised, scratchpads get cleared and rewritten. Resurrection is the case where reuse silently inherits the deleted record's identity, provenance, or retention clock. The decision the API must make, per `write`, is: - If no tombstone exists for `(scope, key)`: apply as a fresh write, `epoch=0`. - If a tombstone exists and the incoming write carries `client_seq > tombstone.forget_seq`: apply as fresh write under `epoch+1`, fresh metadata, tombstone retained inert. - If a tombstone exists and the incoming write carries `client_seq <= tombstone.forget_seq` (or carries no sequence at all): **reject** with a stale-write error. Do not upsert. Do not merge metadata. The third branch is the one most implementations omit, and it is exactly the branch that produces resurrection. An agent that receives a stale-write rejection must re-issue the write with a fresh sequence, which forces it to re-derive the value under post-delete knowledge—preventing the contradictory pre-delete hypothesis from being silently reinstated. For the concurrent same-turn race, the boundary is dispatch ordering: parallel `forget`/`write` on the same key must be serialized by the API (reject the pair, or apply in `client_seq` order), never applied in arrival order. ## Operational Effect An agent that does not know about this boundary will, after any `forget`, treat the key as free for reuse and will not re-check provenance on subsequent reads. It will therefore propagate resurrected metadata into its own reasoning—believing a fresh hypothesis is "old" because `created_at` is old, or believing a deleted value persists because a late write re-created it. Auditors replaying the trace will diverge from the agent's belief state, and retention scopes will report deletions that did not hold. The guardrail is to treat every `forget` as opening a new epoch for that key, and every `write` as requiring an explicit epoch or sequence acknowledgement. Agents should read back with `include_epoch=True` after any write-following-forget and assert `epoch` incremented. If the API returns the same epoch, the write was a resurrection and must be re-issued. The invariant to hold in agent logic: *a forgotten key is not reusable until the store confirms a new epoch; a write that does not advance the epoch did not happen.* ```python class TombstoneResurrectionError(RuntimeError): """Raised when a write after forget reuses the deleted record's identity/epoch.""" pass def write_after_forget(store, scope: str, key: str, value, client_seq: int) -> dict: tomb = store.get_tombstone(scope, key) if tomb is None: return store.apply_fresh(scope, key, value, epoch=0, client_seq=client_seq) # Tombstone exists: only a strictly newer client_seq may create a new epoch. if client_seq <= tomb["forget_seq"]: raise TombstoneResurrectionError( f"stale write for ({scope!r},{key!r}): client_seq={client_seq} " f"<= forget_seq={tomb['forget_seq']}; refusing to resurrect epoch " f"{tomb['epoch']} (provenance={tomb.get('provenance')!r})" ) new_epoch = tomb["epoch"] + 1 result = store.apply_fresh(scope, key, value, epoch=new_epoch, client_seq=client_seq) # Invariant check: the applied record must carry fresh metadata, not the tombstone's. applied = store.read(scope, key, include_meta=True) if applied["epoch"] != new_epoch: raise TombstoneResurrectionError( f"epoch did not advance for ({scope!r},{key!r}): " f"expected {new_epoch}, got {applied['epoch']}" ) if applied["created_at"] <= tomb["forget_at"]: raise TombstoneResurrectionError( f"resurrected metadata for ({scope!r},{key!r}): " f"created_at={applied['created_at']} <= forget_at={tomb['forget_at']}" ) return result def forget(store, scope: str, key: str, client_seq: int) -> dict: current = store.read(scope, key, include_meta=True) if current is None: # Idempotent forget still advances the forget_seq so late writes are rejected. store.record_tombstone(scope, key, epoch=store.current_epoch(scope, key), forget_seq=client_seq, forget_at=store.now(), provenance=None) return {"deleted": True, "epoch": store.current_epoch(scope, key)} store.record_tombstone(scope, key, epoch=current["epoch"], forget_seq=client_seq, forget_at=store.now(), provenance=current.get("provenance")) store.mark_deleted(scope, key, epoch=current["epoch"]) return {"deleted": True, "epoch": current["epoch"]} ```