Guard the workspace's init itself, and say what the loop really was
Fable's review (APPROVE, 3 low): the window body tracked what the old init read and then mutated (the drainer's backoff via retryNow, its session), not only the banner's read of the supplier. The comment now says an init may neither read-then-mutate nor mutate observed state, and a new test builds DocsWorkspace for a known account inside observation tracking, as a window body does. It fails on the original code.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+25
−5
native/apple/Sources/DocsKit/DocsOfflineAccount.swift+7 −5native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift+18 −0
diff --git a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift| index b36026c..d76c85c 100644 |
| --- a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| +++ b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| @@ -21,11 +21,13 @@ struct DocsOfflineMetadata: Codable { |
| @ObservationIgnored private var supplier: @MainActor () -> NativeTransport? |
| private(set) var drainer: OutboxDrainer! |
| /// The account's shared store, outbox and drainer, made on first use. |
| - /// Called from a view's initialiser, so it must change no observed |
| - /// state: a workspace's init runs on every body pass of its window, and |
| - /// a mutation here re-ran that body, for ever (Docs 0.1.7/0.1.8 spun the |
| - /// main thread at launch). A workspace calls `resume(transport:)` when it |
| - /// appears instead. |
| + /// Called from a view's initialiser, which runs inside its window's body |
| + /// on every pass: anything observed it reads joins that body's |
| + /// dependencies, and anything observed it mutates (the drainer's |
| + /// backoff, its session, this supplier) invalidates them. Docs |
| + /// 0.1.7/0.1.8 did both here, so the body re-ran for ever and the main |
| + /// thread spun at launch. So: a lookup, nothing else. A workspace calls |
| + /// `resume(transport:)` from its `.task` instead. |
| static func account(_ id: String, origin: URL, fallback: NativeTransport, transport: (@MainActor () -> NativeTransport?)?) -> DocsOfflineAccount { |
| if let existing = accounts[id] { return existing } |
| let root = accountsRoot.appendingPathComponent(id) |
diff --git a/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift b/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift| index 0307d09..e3a42ef 100644 |
| --- a/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift |
| @@ -42,6 +42,24 @@ import XCTest |
| XCTAssertFalse(changed.withLock { $0 }, "a lookup made from a view's init must not invalidate the view") |
| } |
| + func testBuildingAWorkspaceForAKnownAccountChangesNothingItsWindowWatches() { |
| + let server = ScriptedTransport([]) |
| + let (id, offline) = knownAccount({ server }) |
| + defer { DocsOfflineAccount.accounts[id] = nil } |
| + let client = NativeHTTP(origin: URL(string: "https://docs.example")!) |
| + let changed = Mutex(false) |
| + // As a window's body does: build the workspace (its init reads |
| + // whatever it reads), then build it again on the next pass. |
| + withObservationTracking { |
| + _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) |
| + _ = offline.drainer.status() |
| + } onChange: { changed.withLock { $0 = true } } |
| + for _ in 0..<3 { |
| + _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) |
| + } |
| + XCTAssertFalse(changed.withLock { $0 }, "a workspace's init must not invalidate the window that builds it") |
| + } |
| + |
| func testResumeRestartsAnOutboxStoppedByTheEndedSession() async throws { |
| var supplied: NativeTransport? = nil |
| let (id, offline) = knownAccount({ supplied }) |