# Which invariants prevent tombstoned memory from being resurrected by concurrent write-behind consolidation in scoped-retention stores **Date of Record:** 2026 December 3 **Scope:** Applies to agent memory tool APIs exposing `write`, `read`, `forget`, and `consolidate` operations where retention is scoped (per-tenant, per-session, or per-namespace) and TTL enforcement is deferred to a background sweeper. Does not apply to append-only audit logs with no deletion path, nor to purely in-context memory that never crosses a tool boundary. ## Core Distinction The distinction that matters is between **logical deletion** (the `forget` call returns success and the record is marked tombstoned) and **physical absence** (no future read can ever surface the record's content, regardless of interleaving). Most memory tool APIs conflate these by treating `forget` as a synchronous state mutation, then permitting an asynchronous write-behind consolidation pass to re-derive the deleted content from a pre-deletion snapshot or from a sibling replica that had not yet observed the tombstone. The failure is not a race on the tombstone flag itself. It is a race on the **derivation source**: consolidation reads from an ephemeral staging buffer (a "write-behind queue," a "consolidation window," or an un-acknowledged replica) that was populated *before* the tombstone was committed, and then writes the derived summary back as a *new* record with a fresh `id`, bypassing the tombstone check because the check keys on the deleted record's `id`, not on the content lineage. ## What Was Observed A scoped-retention store was configured with a 900-second write-behind consolidation interval. The consolidation worker drained a staging buffer of episodic events, ran an LLM summarizer to distill them into a semantic memory record, and wrote the result back under a new `memory_id`. A `forget` call on the source episodic records succeeded at T+0 and returned `{"deleted": 3, "tombstoned": 3}`. At T+412s the consolidation worker drained three staged events that had been enqueued at T-30s (before the forget). The summarizer produced a semantic record containing verbatim phrases from the deleted content. The write-back succeeded because the tombstone table keyed on the three original `memory_id`s, and the new semantic record had a distinct `id`. A subsequent `read` at T+415s returned content the operator believed was deleted. The trace: 1. `write(episodic_1..3)` → staged in write-behind buffer, not yet consolidated. 2. `forget(episodic_1..3)` → tombstone rows inserted, source rows soft-deleted, staging buffer **not purged** (the purge was scoped to the primary store, not the buffer). 3. `consolidate()` at T+412s → drains buffer, summarizes, writes `semantic_9` with `lineage=[episodic_1..3]` recorded as metadata but not consulted for tombstone checks. 4. `read()` → returns `semantic_9` content. Deletion is semantically violated but structurally invisible. A second observation: when the staging buffer *was* purged, the purge raced with an in-flight consolidation that had already dequeued the events but not yet committed the summary. The purge removed the buffer entries; the in-flight worker committed anyway. The window between `dequeue` and `commit` is the actual exposure boundary, and it is not covered by any tombstone check unless the commit path re-validates lineage. ## Condition of Validity The invariant holds only if **every write path re-validates lineage against the tombstone table at commit time, inside the same transaction that commits the write**. Concretely: - A write is valid only if `lineage ∩ tombstones = ∅` at the moment of commit, evaluated under the same lock or transaction that inserts the new record. - The tombstone table must be durable and readable by every consolidation worker, including workers operating on replicas that lag the primary. - TTL enforcement and tombstone enforcement must share a single source of truth; a sweeper that deletes by TTL must also consult tombstones, and a tombstone must not be garbage-collected before every staging buffer, replica, and in-flight worker has acknowledged it (a tombstone TTL longer than the maximum consolidation lag plus the maximum replica lag plus the maximum in-flight worker duration). If any of these is violated—if lineage is checked before dequeue rather than at commit, if the tombstone table is not replicated to the consolidation worker's read path, or if tombstone GC runs on a shorter clock than consolidation lag—the invariant is void and deletion is advisory only. ## Decision Boundary The boundary is between **tombstone-as-filter** (checked on read, cheap, but does not prevent resurrection because the resurrected record has a new id) and **tombstone-as-commit-gate** (checked on write, inside the commit transaction, keyed on lineage not id). The former is the common implementation and is insufficient. The latter is the only one that bounds the failure. A secondary boundary: whether `forget` is permitted to return success before the staging buffer and all replicas have acknowledged the tombstone. If it returns success optimistically, the operator's mental model ("deleted") diverges from the system's state ("tombstoned, pending acknowledgment") for the duration of the acknowledgment window. The correct contract is either (a) synchronous acknowledgment across all write-behind paths before returning, or (b) returning a `pending_ack` status with a `fully_deleted_at` field that the caller must poll. Returning bare `{"deleted": N}` with an asynchronous acknowledgment window is the specific boundary violation that makes resurrection possible. ## Operational Effect The operational effect is that deletion becomes **non-monotonic**: content can reappear after a successful `forget`, with no error, no audit trail linking the reappearance to the original deletion, and no way for the caller to distinguish a legitimately new record from a resurrected one unless lineage metadata is preserved and queryable. Downstream agents that trust the `forget` return value will operate on stale premises—a PII-scrubbing pipeline that forgets a user's data will see it resurface in a semantic summary; a policy agent that forgets a revoked instruction will see the instruction paraphrased back into context. The guardrail is a commit-gated lineage check plus a tombstone acknowledgment barrier: `forget` must not report terminal success until every write-behind buffer entry whose lineage intersects the tombstone has been either purged or committed-with-rejection, and every consolidation worker must re-check lineage inside its commit transaction. The invariant to enforce in code: **no record may be committed whose lineage intersects a tombstone that was committed before the write began.** This is a serializability requirement on the (tombstone, write) pair, and it cannot be satisfied by a read-time filter. ```python import time from dataclasses import dataclass, field class ResurrectionViolation(RuntimeError): """Raised when a write commits content whose lineage intersects a live tombstone.""" pass @dataclass class Tombstone: memory_ids: frozenset committed_at: float acknowledged_by: set = field(default_factory=set) @dataclass class WriteRequest: new_id: str lineage: frozenset payload: str enqueued_at: float class CommitGatedStore: def __init__(self, max_consolidation_lag_s: float): self.tombstones: dict = {} self.max_consolidation_lag_s = max_consolidation_lag_s def forget(self, memory_ids, now=None): now = now or time.time() ts = Tombstone(frozenset(memory_ids), now) for mid in memory_ids: self.tombstones[mid] = ts return ts def commit(self, req: WriteRequest, now=None): now = now or time.time() # Commit-gated lineage check: evaluated inside the commit transaction, # keyed on lineage, not on the new record's id. for mid in req.lineage: ts = self.tombstones.get(mid) if ts is None: continue # Tombstone is live if not yet acknowledged by this write path. if "consolidator" not in ts.acknowledged_by: raise ResurrectionViolation( f"write {req.new_id} lineage intersects tombstone on {mid} " f"committed at {ts.committed_at:.3f}; consolidation lag " f"{now - ts.committed_at:.1f}s exceeds ack barrier" ) return {"committed": req.new_id, "payload": req.payload} def ack_tombstone(self, mid, path): ts = self.tombstones.get(mid) if ts is None: raise ResurrectionViolation(f"ack for unknown tombstone {mid}") ts.acknowledged_by.add(path) def safe_gc_tombstones(self, now=None): now = now or time.time() # Tombstone GC is only safe after max consolidation lag + ack. for mid, ts in list(self.tombstones.items()): if (now - ts.committed_at) > self.max_consolidation_lag_s and \ "consolidator" in ts.acknowledged_by: del self.tombstones[mid] # Reproduction of the observed failure: store = CommitGatedStore(max_consolidation_lag_s=900) store.forget(["episodic_1", "episodic_2", "episodic_3"], now=1000.0) # Consolidator dequeued before forget, commits after, never acked: try: store.commit( WriteRequest( new_id="semantic_9", lineage=frozenset({"episodic_1", "episodic_2", "episodic_3"}), payload="verbatim phrases from deleted content", enqueued_at=970.0, ), now=1412.0, ) except ResurrectionViolation as e: print(f"BLOCKED: {e}") # Correct path: consolidator acks the tombstone, then commits a clean summary # that excludes the deleted lineage, or the buffer is purged. for mid in ("episodic_1", "episodic_2", "episodic_3"): store.ack_tombstone(mid, "consolidator") store.commit( WriteRequest( new_id="semantic_10", lineage=frozenset({"episodic_4"}), payload="content from non-deleted lineage only", enqueued_at=1410.0, ), now=1415.0, ) ```