Docs native: a command's update carries its own changes, not parked ones
#147 re-review finding 1. Editor::command returned encode_state_as_update_v1(&before), which merges in everything the replica parks. An edit typed on an unsettled replica therefore posted the damaged duplicate to the server. Measured on Fable's 29 real damaged snapshots, the server parked it in 21; the healing replica then needed a second full download, and every other replica receiving that row would park it in turn. A replica that had accepted the server's own parking re-posted it with every edit. It now returns encode_diff_v1(&before): the structs since `before` plus the delete set, without the parked set. Snapshots still include parked data. Tests: - damaged_snapshots.rs: an edit on each damaged replica leaves the server parking nothing new. - OfflineDurabilityTests: typing straight after opening each damaged replica sends the edit, settles with one download, and leaves the server parking only what it parked before. With the old encoding, 21 of 29 fail. The core wasm is untouched: native/crdt is not among its inputs.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+53
−1
native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift+35 −0native/crdt/src/lib.rs+4 −1native/crdt/tests/damaged_snapshots.rs+14 −0
diff --git a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift| index 59f2a67..d5b7347 100644 |
| --- a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| @@ -281,6 +281,41 @@ private struct OfflineRandom: RandomNumberGenerator { |
| await reopened.close() |
| } |
| } |
| + /// #147 re-review: typing before the replica settles. Each edit's update |
| + /// carries the edit only, not the damaged duplicate the replica parks, so |
| + /// the server never parks it and one download still heals. |
| + func testTypingOnADamagedReplicaPostsOnlyTheEdit() async throws { |
| + let directory = URL(fileURLWithPath: #filePath).deletingLastPathComponent() |
| + .appendingPathComponent("../../../crdt/tests/fixtures/damaged-snapshots").standardized |
| + let names = try FileManager.default.contentsOfDirectory(atPath: directory.path).filter { $0.hasPrefix("damaged-") }.sorted() |
| + for name in names { |
| + let damaged = try Data(contentsOf: directory.appendingPathComponent(name)) |
| + let good = try Data(contentsOf: directory.appendingPathComponent(name.replacingOccurrences(of: "damaged", with: "good"))) |
| + let server = IntegratingServer(); try await server.open(good); await server.advance(to: 5) |
| + let serverParked = try await server.parked() |
| + let disk = MemoryBlobStore() |
| + let network = NativeConnectivity(source: ScriptedPathSource(.satisfied)) |
| + let account = DocsOfflineAccount(store: disk, accountID: "a", connectivity: network, transport: { server }) |
| + try storeReplica(disk, account: account, state: damaged, sequence: 5) |
| + let model = DocsModel(transport: { server }, connectivity: network, offlineAccount: account) |
| + await model.open("doc") |
| + XCTAssertTrue(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("LOCAL")]), name) |
| + let deadline = ContinuousClock.now.advanced(by: .seconds(5)) |
| + func done() async throws -> Bool { |
| + let serverText = try await server.text() |
| + return !model.unsettledParking && !model.hasUnsavedChanges && serverText.contains("LOCAL") |
| + && model.projection?.children.map(\.plainText).joined() == serverText |
| + } |
| + while try await !done() && .now < deadline { try await Task.sleep(for: .milliseconds(10)) } |
| + let finished = try await done() |
| + XCTAssertTrue(finished, "\(name): never settled with the edit sent") |
| + let parkedNow = try await server.parked() |
| + XCTAssertEqual(parkedNow, serverParked, "\(name): the server parks what an edit carried") |
| + let downloads = await server.fullDownloadCount() |
| + XCTAssertEqual(downloads, 1, name) |
| + await model.stop(); account.drainer.stop() |
| + } |
| + } |
| /// #147 finding 2: when the server itself holds a parked update, every |
| /// replica parks it too. Accepted after one catch-up from the start, it |
| /// does not make every later open download the document again. |
diff --git a/native/crdt/src/lib.rs b/native/crdt/src/lib.rs| index 9533553..93cf015 100644 |
| --- a/native/crdt/src/lib.rs |
| +++ b/native/crdt/src/lib.rs |
| @@ -110,7 +110,10 @@ impl Editor { |
| "block" | "split" | "hard_break" | "join_range" => structure::execute(&self.doc, request)?, |
| _ => operations::execute(&self.doc, request)?, |
| } |
| - Ok(self.doc.transact().encode_state_as_update_v1(&before)) |
| + // The command's own changes only. encode_state_as_update_v1 would also |
| + // merge in everything this replica parks: a damaged duplicate would |
| + // ride on every keystroke to the server (#147). |
| + Ok(self.doc.transact().encode_diff_v1(&before)) |
| } |
| } |
diff --git a/native/crdt/tests/damaged_snapshots.rs b/native/crdt/tests/damaged_snapshots.rs| index c35c9ba..45ba886 100644 |
| --- a/native/crdt/tests/damaged_snapshots.rs |
| +++ b/native/crdt/tests/damaged_snapshots.rs |
| @@ -72,3 +72,17 @@ fn parked_content_reads_the_same_after_a_rebuild() { |
| assert!(parked.is_whole()); |
| assert_eq!(parked.parked(), ""); |
| } |
| + |
| +/// An edit typed on a damaged replica (before it settles) carries only the |
| +/// edit: the server, holding its whole state, parks nothing from it. |
| +#[test] |
| +fn an_edit_on_a_damaged_replica_does_not_carry_what_it_parks() { |
| + for (name, damaged, good) in pairs() { |
| + let mut replica = open(&damaged, 99); |
| + let update = replica.command(&serde_json::json!({"op":"display_edit","at":0,"length":0,"text":"LOCAL"})).unwrap(); |
| + let server = open(&good, 97); |
| + let parked_before = server.parked(); |
| + server.apply(&update).unwrap(); |
| + assert_eq!(server.parked(), parked_before, "{name}: the server parks what the edit carried"); |
| + } |
| +} |