feat(cursor): detect inline model errors, surface, notify, and bridge (#878) (#987)

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>
This commit is contained in:
HeavyGee
2026-11-13 18:50:09 +08:00
committed by GitHub
co-authored by HAPI
parent a7e472bb39
commit f0ab4ba79d
87 changed files with 9410 additions and 257 deletions
@@ -12,7 +12,8 @@ import kotlinx.serialization.Serializable
enum class PushType(val wire: String) {
READY("ready"),
PERMISSION_REQUEST("permission-request"),
INPUT_REQUEST("input-request");
INPUT_REQUEST("input-request"),
MODEL_ERROR("model-error");
companion object {
fun fromWire(value: String): PushType? = entries.firstOrNull { it.wire == value }
@@ -22,7 +23,8 @@ enum class PushType(val wire: String) {
/** `data.severity` — visual urgency accent (`hub/src/fcm/fcmService.ts`). */
enum class PushSeverity(val wire: String) {
INFO("info"),
WARNING("warning");
WARNING("warning"),
ERROR("error");
companion object {
fun fromWire(value: String?): PushSeverity? =
@@ -73,6 +75,8 @@ data class PushPayload(
val severity: PushSeverity?,
val contractVersion: String?,
val notifySummary: PushNotifySummary?,
/** Hub coalescing tag (`FcmSendPayload.tag`), when present on the data map. */
val tag: String?,
) {
/**
@@ -94,31 +98,32 @@ data class PushPayload(
PushType.PERMISSION_REQUEST -> requestId != null
PushType.INPUT_REQUEST -> false // Answer in the session, not via approval or message Reply.
PushType.READY -> true
null -> false
PushType.MODEL_ERROR, null -> false
}
/**
* Notification channel routing: `permission_requests` / `input_requests` (HIGH) /
* `ready` (DEFAULT) / [CHANNEL_OTHER] (DEFAULT). Unknown types and
* unknown contract versions land in the default-importance
* [CHANNEL_OTHER] bucket — never in the heads-up channel.
* Notification channel routing: `permission_requests` / `input_requests` /
* [CHANNEL_MODEL_ERROR] (HIGH) / `ready` (DEFAULT) / [CHANNEL_OTHER]
* (DEFAULT). Unknown types and unknown contract versions land in the
* default-importance [CHANNEL_OTHER] bucket — never in the heads-up channel.
*/
val channelId: String
get() = when {
!isKnownContractVersion -> CHANNEL_OTHER
type == PushType.PERMISSION_REQUEST -> CHANNEL_PERMISSION_REQUESTS
type == PushType.INPUT_REQUEST -> CHANNEL_INPUT_REQUESTS
type == PushType.MODEL_ERROR -> CHANNEL_MODEL_ERROR
type == PushType.READY -> CHANNEL_READY
else -> CHANNEL_OTHER
}
/**
* Coalescing tag `type-<sessionId>`: a newer push of the same type for
* the same session replaces the previous notification instead of
* stacking (mirrors the hub-side `tag` scheme it uses for Web Push).
* Coalescing tag: prefer the hub-supplied `data.tag` (event-specific
* for `model-error`) before falling back to `type-<sessionId>`.
*/
val notificationTag: String
get() = "${rawType.ifBlank { "unknown" }}-$sessionId"
get() = tag?.takeIf { it.isNotBlank() }
?: "${rawType.ifBlank { "unknown" }}-$sessionId"
/** Title to render; falls back to the session name, then a constant. */
val displayTitle: String
@@ -158,6 +163,7 @@ data class PushPayload(
* per-channel system settings survive.
*/
const val CHANNEL_OTHER = "task_notifications"
const val CHANNEL_MODEL_ERROR = "model_errors"
private const val DEFAULT_TITLE = "HAPI"
@@ -181,6 +187,7 @@ data class PushPayload(
severity = PushSeverity.fromWire(data["severity"]),
contractVersion = data["contractVersion"]?.takeIf { it.isNotBlank() },
notifySummary = data["notifySummary"]?.let(::parseNotifySummary),
tag = data["tag"]?.takeIf { it.isNotBlank() },
)
}
@@ -97,6 +97,16 @@ class PushPayloadTest {
PushPayload.CHANNEL_READY,
PushPayload.parse(permissionData("type" to "ready"))!!.channelId,
)
assertEquals(
PushPayload.CHANNEL_MODEL_ERROR,
PushPayload.parse(
permissionData(
"type" to "model-error",
"severity" to "error",
"tag" to "model-error-11111111-2222-3333-4444-555555555555-evt-1",
)
)!!.channelId,
)
}
@Test
@@ -142,6 +152,51 @@ class PushPayloadTest {
)
}
@Test
fun `model-error is a known type and prefers the hub event tag`() {
val eventTag = "model-error-11111111-2222-3333-4444-555555555555-evt-1710000000000"
val payload = PushPayload.parse(
permissionData(
"type" to "model-error",
"severity" to "error",
"title" to "Rate limited",
"body" to "status 429",
"tag" to eventTag,
).minus("requestId"),
)!!
assertEquals(PushType.MODEL_ERROR, payload.type)
assertEquals("model-error", payload.rawType)
assertEquals(PushPayload.CHANNEL_MODEL_ERROR, payload.channelId)
assertEquals(eventTag, payload.notificationTag)
assertEquals(PushSeverity.ERROR, payload.severity)
assertFalse(payload.supportsActions)
}
@Test
fun `model-error without a hub tag falls back to type-sessionId`() {
val payload = PushPayload.parse(
permissionData("type" to "model-error").minus("requestId"),
)!!
assertEquals(
"model-error-11111111-2222-3333-4444-555555555555",
payload.notificationTag,
)
}
@Test
fun `distinct model-error tags do not collapse`() {
val session = "11111111-2222-3333-4444-555555555555"
val first = PushPayload.parse(
permissionData("type" to "model-error", "tag" to "model-error-$session-evt-1"),
)!!.notificationTag
val second = PushPayload.parse(
permissionData("type" to "model-error", "tag" to "model-error-$session-evt-2"),
)!!.notificationTag
assertEquals("model-error-$session-evt-1", first)
assertEquals("model-error-$session-evt-2", second)
}
// ---------------------------------------------------------- ready bodies --
@Test