authoravatarclover caruso<git@paperclover.net>2026-10-19 12:43:03-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
log9e5508385b28d6be4fef73d7e6afb795261a690d
tree923105c135538a44b3ebbff46b58561aefe4a019
parentfa9872370dfb4566ea85a9d909497dd2944f868a
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(live): a batch whose base the host dropped is rebased, never held

A Live Share host merges a guest's batch made on an older image only while it still keeps that image, the last 16 at most, and none after it restarts. A batch sent again after an uncertain attempt was refused for want of its base on every try and held its section for good, and a first attempt refused the same way kept being sent unchanged by an app whose sync worker opens a fresh remote after each error. The host now names this refusal apart from passing ones, as a stale handle, and the guest, knowing no copy of the batch can land, rebases its queue onto the host's image, as it does when it comes back from offline: the edits are replayed on the current section, with a conflict page where they cannot merge, and published anew. A test loses a batch's request, restarts the host after it edits the same text, and checks that the batch lands beside the host's edit. - A Live Share guest's edits no longer stay unsent for good after the computer sharing restarts or the section changes many times while they wait. Assisted-by: claude-opus-5.5

4 files changed, 110 insertions(+), 4 deletions(-)

crates/notebook/src/live/share/batch.rs+9-1
...@@ -267,7 +267,15 @@ impl Served {...@@ -267,7 +267,15 @@ impl Served {
267 .find_map(|(held, at, image)| {267 .find_map(|(held, at, image)| {
268 (held == path && *at == stamp).then(|| Arc::clone(image))268 (held == path && *at == stamp).then(|| Arc::clone(image))
269 })269 })
270 .ok_or_else(|| retry("The batch's base is no longer held"))?;270 .ok_or_else(|| {
271 Error::Remote(CommitError {
272 state: CommitState::NotCommitted,
273 error: io::Error::new(
274 io::ErrorKind::StaleNetworkFileHandle,
275 "The batch's base is no longer held",
276 ),
277 })
278 })?;
271 let old_arena = Arena::default();279 let old_arena = Arena::default();
272 let mut old = Section::open(&old_arena, (*base).clone())?;280 let mut old = Section::open(&old_arena, (*base).clone())?;
273 if old.root() != next.root() {281 if old.root() != next.root() {
crates/notebook/src/live/wire.rs+2-1
...@@ -403,7 +403,7 @@ pub struct WireEntry {...@@ -403,7 +403,7 @@ pub struct WireEntry {
403}403}
404404
405/// The `io::ErrorKind`s a failure names, by number; any other is `Other`.405/// The `io::ErrorKind`s a failure names, by number; any other is `Other`.
406const ERROR_KINDS: [io::ErrorKind; 20] = [406const ERROR_KINDS: [io::ErrorKind; 21] = [
407 io::ErrorKind::Other,407 io::ErrorKind::Other,
408 io::ErrorKind::NotFound,408 io::ErrorKind::NotFound,
409 io::ErrorKind::PermissionDenied,409 io::ErrorKind::PermissionDenied,
...@@ -424,6 +424,7 @@ const ERROR_KINDS: [io::ErrorKind; 20] = [...@@ -424,6 +424,7 @@ const ERROR_KINDS: [io::ErrorKind; 20] = [
424 io::ErrorKind::UnexpectedEof,424 io::ErrorKind::UnexpectedEof,
425 io::ErrorKind::Interrupted,425 io::ErrorKind::Interrupted,
426 io::ErrorKind::BrokenPipe,426 io::ErrorKind::BrokenPipe,
427 io::ErrorKind::StaleNetworkFileHandle,
427];428];
428429
429pub fn error_number(kind: io::ErrorKind) -> u16 {430pub fn error_number(kind: io::ErrorKind) -> u16 {
crates/notebook/src/sync.rs+15-2
...@@ -28,7 +28,8 @@ pub trait Remote {...@@ -28,7 +28,8 @@ pub trait Remote {
28 false28 false
29 }29 }
30 /// Where the remote accepts edits, a batch published again with the same revisions,30 /// Where the remote accepts edits, a batch published again with the same revisions,
31 /// however its first attempt ended, lands at most once.31 /// however its first attempt ended, lands at most once; one refused as
32 /// `StaleNetworkFileHandle` never can, as the remote no longer holds its base.
32 fn publish_edits(33 fn publish_edits(
33 &mut self,34 &mut self,
34 transaction: &Transaction,35 transaction: &Transaction,
...@@ -463,7 +464,7 @@ impl Replica {...@@ -463,7 +464,7 @@ impl Replica {
463 /// Publishes the attempted `batch` sealed as `transaction`, rebasing on the remote's464 /// Publishes the attempted `batch` sealed as `transaction`, rebasing on the remote's
464 /// image after where `batched`; `changed` holds the pages a rebase already changed. An465 /// image after where `batched`; `changed` holds the pages a rebase already changed. An
465 /// attempt made `again` stays uncertain until the remote takes it, as the first may466 /// attempt made `again` stays uncertain until the remote takes it, as the first may
466 /// still land.467 /// still land. A batch whose base the remote refuses for good is rebased onto its image.
467 async fn publish_sealed(468 async fn publish_sealed(
468 &self,469 &self,
469 remote: &mut impl Remote,470 remote: &mut impl Remote,
...@@ -494,6 +495,18 @@ impl Replica {...@@ -494,6 +495,18 @@ impl Replica {
494 };495 };
495 match published {496 match published {
496 Ok(()) => {}497 Ok(()) => {}
498 Err(error)
499 if error.state == CommitState::NotCommitted
500 && error.error.kind() == io::ErrorKind::StaleNetworkFileHandle =>
501 {
502 self.lock()?
503 .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?;
504 let image = awaited(remote, Remote::read)
505 .await
506 .map_err(Error::RemoteIo)?;
507 self.rebase(Some(image))?;
508 return Err(error.into());
509 }
497 Err(error) if error.state == CommitState::NotCommitted && !again => {510 Err(error) if error.state == CommitState::NotCommitted && !again => {
498 self.lock()?511 self.lock()?
499 .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?;512 .execute("UPDATE batches SET attempted=0 WHERE id=?1", [batch])?;
crates/notebook/tests/live_batch.rs+84
...@@ -676,3 +676,87 @@ fn a_closed_section_s_uncertain_batch_lands_from_the_background() {...@@ -676,3 +676,87 @@ fn a_closed_section_s_uncertain_batch_lands_from_the_background() {
676 Some(EditStatus::Published { .. })676 Some(EditStatus::Published { .. })
677 ));677 ));
678}678}
679
680/// A batch lost on the way to a host that restarts, while the section moves on, is
681/// rebased onto the host's section once the new host no longer holds its base, and lands
682/// beside the host's own edit.
683#[test]
684fn a_lost_batch_whose_base_the_host_dropped_is_rebased() {
685 use notebook::{EditStatus, Remote, live::share::Guest};
686
687 let directory = tempfile::tempdir().unwrap();
688 let folder = notebook(directory.path());
689 let url = relay(Default::default());
690 let sharing = Sharing::new("").unwrap();
691 let host = host(&folder, &directory.path().join("host"), &sharing, &url);
692 let welcome =
693 notebook::live::share::join(hello("Grace"), &code(&host), "", None, Some(&url)).unwrap();
694 let relay = PresenceRelay::new(&url, &welcome.room);
695 let guest = Guest::start(
696 hello("Grace"),
697 welcome.share,
698 welcome.secret,
699 None,
700 Some(&relay.url),
701 || {},
702 )
703 .unwrap();
704 until("the host was never met", || guest.host().is_some());
705 let file = folder.join("Garden.one");
706 let original = std::fs::read(&file).unwrap();
707 let (space, text, _) = server::text(&original);
708 let typed = |with: &str| Edit {
709 at: 134_000_000_000_000_000,
710 ops: vec![Op::Page {
711 space,
712 op: PageOp::Text {
713 text,
714 range: 0..0,
715 with: with.into(),
716 },
717 }],
718 };
719 let replica = Replica::open_or_create(directory.path().join("grace.sqlite"), None, || {
720 Ok(original.clone())
721 })
722 .unwrap();
723 let remote = std::cell::RefCell::new(HostedRemote::new(&guest, "Garden.one"));
724 let first = replica.apply("Grace", typed("A")).unwrap();
725 until("the first edit was never published", || {
726 let _ = replica.sync_once(&mut *remote.borrow_mut());
727 matches!(
728 replica.status(first).unwrap(),
729 Some(EditStatus::Published { .. })
730 )
731 });
732 until("the host's stamp was never known without asking", || {
733 let before = relay.state.lock().unwrap().access_bytes;
734 remote.borrow_mut().stamp().unwrap();
735 relay.state.lock().unwrap().access_bytes == before
736 });
737 relay.state.lock().unwrap().swallow = true;
738 let second = replica.apply("Grace", typed("B")).unwrap();
739 assert!(replica.sync_once(&mut *remote.borrow_mut()).is_err());
740 assert!(matches!(
741 replica.status(second).unwrap(),
742 Some(EditStatus::AwaitingConfirmation { .. })
743 ));
744
745 // The host quits, edits the section itself, and shares again.
746 let saved = host.sharing();
747 drop(host);
748 let image = std::fs::read(&file).unwrap();
749 std::fs::write(&file, server::typed(&image, space, text, 14..14, "!")).unwrap();
750 let _host = self::host(&folder, &directory.path().join("host"), &saved, &url);
751 until("the uncertain batch never landed", || {
752 let _ = replica.sync_once(&mut HostedRemote::new(&guest, "Garden.one"));
753 matches!(
754 replica.status(second).unwrap(),
755 Some(EditStatus::Published { .. })
756 )
757 });
758 assert_eq!(
759 server::text(&std::fs::read(&file).unwrap()).2,
760 "BAOriginal text!"
761 );
762}