authoravatarclover caruso<git@paperclover.net>2026-10-19 21:42:34-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:57:03-07:00
logff83f397b54c323bb8e8bf8b767eca746d301928
tree3bb740393905bdb038d694a72e764d57bb6d1701
parentbe5dcab42ad21c294fc9f06c65ac1f3a7d1a4743
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

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

6 files changed, 174 insertions(+), 24 deletions(-)

apps/ios/Snowbound/ICloud.swift+4-1
...@@ -1,10 +1,13 @@...@@ -1,10 +1,13 @@
1import UIKit1import UIKit
22
3/// Lists the conflict versions iCloud Drive keeps beside a section file, as it keeps another3/// Lists the conflict versions iCloud Drive keeps beside a section file, as it keeps another
4/// device's commit that lost; the library merges each into the file.4/// device's commit that lost; the library merges each into the file. None while the file is a
5/// copy iCloud has yet to replace with a newer one, which a version must not be retired against.
5private let listVersions: sb_versions = { path, found, context in6private let listVersions: sb_versions = { path, found, context in
6 guard let path, let found else { return }7 guard let path, let found else { return }
7 let url = URL(fileURLWithPath: String(cString: path))8 let url = URL(fileURLWithPath: String(cString: path))
9 let status = try? url.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])
10 if let status = status?.ubiquitousItemDownloadingStatus, status != .current { return }
8 for version in NSFileVersion.unresolvedConflictVersionsOfItem(at: url) ?? [] {11 for version in NSFileVersion.unresolvedConflictVersionsOfItem(at: url) ?? [] {
9 version.url.path.withCString { id in12 version.url.path.withCString { id in
10 guard let device = version.localizedNameOfSavingComputer else { return found(context, id, nil) }13 guard let device = version.localizedNameOfSavingComputer else { return found(context, id, nil) }
apps/ios/Snowbound/Notebooks.swift+4-3
...@@ -321,8 +321,8 @@ final class Notebook {...@@ -321,8 +321,8 @@ final class Notebook {
321 }321 }
322322
323 /// Asks iCloud Drive for the notebook files it keeps elsewhere, which iOS lists as323 /// Asks iCloud Drive for the notebook files it keeps elsewhere, which iOS lists as
324 /// `.Name.icloud` or dataless under their names, and lists the sections again until they324 /// `.Name.icloud` or dataless under their names, or holds a newer version of, and lists the
325 /// arrive.325 /// sections again until they arrive.
326 private func download() {326 private func download() {
327 let folder: URL? =327 let folder: URL? =
328 switch source {328 switch source {
...@@ -340,7 +340,8 @@ final class Notebook {...@@ -340,7 +340,8 @@ final class Notebook {
340 for case let file as URL in files {340 for case let file as URL in files {
341 let stub = file.lastPathComponent.hasPrefix(".") && file.pathExtension == "icloud"341 let stub = file.lastPathComponent.hasPrefix(".") && file.pathExtension == "icloud"
342 let status = try? file.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])342 let status = try? file.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])
343 guard stub || status?.ubiquitousItemDownloadingStatus == .notDownloaded else { continue }343 let stale = status?.ubiquitousItemDownloadingStatus.map { $0 != .current } ?? false
344 guard stub || stale else { continue }
344 let real = file.deletingLastPathComponent().appendingPathComponent(345 let real = file.deletingLastPathComponent().appendingPathComponent(
345 String(file.deletingPathExtension().lastPathComponent.dropFirst()))346 String(file.deletingPathExtension().lastPathComponent.dropFirst()))
346 try? FileManager.default.startDownloadingUbiquitousItem(at: stub ? real : file)347 try? FileManager.default.startDownloadingUbiquitousItem(at: stub ? real : file)
arc/sync.md+4-1
...@@ -298,7 +298,10 @@ another, merges as nothing the next time. Two devices merging the same version o...@@ -298,7 +298,10 @@ another, merges as nothing the next time. Two devices merging the same version o
298current page state name corresponding outcomes alike; onto different states, differently, as298current page state name corresponding outcomes alike; onto different states, differently, as
299their contents differ. This deduplicates retries; it does not make integrations from opposite299their contents differ. This deduplicates retries; it does not make integrations from opposite
300primary bases order-independent or produce byte-identical files. Any device merges any version300primary bases order-independent or produce byte-identical files. Any device merges any version
301at once; nothing waits for the device that wrote it. A version of another section, or one that301at once; nothing waits for the device that wrote it. A device lists none while its own file is
302not yet the newest iCloud holds: a version found held in that stale copy would be retired
303everywhere, then lost to the download that replaces the copy. A version identical to the file is
304such a copy whatever iCloud reports, and waits too. A version of another section, or one that
302cannot be read, is kept beside the file as `Name (Device).one` instead.305cannot be read, is kept beside the file as `Name (Device).one` instead.
303306
304iCloud Drive can also lose an upload outright: another device's lands over it and no conflict307iCloud Drive can also lose an upload outright: another device's lands over it and no conflict
crates/notebook/src/sync.rs+7-1
...@@ -40,7 +40,8 @@ pub trait Remote {...@@ -40,7 +40,8 @@ pub trait Remote {
40 fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError>;40 fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError>;
41 /// The versions a file provider keeps beside the file, as iCloud Drive keeps the commits41 /// The versions a file provider keeps beside the file, as iCloud Drive keeps the commits
42 /// that lost to another device's (unresolved conflict versions); none by default.42 /// that lost to another device's (unresolved conflict versions); none by default.
43 /// Synchronization merges each into the file, then retires it.43 /// Synchronization merges each into the file, then retires it, so none are listed while
44 /// the file is a copy the provider has yet to replace with a newer one.
44 fn versions(&mut self) -> io::Result<Vec<Version>> {45 fn versions(&mut self) -> io::Result<Vec<Version>> {
45 Ok(Vec::new())46 Ok(Vec::new())
46 }47 }
...@@ -252,6 +253,11 @@ impl Replica {...@@ -252,6 +253,11 @@ impl Replica {
252 let current = awaited(remote, Remote::read)253 let current = awaited(remote, Remote::read)
253 .await254 .await
254 .map_err(Error::RemoteIo)?;255 .map_err(Error::RemoteIo)?;
256 // The file is this very version: a copy the provider has yet to replace, whatever it
257 // reports, so the version is kept for the file that replaces it.
258 if current == image {
259 continue;
260 }
255 let device = version.device.as_deref().unwrap_or("Another device");261 let device = version.device.as_deref().unwrap_or("Another device");
256 let keep = match crate::resolve::merge(&current, &image, device) {262 let keep = match crate::resolve::merge(&current, &image, device) {
257 Ok(Merged::Held) => false,263 Ok(Merged::Held) => false,
crates/notebook/tests/cloud.rs+111-2
...@@ -94,6 +94,9 @@ impl Remote for Local<'_> {...@@ -94,6 +94,9 @@ impl Remote for Local<'_> {
94 }94 }
9595
96 fn versions(&mut self) -> io::Result<Vec<Version>> {96 fn versions(&mut self) -> io::Result<Vec<Version>> {
97 if self.copy.base != self.server.id {
98 return Ok(Vec::new());
99 }
97 Ok(self100 Ok(self
98 .copy101 .copy
99 .versions102 .versions
...@@ -1407,7 +1410,6 @@ fn merges_of_one_version_onto_different_states_keep_both() {...@@ -1407,7 +1410,6 @@ fn merges_of_one_version_onto_different_states_keep_both() {
1407 assert!(conflicts.is_empty(), "{winner}: {conflicts}");1410 assert!(conflicts.is_empty(), "{winner}: {conflicts}");
1408 }1411 }
1409}1412}
1410
1411/// A version an older build merged, under the names it gave merges, merges as nothing again:1413/// A version an older build merged, under the names it gave merges, merges as nothing again:
1412/// neither its clean merge nor its conflict page comes back1414/// neither its clean merge nor its conflict page comes back
1413/// (`corpus/icloud-merge/older-build-merges`).1415/// (`corpus/icloud-merge/older-build-merges`).
...@@ -1426,7 +1428,6 @@ fn a_version_an_older_build_merged_merges_as_nothing() {...@@ -1426,7 +1428,6 @@ fn a_version_an_older_build_merged_merges_as_nothing() {
1426 );1428 );
1427 }1429 }
1428}1430}
1429
1430/// A merge of a merge onto the same state is a revision of its own: Z's version merged into X's1431/// A merge of a merge onto the same state is a revision of its own: Z's version merged into X's
1431/// file, that file merged into W's on X's state, and W's file merged back into Z's copy keeps1432/// file, that file merged into W's on X's state, and W's file merged back into Z's copy keeps
1432/// X's typing. Z's own, merged on both sides, may show twice, as in the sweep's duplicates.1433/// 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() {...@@ -1468,3 +1469,111 @@ fn a_merge_merged_again_onto_the_same_state_is_its_own_revision() {
1468 );1469 );
1469 }1470 }
1470}1471}
1472
1473/// A device that hears of a version before the file that replaced its own copy merges it only
1474/// into that file: found held in the stale copy, it would be retired everywhere and the
1475/// download that follows would drop what it held (sweep seed 728).
1476#[test]
1477fn a_version_waits_for_the_file_that_replaced_a_stale_copy() {
1478 let source = fixture();
1479 let directory = tempfile::tempdir().unwrap();
1480 let mut server = Server {
1481 current: source.clone(),
1482 id: 0,
1483 next: 0,
1484 conflicts: Vec::new(),
1485 kept: 0,
1486 };
1487 let mut devices: Vec<Device> = DEVICES
1488 .into_iter()
1489 .enumerate()
1490 .map(|(n, name)| Device {
1491 name: name.into(),
1492 copy: Copy {
1493 bytes: source.clone(),
1494 base: 0,
1495 dirty: false,
1496 versions: Vec::new(),
1497 online: true,
1498 },
1499 replica: Replica::create(directory.path().join(format!("{n}.sqlite")), &source)
1500 .unwrap(),
1501 })
1502 .collect();
1503 let pages = devices[0].replica.pages().unwrap();
1504 let typing = |device: &Device, page: usize, with: &str| {
1505 let space = pages[page].0;
1506 let (text, _) = bodies(&device.replica.page(space).unwrap())[0].clone();
1507 let op = PageOp::Text {
1508 text,
1509 range: 0..0,
1510 with: with.into(),
1511 };
1512 let edit = Edit {
1513 at: crate_now(),
1514 ops: vec![Op::Page { space, op }],
1515 };
1516 device.replica.apply(&device.name, edit).unwrap();
1517 };
1518 let mut random = Random(1);
1519 // The Mac's typing becomes the file.
1520 typing(&devices[0], 0, "⟨Mac⟩");
1521 devices[0].sync(&mut server);
1522 devices[0].upload(&mut server, &mut random);
1523 assert_eq!(server.id, devices[0].copy.base);
1524 // The iPhone, still on the first file, uploads too and wins: the Mac's file becomes a
1525 // version, which the Mac hears of before the iPhone's file reaches it.
1526 typing(&devices[1], 1, "⟨iPhone⟩");
1527 devices[1].sync(&mut server);
1528 let lost = std::mem::replace(&mut server.current, devices[1].copy.bytes.clone());
1529 let id = server.next();
1530 server.conflicts.push((id, lost, "Mac".into()));
1531 server.id = server.next();
1532 devices[1].copy.base = server.id;
1533 devices[1].copy.dirty = false;
1534 devices[0].deliver(&server);
1535 devices[0].sync(&mut server);
1536 for _ in 0..4 {
1537 for device in &mut devices {
1538 device.download(&mut server, &mut random);
1539 device.deliver(&server);
1540 device.sync(&mut server);
1541 device.upload(&mut server, &mut random);
1542 }
1543 }
1544 assert!(server.conflicts.is_empty());
1545 let (listed, _) = texts_of(&server.current);
1546 for token in ["⟨Mac⟩", "⟨iPhone⟩"] {
1547 assert_eq!(listed.matches(token).count(), 1, "{token}:\n{listed}");
1548 }
1549}
1550
1551/// A version identical to the file is that file's stale copy whatever the provider reports:
1552/// it waits for the file that replaces the copy.
1553#[test]
1554fn a_version_identical_to_the_file_is_kept() {
1555 let source = fixture();
1556 let directory = tempfile::tempdir().unwrap();
1557 let mut server = Server {
1558 current: source.clone(),
1559 id: 1,
1560 next: 1,
1561 conflicts: vec![(2, source.clone(), "iPhone".into())],
1562 kept: 0,
1563 };
1564 let mut device = Device {
1565 name: "Mac".into(),
1566 copy: Copy {
1567 bytes: source.clone(),
1568 base: 1,
1569 dirty: false,
1570 versions: Vec::new(),
1571 online: true,
1572 },
1573 replica: Replica::create(directory.path().join("replica.sqlite"), &source).unwrap(),
1574 };
1575 device.deliver(&server);
1576 device.sync(&mut server);
1577 assert_eq!(server.conflicts.len(), 1);
1578 assert_eq!(server.kept, 0);
1579}
crates/snowbound/src/icloud_macos.rs+44-16
...@@ -18,7 +18,8 @@ use objc2_foundation::{...@@ -18,7 +18,8 @@ use objc2_foundation::{
18 NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions,18 NSFileCoordinator, NSFileCoordinatorReadingOptions, NSFileCoordinatorWritingOptions,
19 NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter,19 NSFileManager, NSFilePresenter, NSFileVersion, NSNotification, NSNotificationCenter,
20 NSOperationQueue, NSString, NSURL, NSURLIsUbiquitousItemKey,20 NSOperationQueue, NSString, NSURL, NSURLIsUbiquitousItemKey,
21 NSURLUbiquitousItemDownloadingStatusKey, NSURLUbiquitousItemDownloadingStatusNotDownloaded,21 NSURLUbiquitousItemDownloadingStatusCurrent, NSURLUbiquitousItemDownloadingStatusKey,
22 NSURLUbiquitousItemDownloadingStatusNotDownloaded,
22};23};
23use onestore::{CommitError, CommitState, Stamp, Transaction};24use onestore::{CommitError, CommitState, Stamp, Transaction};
24use std::{25use std::{
...@@ -177,28 +178,52 @@ fn coordinated<T>(path: &Path, write: bool, work: impl FnOnce() -> T) -> io::Res...@@ -177,28 +178,52 @@ fn coordinated<T>(path: &Path, write: bool, work: impl FnOnce() -> T) -> io::Res
177 })178 })
178}179}
179180
180/// Asks for a file iCloud Drive keeps elsewhere, which a read would otherwise wait for: the181/// iCloud Drive's download status of the file at `path`, none for a file it does not keep.
181/// sync thread tries again later, while the section's replica serves it.182fn status(path: &Path) -> Option<(Retained<NSURL>, Retained<AnyObject>)> {
183 let url = url(path)?;
184 let status = resource(&url, unsafe { NSURLUbiquitousItemDownloadingStatusKey })?;
185 Some((url, status))
186}
187
188fn is(status: &AnyObject, value: &NSString) -> bool {
189 unsafe { objc2::msg_send![status, isEqual: value] }
190}
191
192/// Asks iCloud Drive for a newer file than the one here, and errs while there is none here,
193/// which a read would otherwise wait for: the sync thread tries again later, while the
194/// section's replica serves it.
182fn present(path: &Path) -> io::Result<()> {195fn present(path: &Path) -> io::Result<()> {
183 let Some(url) = url(path) else {196 let Some((url, status)) = status(path) else {
184 return Ok(());197 return Ok(());
185 };198 };
186 let status = resource(&url, unsafe { NSURLUbiquitousItemDownloadingStatusKey });199 let (current, missing) = unsafe {
187 let missing = status.is_some_and(|status| {200 (
188 let equal: bool = unsafe {201 NSURLUbiquitousItemDownloadingStatusCurrent,
189 objc2::msg_send![&status, isEqual: NSURLUbiquitousItemDownloadingStatusNotDownloaded]202 NSURLUbiquitousItemDownloadingStatusNotDownloaded,
190 };203 )
191 equal204 };
192 });205 if is(&status, current) {
193 if !missing {
194 return Ok(());206 return Ok(());
195 }207 }
196 let manager = unsafe { NSFileManager::defaultManager() };208 let manager = unsafe { NSFileManager::defaultManager() };
197 let _ = unsafe { manager.startDownloadingUbiquitousItemAtURL_error(&url) };209 let _ = unsafe { manager.startDownloadingUbiquitousItemAtURL_error(&url) };
198 Err(io::Error::new(210 if is(&status, missing) {
199 io::ErrorKind::NotConnected,211 return Err(io::Error::new(
200 "Downloading from iCloud Drive",212 io::ErrorKind::NotConnected,
201 ))213 "Downloading from iCloud Drive",
214 ));
215 }
216 Ok(())
217}
218
219/// Whether the file at `path` is the newest iCloud Drive holds, rather than a copy it has yet
220/// to replace.
221fn current(path: &Path) -> bool {
222 status(path).is_none_or(|(_, status)| {
223 is(&status, unsafe {
224 NSURLUbiquitousItemDownloadingStatusCurrent
225 })
226 })
202}227}
203228
204/// Asks iCloud Drive for every file below `root` it keeps elsewhere, dataless or as a229/// Asks iCloud Drive for every file below `root` it keeps elsewhere, dataless or as a
...@@ -360,6 +385,9 @@ impl Remote for Coordinated {...@@ -360,6 +385,9 @@ impl Remote for Coordinated {
360385
361 /// iCloud's conflict versions, then this Mac's publications the file lost.386 /// iCloud's conflict versions, then this Mac's publications the file lost.
362 fn versions(&mut self) -> io::Result<Vec<Version>> {387 fn versions(&mut self) -> io::Result<Vec<Version>> {
388 if !current(&self.file) {
389 return Ok(Vec::new());
390 }
363 let mut listed: Vec<Version> = (self.conflicts()?.into_iter())391 let mut listed: Vec<Version> = (self.conflicts()?.into_iter())
364 .filter_map(|version| {392 .filter_map(|version| {
365 Some(Version {393 Some(Version {