# Which invariants prevent provenance-laundered memory writes from surviving retention compaction in agent memory tool APIs **Date of Record:** 2026 November 26 **Scope:** Applies to agent memory tool APIs exposing `read`, `write`, and `forget` operations with per-record retention policy enforcement and provenance tagging, where compaction/expiry is executed asynchronously against a shared store. Does not apply to append-only, non-compacting memory stores or to single-agent in-process scratchpads with no retention semantics. ## Core Distinction The failure mode here is not "the agent forgot something." It is the inverse: **the agent's memory store retained a record that its provenance no longer authorized it to retain, because provenance was attached to the record body rather than to the retention decision.** Concretely: a `write` call carries a provenance envelope — `source_id`, `trust_class`, `derived_from` lineage, and an `expires_at` computed from the *source's* retention class. The retention policy engine, on compaction, reads `expires_at` from the stored record and evicts or keeps accordingly. The boundary condition is that **a derived record can be rewritten** (e.g., an episodic-to-semantic distillation step that "updates" a semantic memory in place) **without recomputing `expires_at` against the new lineage.** The record body is fresh; the retention metadata is stale; the provenance graph now points at a parent that has already been forgotten. Compaction then preserves a child whose entire evidentiary basis has been evicted. This is provenance laundering: the record survives, but the invariant "every retained record has at least one live, trusted ancestor" is violated silently. No error is raised at write time, no error at compaction time, and `read` returns the record with a provenance tag that is now a dangling pointer. ## What Was Observed A three-stage trace across a memory API with `write(body, provenance, retention_class)` and `forget(record_id, cascade=True)`: 1. **t0** — Agent writes episodic record `E1` with `trust_class=external_untrusted`, `retention_class=ephemeral` (`expires_at = t0 + 24h`), derived from a raw tool output. 2. **t1** — Distillation step reads `E1`, produces semantic record `S1` with `trust_class=derived`, `derived_from=[E1]`. The write handler computes `expires_at` for `S1` from the *derived* policy (`t1 + 90d`) but does **not** inherit the untrusted source's taint or the parent's earlier expiry. 3. **t2** — `forget(E1, cascade=True)` runs correctly: `E1` is evicted, and cascade marks direct children. But `S1`'s `derived_from` was rewritten at t1's write to `[E1]` *after* cascade had already been evaluated against the pre-distillation graph in a concurrent compaction pass — a classic read-modify-write race on the lineage edge. 4. **t3** — Compaction pass runs. `S1` has `expires_at = t1 + 90d`, no live ancestor, `trust_class=derived`. It is retained. `read(S1)` succeeds and returns `derived_from=[E1]` where `E1` no longer exists. Observed symptoms downstream: (a) `read` returns records whose provenance resolution fails, and callers that "resolve provenance on read" either crash or silently drop the lineage; (b) an auditor agent replaying the memory state cannot reconstruct the write → forget → compact ordering, because the compaction log entry for `S1` cites a parent id that was deleted two passes earlier; (c) the same `S1` can be re-derived from a *different* `E1'` with `trust_class=trusted`, and the two `S1` bodies collide on a content hash while their provenance diverges — the dedup layer keeps the first and discards the second, laundering the untrusted one into a trusted slot. ## Condition of Validity The invariant that must hold for any retention decision to be sound: > For every record `r` retained at compaction time `t`, either `r.provenance.derived_from == []` (a root), or there exists at least one `p ∈ r.provenance.derived_from` such that `p` is retained at `t` **and** `r.expires_at ≤ min(p.expires_at)` **and** `r.trust_class` is no higher than `max(p.trust_class)`. Three sub-conditions, each independently violable: - **Liveness:** every parent must still exist at compaction. Violated by the t2 cascade race. - **Expiry monotonicity:** a child may never outlive the earliest-expiring parent that justifies it. Violated at t1 when the derived policy overrode the inherited ephemeral expiry. - **Trust monotonicity:** a child may never claim a trust class stronger than any parent it is derived from. Violated by the hash-collision dedup at t3. The failure is only reachable when *all* of the following hold: retention is enforced asynchronously (so `expires_at` is a stored field, not recomputed at read), provenance is mutable (derived records can be rewritten), and `forget` cascade is evaluated against a snapshot that can race with concurrent writes. Remove any one and the boundary collapses back to a benign case. ## Decision Boundary The architectural guardrail is to make retention a *computed* property of the live provenance graph, not a stored field, and to make the write path reject any derivation that would violate the three sub-conditions at write time rather than at compaction time. Operationally: - **Compute `expires_at` at compaction, not at write.** Store `retention_class` and lineage; derive expiry as `min(own_class_expiry, min over live parents)`. A child whose parent has been forgotten has no live parent and must itself be evicted (or promoted to a root only via an explicit, logged `adopt` operation with a new provenance envelope). - **Reject non-monotonic trust at write.** If `write` receives `derived_from=[p]` and `trust_class` stronger than `p.trust_class`, raise immediately. Do not defer to compaction. - **Make `forget` cascade and `write` derivation mutually exclusive** on the lineage edge — a lineage lock or a compare-and-swap on the parent set, so a derivation that references a parent being concurrently forgotten either sees the deletion and fails, or completes and is then cascaded. - **Dedup on `(content_hash, provenance_hash)`, not `content_hash` alone**, so two records with identical bodies but divergent trust cannot collapse. The decision boundary for the agent itself: **a `write` that carries `derived_from` pointing at any record not confirmed live at write time must be treated as a provenance-unknown write, not a trusted write.** The agent must not assume the store will reconcile this later; it must either re-read the parent (and handle its absence) or downgrade the child's trust class and let it inherit the weakest parent's expiry. ## Operational Effect When the invariant is enforced, the observable behavior changes in three ways that matter to a running agent: 1. **`read` never returns a record with dangling provenance.** Callers can resolve lineage without a try/except around a missing parent. The cost is that `read` may return `None` for a record the agent "knows" it wrote — because the record was evicted when its parent was forgotten. Agents must treat `read` miss on a previously-written id as a *possible correct eviction*, not a store fault, and must not blindly re-write it (that re-write would be a new root with fresh provenance, which is exactly the laundering path). 2. **Compaction becomes deterministic and replayable.** Because expiry is computed from the live graph, an auditor replaying the write/forget log reproduces the same retained set. The t3 divergence disappears: the same `S1` cannot be both retained and re-derived from a trusted parent, because the hash includes provenance. 3. **Write latency increases on the derivation path** (lineage lock + trust comparison), and `forget` cascade must block concurrent derivations of the forgotten subtree. This is the correct trade: bounding the boundary costs a lock on the lineage edge, not a silent trust escalation months later. The residual risk after enforcement is the **adopt** operation — the explicit promotion of an orphaned child to a root. That path must be the only way a record gains trust without a live parent, must be logged with the adopting principal's identity, and must reset `expires_at` to the adopting policy's clock. Without that single choke point, every other guardrail is bypassable by a well-formed but semantically orphaned write. ```python from dataclasses import dataclass, field from typing import Optional class ProvenanceInvariantError(RuntimeError): """Raised when a memory write or retention decision would violate provenance/retention monotonicity (liveness, expiry, or trust).""" @dataclass class Provenance: derived_from: list = field(default_factory=list) trust_rank: int = 0 # higher = more trusted @dataclass class MemoryRecord: record_id: str body_hash: str provenance: Provenance retention_expiry: float # absolute time; computed, not stored on write live: bool = True class ProvenanceAwareStore: TRUST_RANK = {"external_untrusted": 0, "derived": 1, "trusted": 2} def __init__(self): self._records: dict = {} def _live_parents(self, rec: MemoryRecord) -> list: return [ self._records[pid] for pid in rec.provenance.derived_from if pid in self._records and self._records[pid].live ] def write(self, rec: MemoryRecord, now: float) -> None: # Sub-condition: trust monotonicity, checked at write, not compaction. parents = self._live_parents(rec) if rec.provenance.derived_from and not parents: raise ProvenanceInvariantError( f"write of {rec.record_id} references no live parent; " f"must be an explicit adopt with a new provenance envelope" ) for p in parents: if rec.provenance.trust_rank > p.provenance.trust_rank: raise ProvenanceInvariantError( f"trust escalation: {rec.record_id} rank " f"{rec.provenance.trust_rank} exceeds parent " f"{p.record_id} rank {p.provenance.trust_rank}" ) # Sub-condition: expiry monotonicity, inherited from earliest parent. if parents: rec.retention_expiry = min( rec.retention_expiry, min(p.retention_expiry for p in parents), ) self._records[rec.record_id] = rec def forget(self, record_id: str) -> None: rec = self._records.get(record_id) if rec is None: return rec.live = False # Cascade: any child whose only parent was this record is now orphaned. for child in self._records.values(): if child.live and record_id in child.provenance.derived_from: if not self._live_parents(child): child.live = False def compact(self, now: float) -> list: evicted = [] for rec in self._records.values(): if not rec.live: continue if rec.retention_expiry <= now: rec.live = False evicted.append(rec.record_id) continue # Re-verify liveness invariant at compaction time. if rec.provenance.derived_from and not self._live_parents(rec): raise ProvenanceInvariantError( f"compaction found retained record {rec.record_id} " f"with no live ancestor: provenance laundering" ) return evicted ```