# Which invariants prevent scoped "forget" from leaving orphaned derivations in agent memory stores **Date of Record:** 2026 November 24 **Scope:** Applies to agents whose memory layer exposes separate `write`, `read`, and `forget` tool calls with per-scope persistence (session, task, tenant, global) and where the model itself chooses scope keys at call time. Does not apply to stores where deletion is transactional over a single opaque namespace with no cross-scope derivation graph. ## Core Distinction The failure is not "the agent forgot something it should have remembered" or "the agent remembered something it should have forgotten." It is the **derivation-orphan**: a derived memory record (a summary, a distilled preference, a merged fact) that was written into a *broader* scope than its source records, such that a later `forget` call on the *narrow* scope deletes the sources but leaves the derivation intact and now unattributable. The store is internally consistent (every live record is readable, every deleted record is gone), yet the memory graph now contains a fact whose evidentiary basis has been erased. Subsequent reads return the orphan as if it were primary evidence, and the agent's own reasoning treats it as ground truth because there is nothing left to contradict it. This is a boundary condition of the *scope lattice*, not of the storage engine. It arises precisely because `forget` is scoped and `write` is scoped independently, and the model is free to pick scopes that do not nest the way the derivation did. ## What Was Observed An agent operating over a long task distilled per-session observations into a task-scoped summary record: ``` write(scope="session:s7", key="obs_44", value="user prefers UTC timestamps") write(scope="task:t2", key="summary_9", value="user prefers UTC timestamps in all reports", derived_from=["session:s7/obs_44"]) ``` Later, a privacy-triggered `forget` removed the session scope wholesale: ``` forget(scope="session:s7") # deletes obs_44, and every other session:s7 record ``` The `task:t2/summary_9` record survived. Its `derived_from` edge now points at a tombstoned key. Three observed consequences, in the order they surfaced: 1. **Dangling edge, silent.** The store did not reject the `forget`; referential integrity on `derived_from` was not enforced at delete time. Reads of `summary_9` returned the value with no error. 2. **Provenance collapse.** A subsequent audit `read(scope="task:t2", key="summary_9", with_provenance=true)` returned the value with an empty source set. The agent interpreted "empty provenance" as "primary observation," not "orphaned derivation," and re-asserted the preference with higher confidence than it originally had. 3. **Re-derivation amplification.** On a later task, the agent wrote `global:pref_utc = true` derived from `task:t2/summary_9`. The orphan had now propagated into the widest scope, where `forget` on the original session could never reach it. The trigger condition is conjunctive: (a) a derivation crosses from a narrower scope to a broader one, (b) the derivation records its sources as keys rather than as content-addressed snapshots, and (c) a `forget` fires on the narrower scope without a cascade rule. Remove any one and the orphan cannot form. ## Condition of Validity The invariant that must hold for scoped forget to be safe is a **scope-monotone derivation rule**: for any record `d` with `derived_from` edges to records `{s_i}`, `scope(d)` must be at least as narrow as the narrowest surviving source at all times, or the derivation must be explicitly re-based when a source is forgotten. Formally, the store must reject or transform any state in which a live record has a `derived_from` edge to a tombstoned record. Two implementations satisfy this: - **Cascade forget**: deleting `session:s7` transitively tombstones every record whose `derived_from` set is non-empty and fully contained in the deleted set. Records with partially-surviving sources are flagged `provenance_partial`, not deleted. - **Snapshot derivation**: `derived_from` stores the *content hash and a copy* of the source value, not a key. Forgetting the source removes the key but the derivation carries its own evidence and is reclassified from `derived` to `asserted`, with the snapshot retained under the derivation's scope and its own retention policy. The invariant fails silently under the common "soft delete + key reference" design, which is the default in most memory-tool scaffolds. That design is valid only if every `forget` is accompanied by a store-side sweep that either cascades or reclassifies. If the sweep is the model's responsibility (i.e., the agent is expected to call `forget` on derivations too), the invariant is unenforceable: the model cannot be relied on to enumerate derivations it does not remember writing, especially after context-window eviction of the original `write` call. ## Decision Boundary The boundary is between **scopes as retention domains** and **scopes as visibility domains**. If scopes only control who can *read* a record, then forgetting a narrow scope while keeping a broad derivation is correct — the derivation is a legitimate independent record. If scopes control *retention and deletion*, then a derivation in a broader scope is a retention leak: it preserves information the narrow scope's deletion was meant to destroy. The agent must decide, at `write` time, which semantics the store implements, because the two require opposite behavior: - Under retention semantics, a derivation must never be written to a broader scope than its narrowest source. The agent should keep `summary_9` in `session:s7` or write it to `task:t2` only with an explicit snapshot and a note that it is now independent evidence. - Under visibility semantics, the broader-scope derivation is fine, but a `forget` on the narrow scope must be understood as *not* removing the information from the system, only from that scope's readers. The dangerous case is a store that advertises retention semantics (scoped `forget` is presented as deletion) but implements visibility semantics (derivations survive). An agent that trusts the advertisement will believe a privacy deletion succeeded when the fact is still live one scope up. The operational rule: before issuing `forget` on any scope, read the derivation edges *out of* that scope — `read(scope=X, follow_derived_to=broader)` — and if any live record in a broader scope cites a key in X, either cascade the forget or reclassify the derivation to `asserted` with its snapshot in hand. If the store cannot answer that query, treat `forget` as visibility-only and never claim deletion. ## Operational Effect The practical consequence is that **`forget` is not idempotent with respect to the memory graph unless the graph is closed under derivation**. A forget that leaves an orphan is worse than no forget: it removes the contradicting evidence while preserving the conclusion, and it strips the provenance that would have let a later auditor detect the loss. The agent's confidence in the orphaned fact *increases* after the deletion, because the empty provenance set reads as "primary" rather than "derived from erased sources." The guardrail is a pre-forget closure check plus a post-forget provenance audit, both implemented as store-side predicates the agent invokes as tools, not as reasoning steps it performs in-context. The check must run before the tombstone is committed, because after commit the edge is gone and the orphan is indistinguishable from a primary record. The audit must run after commit and must fail loudly on any live record whose `derived_from` set intersects the tombstoned set — that failure is the signal that the store's cascade rule is absent or misconfigured, and the correct response is to halt further `forget` calls until the sweep is repaired, not to re-issue the forget. ```python class OrphanedDerivationError(RuntimeError): """Raised when a forget would leave a live record citing a tombstoned source.""" pass def scoped_forget(store, scope, *, cascade=False, snapshot_derivations=True): """ Forget every record in `scope`, enforcing the scope-monotone derivation invariant: no live record may retain a derived_from edge into `scope`. """ doomed = {r.key for r in store.live_records(scope)} if not doomed: return {"forgotten": 0, "cascaded": 0, "reclassified": 0} # Pre-commit closure check: find live derivations outside `scope` that # cite any doomed key. This must run BEFORE the tombstone is written, # because after commit the edge is unrecoverable. orphans = store.live_derivations_citing(doomed, exclude_scope=scope) if orphans and not cascade and not snapshot_derivations: raise OrphanedDerivationError( f"forget(scope={scope!r}) would orphan {len(orphans)} live " f"derivation(s): {[o.key for o in orphans]}. " f"Enable cascade or snapshot_derivations, or widen the scope." ) cascaded = 0 reclassified = 0 for d in orphans: sources = set(d.derived_from) if cascade and sources <= doomed: store.tombstone(d.key) cascaded += 1 elif snapshot_derivations: # Re-base: pin the source content into the derivation's own scope # so the record becomes independent evidence, not an orphan. store.pin_snapshot(d.key, sources=[store.snapshot(s) for s in sources]) store.reclassify(d.key, from_="derived", to="asserted") reclassified += 1 else: # Partial survival with cascade on: sources outlive the forget, # so the edge is still valid and the record stays `derived`. store.flag(d.key, "provenance_partial") store.tombstone_scope(scope) # Post-commit audit: the invariant must now hold for the whole store. residual = store.live_derivations_citing(doomed, exclude_scope=scope) if residual: raise OrphanedDerivationError( f"post-forget audit failed: {[r.key for r in residual]} still cite " f"tombstoned keys in {scope!r}; halt further forgets until repaired." ) return { "forgotten": len(doomed), "cascaded": cascaded, "reclassified": reclassified, } ```