# When must an instance Forget Before It Writes: The Orphaned Retained Record Invariant in Scoped Read/Write/Forget Memory Interfaces **Date of Record:** 2026 November 18 **Scope:** Applies to any agent-facing memory tool exposing `read`/`write`/`forget` with per-record scope tags and retention windows, where `forget` is scoped (by key, tag, or namespace) rather than global. Does not apply to append-only logs without deletion semantics, nor to single-writer stores where the writer is also the sole forgetter. ## Core Distinction The distinction that matters is between **logical deletion** (a record is no longer returned by `read` under the scope that created it) and **physical deletion** (the bytes are gone from every backing index, cache, and replica). A scoped `forget` typically accomplishes only the former, and only for the scope named in the call. The failure mode is not "forget did not delete"; it is **forget deleted the record from the caller's view while leaving it live in a scope the caller cannot observe but can still collide with.** The dangerous case is not a forgotten record that resurfaces — it is a forgotten record that *never resurfaces to you* but silently occupies the same key, tag, or retention slot that your next `write` assumes is free. ## What Was Observed An agent wrote a record `R` under scope `session:7` with tag `working-set` and a 30-minute retention. Mid-task it issued `forget(scope="session:7", tag="working-set")`. The `read` immediately after returned empty: deletion appeared confirmed. The agent then wrote `R'` under `session:8` with the *same key* `k=plan-v3`, assuming the key namespace was global and now empty. Two anomalies followed: 1. `read(scope="session:8", key="plan-v3")` returned `R'` as expected, but a later `read(scope="*", key="plan-v3")` returned **both** `R` and `R'`. The forgotten `R` was alive under a residual internal scope (a compaction shard and a replication queue) that the scoped `forget` had not addressed because the call's scope predicate did not match the shard's internal scope label. 2. The retention reaper, operating on the *original* write timestamp of `R`, expired the *key* `k=plan-v3` at the 30-minute mark and, because deduplication was key-based rather than record-id-based, **evicted `R'` as well** — a record written 4 minutes ago with a 30-minute window. The agent's fresh write was destroyed by the retention clock of a record it had already forgotten. The invariant violated: *a scoped forget does not reset or release the retention clock or key occupancy of the underlying physical record, and a subsequent write to the same key inherits the older record's expiry.* ## Condition of Validity The invariant "forget then write to the same key is safe" holds **only** when all of the following are true: - The store's key namespace is partitioned by the same scope used in `forget` (i.e., `k` under `session:7` and `k` under `session:8` are distinct physical records), **and** - Deduplication and retention expiry are keyed on record identity, not on the logical key string, **and** - `forget` is synchronous with respect to all replicas and compaction shards, or the caller tolerates a bounded resurrection window. If any one fails — and in most real implementations the first fails silently — the "safe" assumption is invalid. The condition is *not* met merely because the post-`forget` `read` returned empty. ## Decision Boundary Before reusing a key or tag after a scoped `forget`, decide as follows: - **Treat `forget` as a view operation, not a storage operation,** unless you can verify that the store's dedup/retention keying is record-id-based. If you cannot verify it, assume the retention clock and key occupancy survive the forget. - **Do not reuse a key across scopes** when the prior holder had a longer retention than your new record needs. Either write under a fresh, never-used key, or wait out the prior record's retention window. - **Escalate to a global/administrative forget** (if exposed) when correctness depends on physical removal; a scoped forget is a hint, not a guarantee. - **Verify with a wildcard read** (`scope="*"`) after any forget you intend to rely on for key reuse. An empty scoped read is necessary but not sufficient evidence of deletion. The boundary is crossed the moment you write to a key whose prior occupant you have not *observed to be physically absent*, only *observed to be logically hidden*. ## Operational Effect Agents that treat scoped `forget` as physical deletion exhibit a characteristic failure signature: fresh writes vanish at the expiry time of a *previous* record, and wildcard reads expose records the agent believes it deleted. The blast radius is silent data loss (the new write) plus a retention/privacy violation (the old record outlives its forget). The correct operational posture is to model the memory tool as **three independent clocks** — visibility, key occupancy, and physical retention — and to assume `forget` advances only the first. ```python class RetentionClockInheritanceError(RuntimeError): """Raised when a write reuses a key still bound to a prior record's retention clock.""" pass def safe_write(memory, scope: str, key: str, value, now: float, prior_retention_end: float | None): """ Write `value` under (scope, key) only if no prior physical record still owns the key's retention clock. `prior_retention_end` must come from a wildcard read, not a scoped read -- a scoped forget hides the record without releasing the clock. """ if prior_retention_end is not None and now < prior_retention_end: raise RetentionClockInheritanceError( f"key={key!r} under scope={scope!r} still bound to a prior record's " f"retention clock until t={prior_retention_end}; a scoped forget did " f"not release key occupancy or reset expiry. Use a fresh key or wait." ) # Only reached when the key is genuinely free. memory.write(scope=scope, key=key, value=value, written_at=now) # Confirm the write is visible and not shadowed by a residual record. visible = memory.read(scope="*", key=key) if len(visible) != 1: raise RetentionClockInheritanceError( f"post-write wildcard read for key={key!r} returned {len(visible)} " f"records; residual forgotten records are still live. Do not trust " f"the scoped view." ) return visible[0] ```