oficina / base Public

Let an abandoned set-up spend no minute, and Set it up go ahead

Fable's nits 5 and 6, both in AskSetup's run bookkeeping. A set-up that
ends because it no longer counts (paused, signed out, invalidated) now
gives back its attempt, so coming forward can try again at once instead
of sitting on connect for up to a minute. And the person's own Set it
up invalidates and cancels whatever set-up is in flight and starts
afresh, where the old run's "running" used to refuse it (Disconnect
then Set it up posted nothing). Regressions for both.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev aca798a9f21aa3d9533b76eeb6aeb4350050d15c parent a60baf5
3 files changed, +62 −13
  • native/apple/Sources/BaseKit/Session/AskSetup.swift +27 −13
  • native/apple/Sources/BaseKit/Session/BaseSession.swift +4 −0
  • native/apple/Tests/BaseKitTests/SessionTests.swift +31 −0
diff --git a/native/apple/Sources/BaseKit/Session/AskSetup.swift b/native/apple/Sources/BaseKit/Session/AskSetup.swift
index 8d6b3e9..a216c93 100644
--- a/native/apple/Sources/BaseKit/Session/AskSetup.swift
+++ b/native/apple/Sources/BaseKit/Session/AskSetup.swift
@@ -52,7 +52,9 @@ import RastrilloApple
/// Disconnect: whatever set-up is running must not undo it.
public func invalidate() { epoch += 1 }
private(set) var lastAttempt: Date?
- private(set) var running = false
+ /// The epoch of the run in flight, if any.
+ private var runningEpoch: Int?
+ var running: Bool { runningEpoch != nil }
/// Equipo answered not_linked: no more automatic attempts this session.
private(set) var notLinked = false
@@ -77,20 +79,31 @@ import RastrilloApple
// Base still says connect after not_linked: Ask keeps saying why.
if !force, heldOff() { return false }
if notLinked, !force, ask.phase == .connect || ask.phase == .reconnect { ask.show(.notLinked); return false }
- guard !running, wanted || (force && ask.phase != .loading) else { return false }
+ // One run at a time; but the person's own Set it up goes ahead when
+ // the run in flight was invalidated (a Disconnect, Set it up), and
+ // that one lands nothing.
+ if let inFlight = runningEpoch, !(force && inFlight != epoch) { return false }
+ guard wanted || (force && ask.phase != .loading) else { return false }
if !force {
guard !notLinked else { return false }
if let last = lastAttempt, now().timeIntervalSince(last) < 60 { return false }
}
guard let equipo = ask.equipoOrigin ?? fallbackEquipo() else { return false }
- running = true
- defer { running = false }
- lastAttempt = now()
+ let activity = ask.activity, started = epoch
+ runningEpoch = started
+ defer { if runningEpoch == started { runningEpoch = nil } }
+ let previousAttempt = lastAttempt, thisAttempt = now()
+ lastAttempt = thisAttempt
notLinked = false
// After every suspension: the account, Ask's activity (a pause, a
// sign-out) and this task must all be as they were, or nothing lands.
- let activity = ask.activity, started = epoch
func live() -> Bool { isCurrent() && epoch == started && ask.activity == activity && !ask.paused && !ask.disposed && !Task.isCancelled }
+ // A run that ends because it no longer counts spends no minute: the
+ // next trigger may try at once.
+ func abandon() -> Bool {
+ if lastAttempt == thisAttempt { lastAttempt = previousAttempt }
+ return false
+ }
var tries = 0
while tries < 2 {
tries += 1
@@ -102,37 +115,38 @@ import RastrilloApple
// failure to get a proof: nothing to show; next time.
return false
}
- guard live() else { return false }
+ guard live() else { return abandon() }
do {
let answer = try await client.connectAsk(assertion: proof.assertion, verifier: proof.verifier, auto: !force)
- guard live() else { return false }
+ guard live() else { return abandon() }
if answer.state == .notLinked { notLinked = true; ask.show(.notLinked); return false }
await ask.load()
return true
} catch .server(422, _, "failed") where tries < 2 {
- guard live() else { return false }
+ guard live() else { return abandon() }
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 }
+ guard live() else { return abandon() }
onDisconnected()
await ask.load()
return false
} catch .server(409, _, "not_linked") {
- guard live() else { return false }
+ guard live() else { return abandon() }
notLinked = true
ask.show(.notLinked)
return false
} catch {
// superseded, unavailable, Ask off, a refused session: Ask as
// Base says it is now.
- guard live() else { return false }
+ guard live() else { return abandon() }
await ask.load()
return false
}
}
- if live() { await ask.load() }
+ guard live() else { return abandon() }
+ await ask.load()
return false
}
}
diff --git a/native/apple/Sources/BaseKit/Session/BaseSession.swift b/native/apple/Sources/BaseKit/Session/BaseSession.swift
index 8a88761..c2b8479 100644
--- a/native/apple/Sources/BaseKit/Session/BaseSession.swift
+++ b/native/apple/Sources/BaseKit/Session/BaseSession.swift
@@ -171,6 +171,10 @@ import RastrilloApple
/// Set it up (the person pressed the button): now, whatever the minute.
public func setUpAsk() async {
+ // The person's own: whatever set-up is in flight gives way to it.
+ settingUp?.cancel()
+ settingUp = nil
+ askSetup?.invalidate()
askDisconnected = false
await askSetup?.runIfNeeded(force: true)
}
diff --git a/native/apple/Tests/BaseKitTests/SessionTests.swift b/native/apple/Tests/BaseKitTests/SessionTests.swift
index e0e8199..4c9ca9b 100644
--- a/native/apple/Tests/BaseKitTests/SessionTests.swift
+++ b/native/apple/Tests/BaseKitTests/SessionTests.swift
@@ -334,3 +334,34 @@ extension SessionTests {
session.end()
}
}
+
+extension SessionTests {
+ /// Fable's nit 5: a set-up abandoned in the background (its POST never
+ /// answered here) spends no minute: coming forward tries again at once.
+ func testAnAbandonedSetUpSpendsNoMinute() async throws {
+ let (session, t, ask) = try await setupHeldAtPost()
+ session.background()
+ try await Task.sleep(for: .milliseconds(100))
+ t.release("/api/v1/ask/oficina")
+ try await Task.sleep(for: .milliseconds(100))
+ t.answer("/ask/state", #"{"state":"reconnect","messages":[],"jobs":[],"cards":[],"turn":null,"status":"","cursor":0,"equipo_origin":"https://equipo.alice.example"}"#)
+ await session.foreground()
+ try await waitUntil { t.calls.filter { $0 == "/api/v1/ask/oficina" }.count == 2 }
+ _ = ask
+ session.end()
+ }
+
+ /// Fable's nit 6: Disconnect, then Set it up at once, while the old
+ /// set-up is still waiting on Base: a fresh one posts.
+ func testSetItUpGoesAheadOfAnInvalidatedRun() async throws {
+ let (session, t, _) = try await setupHeldAtPost()
+ t.answer("/ask/disconnect", status: 303, headers: ["location": "/settings"], "")
+ t.release("/ask/disconnect")
+ await session.disconnectAsk()
+ let posting = Task { await session.setUpAsk() }
+ try await waitUntil { t.calls.filter { $0 == "/api/v1/ask/oficina" }.count == 2 }
+ t.release("/api/v1/ask/oficina")
+ await posting.value
+ session.end()
+ }
+}