| author | |
| committer | |
| log | 378fe60115f1ad1b1dd869b559da99c10ff95463 |
| tree | ba62aff7f4186974d5fc8fa2a7e3c2579fe959a0 |
| parent | dadc315e819127b38aa65eaf0c7f975207225fc3 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
A page Snowbound created has no revision metadata root until its first edit,
and the writer stored that edit as a checkpoint with no dependency. Two devices
that each made a first edit to the page before syncing then shared no revision
of it, so merging the conflict version (iCloud, or a provider's lost upload)
failed and the whole version was set aside beside the section: edits that did
not clash were not merged, and its page moves and deletions were not applied.
The edit now depends on the revision before it and declares only the root it
adds, as MS-ONESTORE 2.1.9 allows and as OneNote 2010 writes when typing on a
Snowbound page (corpus/link-edit/native-typed). Only a revision that drops a
root is still a checkpoint. OneNote 2010 cold-opens merged sections written
this way with the merged text, page order and deletions.
- First edits to the same page on two devices now merge when they sync, rather than one device's copy of the section being set aside.
Fixes #152
Assisted-by: claude-opus-5.56 files changed, 280 insertions(+), 21 deletions(-)
crates/notebook/tests/cloud.rs+167| ... | ... | @@ -872,3 +872,170 @@ fn report(token: &str, label: &str, server: &Server, devices: &[Device]) { |
| 872 | 872 | } |
| 873 | 873 | eprintln!("{line}"); |
| 874 | 874 | } |
| 875 | ||
| 876 | /// A section as Snowbound makes it, three titled pages no one has edited yet, and its pages. | |
| 877 | fn untouched() -> (Vec<u8>, Vec<(ExGuid, Page)>) { | |
| 878 | let source = onestore::create_empty_section("first.one", None).unwrap(); | |
| 879 | let arena = onestore::Arena::default(); | |
| 880 | let mut section = Section::open(&arena, source).unwrap(); | |
| 881 | for n in 0..3 { | |
| 882 | let creation = PageCreation::new(None, Some(&format!("Page {n}")), "Fixture").unwrap(); | |
| 883 | edit( | |
| 884 | &mut section, | |
| 885 | "Fixture", | |
| 886 | vec![Op::Section(SectionOp::Create(creation))], | |
| 887 | ); | |
| 888 | } | |
| 889 | section.seal().unwrap(); | |
| 890 | let pages = section | |
| 891 | .pages() | |
| 892 | .unwrap() | |
| 893 | .into_iter() | |
| 894 | .map(|(space, ..)| (space, section.page(space).unwrap())) | |
| 895 | .collect(); | |
| 896 | (section.image(), pages) | |
| 897 | } | |
| 898 | ||
| 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. | |
| 901 | fn merged_first_edits(source: &[u8], edits: &[Vec<Op>; 2], winner: usize) -> Vec<u8> { | |
| 902 | let directory = tempfile::tempdir().unwrap(); | |
| 903 | let mut server = Server { | |
| 904 | current: source.to_vec(), | |
| 905 | id: 0, | |
| 906 | next: 0, | |
| 907 | conflicts: Vec::new(), | |
| 908 | kept: 0, | |
| 909 | }; | |
| 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; | |
| 941 | device.deliver(&server); | |
| 942 | device.sync(&mut server); | |
| 943 | assert!(server.conflicts.is_empty()); | |
| 944 | assert_eq!(server.kept, 0, "the version was set aside whole"); | |
| 945 | device.copy.bytes.clone() | |
| 946 | } | |
| 947 | ||
| 948 | fn title(page: &Page) -> &TextObject { | |
| 949 | page.objects | |
| 950 | .iter() | |
| 951 | .find_map(|object| match object { | |
| 952 | PageObject::Title(title) => match &title.outlines[0].paragraphs[0].content { | |
| 953 | ParagraphContent::Text(text) => Some(text), | |
| 954 | _ => None, | |
| 955 | }, | |
| 956 | _ => None, | |
| 957 | }) | |
| 958 | .unwrap() | |
| 959 | } | |
| 960 | ||
| 961 | fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op { | |
| 962 | Op::Page { | |
| 963 | space: *space, | |
| 964 | op: PageOp::Text { | |
| 965 | text: title(page).id, | |
| 966 | range: 0..0, | |
| 967 | with: with.into(), | |
| 968 | }, | |
| 969 | } | |
| 970 | } | |
| 971 | ||
| 972 | /// Issue #152: the first edits two devices make to a page merge as edits, not as a version | |
| 973 | /// set aside. | |
| 974 | #[test] | |
| 975 | fn first_edits_on_two_devices_merge_as_edits() { | |
| 976 | let (source, pages) = untouched(); | |
| 977 | let (space, page) = &pages[0]; | |
| 978 | let body = lower_page(page, &with_body(page, "Typed on the iPhone.")) | |
| 979 | .unwrap() | |
| 980 | .into_iter() | |
| 981 | .map(|op| Op::Page { space: *space, op }) | |
| 982 | .collect(); | |
| 983 | let edits = [vec![typing_title(&pages[0], "Mac: ")], body]; | |
| 984 | for winner in 0..2 { | |
| 985 | let image = merged_first_edits(&source, &edits, winner); | |
| 986 | let (listed, conflicts) = texts_of(&image); | |
| 987 | assert!(conflicts.is_empty(), "{winner}: {conflicts}"); | |
| 988 | assert!( | |
| 989 | listed.starts_with("Mac: Page 0\nTyped on the iPhone.\n"), | |
| 990 | "{winner}: {listed}" | |
| 991 | ); | |
| 992 | } | |
| 993 | } | |
| 994 | ||
| 995 | /// First edits that clash still leave OneNote's conflict page. | |
| 996 | #[test] | |
| 997 | fn clashing_first_edits_leave_a_conflict_page() { | |
| 998 | let (source, pages) = untouched(); | |
| 999 | let typed = ["Mac: ", "iPhone: "]; | |
| 1000 | let edits = typed.map(|with| vec![typing_title(&pages[0], with)]); | |
| 1001 | for winner in 0..2 { | |
| 1002 | let image = merged_first_edits(&source, &edits, winner); | |
| 1003 | let arena = onestore::Arena::default(); | |
| 1004 | let mut section = Section::open(&arena, image).unwrap(); | |
| 1005 | let conflicts = section.conflicts().unwrap(); | |
| 1006 | let [(of, conflict)] = &conflicts[..] else { | |
| 1007 | panic!("{winner}: {conflicts:?}") | |
| 1008 | }; | |
| 1009 | assert_eq!((*of, conflict.len()), (pages[0].0, 1)); | |
| 1010 | for (space, with) in [(*of, typed[winner]), (conflict[0].space, typed[1 - winner])] { | |
| 1011 | let page = section.page(space).unwrap(); | |
| 1012 | assert_eq!(title(&page).text.text(), format!("{with}Page 0")); | |
| 1013 | } | |
| 1014 | } | |
| 1015 | } | |
| 1016 | ||
| 1017 | /// Pages a version moved and deleted stay so beside another device's first edit. | |
| 1018 | #[test] | |
| 1019 | fn page_moves_and_deletions_survive_a_first_edit_elsewhere() { | |
| 1020 | let (source, pages) = untouched(); | |
| 1021 | let reordering = vec![ | |
| 1022 | Op::Section(SectionOp::Pages(vec![ | |
| 1023 | PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(), | |
| 1024 | ])), | |
| 1025 | Op::Section(SectionOp::Delete(vec![pages[1].0])), | |
| 1026 | ]; | |
| 1027 | let edits = [vec![typing_title(&pages[0], "Mac: ")], reordering]; | |
| 1028 | for winner in 0..2 { | |
| 1029 | let image = merged_first_edits(&source, &edits, winner); | |
| 1030 | let arena = onestore::Arena::default(); | |
| 1031 | let mut section = Section::open(&arena, image).unwrap(); | |
| 1032 | let titles: Vec<String> = section | |
| 1033 | .pages() | |
| 1034 | .unwrap() | |
| 1035 | .into_iter() | |
| 1036 | .map(|(_, title, _)| title) | |
| 1037 | .collect(); | |
| 1038 | assert_eq!(titles, ["Page 2", "Mac: Page 0"], "{winner}"); | |
| 1039 | assert!(section.conflicts().unwrap().is_empty(), "{winner}"); | |
| 1040 | } | |
| 1041 | } |
crates/onestore/src/active.rs+1| ... | ... | @@ -178,6 +178,7 @@ impl<'a> ActivePage<'a> { |
| 178 | 178 | checkpoint, |
| 179 | 179 | changed, |
| 180 | 180 | touched, |
| 181 | .. | |
| 181 | 182 | } = self.live.commit(objects)?; |
| 182 | 183 | if checkpoint { |
| 183 | 184 | let all: Vec<ExGuid> = self.live.revision.objects.keys().copied().collect(); |
crates/onestore/src/section.rs+30-5| ... | ... | @@ -1130,12 +1130,8 @@ fn changed<'a>( |
| 1130 | 1130 | }; |
| 1131 | 1131 | objects.push((object_id, declared(change.jcid, bytes, change.global_ids)?)); |
| 1132 | 1132 | } |
| 1133 | if file.revision.roots != page.revision.roots { | |
| 1134 | file.depth = 0; | |
| 1135 | file.revision.roots.clone_from(&page.revision.roots); | |
| 1136 | } | |
| 1137 | 1133 | Ok(Some(Revised { |
| 1138 | commit: file.commit(objects)?, | |
| 1134 | commit: file.commit_as(objects, page.revision.roots.clone())?, | |
| 1139 | 1135 | replaced, |
| 1140 | 1136 | created, |
| 1141 | 1137 | })) |
| ... | ... | @@ -1584,6 +1580,35 @@ mod tests { |
| 1584 | 1580 | } |
| 1585 | 1581 | } |
| 1586 | 1582 | |
| 1583 | #[test] | |
| 1584 | fn a_seal_refuses_a_root_it_did_not_add() { | |
| 1585 | fn root(transaction: &mut Transaction) { | |
| 1586 | let header = 0x8000705a_u32.to_le_bytes(); | |
| 1587 | let at = transaction | |
| 1588 | .append | |
| 1589 | .windows(4) | |
| 1590 | .position(|w| w == header) | |
| 1591 | .unwrap(); | |
| 1592 | transaction.append[at + 4] ^= 1; | |
| 1593 | } | |
| 1594 | let source = crate::create_section("root.one", "Original", "Author").unwrap(); | |
| 1595 | let space = page_spaces(&source)[0]; | |
| 1596 | let seal = |tamper: Option<fn(&mut Transaction)>| { | |
| 1597 | let arena = Arena::default(); | |
| 1598 | let mut section = Section::open(&arena, source.clone()).unwrap(); | |
| 1599 | section.attribute(space, "Author", Vec::new()).unwrap(); | |
| 1600 | TAMPER.set(tamper); | |
| 1601 | let sealed = section.seal(); | |
| 1602 | TAMPER.set(None); | |
| 1603 | sealed.err().map(|error| error.message) | |
| 1604 | }; | |
| 1605 | assert_eq!(seal(None), None); | |
| 1606 | assert_eq!( | |
| 1607 | seal(Some(root)), | |
| 1608 | Some("A revision declares other objects or roots than it changed") | |
| 1609 | ); | |
| 1610 | } | |
| 1611 | ||
| 1587 | 1612 | #[test] |
| 1588 | 1613 | fn a_replayed_transaction_leaves_the_sealing_section() { |
| 1589 | 1614 | let source = include_bytes!("../../../corpus/m6/native-features-01/notebook/Features.one"); |
crates/onestore/src/write.rs+41-12| ... | ... | @@ -1014,6 +1014,27 @@ impl<'a> LiveRevision<'a> { |
| 1014 | 1014 | &mut self, |
| 1015 | 1015 | replacements: Vec<(ExGuid, crate::Object<'a>)>, |
| 1016 | 1016 | ) -> Result<Commit> { |
| 1017 | self.commit_as(replacements, self.revision.roots.clone()) | |
| 1018 | } | |
| 1019 | ||
| 1020 | /// `commit`, the revision taking `roots`: a dependent revision declares the roots it adds | |
| 1021 | /// or moves and inherits the rest, so dropping one takes a checkpoint. | |
| 1022 | pub(crate) fn commit_as( | |
| 1023 | &mut self, | |
| 1024 | replacements: Vec<(ExGuid, crate::Object<'a>)>, | |
| 1025 | roots: BTreeMap<u32, ExGuid>, | |
| 1026 | ) -> Result<Commit> { | |
| 1027 | let dropped = self | |
| 1028 | .revision | |
| 1029 | .roots | |
| 1030 | .keys() | |
| 1031 | .any(|role| !roots.contains_key(role)); | |
| 1032 | let declared: BTreeMap<u32, ExGuid> = roots | |
| 1033 | .iter() | |
| 1034 | .filter(|(role, id)| self.revision.roots.get(role) != Some(id)) | |
| 1035 | .map(|(role, id)| (*role, *id)) | |
| 1036 | .collect(); | |
| 1037 | self.revision.roots = roots; | |
| 1017 | 1038 | let mut changed = BTreeSet::new(); |
| 1018 | 1039 | // Whether each object whose count may move was reachable before. |
| 1019 | 1040 | let mut touched = BTreeMap::new(); |
| ... | ... | @@ -1087,10 +1108,15 @@ impl<'a> LiveRevision<'a> { |
| 1087 | 1108 | } |
| 1088 | 1109 | } |
| 1089 | 1110 | // Native cold-open fails on long dependency chains; cap their depth at 512. |
| 1090 | let checkpoint = self.depth == 0 || self.depth >= 512; | |
| 1111 | let checkpoint = self.depth == 0 || self.depth >= 512 || dropped; | |
| 1091 | 1112 | self.depth = if checkpoint { 1 } else { self.depth + 1 }; |
| 1092 | 1113 | Ok(Commit { |
| 1093 | 1114 | checkpoint, |
| 1115 | roots: if checkpoint { | |
| 1116 | self.revision.roots.clone() | |
| 1117 | } else { | |
| 1118 | declared | |
| 1119 | }, | |
| 1094 | 1120 | changed, |
| 1095 | 1121 | touched: touched.into_keys().collect(), |
| 1096 | 1122 | }) |
| ... | ... | @@ -1226,6 +1252,8 @@ pub(crate) fn differing<'s, T: PartialEq>(before: &'s [T], after: &'s [T]) -> (& |
| 1226 | 1252 | pub(crate) struct Commit { |
| 1227 | 1253 | /// Whether the revision declares every object, depending on none. |
| 1228 | 1254 | pub checkpoint: bool, |
| 1255 | /// The roots it declares: every root of a checkpoint, else those it adds or moves. | |
| 1256 | pub roots: BTreeMap<u32, ExGuid>, | |
| 1229 | 1257 | /// The objects it declares otherwise: the replacements and those whose counts moved. |
| 1230 | 1258 | pub changed: BTreeSet<ExGuid>, |
| 1231 | 1259 | /// Objects whose reachability or reference count may have moved. |
| ... | ... | @@ -2058,17 +2086,15 @@ impl Appending { |
| 2058 | 2086 | manifest.extend(group); |
| 2059 | 2087 | } |
| 2060 | 2088 | } |
| 2061 | if checkpoint { | |
| 2062 | for (role, id) in &revision.roots { | |
| 2063 | let mut payload = Vec::new(); | |
| 2064 | if is_section { | |
| 2065 | id.encode(&mut payload); | |
| 2066 | } else { | |
| 2067 | payload.extend_from_slice(&compact(*id, toc_table.as_ref().unwrap())?); | |
| 2068 | } | |
| 2069 | payload.extend_from_slice(&role.to_le_bytes()); | |
| 2070 | manifest.push(node(if is_section { 0x5a } else { 0x59 }, None, &payload)?); | |
| 2089 | for (role, id) in &commit.roots { | |
| 2090 | let mut payload = Vec::new(); | |
| 2091 | if is_section { | |
| 2092 | id.encode(&mut payload); | |
| 2093 | } else { | |
| 2094 | payload.extend_from_slice(&compact(*id, toc_table.as_ref().unwrap())?); | |
| 2071 | 2095 | } |
| 2096 | payload.extend_from_slice(&role.to_le_bytes()); | |
| 2097 | manifest.push(node(if is_section { 0x5a } else { 0x59 }, None, &payload)?); | |
| 2072 | 2098 | } |
| 2073 | 2099 | manifest.push(node(0x1c, None, &[])?); |
| 2074 | 2100 | Ok((new_rid, stored)) |
| ... | ... | @@ -2630,9 +2656,12 @@ pub(crate) fn check_transaction( |
| 2630 | 2656 | } else { |
| 2631 | 2657 | commit.changed.clone() |
| 2632 | 2658 | }; |
| 2659 | // Checked against the revision itself, not the roots the manifest was written from. | |
| 2633 | 2660 | if declared != expected |
| 2661 | || roots | |
| 2662 | .iter() | |
| 2663 | .any(|(role, id)| live.revision.roots.get(role) != Some(id)) | |
| 2634 | 2664 | || (commit.checkpoint && roots != live.revision.roots) |
| 2635 | || (!commit.checkpoint && !roots.is_empty()) | |
| 2636 | 2665 | { |
| 2637 | 2666 | return Err(wrong( |
| 2638 | 2667 | "A revision declares other objects or roots than it changed", |
crates/onestore/tests/insertion.rs+14-4| ... | ... | @@ -417,8 +417,10 @@ fn paragraph_and_outline_insertions_publish_metadata_and_references_together() { |
| 417 | 417 | } |
| 418 | 418 | } |
| 419 | 419 | |
| 420 | /// A page's first revision metadata depends on the revision before it and names only its | |
| 421 | /// new root, as OneNote 2010 typing on a Snowbound page does (`corpus/link-edit/native-typed`). | |
| 420 | 422 | #[test] |
| 421 | fn first_revision_metadata_checkpoints_once_and_keeps_history() { | |
| 423 | fn first_revision_metadata_extends_the_chain_and_keeps_history() { | |
| 422 | 424 | let source = onestore::create_section("metadata.one", "Original", "Same author").unwrap(); |
| 423 | 425 | let (sid, _, _, _, text) = targets(&source); |
| 424 | 426 | let store = Store::parse(&source).unwrap(); |
| ... | ... | @@ -428,8 +430,8 @@ fn first_revision_metadata_checkpoints_once_and_keeps_history() { |
| 428 | 430 | let arena = onestore::Arena::default(); |
| 429 | 431 | let mut section = onestore::Section::open(&arena, source.clone()).unwrap(); |
| 430 | 432 | let at = 134_000_000_000_012_345; |
| 431 | let mut previous = None; | |
| 432 | for at in [at, at + 1] { | |
| 433 | let mut previous = Some(index.active(sid).unwrap()); | |
| 434 | for (at, roles) in [(at, &[4][..]), (at + 1, &[])] { | |
| 433 | 435 | section |
| 434 | 436 | .apply( |
| 435 | 437 | "Same author", |
| ... | ... | @@ -452,7 +454,15 @@ fn first_revision_metadata_checkpoints_once_and_keeps_history() { |
| 452 | 454 | let current = RevisionIndex::parse(&current_store).unwrap(); |
| 453 | 455 | current.validate_current().unwrap(); |
| 454 | 456 | let rid = current.active(sid).unwrap(); |
| 455 | assert_eq!(current.spaces[&sid].revisions[&rid].dependency, previous); | |
| 457 | let stored = &current.spaces[&sid].revisions[&rid]; | |
| 458 | assert_eq!(stored.dependency, previous); | |
| 459 | let declared: Vec<u32> = stored | |
| 460 | .nodes | |
| 461 | .iter() | |
| 462 | .filter(|node| node.id == 0x5a) | |
| 463 | .map(|node| u32::from_le_bytes(node.payload[20..].try_into().unwrap())) | |
| 464 | .collect(); | |
| 465 | assert_eq!(declared, roles); | |
| 456 | 466 | let revision = current.resolve_active(sid).unwrap(); |
| 457 | 467 | assert_eq!(metadata::check(&original, &revision, "Same author"), at); |
| 458 | 468 | assert!( |
crates/onestore/tests/protected.rs+27| ... | ... | @@ -247,6 +247,33 @@ fn a_protected_page_edit_is_stored_under_the_section_key() { |
| 247 | 247 | } |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | /// The first edit of a page Snowbound made adds its metadata root in a dependent revision | |
| 251 | /// under the key, which the password alone reads back. | |
| 252 | #[test] | |
| 253 | fn a_protected_first_edit_declares_its_root_in_a_dependent_revision() { | |
| 254 | let plain = onestore::create_section("first.one", "Original", "Author").unwrap(); | |
| 255 | let key = Key::new("password").unwrap(); | |
| 256 | let mut bytes = rekey(&plain, None, Some(&key)).unwrap(); | |
| 257 | let arena = Arena::default(); | |
| 258 | let mut section = Section::unlock(&arena, bytes.clone(), &key).unwrap(); | |
| 259 | let (space, page) = pages(&mut section).remove(0); | |
| 260 | let (edit, after) = edit(space, &page).unwrap(); | |
| 261 | section.apply("Rust", &edit).unwrap(); | |
| 262 | let transaction = section.seal().unwrap().unwrap(); | |
| 263 | transaction.apply(&mut bytes).unwrap(); | |
| 264 | let store = Store::parse(&bytes).unwrap(); | |
| 265 | let index = RevisionIndex::parse(&store).unwrap(); | |
| 266 | let revision = &index.spaces[&space].revisions[&index.active(space).unwrap()]; | |
| 267 | assert!(revision.encrypted && revision.dependency.is_some()); | |
| 268 | assert!(revision.nodes.iter().any(|node| node.id == 0x5a)); | |
| 269 | let unlocked = UnlockedSection::open(&index, "password", Limits::default()).unwrap(); | |
| 270 | let document = unlocked.document().unwrap(); | |
| 271 | assert_eq!( | |
| 272 | Page::from_space(&document, space).unwrap().objects, | |
| 273 | after.objects | |
| 274 | ); | |
| 275 | } | |
| 276 | ||
| 250 | 277 | /// OneNote's revisions that depend on an earlier one carry no key node of their own. |
| 251 | 278 | #[test] |
| 252 | 279 | fn a_native_revision_inherits_the_key_of_its_dependency() { |