Docs native: compare accepted parking by containment
#147 re-review finding 4. Acceptance compared the whole `parked` description, so if part of an accepted set later integrated, the rest read as new and cost one more full download. Parking is now accepted when every parked struct range and every parked deletion range it describes is covered by the accepted ones (CRDTReplica.parking(_:within:)). Test: containment, a struct versus a deletion, unknown clients, and ranges split across adjacent accepted ranges.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+44
−2
native/apple/Sources/DocsKit/CRDTReplica.swift+33 −2native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift+11 −0
diff --git a/native/apple/Sources/DocsKit/CRDTReplica.swift b/native/apple/Sources/DocsKit/CRDTReplica.swift| index e11be20..b7b62fd 100644 |
| --- a/native/apple/Sources/DocsKit/CRDTReplica.swift |
| +++ b/native/apple/Sources/DocsKit/CRDTReplica.swift |
| @@ -98,8 +98,39 @@ actor CRDTReplica { |
| /// True when the replica parks something it has not accepted: damage, or |
| /// something the server has that this replica lacks. |
| func hasUnacceptedParking() throws -> Bool { |
| - let parked = try parked() |
| - return !parked.isEmpty && parked != acceptedParking |
| + !Self.parking(try parked(), within: acceptedParking) |
| + } |
| + /// Whether every parked struct and deletion `parked` describes is also in |
| + /// `accepted` (the engine's `parked` format: "client a-b c-d;" per client, |
| + /// structs, then "|" and deletions). Containment, not equality: when part |
| + /// of an accepted set later integrates, the rest stays accepted (#147). |
| + nonisolated static func parking(_ parked: String, within accepted: String) -> Bool { |
| + func ranges(_ description: String) -> [String: [(UInt64, UInt64)]] { |
| + var out: [String: [(UInt64, UInt64)]] = [:] |
| + let halves = description.split(separator: "|", omittingEmptySubsequences: false) |
| + for (index, half) in halves.enumerated() { |
| + for client in half.split(separator: ";") { |
| + let words = client.split(separator: " ") |
| + guard let id = words.first else { continue } |
| + let key = (index == 0 ? "s" : "d") + id |
| + for word in words.dropFirst() { |
| + let bounds = word.split(separator: "-").compactMap { UInt64($0) } |
| + if bounds.count == 2 { out[key, default: []].append((bounds[0], bounds[1])) } |
| + } |
| + } |
| + } |
| + return out |
| + } |
| + let have = ranges(accepted) |
| + for (key, wanted) in ranges(parked) { |
| + let covering = (have[key] ?? []).sorted { $0.0 < $1.0 } |
| + for (start, end) in wanted { |
| + var reached = start |
| + for (from, to) in covering where from <= reached && to > reached { reached = to } |
| + if reached < end { return false } |
| + } |
| + } |
| + return true |
| } |
| /// Once caught up from the start: rebuild the authority from its own |
| /// snapshot and accept what still parks. Applying the server's state does |
diff --git a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift| index ffcfade..ec3556f 100644 |
| --- a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| @@ -362,6 +362,17 @@ private struct OfflineRandom: RandomNumberGenerator { |
| let downloads = await server.fullDownloadCount() |
| XCTAssertEqual(downloads, 1, "only the headless send's first record downloads") |
| } |
| + /// #147 re-review finding 4: accepted parking is compared as sets of id |
| + /// ranges, so part of it integrating later leaves the rest accepted. |
| + func testAcceptedParkingIsComparedByContainment() { |
| + XCTAssertTrue(CRDTReplica.parking("", within: "")) |
| + XCTAssertTrue(CRDTReplica.parking("7 3-5;", within: "7 0-10;9 0-2;")) |
| + XCTAssertTrue(CRDTReplica.parking("7 3-5;|8 1-2;", within: "7 0-4 4-10;|8 0-3;")) |
| + XCTAssertFalse(CRDTReplica.parking("7 3-12;", within: "7 0-10;")) |
| + XCTAssertFalse(CRDTReplica.parking("9 0-1;", within: "7 0-10;")) |
| + XCTAssertFalse(CRDTReplica.parking("|7 3-5;", within: "7 0-10;"), "a parked deletion is not a parked struct") |
| + XCTAssertFalse(CRDTReplica.parking("7 0-1;", within: "")) |
| + } |
| /// #147 finding 5: a headless send's merge can leave the record parking a |
| /// sent update typed against a change the record lacks. Its sequence is |
| /// kept; the next open's ordinary catch-up supplies the change. |