Docs native: a headless send's first record accepts the server's parking
#147 re-review finding 3. When a headless send writes a document's first record, it stores the server's whole state with nothing accepted. If the server parks something, the next open then paid one full download before accepting it. The send now reads what that state parks and records it, as a first open from the server's state does. Test: the record accepts what the server parks, and the next open makes no further full download.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+30
−2
native/apple/Sources/DocsKit/DocsOfflineAccount.swift+7 −2native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift+23 −0
diff --git a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift| index 9573f4a..af23c9e 100644 |
| --- a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| +++ b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| @@ -267,15 +267,20 @@ struct DocsSentUpdates: Sendable { |
| let assigned = (200..<300).contains(reply.status) ? Int64(reply.header("Docs-Seq") ?? "") : nil |
| posted(DocsSentUpdates(key: key, sequence: assigned, entries: Set(sending.map(\.id)))) |
| try reply.requireSuccess() |
| - var state = update, sequence: Int64 = 0 |
| + var state = update, sequence: Int64 = 0, parked: String? = nil |
| if try store.read("cache/docs/\(key)/replica") == nil { |
| let initial = try await transport.responseDetails("/d/\(metadata.detail.sqid)/updates", maxBytes: 8 * 1024 * 1024) |
| try initial.requireSuccess() |
| guard let cursor = Int64(initial.header("Docs-Seq") ?? "") else { throw NativeClientError.invalidResponse } |
| state = initial.data; sequence = cursor |
| + // The server's whole state: what it parks is the server's own, and |
| + // the first record accepts it, as a first open does (#147). |
| + let reader = CRDTReplica(guid: metadata.detail.guid) |
| + do { _ = try await reader.open(update: state); parked = try await reader.parked(); await reader.close() } |
| + catch { await reader.close(); throw error } |
| } |
| // Keep sent bytes locally before ack can remove their lane files. |
| - try await replicaCache.commit(PersistedReplica(format: 1, sequence: sequence, guid: metadata.detail.guid, savedAt: Date(), state: state), key: key, generation: generation) |
| + try await replicaCache.commit(PersistedReplica(format: 1, sequence: sequence, guid: metadata.detail.guid, savedAt: Date(), state: state, parked: parked), key: key, generation: generation) |
| // More of this lane is waiting (typed during this POST): hold the |
| // drainer until the next POST is allowed, then let it reserve |
| // everything typed by then in one batch. |
diff --git a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift| index d5b7347..ffcfade 100644 |
| --- a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| @@ -339,6 +339,29 @@ private struct OfflineRandom: RandomNumberGenerator { |
| let later = await server.fullDownloadCount() |
| XCTAssertEqual(later, 2, "only the first open without a record downloads") |
| } |
| + /// #147 re-review finding 3: a headless send that writes the document's |
| + /// first record stores the server's state and accepts what it parks, as a |
| + /// first open does, so the next open does not download it again. |
| + func testAHeadlessSendWritingTheFirstRecordAcceptsTheServersParking() async throws { |
| + let (_, typed) = try await parkedReplica() |
| + let server = IntegratingServer(); try await server.open(Data()) |
| + _ = try await server.responseDetails("/d/doc/updates", method: "POST", body: typed, contentType: "application/octet-stream", maxBytes: 1 << 20) |
| + let serverParked = try await server.parked() |
| + XCTAssertFalse(serverParked.isEmpty) |
| + let disk = MemoryBlobStore() |
| + let network = NativeConnectivity(source: ScriptedPathSource(.satisfied)) |
| + while !network.isPathSatisfied { await Task.yield() } |
| + let account = DocsOfflineAccount(store: disk, accountID: "a", connectivity: network, transport: { server }) |
| + try account.saveMetadata(DocsOfflineMetadata(detail: DocumentDetail(sqid: "doc", title: "Document", guid: "offline-property", can_write: true, can_share: false)), key: "doc") |
| + let (other, _) = try await parkedReplica() |
| + _ = try await account.outbox.enqueue(lane: "doc:doc", kind: "update", payload: other) |
| + await account.drainer.drain() |
| + let record = try stored(disk) |
| + XCTAssertEqual(record.parked, serverParked) |
| + try await openUntilSettled(server, account, network, shows: "") |
| + let downloads = await server.fullDownloadCount() |
| + XCTAssertEqual(downloads, 1, "only the headless send's first record downloads") |
| + } |
| /// #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. |