From ff83f397b54c323bb8e8bf8b767eca746d301928 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 19 Oct 2026 21:42:34 -0700 Subject: [PATCH] fix: retire a version only against the newest file A device that heard of a conflict version before the file that replaced its own copy merged the version into that stale copy, found it held there, and retired it everywhere; the download that followed replaced the copy, and what the version held was gone (cloud-drive sweep seed 728). A remote now lists no versions while its file is a copy the provider has yet to replace: on macOS and iOS, while iCloud Drive reports the file as other than current, and a file in that state is asked for again. Whatever a provider reports, a version identical to the file is kept, as the file is then that version's stale copy; the Android provider lists none such already. That iCloud reports a stale file as other than current until the newer one arrives is unverified on two devices. Assisted-by: claude-opus-5.5 --- apps/ios/Snowbound/ICloud.swift | 5 +- apps/ios/Snowbound/Notebooks.swift | 7 +- arc/sync.md | 5 +- crates/notebook/src/sync.rs | 8 +- crates/notebook/tests/cloud.rs | 113 ++++++++++++++++++++++++++- crates/snowbound/src/icloud_macos.rs | 60 ++++++++++---- 6 files changed, 174 insertions(+), 24 deletions(-) diff --git a/apps/ios/Snowbound/ICloud.swift b/apps/ios/Snowbound/ICloud.swift index 11c6d3d6d704b921952c9ec7944bf9a438af98e2..f604a3e9e04fc8a5e1bc7a9b4551ce29071a597a 100644 --- a/apps/ios/Snowbound/ICloud.swift +++ b/apps/ios/Snowbound/ICloud.swift @@ -1,10 +1,13 @@ import UIKit /// Lists the conflict versions iCloud Drive keeps beside a section file, as it keeps another -/// device's commit that lost; the library merges each into the file. +/// device's commit that lost; the library merges each into the file. None while the file is a +/// copy iCloud has yet to replace with a newer one, which a version must not be retired against. private let listVersions: sb_versions = { path, found, context in guard let path, let found else { return } let url = URL(fileURLWithPath: String(cString: path)) + let status = try? url.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey]) + if let status = status?.ubiquitousItemDownloadingStatus, status != .current { return } for version in NSFileVersion.unresolvedConflictVersionsOfItem(at: url) ?? [] { version.url.path.withCString { id in guard let device = version.localizedNameOfSavingComputer else { return found(context, id, nil) } diff --git a/apps/ios/Snowbound/Notebooks.swift b/apps/ios/Snowbound/Notebooks.swift index b92f169bf2596dd7a500ef9995482a3b364062ba..7e14fd277c78e25503d5c3042ebdc514fbfcf955 100644 --- a/apps/ios/Snowbound/Notebooks.swift +++ b/apps/ios/Snowbound/Notebooks.swift @@ -321,8 +321,8 @@ final class Notebook { } /// Asks iCloud Drive for the notebook files it keeps elsewhere, which iOS lists as - /// `.Name.icloud` or dataless under their names, and lists the sections again until they - /// arrive. + /// `.Name.icloud` or dataless under their names, or holds a newer version of, and lists the + /// sections again until they arrive. private func download() { let folder: URL? = switch source { @@ -340,7 +340,8 @@ final class Notebook { for case let file as URL in files { let stub = file.lastPathComponent.hasPrefix(".") && file.pathExtension == "icloud" let status = try? file.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey]) - guard stub || status?.ubiquitousItemDownloadingStatus == .notDownloaded else { continue } + let stale = status?.ubiquitousItemDownloadingStatus.map { $0 != .current } ?? false + guard stub || stale else { continue } let real = file.deletingLastPathComponent().appendingPathComponent( String(file.deletingPathExtension().lastPathComponent.dropFirst())) try? FileManager.default.startDownloadingUbiquitousItem(at: stub ? real : file) diff --git a/arc/sync.md b/arc/sync.md index d25b05d5d9b086f18f8f47faabe7329fc2ef1856..ea55d55df01d6b44d34ed298bf25eff32fbe46d6 100644 --- a/arc/sync.md +++ b/arc/sync.md @@ -298,7 +298,10 @@ another, merges as nothing the next time. Two devices merging the same version o current page state name corresponding outcomes alike; onto different states, differently, as their contents differ. This deduplicates retries; it does not make integrations from opposite primary bases order-independent or produce byte-identical files. Any device merges any version -at once; nothing waits for the device that wrote it. A version of another section, or one that +at once; nothing waits for the device that wrote it. A device lists none while its own file is +not yet the newest iCloud holds: a version found held in that stale copy would be retired +everywhere, then lost to the download that replaces the copy. A version identical to the file is +such a copy whatever iCloud reports, and waits too. A version of another section, or one that cannot be read, is kept beside the file as `Name (Device).one` instead. iCloud Drive can also lose an upload outright: another device's lands over it and no conflict diff --git a/crates/notebook/src/sync.rs b/crates/notebook/src/sync.rs index 9da7bd631b1b9fe32f73ba4ee8599893ff67e0f8..6a6d825211aeb740e97001bc94b67385f8054999 100644 --- a/crates/notebook/src/sync.rs +++ b/crates/notebook/src/sync.rs @@ -40,7 +40,8 @@ pub trait Remote { fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError>; /// The versions a file provider keeps beside the file, as iCloud Drive keeps the commits /// that lost to another device's (unresolved conflict versions); none by default. - /// Synchronization merges each into the file, then retires it. + /// Synchronization merges each into the file, then retires it, so none are listed while + /// the file is a copy the provider has yet to replace with a newer one. fn versions(&mut self) -> io::Result> { Ok(Vec::new()) } @@ -252,6 +253,11 @@ impl Replica { let current = awaited(remote, Remote::read) .await .map_err(Error::RemoteIo)?; + // The file is this very version: a copy the provider has yet to replace, whatever it + // reports, so the version is kept for the file that replaces it. + if current == image { + continue; + } let device = version.device.as_deref().unwrap_or("Another device"); let keep = match crate::resolve::merge(¤t, &image, device) { Ok(Merged::Held) => false, diff --git a/crates/notebook/tests/cloud.rs b/crates/notebook/tests/cloud.rs index 3c658b692bcdc8b39086b6e7d33392718bae06f9..7f7c76b8a4ec2231645c4bb7e24fcc536ed83a2b 100644 --- a/crates/notebook/tests/cloud.rs +++ b/crates/notebook/tests/cloud.rs @@ -94,6 +94,9 @@ impl Remote for Local<'_> { } fn versions(&mut self) -> io::Result> { + if self.copy.base != self.server.id { + return Ok(Vec::new()); + } Ok(self .copy .versions @@ -1407,7 +1410,6 @@ fn merges_of_one_version_onto_different_states_keep_both() { assert!(conflicts.is_empty(), "{winner}: {conflicts}"); } } - /// A version an older build merged, under the names it gave merges, merges as nothing again: /// neither its clean merge nor its conflict page comes back /// (`corpus/icloud-merge/older-build-merges`). @@ -1426,7 +1428,6 @@ fn a_version_an_older_build_merged_merges_as_nothing() { ); } } - /// A merge of a merge onto the same state is a revision of its own: Z's version merged into X's /// file, that file merged into W's on X's state, and W's file merged back into Z's copy keeps /// X's typing. Z's own, merged on both sides, may show twice, as in the sweep's duplicates. @@ -1468,3 +1469,111 @@ fn a_merge_merged_again_onto_the_same_state_is_its_own_revision() { ); } } + +/// A device that hears of a version before the file that replaced its own copy merges it only +/// into that file: found held in the stale copy, it would be retired everywhere and the +/// download that follows would drop what it held (sweep seed 728). +#[test] +fn a_version_waits_for_the_file_that_replaced_a_stale_copy() { + let source = fixture(); + let directory = tempfile::tempdir().unwrap(); + let mut server = Server { + current: source.clone(), + id: 0, + next: 0, + conflicts: Vec::new(), + kept: 0, + }; + let mut devices: Vec = DEVICES + .into_iter() + .enumerate() + .map(|(n, name)| Device { + name: name.into(), + copy: Copy { + bytes: source.clone(), + base: 0, + dirty: false, + versions: Vec::new(), + online: true, + }, + replica: Replica::create(directory.path().join(format!("{n}.sqlite")), &source) + .unwrap(), + }) + .collect(); + let pages = devices[0].replica.pages().unwrap(); + let typing = |device: &Device, page: usize, with: &str| { + let space = pages[page].0; + let (text, _) = bodies(&device.replica.page(space).unwrap())[0].clone(); + let op = PageOp::Text { + text, + range: 0..0, + with: with.into(), + }; + let edit = Edit { + at: crate_now(), + ops: vec![Op::Page { space, op }], + }; + device.replica.apply(&device.name, edit).unwrap(); + }; + let mut random = Random(1); + // The Mac's typing becomes the file. + typing(&devices[0], 0, "⟨Mac⟩"); + devices[0].sync(&mut server); + devices[0].upload(&mut server, &mut random); + assert_eq!(server.id, devices[0].copy.base); + // The iPhone, still on the first file, uploads too and wins: the Mac's file becomes a + // version, which the Mac hears of before the iPhone's file reaches it. + typing(&devices[1], 1, "⟨iPhone⟩"); + devices[1].sync(&mut server); + let lost = std::mem::replace(&mut server.current, devices[1].copy.bytes.clone()); + let id = server.next(); + server.conflicts.push((id, lost, "Mac".into())); + server.id = server.next(); + devices[1].copy.base = server.id; + devices[1].copy.dirty = false; + devices[0].deliver(&server); + devices[0].sync(&mut server); + for _ in 0..4 { + for device in &mut devices { + device.download(&mut server, &mut random); + device.deliver(&server); + device.sync(&mut server); + device.upload(&mut server, &mut random); + } + } + assert!(server.conflicts.is_empty()); + let (listed, _) = texts_of(&server.current); + for token in ["⟨Mac⟩", "⟨iPhone⟩"] { + assert_eq!(listed.matches(token).count(), 1, "{token}:\n{listed}"); + } +} + +/// A version identical to the file is that file's stale copy whatever the provider reports: +/// it waits for the file that replaces the copy. +#[test] +fn a_version_identical_to_the_file_is_kept() { + let source = fixture(); + let directory = tempfile::tempdir().unwrap(); + let mut server = Server { + current: source.clone(), + id: 1, + next: 1, + conflicts: vec![(2, source.clone(), "iPhone".into())], + kept: 0, + }; + let mut device = Device { + name: "Mac".into(), + copy: Copy { + bytes: source.clone(), + base: 1, + dirty: false, + versions: Vec::new(), + online: true, + }, + replica: Replica::create(directory.path().join("replica.sqlite"), &source).unwrap(), + }; + device.deliver(&server); + device.sync(&mut server); + assert_eq!(server.conflicts.len(), 1); + assert_eq!(server.kept, 0); +} diff --git a/crates/snowbound/src/icloud_macos.rs b/crates/snowbound/src/icloud_macos.rs index 5f96905974075f421fa5cbe625afac195c248fbf..e20290aa546d153546ae7d226d66fb91f2f436d6 100644 --- a/crates/snowbound/src/icloud_macos.rs +++ b/crates/snowbound/src/icloud_macos.rs @@ -18,7 +18,8 @@ use objc2_foundation::{ NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions, NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter, NSOperationQueue, NSString, NSURL, NSURLIsUbiquitousItemKey, - NSURLUbiquitousItemDownloadingStatusKey, NSURLUbiquitousItemDownloadingStatusNotDownloaded, + NSURLUbiquitousItemDownloadingStatusCurrent, NSURLUbiquitousItemDownloadingStatusKey, + NSURLUbiquitousItemDownloadingStatusNotDownloaded, }; use onestore::{CommitError, CommitState, Stamp, Transaction}; use std::{ @@ -177,28 +178,52 @@ fn coordinated(path: &Path, write: bool, work: impl FnOnce() -> T) -> io::Res }) } -/// Asks for a file iCloud Drive keeps elsewhere, which a read would otherwise wait for: the -/// sync thread tries again later, while the section's replica serves it. +/// iCloud Drive's download status of the file at `path`, none for a file it does not keep. +fn status(path: &Path) -> Option<(Retained, Retained)> { + let url = url(path)?; + let status = resource(&url, unsafe { NSURLUbiquitousItemDownloadingStatusKey })?; + Some((url, status)) +} + +fn is(status: &AnyObject, value: &NSString) -> bool { + unsafe { objc2::msg_send![status, isEqual: value] } +} + +/// Asks iCloud Drive for a newer file than the one here, and errs while there is none here, +/// which a read would otherwise wait for: the sync thread tries again later, while the +/// section's replica serves it. fn present(path: &Path) -> io::Result<()> { - let Some(url) = url(path) else { + let Some((url, status)) = status(path) else { return Ok(()); }; - let status = resource(&url, unsafe { NSURLUbiquitousItemDownloadingStatusKey }); - let missing = status.is_some_and(|status| { - let equal: bool = unsafe { - objc2::msg_send![&status, isEqual: NSURLUbiquitousItemDownloadingStatusNotDownloaded] - }; - equal - }); - if !missing { + let (current, missing) = unsafe { + ( + NSURLUbiquitousItemDownloadingStatusCurrent, + NSURLUbiquitousItemDownloadingStatusNotDownloaded, + ) + }; + if is(&status, current) { return Ok(()); } let manager = unsafe { NSFileManager::defaultManager() }; let _ = unsafe { manager.startDownloadingUbiquitousItemAtURL_error(&url) }; - Err(io::Error::new( - io::ErrorKind::NotConnected, - "Downloading from iCloud Drive", - )) + if is(&status, missing) { + return Err(io::Error::new( + io::ErrorKind::NotConnected, + "Downloading from iCloud Drive", + )); + } + Ok(()) +} + +/// Whether the file at `path` is the newest iCloud Drive holds, rather than a copy it has yet +/// to replace. +fn current(path: &Path) -> bool { + status(path).is_none_or(|(_, status)| { + is(&status, unsafe { + NSURLUbiquitousItemDownloadingStatusCurrent + }) + }) } /// Asks iCloud Drive for every file below `root` it keeps elsewhere, dataless or as a @@ -360,6 +385,9 @@ impl Remote for Coordinated { /// iCloud's conflict versions, then this Mac's publications the file lost. fn versions(&mut self) -> io::Result> { + if !current(&self.file) { + return Ok(Vec::new()); + } let mut listed: Vec = (self.conflicts()?.into_iter()) .filter_map(|version| { Some(Version { -- 2.54.1