From 378fe60115f1ad1b1dd869b559da99c10ff95463 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Fri, 16 Oct 2026 13:38:33 -0700 Subject: [PATCH] fix(onestore): add a page's first revision metadata in a dependent revision 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.5 --- crates/notebook/tests/cloud.rs | 167 +++++++++++++++++++++++++++++ crates/onestore/src/active.rs | 1 + crates/onestore/src/section.rs | 35 +++++- crates/onestore/src/write.rs | 53 ++++++--- crates/onestore/tests/insertion.rs | 18 +++- crates/onestore/tests/protected.rs | 27 +++++ 6 files changed, 280 insertions(+), 21 deletions(-) diff --git a/crates/notebook/tests/cloud.rs b/crates/notebook/tests/cloud.rs index 66f56983874a64aad83cc2243df1162acb464f84..b473797c5e4002ad982ed7db9db3254fea735b59 100644 --- a/crates/notebook/tests/cloud.rs +++ b/crates/notebook/tests/cloud.rs @@ -872,3 +872,170 @@ 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)>) { + 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 { + let creation = PageCreation::new(None, Some(&format!("Page {n}")), "Fixture").unwrap(); + edit( + &mut section, + "Fixture", + vec![Op::Section(SectionOp::Create(creation))], + ); + } + section.seal().unwrap(); + let pages = section + .pages() + .unwrap() + .into_iter() + .map(|(space, ..)| (space, section.page(space).unwrap())) + .collect(); + (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 { + let directory = tempfile::tempdir().unwrap(); + let mut server = Server { + current: source.to_vec(), + id: 0, + next: 0, + conflicts: Vec::new(), + 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; + 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() +} + +fn title(page: &Page) -> &TextObject { + page.objects + .iter() + .find_map(|object| match object { + PageObject::Title(title) => match &title.outlines[0].paragraphs[0].content { + ParagraphContent::Text(text) => Some(text), + _ => None, + }, + _ => None, + }) + .unwrap() +} + +fn typing_title((space, page): &(ExGuid, Page), with: &str) -> Op { + Op::Page { + space: *space, + op: PageOp::Text { + text: title(page).id, + range: 0..0, + with: with.into(), + }, + } +} + +/// Issue #152: the first edits two devices make to a page merge as edits, not as a version +/// set aside. +#[test] +fn first_edits_on_two_devices_merge_as_edits() { + let (source, pages) = untouched(); + let (space, page) = &pages[0]; + let body = lower_page(page, &with_body(page, "Typed on the iPhone.")) + .unwrap() + .into_iter() + .map(|op| Op::Page { space: *space, op }) + .collect(); + let edits = [vec![typing_title(&pages[0], "Mac: ")], body]; + for winner in 0..2 { + let image = merged_first_edits(&source, &edits, winner); + let (listed, conflicts) = texts_of(&image); + assert!(conflicts.is_empty(), "{winner}: {conflicts}"); + assert!( + listed.starts_with("Mac: Page 0\nTyped on the iPhone.\n"), + "{winner}: {listed}" + ); + } +} + +/// First edits that clash still leave OneNote's conflict page. +#[test] +fn clashing_first_edits_leave_a_conflict_page() { + let (source, pages) = untouched(); + 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 arena = onestore::Arena::default(); + let mut section = Section::open(&arena, image).unwrap(); + let conflicts = section.conflicts().unwrap(); + let [(of, conflict)] = &conflicts[..] else { + panic!("{winner}: {conflicts:?}") + }; + assert_eq!((*of, conflict.len()), (pages[0].0, 1)); + for (space, with) in [(*of, typed[winner]), (conflict[0].space, typed[1 - winner])] { + let page = section.page(space).unwrap(); + assert_eq!(title(&page).text.text(), format!("{with}Page 0")); + } + } +} + +/// 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 reordering = vec![ + Op::Section(SectionOp::Pages(vec![ + PageEdit::move_to(pages[2].0, Some(pages[0].0), 1).unwrap(), + ])), + Op::Section(SectionOp::Delete(vec![pages[1].0])), + ]; + let edits = [vec![typing_title(&pages[0], "Mac: ")], reordering]; + for winner in 0..2 { + let image = merged_first_edits(&source, &edits, 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(); + assert_eq!(titles, ["Page 2", "Mac: Page 0"], "{winner}"); + assert!(section.conflicts().unwrap().is_empty(), "{winner}"); + } +} diff --git a/crates/onestore/src/active.rs b/crates/onestore/src/active.rs index 7dc372abe002e6034a37cbe07dc4140d076fd8b4..5c3e79ac4a5b1978411e5c8803fd0b958b3e08a7 100644 --- a/crates/onestore/src/active.rs +++ b/crates/onestore/src/active.rs @@ -178,6 +178,7 @@ impl<'a> ActivePage<'a> { checkpoint, changed, touched, + .. } = self.live.commit(objects)?; if checkpoint { let all: Vec = self.live.revision.objects.keys().copied().collect(); diff --git a/crates/onestore/src/section.rs b/crates/onestore/src/section.rs index b06485f4a1f924df40dad8058b50c01748779515..4944351a255a5fffe4e362040eccdbb3ad9d7843 100644 --- a/crates/onestore/src/section.rs +++ b/crates/onestore/src/section.rs @@ -1130,12 +1130,8 @@ fn changed<'a>( }; objects.push((object_id, declared(change.jcid, bytes, change.global_ids)?)); } - if file.revision.roots != page.revision.roots { - file.depth = 0; - file.revision.roots.clone_from(&page.revision.roots); - } Ok(Some(Revised { - commit: file.commit(objects)?, + commit: file.commit_as(objects, page.revision.roots.clone())?, replaced, created, })) @@ -1584,6 +1580,35 @@ mod tests { } } + #[test] + fn a_seal_refuses_a_root_it_did_not_add() { + fn root(transaction: &mut Transaction) { + let header = 0x8000705a_u32.to_le_bytes(); + let at = transaction + .append + .windows(4) + .position(|w| w == header) + .unwrap(); + transaction.append[at + 4] ^= 1; + } + let source = crate::create_section("root.one", "Original", "Author").unwrap(); + let space = page_spaces(&source)[0]; + let seal = |tamper: Option| { + let arena = Arena::default(); + let mut section = Section::open(&arena, source.clone()).unwrap(); + section.attribute(space, "Author", Vec::new()).unwrap(); + TAMPER.set(tamper); + let sealed = section.seal(); + TAMPER.set(None); + sealed.err().map(|error| error.message) + }; + assert_eq!(seal(None), None); + assert_eq!( + seal(Some(root)), + Some("A revision declares other objects or roots than it changed") + ); + } + #[test] fn a_replayed_transaction_leaves_the_sealing_section() { let source = include_bytes!("../../../corpus/m6/native-features-01/notebook/Features.one"); diff --git a/crates/onestore/src/write.rs b/crates/onestore/src/write.rs index 600be7d786bd5b5ab5f5a1cd936b3a523d1a2bf4..7c813efe3ff6085659615658757c9c7e123bbb3b 100644 --- a/crates/onestore/src/write.rs +++ b/crates/onestore/src/write.rs @@ -1014,6 +1014,27 @@ impl<'a> LiveRevision<'a> { &mut self, replacements: Vec<(ExGuid, crate::Object<'a>)>, ) -> Result { + self.commit_as(replacements, self.revision.roots.clone()) + } + + /// `commit`, the revision taking `roots`: a dependent revision declares the roots it adds + /// or moves and inherits the rest, so dropping one takes a checkpoint. + pub(crate) fn commit_as( + &mut self, + replacements: Vec<(ExGuid, crate::Object<'a>)>, + roots: BTreeMap, + ) -> Result { + let dropped = self + .revision + .roots + .keys() + .any(|role| !roots.contains_key(role)); + let declared: BTreeMap = roots + .iter() + .filter(|(role, id)| self.revision.roots.get(role) != Some(id)) + .map(|(role, id)| (*role, *id)) + .collect(); + self.revision.roots = roots; let mut changed = BTreeSet::new(); // Whether each object whose count may move was reachable before. let mut touched = BTreeMap::new(); @@ -1087,10 +1108,15 @@ impl<'a> LiveRevision<'a> { } } // Native cold-open fails on long dependency chains; cap their depth at 512. - let checkpoint = self.depth == 0 || self.depth >= 512; + let checkpoint = self.depth == 0 || self.depth >= 512 || dropped; self.depth = if checkpoint { 1 } else { self.depth + 1 }; Ok(Commit { checkpoint, + roots: if checkpoint { + self.revision.roots.clone() + } else { + declared + }, changed, touched: touched.into_keys().collect(), }) @@ -1226,6 +1252,8 @@ pub(crate) fn differing<'s, T: PartialEq>(before: &'s [T], after: &'s [T]) -> (& pub(crate) struct Commit { /// Whether the revision declares every object, depending on none. pub checkpoint: bool, + /// The roots it declares: every root of a checkpoint, else those it adds or moves. + pub roots: BTreeMap, /// The objects it declares otherwise: the replacements and those whose counts moved. pub changed: BTreeSet, /// Objects whose reachability or reference count may have moved. @@ -2058,17 +2086,15 @@ impl Appending { manifest.extend(group); } } - if checkpoint { - for (role, id) in &revision.roots { - let mut payload = Vec::new(); - if is_section { - id.encode(&mut payload); - } else { - payload.extend_from_slice(&compact(*id, toc_table.as_ref().unwrap())?); - } - payload.extend_from_slice(&role.to_le_bytes()); - manifest.push(node(if is_section { 0x5a } else { 0x59 }, None, &payload)?); + for (role, id) in &commit.roots { + let mut payload = Vec::new(); + if is_section { + id.encode(&mut payload); + } else { + payload.extend_from_slice(&compact(*id, toc_table.as_ref().unwrap())?); } + payload.extend_from_slice(&role.to_le_bytes()); + manifest.push(node(if is_section { 0x5a } else { 0x59 }, None, &payload)?); } manifest.push(node(0x1c, None, &[])?); Ok((new_rid, stored)) @@ -2630,9 +2656,12 @@ pub(crate) fn check_transaction( } else { commit.changed.clone() }; + // Checked against the revision itself, not the roots the manifest was written from. if declared != expected + || roots + .iter() + .any(|(role, id)| live.revision.roots.get(role) != Some(id)) || (commit.checkpoint && roots != live.revision.roots) - || (!commit.checkpoint && !roots.is_empty()) { return Err(wrong( "A revision declares other objects or roots than it changed", diff --git a/crates/onestore/tests/insertion.rs b/crates/onestore/tests/insertion.rs index e3ab5406e0fafb25759b9cc9fc495120bd2713b4..d76dfe004ba82c09223985745bea0beda7980a9f 100644 --- a/crates/onestore/tests/insertion.rs +++ b/crates/onestore/tests/insertion.rs @@ -417,8 +417,10 @@ fn paragraph_and_outline_insertions_publish_metadata_and_references_together() { } } +/// A page's first revision metadata depends on the revision before it and names only its +/// new root, as OneNote 2010 typing on a Snowbound page does (`corpus/link-edit/native-typed`). #[test] -fn first_revision_metadata_checkpoints_once_and_keeps_history() { +fn first_revision_metadata_extends_the_chain_and_keeps_history() { let source = onestore::create_section("metadata.one", "Original", "Same author").unwrap(); let (sid, _, _, _, text) = targets(&source); let store = Store::parse(&source).unwrap(); @@ -428,8 +430,8 @@ fn first_revision_metadata_checkpoints_once_and_keeps_history() { let arena = onestore::Arena::default(); let mut section = onestore::Section::open(&arena, source.clone()).unwrap(); let at = 134_000_000_000_012_345; - let mut previous = None; - for at in [at, at + 1] { + let mut previous = Some(index.active(sid).unwrap()); + for (at, roles) in [(at, &[4][..]), (at + 1, &[])] { section .apply( "Same author", @@ -452,7 +454,15 @@ fn first_revision_metadata_checkpoints_once_and_keeps_history() { let current = RevisionIndex::parse(¤t_store).unwrap(); current.validate_current().unwrap(); let rid = current.active(sid).unwrap(); - assert_eq!(current.spaces[&sid].revisions[&rid].dependency, previous); + let stored = ¤t.spaces[&sid].revisions[&rid]; + assert_eq!(stored.dependency, previous); + let declared: Vec = stored + .nodes + .iter() + .filter(|node| node.id == 0x5a) + .map(|node| u32::from_le_bytes(node.payload[20..].try_into().unwrap())) + .collect(); + assert_eq!(declared, roles); let revision = current.resolve_active(sid).unwrap(); assert_eq!(metadata::check(&original, &revision, "Same author"), at); assert!( diff --git a/crates/onestore/tests/protected.rs b/crates/onestore/tests/protected.rs index 597d45df2713783de91b385599da2003fd105ed4..79fc39e45fd4b9e778053cb2eaa34f84b03cf0cf 100644 --- a/crates/onestore/tests/protected.rs +++ b/crates/onestore/tests/protected.rs @@ -247,6 +247,33 @@ fn a_protected_page_edit_is_stored_under_the_section_key() { } } +/// The first edit of a page Snowbound made adds its metadata root in a dependent revision +/// under the key, which the password alone reads back. +#[test] +fn a_protected_first_edit_declares_its_root_in_a_dependent_revision() { + let plain = onestore::create_section("first.one", "Original", "Author").unwrap(); + let key = Key::new("password").unwrap(); + let mut bytes = rekey(&plain, None, Some(&key)).unwrap(); + let arena = Arena::default(); + let mut section = Section::unlock(&arena, bytes.clone(), &key).unwrap(); + let (space, page) = pages(&mut section).remove(0); + let (edit, after) = edit(space, &page).unwrap(); + section.apply("Rust", &edit).unwrap(); + let transaction = section.seal().unwrap().unwrap(); + transaction.apply(&mut bytes).unwrap(); + let store = Store::parse(&bytes).unwrap(); + let index = RevisionIndex::parse(&store).unwrap(); + let revision = &index.spaces[&space].revisions[&index.active(space).unwrap()]; + assert!(revision.encrypted && revision.dependency.is_some()); + assert!(revision.nodes.iter().any(|node| node.id == 0x5a)); + let unlocked = UnlockedSection::open(&index, "password", Limits::default()).unwrap(); + let document = unlocked.document().unwrap(); + assert_eq!( + Page::from_space(&document, space).unwrap().objects, + after.objects + ); +} + /// OneNote's revisions that depend on an earlier one carry no key node of their own. #[test] fn a_native_revision_inherits_the_key_of_its_dependency() { -- 2.54.1