oficina / docs Public

Docs native S2a: a 401's pause is its own reason, and only it is lifted

The re-minted session's 200 used to clear inputPaused whatever had set it,
so a pause from a recovery (native.recovered) or a full outbox was lifted
too (Fable on #169, finding 3). The 401 now sets sessionPaused, a reason of
its own read beside inputPaused (inputBlocked: enqueue, DocumentCommands,
the text view's editability), and the 200 clears only that.
testThe401ResumeLeavesAnotherPauseAlone was red with the old clearing.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 98e737d1d3aff2359bef3da0100bc4cc398b9d45 parent 12780cb
4 files changed, +42 −17
  • native/apple/Sources/DocsKit/DocsModel.swift +20 −13
  • native/apple/Sources/DocsKit/DocsWorkspace.swift +2 −2
  • native/apple/Sources/DocsKit/DocumentCommands.swift +1 −1
  • native/apple/Tests/DocsKitTests/DocsAccessTests.swift +19 −1
diff --git a/native/apple/Sources/DocsKit/DocsModel.swift b/native/apple/Sources/DocsKit/DocsModel.swift
index 416aa88..49493e9 100644
--- a/native/apple/Sources/DocsKit/DocsModel.swift
+++ b/native/apple/Sources/DocsKit/DocsModel.swift
@@ -19,8 +19,12 @@ import RastrilloNative
/// Spec §6 row 1: a 403 or 404 said the document was deleted or is no
/// longer shared. Left again on a later 200.
var unavailable = false
- /// Input paused by a 401 while the session is re-minted (§6 notes).
- @ObservationIgnored private var pausedForSession = false
+ /// Input paused by a 401 while the session is re-minted (§6 notes): a
+ /// reason of its own, beside `inputPaused`, so the re-minted session's
+ /// 200 clears only this one and never a pause something else set.
+ private(set) var sessionPaused = false
+ /// Typing and commands are refused: paused for any reason.
+ var inputBlocked: Bool { inputPaused || sessionPaused }
/// What the window may do with the document (§6 rows 1 and 10).
var documentAccess: DocsAccess { unavailable ? .unavailable : detail?.can_write == true ? .writable : .readOnly }
var explicitCommandRevision: UInt64 = 0
@@ -280,7 +284,7 @@ import RastrilloNative
streamTask?.cancel(); streamTask = nil
let previous = replica; replica = nil; detail = nil; projection = nil
if unavailable { unavailable = false }
- pausedForSession = false
+ if sessionPaused { sessionPaused = false }
loading = true; errorKey = nil
if let previous { await previous.close() }
guard documentEpoch == epoch else { return }
@@ -336,7 +340,7 @@ import RastrilloNative
} catch { if documentEpoch == epoch { errorKey = "docs.error.generic"; detail = nil; projection = nil } }
}
@discardableResult func enqueue(_ command: [String: JSONValue]) -> Bool {
- guard !inputPaused, detail?.can_write == true else { return false }
+ guard !inputBlocked, detail?.can_write == true else { return false }
let buffered = editing.reduce(0) { $0 + ((try? JSONEncoder().encode($1).count) ?? 0) }
guard let incoming = try? JSONEncoder().encode(command).count, incoming <= 2 * 1024 * 1024 - buffered else { errorKey = "native.pending"; return false }
var command = command
@@ -419,11 +423,11 @@ import RastrilloNative
}
if transport() != nil { offlineAccount.drainer.sessionRestored() }
offlineAccount.drainer.retryNow()
- if inputPaused {
+ if inputPaused || sessionPaused {
if let detail { await refreshAccess(detail.sqid, epoch: epoch) }
// Not while the document is gone (§6 row 1) or the session is
// being re-minted after a 401: its 200 resumes input itself.
- guard documentEpoch == epoch, detail?.can_write == true, !unavailable, !pausedForSession else { return }
+ guard documentEpoch == epoch, detail?.can_write == true, !unavailable, !sessionPaused else { return }
inputPaused = false; errorKey = nil
if editingTask == nil { editingTask = Task { await drainEdits() } }
} else { await flush() }
@@ -672,8 +676,13 @@ import RastrilloNative
guard streamTask == nil, replica != nil, !isDraft, let sqid = detail?.sqid else { return }
streamTask = Task { [weak self] in await self?.listen(sqid) }
}
+ /// Asks for the document's access again now (the stream's reopening after
+ /// a 401 does; tests call it directly).
+ func revalidateAccess() async {
+ if let sqid = detail?.sqid { await refreshAccess(sqid, epoch: documentEpoch) }
+ }
private func sessionReopened(_ sqid: String, epoch: UUID) async {
- if pausedForSession { await refreshAccess(sqid, epoch: epoch) }
+ if sessionPaused { await refreshAccess(sqid, epoch: epoch) }
}
/// Revalidate capability after stream/write rejection while retaining the draft.
private func refreshAccess(_ sqid: String, epoch: UUID) async {
@@ -696,15 +705,13 @@ import RastrilloNative
unavailable = false
if current.can_write { Task { [weak self] in await self?.retry() } }
}
- if pausedForSession {
- pausedForSession = false
- if current.can_write, localFailure == nil, laneSummary.held == 0, inputPaused { inputPaused = false }
- }
+ // Only the 401's own reason: a pause something else set
+ // (native.recovered, a full outbox, a held entry) stays.
+ if sessionPaused { sessionPaused = false }
} else if reply.status == 401 {
// The session ended, not the access: pause input and leave
// can_write and the chrome alone while the session is re-minted.
- pausedForSession = true
- if !inputPaused { inputPaused = true }
+ if !sessionPaused { sessionPaused = true }
} else if [403, 404].contains(reply.status) {
// Deleted, or no longer shared: row 1, "No longer available".
if !unavailable { unavailable = true }
diff --git a/native/apple/Sources/DocsKit/DocsWorkspace.swift b/native/apple/Sources/DocsKit/DocsWorkspace.swift
index 46f6be5..57bd9e9 100644
--- a/native/apple/Sources/DocsKit/DocsWorkspace.swift
+++ b/native/apple/Sources/DocsKit/DocsWorkspace.swift
@@ -275,7 +275,7 @@ import SwiftUI
EditorToolbar(model: model, content: text)
Divider()
#endif
- NativeDocumentTextView(content: text, generation: model.projectionGeneration, didRender: model.didRender, compositionChanged: model.compositionChanged, isEditable: model.detail?.can_write == true && !model.inputPaused, paged: paged, pendingInput: model.hasPendingInput, canRender: model.canRender, currentSelection: model.currentSelection, selection: $model.selection, edit: model.enqueue, action: { action in
+ NativeDocumentTextView(content: text, generation: model.projectionGeneration, didRender: model.didRender, compositionChanged: model.compositionChanged, isEditable: model.detail?.can_write == true && !model.inputBlocked, paged: paged, pendingInput: model.hasPendingInput, canRender: model.canRender, currentSelection: model.currentSelection, selection: $model.selection, edit: model.enqueue, action: { action in
if action == "jump" {
// Open a document… (⌘K): the jump palette as a sheet
// over this window, on the Mac too (spec §1, menus).
@@ -283,7 +283,7 @@ import SwiftUI
return true
}
return DocumentCommands(model: model, content: text).perform(action)
- }, selectionMoved: model.selectionMovedExplicitly, canAction: { action in model.detail?.can_write == true && !model.hasPendingInput && !model.composing && !model.inputPaused && (action == "undo" ? projection.canUndo : projection.canRedo) }, explicitCommandRevision: model.explicitCommandRevision, zoom: zoom, typingMarks: model.typingMarks)
+ }, selectionMoved: model.selectionMovedExplicitly, canAction: { action in model.detail?.can_write == true && !model.hasPendingInput && !model.composing && !model.inputBlocked && (action == "undo" ? projection.canUndo : projection.canRedo) }, explicitCommandRevision: model.explicitCommandRevision, zoom: zoom, typingMarks: model.typingMarks)
#if os(macOS)
.inspectorSide(showsInspector ? (layoutDirection == .rightToLeft ? .left : .right) : nil)
#endif
diff --git a/native/apple/Sources/DocsKit/DocumentCommands.swift b/native/apple/Sources/DocsKit/DocumentCommands.swift
index 9123760..60abaf5 100644
--- a/native/apple/Sources/DocsKit/DocumentCommands.swift
+++ b/native/apple/Sources/DocsKit/DocumentCommands.swift
@@ -3,7 +3,7 @@ import Foundation
@MainActor struct DocumentCommands {
let model: DocsModel
let content: NSAttributedString
- var isAvailable: Bool { model.detail?.can_write == true && !model.inputPaused }
+ var isAvailable: Bool { model.detail?.can_write == true && !model.inputBlocked }
var isEnabled: Bool {
isAvailable && !model.composing && !model.hasPendingInput
&& model.renderedGeneration == model.projectionGeneration
diff --git a/native/apple/Tests/DocsKitTests/DocsAccessTests.swift b/native/apple/Tests/DocsKitTests/DocsAccessTests.swift
index f25c7c9..0367f25 100644
--- a/native/apple/Tests/DocsKitTests/DocsAccessTests.swift
+++ b/native/apple/Tests/DocsKitTests/DocsAccessTests.swift
@@ -63,9 +63,11 @@ import XCTest
let (model, server) = try await open()
await server.answer(401)
await revalidate(model)
+ XCTAssertTrue(model.sessionPaused, "paused for the session's own reason")
+ XCTAssertFalse(model.enqueue(["op": .string("display_edit"), "at": .number(0), "length": .number(0), "text": .string("x")]))
XCTAssertEqual(model.detail?.can_write, true, "a 401 never touches can_write")
XCTAssertEqual(model.documentAccess, .writable, "and so no reader chrome")
- XCTAssertTrue(model.inputPaused, "input pauses while the session is re-minted")
+ XCTAssertTrue(model.inputBlocked, "input pauses while the session is re-minted")
// The re-minted session's 200 resumes input, can_write unchanged.
await server.answer(200, canWrite: true)
@@ -75,6 +77,22 @@ import XCTest
await model.stop()
}
+ /// The re-minted session's 200 clears only the 401's own pause: input
+ /// paused for another reason (here a recovery) stays paused.
+ func testThe401ResumeLeavesAnotherPauseAlone() async throws {
+ let (model, server) = try await open()
+ model.inputPaused = true; model.errorKey = "native.recovered"
+ await server.answer(401)
+ await model.revalidateAccess()
+ XCTAssertTrue(model.sessionPaused)
+ await server.answer(200, canWrite: true)
+ await model.revalidateAccess()
+ XCTAssertFalse(model.sessionPaused, "the 401's pause is lifted")
+ XCTAssertTrue(model.inputPaused, "the recovery's pause is not the 401's to lift")
+ XCTAssertTrue(model.inputBlocked)
+ await model.stop()
+ }
+
func testA200WithoutWriteIsViewOnlyInPlace() async throws {
let (model, server) = try await open()
model.selection = NSRange(location: 2, length: 1)