diff --git a/arc/sync.md b/arc/sync.md index 1d4f5c3b2ce3ede9e7c53f6959e06045e1c48799..341ae965dfaa74e0cd413473de6bdfa3cb976515 100644 --- a/arc/sync.md +++ b/arc/sync.md @@ -289,14 +289,16 @@ for each version V the remote lists (Remote::versions) revisions, publish on C's stamp; then retire V (resolved, removed) ``` -Merged page revisions and conflict page identities are named after the incoming version's -revision (a hash of it), so "holds merged" is a lookup by revision identity. A version merged -once, by this device or another, merges as nothing the next time. Two devices merging the same -version onto the same current page state name corresponding outcomes alike. This deduplicates -retries; it does not make integrations from opposite primary bases order-independent or -produce byte-identical files. Any device merges any version at once; nothing waits for the -device that wrote it. A version of another section, or one that cannot be read, is kept beside -the file as `Name (Device).one` instead. +A merged page revision is named after the incoming version's revision and the file's revision it +depends on (a one-way hash of both), and conflict page identities after the version's revision +alone, so "holds merged" is a lookup by revision identity; the names older builds gave merges +are recognised too, as devices run mixed builds. A version merged once, by this device or +another, merges as nothing the next time. Two devices merging the same version onto the same +current page state name corresponding outcomes alike; onto different states, differently, as +their contents differ. This deduplicates retries; it does not make integrations from opposite +primary bases order-independent or produce byte-identical files. Any device merges any version +at once; nothing waits for the device that wrote it. A version of another section, or one that +cannot be read, is kept beside the file as `Name (Device).one` instead. iCloud Drive can also lose an upload outright: another device's lands over it and no conflict version reaches this one. So on the Mac, the iPhone and Android each image a section publishes diff --git a/corpus/icloud-merge/older-build-merges/clash-merged.one b/corpus/icloud-merge/older-build-merges/clash-merged.one new file mode 100644 index 0000000000000000000000000000000000000000..e8745b382bd4abda4541bd6e81506011e1ae2a0c Binary files /dev/null and b/corpus/icloud-merge/older-build-merges/clash-merged.one differ diff --git a/corpus/icloud-merge/older-build-merges/clash-version.one b/corpus/icloud-merge/older-build-merges/clash-version.one new file mode 100644 index 0000000000000000000000000000000000000000..6e060a0c4855871d368eb1fc78177890e71acebf Binary files /dev/null and b/corpus/icloud-merge/older-build-merges/clash-version.one differ diff --git a/corpus/icloud-merge/older-build-merges/clean-merged.one b/corpus/icloud-merge/older-build-merges/clean-merged.one new file mode 100644 index 0000000000000000000000000000000000000000..6a0d4b4e9c05eee63b3b85470c92379abea305e0 Binary files /dev/null and b/corpus/icloud-merge/older-build-merges/clean-merged.one differ diff --git a/corpus/icloud-merge/older-build-merges/clean-version.one b/corpus/icloud-merge/older-build-merges/clean-version.one new file mode 100644 index 0000000000000000000000000000000000000000..16efd99431d7af86a04bb56ddbb64bcf76cea742 Binary files /dev/null and b/corpus/icloud-merge/older-build-merges/clean-version.one differ diff --git a/crates/notebook/README.md b/crates/notebook/README.md index f0c0ab0c7fa80cd305bc6950ce8598904b587e70..1ec9013b7161e1b699475e071ee9f4e9bd75754f 100644 --- a/crates/notebook/README.md +++ b/crates/notebook/README.md @@ -149,10 +149,12 @@ stops at a checkpoint) the newest of the file's the version holds; the section o revisions (`onestore::Section::open_at`) is the base the version's changes lower from (`lower_page`), and they replay on the file through the rebase rules above, conflict pages where they clash. A page whose two copies share no revision stays the file's, the version's kept -beside it as a conflict page (`corpus/icloud-merge/first-edit-checkpoints`). The revisions the merge writes are named after the version's it merged -(`Section::seal_as`), so a version merged once, here or on another device, merges as nothing -again. It is published on the file's stamp, then retired; one of another section, or one that -cannot be read, is retired with `keep` for the remote to keep beside the file. +beside it as a conflict page (`corpus/icloud-merge/first-edit-checkpoints`). The revisions the +merge writes are named after the version's it merged and the file's they depend on +(`Section::seal_as`), so a version merged once, here or on another device or by an older build, +merges as nothing again, and merges of one version onto different states never share a name. It +is published on the file's stamp, then retired; one of another section, or one that cannot be +read, is retired with `keep` for the remote to keep beside the file. A provider can also lose an upload outright, another device's landing over it with no version kept. A remote keeps what it publishes with `published::Published` (`keep` after each publication) and lists each image the file no longer stands as among its versions diff --git a/crates/notebook/src/resolve.rs b/crates/notebook/src/resolve.rs index 9de868cf0cb324a5de7bcbca0ad74d7e8de8f7b6..0e5677e25245d1e28fd97b5e995a451377813e63 100644 --- a/crates/notebook/src/resolve.rs +++ b/crates/notebook/src/resolve.rs @@ -4,11 +4,12 @@ //! (`merge.rs`), with conflict pages where they clash, as a queue replays on a changed //! remote. //! -//! Each revision the merge writes is named after the version's revision it merged -//! (`merged`). A space whose newest revision the file holds, or holds merged, needs nothing; -//! older ones are where the version and the file last agreed. So a version merged once, by -//! this device or another, merges as nothing the next time, and two devices merging it at -//! once write revisions of the same names, which the next merge of the two finds held. +//! Each revision the merge writes is named after the version's revision it merged and the +//! file's revision it merged onto (`merged`), and older builds' names (`named`) still count. +//! A space whose newest revision the file holds, or holds merged, needs nothing; older ones +//! are where the version and the file last agreed. So a version merged once, by this device or +//! another, merges as nothing the next time, and two devices merging it onto one state write +//! revisions of the same names, which the next merge of the two finds held. use crate::{PendingEdit, Result, merge}; use onestore::{ @@ -39,9 +40,9 @@ pub(crate) enum Merged { Foreign, } -/// The name of the revision a merge writes in a space for the version's revision `rid`, a -/// different one for each `kind` of space it writes. -fn merged(rid: ExGuid, kind: &str) -> ExGuid { +/// What older builds named a merge of the version's revision `rid`: the revisions it wrote +/// (`"page"`), and the conflict pages it made (`"conflict"`), which keep the name. +fn named(rid: ExGuid, kind: &str) -> ExGuid { let digest = Sha256::new() .chain_update(b"snowbound merge ") .chain_update(kind) @@ -53,6 +54,25 @@ fn merged(rid: ExGuid, kind: &str) -> ExGuid { ExGuid { guid, n: 1 } } +/// The name of the revision a merge writes for the version's revision `rid` onto the file's +/// revision `onto` of that space, which it depends on: two merges of one version onto +/// different states are different revisions. Onto no revision, as in a space the merge +/// creates, it is the name older builds gave. +fn merged(rid: ExGuid, onto: Option) -> ExGuid { + let mut digest = Sha256::new() + .chain_update(b"snowbound merge page") + .chain_update(rid.guid) + .chain_update(rid.n.to_le_bytes()); + if let Some(onto) = onto { + digest = digest + .chain_update(onto.guid) + .chain_update(onto.n.to_le_bytes()); + } + let mut guid = [0; 16]; + guid.copy_from_slice(&digest.finalize()[..16]); + ExGuid { guid, n: 1 } +} + /// Merges `version` into `current`, the file as it stands, `device` naming who saved the /// version. pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result { @@ -79,18 +99,16 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result Result = changed .iter() - .filter_map(|space| Some((*space, merged(head(space)?, "page")))) + .filter_map(|space| Some((*space, merged(head(space)?, onto(space))))) .collect(); for (space, objects) in &clashes { let Ok(page) = theirs.page(*space) else { @@ -264,7 +286,7 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result Result Merged::Publish(Box::new(transaction)), diff --git a/crates/notebook/tests/cloud.rs b/crates/notebook/tests/cloud.rs index 44644a521cc46b685a3132bd459ea4363163e8cd..f27cdf31113c5c46202584a24426b7e92936f8f1 100644 --- a/crates/notebook/tests/cloud.rs +++ b/crates/notebook/tests/cloud.rs @@ -1206,3 +1206,102 @@ fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() { ); } } + +/// Two devices merge one version onto different states of a page, then their results meet: +/// each merge is its own revision, so neither device's typing is taken as held by the other +/// (sweep seed 600). The version's own typing, merged on both sides, may show twice: the limit +/// on integrations from opposite bases in `arc/sync.md`. +#[test] +fn merges_of_one_version_onto_different_states_keep_both() { + let source = fixture(); + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, source.clone()).unwrap(); + let space = section.pages().unwrap()[0].0; + let (text, body) = bodies(§ion.page(space).unwrap())[0].clone(); + let end = body.encode_utf16().count() as u32; + let typed = |at: u32, with: &str| { + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, source.clone()).unwrap(); + let op = PageOp::Text { + text, + range: at..at, + with: with.into(), + }; + edit(&mut section, "Device", vec![Op::Page { space, op }]); + section.seal().unwrap(); + section.image() + }; + let (a, b, c) = (typed(0, "⟨A⟩"), typed(end, "⟨B⟩"), typed(4, "⟨C⟩")); + let onto_a = merged(&[a, c.clone()], 0); + let onto_b = merged(&[b, c], 0); + for winner in 0..2 { + let image = merged(&[onto_a.clone(), onto_b.clone()], winner); + let (listed, conflicts) = texts_of(&image); + for (token, shown) in [("⟨A⟩", 1..=1), ("⟨B⟩", 1..=1), ("⟨C⟩", 1..=2)] { + let count = listed.matches(token).count(); + assert!(shown.contains(&count), "{winner} {token}:\n{listed}"); + } + assert!(conflicts.is_empty(), "{winner}: {conflicts}"); + } +} + +/// A version an older build merged, under the names it gave merges, merges as nothing again: +/// neither its clean merge nor its conflict page comes back +/// (`corpus/icloud-merge/older-build-merges`). +#[test] +fn a_version_an_older_build_merged_merges_as_nothing() { + let row = format!( + "{}/../../corpus/icloud-merge/older-build-merges", + env!("CARGO_MANIFEST_DIR") + ); + for name in ["clean", "clash"] { + let read = |kind: &str| std::fs::read(format!("{row}/{name}-{kind}.one")).unwrap(); + let file = read("merged"); + assert!( + merged(&[file.clone(), read("version")], 0) == file, + "{name}" + ); + } +} + +/// A merge of a merge onto the same state is a revision of its own: Z's version merged into X's +/// file, that file merged into W's on X's state, and W's file merged back into Z's copy keeps +/// X's typing. Z's own, merged on both sides, may show twice, as in the sweep's duplicates. +#[test] +fn a_merge_merged_again_onto_the_same_state_is_its_own_revision() { + let source = fixture(); + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, source.clone()).unwrap(); + let pages = section.pages().unwrap(); + let space = pages[0].0; + let (text, words) = bodies(§ion.page(space).unwrap())[0].clone(); + let end = words.encode_utf16().count() as u32; + let typed = |image: Vec, space, text, at: u32, with: &str| { + let op = PageOp::Text { + text, + range: at..at, + with: with.into(), + }; + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, image).unwrap(); + edit(&mut section, "Device", vec![Op::Page { space, op }]); + section.seal().unwrap(); + section.image() + }; + let x = typed(source.clone(), space, text, end, "⟨X⟩"); + let z = typed(source.clone(), space, text, 0, "⟨Z⟩"); + let onto_x = merged(&[x.clone(), z.clone()], 0); + let other = pages[1].0; + let (text, _) = bodies(§ion.page(other).unwrap())[0].clone(); + let w = typed(x, other, text, 0, "⟨W⟩"); + let onto_w = merged(&[w, onto_x], 0); + let image = merged(&[z, onto_w], 0); + let (listed, conflicts) = texts_of(&image); + for (token, shown) in [("⟨X⟩", 1..=1), ("⟨W⟩", 1..=1), ("⟨Z⟩", 1..=2)] { + let count = listed.matches(token).count(); + assert!( + shown.contains(&count), + "{token}:\n{listed}\n--\n{conflicts}" + ); + } +} diff --git a/crates/onestore/src/section.rs b/crates/onestore/src/section.rs index c3168ddc4d7dd3f5818c7db06652ead131360a32..c90684622767dcfec1b0e5bdd9c55ff21d023d10 100644 --- a/crates/onestore/src/section.rs +++ b/crates/onestore/src/section.rs @@ -820,17 +820,20 @@ impl<'a> Section<'a> { /// merged, so that the next merge knows it holds that. pub fn seal_as(&mut self, names: &BTreeMap) -> Result> { self.usable()?; - if names.iter().any(|(space, name)| { - name.guid == [0; 16] - || self - .spaces - .get(space) - .is_some_and(|stored| stored.rid == Some(*name)) - }) { - return Err(Error { - offset: 0, - message: "Name each revision anew", - }); + if !names.is_empty() { + let image = self.image(); + let store = Store::parse(&image)?; + let index = RevisionIndex::parse(&store)?; + if names.iter().any(|(space, name)| { + name.guid == [0; 16] + || (index.spaces.get(space)) + .is_some_and(|stored| stored.revisions.contains_key(name)) + }) { + return Err(Error { + offset: 0, + message: "Name each revision anew", + }); + } } self.broken = true; let arena = &self.arena.0;