Handle connect offers as ask.js does
Review findings 5 to 7. A fresh /ask/state now forgets the offers that were waiting for a reply, with their timers, as ask.js load() clears offers and waiting; an obsolete offer no longer reappears with the reply. An offer whose reply never comes shows on its own after three seconds (waiting again while the reply's words stream), and a replied turn's bubble going re-checks the waiting offers, so a connect action is never hidden for good. The newest revision of each connect card is kept whether it is drawn, waiting or neither, so an older one never replaces it, open or connected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+108
−5
native/apple/Sources/BaseKit/Ask/AskModel.swift+49 −5native/apple/Tests/BaseKitTests/AskModelTests.swift+59 −0
diff --git a/native/apple/Sources/BaseKit/Ask/AskModel.swift b/native/apple/Sources/BaseKit/Ask/AskModel.swift| index 6b44e47..05b2034 100644 |
| --- a/native/apple/Sources/BaseKit/Ask/AskModel.swift |
| +++ b/native/apple/Sources/BaseKit/Ask/AskModel.swift |
| @@ -331,6 +331,11 @@ import Observation |
| guard phase.chatting else { return } |
| entries = [] |
| jobCards = [:] |
| + // ask.js load() clears its offers and their timers too. |
| + waitingOffers = [:] |
| + offerRevisions = [:] |
| + offerTimers = [] |
| + offerEpoch += 1 |
| over = [] |
| quiet = [] |
| turn = nil |
| @@ -495,12 +500,22 @@ import Observation |
| } |
| /// An open connect card ends the Assistant's newest reply; one whose |
| - /// reply is still on its way waits for it (reoffer). Connected, it |
| - /// becomes a quiet line where it is. |
| + /// reply is still on its way waits for it (reoffer), and if no words |
| + /// come for `offerWait`, shows on its own (ask.js `wait`). Connected, |
| + /// it becomes a quiet line where it is. |
| private var waitingOffers: [String: AskCard] = [:] |
| + /// The newest revision seen of each connect card, drawn, waiting or |
| + /// neither: an older one never replaces it (ask.js `offer`). |
| + private var offerRevisions: [String: Int64] = [:] |
| + /// Offers waiting on a timer, and the timers' generation: a fresh |
| + /// snapshot ends every timer. |
| + private var offerTimers: Set<String> = [] |
| + private var offerEpoch = 0 |
| + static let offerWait: Duration = .seconds(3) |
| private func offer(_ c: AskCard) { |
| - if let at = index("o:" + c.id), case .offer(let o) = entries[at], o.card.revision > c.revision { return } |
| + if let known = offerRevisions[c.id], known > c.revision { return } |
| + offerRevisions[c.id] = c.revision |
| if !c.isOpen { |
| waitingOffers[c.id] = nil |
| if let at = index("o:" + c.id) { entries[at] = .offer(Offer(card: c, connected: true)) } |
| @@ -508,12 +523,38 @@ import Observation |
| } |
| guard c.connectLink != nil else { return } |
| if onItsWay(c.turnID) { |
| - if index("o:" + c.id) == nil { waitingOffers[c.id] = c } |
| + if index("o:" + c.id) == nil { |
| + waitingOffers[c.id] = c |
| + wait(c.id) |
| + } else if case .offer(var o)? = entries.first(where: { $0.id == "o:" + c.id }), let at = index("o:" + c.id) { |
| + o.card = c |
| + entries[at] = .offer(o) |
| + } |
| return |
| } |
| + waitingOffers[c.id] = nil |
| put(c) |
| } |
| + /// After `offerWait`: the reply's words are streaming, so wait again; |
| + /// otherwise show the offer on its own. |
| + private func wait(_ id: String) { |
| + guard !offerTimers.contains(id) else { return } |
| + offerTimers.insert(id) |
| + let epoch = offerEpoch |
| + schedule(Self.offerWait) { [weak self] in |
| + guard let self, self.offerEpoch == epoch else { return } |
| + self.offerTimers.remove(id) |
| + guard let c = self.waitingOffers[id] else { return } |
| + if self.index("d:" + c.turnID) != nil, self.onItsWay(c.turnID) { |
| + self.wait(id) |
| + } else if self.index("o:" + id) == nil { |
| + self.waitingOffers[id] = nil |
| + self.put(c) |
| + } |
| + } |
| + } |
| + |
| private func onItsWay(_ turnID: String) -> Bool { |
| guard !turnID.isEmpty else { return false } |
| if entries.contains(where: { if case .message(let m) = $0 { !m.isPerson && m.turnID == turnID } else { false } }) { return false } |
| @@ -584,7 +625,10 @@ import Observation |
| private func unthink(_ t: AskTurn) { |
| guard index("t:" + t.id) != nil else { return } |
| if t.state == "replied", t.replyID.isEmpty || index("m:" + t.replyID) == nil { |
| - schedule(.seconds(5)) { [weak self] in self?.drop(t.id) } |
| + schedule(.seconds(5)) { [weak self] in |
| + self?.drop(t.id) |
| + self?.reoffer() |
| + } |
| return |
| } |
| drop(t.id) |
diff --git a/native/apple/Tests/BaseKitTests/AskModelTests.swift b/native/apple/Tests/BaseKitTests/AskModelTests.swift| index 45b1164..6097552 100644 |
| --- a/native/apple/Tests/BaseKitTests/AskModelTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/AskModelTests.swift |
| @@ -296,4 +296,63 @@ import XCTest |
| await m.stop() |
| XCTAssertEqual(fields(t.calls[3])["key"], fields(t.calls[4])["key"]) |
| } |
| + |
| + // MARK: Review findings 5 to 7: connect offers |
| + |
| + func connect(_ revision: Int64, state: String = "open", turn: String = "t5") -> AskCard { |
| + AskCard(id: "k1", revision: revision, kind: "connect", state: state, question: "Connect?", |
| + links: [.init(label: "Connect", url: URL(string: "https://fennel.base.example/ask/oficina/connect"), rel: "oficina_connect")], turnID: turn) |
| + } |
| + |
| + /// A fresh snapshot forgets offers that were waiting for a reply. |
| + func testASnapshotForgetsWaitingOffers() throws { |
| + let (m, _) = model() |
| + m.apply(Fixture.snapshot("state-ready.json")) |
| + m.apply(.turn(AskTurn(id: "t5", kind: "chat", state: "running"))) |
| + m.apply(.card(connect(1))) |
| + let json = #"{"state":"ready","messages":[{"id":"reply","role":"assistant","text":"Done.","turn_id":"t5"}],"jobs":[],"cards":[],"turn":null,"status":"","cursor":50}"# |
| + m.apply(try JSONDecoder().decode(AskSnapshot.self, from: Data(json.utf8))) |
| + XCTAssertEqual(ids(m), ["m:reply"]) |
| + for run in later { run() } |
| + XCTAssertEqual(ids(m), ["m:reply"], "nor does an old timer bring it back") |
| + } |
| + |
| + /// No reply ever comes: after the wait the offer shows on its own, and |
| + /// a replied turn's bubble going lets it show too. |
| + func testAnOfferIsNeverHiddenForGood() { |
| + let (m, _) = model() |
| + m.apply(.turn(AskTurn(id: "t5", kind: "chat", state: "running"))) |
| + m.apply(.card(connect(1))) |
| + m.apply(.turn(AskTurn(id: "t5", kind: "chat", state: "replied"))) |
| + XCTAssertEqual(ids(m), ["t:t5"]) |
| + while !later.isEmpty { later.removeFirst()() } |
| + XCTAssertEqual(ids(m), ["o:k1"]) |
| + } |
| + |
| + func testAnOfferWaitsAgainWhileItsWordsStream() { |
| + let (m, _) = model() |
| + m.apply(.turn(AskTurn(id: "t5", kind: "chat", state: "running"))) |
| + m.apply(.card(connect(1))) |
| + m.apply(.delta(AskDelta(turnID: "t5", offset: 0, text: "Connect Oficina"))) |
| + later.removeFirst()() |
| + XCTAssertFalse(ids(m).contains("o:k1"), "words are streaming: the reply is coming") |
| + XCTAssertEqual(later.count, 1) |
| + m.apply(.message(AskMessage(id: "r5", role: "assistant", text: "Connect Oficina to see it.", turnID: "t5"))) |
| + XCTAssertEqual(ids(m), ["m:r5", "o:k1"]) |
| + } |
| + |
| + /// An older revision never replaces a newer one, drawn or waiting. |
| + func testAnOfferNeverGoesBackARevision() { |
| + let (m, _) = model() |
| + m.apply(.turn(AskTurn(id: "t5", kind: "chat", state: "running"))) |
| + m.apply(.card(connect(2))) |
| + m.apply(.card(connect(1))) |
| + m.apply(.message(AskMessage(id: "r5", role: "assistant", text: "Connect.", turnID: "t5"))) |
| + guard case .offer(let o) = m.entries.last else { return XCTFail("\(m.entries)") } |
| + XCTAssertEqual(o.card.revision, 2) |
| + m.apply(.card(connect(3, state: "resolved"))) |
| + m.apply(.card(connect(2))) |
| + guard case .offer(let done) = m.entries.last else { return XCTFail() } |
| + XCTAssertTrue(done.connected, "an older open revision doesn't undo the connection") |
| + } |
| } |