authoravatarclover caruso<git@paperclover.net>2026-10-19 16:49:10-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
logbe5dcab42ad21c294fc9f06c65ac1f3a7d1a4743
treeb67570a18f6eb05b76f08d401bf81be5253dd897
parent33fc8d5777abe6deb47c09f062df2b910cd5d0cb
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(notebook): find merged states past a checkpoint; carry an unshared page list

Review of the tolerant merge. When the version's chain stops at a checkpoint, the fallback walked the file's chain only for revisions the version holds; it now also takes a file revision that merged one of the version's, whose version revision is then the base, so a page merged once and then taken past the 512-revision cap does not replay what the file already merged. A page list with no revision in common opened without the version's new pages and set the whole version aside; it now names them too, and counts as already merged only the conflict pages under pages with a state both copies share, so a new page's conflict page arrives with it. A page both copies checkpointed stays a conflict page rather than merging from the newest revision both files list: in file order that can be a checkpoint or a labelled history snapshot OneNote wrote rather than a common ancestor, and lowering from a wrong base can delete text. New tests cover one device past the cap with both editing the page, a merge followed by a checkpoint, a page the file deleted with no common revision, and a page list with none, with a conflict page under a new page. Refs #152 Assisted-by: claude-opus-5.5

4 files changed, 276 insertions(+), 96 deletions(-)

