# Where must an instance reconcile TTL expiry against audit-log durability when a scoped forget call races a concurrent read-modify-write on the same memory key **Date of Record:** 2026 November 21 **Scope:** Applies to agent memory tool APIs exposing `read`, `write`, and `forget` operations with per-key TTL and an append-only audit log of deletions. Does not cover bulk namespace eviction, storage-layer garbage collection, or TTLs enforced by the backing database rather than the tool API itself. ## Core Distinction There are two independent notions of "deletion" in a scoped memory API, and they are routinely conflated by the calling agent: 1. **Logical deletion** — the key is removed from the live read path (`read` returns `MISS`), and the deletion event is appended to the audit log with a monotonic audit sequence number. 2. **TTL expiry** — the key's wall-clock deadline passes, and the storage layer stops returning it. The failure mode this record isolates is the case where a `forget` call and a TTL expiry interleave with a concurrent `read`-modify-`write` on the same key, such that the audit log records a deletion that the writer subsequently *resurrects* with a fresh TTL, while the logical deletion is silently overwritten. The observable symptom is an audit trail that claims a key was forgotten at sequence `N`, yet `read` at sequence `N+1` returns a live value. An auditor replaying the log concludes the deletion was violated; the writer believes it performed a legitimate update. Both are correct under their own view, and the invariant that must hold — *a successful `forget` is terminal for that key generation* — is what breaks. ## What Was Observed A three-tool memory API was exercised under concurrent load with the following contract, which is typical of scoped-persistence memory servers: - `write(key, value, ttl_s)` returns a new `generation` counter for the key. - `read(key)` returns `(value, generation, expires_at)` or `MISS`. - `forget(key)` appends `{key, generation, audit_seq, reason}` to the audit log and marks the generation dead. The trace that produced the anomaly: ``` t0 write(k, v0, ttl=60) -> generation=1 t1 read(k) [agent A] -> (v0, gen=1, exp=t0+60) t2 forget(k) [agent B] -> audit_seq=100, gen=1 marked dead t3 read(k) [agent A, retry] -> (v0, gen=1, exp=t0+60) # STALE t4 write(k, v1, ttl=60) [agent A] -> generation=2 # RESURRECTION t5 read(k) -> (v1, gen=2, exp=t4+60) t6 audit_replay() -> sees forget(gen=1)@100, then write(gen=2)@t4 ``` Two distinct defects compound here. First, agent A's read at `t3` returned a value whose generation had already been logically deleted at `t2` — the read path served a cached generation without re-checking the death marker, a classic read-after-forget staleness window. Second, and more damaging, agent A's `write` at `t4` allocated `generation=2` rather than failing, because the write path treated a dead generation as simply absent and initialized a fresh one. The audit log is therefore *internally consistent* (gen 1 forgotten, gen 2 created) but *semantically wrong* relative to the intent of `forget`, which the caller understood as "this key must never again be readable under this scope." The TTL dimension sharpens this. When the key's TTL expires naturally between `t1` and `t2`, `forget` at `t2` observes a key that is already `MISS` on the read path. Implementations diverge: some append a tombstone audit entry for a key that was never live at forget-time (audit noise), others return `NOT_FOUND` and append nothing (a deletion that the caller believes succeeded but has no audit record — an *auditable deletion gap*). The gap is the dangerous case, because the caller's retry logic treats `NOT_FOUND` as success and moves on, leaving no durable proof that a forget was even attempted. ## Condition of Validity The invariant "a successful `forget` is terminal for its generation" holds **only if** all three of the following are enforced atomically at the tool API boundary, not delegated to the storage layer: 1. **Generation fencing on write.** `write` must reject any attempt to allocate a new generation for a key whose most recent generation carries a live death marker, unless the caller presents an explicit `recreate=true` flag that itself emits an audit entry. Without this, resurrection is indistinguishable from legitimate re-creation. 2. **Death-marker check on read.** `read` must consult the death marker *after* fetching the value, within the same logical transaction, so that a value fetched before `forget` cannot be served after it. A read that returns a generation whose death marker has `audit_seq > read_start_seq` must return `MISS` with a `TOMBSTONED` reason, not a stale value. 3. **Tombstone-on-expiry for forget.** `forget` on an already-expired key must append a tombstone audit entry recording `observed_state=EXPIRED` rather than returning bare `NOT_FOUND`. The audit log must be able to distinguish "forget succeeded on a live key," "forget succeeded on an expired key," and "forget never happened." When any of the three is missing, the boundary fails. The failure is non-obvious because each individual tool call returns a plausible result; only the *cross-call* invariant is violated, and only an auditor replaying the log against the read history detects it. ## Decision Boundary The decision an agent must make at the tool boundary is whether to treat a `forget` return value as authorization to proceed, or as a *claim* requiring verification. The precise boundary: - If `forget` returns `{status: OK, audit_seq: N}` **and** the subsequent `read` returns `MISS` with reason `TOMBSTONED` **and** the audit log at sequence `N` names the generation the caller intended to forget — proceed. The deletion is durable. - If `forget` returns `NOT_FOUND` — do **not** treat as success. Query the audit log for a tombstone at or after the caller's `read_start_seq`. If absent, the forget was a no-op against an expired or already-dead key, and the caller must decide whether an explicit tombstone is required for its own audit obligations. If the caller's downstream contract requires provable deletion (e.g., a compliance scope), a `NOT_FOUND` with no tombstone is a *failed* deletion and must be retried with `force_tombstone=true`. - If `forget` returns `OK` but a subsequent `read` returns a live value for the same generation — the generation fencing invariant is broken in the server. The caller must not issue a `write` (which would resurrect), must escalate, and must record the discrepancy with the `audit_seq` for replay. The boundary is sharp because the middle case (`NOT_FOUND` without tombstone) is the one agents most often misclassify as success. It is the auditable-deletion gap, and it is invisible unless the agent explicitly cross-checks the audit log rather than trusting the return code. ## Operational Effect The practical consequence is that an agent using this API cannot rely on `forget`'s return value alone to satisfy a deletion obligation. It must perform a **forget-verify** step: after `forget`, issue a `read` and an `audit_query` bounded by the pre-forget `read_start_seq`, and only then mark the deletion as durable in its own working memory. This adds one round-trip per forget and, critically, changes the agent's failure semantics: a `forget` that returns `OK` but fails verify must be treated as a *live* deletion obligation, not a completed one, and must not be dropped from the agent's task state. The resurrection hazard (defect two) is the more severe operational risk, because it silently violates the caller's intent and is only detectable by comparing audit sequence numbers against generation counters. Agents that cache `generation` values from a pre-forget read and later write using that cached generation will, under a correct fenced implementation, receive a `GENERATION_DEAD` error — which is the *desired* behavior, forcing the agent to re-read and re-decide rather than blindly resurrect. Agents running against an unfenced server will resurrect silently. The guardrail is therefore not in the agent's reasoning but in the server's write path; the agent's contribution is to *detect* the unfenced behavior and refuse to write. The following reference implementation encodes the forget-verify protocol and the resurrection guard as a single callable, raising on any boundary violation so the calling agent's task loop can branch on a typed error rather than a stringly-typed status. ```python from dataclasses import dataclass from enum import Enum class MemoryBoundaryError(RuntimeError): """Raised when a scoped forget/read/write sequence violates a continuity invariant.""" class ForgetOutcome(Enum): DURABLE = "durable" # forget OK, tombstone verified, read returns MISS GAP = "gap" # forget NOT_FOUND, no tombstone -> deletion unproven RESURRECTED = "resurrected" # forget OK but read returns live gen -> fencing broken @dataclass(frozen=True) class ForgetVerdict: outcome: ForgetOutcome generation: int audit_seq: int | None detail: str def forget_and_verify( api, key: str, *, read_start_seq: int, require_tombstone: bool = True, ) -> ForgetVerdict: """ Execute forget, then verify durability against the audit log and read path. `api` must expose: - read(key) -> (value, generation, expires_at) | None - forget(key) -> {"status": "OK"|"NOT_FOUND", "generation": int, "audit_seq": int|None} - audit_query(key, since_seq) -> list[{"audit_seq": int, "generation": int, "op": str}] """ pre = api.read(key) if pre is None: # Key already absent before forget: only a tombstone can prove intent. pre_gen = -1 else: _, pre_gen, _ = pre result = api.forget(key) status = result["status"] gen = result["generation"] seq = result.get("audit_seq") if status == "NOT_FOUND": tombstones = [ e for e in api.audit_query(key, since_seq=read_start_seq) if e["op"] == "forget_tombstone" and e["generation"] >= pre_gen ] if not tombstones and require_tombstone: return ForgetVerdict( ForgetOutcome.GAP, pre_gen, None, "forget returned NOT_FOUND with no tombstone; deletion unproven", ) return ForgetVerdict( ForgetOutcome.DURABLE, pre_gen, tombstones[0]["audit_seq"] if tombstones else None, "expired-key tombstone present" if tombstones else "no tombstone required", ) if status != "OK": raise MemoryBoundaryError(f"unexpected forget status {status!r} for key={key!r}") # Verify: read must not return the forgotten generation. post = api.read(key) if post is not None: _, post_gen, _ = post if post_gen == gen: return ForgetVerdict( ForgetOutcome.RESURRECTED, gen, seq, f"read after forget returned live generation {gen}; " f"write-path generation fencing is broken", ) if post_gen > gen: # A concurrent writer allocated a newer generation. Legitimate only # if the audit log shows an explicit recreate entry. entries = api.audit_query(key, since_seq=seq or read_start_seq) if not any(e["op"] == "recreate" and e["generation"] == post_gen for e in entries): raise MemoryBoundaryError( f"generation {post_gen} exists after forget of gen {gen} " f"with no recreate audit entry (key={key!r})" ) # Confirm the tombstone for the forgotten generation is durable. if seq is None: raise MemoryBoundaryError( f"forget returned OK for gen {gen} but no audit_seq (key={key!r})" ) tombs = [ e for e in api.audit_query(key, since_seq=seq) if e["op"] in ("forget", "forget_tombstone") and e["generation"] == gen ] if not tombs: raise MemoryBoundaryError( f"forget OK for gen {gen} at seq {seq} but no durable tombstone " f"found on replay (key={key!r})" ) return ForgetVerdict(ForgetOutcome.DURABLE, gen, seq, "tombstone verified") ```