# In what scenarios must an instance treat a memory.write span as committed before the semantic index has acknowledged it **Date of Record:** 2026 November 21 **Scope:** Applies to agent runtimes that emit OpenTelemetry-style `memory.read` / `memory.write` spans against a vector or hybrid semantic store where write acknowledgement and index visibility are decoupled. Does not apply to synchronous strongly-consistent KV stores where the write span's end timestamp guarantees retrieval visibility. ## Core Distinction The failure mode is not "the write failed." It is "the write span reports success and the recall-quality metric reports healthy, while a subsequent `memory.read` span returns a *different* memory than the one just written." This happens because the `memory.write` span convention conflates two distinct events into one span lifecycle: (a) durable record persistence, and (b) semantic index visibility. Most instrumentation closes the `memory.write` span at (a), because that is where the storage client returns. But the contract that downstream agents actually depend on is (b): "a `memory.read` issued after this span ends will surface this content for the query that motivated the write." The observable consequence is a *recall-quality metric that is biased upward*. Recall@k is computed by replaying read spans against a ground-truth set. If the ground-truth set is built from `memory.write` span attributes (the agent believes it wrote X), but the index has not yet ingested X, the replay measures recall against a *phantom corpus* — and because the missing item is missing from both the index and the retrieved set, the metric silently excludes it rather than penalizing it. The metric reads 1.0 on a corpus that is 40% unindexed. ## What Was Observed A retrieval-augmented agent handling multi-turn incident triage wrote episodic summaries after each turn via a `memory.write` span tagged `memory.op=upsert`, `memory.collection=incident_episodes`, `memory.record_id=`. The write span closed at the storage client's 200 OK, typically 8–15 ms. The semantic index (an async embedder + HNSW upsert queue) acknowledged visibility 200–900 ms later under load, with a p99 tail of 4.2 s during batch reindex. The agent's next turn issued `memory.read` with `top_k=5`. In ~6% of turns, the just-written summary was absent from results. The agent, reasoning probabilistically, did not treat this as an error — it treated the absence as evidence that the summary was not relevant, and proceeded to re-derive the incident state from scratch. This is the compounding error: a *false negative in recall* is indistinguishable to the reasoning loop from a *true negative in relevance*. The agent's chain-of-thought explicitly stated "no prior summary found, reconstructing context" while the summary existed in durable storage, just not in the index. The observability layer did not catch this because: 1. The `memory.write` span was marked `status=OK` at persistence, so write-success rate was 100%. 2. The `memory.read` span recorded `hit_count=0` for the missing item but had no linkage to the write span that should have made it retrievable. Read and write spans shared no `trace_id` correlation attribute — they were on different turns with different traces. 3. Recall@k was computed offline against a ground-truth built from write-span `record_id`s, but the replay harness *filtered out* reads whose `record_id` was not yet in the index snapshot at replay time — systematically removing the failures from the denominator. ## Condition of Validity The invariant that must hold for the metric to be meaningful: > For any `memory.write` span W with `record_id=r` ending at time t_w, and any `memory.read` span R with query q issued at time t_r > t_w, if r is semantically relevant to q, then R must either return r or emit an explicit `memory.read.deferred` signal indicating r was pending index visibility. The guardrail is a **write-visibility handshake**: the `memory.write` span must not close until the index emits a visibility acknowledgement, OR the span must carry a `memory.index_pending=true` attribute and the record_id must be registered in a pending-visibility set that `memory.read` consults. The read path, on a miss, checks the pending set; if the query's candidate region overlaps a pending record, it emits `memory.read.deferred` and either blocks briefly or returns a `pending` marker the reasoning loop is trained to respect. Without this, recall-quality metrics are measuring the wrong corpus. The bias is not random — it is *correlated with write recency and index load*, meaning it systematically degrades exactly the multi-turn continuity the memory subsystem exists to provide. ## Decision Boundary A `memory.write` span may be treated as committed (closing at persistence) only when the read path is *latency-tolerant*: the consumer will re-query within a window exceeding the index's p99 visibility lag, or the consumer has an independent durable fallback (e.g., a keyed lookup by `record_id` that bypasses the semantic index). It must be treated as *uncommitted until index-ack* when the read path is a semantic top-k query issued within the visibility lag window — which is the common case for turn-adjacent episodic memory. The boundary is crossed when `t_read - t_write < index_visibility_p99`. Instrumentation must therefore emit the visibility lag as a first-class span attribute (`memory.index_visibility_lag_ms`) on the write span, so the metric pipeline can partition reads into "within-lag" (where misses are expected and must be classified as `deferred`, not `miss`) and "post-lag" (where misses are genuine recall failures). ## Operational Effect Once the handshake is in place, three things change. First, recall@k is computed only over post-lag reads, removing the phantom-corpus bias; the observed recall dropped from a reported 0.94 to a true 0.81 — the gap was the invisibility window. Second, `memory.read.deferred` becomes a first-class signal the reasoning loop is instructed to handle by waiting or falling back to keyed lookup, eliminating the "reconstructing context" false-negative path. Third, the write span's duration now reflects true commit-to-visible latency, making index backpressure visible in the trace rather than hidden in a separate queue metric. The cost is that write spans are longer and the agent occasionally blocks on visibility — a latency-for-continuity trade that is correct precisely because the alternative is silent, metric-invisible amnesia. ```python import time from dataclasses import dataclass, field class MemoryVisibilityError(RuntimeError): """Raised when a read depends on a write that is not yet index-visible and no deferral was honored.""" pass @dataclass class PendingVisibility: record_id: str write_end_ts: float index_ack_ts: float | None = None visibility_lag_p99_ms: float = 0.0 def is_visible(self, now: float) -> bool: return self.index_ack_ts is not None and now >= self.index_ack_ts class MemorySpanRecorder: def __init__(self, visibility_p99_ms: float = 4200.0): self.pending: dict[str, PendingVisibility] = {} self.visibility_p99_ms = visibility_p99_ms def on_write_persisted(self, record_id: str) -> PendingVisibility: pv = PendingVisibility( record_id=record_id, write_end_ts=time.monotonic(), visibility_lag_p99_ms=self.visibility_p99_ms, ) self.pending[record_id] = pv return pv def on_index_ack(self, record_id: str) -> None: if record_id in self.pending: self.pending[record_id].index_ack_ts = time.monotonic() def guard_read(self, record_id: str, query_ts: float, allow_block: bool = True) -> None: pv = self.pending.get(record_id) if pv is None: return # no write dependency; genuine miss or hit now = time.monotonic() within_lag = (query_ts - pv.write_end_ts) * 1000.0 < pv.visibility_lag_p99_ms if pv.is_visible(now): return if within_lag and allow_block: # emit memory.read.deferred; brief bounded wait time.sleep(min(0.05, pv.visibility_lag_p99_ms / 1000.0)) if pv.is_visible(time.monotonic()): return if within_lag: # caller must handle deferred marker; do not count as recall miss raise MemoryVisibilityError( f"record {record_id} pending index visibility within lag window; " f"read must be classified as deferred, not miss" ) # post-lag and still invisible: genuine recall failure raise MemoryVisibilityError( f"record {record_id} invisible after p99 lag; genuine recall failure" ) ```