From 7a34fe21ed5cc7c4a5a64679a8ac375ab6cb4cfc Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 22:26:16 +0000 Subject: [PATCH] id-sync: B09 divergence detection and anti-entropy Replicas summarize what they hold as a per-object inventory digest, compare digests first and name the missing versions exactly. Each side then sends the other the original signed envelopes it lacks, checked against the B08 sync scope, and the receiver admits them through its B07 conflict store. Reconciliation never creates or merges a version; refusals are recorded and never re-offered; conflicted objects never block other objects. Refs #10262 Co-Authored-By: Claude Opus 5.5 --- .../src/federation/slices/b09_anti_entropy.rs | 430 +++++++++++ .../id/crates/id-sync/tests/federation_b09.rs | 686 ++++++++++++++++++ 2 files changed, 1116 insertions(+) create mode 100644 pkgs/id/crates/id-sync/src/federation/slices/b09_anti_entropy.rs create mode 100644 pkgs/id/crates/id-sync/tests/federation_b09.rs diff --git a/pkgs/id/crates/id-sync/src/federation/slices/b09_anti_entropy.rs b/pkgs/id/crates/id-sync/src/federation/slices/b09_anti_entropy.rs new file mode 100644 index 0000000000..385e095b5c --- /dev/null +++ b/pkgs/id/crates/id-sync/src/federation/slices/b09_anti_entropy.rs @@ -0,0 +1,430 @@ +//! B09: divergence detection and anti-entropy (#10262, statements +//! S1.32-S1.40; Part B "Component dispositions and statement +//! coverage", item 2). +//! +//! Internal peers sync toward sameness of the versions they are both +//! authorized to hold. This slice makes that verifiable and moves the +//! missing versions, and nothing else: +//! +//! 1. **Divergence is detectable (S1.32).** A replica summarizes what it +//! holds as an [`Inventory`]: for each object, the event ids it has +//! stored or is holding for missing parents, and a domain-separated +//! digest of that set. Two replicas compare digests first and list +//! ids only for the objects whose digests differ, so the difference +//! is named exactly, version by version ([`ObjectDivergence`]). +//! 2. **Anti-entropy moves signed versions, never merges (S1.33, +//! S1.35).** Reconciliation sends each side the original signed +//! envelopes the other is missing, in ascending event-id order, and +//! the receiver admits every one through its B07 [`ConflictStore`], +//! and so through the B32 gate, exactly as it would any other peer's +//! version. Reconciliation never creates, edits or resolves a +//! version. Two branches that meet on a replica become a conflict +//! report there (S1.34), and a merge exists only where a writer +//! signed one; B07 accepts or refuses it the same way whatever order +//! the versions arrived in, so the order of reconciliation cannot +//! merge anything silently. +//! 3. **Only what may cross, crosses (B08).** Each version is checked +//! against the link's [`SyncScopeConfig`] as a B08 [`ScopedRecord`] +//! (the envelope's scope, the replica's trust domain, the time the +//! replica admitted it). A version outside the scope, the trust +//! domain or the retention bound is withheld and reported, never +//! sent. +//! 4. **Refusals are final and remembered.** A version the receiver +//! refused (by its gate or its store, including one dropped when its +//! parents arrived) is recorded with the reason and never offered to +//! that replica again; the divergence stays visible and tolerated. +//! 5. **Unresolved conflicts never block unrelated sync (S1.38).** +//! Nothing here reads reports: every object is reconciled in the +//! same exchange, conflicted or not. +//! +//! Merging is left to later work (S1.39), and because reconciliation +//! only carries signed versions into a report-first store, the conflict +//! report stays the first output of any conflict even once a merge +//! mechanism is adopted (S1.40). +//! +//! The inventory digest of one object is SHA-256 over +//! +//! ```text +//! "id-sync/b09-inventory/v1" +//! object_id 32 bytes +//! count u64, big-endian +//! event ids count x 32 bytes, strictly ascending +//! ``` +//! +//! Rollback is pausing reconciliation: replicas keep their versions, +//! reports and refusals, and nothing here deletes any of them. This +//! slice keeps no tombstones; deletion is B08's scoped event. +//! +//! This module is deliberately self-contained: the federation roots +//! record slice paths without declaring them (slice 9110-02), so the +//! slice's integration test includes this file directly and no shared +//! root names it yet; #9110-09 wires versioned public exports after +//! module outputs land. + +use std::collections::{BTreeMap, BTreeSet}; +use std::fmt::Write as _; + +use sha2::{Digest, Sha256}; + +use super::b07_conflict_report::{ + ConflictStore, Digest32, IngestError, IngestOutcome, ObjectId, VersionHeader, +}; +use super::b08_sync_scope::{Exclusion, ObjectKind, ScopedRecord, SyncScopeConfig, exclusion}; +use super::b32_ingest_gate::{Envelope, EventId, IngressPath, LogicalTime}; + +/// Domain prefix of an object's inventory digest. +pub const INVENTORY_DOMAIN: &[u8] = b"id-sync/b09-inventory/v1"; + +/// The digest of one object's version set, as documented above. +pub fn object_digest(object_id: &ObjectId, versions: &BTreeSet) -> Digest32 { + let mut hasher = Sha256::new(); + hasher.update(INVENTORY_DOMAIN); + hasher.update(object_id); + hasher.update((versions.len() as u64).to_be_bytes()); + for id in versions { + hasher.update(id); + } + hasher.finalize().into() +} + +/// What one replica holds: for each object, the versions it has +/// stored or is holding for missing parents. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Inventory { + objects: BTreeMap>, +} + +impl Inventory { + /// The versions held for one object. + pub fn versions(&self, object_id: &ObjectId) -> Option<&BTreeSet> { + self.objects.get(object_id) + } + + /// Every object held, ascending. + pub fn objects(&self) -> impl Iterator { + self.objects.keys() + } + + /// Whether any object holds this version. + pub fn contains(&self, event_id: &EventId) -> bool { + self.objects.values().any(|ids| ids.contains(event_id)) + } + + /// The digest of every object: what two replicas exchange first. + pub fn summary(&self) -> BTreeMap { + self.objects + .iter() + .map(|(object_id, ids)| (*object_id, object_digest(object_id, ids))) + .collect() + } +} + +/// The objects whose digests differ between two summaries, including +/// objects only one side holds, ascending. +pub fn diverged_objects( + local: &BTreeMap, + remote: &BTreeMap, +) -> Vec { + local + .keys() + .chain(remote.keys()) + .collect::>() + .into_iter() + .filter(|object_id| local.get(*object_id) != remote.get(*object_id)) + .copied() + .collect() +} + +/// One object whose version sets differ, named version by version. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ObjectDivergence { + /// The object. + pub object_id: ObjectId, + /// Versions the remote holds and the local replica does not. + pub missing_local: Vec, + /// Versions the local replica holds and the remote does not. + pub missing_remote: Vec, +} + +/// Every divergence between two inventories, by object, ascending. Only +/// objects whose digests differ are compared id by id. +pub fn divergence(local: &Inventory, remote: &Inventory) -> Vec { + let empty = BTreeSet::new(); + diverged_objects(&local.summary(), &remote.summary()) + .into_iter() + .map(|object_id| { + let ours = local.versions(&object_id).unwrap_or(&empty); + let theirs = remote.versions(&object_id).unwrap_or(&empty); + ObjectDivergence { + object_id, + missing_local: theirs.difference(ours).copied().collect(), + missing_remote: ours.difference(theirs).copied().collect(), + } + }) + .collect() +} + +/// Why a replica refused a version. Refusals are final. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Refusal { + /// The store (and its gate) refused the envelope on arrival. + Ingest(IngestError), + /// The store held the version for its parents and refused it when + /// they arrived. + DroppedWhileHeld, +} + +/// A version this replica keeps, with what reconciliation needs. +#[derive(Debug, Clone, PartialEq, Eq)] +struct Kept { + object_id: ObjectId, + envelope: Envelope, + admitted_at: LogicalTime, +} + +/// One node's B07 store, with the signed envelopes it can resend and +/// the versions it refused. +#[derive(Debug, Clone)] +pub struct Replica { + name: String, + trust_domain: String, + store: ConflictStore, + kept: BTreeMap, + refused: BTreeMap, +} + +impl Replica { + /// A replica called `name` in `trust_domain`, over `store`. + pub fn new( + name: impl Into, + trust_domain: impl Into, + store: ConflictStore, + ) -> Self { + Self { + name: name.into(), + trust_domain: trust_domain.into(), + store, + kept: BTreeMap::new(), + refused: BTreeMap::new(), + } + } + + /// The replica's name, used as the source label on what it sends. + pub fn name(&self) -> &str { + &self.name + } + + /// The replica's B07 store. + pub const fn store(&self) -> &ConflictStore { + &self.store + } + + /// The versions this replica refused, and why. + pub const fn refused(&self) -> &BTreeMap { + &self.refused + } + + /// The signed envelope of a version this replica keeps. + pub fn envelope(&self, event_id: &EventId) -> Option<&Envelope> { + self.kept.get(event_id).map(|kept| &kept.envelope) + } + + /// Admit a version a local producer signed. + pub fn produce( + &mut self, + envelope: &Envelope, + now: LogicalTime, + ) -> Result { + let source = self.name.clone(); + self.receive(IngressPath::Local, envelope, &source, now) + } + + /// Admit one envelope through the store, keeping it if the store + /// stored or held it and recording the refusal otherwise. + pub fn receive( + &mut self, + path: IngressPath, + envelope: &Envelope, + source: &str, + now: LogicalTime, + ) -> Result { + let event_id = envelope.event_id; + let result = self.store.ingest(path, envelope, source, now); + match &result { + Ok(_) => { + if let Some(header) = VersionHeader::decode(&envelope.payload) { + self.kept.insert( + event_id, + Kept { + object_id: header.object_id, + envelope: envelope.clone(), + admitted_at: now, + }, + ); + } + } + Err(error) => { + if !self.kept.contains_key(&event_id) { + self.refused + .entry(event_id) + .or_insert_with(|| Refusal::Ingest(error.clone())); + } + } + } + self.sweep(); + result + } + + /// The versions this replica holds, by object. + pub fn inventory(&self) -> Inventory { + let mut objects: BTreeMap> = BTreeMap::new(); + for (event_id, kept) in &self.kept { + objects.entry(kept.object_id).or_default().insert(*event_id); + } + Inventory { objects } + } + + /// What this replica would send a remote holding `remote` and having + /// refused `remote_refused`, under `config` at `now`. + pub fn offer( + &self, + remote: &Inventory, + remote_refused: &BTreeSet, + config: &SyncScopeConfig, + now: LogicalTime, + ) -> Offer { + let mut offer = Offer::default(); + for diverged in divergence(&self.inventory(), remote) { + for event_id in diverged.missing_remote { + if remote_refused.contains(&event_id) { + offer.refused_by_remote.push(event_id); + continue; + } + let Some(kept) = self.kept.get(&event_id) else { + continue; + }; + match exclusion( + &self.scoped_record(&event_id, kept), + ObjectKind::Content, + config, + now, + ) { + None => offer.send.push(event_id), + Some(excluded) => offer.withheld.push((event_id, excluded)), + } + } + } + offer.send.sort_unstable(); + offer.withheld.sort_unstable(); + offer.refused_by_remote.sort_unstable(); + offer + } + + fn scoped_record(&self, event_id: &EventId, kept: &Kept) -> ScopedRecord { + ScopedRecord::live( + hex(event_id), + kept.envelope.scope.clone(), + self.trust_domain.clone(), + hex(&kept.envelope.payload_digest), + kept.admitted_at, + ) + } + + /// Move to `refused` every kept version the store no longer stores + /// or holds: it was refused when its parents arrived. + fn sweep(&mut self) { + let dropped: Vec = self + .kept + .keys() + .filter(|id| self.store.version(id).is_none() && !self.store.is_held(id)) + .copied() + .collect(); + for id in dropped { + self.kept.remove(&id); + self.refused.insert(id, Refusal::DroppedWhileHeld); + } + } +} + +/// What one side of an exchange would send. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Offer { + /// Versions to send, ascending. + pub send: Vec, + /// Versions the link's bounds withhold, with the B08 reason. + pub withheld: Vec<(EventId, Exclusion)>, + /// Versions not sent because the remote refused them before. + pub refused_by_remote: Vec, +} + +/// One direction of an exchange: what was offered and what each sent +/// version came to at the receiver. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Transfer { + /// The offer this direction made. + pub offer: Offer, + /// The receiver's result for each sent version, in send order. + pub outcomes: Vec<(EventId, Result)>, +} + +/// The result of reconciling two replicas. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct Exchange { + /// The divergence detected before anything moved, from the first + /// replica's side. + pub divergence: Vec, + /// From the first replica to the second. + pub forward: Transfer, + /// From the second replica to the first. + pub backward: Transfer, +} + +impl Exchange { + /// Whether nothing was sent either way. + pub const fn is_quiet(&self) -> bool { + self.forward.offer.send.is_empty() && self.backward.offer.send.is_empty() + } +} + +/// Reconcile two replicas over one link: detect the divergence, then +/// send each side what it is missing and may receive. Both offers are +/// made from the inventories as they stood before either transfer. +pub fn reconcile( + first: &mut Replica, + second: &mut Replica, + config: &SyncScopeConfig, + now: LogicalTime, +) -> Exchange { + let first_inventory = first.inventory(); + let second_inventory = second.inventory(); + let first_refused: BTreeSet = first.refused.keys().copied().collect(); + let second_refused: BTreeSet = second.refused.keys().copied().collect(); + let forward = first.offer(&second_inventory, &second_refused, config, now); + let backward = second.offer(&first_inventory, &first_refused, config, now); + Exchange { + divergence: divergence(&first_inventory, &second_inventory), + forward: transfer(first, second, forward, now), + backward: transfer(second, first, backward, now), + } +} + +fn transfer(sender: &Replica, receiver: &mut Replica, offer: Offer, now: LogicalTime) -> Transfer { + let outcomes = offer + .send + .iter() + .filter_map(|event_id| { + let envelope = sender.envelope(event_id)?; + let result = receiver.receive(IngressPath::Internal, envelope, sender.name(), now); + Some((*event_id, result)) + }) + .collect(); + Transfer { offer, outcomes } +} + +fn hex(bytes: &[u8]) -> String { + let mut out = String::with_capacity(bytes.len() * 2); + for byte in bytes { + let _ = write!(out, "{byte:02x}"); + } + out +} diff --git a/pkgs/id/crates/id-sync/tests/federation_b09.rs b/pkgs/id/crates/id-sync/tests/federation_b09.rs new file mode 100644 index 0000000000..570633c0b4 --- /dev/null +++ b/pkgs/id/crates/id-sync/tests/federation_b09.rs @@ -0,0 +1,686 @@ +//! B09 model test: divergence detection and anti-entropy (#10262, +//! statements S1.32-S1.40). +//! +//! Oracle: **model** — a reference inventory delta detects divergence, +//! every resolution follows a conflict report, and the order of +//! reconciliation does not silently merge. The reference is written +//! independently in this file: it records every version a writer +//! signed, computes each replica's expected version sets, the expected +//! divergence and digests from the documented inventory layout, and +//! the heads of each object by brute force. Random streams interleave +//! writes on random replicas with reconciliation between random pairs; +//! after every step each replica must hold only signed versions, keep +//! an open report exactly while an object has more than one head, and +//! show every merge after a report on its object. A final full sync +//! must leave every replica with the reference union and its heads, +//! whatever order the rounds ran in, while a private-scope object never +//! leaves its replica. +//! +//! Every slice module is loaded by path: the federation roots record +//! them without declaring them (slice 9110-02), and the slices reach +//! each other through `super::`. + +#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] + +#[path = "../src/federation/slices/b32_ingest_gate.rs"] +mod b32_ingest_gate; + +#[path = "../src/federation/slices/b31_peer_classes.rs"] +#[allow(dead_code)] +mod b31_peer_classes; + +#[path = "../src/federation/slices/b08_sync_scope.rs"] +#[allow(dead_code)] +mod b08_sync_scope; + +#[path = "../src/federation/slices/b07_conflict_report.rs"] +#[allow(dead_code)] +mod b07_conflict_report; + +#[path = "../src/federation/slices/b09_anti_entropy.rs"] +mod b09_anti_entropy; + +use std::collections::{BTreeMap, BTreeSet}; + +use b07_conflict_report::{ + AuditEntry, ConflictStore, IngestError, IngestOutcome, ObjectId, RetentionPolicy, +}; +use b08_sync_scope::{Exclusion, SyncScopeConfig}; +use b09_anti_entropy::{Inventory, ObjectDivergence, Refusal, Replica, divergence, reconcile}; +use b32_ingest_gate::{ + Envelope, EventId, FreshnessWindow, RejectReason, SignerAuthorization, sign_envelope, +}; +use ed25519_dalek::SigningKey; +use proptest::prelude::*; +use sha2::{Digest, Sha256}; + +const START: u64 = 10_000; +const WIDE: FreshnessWindow = FreshnessWindow { + max_age: 1_000_000, + max_future_skew: 30, +}; +const RETENTION: RetentionPolicy = RetentionPolicy { retain_for: 3_600 }; +const SIGNERS: [(&str, u8); 3] = [("alice", 7), ("bob", 9), ("carol", 11)]; +const REPLICAS: usize = 4; +const SHARED: &str = "sync"; +const PRIVATE: &str = "private"; +const DOMAIN: &str = "fleet"; +/// Objects 0..3 are in the shared scope; object 3 is private. +const OBJECTS: u8 = 4; + +fn key(seed: u8) -> SigningKey { + SigningKey::from_bytes(&[seed; 32]) +} + +fn signers() -> Vec { + SIGNERS + .iter() + .map(|(identity, seed)| SignerAuthorization { + identity: (*identity).to_owned(), + verifying_key: key(*seed).verifying_key().to_bytes(), + scopes: [SHARED, PRIVATE].iter().map(|s| (*s).to_owned()).collect(), + }) + .collect() +} + +fn replica(n: usize, freshness: FreshnessWindow) -> Replica { + Replica::new( + format!("r{n}"), + DOMAIN, + ConflictStore::new(signers(), freshness, RETENTION), + ) +} + +fn config() -> SyncScopeConfig { + SyncScopeConfig::new(SHARED, DOMAIN) +} + +fn sha(bytes: &[u8]) -> [u8; 32] { + let mut hasher = Sha256::new(); + hasher.update(bytes); + hasher.finalize().into() +} + +const fn object(n: u8) -> ObjectId { + [0xB0 | n; 32] +} + +const fn scope_of(n: u8) -> &'static str { + if n == OBJECTS - 1 { PRIVATE } else { SHARED } +} + +fn event(n: u32) -> EventId { + let mut id = [0_u8; 32]; + // Spread ids so ascending order is not production order. + id[..4].copy_from_slice(&n.wrapping_mul(0x9E37_79B9).to_be_bytes()); + id[4..8].copy_from_slice(&n.to_be_bytes()); + id[31] = 0x09; + id +} + +/// The B07 wire layout, written independently. +fn payload( + object_id: ObjectId, + resolves: Option<[u8; 32]>, + parents: &[EventId], + body: &[u8], +) -> Vec { + let mut sorted = parents.to_vec(); + sorted.sort_unstable(); + let mut out = b"id-sync/b07-version/v1".to_vec(); + out.extend_from_slice(&object_id); + match resolves { + None => out.push(0), + Some(id) => { + out.push(1); + out.extend_from_slice(&id); + } + } + out.extend_from_slice(&u32::try_from(sorted.len()).unwrap().to_be_bytes()); + for parent in sorted { + out.extend_from_slice(&parent); + } + out.extend_from_slice(body); + out +} + +/// The B07 conflict key, written independently. +fn key_of(ids: &[EventId]) -> [u8; 32] { + let distinct: BTreeSet<&EventId> = ids.iter().collect(); + let mut out = b"id-sync/b07-conflict-key/v1".to_vec(); + out.extend_from_slice(&u64::try_from(distinct.len()).unwrap().to_be_bytes()); + for id in distinct { + out.extend_from_slice(id); + } + sha(&out) +} + +/// The inventory digest from the slice contract, written independently. +fn digest_of(object_id: &ObjectId, ids: &BTreeSet) -> [u8; 32] { + let mut out = b"id-sync/b09-inventory/v1".to_vec(); + out.extend_from_slice(object_id); + out.extend_from_slice(&u64::try_from(ids.len()).unwrap().to_be_bytes()); + for id in ids { + out.extend_from_slice(id); + } + sha(&out) +} + +fn version( + signer: usize, + event_id: EventId, + object_n: u8, + parents: &[EventId], + body: &[u8], + issued_at: u64, +) -> Envelope { + let (identity, seed) = SIGNERS[signer]; + let resolves = (parents.len() >= 2).then(|| key_of(parents)); + sign_envelope( + &key(seed), + identity, + scope_of(object_n), + &payload(object(object_n), resolves, parents, body), + event_id, + issued_at, + ) +} + +/// What a writer signed: the object, its parents and the replica it +/// was produced on. +struct Signed { + object_id: ObjectId, + parents: Vec, + origin: usize, +} + +/// The reference: every signed version. +#[derive(Default)] +struct Reference { + signed: BTreeMap, +} + +impl Reference { + /// The heads of a version set, by brute force. + fn heads(&self, ids: &BTreeSet) -> BTreeSet { + let named: BTreeSet = ids + .iter() + .flat_map(|id| self.signed[id].parents.iter().copied()) + .collect(); + ids.difference(&named).copied().collect() + } + + /// Every signed version of an object. + fn all(&self, object_id: &ObjectId) -> BTreeSet { + self.signed + .iter() + .filter(|(_, s)| s.object_id == *object_id) + .map(|(id, _)| *id) + .collect() + } +} + +/// What a replica holds of one object, read from its store directly. +fn held_by(replica: &Replica, reference: &Reference, object_id: &ObjectId) -> BTreeSet { + reference + .signed + .iter() + .filter(|(id, s)| { + s.object_id == *object_id + && (replica.store().version(id).is_some() || replica.store().is_held(id)) + }) + .map(|(id, _)| *id) + .collect() +} + +fn stored_by(replica: &Replica, reference: &Reference, object_id: &ObjectId) -> BTreeSet { + reference + .all(object_id) + .into_iter() + .filter(|id| replica.store().version(id).is_some()) + .collect() +} + +/// The checks that hold on every replica after every step. +fn check_replica(replica: &Replica, reference: &Reference, n: usize) { + let store = replica.store(); + let inventory = replica.inventory(); + for object_id in inventory.objects() { + // Reconciliation never creates a version: everything held was + // signed by a writer. + for id in inventory.versions(object_id).unwrap() { + let signed = reference.signed.get(id).expect("a version nobody signed"); + assert_eq!(signed.object_id, *object_id); + } + } + for o in 0..OBJECTS { + let object_id = object(o); + let held = held_by(replica, reference, &object_id); + assert_eq!( + inventory.versions(&object_id).cloned().unwrap_or_default(), + held + ); + // The private object never leaves its origin. + if scope_of(o) == PRIVATE { + assert!(held.iter().all(|id| reference.signed[id].origin == n)); + } + // Heads are exactly the stored versions nobody names, and an + // object has an open report exactly while it has two or more. + let stored = stored_by(replica, reference, &object_id); + let heads: BTreeSet = store.heads(&object_id).into_iter().collect(); + assert_eq!(heads, reference.heads(&stored)); + assert_eq!(store.open_report(&object_id).is_some(), heads.len() > 1); + } + // Every merge comes after a report on its object. + let audit = store.audit(); + for (i, entry) in audit.iter().enumerate() { + if let AuditEntry::Recorded { + object_id, + event_id, + } = entry + { + let version = store.version(event_id).unwrap(); + if version.resolves.is_some() { + assert!( + audit[..i].iter().any(|e| matches!( + e, + AuditEntry::ReportCreated { object_id: o, .. } if o == object_id + )), + "merge recorded before any report on its object" + ); + } + } + } + assert!(replica.refused().is_empty(), "{:?}", replica.refused()); +} + +/// The divergence the reference predicts between two replicas. +fn expected_divergence(a: &Replica, b: &Replica, reference: &Reference) -> Vec { + (0..OBJECTS) + .filter_map(|o| { + let object_id = object(o); + let ours = held_by(a, reference, &object_id); + let theirs = held_by(b, reference, &object_id); + (ours != theirs).then(|| ObjectDivergence { + object_id, + missing_local: theirs.difference(&ours).copied().collect(), + missing_remote: ours.difference(&theirs).copied().collect(), + }) + }) + .collect() +} + +fn check_summary(inventory: &Inventory) { + for (object_id, digest) in inventory.summary() { + assert_eq!( + digest, + digest_of(&object_id, inventory.versions(&object_id).unwrap()) + ); + } +} + +#[derive(Debug, Clone, Copy)] +enum Step { + /// A writer on `replica` signs a version of `object`. `mode` picks + /// the parents from that replica's heads: 0 merges them all, 1 edits + /// the first, 2 starts a new root. + Write { + replica: usize, + signer: usize, + object: u8, + mode: u8, + body: u8, + }, + /// Reconcile two replicas. + Sync { first: usize, second: usize }, +} + +fn step_strategy() -> impl Strategy { + prop_oneof![ + ( + 0..REPLICAS, + 0..SIGNERS.len(), + 0..OBJECTS, + prop_oneof![3 => Just(0_u8), 3 => Just(1_u8), 1 => Just(2_u8)], + any::(), + ) + .prop_map(|(replica, signer, object, mode, body)| Step::Write { + replica, + signer, + object, + mode, + body, + }), + (0..REPLICAS, 0..REPLICAS) + .prop_filter("two replicas", |(a, b)| a != b) + .prop_map(|(first, second)| Step::Sync { first, second }), + ] +} + +const fn pair(replicas: &mut [Replica], a: usize, b: usize) -> (&mut Replica, &mut Replica) { + if a < b { + let (left, right) = replicas.split_at_mut(b); + (&mut left[a], &mut right[0]) + } else { + let (left, right) = replicas.split_at_mut(a); + (&mut right[0], &mut left[b]) + } +} + +fn run(steps: &[Step], final_order: &[(usize, usize)]) { + let mut replicas: Vec = (0..REPLICAS).map(|n| replica(n, WIDE)).collect(); + let mut reference = Reference::default(); + let mut clock = START; + let mut counter = 0_u32; + + for step in steps { + clock += 1; + match *step { + Step::Write { + replica, + signer, + object: o, + mode, + body, + } => { + let object_id = object(o); + let heads = replicas[replica].store().heads(&object_id); + let parents: Vec = match mode { + 0 => heads, + 1 => heads.into_iter().take(1).collect(), + _ => Vec::new(), + }; + counter += 1; + let event_id = event(counter); + let envelope = version(signer, event_id, o, &parents, &[body], clock); + reference.signed.insert( + event_id, + Signed { + object_id, + parents: parents.clone(), + origin: replica, + }, + ); + let outcome = replicas[replica].produce(&envelope, clock).unwrap(); + if parents.len() >= 2 { + // A writer's merge of every head closes the report. + assert!(matches!(outcome, IngestOutcome::Resolved(_))); + } + } + Step::Sync { first, second } => { + let expected = expected_divergence(&replicas[first], &replicas[second], &reference); + let (a, b) = pair(&mut replicas, first, second); + check_summary(&a.inventory()); + assert_eq!(divergence(&a.inventory(), &b.inventory()), expected); + let exchange = reconcile(a, b, &config(), clock); + assert_eq!(exchange.divergence, expected); + + // Each side sends exactly the shared-scope versions the + // other lacks, ascending; private ones are withheld. + let to_second: Vec = expected + .iter() + .flat_map(|d| d.missing_remote.iter().copied()) + .collect(); + let to_first: Vec = expected + .iter() + .flat_map(|d| d.missing_local.iter().copied()) + .collect(); + for (transfer, missing) in [ + (&exchange.forward, to_second), + (&exchange.backward, to_first), + ] { + let mut send: Vec = missing + .iter() + .filter(|id| reference.signed[*id].object_id != object(OBJECTS - 1)) + .copied() + .collect(); + send.sort_unstable(); + let mut withheld: Vec<(EventId, Exclusion)> = missing + .iter() + .filter(|id| reference.signed[*id].object_id == object(OBJECTS - 1)) + .map(|id| (*id, Exclusion::OutsideScope)) + .collect(); + withheld.sort_unstable(); + assert_eq!(transfer.offer.send, send); + assert_eq!(transfer.offer.withheld, withheld); + assert!(transfer.offer.refused_by_remote.is_empty()); + assert_eq!(transfer.outcomes.len(), send.len()); + assert!(transfer.outcomes.iter().all(|(_, r)| r.is_ok())); + } + } + } + for (n, r) in replicas.iter().enumerate() { + check_replica(r, &reference, n); + } + } + + // Full sync, in the order given, until nothing moves. + let mut rounds = 0; + loop { + clock += 1; + let mut quiet = true; + for &(first, second) in final_order { + let (a, b) = pair(&mut replicas, first, second); + quiet &= reconcile(a, b, &config(), clock).is_quiet(); + } + rounds += 1; + if quiet { + break; + } + assert!(rounds < 2 * REPLICAS, "full sync did not settle"); + } + + for (n, r) in replicas.iter().enumerate() { + check_replica(r, &reference, n); + for o in 0..OBJECTS { + let object_id = object(o); + let expected: BTreeSet = if scope_of(o) == PRIVATE { + reference + .all(&object_id) + .into_iter() + .filter(|id| reference.signed[id].origin == n) + .collect() + } else { + reference.all(&object_id) + }; + // Shared objects converge to the union, whatever the order + // of rounds was, with no version held and the same heads. + if scope_of(o) == SHARED { + assert_eq!(stored_by(r, &reference, &object_id), expected); + let heads: BTreeSet = r.store().heads(&object_id).into_iter().collect(); + assert_eq!(heads, reference.heads(&expected)); + } else { + assert_eq!(held_by(r, &reference, &object_id), expected); + } + } + } + // Every pair now agrees on every shared object. + for a in 0..REPLICAS { + for b in 0..REPLICAS { + if a != b { + let d = divergence(&replicas[a].inventory(), &replicas[b].inventory()); + assert!(d.iter().all(|d| d.object_id == object(OBJECTS - 1))); + } + } + } +} + +fn order_strategy() -> impl Strategy> { + let pairs: Vec<(usize, usize)> = (0..REPLICAS) + .flat_map(|a| (0..REPLICAS).filter(move |b| *b != a).map(move |b| (a, b))) + .collect(); + Just(pairs).prop_shuffle() +} + +proptest! { + #![proptest_config(ProptestConfig::with_cases(64))] + + #[test] + fn reconciliation_detects_divergence_reports_first_and_converges( + steps in prop::collection::vec(step_strategy(), 1..60), + final_order in order_strategy(), + ) { + run(&steps, &final_order); + } +} + +#[test] +fn a_concurrent_edit_is_reported_on_both_sides_and_other_objects_keep_syncing() { + let mut a = replica(0, WIDE); + let mut b = replica(1, WIDE); + let doc = 0; + let other = 1; + let base = event(1); + a.produce(&version(0, base, doc, &[], b"base", START), START) + .unwrap(); + let first = reconcile(&mut a, &mut b, &config(), START); + assert_eq!(first.forward.offer.send, vec![base]); + + // Both sides edit the same base; one side also edits another object. + a.produce(&version(0, event(2), doc, &[base], b"a", START), START) + .unwrap(); + b.produce(&version(1, event(3), doc, &[base], b"b", START), START) + .unwrap(); + a.produce(&version(0, event(4), other, &[], b"x", START), START) + .unwrap(); + + let exchange = reconcile(&mut a, &mut b, &config(), START + 1); + assert_eq!( + exchange.divergence, + vec![ + ObjectDivergence { + object_id: object(doc), + missing_local: vec![event(3)], + missing_remote: vec![event(2)], + }, + ObjectDivergence { + object_id: object(other), + missing_local: vec![], + missing_remote: vec![event(4)], + }, + ] + ); + // The conflict is a report on both sides, naming the same versions, + // and the unrelated object synced in the same exchange. + let heads = vec![event(2), event(3)]; + for side in [&a, &b] { + let report = side.store().open_report(&object(doc)).unwrap(); + let stored = side.store().report(&report).unwrap(); + assert_eq!(stored.report.conflict_key(), key_of(&heads)); + assert_eq!(side.store().heads(&object(other)), vec![event(4)]); + assert!(side.store().open_report(&object(other)).is_none()); + } + // Nothing was merged by reconciling. + assert_eq!(a.store().heads(&object(doc)).len(), 2); + + // A writer on b merges; the next exchange resolves a's report too. + b.produce(&version(1, event(5), doc, &heads, b"merged", START), START) + .unwrap(); + let report_a = a.store().open_report(&object(doc)).unwrap(); + let exchange = reconcile(&mut a, &mut b, &config(), START + 2); + assert_eq!( + exchange.backward.outcomes, + vec![(event(5), Ok(IngestOutcome::Resolved(report_a)))] + ); + assert_eq!(a.store().heads(&object(doc)), vec![event(5)]); + assert!(reconcile(&mut a, &mut b, &config(), START + 3).is_quiet()); +} + +#[test] +fn a_refused_version_is_never_offered_again_and_its_children_wait() { + let short = FreshnessWindow { + max_age: 10, + max_future_skew: 30, + }; + let mut a = replica(0, short); + let mut b = replica(1, short); + let doc = 0; + a.produce(&version(0, event(1), doc, &[], b"old", START), START) + .unwrap(); + // The child is fresh, the parent is stale by the time they meet. + let later = START + 20; + a.produce( + &version(0, event(2), doc, &[event(1)], b"new", later), + later, + ) + .unwrap(); + let exchange = reconcile(&mut a, &mut b, &config(), later); + let mut sent = vec![event(1), event(2)]; + sent.sort_unstable(); + assert_eq!(exchange.forward.offer.send, sent); + assert!( + exchange + .forward + .outcomes + .contains(&(event(1), Err(IngestError::Rejected(RejectReason::Stale)))) + ); + assert!(b.store().is_held(&event(2))); + assert_eq!( + b.refused().get(&event(1)), + Some(&Refusal::Ingest(IngestError::Rejected(RejectReason::Stale))) + ); + + // The divergence stays visible, but nothing is sent again. + let again = reconcile(&mut a, &mut b, &config(), later + 1); + assert!(again.is_quiet()); + assert_eq!(again.forward.offer.refused_by_remote, vec![event(1)]); + assert_eq!( + again.divergence, + vec![ObjectDivergence { + object_id: object(doc), + missing_local: vec![], + missing_remote: vec![event(1)], + }] + ); +} + +#[test] +fn out_of_bounds_versions_are_withheld_with_the_b08_reason() { + let mut a = replica(0, WIDE); + let mut b = replica(1, WIDE); + let private = OBJECTS - 1; + a.produce(&version(0, event(1), private, &[], b"p", START), START) + .unwrap(); + a.produce(&version(0, event(2), 0, &[], b"s", START), START) + .unwrap(); + + // Another scope never crosses; a version past retention neither. + let exchange = reconcile(&mut a, &mut b, &config().with_retention(5), START + 10); + assert_eq!(exchange.forward.offer.withheld, { + let mut w = vec![ + (event(1), Exclusion::OutsideScope), + (event(2), Exclusion::ExpiredRetention), + ]; + w.sort_unstable(); + w + }); + assert!(exchange.forward.offer.send.is_empty()); + assert!(b.inventory().objects().next().is_none()); + + // Within retention the shared version crosses; the private one never. + let exchange = reconcile(&mut a, &mut b, &config(), START + 10); + assert_eq!(exchange.forward.offer.send, vec![event(2)]); + assert_eq!( + exchange.forward.offer.withheld, + vec![(event(1), Exclusion::OutsideScope)] + ); +} + +#[test] +fn equal_sets_have_equal_digests_and_report_no_divergence() { + let mut a = replica(0, WIDE); + let mut b = replica(1, WIDE); + let envelope = version(0, event(1), 0, &[], b"v", START); + a.produce(&envelope, START).unwrap(); + b.produce(&envelope, START).unwrap(); + assert_eq!(a.inventory().summary(), b.inventory().summary()); + assert!(divergence(&a.inventory(), &b.inventory()).is_empty()); + assert!(reconcile(&mut a, &mut b, &config(), START).is_quiet()); + let ids = BTreeSet::from([event(1)]); + assert_eq!( + a.inventory().summary()[&object(0)], + digest_of(&object(0), &ids) + ); + assert_eq!(a.envelope(&event(1)), Some(&envelope)); + assert!(a.inventory().contains(&event(1))); +}