diff --git a/arc/sync.md b/arc/sync.md index 341ae965dfaa74e0cd413473de6bdfa3cb976515..d25b05d5d9b086f18f8f47faabe7329fc2ef1856 100644 --- a/arc/sync.md +++ b/arc/sync.md @@ -280,7 +280,8 @@ fact, and each sync step merges the versions it finds before anything else: for each version V the remote lists (Remote::versions) C := the file, read coordinated per space of V: a := its newest revision C holds, or holds merged, - else C's newest that V holds (V's chain stopped at a checkpoint), else none + else C's newest that V holds or that merged one of V's (V's chain stopped + at a checkpoint), else none ancestor := V opened at those revisions (Section::open_at) ops := lower_page(ancestor[S], V[S]) per page V changed, V's new pages imported under their identities, its deletions, moves and conflict pages; a page with no a stays C's, diff --git a/crates/notebook/README.md b/crates/notebook/README.md index 1ec9013b7161e1b699475e071ee9f4e9bd75754f..76c5e5926b7975b303403317fdc1eb182e140a2f 100644 --- a/crates/notebook/README.md +++ b/crates/notebook/README.md @@ -145,16 +145,17 @@ A remote a file provider keeps may list versions of the file beside it (`Remote: `version`, `retire`), as iCloud Drive keeps a commit that lost to another device's. Each step merges them first (`resolve.rs`): per object space, the newest revision of the version's that the file holds, or holds merged, is where the two last agreed, or failing that (a chain that -stops at a checkpoint) the newest of the file's the version holds; the section opened at those -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 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. +stops at a checkpoint) the newest of the file's that the version holds or that merged one of the +version's; the section opened at those 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`), and a page list with none counts as unchanged. +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 0e5677e25245d1e28fd97b5e995a451377813e63..758f2d4079666195a95d758c2d74040c3c72fca6 100644 --- a/crates/notebook/src/resolve.rs +++ b/crates/notebook/src/resolve.rs @@ -112,11 +112,18 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result Result = heads .keys() .filter(|space| !shared.contains_key(space) && file.spaces.contains_key(space)) .copied() .collect(); - for space in &unshared { - shared.insert(*space, heads[space]); + // Pages with a state both share, under which the base's conflict pages are both's. + let common: BTreeSet = shared.keys().copied().collect(); + let listless = unshared.contains(&file.root); + for (space, head) in &heads { + if listless || unshared.contains(space) { + shared.entry(*space).or_insert(*head); + } } let arenas = (Arena::default(), Arena::default(), Arena::default()); let mut ancestor = @@ -189,7 +202,9 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result = before .iter() .filter(|space| !kept.contains(space)) diff --git a/crates/notebook/tests/cloud.rs b/crates/notebook/tests/cloud.rs index f27cdf31113c5c46202584a24426b7e92936f8f1..3c658b692bcdc8b39086b6e7d33392718bae06f9 100644 --- a/crates/notebook/tests/cloud.rs +++ b/crates/notebook/tests/cloud.rs @@ -962,6 +962,68 @@ fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op { } } +/// The `n`th of a run of edits that type "x" at the start of a page's title and take it out. +fn toggling(page: &(ExGuid, Page), n: usize) -> Vec { + let mut op = typing_title(page, "x"); + if let Op::Page { + op: PageOp::Text { range, with, .. }, + .. + } = &mut op + && n % 2 == 1 + { + *range = 0..1; + with.clear(); + } + vec![op] +} + +/// The ops adding a body of `text` to a page. +fn body((space, page): &(ExGuid, Page), text: &str) -> Vec { + lower_page(page, &with_body(page, text)) + .unwrap() + .into_iter() + .map(|op| Op::Page { space: *space, op }) + .collect() +} + +/// `image` with each of `edits` sealed as a revision of its own. +fn sealed(image: Vec, author: &str, edits: impl IntoIterator>) -> Vec { + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, image).unwrap(); + for ops in edits { + edit(&mut section, author, ops); + section.seal().unwrap(); + } + section.image() +} + +/// Revisions in the dependency chain of `space`'s newest revision in `image`. +fn depth(image: &[u8], space: ExGuid) -> usize { + let store = onestore::Store::parse(image).unwrap(); + let index = onestore::RevisionIndex::parse(&store).unwrap(); + let revisions = &index.spaces[&space].revisions; + std::iter::successors(index.active(space).ok(), |rid| revisions[rid].dependency).count() +} + +/// `pages` as `image` holds them. +fn reread(image: &[u8], pages: &[(ExGuid, Page)]) -> Vec<(ExGuid, Page)> { + let arena = onestore::Arena::default(); + let section = Section::open(&arena, image.to_vec()).unwrap(); + pages + .iter() + .map(|(space, _)| (*space, section.page(*space).unwrap())) + .collect() +} + +/// `untouched(count)` with its first page's chain `deep` revisions long. +fn deep(count: usize, deep: usize) -> (Vec, Vec<(ExGuid, Page)>) { + let (source, pages) = untouched(count); + let runs = deep - depth(&source, pages[0].0); + let source = sealed(source, "Fixture", (0..runs).map(|n| toggling(&pages[0], n))); + let pages = reread(&source, &pages); + (source, pages) +} + /// Issue #152: the first edits two devices make to a page merge as edits, not as a version /// set aside. #[test] @@ -1036,16 +1098,8 @@ fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { /// What the Mac and the iPhone do to four pages: the Mac types in the first page's title; the /// iPhone writes on the first and second pages, moves the fourth first and deletes the third. fn apart(pages: &[(ExGuid, Page)]) -> [Vec; 2] { - let body = |(space, page): &(ExGuid, Page), text| { - let space = *space; - lower_page(page, &with_body(page, text)) - .unwrap() - .into_iter() - .map(move |op| Op::Page { space, op }) - }; - let mut iphone: Vec = body(&pages[0], "Typed on the iPhone.") - .chain(body(&pages[1], "Page 1 from the iPhone.")) - .collect(); + let mut iphone = body(&pages[0], "Typed on the iPhone."); + iphone.extend(body(&pages[1], "Page 1 from the iPhone.")); iphone.push(Op::Section(SectionOp::Pages(vec![ PageEdit::move_to(pages[3].0, Some(pages[0].0), 1).unwrap(), ]))); @@ -1056,7 +1110,7 @@ fn apart(pages: &[(ExGuid, Page)]) -> [Vec; 2] { /// `apart`'s copies merged either way round, where the first page's two copies share no /// revision: that page stays the file's with the other kept beside it as a conflict page, and /// every other change merges. -fn assert_merged_beside(copies: &[Vec; 2], pages: &[(ExGuid, Page)]) { +fn assert_merged_beside(copies: &[Vec; 2], pages: &[(ExGuid, Page)], first: &str) { let space = pages[0].0; for copy in copies { let store = onestore::Store::parse(copy).unwrap(); @@ -1083,8 +1137,8 @@ fn assert_merged_beside(copies: &[Vec; 2], pages: &[(ExGuid, Page)]) { .into_iter() .map(|(_, title, _)| title) .collect(); - let first = ["Mac: Page 0", "Page 0"][winner]; - assert_eq!(titles, ["Page 3", first, "Page 1"], "{winner}"); + let first = [format!("Mac: {first}"), first.to_owned()][winner].clone(); + assert_eq!(titles, ["Page 3", &first, "Page 1"], "{winner}"); assert_eq!( bodies(§ion.page(pages[1].0).unwrap())[0].1, "Page 1 from the iPhone." @@ -1105,38 +1159,9 @@ fn assert_merged_beside(copies: &[Vec; 2], pages: &[(ExGuid, Page)]) { /// conflict page; the rest of the version still merges. #[test] fn pages_checkpointed_on_both_devices_merge_as_conflict_pages() { - let (source, pages) = untouched(4); - let arena = onestore::Arena::default(); - let mut section = Section::open(&arena, source).unwrap(); - let (space, page) = &pages[0]; - let body = lower_page(page, &with_body(page, "Body")) - .unwrap() - .into_iter() - .map(|op| Op::Page { space: *space, op }) - .collect(); - edit(&mut section, "Fixture", body); - section.seal().unwrap(); - let (text, _) = bodies(§ion.page(*space).unwrap())[0].clone(); - for n in 0..510 { - let (range, with) = if n % 2 == 0 { (0..0, "x") } else { (0..1, "") }; - let op = PageOp::Text { - text, - range, - with: with.into(), - }; - edit( - &mut section, - "Fixture", - vec![Op::Page { space: *space, op }], - ); - section.seal().unwrap(); - } - let source = section.image(); - let pages: Vec<(ExGuid, Page)> = pages - .iter() - .map(|(space, _)| (*space, section.page(*space).unwrap())) - .collect(); - assert_merged_beside(&diverged(&source, &apart(&pages)), &pages); + let (source, pages) = deep(4, 512); + let first = title(&pages[0].1).text.text().to_owned(); + assert_merged_beside(&diverged(&source, &apart(&pages)), &pages, &first); } /// The same for sections written before #152, whose first edit to a page checkpointed: @@ -1156,7 +1181,7 @@ fn pages_first_edited_before_152_merge_as_conflict_pages() { .into_iter() .map(|(space, ..)| (space, section.page(space).unwrap())) .collect(); - assert_merged_beside(&copies, &pages); + assert_merged_beside(&copies, &pages, "Page 0"); } /// A page one device takes past the chain cap merges as edits with the other device's edits @@ -1164,38 +1189,17 @@ fn pages_first_edited_before_152_merge_as_conflict_pages() { #[test] fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() { let (source, pages) = untouched(2); - let arena = onestore::Arena::default(); - let mut section = Section::open(&arena, source.clone()).unwrap(); - for n in 0..521 { - let with = if n == 520 { "Mac: " } else { "x" }; - let mut op = typing_title(&pages[0], with); - if let Op::Page { - op: PageOp::Text { range, with, .. }, - .. - } = &mut op - && n % 2 == 1 - { - *range = 0..1; - with.clear(); - } - edit(&mut section, "Mac", vec![op]); - section.seal().unwrap(); - } - let mac = section.image(); - let store = onestore::Store::parse(&mac).unwrap(); - let index = onestore::RevisionIndex::parse(&store).unwrap(); - let space = &index.spaces[&pages[0].0]; - let chain = std::iter::successors(index.active(pages[0].0).ok(), |rid| { - space.revisions[rid].dependency - }); - assert!(chain.count() < 512); - let (space, page) = &pages[1]; - let body = lower_page(page, &with_body(page, "Page 1 from the iPhone.")) - .unwrap() - .into_iter() - .map(|op| Op::Page { space: *space, op }) - .collect(); - let [_, iphone] = diverged(&source, &[Vec::new(), body]); + let runs = (0..520).map(|n| toggling(&pages[0], n)); + let mac = sealed( + source.clone(), + "Mac", + runs.chain([vec![typing_title(&pages[0], "Mac: ")]]), + ); + assert!(depth(&mac, pages[0].0) < 512); + let [_, iphone] = diverged( + &source, + &[Vec::new(), body(&pages[1], "Page 1 from the iPhone.")], + ); for winner in 0..2 { let image = merged(&[mac.clone(), iphone.clone()], winner); let (listed, conflicts) = texts_of(&image); @@ -1207,6 +1211,165 @@ fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() { } } +/// A page one device takes past the chain cap merges with the other device's edit to it. +#[test] +fn a_page_past_the_cap_on_one_device_merges_with_the_others_edit_to_it() { + let (source, pages) = deep(2, 500); + let first = title(&pages[0].1).text.text().to_owned(); + let runs = (0..20).map(|n| toggling(&pages[0], n)); + let mac = sealed( + source.clone(), + "Mac", + runs.chain([vec![typing_title(&pages[0], "Mac: ")]]), + ); + assert!(depth(&mac, pages[0].0) < 20); + let [_, iphone] = diverged( + &source, + &[Vec::new(), body(&pages[0], "Typed on the iPhone.")], + ); + for winner in 0..2 { + let image = merged(&[mac.clone(), iphone.clone()], winner); + let (listed, conflicts) = texts_of(&image); + assert!(conflicts.is_empty(), "{winner}: {conflicts}"); + assert_eq!( + listed, + format!("Mac: {first}\nTyped on the iPhone.\nPage 1\n"), + "{winner}" + ); + } +} + +/// A version whose page the file holds merged, then taken past the chain cap, merges from +/// that merge: what the file already merged is not replayed. +#[test] +fn a_page_merged_then_past_the_cap_merges_as_edits() { + let (source, pages) = deep(2, 490); + let first = title(&pages[0].1).text.text().to_owned(); + let iphone = sealed( + source.clone(), + "iPhone", + [body(&pages[0], "A from the iPhone.")], + ); + let mac = sealed(source, "Mac", [vec![typing_title(&pages[1], "Mac: ")]]); + let mac = merged(&[mac, iphone.clone()], 0); + let page = reread(&iphone, &pages[..1]).remove(0); + let runs = (0..30).map(|n| toggling(&page, n)); + let iphone = sealed( + iphone, + "iPhone", + runs.chain([vec![typing_title(&page, "iPhone: ")]]), + ); + assert!(depth(&iphone, page.0) < 30); + for winner in 0..2 { + let image = merged(&[mac.clone(), iphone.clone()], winner); + let (listed, conflicts) = texts_of(&image); + assert!(conflicts.is_empty(), "{winner}: {conflicts}"); + assert_eq!( + listed, + format!("iPhone: {first}\nA from the iPhone.\nMac: Page 1\n"), + "{winner}" + ); + } +} + +/// A page the file deleted, which the version edited with no revision in common, comes back +/// as a copy, as a page deleted on one side and edited on the other does. +#[test] +fn a_page_the_file_deleted_with_no_common_revision_comes_back() { + let (source, pages) = deep(2, 512); + let space = pages[0].0; + let deleting = Op::Section(SectionOp::Delete(vec![space])); + let mac = sealed( + source.clone(), + "Mac", + [vec![typing_title(&pages[0], "Mac: ")], vec![deleting]], + ); + let iphone = sealed(source, "iPhone", [body(&pages[0], "Typed on the iPhone.")]); + for copy in [&mac, &iphone] { + assert!(depth(copy, space) < 3); + } + let (listed, conflicts) = texts_of(&merged(&[mac, iphone], 0)); + assert_eq!( + listed.matches("Typed on the iPhone.").count(), + 1, + "{listed}" + ); + assert!(!listed.contains("Mac: "), "{listed}"); + assert!(conflicts.is_empty(), "{conflicts}"); +} + +/// A page list with no revision in common counts as the file's: the version's new page, with +/// its conflict page, and page edits merge, its deletion does not. +#[test] +fn a_page_list_with_no_common_revision_counts_as_unchanged() { + let (source, pages) = untouched(3); + let root = { + let store = onestore::Store::parse(&source).unwrap(); + onestore::RevisionIndex::parse(&store).unwrap().root + }; + let moving = |n: usize| { + let (space, before) = if n.is_multiple_of(2) { (1, 0) } else { (0, 1) }; + let edit = PageEdit::move_to(pages[space].0, Some(pages[before].0), 1).unwrap(); + vec![Op::Section(SectionOp::Pages(vec![edit]))] + }; + let runs = 512 - depth(&source, root); + let source = sealed(source, "Fixture", (0..runs).map(moving)); + let pages = reread(&source, &pages); + let first = PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(); + let mac = sealed( + source.clone(), + "Mac", + [ + vec![Op::Section(SectionOp::Pages(vec![first]))], + vec![typing_title(&pages[0], "Mac: ")], + ], + ); + let creation = PageCreation::new(None, Some("From the iPhone"), "iPhone").unwrap(); + let made = creation.space(); + let iphone = sealed( + source, + "iPhone", + [ + vec![Op::Section(SectionOp::Delete(vec![pages[1].0]))], + vec![Op::Section(SectionOp::Create(creation))], + body(&pages[2], "Page 2 from the iPhone."), + ], + ); + let page = reread(&iphone, &[(made, pages[0].1.clone())]).remove(0).1; + let conflict = Op::Section(SectionOp::Conflict { + of: made, + creation: PageCreation::new(None, Some("From the iPhone"), "Someone") + .unwrap() + .in_space(new_id().unwrap().guid) + .unwrap(), + page: with_body(&page, "Kept beside it."), + objects: Vec::new(), + }); + let iphone = sealed(iphone, "iPhone", [vec![conflict]]); + for copy in [&mac, &iphone] { + assert!(depth(copy, root) < 4); + } + let image = merged(&[mac, iphone], 0); + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, image.clone()).unwrap(); + let conflicts = section.conflicts().unwrap(); + let [(of, beside)] = &conflicts[..] else { + panic!("{conflicts:?}") + }; + assert_eq!((*of, beside.len()), (made, 1)); + let beside = bodies(§ion.page(beside[0].space).unwrap()); + assert!(beside.iter().any(|(_, text)| text == "Kept beside it.")); + let (listed, _) = texts_of(&image); + for text in [ + "Mac: Page 0", + "Page 1", + "Page 2 from the iPhone.", + "From the iPhone", + ] { + assert_eq!(listed.matches(text).count(), 1, "{text}:\n{listed}"); + } +} + /// 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