From 126c7a891494ab1998b1e6de368edf62a80cc7ab Mon Sep 17 00:00:00 2001 From: clover caruso Date: Fri, 16 Oct 2026 14:16:01 -0700 Subject: [PATCH] fix(notebook): keep a page with no shared revision as a conflict page When a page's two copies shared no revision, merging a conflict version failed and the whole version was set aside beside the section. The version's chain ends at a checkpoint without reaching the file whenever its device checkpointed the page: a first edit in a section written before #152, or a page taken past the 512-revision cap. The merge now looks the other way when the version's chain falls short: the newest of the file's revisions that the version holds is the base, so a page only one device took past a checkpoint still merges as edits. Where neither copy holds a revision of the other's, as when both checkpointed the page, the file's page stays and the version's is kept beside it as a conflict page, while the version's other pages, moves and deletions still merge. corpus/icloud-merge/first-edit-checkpoints holds two device copies written by the writer before #152. OneNote 2010 cold-opens these merges, either device winning, with each conflict page under its page. Refs #152 Assisted-by: claude-opus-5.5 --- arc/sync.md | 6 +- corpus/icloud-merge/README.md | 8 + .../first-edit-checkpoints/Mac.one | Bin 0 -> 13512 bytes .../first-edit-checkpoints/iPhone.one | Bin 0 -> 17872 bytes crates/notebook/README.md | 6 +- crates/notebook/src/resolve.rs | 54 ++-- crates/notebook/tests/cloud.rs | 263 ++++++++++++++---- 7 files changed, 261 insertions(+), 76 deletions(-) create mode 100644 corpus/icloud-merge/first-edit-checkpoints/Mac.one create mode 100644 corpus/icloud-merge/first-edit-checkpoints/iPhone.one diff --git a/arc/sync.md b/arc/sync.md index 9cbb7bbb202426d19866de67cb337f4e0f04bd57..1d4f5c3b2ce3ede9e7c53f6959e06045e1c48799 100644 --- a/arc/sync.md +++ b/arc/sync.md @@ -279,10 +279,12 @@ fact, and each sync step merges the versions it finds before anything else: ```text 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 + 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 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 + identities, its deletions, moves and conflict pages; a page with no a stays C's, + V's beside it as a conflict page replay ops on C through merge.rs (conflict pages where they clash), seal_as the named revisions, publish on C's stamp; then retire V (resolved, removed) ``` diff --git a/corpus/icloud-merge/README.md b/corpus/icloud-merge/README.md index c64d4f35c88d514e47ba0c042a6d5a65ac3f257a..58e6da2af982c762898362786d3a5941053c5abc 100644 --- a/corpus/icloud-merge/README.md +++ b/corpus/icloud-merge/README.md @@ -11,3 +11,11 @@ appended text and the new page replay, and the clashing edit becomes a conflict `--screenshots`): both pages list, the first with its conflict mark and the yellow bar "This page has changes that could not be merged during synchronization", its positioned outline ending "(typed on the iPhone)", and "From the iPhone" holding "Written offline on the iPhone." + +`first-edit-checkpoints` holds the two copies of `apart` in `crates/notebook/tests/cloud.rs` +(a fresh Snowbound section of four pages; the Mac types in the first page's title, the iPhone +writes on the first two pages, moves the fourth first and deletes the third) as the writer +before #152 stored them: each page's first edit a checkpoint with no dependency, so the first +page's two copies share no revision. Merged either way round, that page stays the file's with +the other copy as its conflict page, and the rest merges +(`pages_first_edited_before_152_merge_as_conflict_pages`). diff --git a/corpus/icloud-merge/first-edit-checkpoints/Mac.one b/corpus/icloud-merge/first-edit-checkpoints/Mac.one new file mode 100644 index 0000000000000000000000000000000000000000..d0318bf8facfd99f58614e7f39826ecb254be098 GIT binary patch literal 13512 zcmeHN3s_Xg6&@Z1R1}ODQ5INUipH8%P>E4og(5}~zL@xtR4ht-RaEMuf;R1fkDye= zH~0#Pi54{!DJC1+Dv7DF(x#Z$LK|(c8WlAWV~l8G?D_AV!Mk?{7W-*G({jFV_TD>Z z=FFKhXZ|@evuB?kzv!1WyAn$Fjm@m;x$&cr$3FXX_S7w-xBd85-+pdsSxiSmPi^-0 zTXTKn>s4LWe0H>?rWf(GNff_Bgm)K_!Ji^um)-c;Fzoc;wMl}j(eQCA8|E~os z&dg+BhRFuz>rGZD8hB^#Nx!dR*B16a@wn%4KQ^~Ax;*8SQ=@?_E-c5N`Q$}1pMwVG z6HW{yb!NZ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.md b/crates/notebook/README.md index ea5e1dbb3a7b01ad1486f6eb88f7fea17a53247f..f0c0ab0c7fa80cd305bc6950ce8598904b587e70 100644 --- a/crates/notebook/README.md +++ b/crates/notebook/README.md @@ -144,10 +144,12 @@ of the local one, placed the same way (`corpus/conflict-page/native-pages`, A remote a file provider keeps may list versions of the file beside it (`Remote::versions`, `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; the section opened at those +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. The revisions the merge writes are named after the version's it merged +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. diff --git a/crates/notebook/src/resolve.rs b/crates/notebook/src/resolve.rs index da419529de2eb428aaf7001a47d0d3a4a5f8478b..9de868cf0cb324a5de7bcbca0ad74d7e8de8f7b6 100644 --- a/crates/notebook/src/resolve.rs +++ b/crates/notebook/src/resolve.rs @@ -12,8 +12,8 @@ use crate::{PendingEdit, Result, merge}; use onestore::{ - Arena, ConflictPage, ExGuid, PageCreation, PageEdit, RevisionIndex, Section, Store, - Transaction, + Arena, ConflictPage, ExGuid, ObjectSpace, PageCreation, PageEdit, RevisionIndex, Section, + Store, Transaction, op::{Edit, Op, SectionOp, lower_page}, }; use sha2::{Digest, Sha256}; @@ -93,32 +93,33 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result = heads .iter() .filter(|(space, head)| shared.get(space) != Some(head)) .map(|(space, _)| *space) .collect(); + // Spaces both hold with no state in common read as the version leaves them: its page list + // as unchanged, a page as one kept beside the file's when they differ. + let unshared: BTreeSet = heads + .keys() + .filter(|space| !shared.contains_key(space) && file.spaces.contains_key(space)) + .copied() + .collect(); + for space in &unshared { + shared.insert(*space, heads[space]); + } let arenas = (Arena::default(), Arena::default(), Arena::default()); let mut ancestor = Section::open_at(&arenas.0, vec![version.to_vec(), current.to_vec()], &shared)?; @@ -188,10 +189,10 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result lower_page(&ancestor.page(*space)?, &page).ok(), - None if ours.page(*space).ok().as_ref() == Some(&page) => Some(Vec::new()), - None => None, + let lowered = if unshared.contains(space) { + (ours.page(*space).ok().as_ref() == Some(&page)).then(Vec::new) + } else { + lower_page(&ancestor.page(*space)?, &page).ok() }; match lowered { Some(lowered) => { @@ -284,6 +285,11 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result(space: &'s ObjectSpace<'_>, from: ExGuid) -> impl Iterator + 's { + std::iter::successors(Some(from), |rid| space.revisions.get(rid)?.dependency) +} + /// A page's title, where it has one, as a page made for it takes. fn titled(page: &onestore::page::Page) -> Option<&str> { page.objects diff --git a/crates/notebook/tests/cloud.rs b/crates/notebook/tests/cloud.rs index b473797c5e4002ad982ed7db9db3254fea735b59..44644a521cc46b685a3132bd459ea4363163e8cd 100644 --- a/crates/notebook/tests/cloud.rs +++ b/crates/notebook/tests/cloud.rs @@ -873,12 +873,12 @@ fn report(token: &str, label: &str, server: &Server, devices: &[Device]) { eprintln!("{line}"); } -/// A section as Snowbound makes it, three titled pages no one has edited yet, and its pages. -fn untouched() -> (Vec, Vec<(ExGuid, Page)>) { +/// A section as Snowbound makes it, `count` titled pages no one has edited yet, and its pages. +fn untouched(count: usize) -> (Vec, Vec<(ExGuid, Page)>) { let source = onestore::create_empty_section("first.one", None).unwrap(); let arena = onestore::Arena::default(); let mut section = Section::open(&arena, source).unwrap(); - for n in 0..3 { + for n in 0..count { let creation = PageCreation::new(None, Some(&format!("Page {n}")), "Fixture").unwrap(); edit( &mut section, @@ -896,53 +896,46 @@ fn untouched() -> (Vec, Vec<(ExGuid, Page)>) { (section.image(), pages) } -/// Two devices each make one edit to `source`, then the one whose upload became the file -/// merges the other's, a conflict version; returns the file. -fn merged_first_edits(source: &[u8], edits: &[Vec; 2], winner: usize) -> Vec { +const DEVICES: [&str; 2] = ["Mac", "iPhone"]; + +/// The copies two devices make of `source`, each publishing its edit to its own. +fn diverged(source: &[u8], edits: &[Vec; 2]) -> [Vec; 2] { + [0, 1].map(|n| { + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, source.to_vec()).unwrap(); + edit(&mut section, DEVICES[n], edits[n].clone()); + section.seal().unwrap(); + section.image() + }) +} + +/// The file once the device whose copy it is has merged the other's, a conflict version. +fn merged(copies: &[Vec; 2], winner: usize) -> Vec { let directory = tempfile::tempdir().unwrap(); + let file = &copies[winner]; let mut server = Server { - current: source.to_vec(), - id: 0, - next: 0, - conflicts: Vec::new(), + current: file.clone(), + id: 1, + next: 1, + conflicts: vec![(2, copies[1 - winner].clone(), DEVICES[1 - winner].into())], kept: 0, }; - let mut devices: Vec = ["Mac", "iPhone"] - .into_iter() - .enumerate() - .map(|(n, name)| Device { - name: name.into(), - copy: Copy { - bytes: source.to_vec(), - base: 0, - dirty: false, - versions: Vec::new(), - online: true, - }, - replica: Replica::create(directory.path().join(format!("{n}.sqlite")), source).unwrap(), - }) - .collect(); - for (device, ops) in devices.iter_mut().zip(edits) { - let edit = Edit { - at: crate_now(), - ops: ops.clone(), - }; - device.replica.apply(&device.name, edit).unwrap(); - device.sync(&mut server); - } - let lost = &devices[1 - winner]; - let lost = (server.next(), lost.copy.bytes.clone(), lost.name.clone()); - server.conflicts.push(lost); - server.current = devices[winner].copy.bytes.clone(); - server.id = server.next(); - let device = &mut devices[winner]; - device.copy.base = server.id; - device.copy.dirty = false; + let mut device = Device { + name: DEVICES[winner].into(), + copy: Copy { + bytes: file.clone(), + base: 1, + dirty: false, + versions: Vec::new(), + online: true, + }, + replica: Replica::create(directory.path().join("replica.sqlite"), file).unwrap(), + }; device.deliver(&server); device.sync(&mut server); assert!(server.conflicts.is_empty()); assert_eq!(server.kept, 0, "the version was set aside whole"); - device.copy.bytes.clone() + device.copy.bytes } fn title(page: &Page) -> &TextObject { @@ -973,7 +966,7 @@ fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op { /// set aside. #[test] fn first_edits_on_two_devices_merge_as_edits() { - let (source, pages) = untouched(); + let (source, pages) = untouched(3); let (space, page) = &pages[0]; let body = lower_page(page, &with_body(page, "Typed on the iPhone.")) .unwrap() @@ -982,7 +975,7 @@ fn first_edits_on_two_devices_merge_as_edits() { .collect(); let edits = [vec![typing_title(&pages[0], "Mac: ")], body]; for winner in 0..2 { - let image = merged_first_edits(&source, &edits, winner); + let image = merged(&diverged(&source, &edits), winner); let (listed, conflicts) = texts_of(&image); assert!(conflicts.is_empty(), "{winner}: {conflicts}"); assert!( @@ -995,11 +988,11 @@ fn first_edits_on_two_devices_merge_as_edits() { /// First edits that clash still leave OneNote's conflict page. #[test] fn clashing_first_edits_leave_a_conflict_page() { - let (source, pages) = untouched(); + let (source, pages) = untouched(3); let typed = ["Mac: ", "iPhone: "]; let edits = typed.map(|with| vec![typing_title(&pages[0], with)]); for winner in 0..2 { - let image = merged_first_edits(&source, &edits, winner); + let image = merged(&diverged(&source, &edits), winner); let arena = onestore::Arena::default(); let mut section = Section::open(&arena, image).unwrap(); let conflicts = section.conflicts().unwrap(); @@ -1017,7 +1010,7 @@ fn clashing_first_edits_leave_a_conflict_page() { /// Pages a version moved and deleted stay so beside another device's first edit. #[test] fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { - let (source, pages) = untouched(); + let (source, pages) = untouched(3); let reordering = vec![ Op::Section(SectionOp::Pages(vec![ PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(), @@ -1026,7 +1019,7 @@ fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { ]; let edits = [vec![typing_title(&pages[0], "Mac: ")], reordering]; for winner in 0..2 { - let image = merged_first_edits(&source, &edits, winner); + let image = merged(&diverged(&source, &edits), winner); let arena = onestore::Arena::default(); let mut section = Section::open(&arena, image).unwrap(); let titles: Vec = section @@ -1039,3 +1032,177 @@ fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { assert!(section.conflicts().unwrap().is_empty(), "{winner}"); } } + +/// 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(); + iphone.push(Op::Section(SectionOp::Pages(vec![ + PageEdit::move_to(pages[3].0, Some(pages[0].0), 1).unwrap(), + ]))); + iphone.push(Op::Section(SectionOp::Delete(vec![pages[2].0]))); + [vec![typing_title(&pages[0], "Mac: ")], iphone] +} + +/// `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)]) { + let space = pages[0].0; + for copy in copies { + let store = onestore::Store::parse(copy).unwrap(); + let index = onestore::RevisionIndex::parse(&store).unwrap(); + let head = index.active(space).unwrap(); + assert_eq!(index.spaces[&space].revisions[&head].dependency, None); + } + // Whether a page holds the Mac's edit and the iPhone's. + let marks = |page: &Page| { + ( + title(page).text.text().starts_with("Mac: "), + bodies(page) + .iter() + .any(|(_, text)| text == "Typed on the iPhone."), + ) + }; + for winner in 0..2 { + let image = merged(copies, winner); + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, image).unwrap(); + let titles: Vec = section + .pages() + .unwrap() + .into_iter() + .map(|(_, title, _)| title) + .collect(); + let first = ["Mac: Page 0", "Page 0"][winner]; + 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." + ); + let conflicts = section.conflicts().unwrap(); + let [(of, conflict)] = &conflicts[..] else { + panic!("{winner}: {conflicts:?}") + }; + assert_eq!((*of, conflict.len()), (space, 1)); + let page = section.page(space).unwrap(); + assert_eq!(marks(&page), (winner == 0, winner == 1)); + let beside = section.page(conflict[0].space).unwrap(); + assert_eq!(marks(&beside), (winner == 1, winner == 0)); + } +} + +/// A page both devices edited past the chain cap, each with a checkpoint, merges as a +/// 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); +} + +/// The same for sections written before #152, whose first edit to a page checkpointed: +/// `corpus/icloud-merge/first-edit-checkpoints`. +#[test] +fn pages_first_edited_before_152_merge_as_conflict_pages() { + let row = format!( + "{}/../../corpus/icloud-merge/first-edit-checkpoints", + env!("CARGO_MANIFEST_DIR") + ); + let copies = DEVICES.map(|device| std::fs::read(format!("{row}/{device}.one")).unwrap()); + let arena = onestore::Arena::default(); + let mut section = Section::open(&arena, copies[0].clone()).unwrap(); + let pages: Vec<(ExGuid, Page)> = section + .pages() + .unwrap() + .into_iter() + .map(|(space, ..)| (space, section.page(space).unwrap())) + .collect(); + assert_merged_beside(&copies, &pages); +} + +/// A page one device takes past the chain cap merges as edits with the other device's edits +/// elsewhere: the file's state of the page is one the version passed through. +#[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]); + 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, "Mac: Page 0\nPage 1\nPage 1 from the iPhone.\n", + "{winner}" + ); + } +} -- 2.54.1