From 650ea9ccbc132590b96c6c067d62f2dc753117b8 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 17:14:20 -0700 Subject: [PATCH] fix(live): a protected section's uncertain commit is sent again too A protected section publishes its sealed transaction whole, as the host cannot read its edits, so an attempt made uncertain by a dropped connection held the section until a review nothing in the app offers. A Live Share host commits such a transaction only on the image it was made on, so the same transaction sent again lands at most once, and the guest now sends it again like a batch. The host now names a commit made on an image it has moved past a stale handle, as it does a batch whose base it dropped; the guest then reads the section, takes the commit as landed where the section holds its revisions, and otherwise replays its queue on the section, as it can never land. Without that check a commit whose answer was lost after it landed would be replayed on a section where it had since been undone, bringing it back. As the guest has rebased by the time it reports such a refusal, its sync worker keeps the remote and tries again after the short pause it takes when another writer holds the section, rather than waiting for the next check. A test loses a protected commit's request and checks it lands once the guest is back, then loses the answer to one that landed, has the host delete what it typed, and checks that it stays deleted. - Edits to a password-protected section of a Live Share notebook, sent just as the connection dropped, now publish once the guest reconnects. Assisted-by: claude-opus-5.5 --- arc/sync.md | 7 +- crates/notebook/README.md | 6 +- crates/notebook/src/live/share.rs | 2 +- crates/notebook/src/sync.rs | 41 +++++-- crates/notebook/src/worker.rs | 5 +- crates/notebook/tests/live_batch.rs | 161 ++++++++++++++++++++++++++++ 6 files changed, 205 insertions(+), 17 deletions(-) diff --git a/arc/sync.md b/arc/sync.md index 8c9be492de561b6eb293de03b61ef3424d8cc189..73270157715c92e077dec0fb0b7af2104fe8b721 100644 --- a/arc/sync.md +++ b/arc/sync.md @@ -129,8 +129,11 @@ Uncertainty is handled explicitly. A publication attempt is recorded before the first network byte. If the reply is lost, the attempt confirms only when the remote holds its revisions (or every page it changed, as it changed them). Otherwise it waits as `AwaitingConfirmation`. A Live Share host lands a batch at -most once, as it looks for the batch's revisions before applying it, so the next -step sends it the same batch again. Any other remote, where a late write could land +most once, as it looks for the batch's revisions before applying it, and commits a +protected section's sealed transaction only on the image it was made on, so the next +step sends it the same batch again. Refused because the host no longer holds that +image, the batch has landed if the section holds its revisions, and else never will: +the queue replays on the section as it is. Any other remote, where a late write could land a second copy, waits for `release`, which exports a recovery archive first, then republishes or abandons. diff --git a/crates/notebook/README.md b/crates/notebook/README.md index cca453712e71f4c92abb59e39e3d1eb119347eb5..9ad30b18a8e5401ef5e527b6af2f3aefef6ba82a 100644 --- a/crates/notebook/README.md +++ b/crates/notebook/README.md @@ -165,8 +165,10 @@ merges back. Publication attempts are recorded before network I/O. An attempt confirms only when the remote holds its revisions, or every page it changed as it changed them, followed by `Remote::confirm`; otherwise it stays `AwaitingConfirmation`. A remote that -accepts edits (a Live Share host) lands a batch at most once, so the next step sends it the -same batch again; any other remote never gets it again unreviewed. +accepts edits (a Live Share host) lands a batch at most once, a protected section's +transaction included, so the next step sends it the same batch again; one it refuses as +`StaleNetworkFileHandle` is acknowledged where the image holds its revisions, else rebased. +Any other remote never gets it again unreviewed. `release(id, archive, Resolution)` exports the queue first, then publishes the batch again (`Mine`) or abandons every unpublished edit (`Theirs`). A durable `Published` receipt survives reopening. diff --git a/crates/notebook/src/live/share.rs b/crates/notebook/src/live/share.rs index ff981ca97260110ec28bf6cbea7911ce158946ef..c519944f82835da3dc8b428b0f6a33e64ddd2cf1 100644 --- a/crates/notebook/src/live/share.rs +++ b/crates/notebook/src/live/share.rs @@ -777,7 +777,7 @@ impl Served { })?; if Stamp::of(&image).ok().as_ref() != Some(transaction.base()) { return Err(not_committed(io::Error::new( - io::ErrorKind::ResourceBusy, + io::ErrorKind::StaleNetworkFileHandle, "The section changed since", ))); } diff --git a/crates/notebook/src/sync.rs b/crates/notebook/src/sync.rs index e1b22f0168d6f631e6ac7420b764a5d4e9e625ed..efb02997c183e01fff29316c294655281203618d 100644 --- a/crates/notebook/src/sync.rs +++ b/crates/notebook/src/sync.rs @@ -27,9 +27,10 @@ pub trait Remote { fn accepts_edits(&self) -> bool { false } - /// Where the remote accepts edits, a batch published again with the same revisions, - /// however its first attempt ended, lands at most once; one refused as - /// `StaleNetworkFileHandle` never can, as the remote no longer holds its base. + /// Where the remote accepts edits, a batch published again with the same revisions, or + /// a protected section's transaction published again, however its first attempt ended, + /// lands at most once; one refused as `StaleNetworkFileHandle` has landed if the image + /// holds its revisions, and else never can, as the remote no longer holds its base. fn publish_edits( &mut self, transaction: &Transaction, @@ -285,11 +286,9 @@ impl Replica { .await .map_err(Error::RemoteIo)?; let replay = match &state.blocked { - Some(blocked) if self.section.key.is_none() && remote.accepts_edits() => { - working::sealed(&*self.lock()?)? - .filter(|sealed| sealed.batch == blocked.batch) - .and_then(|sealed| sealed.transaction) - } + Some(blocked) if remote.accepts_edits() => working::sealed(&*self.lock()?)? + .filter(|sealed| sealed.batch == blocked.batch) + .and_then(|sealed| sealed.transaction), _ => None, }; if let (Some(blocked), Some(transaction)) = (&state.blocked, replay) { @@ -464,7 +463,8 @@ impl Replica { /// Publishes the attempted `batch` sealed as `transaction`, rebasing on the remote's /// image after where `batched`; `changed` holds the pages a rebase already changed. An /// attempt made `again` stays uncertain until the remote takes it, as the first may - /// still land. A batch whose base the remote refuses for good is rebased onto its image. + /// still land. A batch whose base the remote refuses for good is acknowledged where the + /// remote's image holds it, else rebased onto that image. async fn publish_sealed( &self, remote: &mut impl Remote, @@ -499,11 +499,30 @@ impl Replica { if error.state == CommitState::NotCommitted && error.error.kind() == io::ErrorKind::StaleNetworkFileHandle => { - self.lock()? - .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?; let image = awaited(remote, Remote::read) .await .map_err(Error::RemoteIo)?; + // A whole commit made uncertain may have landed before the one refused. + let revisions = decode_revisions(&self.lock()?.query_row( + "SELECT revisions FROM batches WHERE id=?1", + [batch], + |row| row.get::<_, String>(0), + )?)?; + let store = Store::parse(&image)?; + let index = RevisionIndex::parse(&store)?; + if revisions.iter().all(|(space, revision)| { + (index.spaces.get(space)) + .is_some_and(|space| space.revisions.contains_key(revision)) + }) { + self.acknowledge(batch, Some(&transaction), None)?; + let revision = self.receipt(id)?; + return Ok(Synced { + edit: Some((id, EditStatus::Published { revision })), + changed: self.rebase(Some(image))?, + }); + } + self.lock()? + .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?; self.rebase(Some(image))?; return Err(error.into()); } diff --git a/crates/notebook/src/worker.rs b/crates/notebook/src/worker.rs index 2bff7a5be4c2ef1c270af3f5f53c162eaef6e8a8..a7d1ae8c113e5823fefbe7002d55b65ae6bc7058 100644 --- a/crates/notebook/src/worker.rs +++ b/crates/notebook/src/worker.rs @@ -296,12 +296,15 @@ impl Replica { } Ok(Synced { edit: None, .. }) => contention = 0, Ok(_) => {} + // A commit made on an image the remote moved past was rebased already. Err(Error::Remote(onestore::CommitError { state: onestore::CommitState::NotCommitted, ref error, })) if matches!( error.kind(), - io::ErrorKind::WouldBlock | io::ErrorKind::ResourceBusy + io::ErrorKind::WouldBlock + | io::ErrorKind::ResourceBusy + | io::ErrorKind::StaleNetworkFileHandle ) => { contention = contention.saturating_add(1); diff --git a/crates/notebook/tests/live_batch.rs b/crates/notebook/tests/live_batch.rs index 4e558384bfe1baad9d2da080ecc3a8da823a2452..78618ca46773763ee49c30450a1d5cf539b884a8 100644 --- a/crates/notebook/tests/live_batch.rs +++ b/crates/notebook/tests/live_batch.rs @@ -760,3 +760,164 @@ fn a_lost_batch_whose_base_the_host_dropped_is_rebased() { "BAOriginal text!" ); } + +/// A protected section's commit is sent again as it was, whole: one the relay lost lands +/// once the guest is back, and one whose answer was lost after it landed is found in the +/// host's section rather than landing twice. +#[test] +fn a_protected_section_s_uncertain_commit_lands_once() { + use notebook::{EditStatus, Error, PendingEdit, Remote, live::share::Guest, session::Notebook}; + use onestore::{CommitError, CommitState, ExGuid, Stamp, Transaction}; + use std::collections::BTreeMap; + + /// A remote whose commits land but whose answers are lost. + struct Unanswered(HostedRemote); + impl Remote for Unanswered { + fn read(&mut self) -> std::io::Result> { + self.0.read() + } + fn stamp(&mut self) -> std::io::Result { + self.0.stamp() + } + fn publish(&mut self, transaction: &Transaction) -> Result<(), CommitError> { + self.0.publish(transaction)?; + Err(CommitError { + state: CommitState::Unknown, + error: std::io::ErrorKind::BrokenPipe.into(), + }) + } + fn confirm(&mut self, stamp: &Stamp) -> Result<(), CommitError> { + self.0.confirm(stamp) + } + fn accepts_edits(&self) -> bool { + self.0.accepts_edits() + } + fn publish_edits( + &mut self, + transaction: &Transaction, + _: &[PendingEdit], + _: &BTreeMap, + ) -> Result<(), CommitError> { + self.publish(transaction) + } + } + + let directory = tempfile::tempdir().unwrap(); + let folder = notebook(directory.path()); + let url = relay(Default::default()); + let sharing = Sharing::new("").unwrap(); + let host = host(&folder, &directory.path().join("host"), &sharing, &url); + let welcome = + notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap(); + let relay = PresenceRelay::new(&url, &welcome.room); + let guest = Guest::start( + hello("Grace"), + welcome.share, + welcome.secret, + None, + Some(&relay.url), + || {}, + ) + .unwrap(); + until("the host was never met", || guest.host().is_some()); + let notebook = + Notebook::open_hosted(Arc::clone(&guest), directory.path().join("grace")).unwrap(); + let key = notebook.unlock("Sealed.one", PASSWORD).unwrap(); + let file = folder.join("Sealed.one"); + let original = std::fs::read(&file).unwrap(); + let replica = + Replica::open_or_create(directory.path().join("sealed.sqlite"), Some(&key), || { + Ok(original.clone()) + }) + .unwrap(); + let arena = onestore::Arena::default(); + let mut unlocked = onestore::Section::unlock(&arena, original.clone(), &key).unwrap(); + let (space, ..) = unlocked.pages().unwrap()[0]; + let text = server::page_texts(&unlocked.page(space).unwrap()); + assert_eq!(text, ["Sealed text"]); + let paragraph = unlocked + .page(space) + .unwrap() + .objects + .iter() + .find_map(|object| match object { + onestore::page::PageObject::Outline(outline) => outline + .paragraphs + .iter() + .find_map(|p| p.text().map(|t| t.id)), + _ => None, + }) + .unwrap(); + let typed = |range: std::ops::Range, with: &str| Edit { + at: 134_000_000_000_000_000, + ops: vec![Op::Page { + space, + op: PageOp::Text { + text: paragraph, + range, + with: with.into(), + }, + }], + }; + let stored = || { + let arena = onestore::Arena::default(); + let section = + onestore::Section::unlock(&arena, std::fs::read(&file).unwrap(), &key).unwrap(); + server::page_texts(§ion.page(space).unwrap()) + }; + let published = |id| { + matches!( + replica.status(id).unwrap(), + Some(EditStatus::Published { .. }) + ) + }; + let first = replica.apply("Grace", typed(0..0, "A")).unwrap(); + until("the first edit was never published", || { + let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")); + published(first) + }); + HostedRemote::new(&guest, "Sealed.one").read().unwrap(); + until("the host's stamp was never known without asking", || { + host.touched(&["Sealed.one".into()]); + let before = relay.state.lock().unwrap().access_bytes; + HostedRemote::new(&guest, "Sealed.one").stamp().unwrap(); + relay.state.lock().unwrap().access_bytes == before + }); + + relay.state.lock().unwrap().swallow = true; + let second = replica.apply("Grace", typed(0..0, "B")).unwrap(); + assert!(matches!( + replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")), + Err(Error::Remote(CommitError { + state: CommitState::Unknown, + .. + })) + )); + until("the commit the relay lost never landed", || { + let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")); + published(second) + }); + assert_eq!(stored(), ["BASealed text"]); + + let third = replica.apply("Grace", typed(0..0, "C")).unwrap(); + until("the commit whose answer was lost never landed", || { + replica + .sync_once(&mut Unanswered(HostedRemote::new(&guest, "Sealed.one"))) + .is_err() + }); + assert_eq!(stored(), ["CBASealed text"]); + // The host deletes what the commit typed before the guest hears of it. + let image = std::fs::read(&file).unwrap(); + let arena = onestore::Arena::default(); + let mut section = onestore::Section::unlock(&arena, image.clone(), &key).unwrap(); + section.apply("Ada", &typed(0..1, "")).unwrap(); + let mut image = image; + section.seal().unwrap().unwrap().apply(&mut image).unwrap(); + std::fs::write(&file, image).unwrap(); + host.touched(&["Sealed.one".into()]); + until("the commit that landed was never confirmed", || { + let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Sealed.one")); + published(third) + }); + assert_eq!(stored(), ["BASealed text"]); +} -- 2.54.1