aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-17 02:23:17 +0000
committertv <tv@krebsco.de>2026-11-17 03:25:16 +0100
commit696a55d42b5d6a7b1fb53dfe6c888b41bc5cd421 (patch)
tree7c383fc44f21038da7074aa9384b7aa4a1bc59a9
parent70b969c3c4932e882e3ed3e7249c18722e6bb16f (diff)
sync: ask for what changes waiting in screening lack
Screening keeps a change whose dependencies haven't come from Automerge, waiting, so Automerge never learns what it lacks: our next message asks for the peer's heads alone, and the peer, forgetting having sent those, sends them again, and what they build on only as far as it still counts it as sent. So the session asks for what changes waiting build on and the document lacks too (`Waiting::lacking`, `SyncSession::lacks`), as Automerge asks for what the changes it holds back lack.
-rw-r--r--src/access/screen.rs16
-rw-r--r--src/document/sync.rs44
-rw-r--r--src/swarm/sync.rs1
3 files changed, 60 insertions, 1 deletions
diff --git a/src/access/screen.rs b/src/access/screen.rs
index b6fb8fd..e37f2b9 100644
--- a/src/access/screen.rs
+++ b/src/access/screen.rs
@@ -283,6 +283,21 @@ impl Waiting {
self.0.len()
}
+ /// What the changes waiting build on that `doc` lacks, but for those waiting
+ /// themselves: what to ask the peers for (`SyncSession::lacks`).
+ pub(crate) fn lacking(&self, doc: &Document) -> Vec<ChangeHash> {
+ let waiting: HashSet<ChangeHash> = self.0.iter().map(Change::hash).collect();
+ let mut lacking: Vec<ChangeHash> = self
+ .0
+ .iter()
+ .flat_map(|change| change.deps().iter().copied())
+ .filter(|dep| !waiting.contains(dep) && !crate::document::changes::holds(doc, dep))
+ .collect();
+ lacking.sort_unstable();
+ lacking.dedup();
+ lacking
+ }
+
/// The changes waiting, oldest first.
#[cfg(test)]
pub(crate) fn changes(&self) -> &[Change] {
@@ -1460,6 +1475,7 @@ mod tests {
let screened = screen(&mut ours, last, &mut waiting);
assert_eq!(screened.applied, 0);
assert_eq!(waiting.len(), 1);
+ assert_eq!(waiting.lacking(&ours), middle, "what it lacks, to ask for");
// The rest: all in, the waiting one too.
let screened = screen(&mut ours, received(&theirs, &[]), &mut waiting);
assert_eq!(screened.applied, 4);
diff --git a/src/document/sync.rs b/src/document/sync.rs
index e9afe06..db4eed7 100644
--- a/src/document/sync.rs
+++ b/src/document/sync.rs
@@ -208,6 +208,9 @@ pub(crate) struct SyncSession {
/// The heads the peer said last, in the newest of its messages so far (one that
/// comes late doesn't count: `older_keeps_newer`).
said_heads: Option<Vec<crate::document::ChangeHash>>,
+ /// What changes waiting in screening build on and we lack (`access::screen`): asked
+ /// for in our next message (`lacks`).
+ lacking: Vec<crate::document::ChangeHash>,
/// The wait before answering a peer that offers again only changes we refused
/// (`refused_again`).
@@ -266,6 +269,7 @@ impl SyncSession {
known_elsewhere: std::collections::HashMap::new(),
assumed_lately: std::collections::VecDeque::new(),
said_heads: None,
+ lacking: Vec::new(),
refused_backoff: Backoff::new(REFUSED_WAIT, REFUSED_WAIT_MAX),
refusals_told: None,
reply: None,
@@ -295,7 +299,16 @@ impl SyncSession {
self.state.last_sent_heads.clone(),
self.state.have_responded,
);
- let message = doc.generate_sync_message(&mut self.state);
+ let message = doc
+ .generate_sync_message(&mut self.state)
+ .map(|mut message| {
+ for hash in &self.lacking {
+ if !message.need.contains(hash) && !crate::document::changes::holds(doc, hash) {
+ message.need.push(*hash);
+ }
+ }
+ message
+ });
let in_step = self.state.their_heads.as_ref() == Some(&doc.get_heads());
let sent: Vec<_> = self
.state
@@ -904,6 +917,14 @@ impl SyncSession {
before - self.state.sent_hashes.len()
}
+ /// `lacking`, what changes waiting in screening build on and we lack
+ /// (`Waiting::lacking`): our next message asks for them (`need`), as Automerge asks
+ /// for what the changes it holds back lack. Screening keeps them from Automerge,
+ /// which then asks for the peer's heads alone.
+ pub(crate) fn lacks(&mut self, lacking: Vec<crate::document::ChangeHash>) {
+ self.lacking = lacking;
+ }
+
/// The peer has heads we lack (its keepalive said so): ask for what they need,
/// even though Automerge thinks a message is still in flight.
pub(crate) fn pull_from(&mut self, heads: Vec<crate::document::ChangeHash>) {
@@ -1554,6 +1575,27 @@ mod tests {
assert_eq!(decode(&ask).unwrap().0.need, vec![lost]);
}
+ /// What changes held back in screening lack is asked for (`lacks`), as Automerge
+ /// asks for what the changes it holds back lack.
+ #[test]
+ fn a_message_asks_for_what_changes_held_back_lack() {
+ use crate::document::testing::change;
+ use automerge::{ROOT, transaction::Transactable};
+ 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();
+ change(&mut ours, |doc| doc.put(ROOT, "later", 3).unwrap());
+ b.pull_from(ours.get_heads());
+ b.lacks(lacking.clone());
+ let ask = b
+ .generate(&theirs, Instant::now(), SyncBytes::default())
+ .expect("an ask");
+ let need = decode(&ask).unwrap().0.need;
+ assert!(lacking.iter().all(|hash| need.contains(hash)));
+ }
+
/// Automerge, as we rely on it: a message taken back for a delta leaves Automerge as
/// if it hadn't been generated (`withdraw_for_delta` restores what it had sent): the
/// peer opened, our answer was the delta's message, so our heads never went. Once
diff --git a/src/swarm/sync.rs b/src/swarm/sync.rs
index 528fe47..5f30440 100644
--- a/src/swarm/sync.rs
+++ b/src/swarm/sync.rs
@@ -706,6 +706,7 @@ pub(crate) async fn handle_sync(
if let Some(actor) = outcome.fresh_actor {
remake.add_own_actor(actor);
}
+ sync.lacks(screening.lock_or_recover().waiting.lacking(doc));
}
sync.receive_logged(doc, message, log)
})();