Docs native S1a: a report deferred from attach belongs to its document (#170, final 2)
Fable's final check, finding 2. attach() draws at once and tells the model on the next turn; if the model reset the surface in between (another document opened), the old document's image sources and generation still reached it. The deferred report now checks that no reset happened and the surface still shows that generation. SurfaceLifecycleTests.testADeferredReportIsDroppedAfterAReset resets in the same turn as the attach; it failed before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+31
−1
native/apple/Sources/DocsKit/DocumentSurface.swift+10 −1native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift+21 −0
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift| index 1961a09..9d26481 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -67,6 +67,8 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| /// Publications handed to `apply`, drawn or not (tests: "no later |
| /// publication"). |
| private(set) var publicationsReceived = 0 |
| + /// Moves on every reset: a report deferred to the next turn checks it. |
| + private var resets: UInt64 = 0 |
| /// Tests only: runs as a publication arrives, before it is applied. |
| var willApply: ((Publication) -> Void)? |
| @@ -124,6 +126,7 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| /// keeps the same host, which is not rebuilt to attach it again; a |
| /// rebuilt host's new view is drawn whole by `attach`. |
| func reset() { |
| + resets &+= 1 |
| appliedRevision = 0; generation = 0; newest = nil |
| pendingDirty = []; fullDiff = false |
| entries = [:]; order = []; sourceCounts = [:]; images = [:] |
| @@ -235,7 +238,13 @@ typealias DocsStorageEditActions = NSTextStorage.EditActions |
| // turn: no observed state is assigned during a view update. |
| let rendered = newest.generation |
| if notifyLater { |
| - Task { @MainActor [weak self] in self?.report(sources: sources); self?.hooks.didRender(rendered) } |
| + // Only if the surface still shows that draw on the next turn: |
| + // a reset in between (another document) makes it the old one's. |
| + let resets = self.resets |
| + Task { @MainActor [weak self] in |
| + guard let self, self.resets == resets, self.generation == rendered else { return } |
| + self.report(sources: sources); self.hooks.didRender(rendered) |
| + } |
| } else { report(sources: sources); hooks.didRender(rendered) } |
| } |
diff --git a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift| index 41b25af..ebf6f6c 100644 |
| --- a/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/SurfaceLifecycleTests.swift |
| @@ -54,4 +54,25 @@ import XCTest |
| for _ in 0..<20 where after.count < 2 { try await Task.sleep(for: .milliseconds(5)) } |
| XCTAssertEqual(Set(after), ["didRender", "imageSources"], "the model hears both on the next turn") |
| } |
| + |
| + /// A report deferred from an attach belongs to the document drawn then: |
| + /// if the model resets the surface (another document opens) in the same |
| + /// turn, the old document's image sources and generation never reach it |
| + /// (Fable on #170, final check, finding 2). |
| + func testADeferredReportIsDroppedAfterAReset() async throws { |
| + let image = CRDTNode(id: "i", type: "paragraph", children: [CRDTNode(id: "img", type: "image", attrs: ["src": "https://example.invalid/a.png", "alt": "A"])]) |
| + let surface = DocumentSurface() |
| + var sources: [[String]] = [], rendered: [UInt64] = [] |
| + var hooks = DocumentSurface.Hooks() |
| + hooks.didRender = { rendered.append($0) } |
| + hooks.imageSourcesChanged = { sources.append($0) } |
| + surface.hooks = hooks |
| + surface.apply(DocumentSurface.Publication(generation: 3, projection: SurfaceFixture.projection([image]), revision: 1, dirty: nil)) |
| + let text = DocsTextView(frame: CGRect(x: 0, y: 0, width: 500, height: 700)) |
| + surface.attach(text) |
| + surface.reset() |
| + for _ in 0..<10 { try await Task.sleep(for: .milliseconds(5)) } |
| + XCTAssertEqual(rendered, [], "the old document's generation is not reported after the reset") |
| + XCTAssertEqual(sources.last ?? [], [], "the last image sources the model hears are the reset's, none") |
| + } |
| } |