mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
Cursor ACP sessions can fail mid-turn with a transient model error that the agent reports inline rather than as an RPC rejection. HAPI treated those turns as successful, so the session went quiet with no banner, no push, and no way to resume without retyping the prompt. This adds the full path: - detect: classify inline/stderr model errors from the Cursor ACP stream, requiring strong signatures before typing a failure as a model error - surface: persist `lastModelError` on session metadata; render a chat banner and a session-list indicator, with the prompt text kept out of list/SSE summaries so they never ship up to 32 KB of user prompt - notify: deliver a push for an unacknowledged error, watermarked on the hub copy so a restart does not re-page the same eventId - bridge & retry: offer a one-shot recovery prompt that replays the failed turn, bound to the originating eventId and gated so a real user turn always supersedes it. Auto-bridge is opt-in and defaults to OFF, persisted in hub settings and fanned out to live Cursor CLIs. Re-cut onto current upstream/main. The branch had accumulated 108 iterative hardening commits against a 289-commit-older base; replaying them individually would have re-resolved the same conflicts ~100 times. The pre-re-cut tip was 2c28a1365 if the granular history is ever needed. Absorbing upstream required adapting to three refactors that landed meanwhile: - `MessageQueue2` grew an `internal?: boolean` control-message flag upstream while this branch added richer `internal` provenance. Unified on a tagged `QueueItemInternal` union (`control` | `model-error-bridge`) so one field carries one concept. `hasPendingNonBridgeTurn()` now treats *any* internal item as non-user: otherwise a queued `/auto-review` control slash would have counted as a user turn and wrongly superseded a live bridge. - `AcpSdkBackend.prompt()` gained a `timing` callback upstream; merged it with this branch's `shouldSend` gate into a single options object. - The hub settings route moved to a unified `updateSettings` transaction. `autoBridgeTransientModelErrors` is applied outside that transaction because SyncEngine owns its CLI fan-out and rollback under its own lock. via [HAPI](https://hapi.run) Co-authored-by: HAPI <noreply@hapi.run>
202 lines
8.0 KiB
Swift
202 lines
8.0 KiB
Swift
import Foundation
|
|
import HapiClient
|
|
import Testing
|
|
|
|
@Suite("Push payload parsing")
|
|
struct PushPayloadTests {
|
|
private let fullFields: [String: String] = [
|
|
"type": "permission-request",
|
|
"sessionId": "sess-1",
|
|
"sessionName": "claude - hapi",
|
|
"url": "/sessions/sess-1",
|
|
"title": "Permission needed",
|
|
"body": "Bash: rm -rf build",
|
|
"requestId": "req-9",
|
|
"severity": "warning",
|
|
"contractVersion": "1",
|
|
]
|
|
|
|
@Test func parsesEveryContractField() throws {
|
|
let payload = try #require(PushPayload.parse(dictionary: fullFields))
|
|
#expect(payload.type == .permissionRequest)
|
|
#expect(payload.rawType == "permission-request")
|
|
#expect(payload.sessionId == "sess-1")
|
|
#expect(payload.sessionName == "claude - hapi")
|
|
#expect(payload.url == "/sessions/sess-1")
|
|
#expect(payload.title == "Permission needed")
|
|
#expect(payload.body == "Bash: rm -rf build")
|
|
#expect(payload.requestId == "req-9")
|
|
#expect(payload.severity == .warning)
|
|
#expect(payload.contractVersion == "1")
|
|
#expect(payload.supportsActions)
|
|
#expect(payload.categoryIdentifier == "permission-request")
|
|
}
|
|
|
|
@Test func missingSessionIdDropsTheMessage() {
|
|
var fields = fullFields
|
|
fields["sessionId"] = nil
|
|
#expect(PushPayload.parse(dictionary: fields) == nil)
|
|
fields["sessionId"] = " "
|
|
#expect(PushPayload.parse(dictionary: fields) == nil)
|
|
}
|
|
|
|
@Test func permissionRequestWithoutRequestIdHasNoActions() throws {
|
|
var fields = fullFields
|
|
fields["requestId"] = nil
|
|
let payload = try #require(PushPayload.parse(dictionary: fields))
|
|
#expect(!payload.supportsActions)
|
|
#expect(payload.categoryIdentifier == nil)
|
|
}
|
|
|
|
@Test func inputRequestShowsTheQuestionButNeverOffersApprovalOrReply() throws {
|
|
var fields = fullFields
|
|
fields["type"] = "input-request"
|
|
fields["title"] = "Codex needs your input"
|
|
fields["body"] = "需要检查安全性吗?\n+1 more question\n查看 PR #1842 的改动"
|
|
fields["severity"] = "info"
|
|
// Ready summaries must not override the question, even if one is present.
|
|
fields["notifySummary"] = #"{"summary":"Unrelated summary"}"#
|
|
for version in ["1", "2"] {
|
|
fields["contractVersion"] = version
|
|
let payload = try #require(PushPayload.parse(dictionary: fields))
|
|
#expect(payload.type == .inputRequest)
|
|
#expect(payload.requestId == "req-9")
|
|
#expect(payload.url == "/sessions/sess-1")
|
|
#expect(payload.displayTitle == fields["title"])
|
|
#expect(payload.displayBody == fields["body"])
|
|
#expect(!payload.supportsActions)
|
|
#expect(payload.categoryIdentifier == nil)
|
|
let plaintext = try JSONSerialization.data(withJSONObject: fields)
|
|
#expect(PushPayload.parse(plaintext: plaintext) == payload)
|
|
}
|
|
}
|
|
|
|
@Test func unknownTypeDegradesToPlainNotification() throws {
|
|
let payload = try #require(PushPayload.parse(dictionary: [
|
|
"type": "hologram",
|
|
"sessionId": "s1",
|
|
"title": "T",
|
|
"body": "B",
|
|
"contractVersion": "1",
|
|
]))
|
|
#expect(payload.type == nil)
|
|
#expect(payload.rawType == "hologram")
|
|
#expect(!payload.supportsActions)
|
|
#expect(payload.categoryIdentifier == nil)
|
|
#expect(payload.displayTitle == "T")
|
|
#expect(payload.displayBody == "B")
|
|
}
|
|
|
|
@Test func unknownContractVersionDisablesActionsButKeepsContent() throws {
|
|
var fields = fullFields
|
|
fields["contractVersion"] = "2"
|
|
let payload = try #require(PushPayload.parse(dictionary: fields))
|
|
#expect(!payload.isKnownContractVersion)
|
|
#expect(!payload.supportsActions)
|
|
#expect(payload.categoryIdentifier == nil)
|
|
#expect(payload.displayTitle == "Permission needed")
|
|
}
|
|
|
|
@Test func modelErrorIsKnownTypeAndPrefersHubTag() throws {
|
|
let eventTag = "model-error-sess-1-evt-1710000000000"
|
|
let payload = try #require(PushPayload.parse(dictionary: [
|
|
"type": "model-error",
|
|
"sessionId": "sess-1",
|
|
"title": "Rate limited",
|
|
"body": "status 429",
|
|
"severity": "error",
|
|
"contractVersion": "1",
|
|
"tag": eventTag,
|
|
]))
|
|
#expect(payload.type == .modelError)
|
|
#expect(payload.rawType == "model-error")
|
|
#expect(payload.notificationTag == eventTag)
|
|
#expect(!payload.supportsActions)
|
|
#expect(payload.categoryIdentifier == nil)
|
|
#expect(payload.severity == .error)
|
|
}
|
|
|
|
@Test func modelErrorWithoutHubTagFallsBackToTypeSessionId() throws {
|
|
let payload = try #require(PushPayload.parse(dictionary: [
|
|
"type": "model-error",
|
|
"sessionId": "sess-1",
|
|
"contractVersion": "1",
|
|
]))
|
|
#expect(payload.notificationTag == "model-error-sess-1")
|
|
}
|
|
|
|
@Test func absentContractVersionCountsAsKnown() throws {
|
|
var fields = fullFields
|
|
fields["contractVersion"] = nil
|
|
let payload = try #require(PushPayload.parse(dictionary: fields))
|
|
#expect(payload.isKnownContractVersion)
|
|
#expect(payload.supportsActions)
|
|
}
|
|
|
|
@Test func titleFallsBackToSessionNameThenConstant() throws {
|
|
let named = try #require(PushPayload.parse(dictionary: [
|
|
"type": "ready", "sessionId": "s1", "sessionName": "claude - hapi",
|
|
]))
|
|
#expect(named.displayTitle == "claude - hapi")
|
|
let bare = try #require(PushPayload.parse(dictionary: [
|
|
"type": "ready", "sessionId": "s1",
|
|
]))
|
|
#expect(bare.displayTitle == "HAPI")
|
|
}
|
|
|
|
@Test func readySummaryComposesDisplayBody() throws {
|
|
let summaryJSON = #"{"version":1,"summary":"Tests green","action":"Review the diff"}"#
|
|
let payload = try #require(PushPayload.parse(dictionary: [
|
|
"type": "ready",
|
|
"sessionId": "s1",
|
|
"body": "hub body",
|
|
"notifySummary": summaryJSON,
|
|
]))
|
|
#expect(payload.notifySummary?.version == 1)
|
|
#expect(payload.displayBody == "Tests green\n-> Review the diff")
|
|
}
|
|
|
|
@Test func readySummaryEqualActionCollapsesToOneLine() throws {
|
|
let summaryJSON = #"{"summary":"Done","action":"Done"}"#
|
|
let payload = try #require(PushPayload.parse(dictionary: [
|
|
"type": "ready", "sessionId": "s1", "notifySummary": summaryJSON,
|
|
]))
|
|
#expect(payload.displayBody == "Done")
|
|
}
|
|
|
|
@Test func malformedNotifySummaryFallsBackToBody() throws {
|
|
let payload = try #require(PushPayload.parse(dictionary: [
|
|
"type": "ready",
|
|
"sessionId": "s1",
|
|
"body": "hub body",
|
|
"notifySummary": "{not json",
|
|
]))
|
|
#expect(payload.notifySummary == nil)
|
|
#expect(payload.displayBody == "hub body")
|
|
}
|
|
|
|
// MARK: - Plaintext (decrypted envelope) form
|
|
|
|
@Test func parsesPlaintextJSONObject() throws {
|
|
let json = #"{"type":"ready","sessionId":"s1","title":"HAPI","body":"Ready for input","contractVersion":"1"}"#
|
|
let payload = try #require(PushPayload.parse(plaintext: Data(json.utf8)))
|
|
#expect(payload.type == .ready)
|
|
#expect(payload.sessionId == "s1")
|
|
#expect(payload.categoryIdentifier == "ready")
|
|
}
|
|
|
|
@Test func plaintextToleratesNumericScalarsAndSkipsNested() throws {
|
|
// A future hub emitting a bare number for contractVersion must not
|
|
// drop the message; nested values are ignored, not fatal.
|
|
let json = #"{"type":"ready","sessionId":"s1","contractVersion":1,"extra":{"a":1}}"#
|
|
let payload = try #require(PushPayload.parse(plaintext: Data(json.utf8)))
|
|
#expect(payload.contractVersion == "1")
|
|
#expect(payload.isKnownContractVersion)
|
|
}
|
|
|
|
@Test func plaintextRejectsNonObjectOrGarbage() {
|
|
#expect(PushPayload.parse(plaintext: Data("[1,2]".utf8)) == nil)
|
|
#expect(PushPayload.parse(plaintext: Data("nope".utf8)) == nil)
|
|
}
|
|
}
|