Keep a write's key until a readable answer settles it
Review finding 4: an answer Base sent but the app couldn't read (or a request cancelled once it may have reached Base) forgot the message's idempotency key, so sending the same text again could make a second message. Messages, answers and Stop now keep their key through every failure but a readable refusal, as the web's post does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+37
−3
native/apple/Sources/BaseKit/Ask/AskModel.swift+6 −3native/apple/Tests/BaseKitTests/AskModelTests.swift+31 −0
diff --git a/native/apple/Sources/BaseKit/Ask/AskModel.swift b/native/apple/Sources/BaseKit/Ask/AskModel.swift| index 28f11e6..6b44e47 100644 |
| --- a/native/apple/Sources/BaseKit/Ask/AskModel.swift |
| +++ b/native/apple/Sources/BaseKit/Ask/AskModel.swift |
| @@ -296,8 +296,9 @@ import Observation |
| /// What a refused write shows (ask.js `post`): Ask's own states stand in |
| /// for the conversation, or add their note; a 409 is stale (the next |
| /// events show how things are); anything else asks to try again. The |
| - /// key is forgotten once the outcome is known (anything but offline and |
| - /// a 502). |
| + /// key is forgotten only once a readable answer settles the outcome: |
| + /// never after an offline failure, a 502, an unreadable answer or a |
| + /// cancellation. |
| private func refusedWrite(_ error: BaseAPIError, keepsKey forget: () -> Void) { |
| switch error { |
| case .unauthenticated: |
| @@ -313,7 +314,9 @@ import Observation |
| status = BaseText.text("ask.not_sent") |
| } |
| case .badResponse: |
| - forget() |
| + // An unreadable answer (or the request cancelled once it may |
| + // have reached Base) leaves the outcome unknown: the key stays, |
| + // so sending again is the same write. |
| status = BaseText.text("ask.not_sent") |
| } |
| } |
diff --git a/native/apple/Tests/BaseKitTests/AskModelTests.swift b/native/apple/Tests/BaseKitTests/AskModelTests.swift| index def5de4..45b1164 100644 |
| --- a/native/apple/Tests/BaseKitTests/AskModelTests.swift |
| +++ b/native/apple/Tests/BaseKitTests/AskModelTests.swift |
| @@ -265,4 +265,35 @@ import XCTest |
| XCTAssertEqual(t.calls.filter { $0.hasPrefix("/ask/events") }.count, 1, "one stream, the newer load's") |
| m.pause() |
| } |
| + |
| + // MARK: Review finding 4 |
| + |
| + /// An accepted message whose answer can't be read: the outcome is |
| + /// unknown, so sending again reuses its key. So do answers and Stop. |
| + func testAnUnreadableAnswerKeepsTheKey() async { |
| + let sent = #"{"message":{"id":"m9","role":"person","text":"Hello","turn_id":"t9"},"turn":{"id":"t9","kind":"chat","state":"queued"}}"# |
| + let (m, t) = model([json(200, "<html>"), json(200, sent)]) |
| + m.apply(Fixture.snapshot("state-ready.json")) |
| + m.text = "Hello" |
| + await m.send() |
| + XCTAssertEqual(m.text, "Hello") |
| + await m.send() |
| + XCTAssertEqual(fields(t.calls[0])["key"], fields(t.calls[1])["key"]) |
| + XCTAssertEqual(m.text, "") |
| + } |
| + |
| + func testAnswersAndStopKeepTheirKeysUntilSettled() async { |
| + let (m, t) = model([json(200, "not json"), .fail(CancellationError()), json(200, #"{"card":null,"message":null,"turn":null}"#), |
| + json(200, "oops"), json(200, #"{"state":"ready"}"#)]) |
| + m.apply(Fixture.snapshot("state-ready.json")) |
| + guard case .job(let j2) = m.entries[3], let card = j2.open.first else { return XCTFail() } |
| + await m.answer(card, card.options[0]) |
| + await m.answer(card, card.options[0]) |
| + await m.answer(card, card.options[0]) |
| + XCTAssertEqual(Set(t.calls.prefix(3).map { fields($0)["key"]! }).count, 1) |
| + m.apply(.turn(AskTurn(id: "t8", kind: "chat", state: "running"))) |
| + await m.stop() |
| + await m.stop() |
| + XCTAssertEqual(fields(t.calls[3])["key"], fields(t.calls[4])["key"]) |
| + } |
| } |