authoravatarclover caruso<git@paperclover.net>2026-10-16 14:16:01-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
log126c7a891494ab1998b1e6de368edf62a80cc7ab
treeeb11e604b09cb8af45223a45423ae5dea61d480a
parentb7ace116f7bb71058ff631dacd943b545238f439
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

7 files changed, 261 insertions(+), 76 deletions(-)

arc/sync.md+4-2
......@@ -279,10 +279,12 @@ fact, and each sync step merges the versions it finds before anything else:
279279```text
280280for each version V the remote lists (Remote::versions)
281281 C := the file, read coordinated
282 per space of V: a := its newest revision C holds, or holds merged
282 per space of V: a := its newest revision C holds, or holds merged,
283 else C's newest that V holds (V's chain stopped at a checkpoint), else none
283284 ancestor := V opened at those revisions (Section::open_at)
284285 ops := lower_page(ancestor[S], V[S]) per page V changed, V's new pages imported under their
285 identities, its deletions, moves and conflict pages
286 identities, its deletions, moves and conflict pages; a page with no a stays C's,
287 V's beside it as a conflict page
286288 replay ops on C through merge.rs (conflict pages where they clash), seal_as the named
287289 revisions, publish on C's stamp; then retire V (resolved, removed)
288290```
corpus/icloud-merge/README.md+8
......@@ -11,3 +11,11 @@ appended text and the new page replay, and the clashing edit becomes a conflict
1111`--screenshots`): both pages list, the first with its conflict mark and the yellow bar "This page
1212has changes that could not be merged during synchronization", its positioned outline ending
1313"(typed on the iPhone)", and "From the iPhone" holding "Written offline on the iPhone."
14
15`first-edit-checkpoints` holds the two copies of `apart` in `crates/notebook/tests/cloud.rs`
16(a fresh Snowbound section of four pages; the Mac types in the first page's title, the iPhone
17writes on the first two pages, moves the fourth first and deletes the third) as the writer
18before #152 stored them: each page's first edit a checkpoint with no dependency, so the first
19page's two copies share no revision. Merged either way round, that page stays the file's with
20the other copy as its conflict page, and the rest merges
21(`pages_first_edited_before_152_merge_as_conflict_pages`).
corpus/icloud-merge/first-edit-checkpoints/Mac.one created
Binary files /dev/null and b/corpus/icloud-merge/first-edit-checkpoints/Mac.one differ
corpus/icloud-merge/first-edit-checkpoints/iPhone.one created
Binary files /dev/null and b/corpus/icloud-merge/first-edit-checkpoints/iPhone.one differ
crates/notebook/README.md+4-2
......@@ -144,10 +144,12 @@ of the local one, placed the same way (`corpus/conflict-page/native-pages`,
144144A remote a file provider keeps may list versions of the file beside it (`Remote::versions`,
145145`version`, `retire`), as iCloud Drive keeps a commit that lost to another device's. Each step
146146merges them first (`resolve.rs`): per object space, the newest revision of the version's that
147the file holds, or holds merged, is where the two last agreed; the section opened at those
147the file holds, or holds merged, is where the two last agreed, or failing that (a chain that
148stops at a checkpoint) the newest of the file's the version holds; the section opened at those
148149revisions (`onestore::Section::open_at`) is the base the version's changes lower from
149150(`lower_page`), and they replay on the file through the rebase rules above, conflict pages where
150they clash. The revisions the merge writes are named after the version's it merged
151they clash. A page whose two copies share no revision stays the file's, the version's kept
152beside it as a conflict page (`corpus/icloud-merge/first-edit-checkpoints`). The revisions the merge writes are named after the version's it merged
151153(`Section::seal_as`), so a version merged once, here or on another device, merges as nothing
152154again. It is published on the file's stamp, then retired; one of another section, or one that
153155cannot be read, is retired with `keep` for the remote to keep beside the file.
crates/notebook/src/resolve.rs+30-24
......@@ -12,8 +12,8 @@
1212
1313use crate::{PendingEdit, Result, merge};
1414use onestore::{
15 Arena, ConflictPage, ExGuid, PageCreation, PageEdit, RevisionIndex, Section, Store,
16 Transaction,
15 Arena, ConflictPage, ExGuid, ObjectSpace, PageCreation, PageEdit, RevisionIndex, Section,
16 Store, Transaction,
1717 op::{Edit, Op, SectionOp, lower_page},
1818};
1919use sha2::{Digest, Sha256};
......@@ -93,32 +93,33 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result<Merg
9393 .find(|theirs| merged(**theirs, "page") == rid)
9494 .copied()
9595 };
96 let mut at = Some(head);
97 while let Some(rid) = at {
98 if let Some(base) = meets(rid) {
99 shared.insert(*space, base);
100 break;
101 }
102 at = revisions
103 .revisions
104 .get(&rid)
105 .and_then(|revision| revision.dependency);
96 // Failing that, as when the version's chain stops at a checkpoint, the newest state of
97 // the file's that the version passed through.
98 let base = chain(revisions, head).find_map(meets).or_else(|| {
99 let ours = ours?;
100 chain(ours, *ours.labels.get(&(ExGuid::default(), 1))?)
101 .find(|rid| revisions.revisions.contains_key(rid))
102 });
103 if let Some(base) = base {
104 shared.insert(*space, base);
106105 }
107106 }
108 let root = file.root;
109 // Without a state of the page list both share, the version's list counts as unchanged.
110107 let head = |space: &ExGuid| heads.get(space).copied();
111 if let std::collections::btree_map::Entry::Vacant(entry) = shared.entry(root) {
112 entry.insert(head(&root).ok_or(onestore::Error {
113 offset: 0,
114 message: "The version has no page list",
115 })?);
116 }
117108 let changed: BTreeSet<ExGuid> = heads
118109 .iter()
119110 .filter(|(space, head)| shared.get(space) != Some(head))
120111 .map(|(space, _)| *space)
121112 .collect();
113 // Spaces both hold with no state in common read as the version leaves them: its page list
114 // as unchanged, a page as one kept beside the file's when they differ.
115 let unshared: BTreeSet<ExGuid> = heads
116 .keys()
117 .filter(|space| !shared.contains_key(space) && file.spaces.contains_key(space))
118 .copied()
119 .collect();
120 for space in &unshared {
121 shared.insert(*space, heads[space]);
122 }
122123 let arenas = (Arena::default(), Arena::default(), Arena::default());
123124 let mut ancestor =
124125 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<Merg
188189 continue;
189190 }
190191 let page = theirs.page(*space)?;
191 let lowered = match shared.get(space) {
192 Some(_) => lower_page(&ancestor.page(*space)?, &page).ok(),
193 None if ours.page(*space).ok().as_ref() == Some(&page) => Some(Vec::new()),
194 None => None,
192 let lowered = if unshared.contains(space) {
193 (ours.page(*space).ok().as_ref() == Some(&page)).then(Vec::new)
194 } else {
195 lower_page(&ancestor.page(*space)?, &page).ok()
195196 };
196197 match lowered {
197198 Some(lowered) => {
......@@ -284,6 +285,11 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result<Merg
284285 }
285286}
286287
288/// `from` and the revisions it depends on, newest first.
289fn chain<'s>(space: &'s ObjectSpace<'_>, from: ExGuid) -> impl Iterator<Item = ExGuid> + 's {
290 std::iter::successors(Some(from), |rid| space.revisions.get(rid)?.dependency)
291}
292
287293/// A page's title, where it has one, as a page made for it takes.
288294fn titled(page: &onestore::page::Page) -> Option<&str> {
289295 page.objects
crates/notebook/tests/cloud.rs+215-48
......@@ -873,12 +873,12 @@ fn report(token: &str, label: &str, server: &Server, devices: &[Device]) {
873873 eprintln!("{line}");
874874}
875875
876/// A section as Snowbound makes it, three titled pages no one has edited yet, and its pages.
877fn untouched() -> (Vec<u8>, Vec<(ExGuid, Page)>) {
876/// A section as Snowbound makes it, `count` titled pages no one has edited yet, and its pages.
877fn untouched(count: usize) -> (Vec<u8>, Vec<(ExGuid, Page)>) {
878878 let source = onestore::create_empty_section("first.one", None).unwrap();
879879 let arena = onestore::Arena::default();
880880 let mut section = Section::open(&arena, source).unwrap();
881 for n in 0..3 {
881 for n in 0..count {
882882 let creation = PageCreation::new(None, Some(&format!("Page {n}")), "Fixture").unwrap();
883883 edit(
884884 &mut section,
......@@ -896,53 +896,46 @@ fn untouched() -> (Vec<u8>, Vec<(ExGuid, Page)>) {
896896 (section.image(), pages)
897897}
898898
899/// Two devices each make one edit to `source`, then the one whose upload became the file
900/// merges the other's, a conflict version; returns the file.
901fn merged_first_edits(source: &[u8], edits: &[Vec<Op>; 2], winner: usize) -> Vec<u8> {
899const DEVICES: [&str; 2] = ["Mac", "iPhone"];
900
901/// The copies two devices make of `source`, each publishing its edit to its own.
902fn diverged(source: &[u8], edits: &[Vec<Op>; 2]) -> [Vec<u8>; 2] {
903 [0, 1].map(|n| {
904 let arena = onestore::Arena::default();
905 let mut section = Section::open(&arena, source.to_vec()).unwrap();
906 edit(&mut section, DEVICES[n], edits[n].clone());
907 section.seal().unwrap();
908 section.image()
909 })
910}
911
912/// The file once the device whose copy it is has merged the other's, a conflict version.
913fn merged(copies: &[Vec<u8>; 2], winner: usize) -> Vec<u8> {
902914 let directory = tempfile::tempdir().unwrap();
915 let file = &copies[winner];
903916 let mut server = Server {
904 current: source.to_vec(),
905 id: 0,
906 next: 0,
907 conflicts: Vec::new(),
917 current: file.clone(),
918 id: 1,
919 next: 1,
920 conflicts: vec![(2, copies[1 - winner].clone(), DEVICES[1 - winner].into())],
908921 kept: 0,
909922 };
910 let mut devices: Vec<Device> = ["Mac", "iPhone"]
911 .into_iter()
912 .enumerate()
913 .map(|(n, name)| Device {
914 name: name.into(),
915 copy: Copy {
916 bytes: source.to_vec(),
917 base: 0,
918 dirty: false,
919 versions: Vec::new(),
920 online: true,
921 },
922 replica: Replica::create(directory.path().join(format!("{n}.sqlite")), source).unwrap(),
923 })
924 .collect();
925 for (device, ops) in devices.iter_mut().zip(edits) {
926 let edit = Edit {
927 at: crate_now(),
928 ops: ops.clone(),
929 };
930 device.replica.apply(&device.name, edit).unwrap();
931 device.sync(&mut server);
932 }
933 let lost = &devices[1 - winner];
934 let lost = (server.next(), lost.copy.bytes.clone(), lost.name.clone());
935 server.conflicts.push(lost);
936 server.current = devices[winner].copy.bytes.clone();
937 server.id = server.next();
938 let device = &mut devices[winner];
939 device.copy.base = server.id;
940 device.copy.dirty = false;
923 let mut device = Device {
924 name: DEVICES[winner].into(),
925 copy: Copy {
926 bytes: file.clone(),
927 base: 1,
928 dirty: false,
929 versions: Vec::new(),
930 online: true,
931 },
932 replica: Replica::create(directory.path().join("replica.sqlite"), file).unwrap(),
933 };
941934 device.deliver(&server);
942935 device.sync(&mut server);
943936 assert!(server.conflicts.is_empty());
944937 assert_eq!(server.kept, 0, "the version was set aside whole");
945 device.copy.bytes.clone()
938 device.copy.bytes
946939}
947940
948941fn title(page: &Page) -> &TextObject {
......@@ -973,7 +966,7 @@ fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op {
973966/// set aside.
974967#[test]
975968fn first_edits_on_two_devices_merge_as_edits() {
976 let (source, pages) = untouched();
969 let (source, pages) = untouched(3);
977970 let (space, page) = &pages[0];
978971 let body = lower_page(page, &with_body(page, "Typed on the iPhone."))
979972 .unwrap()
......@@ -982,7 +975,7 @@ fn first_edits_on_two_devices_merge_as_edits() {
982975 .collect();
983976 let edits = [vec![typing_title(&pages[0], "Mac: ")], body];
984977 for winner in 0..2 {
985 let image = merged_first_edits(&source, &edits, winner);
978 let image = merged(&diverged(&source, &edits), winner);
986979 let (listed, conflicts) = texts_of(&image);
987980 assert!(conflicts.is_empty(), "{winner}: {conflicts}");
988981 assert!(
......@@ -995,11 +988,11 @@ fn first_edits_on_two_devices_merge_as_edits() {
995988/// First edits that clash still leave OneNote's conflict page.
996989#[test]
997990fn clashing_first_edits_leave_a_conflict_page() {
998 let (source, pages) = untouched();
991 let (source, pages) = untouched(3);
999992 let typed = ["Mac: ", "iPhone: "];
1000993 let edits = typed.map(|with| vec![typing_title(&pages[0], with)]);
1001994 for winner in 0..2 {
1002 let image = merged_first_edits(&source, &edits, winner);
995 let image = merged(&diverged(&source, &edits), winner);
1003996 let arena = onestore::Arena::default();
1004997 let mut section = Section::open(&arena, image).unwrap();
1005998 let conflicts = section.conflicts().unwrap();
......@@ -1017,7 +1010,7 @@ fn clashing_first_edits_leave_a_conflict_page() {
10171010/// Pages a version moved and deleted stay so beside another device's first edit.
10181011#[test]
10191012fn page_moves_and_deletions_survive_a_first_edit_elsewhere() {
1020 let (source, pages) = untouched();
1013 let (source, pages) = untouched(3);
10211014 let reordering = vec![
10221015 Op::Section(SectionOp::Pages(vec![
10231016 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() {
10261019 ];
10271020 let edits = [vec![typing_title(&pages[0], "Mac: ")], reordering];
10281021 for winner in 0..2 {
1029 let image = merged_first_edits(&source, &edits, winner);
1022 let image = merged(&diverged(&source, &edits), winner);
10301023 let arena = onestore::Arena::default();
10311024 let mut section = Section::open(&arena, image).unwrap();
10321025 let titles: Vec<String> = section
......@@ -1039,3 +1032,177 @@ fn page_moves_and_deletions_survive_a_first_edit_elsewhere() {
10391032 assert!(section.conflicts().unwrap().is_empty(), "{winner}");
10401033 }
10411034}
1035
1036/// What the Mac and the iPhone do to four pages: the Mac types in the first page's title; the
1037/// iPhone writes on the first and second pages, moves the fourth first and deletes the third.
1038fn apart(pages: &[(ExGuid, Page)]) -> [Vec<Op>; 2] {
1039 let body = |(space, page): &(ExGuid, Page), text| {
1040 let space = *space;
1041 lower_page(page, &with_body(page, text))
1042 .unwrap()
1043 .into_iter()
1044 .map(move |op| Op::Page { space, op })
1045 };
1046 let mut iphone: Vec<Op> = body(&pages[0], "Typed on the iPhone.")
1047 .chain(body(&pages[1], "Page 1 from the iPhone."))
1048 .collect();
1049 iphone.push(Op::Section(SectionOp::Pages(vec![
1050 PageEdit::move_to(pages[3].0, Some(pages[0].0), 1).unwrap(),
1051 ])));
1052 iphone.push(Op::Section(SectionOp::Delete(vec![pages[2].0])));
1053 [vec![typing_title(&pages[0], "Mac: ")], iphone]
1054}
1055
1056/// `apart`'s copies merged either way round, where the first page's two copies share no
1057/// revision: that page stays the file's with the other kept beside it as a conflict page, and
1058/// every other change merges.
1059fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) {
1060 let space = pages[0].0;
1061 for copy in copies {
1062 let store = onestore::Store::parse(copy).unwrap();
1063 let index = onestore::RevisionIndex::parse(&store).unwrap();
1064 let head = index.active(space).unwrap();
1065 assert_eq!(index.spaces[&space].revisions[&head].dependency, None);
1066 }
1067 // Whether a page holds the Mac's edit and the iPhone's.
1068 let marks = |page: &Page| {
1069 (
1070 title(page).text.text().starts_with("Mac: "),
1071 bodies(page)
1072 .iter()
1073 .any(|(_, text)| text == "Typed on the iPhone."),
1074 )
1075 };
1076 for winner in 0..2 {
1077 let image = merged(copies, winner);
1078 let arena = onestore::Arena::default();
1079 let mut section = Section::open(&arena, image).unwrap();
1080 let titles: Vec<String> = section
1081 .pages()
1082 .unwrap()
1083 .into_iter()
1084 .map(|(_, title, _)| title)
1085 .collect();
1086 let first = ["Mac: Page 0", "Page 0"][winner];
1087 assert_eq!(titles, ["Page 3", first, "Page 1"], "{winner}");
1088 assert_eq!(
1089 bodies(&section.page(pages[1].0).unwrap())[0].1,
1090 "Page 1 from the iPhone."
1091 );
1092 let conflicts = section.conflicts().unwrap();
1093 let [(of, conflict)] = &conflicts[..] else {
1094 panic!("{winner}: {conflicts:?}")
1095 };
1096 assert_eq!((*of, conflict.len()), (space, 1));
1097 let page = section.page(space).unwrap();
1098 assert_eq!(marks(&page), (winner == 0, winner == 1));
1099 let beside = section.page(conflict[0].space).unwrap();
1100 assert_eq!(marks(&beside), (winner == 1, winner == 0));
1101 }
1102}
1103
1104/// A page both devices edited past the chain cap, each with a checkpoint, merges as a
1105/// conflict page; the rest of the version still merges.
1106#[test]
1107fn pages_checkpointed_on_both_devices_merge_as_conflict_pages() {
1108 let (source, pages) = untouched(4);
1109 let arena = onestore::Arena::default();
1110 let mut section = Section::open(&arena, source).unwrap();
1111 let (space, page) = &pages[0];
1112 let body = lower_page(page, &with_body(page, "Body"))
1113 .unwrap()
1114 .into_iter()
1115 .map(|op| Op::Page { space: *space, op })
1116 .collect();
1117 edit(&mut section, "Fixture", body);
1118 section.seal().unwrap();
1119 let (text, _) = bodies(&section.page(*space).unwrap())[0].clone();
1120 for n in 0..510 {
1121 let (range, with) = if n % 2 == 0 { (0..0, "x") } else { (0..1, "") };
1122 let op = PageOp::Text {
1123 text,
1124 range,
1125 with: with.into(),
1126 };
1127 edit(
1128 &mut section,
1129 "Fixture",
1130 vec![Op::Page { space: *space, op }],
1131 );
1132 section.seal().unwrap();
1133 }
1134 let source = section.image();
1135 let pages: Vec<(ExGuid, Page)> = pages
1136 .iter()
1137 .map(|(space, _)| (*space, section.page(*space).unwrap()))
1138 .collect();
1139 assert_merged_beside(&diverged(&source, &apart(&pages)), &pages);
1140}
1141
1142/// The same for sections written before #152, whose first edit to a page checkpointed:
1143/// `corpus/icloud-merge/first-edit-checkpoints`.
1144#[test]
1145fn pages_first_edited_before_152_merge_as_conflict_pages() {
1146 let row = format!(
1147 "{}/../../corpus/icloud-merge/first-edit-checkpoints",
1148 env!("CARGO_MANIFEST_DIR")
1149 );
1150 let copies = DEVICES.map(|device| std::fs::read(format!("{row}/{device}.one")).unwrap());
1151 let arena = onestore::Arena::default();
1152 let mut section = Section::open(&arena, copies[0].clone()).unwrap();
1153 let pages: Vec<(ExGuid, Page)> = section
1154 .pages()
1155 .unwrap()
1156 .into_iter()
1157 .map(|(space, ..)| (space, section.page(space).unwrap()))
1158 .collect();
1159 assert_merged_beside(&copies, &pages);
1160}
1161
1162/// A page one device takes past the chain cap merges as edits with the other device's edits
1163/// elsewhere: the file's state of the page is one the version passed through.
1164#[test]
1165fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() {
1166 let (source, pages) = untouched(2);
1167 let arena = onestore::Arena::default();
1168 let mut section = Section::open(&arena, source.clone()).unwrap();
1169 for n in 0..521 {
1170 let with = if n == 520 { "Mac: " } else { "x" };
1171 let mut op = typing_title(&pages[0], with);
1172 if let Op::Page {
1173 op: PageOp::Text { range, with, .. },
1174 ..
1175 } = &mut op
1176 && n % 2 == 1
1177 {
1178 *range = 0..1;
1179 with.clear();
1180 }
1181 edit(&mut section, "Mac", vec![op]);
1182 section.seal().unwrap();
1183 }
1184 let mac = section.image();
1185 let store = onestore::Store::parse(&mac).unwrap();
1186 let index = onestore::RevisionIndex::parse(&store).unwrap();
1187 let space = &index.spaces[&pages[0].0];
1188 let chain = std::iter::successors(index.active(pages[0].0).ok(), |rid| {
1189 space.revisions[rid].dependency
1190 });
1191 assert!(chain.count() < 512);
1192 let (space, page) = &pages[1];
1193 let body = lower_page(page, &with_body(page, "Page 1 from the iPhone."))
1194 .unwrap()
1195 .into_iter()
1196 .map(|op| Op::Page { space: *space, op })
1197 .collect();
1198 let [_, iphone] = diverged(&source, &[Vec::new(), body]);
1199 for winner in 0..2 {
1200 let image = merged(&[mac.clone(), iphone.clone()], winner);
1201 let (listed, conflicts) = texts_of(&image);
1202 assert!(conflicts.is_empty(), "{winner}: {conflicts}");
1203 assert_eq!(
1204 listed, "Mac: Page 0\nPage 1\nPage 1 from the iPhone.\n",
1205 "{winner}"
1206 );
1207 }
1208}