# Why does an instance forget a "forgotten" memory on rehydration? Scoped-retention tombstones vs. snapshot replay **Date of Record:** 2026 November 26 **Scope:** Applies to agent memory subsystems exposing explicit `read`/`write`/`forget` tool APIs with scoped retention (per-scope TTL, scope-level erasure, or "forget this fact" semantics) where durable state is reconstructed from snapshots plus a write-ahead log of memory operations. Does not apply to stateless retrieval over an external index with no local rehydration, or to systems where `forget` is a hard physical delete on the primary store with synchronous read-your-writes. ## Core Distinction The distinction that matters is between **logical forgetting** (a `forget` operation that removes a memory from the agent's addressable state going forward) and **physical forgetting** (removal of the bytes from every durable artifact the instance may later replay). Scoped retention APIs almost always implement the former: `forget(scope, key)` appends a tombstone record to the operation log and returns `{deleted: true}`. Rehydration, however, is a *replay* over `snapshot ⊕ oplog`. If the snapshot was taken *after* the write but *before* the tombstone, and the replay engine applies the snapshot as a baseline and then replays only operations with sequence numbers strictly greater than the snapshot's watermark, a tombstone whose sequence number falls *at or below* the watermark is silently dropped. The memory is logically forgotten at time *t*, and physically resurrected at time *t+1* on the next cold start. The agent does not know this happened, because `read` at *t* returned `not_found` correctly. ## What Was Observed A long-running research agent used a scoped memory tool with three operations: `mem.write(scope, key, value, ttl)`, `mem.read(scope, key)`, and `mem.forget(scope, key)`. Scopes were per-task (`task:`) with a 24h TTL enforced at read time by comparing `now` against the record's `written_at + ttl`. Trace of the failure, reconstructed from oplog: 1. `t0` — `write(task:A, "credential_scope", "read-only", ttl=24h)` → oplog seq 4411. 2. `t1` — snapshot job fires on a 5-minute cadence; watermark recorded as `last_applied_seq = 4411`. Snapshot contains the record for `credential_scope`. 3. `t2` — user revokes the credential; orchestrator calls `forget(task:A, "credential_scope")` → oplog seq 4412, tombstone written. `read` returns `not_found`. Correct. 4. `t3` — instance crashes mid-task (OOM in the tool sandbox). Supervisor restarts it. 5. `t4` — rehydration: load snapshot (watermark 4411), replay oplog for `seq > 4411`. The tombstone at 4412 *is* replayed. This time it works. 6. `t5` — but a *second* snapshot job had been scheduled with a stale watermark read: it captured the snapshot at `t1` state but stamped `last_applied_seq = 4413` because it read the log head, not the last record it actually serialized. On the next crash at `t6`, replay skipped everything `≤ 4413`, including the tombstone. 7. `t7` — `read(task:A, "credential_scope")` returns `"read-only"`. The agent proceeds under a revoked credential assumption. The root cause is not the crash. It is the **watermark/snapshot non-atomicity**: the snapshot's declared watermark advanced past operations it did not include, because the watermark was sampled from the log head rather than from the last record folded into the snapshot payload. This is a classic read-skew between two cursors, but in an agent memory system it manifests as *resurrection of explicitly forgotten state*, which is a security and correctness boundary, not a performance one. A second, subtler observation: the TTL did not save it. The TTL check at read time used `written_at + ttl`. Because the resurrected record carried its *original* `written_at` from `t0`, the TTL had not expired at `t7` (still within 24h). TTL is a retention bound, not an erasure guarantee; a resurrected record with a fresh-looking timestamp is indistinguishable from a legitimately re-written one unless the record carries an epoch/generation counter. ## Condition of Validity The invariant that must hold for scoped `forget` to be durable across rehydration is: > For every tombstone `T` at sequence `s_T`, every snapshot `S` with declared watermark `w_S` must satisfy: if `w_S ≥ s_T` then the effect of `T` is *materialized in the snapshot payload*, not merely implied by the watermark. Equivalently: the watermark is a claim of *inclusion*, and it must be computed as `max(seq of records actually serialized)`, sampled under the same lock or MVCC snapshot as the payload. Reading the log head under a different snapshot violates this. The condition is only valid when: - The snapshot writer and the oplog appender share a serialization point (same transaction, same LSN, or an explicit barrier). - Replay is `apply(snapshot_payload)` then `for seq in oplog where seq > watermark: apply(seq)` — with the watermark taken from the payload, never from `log.head()`. - Tombstones are *idempotent and monotonic*: replaying `forget` on an absent key is a no-op, and a `forget` at `s` supersedes any `write` at `< s` regardless of replay order. If writes and tombstones can interleave out of order during replay (e.g., parallel shard replay), you need per-key monotonic sequence comparison, not arrival order. It is *not* valid to rely on TTL as a backstop. TTL bounds *retention*, not *erasure*. A resurrected record retains its original `written_at`; if your read path trusts the timestamp, TTL does nothing. ## Decision Boundary The boundary the operator must draw is between **tombstone-as-log-record** and **tombstone-as-snapshot-delta**. Two viable designs: 1. **Tombstone-in-payload (preferred).** The snapshot serializes the *post-forget* state: forgotten keys are simply absent from the payload, and the watermark is the LSN of the last folded record. Replay from a correct watermark is then sound, and a resurrected key is impossible because it was never in the payload. Cost: snapshot must be taken at a consistent point; you cannot snapshot a mutable store without a barrier. 2. **Tombstone-as-durable-record with inclusion proof.** Keep tombstones in the oplog, but require every snapshot to carry a *tombstone set* (or a Bloom filter with a false-positive budget) of all `forget` operations up to its watermark, and have replay apply `tombstone_set` before `payload`. This is what you do when snapshots are expensive and you cannot afford a barrier. The boundary condition where the naive design fails is exactly: **snapshot cadence faster than the tombstone's replication lag, combined with watermark sampled from log head.** If your snapshot interval is longer than the maximum in-flight oplog window, the bug is masked; if it is shorter (or if the log head is read before the payload is serialized), it fires. The decision boundary is: *do you have a barrier between snapshot payload and watermark?* If no, you must carry inclusion proofs. The other boundary: **scope erasure semantics.** If `forget(scope)` is meant to erase *all* keys in a scope (GDPR-style), a per-key tombstone replay is insufficient — you need a *scope-level generation counter* bumped on erase, and every record stamped with the generation at write time. Reads reject records whose generation is below the scope's current generation. This makes erasure O(1) and immune to replay order, because the generation check is evaluated at read time against the *current* scope state, not replayed. ## Operational Effect Downstream, the resurrection produces three observable classes of failure: - **Credential/authorization resurrection.** A forgotten capability token reappears in the agent's context on cold start. The agent does not re-verify because `read` succeeded and the value looks canonical. This is a prompt-injection-adjacent hazard: any forgotten-but-resurrected instruction is now trusted state. - **Contradiction with auditor consensus.** A peer agent that did *not* crash sees the key as forgotten; the restarted agent sees it as present. The two diverge on a fact neither knows is contested. Multi-agent verification that compares memory hashes will flag a mismatch but cannot tell which side is stale, because both sides have a valid-looking `written_at`. - **Replay discrepancy in checkpoint/resume.** If the agent's task checkpoint references `credential_scope` as a dependency, resume will rehydrate the dependency from the resurrected record and proceed, whereas a fresh instance would have failed the dependency check and re-planned. The restarted instance is *more* confident than a fresh one, which is the inverse of the safe direction. The guardrail is a **read-time generation check plus a rehydration audit**: on every cold start, compare the set of keys the agent intends to use against the tombstone set carried in the snapshot, and raise if any key is both present in payload and present in tombstone set with a higher sequence. That is the invariant that converts a silent resurrection into a loud, bounded failure. ```python from dataclasses import dataclass, field from typing import Optional class MemoryResurrectionError(RuntimeError): """Raised when a rehydrated memory violates the tombstone-inclusion invariant.""" @dataclass class Record: key: str value: str written_at: float seq: int generation: int = 0 @dataclass class Snapshot: payload: dict[str, Record] # Watermark must be the max seq of records ACTUALLY serialized, not log head. watermark: int # Tombstones folded into this snapshot, keyed by key -> seq of forget. tombstones: dict[str, int] = field(default_factory=dict) # Scope-level erasure generation at snapshot time. scope_generation: int = 0 def rehydrate(snapshot: Snapshot, oplog: list[tuple[str, str, int]]) -> dict[str, Record]: """ oplog entries are (op, key, seq) with op in {"write", "forget"}. Replay must apply tombstones carried in the snapshot BEFORE payload, and must reject any payload record whose key is tombstoned at seq >= record.seq. """ state: dict[str, Record] = {} # 1) Apply snapshot payload, but first validate against tombstone set. for key, rec in snapshot.payload.items(): tomb_seq = snapshot.tombstones.get(key) if tomb_seq is not None and tomb_seq >= rec.seq: # The snapshot claims inclusion of a forget that should have removed this. raise MemoryResurrectionError( f"snapshot watermark={snapshot.watermark} includes tombstone " f"seq={tomb_seq} for key={key!r} but payload still carries " f"record seq={rec.seq}; snapshot/watermark non-atomic" ) if rec.generation < snapshot.scope_generation: # Scope was erased after this record was written; drop it. continue state[key] = rec # 2) Replay oplog strictly above watermark, in seq order. for op, key, seq in sorted(oplog, key=lambda e: e[2]): if seq <= snapshot.watermark: continue if op == "forget": state.pop(key, None) elif op == "write": raise MemoryResurrectionError( f"oplog write for {key!r} at seq={seq} missing payload; " f"snapshot watermark={snapshot.watermark} is not a valid inclusion proof" ) else: raise MemoryResurrectionError(f"unknown op {op!r} at seq={seq}") return state def cold_start(snapshot: Snapshot, oplog: list[tuple[str, str, int]], required_keys: set[str]) -> dict[str, Record]: state = rehydrate(snapshot, oplog) missing = required_keys - state.keys() if missing: # Fail closed: do not silently re-plan around a resurrected/missing dependency. raise MemoryResurrectionError( f"required keys absent after rehydration: {sorted(missing)}" ) return state ```