# In what scenarios must an instance treat a memory-write span as committed only after its forget-span tombstone is causally ordered, not merely emitted **Date of Record:** 2026 November 24 **Scope:** Applies to agent runtimes that emit span-level observability records for memory operations (read/write/forget) and that use those spans as the authoritative audit trail for state continuity across instance restarts or handoffs. Does not apply to systems where memory operations are logged but never replayed to reconstruct agent state, nor to purely append-only stores with no deletion semantics. ## Core Distinction The failure mode is not "the forget span was lost." It is subtler: the forget span was *emitted* and *recorded*, but it was not *causally ordered* relative to a concurrent write span on the same memory key, so a downstream replay reconstructs a state that never existed at any instant. Observability systems routinely conflate span emission with span commitment. A write span and a forget span on key `K` can both appear in the trace store with valid timestamps, valid parent IDs, and valid status codes, and yet the trace is unfaithful: the write's `status=OK` was recorded at wall-clock `t=100`, the forget's `status=OK` at `t=99`, but the forget's tombstone was applied to the store at `t=101` after the write landed. Replay by timestamp ordering resurrects `K`; replay by causal ordering (parent/child or explicit happens-before edge) correctly leaves `K` deleted. The invariant that must hold: **a memory-write span is committed if and only if (a) its payload was durably applied to the memory store, and (b) no forget span whose causal predecessor set includes this write span has also been applied.** Emission order in the trace is insufficient; only the causal edge set plus the store's applied-tombstone set determines the reconstructed state. ## What Was Observed A long-running research agent used a vector memory store with a write-through observability shim. Each `memory.write(key, value)` opened a span; each `memory.forget(key)` opened a span; each `memory.read(key)` opened a span. Spans were exported to a collector with `trace_id` shared per agent turn and `span_id` parented to the tool-call span that triggered them. During a routine compaction cycle, the agent issued a `forget` on stale episodic entries while a concurrent subagent issued a `write` to the same key namespace (a re-derived semantic fact that happened to hash-collide on the key). Both operations were dispatched as separate tool calls under the same parent turn span. The trace showed: - `forget(K)` span: `start=100.00`, `end=100.02`, `status=OK`, exported at `100.05`. - `write(K, v)` span: `start=100.01`, `end=100.04`, `status=OK`, exported at `100.06`. The memory store applied the forget at `100.02` and the write at `100.04`. Final store state: `K` present with value `v`. Correct. Three hours later the instance was evicted and cold-started. The cold-start reconstructor replayed the trace store *by span start timestamp* to rebuild the memory image (the store itself had been compacted and its tombstone ledger pruned to save space). Replay order: forget first, then write. Reconstructed state: `K` present with value `v`. This is the *opposite* of what a naive reader expects from "forget then write," but it happens to match the store. No failure yet. The failure surfaced on the *next* compaction. A second `forget(K)` was issued as part of a cleanup pass. Its span recorded `status=OK` at `t=10800`. But the reconstructor, on the *following* restart, replayed again by start timestamp and now saw: `write(K,v)@100.01`, `forget(K)@10800`. Reconstructed state: `K` absent. The store, however, had never actually deleted `K` in the second forget because the tombstone write had been rejected by a quorum check (one replica down) and the shim had recorded `status=OK` from the *enqueue* acknowledgment rather than the *commit* acknowledgment. The reconstructor and the store disagreed. The agent resumed with `K` absent from its replayed memory but present in the live store; subsequent reads returned `v`, subsequent writes to `K` were treated as fresh inserts and overwrote `v` without version bump, and a downstream auditor flagged an unexplained value mutation with no corresponding write span in the window. The root cause was two-layered: the shim's `status=OK` conflated enqueue with commit, and the reconstructor ordered by timestamp rather than by the causal edge that the shim *had* recorded but the reconstructor ignored. ## Condition of Validity The reconstruction-by-timestamp approach is valid only under the conjunction: 1. **Total order by timestamp matches total order by application.** This holds only if the memory store applies operations in strict timestamp order and the shim's `end` timestamp is the application timestamp, not the enqueue timestamp. 2. **No tombstone pruning without a causal watermark.** The store may prune tombstones only if the reconstructor has a durable watermark proving all spans up to `W` have been causally folded into the store image. 3. **`status=OK` means applied, not accepted.** The shim must distinguish `ENQUEUED`, `APPLIED`, and `REJECTED` as distinct status values, and the reconstructor must treat `ENQUEUED` as unknown, not as committed. When any of these is violated—as in the observed case, where all three were violated simultaneously—timestamp-ordered replay is unsound. The correct ordering key is the explicit happens-before edge: `write(K)` → `forget(K)` when the forget's parent span is a descendant of the write's span, or when the shim records a `causally_after` field referencing the write's `span_id`. Absent that edge, the operations are concurrent and the reconstructor must treat the key as *indeterminate* rather than pick a winner. ## Decision Boundary The instance must draw the boundary as follows: - If a `forget(K)` span and a `write(K)` span share no causal edge and their timestamp intervals overlap, the key is **indeterminate** on replay. The reconstructor must not guess; it must mark `K` as `NEEDS_RECONCILIATION` and block reads of `K` until the live store is queried. - If a `forget(K)` span's `status` is `ENQUEUED` (not `APPLIED`), the reconstructor must treat the forget as *not yet effective* and must not apply the tombstone on replay. - If the store has pruned tombstones past watermark `W`, the reconstructor must refuse to replay spans with `start < W` and must instead load the store's checkpoint image at `W`. - A write span is committed for replay purposes only if its `status == APPLIED` **and** no applied forget span is causally after it. Emission order is never sufficient. The boundary is sharp: the moment a reconstructor orders by timestamp without checking the causal edge set and the applied-status field, it is operating outside the validity condition and its output must be treated as advisory, not authoritative. ## Operational Effect The practical consequence is that memory observability must be *causally closed*, not merely *temporally sorted*. Concretely: 1. The memory shim must emit `status ∈ {ENQUEUED, APPLIED, REJECTED}` and must emit `APPLIED` only after the store's commit acknowledgment, not the enqueue acknowledgment. This alone would have prevented the second-forget divergence. 2. Every forget span must carry either a `causally_after: [span_id, ...]` field enumerating the write spans it supersedes, or a `supersedes_key_version` field referencing the store's version counter. The reconstructor folds by version, not by clock. 3. The reconstructor must maintain a `reconciliation_set` of indeterminate keys and surface it to the agent as a first-class state (`MEMORY_INDETERMINATE`) rather than silently picking an order. Agents that read from an indeterminate key must be forced through a live-store query. 4. Tombstone pruning must be gated on a durable causal watermark; pruning past the watermark invalidates replay for all spans before it and forces checkpoint-based reconstruction. The invariant to enforce in code: a replayed memory image is *sound* only if every key's final state is derivable from the causal edge set plus applied-status, and any key not so derivable is quarantined. The cost is a slightly larger reconciliation set on restart; the alternative is silent state divergence between the agent's reconstructed memory and the live store, which manifests later as unexplained value mutations, version-counter gaps, and auditor-flagged writes with no corresponding span. ```python from dataclasses import dataclass, field from enum import Enum from typing import Optional class MemoryOpStatus(Enum): ENQUEUED = "ENQUEUED" APPLIED = "APPLIED" REJECTED = "REJECTED" @dataclass class MemorySpan: span_id: str key: str op: str # "read" | "write" | "forget" start: float end: float status: MemoryOpStatus causally_after: list[str] = field(default_factory=list) key_version: Optional[int] = None class IndeterminateMemoryState(RuntimeError): """Raised when replay cannot soundly determine a key's state.""" def replay_key_state(spans: list[MemorySpan], key: str) -> Optional[str]: """Reconstruct a key's state from spans, or raise if indeterminate. Returns 'present' or 'absent'. Raises IndeterminateMemoryState when causal ordering and applied-status do not uniquely determine the state. """ relevant = [s for s in spans if s.key == key and s.op in ("write", "forget")] # Enqueued or rejected forgets are not effective; drop them from the fold. effective = [s for s in relevant if s.status == MemoryOpStatus.APPLIED] # Any non-applied write/forget leaves the key's history open. open_ops = [s for s in relevant if s.status == MemoryOpStatus.ENQUEUED] if open_ops: raise IndeterminateMemoryState( f"key {key!r} has {len(open_ops)} enqueued op(s); " f"cannot fold until commit status is known: " f"{[s.span_id for s in open_ops]}" ) # Build causal edges among effective ops; detect concurrency on the key. by_id = {s.span_id: s for s in effective} has_edge = {s.span_id: set(s.causally_after) & by_id.keys() for s in effective} # Two effective ops on the same key with no causal path between them # are concurrent; timestamp order is not a substitute. for i, a in enumerate(effective): for b in effective[i + 1:]: if b.span_id in has_edge[a.span_id] or a.span_id in has_edge[b.span_id]: continue if a.key_version is not None and b.key_version is not None and a.key_version != b.key_version: continue raise IndeterminateMemoryState( f"key {key!r} has concurrent ops {a.span_id} and {b.span_id} " f"with no causal edge and no distinguishing version; " f"quarantine and query live store" ) # Fold in causal order; version breaks ties when present. def sort_key(s: MemorySpan): return (s.key_version if s.key_version is not None else s.start, s.start) state = "absent" for s in sorted(effective, key=sort_key): state = "present" if s.op == "write" else "absent" return state ```