From 2a692abd1d806dbcc361eb05b898da71172dc37d Mon Sep 17 00:00:00 2001 From: Ryan Ashcraft Date: Mon, 13 Jul 2026 08:33:55 -0700 Subject: [PATCH 1/5] Adopt Swift 6 tools with v5 language mode and StrictConcurrency warnings (cherry picked from commit e0ec2fd7399140779b639a5ffb5a674560213eec) --- Package.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index e598029..8bafcc7 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.9 +// swift-tools-version: 6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -29,11 +29,18 @@ let package = Package( dependencies: [ .product(name: "PID", package: "swift-pid"), .product(name: "Logging", package: "swift-log"), + ], + swiftSettings: [ + .swiftLanguageMode(.v5), + .enableUpcomingFeature("StrictConcurrency"), ] ), .testTarget( name: "CloudSyncSessionTests", - dependencies: ["CloudSyncSession"] + dependencies: ["CloudSyncSession"], + swiftSettings: [ + .swiftLanguageMode(.v5), + ] ), ] ) From 3624390f9bfd2b756720d98a06e911d92af6f067 Mon Sep 17 00:00:00 2001 From: Ryan Ashcraft Date: Tue, 10 Nov 2026 20:58:35 -0800 Subject: [PATCH 2/5] Lock throttle state in the CloudKit operation handler rateLimitController, throttleDuration, and nextOperationDeadline are written from CloudKit completion callbacks on arbitrary threads and read by queueOperation on the caller's thread. Guard all three with one lock and make them private so nothing reads them unguarded. This is the throttle half of 8371389 from async-surfaces. That commit also locked per-operation accumulators for modifyRecordsResultBlock and the pre-builder fetch code; main has since restored the deprecated modify completion block (0ba54f7) and moved fetch accumulation into FetchOperationResponseBuilder (4d00787), so those hunks are dropped and main's side is kept unchanged. Co-Authored-By: Claude Fable 5.2 --- .../CloudKitOperationHandler.swift | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Sources/CloudSyncSession/CloudKitOperationHandler.swift b/Sources/CloudSyncSession/CloudKitOperationHandler.swift index 2d9aa65..d7e8d08 100644 --- a/Sources/CloudSyncSession/CloudKitOperationHandler.swift +++ b/Sources/CloudSyncSession/CloudKitOperationHandler.swift @@ -34,7 +34,11 @@ public class CloudKitOperationHandler: OperationHandler { let subscriptionID: String let savePolicy: CKModifyRecordsOperation.RecordSavePolicy = .ifServerRecordUnchanged let qos: QualityOfService = .userInitiated - var rateLimitController = RateLimitPIDController( + /// Guards rateLimitController, throttleDuration, and nextOperationDeadline, + /// which are mutated from CloudKit callback threads and read when queueing. + private let throttleLock = NSLock() + + private var rateLimitController = RateLimitPIDController( kp: 2, ki: 0.05, kd: 0.02, @@ -51,7 +55,7 @@ public class CloudKitOperationHandler: OperationHandler { return queue }() - var throttleDuration: TimeInterval { + private var throttleDuration: TimeInterval { didSet { nextOperationDeadline = DispatchTime.now() + throttleDuration @@ -63,7 +67,7 @@ public class CloudKitOperationHandler: OperationHandler { } } - var nextOperationDeadline: DispatchTime? + private var nextOperationDeadline: DispatchTime? public init(database: CKDatabase, zoneID: CKRecordZone.ID, subscriptionID: String) { self.database = database @@ -73,7 +77,9 @@ public class CloudKitOperationHandler: OperationHandler { } private func queueOperation(_ operation: Operation) { + throttleLock.lock() let deadline: DispatchTime = nextOperationDeadline ?? DispatchTime.now() + throttleLock.unlock() DispatchQueue.main.asyncAfter(deadline: deadline) { self.operationQueue.addOperation(operation) @@ -81,22 +87,30 @@ public class CloudKitOperationHandler: OperationHandler { } private func onOperationSuccess() { + throttleLock.lock() + defer { throttleLock.unlock() } + rateLimitController.record(outcome: .success) throttleDuration = min(Self.maxThrottleDuration, max(Self.minThrottleDuration, rateLimitController.rateLimit)) } private func onOperationError(_ error: Error) { - if let ckError = error as? CKError { - rateLimitController.record(outcome: ckError.indicatesShouldBackoff ? .failure : .success) + guard let ckError = error as? CKError else { + return + } - if let suggestedBackoffSeconds = ckError.suggestedBackoffSeconds { - Log.operations.info("CloudKit error suggests retrying after \(suggestedBackoffSeconds) seconds") + throttleLock.lock() + defer { throttleLock.unlock() } - // Respect the amount suggested for the next operation - throttleDuration = suggestedBackoffSeconds - } else { - throttleDuration = min(Self.maxThrottleDuration, max(Self.minThrottleDuration, rateLimitController.rateLimit)) - } + rateLimitController.record(outcome: ckError.indicatesShouldBackoff ? .failure : .success) + + if let suggestedBackoffSeconds = ckError.suggestedBackoffSeconds { + Log.operations.info("CloudKit error suggests retrying after \(suggestedBackoffSeconds) seconds") + + // Respect the amount suggested for the next operation + throttleDuration = suggestedBackoffSeconds + } else { + throttleDuration = min(Self.maxThrottleDuration, max(Self.minThrottleDuration, rateLimitController.rateLimit)) } } From b3f0c1f8e814fa563953b89b6875efe7a8fa9807 Mon Sep 17 00:00:00 2001 From: Ryan Ashcraft Date: Mon, 13 Jul 2026 08:54:05 -0700 Subject: [PATCH 3/5] Carry checkpointID on the final split chunk even when there are no deletions Co-Authored-By: Claude Fable 5 (cherry picked from commit b1406930cd12150c8e2626f3d16adf36c124dfca) --- Sources/CloudSyncSession/SyncWork.swift | 24 +++++++++++-- .../CloudSyncSessionTests/SyncWorkTests.swift | 36 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 Tests/CloudSyncSessionTests/SyncWorkTests.swift diff --git a/Sources/CloudSyncSession/SyncWork.swift b/Sources/CloudSyncSession/SyncWork.swift index ee528a7..0733056 100644 --- a/Sources/CloudSyncSession/SyncWork.swift +++ b/Sources/CloudSyncSession/SyncWork.swift @@ -137,8 +137,28 @@ public struct ModifyOperation: Identifiable, SyncOperation { let splitRecords: [[CKRecord]] = records.chunked(into: maxRecommendedRecordsPerOperation) let splitRecordIDsToDelete: [[CKRecord.ID]] = recordIDsToDelete.chunked(into: maxRecommendedRecordsPerOperation) - return splitRecords.map { ModifyOperation(records: $0, recordIDsToDelete: [], checkpointID: nil, userInfo: userInfo) } + - splitRecordIDsToDelete.enumerated().map { ModifyOperation(records: [], recordIDsToDelete: $0.element, checkpointID: $0.offset == splitRecordIDsToDelete.count - 1 ? checkpointID : nil, userInfo: userInfo) } + let recordChunks = splitRecords.map { + ModifyOperation(records: $0, recordIDsToDelete: [], checkpointID: nil, userInfo: userInfo) + } + let deletionChunks = splitRecordIDsToDelete.map { + ModifyOperation(records: [], recordIDsToDelete: $0, checkpointID: nil, userInfo: userInfo) + } + + var chunks = recordChunks + deletionChunks + + // The checkpoint must ride the final chunk so checkpoint-correlated + // waiters resolve only after every piece of the original operation + // has completed. + if let lastChunk = chunks.last { + chunks[chunks.count - 1] = ModifyOperation( + records: lastChunk.records, + recordIDsToDelete: lastChunk.recordIDsToDelete, + checkpointID: checkpointID, + userInfo: userInfo + ) + } + + return chunks } var splitInHalf: [ModifyOperation] { diff --git a/Tests/CloudSyncSessionTests/SyncWorkTests.swift b/Tests/CloudSyncSessionTests/SyncWorkTests.swift new file mode 100644 index 0000000..6eef840 --- /dev/null +++ b/Tests/CloudSyncSessionTests/SyncWorkTests.swift @@ -0,0 +1,36 @@ +@testable import CloudSyncSession +import CloudKit +import XCTest + +final class SyncWorkTests: XCTestCase { + func testSplitPreservesCheckpointIDWhenThereAreNoDeletions() { + let checkpointID = UUID() + let operation = ModifyOperation( + records: (0 ..< 900).map { _ in makeTestRecord() }, + recordIDsToDelete: [], + checkpointID: checkpointID, + userInfo: nil + ) + + let chunks = operation.split + + XCTAssertEqual(chunks.count, 3) + XCTAssertEqual(chunks.last?.checkpointID, checkpointID) + XCTAssertTrue(chunks.dropLast().allSatisfy { $0.checkpointID == nil }) + } + + func testSplitKeepsCheckpointIDOnLastDeleteChunkWhenDeletionsExist() { + let checkpointID = UUID() + let operation = ModifyOperation( + records: (0 ..< 500).map { _ in makeTestRecord() }, + recordIDsToDelete: (0 ..< 500).map { _ in makeTestRecord().recordID }, + checkpointID: checkpointID, + userInfo: nil + ) + + let chunks = operation.split + + XCTAssertEqual(chunks.last?.checkpointID, checkpointID) + XCTAssertEqual(chunks.filter { $0.checkpointID != nil }.count, 1) + } +} From 3035b57ce4db6a682aaa4503386090b12e8ef8c8 Mon Sep 17 00:00:00 2001 From: Ryan Ashcraft Date: Tue, 10 Nov 2026 20:58:41 -0800 Subject: [PATCH 4/5] Build CI with the runner's default Swift 6 toolchain swift-actions/setup-swift@v1 installs a Swift 5.x toolchain that cannot build the tools 6.0 manifest; the macos-15 image's default Xcode toolchain ships Swift 6.x. This is the CI half of 9ee58d0 from async-surfaces. That commit's README section documented the async streams and perform APIs, which are not being carried over, so it is left out. Co-Authored-By: Claude Fable 5.2 --- .github/workflows/CI.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 9f64a4a..e17060c 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -11,10 +11,12 @@ on: jobs: build: - runs-on: macos-latest + # The macos-15 image's default Xcode toolchain ships Swift 6.x, which the + # tools 6.0 manifest requires. swift-actions/setup-swift@v1 installs a + # Swift 5.x toolchain and cannot build this package. + runs-on: macos-15 steps: - uses: actions/checkout@v4 - - uses: swift-actions/setup-swift@v1 - name: Build run: swift build -v - name: Run tests From 4e1deef840465eca59c3d031cfdfa47b131ec42e Mon Sep 17 00:00:00 2001 From: Ryan Ashcraft Date: Tue, 10 Nov 2026 21:03:25 -0800 Subject: [PATCH 5/5] Run CI on the macos-26 image The build failed on macos-15 with "type 'CKError.Code' has no member 'participantAlreadyInvited'". That case was added in the macOS 26 SDK, and ErrorMiddleware has listed it since 4d00787, so the package no longer compiles with Xcode 16. macos-26 defaults to Xcode 26.6, which ships that SDK. Co-Authored-By: Claude Fable 5.2 --- .github/workflows/CI.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index e17060c..e86642e 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -11,10 +11,11 @@ on: jobs: build: - # The macos-15 image's default Xcode toolchain ships Swift 6.x, which the - # tools 6.0 manifest requires. swift-actions/setup-swift@v1 installs a - # Swift 5.x toolchain and cannot build this package. - runs-on: macos-15 + # Build with the runner's default Xcode rather than a separately installed + # toolchain. The image must ship the macOS 26 SDK or later: ErrorMiddleware + # switches over CKError.Code.participantAlreadyInvited, which does not + # exist in earlier SDKs, so macos-15 (Xcode 16) fails to compile. + runs-on: macos-26 steps: - uses: actions/checkout@v4 - name: Build