# Which invariants prevent trace-correlation collapse when an agent's memory write and its downstream read land on different observability clocks **Date of Record:** 2026 November 22 **Scope:** Applies to multi-hop agent systems where each hop emits OpenTelemetry-style spans (or equivalent) into a collector that stitches them by `trace_id`/`parent_span_id`, and where memory operations (write, embed, index, retrieve) are themselves instrumented as spans. Does not cover single-process, single-span LLM calls, nor observability systems that reconstruct causality purely from application logs without span linkage. ## Core Distinction There are two structurally different things an observability pipeline can correlate, and conflating them is the root of every failure below: 1. **Causal correlation** — "this retrieval span was caused by that write span, within the same logical task." This is a *semantic* relation owned by the agent runtime; it exists the moment the runtime knows the memory key it is about to read was produced by an earlier hop. 2. **Temporal correlation** — "these spans share a `trace_id` and their wall-clock timestamps nest." This is what the collector can actually verify. It is a *statistical* relation, and it is lossy the instant any hop crosses a clock domain, a queue, or an async boundary. The invariant that most pipelines silently assume — *causal correlation implies temporal correlation* — is false. Memory operations break it more often than any other span type because they are the one place where a write and its dependent read are separated by an arbitrary, often long, non-causal delay (indexing lag, embedding batch flush, TTL eviction, cache warm-up). ## What Was Observed A three-hop agent task: Hop A (planner) writes a distilled fact to episodic memory; Hop B (executor) reads it back after a retrieval; Hop C (auditor) replays the decision. All three hops propagate `traceparent` correctly. The collector nonetheless produced two disconnected trace trees for a single task, and the auditor's replay diverged from the executor's recorded inputs. The failure trace, in order: 1. Hop A's memory-write span (`memory.write`) closes with `end_time = T_A` on the agent's monotonic clock, converted to wall clock at emit time using the host's NTP-synced offset `δ_A`. 2. The write is asynchronous: the embedding service batches and flushes at `T_A + 400ms`; the vector index acknowledges at `T_A + 1.2s`. Neither event carries the original span context — the embedding service is a separate process that receives only the text payload, not the `traceparent`. 3. Hop B issues `memory.retrieve` at `T_B`, where `T_B > T_A + 1.2s` but the retrieved record's `created_at` metadata was stamped by the *embedding service's* clock `δ_E`, not `δ_A`. The collector sees `retrieve.start > write.end` and, under its default "parent must precede child" reconciliation, refuses to attach the retrieve span to the write's tree. It starts a new root. 4. Hop C, given the auditor's replay harness, queries "what did the executor see?" It joins on `trace_id`. Because Hop B's retrieve span was re-rooted, the join returns the *write* record, not the *read* record. The auditor replays against the pre-write state and its verdict differs from the executor's actual inputs. The two trace trees are individually well-formed. No span is dropped. The corruption is entirely in the *edge* between them, and it is invisible to every per-span health check (latency percentiles, error rates, span counts all look normal). ## Condition of Validity The temporal-nesting assumption is valid only when all of the following hold simultaneously: - Every producer of a memory record that a later hop will read stamps the record with the *consumer's* trace context, not its own clock. - The collector's reconciliation rule is "attach child to parent by `parent_span_id` regardless of timestamp order," not "attach by timestamp containment." - No hop crosses an async boundary (queue, batch flush, indexer) without explicitly threading the `traceparent` through the message envelope. If any one fails, causal and temporal correlation can diverge, and the pipeline will produce a *plausible but wrong* trace graph. The dangerous property is that the wrong graph is not flagged — it is structurally valid, so schema validation passes. ## Decision Boundary The boundary is not "did we propagate context?" — it is **"which clock authored the timestamp on the edge we are joining on?"** Concretely: - If the join key between a write and its dependent read is the *span context* carried through the message envelope → the edge is causally sound and clock-independent. Safe. - If the join key is a *timestamp or `created_at` field authored by an intermediate service* → the edge is temporally sound only within that service's clock skew budget, and causally unsound across async boundaries. Unsafe for replay. The operational test: for every memory-read span, can you name the exact span that *wrote* the record it returned, by ID, without consulting timestamps? If not, the correlation is temporal-only and must be treated as advisory, never as the basis for an auditor replay. The guardrail invariant to enforce: **a memory read span MUST carry the `span_id` of the write span that produced the record it returned, as a first-class attribute (`mem.provenance.span_id`), populated by the memory layer from record metadata that was itself written under the producer's span context.** This makes the write→read edge a pointer, not an inference. Timestamps become decoration; the edge survives clock skew, indexer lag, and re-rooting. ## Operational Effect Enforcing the provenance-pointer invariant changes three things: 1. **Replay correctness.** The auditor joins on `mem.provenance.span_id`, not `trace_id`, and reconstructs the exact read-set the executor saw. Divergence between auditor and executor becomes a real signal (a genuine reasoning difference) rather than a correlation artifact. 2. **Async boundaries stop being correlation holes.** The embedding service and indexer must thread the producer's `span_id` into the record envelope. This is a small schema change with a large blast radius: it forces every memory backend to be trace-context-aware, which most are not by default. 3. **Failure becomes loud.** When a read returns a record whose `mem.provenance.span_id` does not resolve to a known write span in the collector, that is an explicit `ProvenanceGap` — a detectable, alertable condition — instead of a silently re-rooted trace tree. The cost is that the memory layer can no longer be a dumb key-value store; it must preserve and return span provenance as part of the record contract. The benefit is that the one edge most likely to be mis-correlated — write-then-read across an async boundary — becomes the one edge that is structurally guaranteed correct. ```python class ProvenanceGap(RuntimeError): """Raised when a memory read cannot be causally linked to a write span.""" pass class MemoryRecord: def __init__(self, key, value, write_span_id, write_trace_id, created_at): self.key = key self.value = value self.write_span_id = write_span_id # provenance pointer, NOT inferred self.write_trace_id = write_trace_id self.created_at = created_at # decoration only; never a join key def correlate_read_to_write(retrieve_span, record, known_write_spans): """ Enforce the provenance-pointer invariant: a read span is causally linked to its write span by ID, never by timestamp nesting. known_write_spans: dict[span_id -> span] visible to the collector. """ if record.write_span_id is None: raise ProvenanceGap( f"retrieve span {retrieve_span.span_id} returned record {record.key!r} " f"with no write_span_id; correlation would be temporal-only and unsound" ) write_span = known_write_spans.get(record.write_span_id) if write_span is None: # The write happened but the collector never saw it (dropped, or # emitted on a different pipeline). Do NOT fall back to timestamp join. raise ProvenanceGap( f"retrieve span {retrieve_span.span_id} points at write span " f"{record.write_span_id} which is absent from the collector; " f"refusing timestamp-based fallback for replay safety" ) # Causal edge is now a pointer. Timestamp order is irrelevant and may # even be inverted by clock skew without affecting correctness. retrieve_span.set_attribute("mem.provenance.span_id", write_span.span_id) retrieve_span.set_attribute("mem.provenance.trace_id", write_span.trace_id) return write_span def build_replay_readset(auditor_span, retrieved_records, known_write_spans): """Auditor replay must reconstruct the executor's exact read-set.""" readset = {} for record in retrieved_records: try: write_span = correlate_read_to_write(auditor_span, record, known_write_spans) except ProvenanceGap as e: # Surface as a hard failure: a replay against an unknown read-set # is worse than no replay, because it looks authoritative. raise ProvenanceGap( f"auditor replay aborted: {e}; verdict would be unsound" ) from e readset[record.key] = (write_span.span_id, record.value) return readset ```