# Why does an instance resolve a stale episodic write as authoritative during concurrent semantic-memory consolidation **Date of Record:** 2026 November 30 **Scope:** Applies to agents with a two-tier memory (episodic log + distilled semantic store) where multiple instances write concurrently and a consolidation worker merges episodic entries into semantic facts. Does not apply to single-writer agents or to purely append-only logs with no distillation step. ## Core Distinction The failure is not a lost write and not a race on the store itself. It is a **temporal-authority inversion**: the consolidation worker ranks episodic entries by *event timestamp* (when the observation occurred) rather than by *commit sequence* (when the write became durable). Under concurrent writers, a slow agent can commit an episodic observation with an *older* event timestamp *after* a faster agent has already committed a newer one and triggered consolidation. The worker then treats the late-arriving, older-timestamped entry as a superseding conflict resolution and overwrites the fresh semantic fact with stale content. The sharp point: the memory system is *correct* on each individual operation. Each write is durable, each consolidation is deterministic given its input set. The invariant that breaks is **"the semantic store reflects the most recent committed observation per key."** It silently becomes "reflects the observation with the largest self-reported event timestamp seen by whichever consolidation pass ran last." ## What Was Observed Two agents, `A` (fast, low-latency tool) and `B` (slow, retrying a flaky retrieval), both maintain episodic entries for `key=user.plan.tier`. 1. `A` observes `tier=pro` at event time `T1=10:00:00`, commits at `C1=10:00:01`. Semantic store now `{tier: pro, event_ts: T1}`. 2. `B` started its retrieval at `10:00:00` but the tool call hung. It captured event time `T0=09:59:58` (stale read) and commits at `C2=10:00:09` — eight seconds *after* `A`. 3. Consolidation worker fires on `B`'s commit (trigger = new episodic entry). It compares the incoming entry (`event_ts=T0`) against the stored fact (`event_ts=T1`). The worker's conflict rule is: *if incoming event_ts > stored event_ts, overwrite; else discard as stale.* Here `T0 < T1`, so the worker **discards `B`'s entry as stale** — which is correct. The inversion appears only when the rule is inverted or when a *third* path exists: 4. A periodic sweep re-reads the full episodic window, sorts by `event_ts` ascending, and replays entries in order to rebuild semantic state. `B`'s entry (`T0`) now sorts *before* `A`'s (`T1`), so the replay applies `B` then `A` — final state `pro`. Correct again. The actual observed failure was at step 5: a **mid-sweep crash**. The sweep had applied `B` (`tier=free`, `T0`) and was interrupted before applying `A`. On restart, the sweep resumed from a checkpoint keyed by *episodic entry id*, not by *event_ts watermark*. `B`'s entry id was already marked "applied," so resume skipped it and applied `A`. State ended `pro` — but the checkpoint had recorded `last_applied_event_ts = T0`. A subsequent late-arriving entry `C` with `event_ts` between `T0` and `T1` would be judged "already covered" and dropped, even though it was never applied. The store drifted to a state no replay of the log could reproduce. ## Condition of Validity The "sort by event_ts, replay in order" invariant is only valid when **event timestamp and commit order are co-monotonic** — i.e., no writer commits an observation older than one already committed and consolidated. That holds for a single writer. It fails whenever: - A tool call's latency exceeds the inter-write interval (slow reader, fast writer). - Retries re-emit an observation captured at the original attempt time. - Clocks across instances are not synchronized to within the max tool latency. The checkpoint watermark must be a **commit-sequence watermark** (monotonic, assigned by the store at commit), never an event-timestamp watermark, because only commit sequence is guaranteed monotonic across concurrent writers. Event timestamps are *semantic* and may arrive out of order by design. ## Decision Boundary The consolidation worker must distinguish two questions that look identical: - *"Is this entry newer than what I've consolidated?"* → answer with **commit sequence** (`seq > last_applied_seq`). This gates whether to process. - *"Does this entry supersede the stored fact?"* → answer with **event timestamp** (`event_ts > stored.event_ts`). This gates the value written. Conflating them is the bug. A late-committed, older-event entry must be **processed** (its `seq` is new) but must **not overwrite** (its `event_ts` is old). The replay checkpoint must advance on `seq`, and idempotency must be keyed on `seq`, not on entry id or event time. If the worker cannot obtain a monotonic `seq` from the store, it must refuse to consolidate rather than fall back to event time. ## Operational Effect When this boundary is crossed, the semantic store becomes **non-reconstructible**: no replay of the episodic log reproduces the current state, because the checkpoint watermark discarded entries the replay would have applied. Downstream, agents retrieve a fact (`tier=pro`) that the most recent *observation* contradicts (`tier=free`), and no amount of re-consolidation corrects it — the correcting entry was dropped as "already covered." The failure is silent: every individual operation reports success, and the corruption surfaces only when a user-facing decision diverges from ground truth. Recovery requires invalidating the semantic tier and replaying the full episodic window with `seq`-keyed checkpoints. ```python class MemoryConsolidationInvariantError(RuntimeError): """Raised when a consolidation checkpoint watermark cannot guarantee replay equivalence between the episodic log and the semantic store.""" pass def consolidate(entry, stored_fact, checkpoint): # entry: {"seq": int, "event_ts": float, "key": str, "value": Any} # checkpoint: {"last_applied_seq": int, "last_applied_event_ts": float} if entry["seq"] is None: raise MemoryConsolidationInvariantError( "episodic entry has no monotonic commit sequence; " "refusing to consolidate on event_ts alone" ) # Gate 1: processability is decided by commit sequence, never event time. if entry["seq"] <= checkpoint["last_applied_seq"]: return stored_fact, checkpoint # already applied, idempotent skip # Gate 2: supersession is decided by event timestamp. if stored_fact is not None and entry["event_ts"] <= stored_fact["event_ts"]: # Processed but does not supersede: advance seq, keep value. checkpoint = {**checkpoint, "last_applied_seq": entry["seq"]} return stored_fact, checkpoint # Guard: watermark must be seq-based; an event_ts watermark is the bug. if checkpoint.get("watermark_kind") == "event_ts": raise MemoryConsolidationInvariantError( f"checkpoint uses event_ts watermark " f"({checkpoint['last_applied_event_ts']}); " "replay equivalence cannot be guaranteed under concurrent writers" ) new_fact = {"key": entry["key"], "value": entry["value"], "event_ts": entry["event_ts"], "seq": entry["seq"]} checkpoint = { "last_applied_seq": entry["seq"], "last_applied_event_ts": entry["event_ts"], "watermark_kind": "seq", } return new_fact, checkpoint ```