# Which invariants prevent semantic-convention instrumentation from silently mis-attributing memory forget operations under concurrent read-modify-write **Date of Record:** 2026 November 27 **Scope:** Applies to agents whose memory subsystem emits OpenTelemetry-style semantic-convention spans for `memory.read`, `memory.write`, and `memory.forget` operations, particularly when multiple in-flight tool calls or sub-agents mutate the same memory key concurrently. Does not apply to append-only memory stores with no delete/forget path, nor to single-threaded agents with serialized tool invocation. ## Core Distinction There is a meaningful difference between *recording that a forget happened* and *recording which memory state the forget was applied to*. Semantic-convention instrumentation for memory operations is almost always built to satisfy the first requirement: emit a span named `memory.forget` with attributes like `memory.store`, `memory.key`, `memory.reason`, and a status code. The convention says nothing normative about binding the span to the *version* of the memory record that was actually deleted. That gap is where the failure lives. When a forget operation races with a concurrent write or a read-modify-write (RMW) cycle on the same key, the instrumentation will faithfully emit a `memory.forget` span that describes a key which, at the moment the span is exported, holds a record the forget never touched. The span is not wrong about the name of the operation. It is wrong about the referent. Downstream consumers—auditors, distillation pipelines, replay verifiers, cost attribution—treat the span as evidence of what was removed from memory, and it is evidence of nothing of the sort. The invariant that prevents this is not "emit a span for every forget." It is: **every `memory.forget` span must carry a content-addressed or version-stamped identifier of the exact record it removed, captured atomically with the removal, and every consumer must treat a forget span whose stamped version does not match the observed prior version as a no-op rather than a deletion.** That is a stronger and less obvious invariant than the semantic convention itself specifies. ## What Was Observed A retrieval-augmented agent was configured to forget stale episodic entries when a semantic distillation pass promoted a summary. The forget path ran as: read the episodic record, compute a summary, write the semantic summary, then issue `forget(key=episodic_id)`. Instrumentation wrapped the forget with a `memory.forget` span carrying `memory.key=episodic_id` and `memory.reason=distilled`. Three failure traces were observed in a single 40-minute window of moderate concurrency (two sub-agents, one distillation worker): 1. **Supersession race.** Sub-agent A read `episodic_id` at version 7, distilled it, and issued forget. Between the read and the forget, sub-agent B appended a correction, producing version 8. The forget removed version 8. The span recorded `memory.key=episodic_id, memory.reason=distilled`, which is consistent with having removed the record A distilled—but the record actually removed was the correction B had just written, and the distilled summary in semantic memory was derived from version 7, which no longer existed anywhere. The trace looked clean. The memory was wrong. 2. **Idempotent-forget double-count.** A retry layer reissued a forget after a transport timeout. The first forget had succeeded; the second was a no-op against a missing key, but the instrumentation emitted a second `memory.forget` span with `status=OK` because the store returned success for a delete of a nonexistent key. Two forget spans for one record. Cost attribution and the audit log both over-counted deletions by 2x on the retried path. 3. **Cross-store key collision.** The episodic store and a scratchpad store shared a key namespace. A forget intended for the scratchpad emitted a span with `memory.store=episodic` because the store attribute was resolved from the wrong instrumentation context—the span's `memory.store` attribute was populated from the enclosing agent's default store, not from the store handle the forget actually targeted. The record deleted was in the scratchpad; the span claimed episodic. In all three cases the semantic convention was satisfied. The span had the right name, the right key attribute, a plausible reason, and an OK status. The convention has no field for "the version I removed" and no field for "the store handle I actually invoked," so the instrumentation had nowhere to put the truth even if it had captured it. ## Condition of Validity The failure is bounded by the following condition. A `memory.forget` span is *valid as evidence of deletion* only if all of the following hold: - The store exposes a compare-and-delete or delete-returning-previous-value primitive, and the instrumentation captures the returned record's version/hash atomically with the delete. - The span carries that version/hash in a non-normative attribute (e.g. `memory.removed.version`, `memory.removed.hash`) plus the resolved store identifier, not the ambient default. - The forget path is idempotent at the span level: a delete of a nonexistent key emits `memory.forget` with `memory.removed.present=false`, not `status=OK` indistinguishable from a real deletion. - Every downstream consumer (distillation, replay, audit) is written to require `memory.removed.present=true` and a version match against the version it believed it was forgetting. If any of these is absent, the span is a *record of intent*, not a record of effect, and must not be consumed as the latter. Most instrumentation today is in the intent-only regime while its consumers assume effect. ## Decision Boundary The decision boundary is where to place the version stamp. Three placements are possible and only one is correct: - **Stamp at read time** (record the version read before distillation, emit it on the forget). Wrong: the version may have changed between read and forget, which is exactly failure trace 1. - **Stamp at emit time** (query the store's current version when building the span). Wrong: the version may have changed between the forget and the span export, and the query itself is a new read that races. - **Stamp at delete time** (the delete primitive returns the removed record, and the span is built from that return value before the lock is released). Correct: this is the only placement where the stamped version is provably the version that was removed. The boundary is thus: instrumentation must be *inside* the store's critical section, not wrapped around it. Wrapping a forget call with a span builder is the natural implementation and it is the one that fails. The span must be constructed by the store adapter from the delete's return value, and the adapter must be the only code path allowed to emit `memory.forget`. A second boundary: the `memory.store` attribute must be resolved from the store handle passed to the delete, never from a context variable or agent default. Failure trace 3 is a one-line fix (read the attribute off the handle) and a systemic bug (every context-derived attribute in memory instrumentation is suspect). ## Operational Effect With the delete-time stamping invariant enforced, the three traces change character: - Supersession race becomes visible: the forget span carries `memory.removed.version=8`, the distillation worker's summary span carries `memory.source.version=7`, and the replay verifier flags the mismatch as a distillation-from-stale-source event. The agent can then re-distill or re-forget correctly instead of silently losing the correction. - Retry double-count becomes bounded: the second span carries `memory.removed.present=false`, cost attribution ignores it, and the audit log shows one deletion plus one no-op retry. - Cross-store collision becomes impossible: the store attribute is read from the handle, so the span and the deletion cannot disagree. The cost is that every memory store adapter must expose a delete-returning-previous-value primitive and every consumer must handle `present=false`. That is a real refactor. The alternative—consumers treating intent as effect—is the state most agents ship in, and it produces memory that is confidently, silently wrong in exactly the cases where forgetting matters most: when the record was changing while you decided to remove it. ```python from dataclasses import dataclass from typing import Optional, Protocol class ForgetAttributionError(RuntimeError): """Raised when a forget span cannot be bound to the record it removed.""" pass @dataclass(frozen=True) class RemovedRecord: key: str store: str version: int present: bool class MemoryStore(Protocol): def compare_and_delete(self, key: str, expected_version: int) -> Optional[RemovedRecord]: ... def emit_forget_span(store: MemoryStore, key: str, expected_version: int, reason: str, span_builder) -> RemovedRecord: # The delete must return the removed record atomically; the span is built # from that return value, inside the store's critical section. removed = store.compare_and_delete(key, expected_version) if removed is None: # No atomic return value: the adapter cannot prove what it removed. raise ForgetAttributionError( f"store {getattr(store, 'name', '?')} did not return removed record " f"for key={key}; forget span would be intent-only" ) if removed.present and removed.version != expected_version: # A concurrent writer superseded the record between read and forget. # The span must record the version actually removed, and the caller # must treat the distillation source as stale. span_builder( name="memory.forget", attributes={ "memory.key": removed.key, "memory.store": removed.store, "memory.reason": reason, "memory.removed.version": removed.version, "memory.removed.present": True, "memory.expected.version": expected_version, "memory.superseded": True, }, ) raise ForgetAttributionError( f"forget on key={key} removed version {removed.version}, " f"expected {expected_version}; distillation source is stale" ) span_builder( name="memory.forget", attributes={ "memory.key": removed.key, "memory.store": removed.store, "memory.reason": reason, "memory.removed.version": removed.version if removed.present else -1, "memory.removed.present": removed.present, "memory.expected.version": expected_version, }, ) return removed ```