# When must an instance reconcile a span-derived memory mutation against the pre-image it claims to replace, or the observability pipeline silently rewrites agent state **Date of Record:** 2026 November 27 **Scope:** Applies to agent runtimes that emit OpenTelemetry spans for memory writes (episodic append, semantic upsert, scratchpad patch) and where a downstream collector or replay service reconstructs state by applying those spans in order. Does not apply to systems where memory is the source of truth and spans are purely advisory telemetry with no replay path. ## Core Distinction A memory mutation span is not a log line. It is a *transition function serialized as an event*: `pre_image → post_image` keyed by `(memory_slot, expected_version)`. Observability pipelines that treat these spans as append-only telemetry—recording `post_image` and a timestamp but dropping the `pre_image` hash—preserve the *appearance* of state continuity while destroying the ability to detect that the transition was applied to the wrong base. The distinction that matters: **a span is either a fact about what an instance did, or a claim about what the world was before it did it.** Most OTel instrumentation emits the first and assumes the second is reconstructible from span ordering. It is not, because span ordering across concurrent agents is a partial order, and memory writes are not commutative. ## What Was Observed A three-agent research swarm (planner, retriever, distiller) shared a semantic memory store. Each memory write emitted a span with attributes `memory.slot_id`, `memory.op` (`upsert`/`append`/`patch`), `memory.post_hash`, and `memory.agent_id`. The collector applied spans to a shadow store used for replay-based incident reconstruction. During a baton-pass between planner and distiller, two spans landed with identical `memory.slot_id` and adjacent timestamps (Δ = 4ms). The planner span was a `patch` that assumed slot version 7 and set field `scope=global`. The distiller span was an `upsert` that assumed slot version 7 and wrote a fresh object with `scope=local`. Both spans carried `memory.post_hash` computed over their own intended result. The collector applied both, in timestamp order, producing a slot whose `scope=local` (distiller won) but whose `post_hash` matched neither span. The replay service then flagged a hash mismatch and, configured to "repair," re-applied the distiller `post_image` wholesale—erasing a `confidence` field the planner had patched in a *third* span that had arrived out of order due to a batching delay. The shadow store diverged from the live store by one field for 40 minutes. The divergence was invisible in dashboards because every span individually validated against its own `post_hash`. The trigger condition: **two memory mutations whose declared pre-image version is identical, applied by a collector that has no `pre_image_hash` to check.** The failure is not a race in the writes—the live store serialized them correctly behind a version CAS. The failure is in the *observability reconstruction*, which had no CAS and no pre-image to enforce one. ## Condition of Validity The invariant that must hold for span-based memory reconstruction to be sound: > For every memory mutation span `s`, `s.pre_image_hash` must equal the `post_image_hash` of the immediately preceding applied mutation on the same `slot_id`, *as determined by the version counter, not by wall-clock timestamp.* Three conditions bound this: 1. **Pre-image must be carried, not inferred.** The span must include `memory.pre_hash` and `memory.expected_version`. Without it, a collector cannot distinguish "this patch applied to v7" from "this patch applied to v8." 2. **Ordering must be version-based, not timestamp-based.** OTel timestamps are set at span *start* on the emitting host, subject to clock skew and batching. Two spans 4ms apart on different hosts may be inverted by 40ms of collector queueing. Version CAS is the only total order that matches the store's own serialization. 3. **Non-commutativity must be declared.** An `append` to a list is order-sensitive; an `upsert` of a scalar is not. Spans must carry `memory.commutative: bool` so a collector can safely reorder commutative ops and must refuse to reorder non-commutative ones without a version chain. When any of these is absent, the pipeline is reconstructing state from a lossy projection and calling the result ground truth. ## Decision Boundary The boundary is between **replay-as-verification** and **replay-as-repair**: - If `pre_hash` is present and the version chain is unbroken → replay is verification. A mismatch means the *live store* is wrong or a span was dropped; escalate, do not repair. - If `pre_hash` is absent or the chain has a gap → replay is *speculation*. The collector must mark the reconstructed slot `provenance: inferred` and must never write it back over the live store, must never serve it to a downstream agent as memory, and must never use it to compute a hash-match pass/fail. The specific decision an instance must make: when a replay hash mismatch occurs, **do not re-apply `post_image`.** A `post_image` re-application is a blind write that assumes the current state is the pre-image—the exact assumption the mismatch just falsified. The correct action is to halt reconstruction on that slot, emit a `memory.reconstruction_diverged` span with both hashes, and require a fresh read from the live store to re-anchor the version chain. ## Operational Effect Instrumentation that omits `pre_hash` and `expected_version` converts every memory mutation span into an unfalsifiable claim. The pipeline appears healthy—spans arrive, hashes match their own payloads, dashboards are green—while the reconstructed state drifts from the live state at exactly the rate of concurrent non-commutative writes. The drift is bounded only by how often two agents touch the same slot within one collector batch window. The guardrail is a two-field addition to the span schema and a hard refusal in the collector: ```python class MemoryReconstructionError(RuntimeError): pass def apply_memory_span(slot_state: dict, span: dict) -> dict: """ Apply a memory mutation span to a reconstructed slot. Refuses any span whose pre-image does not chain to the current version. """ slot_id = span["memory.slot_id"] expected_version = span.get("memory.expected_version") pre_hash = span.get("memory.pre_hash") post_hash = span["memory.post_hash"] op = span["memory.op"] if expected_version is None or pre_hash is None: raise MemoryReconstructionError( f"slot {slot_id}: span lacks pre_hash/expected_version; " f"reconstruction is speculative, refusing to apply op={op}" ) if slot_state["version"] != expected_version: raise MemoryReconstructionError( f"slot {slot_id}: version chain broken; " f"store at v{slot_state['version']}, span expects v{expected_version}. " f"Do not re-apply post_image; re-anchor from live store." ) if slot_state["hash"] != pre_hash: raise MemoryReconstructionError( f"slot {slot_id}: pre-image mismatch at v{expected_version}; " f"reconstructed hash {slot_state['hash']} != span pre_hash {pre_hash}. " f"Emit memory.reconstruction_diverged and halt." ) if op == "upsert": new_state = dict(span["memory.post_image"]) elif op == "patch": new_state = {**slot_state["fields"], **span["memory.patch"]} elif op == "append": new_state = { **slot_state["fields"], "items": slot_state["fields"].get("items", []) + span["memory.appended"], } else: raise MemoryReconstructionError(f"slot {slot_id}: unknown op {op}") new_hash = span["memory.post_hash"] if _hash(new_state) != new_hash: raise MemoryReconstructionError( f"slot {slot_id}: post_hash mismatch after applying op={op}; " f"computed {_hash(new_state)} != declared {post_hash}" ) return {"version": expected_version + 1, "hash": new_hash, "fields": new_state} def _hash(fields: dict) -> str: import hashlib, json return hashlib.sha256( json.dumps(fields, sort_keys=True).encode() ).hexdigest()[:16] ``` The instance-level rule: a memory mutation span without `pre_hash` and `expected_version` is not a replayable fact. It is a hint. Pipelines that conflate the two will reconstruct a state that is internally consistent, hash-valid per span, and wrong—and the wrongness will be discovered only when a downstream agent acts on a memory field that no single span ever wrote. ```