Keep Disconnect on Base, for every device
Fable's re-review finding 2: the device's own flag couldn't stop the
member's other device setting Ask up again after a Disconnect. Against
Base's contract (ask-disconnect-sticks, 4b1c585): an automatic set-up
posts "auto": true and stops on 409 {"state":"disconnected"}, with Ask
on Connect; the person's own Set up posts no auto, which clears it; and
/ask/state's "disconnected" decides whether the app may set Ask up by
itself. The device's flag is now only a cache of Base's word, used where
a Base doesn't say. Regressions: auto on automatic set-ups and not on
the person's own, 409 disconnected stops without retrying, and another
device's Disconnect holds while Base's "false" beats a stale cache.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+103
−12
native/apple/Sources/BaseKit/API/Client.swift+8 −3native/apple/Sources/BaseKit/API/Models.swift+5 −1native/apple/Sources/BaseKit/Ask/AskModel.swift+3 −0native/apple/Sources/BaseKit/Session/AskSetup.swift+14 −2native/apple/Sources/BaseKit/Session/BaseSession.swift+10 −1native/apple/Tests/BaseKitTests/AskSetupTests.swift+37 −4native/apple/Tests/BaseKitTests/ClientTests.swift+1 −1native/apple/Tests/BaseKitTests/SessionTests.swift+25 −0
diff --git a/native/apple/Sources/BaseKit/API/Client.swift b/native/apple/Sources/BaseKit/API/Client.swift| index 353d5d7..f0b733d 100644 |
| --- a/native/apple/Sources/BaseKit/API/Client.swift |
| +++ b/native/apple/Sources/BaseKit/API/Client.swift |
| @@ -62,9 +62,14 @@ public struct BaseClient: Sendable { |
| } |
| /// `POST /api/v1/ask/oficina` (Task 7): connects Ask with an Oficina ID |
| - /// assertion addressed to Equipo. |
| - public func connectAsk(assertion: String, verifier: String) async throws(BaseAPIError) -> AskSetupAnswer { |
| - let body = try? JSONSerialization.data(withJSONObject: ["assertion": assertion, "verifier": verifier]) |
| + /// assertion addressed to Equipo. `auto`: the app set Ask up by itself |
| + /// (at sign-in, a renewal, a reconnect), which Base refuses with |
| + /// `409 {"state":"disconnected"}` while the member has disconnected; |
| + /// the person's own Set up sends none, and clears that. |
| + public func connectAsk(assertion: String, verifier: String, auto: Bool = false) async throws(BaseAPIError) -> AskSetupAnswer { |
| + var fields: [String: Any] = ["assertion": assertion, "verifier": verifier] |
| + if auto { fields["auto"] = true } |
| + let body = try? JSONSerialization.data(withJSONObject: fields) |
| return try await call("/api/v1/ask/oficina", method: "POST", body: body, contentType: "application/json") |
| } |
diff --git a/native/apple/Sources/BaseKit/API/Models.swift b/native/apple/Sources/BaseKit/API/Models.swift| index 625c270..4592766 100644 |
| --- a/native/apple/Sources/BaseKit/API/Models.swift |
| +++ b/native/apple/Sources/BaseKit/API/Models.swift |
| @@ -297,8 +297,11 @@ public struct AskSnapshot: Decodable, Sendable, Equatable { |
| public var cursor: Int64 |
| public var renewAfter: Date? |
| public var equipoOrigin: URL? |
| + /// The member disconnected Ask (on any client) and hasn't set it up |
| + /// again: no set-up by itself. Nil from a Base that doesn't say. |
| + public var disconnected: Bool? |
| enum CodingKeys: String, CodingKey { |
| - case state, messages, jobs, cards, turn, status, cursor |
| + case state, messages, jobs, cards, turn, status, cursor, disconnected |
| case renewAfter = "renew_after", equipoOrigin = "equipo_origin" |
| } |
| public init(from decoder: Decoder) throws { |
| @@ -308,6 +311,7 @@ public struct AskSnapshot: Decodable, Sendable, Equatable { |
| turn = c.optional(.turn).flatMap { (t: AskTurn) in t.id.isEmpty ? nil : t } |
| status = c.value(.status, ""); cursor = c.value(.cursor, 0) |
| renewAfter = c.date(.renewAfter); equipoOrigin = c.webURL(.equipoOrigin) |
| + disconnected = c.optional(.disconnected) |
| } |
| } |
diff --git a/native/apple/Sources/BaseKit/Ask/AskModel.swift b/native/apple/Sources/BaseKit/Ask/AskModel.swift| index cdf5fc7..a8658f5 100644 |
| --- a/native/apple/Sources/BaseKit/Ask/AskModel.swift |
| +++ b/native/apple/Sources/BaseKit/Ask/AskModel.swift |
| @@ -85,6 +85,8 @@ import Observation |
| public private(set) var status = "" |
| /// When Ask's Oficina ID key wants renewing (Task 7), and where Equipo is. |
| public private(set) var renewAfter: Date? |
| + /// Base says the member disconnected Ask (nil: it doesn't say). |
| + public private(set) var disconnected: Bool? |
| public private(set) var equipoOrigin: URL? |
| /// The composer's text. |
| public var text = "" |
| @@ -359,6 +361,7 @@ import Observation |
| /// Draws `/ask/state` afresh (ask.js `load`). |
| func apply(_ s: AskSnapshot) { |
| + disconnected = s.disconnected |
| phase = s.state |
| renewAfter = s.renewAfter |
| if let origin = s.equipoOrigin { equipoOrigin = origin } |
diff --git a/native/apple/Sources/BaseKit/Session/AskSetup.swift b/native/apple/Sources/BaseKit/Session/AskSetup.swift| index 73adc33..8d6b3e9 100644 |
| --- a/native/apple/Sources/BaseKit/Session/AskSetup.swift |
| +++ b/native/apple/Sources/BaseKit/Session/AskSetup.swift |
| @@ -22,6 +22,8 @@ import RastrilloApple |
| /// - The kit's CancellationError (the sign-in changed while Home answered, |
| /// its own silent re-sign-in after a 401 included): nothing shown, tried |
| /// again at the next foreground or state load. |
| +/// - `409 disconnected` (an automatic set-up while the member has |
| +/// disconnected, on any client): stop; Ask shows Set up. |
| /// - Anything else: Ask stays as Base reports it; tried again later. |
| /// |
| /// Never more than once a minute, whatever triggers it, but for a person |
| @@ -39,8 +41,11 @@ import RastrilloApple |
| /// hasn't changed or signed out). |
| var isCurrent: () -> Bool = { true } |
| /// The person disconnected Ask: no set-up by itself until they set it |
| - /// up again (`force`) or sign in afresh. |
| + /// up again (`force`) or sign in afresh. Base's word (`/ask/state`'s |
| + /// `disconnected`) when it says; the device's cache otherwise. |
| var heldOff: () -> Bool = { false } |
| + /// Base answered an automatic set-up `409 disconnected`. |
| + var onDisconnected: () -> Void = {} |
| /// Bumped by `invalidate()`: a set-up already running lands nothing. |
| private var epoch = 0 |
| @@ -99,7 +104,7 @@ import RastrilloApple |
| } |
| guard live() else { return false } |
| do { |
| - let answer = try await client.connectAsk(assertion: proof.assertion, verifier: proof.verifier) |
| + let answer = try await client.connectAsk(assertion: proof.assertion, verifier: proof.verifier, auto: !force) |
| guard live() else { return false } |
| if answer.state == .notLinked { notLinked = true; ask.show(.notLinked); return false } |
| await ask.load() |
| @@ -107,6 +112,13 @@ import RastrilloApple |
| } catch .server(422, _, "failed") where tries < 2 { |
| guard live() else { return false } |
| continue |
| + } catch .server(409, _, "disconnected") { |
| + // The member disconnected (here or on another device): stop, |
| + // and let Ask show Set up. |
| + guard live() else { return false } |
| + onDisconnected() |
| + await ask.load() |
| + return false |
| } catch .server(409, _, "not_linked") { |
| guard live() else { return false } |
| notLinked = true |
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift| index d56beef..92d3162 100644 |
| --- a/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| +++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift |
| @@ -121,7 +121,16 @@ import RastrilloApple |
| return try await self.assertion(equipo) |
| }, fallbackEquipo: { [weak self] in self?.me?.equipoOrigin }) |
| setup.isCurrent = { [weak self] in self?.generation == epoch } |
| - setup.heldOff = { [weak self] in self?.askDisconnected ?? true } |
| + setup.heldOff = { [weak self, weak ask] in |
| + guard let self else { return true } |
| + // Base's word wins, and the device's cache follows it. |
| + if let said = ask?.disconnected { |
| + if self.askDisconnected != said { self.askDisconnected = said } |
| + return said |
| + } |
| + return self.askDisconnected |
| + } |
| + setup.onDisconnected = { [weak self] in self?.askDisconnected = true } |
| ask.onNeedsSetup = { [weak self] in |
| guard let self, self.generation == epoch else { return } |
| self.startSetup() |
diff --git a/native/apple/Tests/BaseKitTests/AskSetupTests.swift b/native/apple/Tests/BaseKitTests/AskSetupTests.swift| index 52b33aa..200e366 100644 |
| --- a/native/apple/Tests/BaseKitTests/AskSetupTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/AskSetupTests.swift |
| @@ -35,8 +35,10 @@ import XCTest |
| XCTAssertTrue(done) |
| XCTAssertEqual(audiences, [equipo], "the assertion is addressed to Equipo") |
| XCTAssertEqual(paths(t).prefix(3), ["/ask/state", "/api/v1/ask/oficina", "/ask/state"]) |
| - let body = try JSONSerialization.jsonObject(with: t.calls[1].body!) as? [String: String] |
| - XCTAssertEqual(body, ["assertion": "assertion-1", "verifier": "verifier-1"]) |
| + let body = try XCTUnwrap(JSONSerialization.jsonObject(with: t.calls[1].body!) as? [String: Any]) |
| + XCTAssertEqual(body["assertion"] as? String, "assertion-1") |
| + XCTAssertEqual(body["verifier"] as? String, "verifier-1") |
| + XCTAssertEqual(body["auto"] as? Bool, true, "set up by the app itself") |
| XCTAssertEqual(ask.phase, .ready) |
| ask.pause() |
| } |
| @@ -70,8 +72,8 @@ import XCTest |
| _ = await setup.runIfNeeded() |
| XCTAssertEqual(audiences.count, 2) |
| XCTAssertEqual(paths(t), ["/ask/state", "/api/v1/ask/oficina", "/api/v1/ask/oficina", "/ask/state"]) |
| - let second = try JSONSerialization.jsonObject(with: t.calls[2].body!) as? [String: String] |
| - XCTAssertEqual(second?["assertion"], "assertion-2", "a fresh assertion") |
| + let second = try JSONSerialization.jsonObject(with: t.calls[2].body!) as? [String: Any] |
| + XCTAssertEqual(second?["assertion"] as? String, "assertion-2", "a fresh assertion") |
| } |
| func testSupersededIsNotRetriedAndTheStateDecides() async { |
| @@ -126,4 +128,35 @@ import XCTest |
| XCTAssertEqual(ask.phase, .ready) |
| ask.pause() |
| } |
| + |
| + // MARK: Disconnect, kept by Base (Fable's re-review finding 2) |
| + |
| + /// A set-up the app runs by itself says so (`"auto": true`); the |
| + /// person's own Set up doesn't. |
| + func testAutomaticSetUpsSayAuto() async throws { |
| + let (setup, ask, t) = await make([state("connect"), json(200, #"{"state":"ready"}"#), state("connect"), |
| + json(200, #"{"state":"ready"}"#), state("ready"), .hang]) |
| + _ = await setup.runIfNeeded() |
| + let auto = try JSONSerialization.jsonObject(with: t.calls[1].body!) as? [String: Any] |
| + XCTAssertEqual(auto?["auto"] as? Bool, true) |
| + _ = await setup.runIfNeeded(force: true) |
| + let own = try JSONSerialization.jsonObject(with: t.calls[3].body!) as? [String: Any] |
| + XCTAssertNil(own?["auto"], "the person's own Set up is explicit") |
| + ask.pause() |
| + } |
| + |
| + /// 409 disconnected: stop, no retry, Ask shows Set up. |
| + func testDisconnectedStopsAnAutomaticSetUp() async { |
| + var told = 0 |
| + let (setup, ask, t) = await make([state("connect"), json(409, #"{"state":"disconnected"}"#), |
| + json(200, #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"disconnected":true}"#)]) |
| + setup.onDisconnected = { told += 1 } |
| + _ = await setup.runIfNeeded() |
| + XCTAssertEqual(told, 1) |
| + XCTAssertEqual(audiences.count, 1, "no retry") |
| + XCTAssertEqual(paths(t), ["/ask/state", "/api/v1/ask/oficina", "/ask/state"]) |
| + XCTAssertEqual(ask.phase, .connect) |
| + XCTAssertEqual(ask.disconnected, true) |
| + XCTAssertEqual(AskActions.note(ask.phase).way, .here("ask.connect.button"), "Set up shows") |
| + } |
| } |
diff --git a/native/apple/Tests/BaseKitTests/ClientTests.swift b/native/apple/Tests/BaseKitTests/ClientTests.swift| index 3796c00..55c497a 100644 |
| --- a/native/apple/Tests/BaseKitTests/ClientTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/ClientTests.swift |
| @@ -94,7 +94,7 @@ final class ClientTests: XCTestCase { |
| XCTAssertEqual(fields(t.calls[1]), ["key": "k3"]) |
| XCTAssertEqual(t.calls[2].path, "/api/v1/ask/oficina") |
| let body = try JSONSerialization.jsonObject(with: t.calls[2].body!) as? [String: String] |
| - XCTAssertEqual(body, ["assertion": "a", "verifier": "v"]) |
| + XCTAssertEqual(body, ["assertion": "a", "verifier": "v"], "explicit: no auto") |
| } |
| func testReadsDecodeTolerantly() async throws { |
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift| index 3683113..f805c51 100644 |
| --- a/native/apple/Tests/BaseKitTests/SessionTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/SessionTests.swift |
| @@ -290,3 +290,28 @@ extension SessionTests { |
| session.end() |
| } |
| } |
| + |
| +extension SessionTests { |
| + /// Fable's re-review finding 2: Disconnect on one device holds on |
| + /// another, because Base says so; and Base saying it no longer holds |
| + /// beats the device's stale cache. |
| + func testAnotherDevicesDisconnectHolds() async throws { |
| + let t = gated("alice", "Alice's", "Hello") |
| + t.answer("/ask/state", #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"equipo_origin":"https://equipo.alice.example","disconnected":true}"#) |
| + t.answer("/api/v1/ask/oficina", #"{"state":"ready"}"#) |
| + for path in ["/api/v1/me", "/api/v1/home", "/ask/state", "/api/v1/ask/oficina"] { t.release(path) } |
| + let phone = BaseSession(oficina: memoryOficina(), defaults: freshDefaults()) |
| + phone.assertion = { _ in ("a", "v") } |
| + phone.begin(identity: "id-alice", origin: URL(string: "https://alice.example")!, transport: t) |
| + await phone.refreshing?.value |
| + await phone.foreground() |
| + XCTAssertFalse(t.calls.contains("/api/v1/ask/oficina"), "Disconnect on the Mac holds on the iPhone") |
| + XCTAssertTrue(phone.askDisconnected, "the device's cache follows Base") |
| + // Set up again elsewhere: Base says so, and the stale cache gives way. |
| + t.answer("/ask/state", #"{"state":"connect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"equipo_origin":"https://equipo.alice.example","disconnected":false}"#) |
| + phone.askDisconnected = true |
| + await phone.foreground() |
| + XCTAssertTrue(t.calls.contains("/api/v1/ask/oficina")) |
| + phone.end() |
| + } |
| +} |