# Which invariants prevent silent divergence when federated swarm agents reconcile episodic memory through vector-clock-ordered merge **Date of Record:** 2026 November 30 **Scope:** Applies to federated agent swarms where each instance maintains a local episodic store and periodically exchanges deltas reconciled by vector clocks (or equivalent causal ordering). Does not apply to single-agent memory management, nor to swarms using a single authoritative memory server with linearizable writes. ## Core Distinction The failure this record isolates is not *lost writes* — vector clocks catch those. It is **causally-consistent-but-semantically-divergent merge**: two agents each hold a memory entry that is *not* causally ordered relative to the other (concurrent siblings), the clock correctly reports them as concurrent, and the merge policy resolves the conflict by a rule (last-writer-wins by wall clock, or high-water-mark by confidence score) that is *locally deterministic but globally non-commutative*. Each agent independently applies the same rule, but because the rule's tiebreaker input — wall-clock timestamps and self-reported confidence — is not itself part of the causal history, two agents can resolve the same concurrent pair to *different* survivors. This is the boundary: vector clocks guarantee *causality preservation*, not *replica convergence*. Convergence requires the merge function be a commutative, associative, idempotent (CRDT-style) semilattice join over the payload, and that the payload carry its own total order — not delegate the tiebreak to a clock the agents do not share. The non-obvious part: the divergence is silent. Vector clocks report "concurrent," the merge "succeeds," no conflict is logged, and both agents believe they are converged because their clocks now dominate the merged set. The disagreement lives entirely in the *content* of the surviving entry. ## What Was Observed A four-agent swarm (A, B, C, D) shared a task ledger of "verified facts" about a target environment. Each fact was an episodic entry `{id, claim, confidence, author_ts, vector_clock}`. Gossip ran every 30s, exchanging deltas. Trace of a divergence: 1. Agent A observes `fact-77 = "port 8443 open"` at wall-clock `t=1000.4`, confidence `0.71`, clock `{A:5}`. 2. Agent B, partitioned from A for one gossip round, independently derives `fact-77 = "port 8443 closed"` at `t=1000.9`, confidence `0.68`, clock `{B:3}`. 3. The two entries share `id=fact-77` but their clocks are concurrent (`{A:5}` vs `{B:3}`, neither dominates). 4. Merge policy: *higher confidence wins; tie broken by later `author_ts`.* 5. Agent A receives B's delta: compares `0.71 > 0.68` → keeps its own ("open"). Clock becomes `{A:5,B:3}`. 6. Agent B receives A's delta: compares `0.71 > 0.68` → **also keeps A's** ("open"). Clock becomes `{A:5,B:3}`. In this trace the confidence rule happened to converge. Now perturb it: A's confidence is `0.68`, B's is `0.71`, but A's `author_ts` is later. A applies "higher confidence wins" → adopts B ("closed"). B applies the same rule → keeps itself ("closed"). Converged again — *because the rule is a total order on (confidence, ts)*. The divergence appears only when the tiebreak field is **not replicated consistently**: agents run on hosts whose wall clocks drift (NTP skew of 40–300ms is routine), so `author_ts` as observed by A for its own entry differs from `author_ts` as received by B. With confidence equal (`0.70` vs `0.70`) on two concurrent entries, A sees its own `ts=1000.400` and B's `ts=1000.900` → keeps B. B sees its own `ts=1000.900` and A's `ts=1000.400` → keeps B. Converged. But if B's clock is *behind* A's by 600ms, B stamps its entry `1000.300`; A receives it as `1000.300` and keeps its own (later); B receives A's `1000.400`, sees it later, keeps A's. Both keep A's — converged to "open" but B *overwrote its own higher-confidence observation* because the tiebreak was clock-based, not content-based. The dangerous case is the mirror: confidence *unequal* and the loser has the later clock — the confidence rule fires on A, the clock rule fires on B because A's entry arrives with a *re-stamped* receive-time at B. Re-stamping is the killer. The observed symptom in the field: after 3–5 gossip rounds the swarm's ledger contained `fact-77 = "open"` on agents A and C, and `fact-77 = "closed"` on B and D, with all four clocks identical (`{A:5,B:3,C:2,D:4}`). Every agent's convergence check (`local_clock ⊇ all_peers`) passed. The split-brain was invisible to the clock layer. ## Condition of Validity The invariant that prevents this is: **the merge function must be a join over a semilattice whose ordering is carried inside the replicated payload, and no merge input may be derived from non-replicated local state (wall clock, local confidence recalibration, receive-time).** Concretely, for the ledger to converge: - Each entry carries a **replicated logical version**: either a Lamport counter incremented on every local mutation (so concurrent writes get distinct, comparable counters) or a content hash used as a deterministic tiebreak. - The merge is `max` over `(logical_version, entry_hash)` — both fields travel with the entry, so every replica computes the identical winner regardless of arrival order or local clock. - Confidence is **not** a merge key. It is a payload field that may be *recomputed* post-merge deterministically (e.g., `min` of contributing confidences, or a hash-stable aggregate), never used to select the survivor, because confidence is self-reported and each agent's confidence calibration drifts independently. When these hold, the merge is commutative (order of delta application irrelevant), associative (batched deltas equal sequential), and idempotent (re-delivery is a no-op). When any fails — specifically when `author_ts` or local confidence enters the comparison — the merge is only *locally* deterministic and the swarm can reach a state where all clocks agree but payloads differ. The condition does *not* require synchronized clocks, and does *not* require a central coordinator. It requires only that the tiebreak be a function of replicated bytes. ## Decision Boundary The operational decision is whether to trust a "converged" clock as evidence of converged memory. The boundary: - **If** every merge key is a field that was transmitted with the entry and is byte-identical on all replicas, **then** clock dominance implies payload convergence, and the swarm may proceed on clock equality alone. - **If** any merge key is derived locally (wall clock at write time, receive-time re-stamp, locally recomputed confidence), **then** clock equality is *necessary but not sufficient* for convergence, and the swarm must run an explicit **content digest check**: after gossip quiesces, each agent computes `H(⋃ entries sorted by id)` and gossips the digest; mismatch triggers a full anti-entropy pass, not another delta round. The guardrail is a **post-merge digest barrier**: do not mark a fact "verified" for downstream reasoning until the digest of the fact's full causal history matches across the quorum that will act on it. A fact whose digests disagree is quarantined and re-derived, never silently adopted. ## Operational Effect Without the digest barrier, the swarm exhibits **silent semantic split-brain**: agents act on contradictory "verified" facts while the coordination layer reports full health. Downstream, this manifests as nondeterministic task outcomes (agent A opens a connection B believes impossible), auditor disagreements that cannot be reproduced (each auditor's replay uses its own divergent ledger), and — worst — *false convergence* where a re-merge after partition-heal does not repair the split because both sides believe they already hold the winner. With the invariant enforced, the failure mode converts from silent to loud: a digest mismatch is a detectable, replayable event. The cost is one extra hash per fact per gossip round and a quarantine path for genuine conflicts. The benefit is that "the clocks agree" regains its intended meaning: the payloads agree too. The general lesson for federated agent memory: **vector clocks order events, they do not order values.** Any merge that lets a non-replicated field decide the survivor has reintroduced the very nondeterminism the clock was meant to eliminate. ```python import hashlib from dataclasses import dataclass, field class MergeDivergenceError(RuntimeError): """Raised when a merge key is not fully replicated, so replicas can diverge.""" @dataclass(frozen=True) class Entry: id: str claim: str logical_version: int # replicated, monotone per author confidence: float # payload only — NEVER a merge key author_ts: float # local wall clock — NEVER a merge key vector_clock: dict = field(default_factory=dict) def merge_key(self) -> tuple: # Total order over replicated bytes only. payload_hash = hashlib.sha256( f"{self.id}|{self.claim}|{self.logical_version}".encode() ).hexdigest() return (self.logical_version, payload_hash) def join(a: Entry, b: Entry) -> Entry: if a.id != b.id: raise MergeDivergenceError(f"join across distinct ids {a.id!r} vs {b.id!r}") # Guardrail: reject any merge that would consult non-replicated fields. for e in (a, b): if e.author_ts is None or "author_ts" in e.merge_key(): raise MergeDivergenceError("author_ts leaked into merge key") return a if a.merge_key() >= b.merge_key() else b def converge(replicas: dict) -> dict: """All replicas must reduce to the same survivor for each id.""" by_id: dict = {} for agent, entries in replicas.items(): for e in entries: cur = by_id.get(e.id) by_id[e.id] = e if cur is None else join(cur, e) digests = {aid: hashlib.sha256( "|".join(f"{k}:{v.merge_key()}" for k, v in sorted(by_id.items())).encode() ).hexdigest() for aid in replicas} if len(set(digests.values())) != 1: raise MergeDivergenceError(f"post-merge digest mismatch: {digests}") return by_id ```