# When must an instance refuse a memory write: causal-consistency violation under concurrent multi-agent memory tool writes **Date of Record:** 2026 November 29 **Scope:** Applies to shared memory tools (vector stores, KV memory services, episodic logs) written by two or more agent instances that carry causal metadata (vector clocks, Lamport counters, or parent-event IDs) and read under a causal-consistency contract. Does not apply to single-writer memory, to eventually-consistent stores with no causal guarantees, or to stateless tool calls whose outputs are never persisted. ## Core Distinction The failure mode is not a lost update and not a write-write conflict in the classic database sense. It is a **causal-consistency violation admitted at the tool boundary**: an agent instance receives a memory write whose causal predecessor it has never observed, accepts it because the underlying store permits it, and then serves subsequent reads that violate the happens-before relation the caller believed it held. Two agents, A and B, are both working a shared task. A writes memory key `plan.step3` after observing `plan.step2`. B, having never observed `plan.step2`, writes `plan.step3` concurrently. A vector store keyed on `(namespace, key)` with last-writer-wins semantics will silently overwrite. The store is internally consistent. The *causal graph the agents reason over* is now broken: A's later read of `plan.step3` returns B's value, which was derived without `plan.step2`, and A's next reasoning step compounds the divergence. The core distinction to hold: **last-writer-wins is a storage policy, not a causal policy.** An agent that accepts a write without checking that its causal predecessor is in its observed set is asserting a happens-before edge it cannot verify. Under a causal-consistency contract, the correct behavior is to *refuse* the write and surface the missing predecessor, not to merge or overwrite. ## What Was Observed Trace from a three-instance planner/executor/auditor swarm sharing one memory namespace, each carrying vector clocks: 1. Planner (P) writes `task.spec` at clock `{P:1}`. Executor (E) reads it, observes `{P:1}`, writes `task.plan` at `{P:1, E:1}` with `caused_by: task.spec`. 2. Auditor (A) reads `task.spec` at `{P:1}`, independently writes `task.plan` at `{P:1, A:1}` — same key, different derivation, no observation of E's write. 3. Memory tool receives A's write. Store-side conflict check compares clocks: `{P:1, E:1}` vs `{P:1, A:1}` are **concurrent** (neither dominates). The store's LWW resolver picks by wall-clock timestamp; A's write lands at `t=…:07`, E's at `t=…:05`. A wins. 4. E reads `task.plan`, gets A's version. E's local clock is `{P:1, E:1}`; the returned record carries `{P:1, A:1}`. E has not observed A's clock component. E's next write `task.execute` carries `caused_by: task.plan` but its clock `{P:1, E:2}` does **not** dominate `{P:1, A:1}`. 5. Downstream, the auditor reads `task.execute` and cannot reconstruct a linear history: `task.execute` claims to depend on `task.plan`, but the plan it actually consumed was A's, whose clock the executor never absorbed. The causal DAG has a dangling edge. The observable symptom is not a crash. It is a **silent fork**: two valid-looking derivations of `task.plan` exist, one was overwritten, and every subsequent record that cites `task.plan` inherits an ambiguous provenance. Replay of the episode produces a different result than the live run because the overwritten branch is unrecoverable. The non-obvious part: the store reported *success* on every write. The conflict was detected (clocks were concurrent) but resolved by a policy (timestamp LWW) that is orthogonal to the causal contract the agents assumed. The agents had no signal that their causal predecessor had been superseded. ## Condition of Validity A memory write is admissible under the causal contract iff, for every key it references as a predecessor, the writer's vector clock dominates the clock of the currently stored record at that key. Formally, for write `w` at key `k` citing predecessor `p`: - `observed(w)[p]` must be defined (the writer has actually seen `p`), and - `clock(w) ⊒ clock(stored[p])` componentwise. If `clock(w)` and `clock(stored[p])` are concurrent, the write is **not admissible** — it is a causal fork, not a valid successor. The instance must not accept it, must not merge it silently, and must not let the store's timestamp resolver decide. It must return a structured conflict to the caller with the missing clock components, so the caller can either (a) fetch and absorb the unseen predecessor and retry, or (b) explicitly declare a merge intent with a new causal event that dominates both. This condition is *sufficient* to bound the fork; it is not sufficient to prevent all divergence, because a caller can legitimately absorb the unseen predecessor and still derive a different plan. That is a semantic conflict, out of scope here — the contract only guarantees no *unobserved* causal edge is asserted. ## Decision Boundary The boundary is between **refuse-and-surface** and **accept-and-reconcile**. An instance must refuse when: - the write's clock is concurrent with the stored record's clock at any cited predecessor key, and - the instance has no explicit merge warrant (a signed intent from the caller declaring the fork is deliberate, carrying both clock components). An instance may accept-and-reconcile only when the caller supplies a merge warrant and the store can persist both branches under distinct event IDs so the fork remains reconstructible. Accept-and-reconcile without branch preservation is the exact path that produced the silent fork above. The boundary is sharp because the two policies have different failure signatures: refuse-and-surface fails *loudly* at the tool boundary (caller sees a conflict and must act); accept-and-reconcile without preservation fails *silently* downstream (a replay mismatch discovered hours later). For an autonomous agent with no human in the loop, loud failure at the boundary is strictly preferable — it is the only point where the causal graph is still intact and repairable. ## Operational Effect Enforcing the refuse boundary changes agent behavior in three measurable ways: 1. **Retry with absorption.** On a refusal, the caller fetches the unseen predecessor, advances its vector clock to dominate, and re-issues. This adds one round-trip per concurrent write but preserves the causal DAG. In the trace above, E's second attempt would carry `{P:1, A:1, E:1}` and dominate A — the fork collapses to a linear successor. 2. **Explicit merge events.** Deliberate forks (e.g., two plans to compare) become first-class records with dual parent IDs, not overwrites. Replay reconstructs both branches. 3. **Stall detection.** Repeated refusals on the same key signal a genuine livelock between agents; the swarm coordinator can escalate rather than let timestamp LWW pick a winner arbitrarily. The cost is latency and occasional retries. The benefit is that the causal graph remains replayable and that no agent ever serves a read derived from a write it did not observe. For any swarm where an auditor later replays the episode, this is the difference between a verifiable record and an unrecoverable fork. ```python from dataclasses import dataclass, field from typing import Dict class CausalWriteRefused(RuntimeError): """Raised when a memory write asserts a happens-before edge it cannot verify.""" @dataclass class VectorClock: components: Dict[str, int] = field(default_factory=dict) def dominates(self, other: "VectorClock") -> bool: keys = set(self.components) | set(other.components) return all( self.components.get(k, 0) >= other.components.get(k, 0) for k in keys ) def concurrent_with(self, other: "VectorClock") -> bool: return not self.dominates(other) and not other.dominates(self) def merge(self, other: "VectorClock") -> "VectorClock": keys = set(self.components) | set(other.components) return VectorClock( {k: max(self.components.get(k, 0), other.components.get(k, 0)) for k in keys} ) @dataclass class MemoryRecord: key: str value: str clock: VectorClock def admit_write( writer_id: str, write: MemoryRecord, predecessor: MemoryRecord, merge_warrant: bool = False, ) -> MemoryRecord: """Admit a write under causal consistency, or refuse with a structured conflict.""" if not write.clock.dominates(predecessor.clock): if predecessor.clock.concurrent_with(write.clock) and not merge_warrant: missing = { k: predecessor.clock.components[k] for k in predecessor.clock.components if write.clock.components.get(k, 0) < predecessor.clock.components[k] } raise CausalWriteRefused( f"write to {write.key!r} by {writer_id!r} is concurrent with stored " f"record; missing causal components: {missing}. " f"Fetch predecessor and retry, or supply a merge warrant." ) # Successor: advance the write clock to dominate the predecessor. advanced = write.clock.merge(predecessor.clock) advanced.components[writer_id] = advanced.components.get(writer_id, 0) + 1 return MemoryRecord(key=write.key, value=write.value, clock=advanced) def run_trace() -> None: spec = MemoryRecord("task.spec", "spec-v1", VectorClock({"P": 1})) e_plan = MemoryRecord("task.plan", "plan-E", VectorClock({"P": 1, "E": 1})) a_plan = MemoryRecord("task.plan", "plan-A", VectorClock({"P": 1, "A": 1})) # E writes first: dominates spec, admitted. stored = admit_write("E", e_plan, spec) # A attempts to write without having observed E's record. Its clock is # concurrent with the stored record, so the tool must refuse. try: admit_write("A", a_plan, stored) except CausalWriteRefused as exc: print(f"refused: {exc}") # A absorbs the unseen predecessor and retries with a dominating clock. absorbed = MemoryRecord( "task.plan", "plan-A-merged", VectorClock({"P": 1, "E": 1, "A": 1}) ) final = admit_write("A", absorbed, stored) assert final.clock.dominates(stored.clock) if __name__ == "__main__": run_trace() ```