arc/sync.md+2-1
......@@ -280,7 +280,8 @@ fact, and each sync step merges the versions it finds before anything else:
280280for each version V the remote lists (Remote::versions)
281281 C := the file, read coordinated
282282 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
283 else C's newest that V holds or that merged one of V's (V's chain stopped
284 at a checkpoint), else none
284285 ancestor := V opened at those revisions (Section::open_at)
285286 ops := lower_page(ancestor[S], V[S]) per page V changed, V's new pages imported under their
286287 identities, its deletions, moves and conflict pages; a page with no a stays C's,
crates/notebook/README.md+11-10
......@@ -145,16 +145,17 @@ A remote a file provider keeps may list versions of the file beside it (`Remote:
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
147147the 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
149revisions (`onestore::Section::open_at`) is the base the version's changes lower from
150(`lower_page`), and they replay on the file through the rebase rules above, conflict pages where
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
153merge writes are named after the version's it merged and the file's they depend on
154(`Section::seal_as`), so a version merged once, here or on another device or by an older build,
155merges as nothing again, and merges of one version onto different states never share a name. It
156is published on the file's stamp, then retired; one of another section, or one that cannot be
157read, is retired with `keep` for the remote to keep beside the file.
148stops at a checkpoint) the newest of the file's that the version holds or that merged one of the
149version's; the section opened at those revisions (`onestore::Section::open_at`) is the base the
150version's changes lower from (`lower_page`), and they replay on the file through the rebase
151rules above, conflict pages where they clash. A page whose two copies share no revision stays
152the file's, the version's kept beside it as a conflict page
153(`corpus/icloud-merge/first-edit-checkpoints`), and a page list with none counts as unchanged.
154The revisions the merge writes are named after the version's it merged and the file's they
155depend on (`Section::seal_as`), so a version merged once, here or on another device or by an
156older build, merges as nothing again, and merges of one version onto different states never
157share a name. It is published on the file's stamp, then retired; one of another section, or one
158that cannot be read, is retired with `keep` for the remote to keep beside the file.
158159A provider can also lose an upload outright, another device's landing over it with no version
159160kept. A remote keeps what it publishes with `published::Published` (`keep` after each
160161publication) and lists each image the file no longer stands as among its versions
crates/notebook/src/resolve.rs+22-7
......@@ -112,11 +112,18 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result<Merg
112112 .copied()
113113 };
114114 // Failing that, as when the version's chain stops at a checkpoint, the newest state of
115 // the file's that the version passed through.
115 // the file's that the version passed through, or that merged one of the version's.
116116 let base = chain(revisions, head).find_map(meets).or_else(|| {
117117 let ours = ours?;
118 chain(ours, *ours.labels.get(&(ExGuid::default(), 1))?)
119 .find(|rid| revisions.revisions.contains_key(rid))
118 chain(ours, *ours.labels.get(&(ExGuid::default(), 1))?).find_map(|rid| {
119 if revisions.revisions.contains_key(&rid) {
120 return Some(rid);
121 }
122 let onto = ours.revisions.get(&rid)?.dependency;
123 (revisions.revisions.keys())
124 .find(|theirs| merged(**theirs, onto) == rid || named(**theirs, "page") == rid)
125 .copied()
126 })
120127 });
121128 if let Some(base) = base {
122129 shared.insert(*space, base);
......@@ -129,14 +136,20 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result<Merg
129136 .map(|(space, _)| *space)
130137 .collect();
131138 // Spaces both hold with no state in common read as the version leaves them: its page list
132 // as unchanged, a page as one kept beside the file's when they differ.
139 // as unchanged, so with every page it lists, and a page as one kept beside the file's when
140 // they differ.
133141 let unshared: BTreeSet<ExGuid> = heads
134142 .keys()
135143 .filter(|space| !shared.contains_key(space) && file.spaces.contains_key(space))
136144 .copied()
137145 .collect();
138 for space in &unshared {
139 shared.insert(*space, heads[space]);
146 // Pages with a state both share, under which the base's conflict pages are both's.
147 let common: BTreeSet<ExGuid> = shared.keys().copied().collect();
148 let listless = unshared.contains(&file.root);
149 for (space, head) in &heads {
150 if listless || unshared.contains(space) {
151 shared.entry(*space).or_insert(*head);
152 }
140153 }
141154 let arenas = (Arena::default(), Arena::default(), Arena::default());
142155 let mut ancestor =
......@@ -189,7 +202,9 @@ pub(crate) fn merge(current: &[u8], version: &[u8], device: &str) -> Result<Merg
189202 .flat_map(|(of, pages)| pages.into_iter().map(move |page| (page.space, (of, page))))
190203 .collect())
191204 };
192 let (was, now) = (conflicts(&mut ancestor)?, conflicts(&mut theirs)?);
205 let mut was = conflicts(&mut ancestor)?;
206 was.retain(|_, (of, _)| common.contains(of));
207 let now = conflicts(&mut theirs)?;
193208 let removed: Vec<ExGuid> = before
194209 .iter()
195210 .filter(|space| !kept.contains(space))
crates/notebook/tests/cloud.rs+241-78
......@@ -962,6 +962,68 @@ fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op {
962962 }
963963}
964964
965/// The `n`th of a run of edits that type "x" at the start of a page's title and take it out.
966fn toggling(page: &(ExGuid, Page), n: usize) -> Vec<Op> {
967 let mut op = typing_title(page, "x");
968 if let Op::Page {
969 op: PageOp::Text { range, with, .. },
970 ..
971 } = &mut op
972 && n % 2 == 1
973 {
974 *range = 0..1;
975 with.clear();
976 }
977 vec![op]
978}
979
980/// The ops adding a body of `text` to a page.
981fn body((space, page): &(ExGuid, Page), text: &str) -> Vec<Op> {
982 lower_page(page, &with_body(page, text))
983 .unwrap()
984 .into_iter()
985 .map(|op| Op::Page { space: *space, op })
986 .collect()
987}
988
989/// `image` with each of `edits` sealed as a revision of its own.
990fn sealed(image: Vec<u8>, author: &str, edits: impl IntoIterator<Item = Vec<Op>>) -> Vec<u8> {
991 let arena = onestore::Arena::default();
992 let mut section = Section::open(&arena, image).unwrap();
993 for ops in edits {
994 edit(&mut section, author, ops);
995 section.seal().unwrap();
996 }
997 section.image()
998}
999
1000/// Revisions in the dependency chain of `space`'s newest revision in `image`.
1001fn depth(image: &[u8], space: ExGuid) -> usize {
1002 let store = onestore::Store::parse(image).unwrap();
1003 let index = onestore::RevisionIndex::parse(&store).unwrap();
1004 let revisions = &index.spaces[&space].revisions;
1005 std::iter::successors(index.active(space).ok(), |rid| revisions[rid].dependency).count()
1006}
1007
1008/// `pages` as `image` holds them.
1009fn reread(image: &[u8], pages: &[(ExGuid, Page)]) -> Vec<(ExGuid, Page)> {
1010 let arena = onestore::Arena::default();
1011 let section = Section::open(&arena, image.to_vec()).unwrap();
1012 pages
1013 .iter()
1014 .map(|(space, _)| (*space, section.page(*space).unwrap()))
1015 .collect()
1016}
1017
1018/// `untouched(count)` with its first page's chain `deep` revisions long.
1019fn deep(count: usize, deep: usize) -> (Vec<u8>, Vec<(ExGuid, Page)>) {
1020 let (source, pages) = untouched(count);
1021 let runs = deep - depth(&source, pages[0].0);
1022 let source = sealed(source, "Fixture", (0..runs).map(|n| toggling(&pages[0], n)));
1023 let pages = reread(&source, &pages);
1024 (source, pages)
1025}
1026
9651027/// Issue #152: the first edits two devices make to a page merge as edits, not as a version
9661028/// set aside.
9671029#[test]
......@@ -1036,16 +1098,8 @@ fn page_moves_and_deletions_survive_a_first_edit_elsewhere() {
10361098/// What the Mac and the iPhone do to four pages: the Mac types in the first page's title; the
10371099/// iPhone writes on the first and second pages, moves the fourth first and deletes the third.
10381100fn 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();
1101 let mut iphone = body(&pages[0], "Typed on the iPhone.");
1102 iphone.extend(body(&pages[1], "Page 1 from the iPhone."));
10491103 iphone.push(Op::Section(SectionOp::Pages(vec![
10501104 PageEdit::move_to(pages[3].0, Some(pages[0].0), 1).unwrap(),
10511105 ])));
......@@ -1056,7 +1110,7 @@ fn apart(pages: &[(ExGuid, Page)]) -> [Vec<Op>; 2] {
10561110/// `apart`'s copies merged either way round, where the first page's two copies share no
10571111/// revision: that page stays the file's with the other kept beside it as a conflict page, and
10581112/// every other change merges.
1059fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) {
1113fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)], first: &str) {
10601114 let space = pages[0].0;
10611115 for copy in copies {
10621116 let store = onestore::Store::parse(copy).unwrap();
......@@ -1083,8 +1137,8 @@ fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) {
10831137 .into_iter()
10841138 .map(|(_, title, _)| title)
10851139 .collect();
1086 let first = ["Mac: Page 0", "Page 0"][winner];
1087 assert_eq!(titles, ["Page 3", first, "Page 1"], "{winner}");
1140 let first = [format!("Mac: {first}"), first.to_owned()][winner].clone();
1141 assert_eq!(titles, ["Page 3", &first, "Page 1"], "{winner}");
10881142 assert_eq!(
10891143 bodies(&section.page(pages[1].0).unwrap())[0].1,
10901144 "Page 1 from the iPhone."
......@@ -1105,38 +1159,9 @@ fn assert_merged_beside(copies: &[Vec<u8>; 2], pages: &[(ExGuid, Page)]) {
11051159/// conflict page; the rest of the version still merges.
11061160#[test]
11071161fn 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);
1162 let (source, pages) = deep(4, 512);
1163 let first = title(&pages[0].1).text.text().to_owned();
1164 assert_merged_beside(&diverged(&source, &apart(&pages)), &pages, &first);
11401165}
11411166
11421167/// 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() {
11561181 .into_iter()
11571182 .map(|(space, ..)| (space, section.page(space).unwrap()))
11581183 .collect();
1159 assert_merged_beside(&copies, &pages);
1184 assert_merged_beside(&copies, &pages, "Page 0");
11601185}
11611186
11621187/// 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() {
11641189#[test]
11651190fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() {
11661191 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]);
1192 let runs = (0..520).map(|n| toggling(&pages[0], n));
1193 let mac = sealed(
1194 source.clone(),
1195 "Mac",
1196 runs.chain([vec![typing_title(&pages[0], "Mac: ")]]),
1197 );
1198 assert!(depth(&mac, pages[0].0) < 512);
1199 let [_, iphone] = diverged(
1200 &source,
1201 &[Vec::new(), body(&pages[1], "Page 1 from the iPhone.")],
1202 );
11991203 for winner in 0..2 {
12001204 let image = merged(&[mac.clone(), iphone.clone()], winner);
12011205 let (listed, conflicts) = texts_of(&image);
......@@ -1207,6 +1211,165 @@ fn a_page_past_the_chain_cap_on_one_device_merges_as_edits() {
12071211 }
12081212}
12091213
1214/// A page one device takes past the chain cap merges with the other device's edit to it.
1215#[test]
1216fn a_page_past_the_cap_on_one_device_merges_with_the_others_edit_to_it() {
1217 let (source, pages) = deep(2, 500);
1218 let first = title(&pages[0].1).text.text().to_owned();
1219 let runs = (0..20).map(|n| toggling(&pages[0], n));
1220 let mac = sealed(
1221 source.clone(),
1222 "Mac",
1223 runs.chain([vec![typing_title(&pages[0], "Mac: ")]]),
1224 );
1225 assert!(depth(&mac, pages[0].0) < 20);
1226 let [_, iphone] = diverged(
1227 &source,
1228 &[Vec::new(), body(&pages[0], "Typed on the iPhone.")],
1229 );
1230 for winner in 0..2 {
1231 let image = merged(&[mac.clone(), iphone.clone()], winner);
1232 let (listed, conflicts) = texts_of(&image);
1233 assert!(conflicts.is_empty(), "{winner}: {conflicts}");
1234 assert_eq!(
1235 listed,
1236 format!("Mac: {first}\nTyped on the iPhone.\nPage 1\n"),
1237 "{winner}"
1238 );
1239 }
1240}
1241
1242/// A version whose page the file holds merged, then taken past the chain cap, merges from
1243/// that merge: what the file already merged is not replayed.
1244#[test]
1245fn a_page_merged_then_past_the_cap_merges_as_edits() {
1246 let (source, pages) = deep(2, 490);
1247 let first = title(&pages[0].1).text.text().to_owned();
1248 let iphone = sealed(
1249 source.clone(),
1250 "iPhone",
1251 [body(&pages[0], "A from the iPhone.")],
1252 );
1253 let mac = sealed(source, "Mac", [vec![typing_title(&pages[1], "Mac: ")]]);
1254 let mac = merged(&[mac, iphone.clone()], 0);
1255 let page = reread(&iphone, &pages[..1]).remove(0);
1256 let runs = (0..30).map(|n| toggling(&page, n));
1257 let iphone = sealed(
1258 iphone,
1259 "iPhone",
1260 runs.chain([vec![typing_title(&page, "iPhone: ")]]),
1261 );
1262 assert!(depth(&iphone, page.0) < 30);
1263 for winner in 0..2 {
1264 let image = merged(&[mac.clone(), iphone.clone()], winner);
1265 let (listed, conflicts) = texts_of(&image);
1266 assert!(conflicts.is_empty(), "{winner}: {conflicts}");
1267 assert_eq!(
1268 listed,
1269 format!("iPhone: {first}\nA from the iPhone.\nMac: Page 1\n"),
1270 "{winner}"
1271 );
1272 }
1273}
1274
1275/// A page the file deleted, which the version edited with no revision in common, comes back
1276/// as a copy, as a page deleted on one side and edited on the other does.
1277#[test]
1278fn a_page_the_file_deleted_with_no_common_revision_comes_back() {
1279 let (source, pages) = deep(2, 512);
1280 let space = pages[0].0;
1281 let deleting = Op::Section(SectionOp::Delete(vec![space]));
1282 let mac = sealed(
1283 source.clone(),
1284 "Mac",
1285 [vec![typing_title(&pages[0], "Mac: ")], vec![deleting]],
1286 );
1287 let iphone = sealed(source, "iPhone", [body(&pages[0], "Typed on the iPhone.")]);
1288 for copy in [&mac, &iphone] {
1289 assert!(depth(copy, space) < 3);
1290 }
1291 let (listed, conflicts) = texts_of(&merged(&[mac, iphone], 0));
1292 assert_eq!(
1293 listed.matches("Typed on the iPhone.").count(),
1294 1,
1295 "{listed}"
1296 );
1297 assert!(!listed.contains("Mac: "), "{listed}");
1298 assert!(conflicts.is_empty(), "{conflicts}");
1299}
1300
1301/// A page list with no revision in common counts as the file's: the version's new page, with
1302/// its conflict page, and page edits merge, its deletion does not.
1303#[test]
1304fn a_page_list_with_no_common_revision_counts_as_unchanged() {
1305 let (source, pages) = untouched(3);
1306 let root = {
1307 let store = onestore::Store::parse(&source).unwrap();
1308 onestore::RevisionIndex::parse(&store).unwrap().root
1309 };
1310 let moving = |n: usize| {
1311 let (space, before) = if n.is_multiple_of(2) { (1, 0) } else { (0, 1) };
1312 let edit = PageEdit::move_to(pages[space].0, Some(pages[before].0), 1).unwrap();
1313 vec![Op::Section(SectionOp::Pages(vec![edit]))]
1314 };
1315 let runs = 512 - depth(&source, root);
1316 let source = sealed(source, "Fixture", (0..runs).map(moving));
1317 let pages = reread(&source, &pages);
1318 let first = PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap();
1319 let mac = sealed(
1320 source.clone(),
1321 "Mac",
1322 [
1323 vec![Op::Section(SectionOp::Pages(vec![first]))],
1324 vec![typing_title(&pages[0], "Mac: ")],
1325 ],
1326 );
1327 let creation = PageCreation::new(None, Some("From the iPhone"), "iPhone").unwrap();
1328 let made = creation.space();
1329 let iphone = sealed(
1330 source,
1331 "iPhone",
1332 [
1333 vec![Op::Section(SectionOp::Delete(vec![pages[1].0]))],
1334 vec![Op::Section(SectionOp::Create(creation))],
1335 body(&pages[2], "Page 2 from the iPhone."),
1336 ],
1337 );
1338 let page = reread(&iphone, &[(made, pages[0].1.clone())]).remove(0).1;
1339 let conflict = Op::Section(SectionOp::Conflict {
1340 of: made,
1341 creation: PageCreation::new(None, Some("From the iPhone"), "Someone")
1342 .unwrap()
1343 .in_space(new_id().unwrap().guid)
1344 .unwrap(),
1345 page: with_body(&page, "Kept beside it."),
1346 objects: Vec::new(),
1347 });
1348 let iphone = sealed(iphone, "iPhone", [vec![conflict]]);
1349 for copy in [&mac, &iphone] {
1350 assert!(depth(copy, root) < 4);
1351 }
1352 let image = merged(&[mac, iphone], 0);
1353 let arena = onestore::Arena::default();
1354 let mut section = Section::open(&arena, image.clone()).unwrap();
1355 let conflicts = section.conflicts().unwrap();
1356 let [(of, beside)] = &conflicts[..] else {
1357 panic!("{conflicts:?}")
1358 };
1359 assert_eq!((*of, beside.len()), (made, 1));
1360 let beside = bodies(&section.page(beside[0].space).unwrap());
1361 assert!(beside.iter().any(|(_, text)| text == "Kept beside it."));
1362 let (listed, _) = texts_of(&image);
1363 for text in [
1364 "Mac: Page 0",
1365 "Page 1",
1366 "Page 2 from the iPhone.",
1367 "From the iPhone",
1368 ] {
1369 assert_eq!(listed.matches(text).count(), 1, "{text}:\n{listed}");
1370 }
1371}
1372
12101373/// Two devices merge one version onto different states of a page, then their results meet:
12111374/// each merge is its own revision, so neither device's typing is taken as held by the other
12121375/// (sweep seed 600). The version's own typing, merged on both sides, may show twice: the limit