From 2068eb238a8a5d8042de4cb5ac5ab584054bfb0f Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 02:30:35 +0000 Subject: sync: keep what a message asks for, and the heads answered from, sorted Automerge encodes the hashes of a sync message sorted, and asserts so: the hashes asked for that changes waiting in screening lack were added after those Automerge asks for, unsorted, and the heads a peer last said that we hold were taken as the peer's `have` as they came. Both are sorted now. --- src/document/sync.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/document/sync.rs b/src/document/sync.rs index 4c633f5..51cf9eb 100644 --- a/src/document/sync.rs +++ b/src/document/sync.rs @@ -311,6 +311,8 @@ impl SyncSession { message.need.push(*hash); } } + // Encoded sorted, as Automerge has them. + message.need.sort_unstable(); message }); let in_step = self.state.their_heads.as_ref() == Some(&doc.get_heads()); @@ -1224,9 +1226,11 @@ fn lost_beyond_sent( if !lost { return false; } - state.shared_heads = held_said.to_vec(); + let mut held_said = held_said.to_vec(); + held_said.sort_unstable(); + state.shared_heads = held_said.clone(); state.their_have = Some(vec![sync::Have { - last_sync: held_said.to_vec(), + last_sync: held_said, bloom: sync::BloomFilter::default(), }]); true @@ -1668,7 +1672,7 @@ mod tests { } /// What changes held back in screening lack is asked for (`lacks`), as Automerge - /// asks for what the changes it holds back lack. + /// asks for what the changes it holds back lack, sorted as Automerge encodes them. #[test] fn a_message_asks_for_what_changes_held_back_lack() { use crate::document::testing::change; @@ -1676,8 +1680,12 @@ mod tests { let (mut ours, mut theirs) = (Document::new(), Document::new()); change(&mut ours, |doc| doc.put(ROOT, "early", 1).unwrap()); let (_, mut b) = in_step(&mut ours, &mut theirs); - change(&mut ours, |doc| doc.put(ROOT, "lacking", 2).unwrap()); - let lacking = ours.get_heads(); + let mut lacking = Vec::new(); + for n in 0..3 { + change(&mut ours, |doc| doc.put(ROOT, "lacking", n).unwrap()); + lacking.push(ours.get_heads()[0]); + } + lacking.sort_unstable_by(|a, b| b.cmp(a)); change(&mut ours, |doc| doc.put(ROOT, "later", 3).unwrap()); b.pull_from(ours.get_heads()); b.lacks(lacking.clone()); -- cgit v1.3.1