Files
hapi/cli/src/utils/MessageQueue2.ts
T
f0ab4ba79d 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>
2026-11-13 18:50:09 +08:00

784 lines
28 KiB
TypeScript

import { logger } from "@/ui/logger";
/**
* Queue-owned provenance for items HAPI enqueued itself, rather than a
* user-authored turn. Tagged so each internal producer stays distinguishable:
* `control` is a plain control message (e.g. the Cursor Auto-review toggle
* slash); `model-error-bridge` carries the event it is recovering.
*/
export type QueueItemInternal =
| { kind: 'model-error-bridge'; eventId: string }
| { kind: 'control' };
export interface QueueItem<T> {
message: string;
mode: T;
modeHash: string;
localId?: string;
isolate?: boolean; // If true, this message must be processed alone
/** Stable FIFO key used when an async reservation is restored later. */
enqueueOrder?: number;
/**
* Set when HAPI enqueued this item itself, not a user-authored turn.
* Queue-owned provenance — never inferred from caller localId.
*/
internal?: QueueItemInternal;
}
export type QueueItemOptions = {
internal?: QueueItemInternal;
};
export type QueueReservation<T> = {
item: QueueItem<T>;
index: number;
previousItem: QueueItem<T> | null;
nextItem: QueueItem<T> | null;
/** Once ambiguous, retries must remain held unless explicitly committed. */
originIndeterminate: boolean;
cancelReason?: 'explicit' | 'queue-reset';
state: 'reserved' | 'dispatching' | 'indeterminate' | 'cancelled';
};
export type CollectedQueueItem = {
message: string
localId?: string
internal?: QueueItemInternal
};
/**
* A mode-aware message queue that stores messages with their modes.
* Returns consistent batches of messages with the same mode.
*/
export class MessageQueue2<T> {
public queue: QueueItem<T>[] = []; // Made public for testing
private waiter: ((hasMessages: boolean) => void) | null = null;
private closed = false;
private onMessageHandler: ((message: string, mode: T) => void) | null = null;
onBatchConsumed: ((localIds: string[]) => void) | null = null;
/** Optional synchronous admission, before removing or acknowledging a batch. */
beforeDequeue: (() => void) | null = null;
modeHasher: (mode: T) => string;
private readonly reservations = new Map<string, QueueReservation<T>>();
private readonly consumedReservations = new Set<string>();
private nextEnqueueOrder = 0;
private previousEnqueueOrder = -1;
constructor(
modeHasher: (mode: T) => string,
onMessageHandler: ((message: string, mode: T) => void) | null = null
) {
this.modeHasher = modeHasher;
this.onMessageHandler = onMessageHandler;
logger.debug(`[MessageQueue2] Initialized`);
}
/**
* Set a handler that will be called when a message arrives
*/
setOnMessage(handler: ((message: string, mode: T) => void) | null): void {
this.onMessageHandler = handler;
}
/**
* Push a message to the queue with a mode.
*/
push(message: string, mode: T, localId?: string): void {
if (this.closed) {
throw new Error('Cannot push to closed queue');
}
const modeHash = this.modeHasher(mode);
logger.debug(`[MessageQueue2] push() called with mode hash: ${modeHash}`);
const item = {
message,
mode,
modeHash,
localId,
isolate: false,
enqueueOrder: this.nextEnqueueOrder++
};
Object.defineProperty(item, 'enqueueOrder', { value: item.enqueueOrder, enumerable: false, writable: true });
this.queue.push(item);
// Trigger message handler if set
if (this.onMessageHandler) {
this.onMessageHandler(message, mode);
}
// Notify waiter if any
if (this.waiter) {
logger.debug(`[MessageQueue2] Notifying waiter`);
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
logger.debug(`[MessageQueue2] push() completed. Queue size: ${this.queue.length}`);
}
/**
* Push a message immediately without batching delay.
* Does not clear the queue or enforce isolation.
*/
pushImmediate(message: string, mode: T, localId?: string): void {
if (this.closed) {
throw new Error('Cannot push to closed queue');
}
const modeHash = this.modeHasher(mode);
logger.debug(`[MessageQueue2] pushImmediate() called with mode hash: ${modeHash}`);
const item = {
message,
mode,
modeHash,
localId,
isolate: false,
enqueueOrder: this.nextEnqueueOrder++
};
Object.defineProperty(item, 'enqueueOrder', { value: item.enqueueOrder, enumerable: false, writable: true });
this.queue.push(item);
// Trigger message handler if set
if (this.onMessageHandler) {
this.onMessageHandler(message, mode);
}
// Notify waiter if any
if (this.waiter) {
logger.debug(`[MessageQueue2] Notifying waiter for immediate message`);
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
logger.debug(`[MessageQueue2] pushImmediate() completed. Queue size: ${this.queue.length}`);
}
/**
* Push a message that must be processed in isolation, preserving any
* messages already queued ahead of it. The new message is never batched
* with siblings (neither the ones before it, nor any that arrive after).
* Use this when a slash command must run alone but earlier prompts must
* still be delivered in order.
*/
pushIsolated(message: string, mode: T, localId?: string, options?: QueueItemOptions): void {
if (this.closed) {
throw new Error('Cannot push to closed queue');
}
const modeHash = this.modeHasher(mode);
logger.debug(`[MessageQueue2] pushIsolated() called with mode hash: ${modeHash} - preserving ${this.queue.length} pending messages`);
const item = {
message,
mode,
modeHash,
localId,
isolate: true,
internal: options?.internal,
enqueueOrder: this.nextEnqueueOrder++
};
Object.defineProperty(item, 'enqueueOrder', { value: item.enqueueOrder, enumerable: false, writable: true });
this.queue.push(item);
if (this.onMessageHandler) {
this.onMessageHandler(message, mode);
}
if (this.waiter) {
logger.debug(`[MessageQueue2] Notifying waiter for isolated message`);
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
logger.debug(`[MessageQueue2] pushIsolated() completed. Queue size: ${this.queue.length}`);
}
/**
* Push a message that must be processed in complete isolation.
* Clears any pending messages and ensures this message is never batched with others.
* Used for special commands that require dedicated processing.
*/
pushIsolateAndClear(message: string, mode: T, localId?: string): void {
if (this.closed) {
throw new Error('Cannot push to closed queue');
}
const modeHash = this.modeHasher(mode);
logger.debug(`[MessageQueue2] pushIsolateAndClear() called with mode hash: ${modeHash} - clearing ${this.queue.length} pending messages`);
// Clear any pending messages to ensure this message is processed in complete isolation
this.queue = [];
// Reservations live outside this.queue; a steer awaiting an explicit
// rejection must not restore a prompt the clear command discarded.
this.cancelReservations();
const item = {
message,
mode,
modeHash,
localId,
isolate: true,
enqueueOrder: this.nextEnqueueOrder++
};
Object.defineProperty(item, 'enqueueOrder', { value: item.enqueueOrder, enumerable: false, writable: true });
this.queue.push(item);
// Trigger message handler if set
if (this.onMessageHandler) {
this.onMessageHandler(message, mode);
}
// Notify waiter if any
if (this.waiter) {
logger.debug(`[MessageQueue2] Notifying waiter for isolated message`);
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
logger.debug(`[MessageQueue2] pushIsolateAndClear() completed. Queue size: ${this.queue.length}`);
}
/**
* Push a message to the beginning of the queue with a mode.
*/
unshift(message: string, mode: T, localId?: string): void {
if (this.closed) {
throw new Error('Cannot unshift to closed queue');
}
const modeHash = this.modeHasher(mode);
logger.debug(`[MessageQueue2] unshift() called with mode hash: ${modeHash}`);
const item = {
message,
mode,
modeHash,
localId,
isolate: false,
enqueueOrder: this.previousEnqueueOrder--
};
Object.defineProperty(item, 'enqueueOrder', { value: item.enqueueOrder, enumerable: false, writable: true });
this.queue.unshift(item);
// Trigger message handler if set
if (this.onMessageHandler) {
this.onMessageHandler(message, mode);
}
// Notify waiter if any
if (this.waiter) {
logger.debug(`[MessageQueue2] Notifying waiter`);
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
logger.debug(`[MessageQueue2] unshift() completed. Queue size: ${this.queue.length}`);
}
/**
* Push a message to the beginning of the queue with isolation preserved.
* Mirrors `pushIsolated` but inserts at the head. Use this when requeueing a
* batch that was originally collected under isolation (e.g. a slash command
* that failed transiently and must retry without batching against sibling
* prompts).
*/
unshiftIsolated(message: string, mode: T, localId?: string, options?: QueueItemOptions): void {
if (this.closed) {
throw new Error('Cannot unshift to closed queue');
}
const modeHash = this.modeHasher(mode);
logger.debug(`[MessageQueue2] unshiftIsolated() called with mode hash: ${modeHash}`);
const item = {
message,
mode,
modeHash,
localId,
isolate: true,
internal: options?.internal,
enqueueOrder: this.previousEnqueueOrder--
};
Object.defineProperty(item, 'enqueueOrder', { value: item.enqueueOrder, enumerable: false, writable: true });
this.queue.unshift(item);
if (this.onMessageHandler) {
this.onMessageHandler(message, mode);
}
if (this.waiter) {
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
logger.debug(`[MessageQueue2] unshiftIsolated() completed. Queue size: ${this.queue.length}`);
}
/**
* True when a user/API turn is already waiting (queued or reserved).
* Only items with no `internal` provenance count: every internal kind —
* the bridge itself and HAPI-enqueued control slashes alike — is ours,
* not a newer user instruction that should supersede a bridge.
*/
hasPendingNonBridgeTurn(): boolean {
if (this.queue.some((item) => item.internal === undefined)) {
return true;
}
// Soft-steer takeByLocalId removes the row from queue before
// turnHasSteeredInput / softSteerWaiters are set — Bridge guards must
// still see that newer instruction while the reservation is live.
for (const { item, state } of this.reservations.values()) {
if (state === 'cancelled') continue;
if (item.internal !== undefined) continue;
return true;
}
return false;
}
/** Drop a pending model-error bridge by its eventId (queue-owned provenance). */
cancelModelErrorBridge(eventId: string): boolean {
if (!eventId) return false;
const idx = this.queue.findIndex(
(item) => item.internal?.kind === 'model-error-bridge'
&& item.internal.eventId === eventId
);
if (idx === -1) return false;
this.queue.splice(idx, 1);
return true;
}
/**
* Remove the first queued message that matches the given localId.
* Returns true if a message was removed, false if not found.
* Best-effort: if the CLI is offline when cancel is issued, the message
* may already have been collected for invocation and won't be found here.
*/
cancelByLocalId(localId: string): boolean | 'in-flight' | 'indeterminate' | 'consumed' {
if (!localId) return false;
const idx = this.queue.findIndex(item => item.localId === localId);
if (idx !== -1) {
this.queue.splice(idx, 1);
return true;
}
const reservation = this.reservations.get(localId);
if (!reservation) {
return this.consumedReservations.has(localId) ? 'consumed' : false;
}
if (reservation.state === 'dispatching') {
// The row is inside an async steer: it cannot be removed, but it is
// also NOT already consumed — the hub must not stamp invoked_at.
return 'in-flight';
}
if (reservation.state === 'indeterminate') {
// Explicit Cancel/ Edit is the user's resolution of an unknown
// outcome; release the held reservation so it cannot settle later.
reservation.cancelReason = 'explicit';
reservation.state = 'cancelled';
this.reservations.delete(localId);
return true;
}
reservation.cancelReason = 'explicit';
reservation.state = 'cancelled';
this.reservations.delete(localId);
return true;
}
/**
* Look up a queued item by localId without removing it.
*/
peekByLocalId(localId: string): QueueItem<T> | null {
if (!localId) return null;
return this.queue.find(item => item.localId === localId) ?? null;
}
/**
* Remove and return a queued item by localId (with its original index),
* or null if not found. Pair with {@link restoreTakenItem} when an async
* operation may need to put the item back in order.
*/
takeByLocalId(localId: string): QueueReservation<T> | null {
if (!localId) return null;
// An indeterminate steer is deliberately held outside the normal queue.
// A later explicit Steer owns a fresh attempt, so an abandoned
// request's late callbacks cannot mutate its replacement.
const existing = this.reservations.get(localId);
if (existing) {
if (existing.state !== 'indeterminate') return null;
const retry: QueueReservation<T> = { ...existing, state: 'reserved', cancelReason: undefined };
existing.state = 'cancelled';
existing.cancelReason = 'explicit';
this.reservations.set(localId, retry);
return retry;
}
const idx = this.queue.findIndex(item => item.localId === localId);
if (idx === -1) return null;
const [item] = this.queue.splice(idx, 1);
if (!item) return null;
const reservation: QueueReservation<T> = {
item,
index: idx,
previousItem: this.queue[idx - 1] ?? null,
nextItem: this.queue[idx] ?? null,
originIndeterminate: false,
state: 'reserved'
};
if (item.localId) {
this.reservations.set(item.localId, reservation);
}
return reservation;
}
/**
* Re-insert an item previously removed by {@link takeByLocalId} at its
* original index (clamped if the queue shrank).
*/
restoreReservation(reservation: QueueReservation<T>): boolean {
if (reservation.state === 'cancelled') {
return false;
}
if (reservation.originIndeterminate) {
reservation.state = 'indeterminate';
return true;
}
if (this.closed) {
throw new Error('Cannot restore into closed queue');
}
if (reservation.item.localId) {
if (this.reservations.get(reservation.item.localId) !== reservation) {
return false;
}
this.reservations.delete(reservation.item.localId);
}
const order = reservation.item.enqueueOrder;
const orderedIndex = order === undefined
? -1
: this.queue.findIndex((item) => item.enqueueOrder !== undefined && item.enqueueOrder > order);
const nextIndex = reservation.nextItem
? this.queue.indexOf(reservation.nextItem)
: -1;
const previousIndex = reservation.previousItem
? this.queue.indexOf(reservation.previousItem)
: -1;
const idx = orderedIndex >= 0
? orderedIndex
: nextIndex >= 0
? nextIndex
: previousIndex >= 0
? previousIndex + 1
: this.queue.length;
this.queue.splice(idx, 0, reservation.item);
if (this.waiter) {
const waiter = this.waiter;
this.waiter = null;
waiter(true);
}
return true;
}
commitReservation(reservation: QueueReservation<T>): boolean {
if (reservation.state === 'cancelled' && reservation.cancelReason !== 'queue-reset') {
return false;
}
if (reservation.item.localId) {
const active = this.reservations.get(reservation.item.localId);
if (active === reservation) {
this.reservations.delete(reservation.item.localId);
} else if (reservation.cancelReason !== 'queue-reset') {
return false;
}
this.rememberConsumedReservation(reservation.item.localId);
}
return true;
}
beginReservationDispatch(reservation: QueueReservation<T>): boolean {
if (reservation.state !== 'reserved') {
return false;
}
if (reservation.item.localId && this.reservations.get(reservation.item.localId) !== reservation) {
return false;
}
reservation.state = 'dispatching';
return true;
}
restoreTakenItem(taken: QueueReservation<T>): void {
this.restoreReservation(taken);
}
/** Release a held reservation only for the explicit retry path. */
releaseIndeterminateReservation(localId: string): boolean {
const reservation = this.reservations.get(localId);
if (!reservation || reservation.state !== 'indeterminate') return false;
reservation.cancelReason = 'explicit';
reservation.state = 'cancelled';
this.reservations.delete(localId);
return true;
}
/** Hold a dispatched steer for explicit retry/cancel without replaying it. */
markReservationIndeterminate(reservation: QueueReservation<T>): boolean {
if (reservation.state !== 'dispatching') return false;
if (reservation.item.localId && this.reservations.get(reservation.item.localId) !== reservation) {
return false;
}
reservation.originIndeterminate = true;
reservation.state = 'indeterminate';
return true;
}
private rememberConsumedReservation(localId: string): void {
this.consumedReservations.delete(localId);
this.consumedReservations.add(localId);
while (this.consumedReservations.size > 256) {
const oldest = this.consumedReservations.values().next().value;
if (oldest === undefined) break;
this.consumedReservations.delete(oldest);
}
}
private cancelReservations(preserveDispatching = false): void {
for (const [localId, reservation] of this.reservations) {
if (preserveDispatching && (reservation.state === 'dispatching' || reservation.state === 'indeterminate')) {
continue;
}
reservation.cancelReason = 'queue-reset';
reservation.state = 'cancelled';
this.reservations.delete(localId);
}
}
/**
* Reset the queue - clears all messages and resets to empty state
*/
reset(options?: { preserveDispatchingReservations?: boolean }): void {
logger.debug(`[MessageQueue2] reset() called. Clearing ${this.queue.length} messages`);
this.queue = [];
this.cancelReservations(options?.preserveDispatchingReservations === true);
this.closed = false;
// Clear waiter without calling it since we're not closing
this.waiter = null;
}
/**
* localIds of messages still pending in the queue (enqueued but not yet
* consumed/acked). Lets a caller reconcile them with the hub before a
* reset() that would otherwise drop them without an ack.
*/
pendingLocalIds(): string[] {
return this.queue
.map((item) => item.localId)
.filter((id): id is string => typeof id === 'string');
}
/** True if any pending (not yet dequeued) message matches the predicate. */
hasMessageMatching(predicate: (message: string) => boolean): boolean {
return this.queue.some((item) => predicate(item.message));
}
/**
* Drop pending items matching the predicate (e.g. reconcile an internal
* slash that a fresh spawn already satisfies). Does not hub-ack.
*/
removeItemsMatching(predicate: (item: QueueItem<T>) => boolean): number {
const kept: QueueItem<T>[] = [];
let removed = 0;
for (const item of this.queue) {
if (!predicate(item)) {
kept.push(item);
continue;
}
removed += 1;
if (item.localId) {
const reservation = this.reservations.get(item.localId);
if (reservation) {
reservation.cancelReason = 'explicit';
reservation.state = 'cancelled';
this.reservations.delete(item.localId);
}
}
}
this.queue = kept;
return removed;
}
/**
* Close the queue - no more messages can be pushed
*/
close(): void {
logger.debug(`[MessageQueue2] close() called`);
this.closed = true;
this.cancelReservations();
// Notify any waiting caller
if (this.waiter) {
const waiter = this.waiter;
this.waiter = null;
waiter(false);
}
}
/**
* Check if the queue is closed
*/
isClosed(): boolean {
return this.closed;
}
/**
* Get the current queue size
*/
size(): number {
return this.queue.length;
}
/**
* Wait for messages and return all messages with the same mode as a single string
* Returns { message: string, mode: T } or null if aborted/closed
*/
async waitForMessagesAndGetAsString(abortSignal?: AbortSignal): Promise<{ message: string, mode: T, isolate: boolean, hash: string, items: CollectedQueueItem[] } | null> {
// If we have messages, return them immediately
if (this.queue.length > 0) {
return this.collectBatch();
}
// If closed or already aborted, return null
if (this.closed || abortSignal?.aborted) {
return null;
}
// Wait for messages to arrive
const hasMessages = await this.waitForMessages(abortSignal);
if (!hasMessages) {
return null;
}
return this.collectBatch();
}
/**
* Collect a batch of messages with the same mode, respecting isolation requirements
*/
private collectBatch(): { message: string, mode: T, hash: string, isolate: boolean, items: CollectedQueueItem[] } | null {
if (this.queue.length === 0) {
return null;
}
this.beforeDequeue?.();
const firstItem = this.queue[0];
const sameModeMessages: string[] = [];
const consumedLocalIds: string[] = [];
// Per-item breakdown of this batch, preserved alongside the joined
// `message` string below so callers that need to requeue individual
// messages (e.g. restoring a failed batch with each item's own
// localId intact) don't have to re-split an already-joined string.
const items: CollectedQueueItem[] = [];
let mode = firstItem.mode;
let isolate = firstItem.isolate ?? false;
const targetModeHash = firstItem.modeHash;
// If the first message requires isolation, only process it alone
if (firstItem.isolate) {
const item = this.queue.shift()!;
sameModeMessages.push(item.message);
items.push({ message: item.message, localId: item.localId, internal: item.internal });
if (item.localId) consumedLocalIds.push(item.localId);
logger.debug(`[MessageQueue2] Collected isolated message with mode hash: ${targetModeHash}`);
} else {
// Collect all messages with the same mode until we hit an isolated message
while (this.queue.length > 0 &&
this.queue[0].modeHash === targetModeHash &&
!this.queue[0].isolate) {
const item = this.queue.shift()!;
sameModeMessages.push(item.message);
items.push({ message: item.message, localId: item.localId, internal: item.internal });
if (item.localId) consumedLocalIds.push(item.localId);
}
logger.debug(`[MessageQueue2] Collected batch of ${sameModeMessages.length} messages with mode hash: ${targetModeHash}`);
}
// Join all messages with newlines
const combinedMessage = sameModeMessages.join('\n');
if (consumedLocalIds.length > 0) {
this.onBatchConsumed?.(consumedLocalIds);
}
return {
message: combinedMessage,
mode,
hash: targetModeHash,
isolate,
items
};
}
/**
* Wait for messages to arrive
*/
private waitForMessages(abortSignal?: AbortSignal): Promise<boolean> {
return new Promise((resolve) => {
let settled = false;
let abortHandler: (() => void) | null = null;
let waiterFunc: (hasMessages: boolean) => void;
const finish = (hasMessages: boolean) => {
if (settled) {
return;
}
settled = true;
if (this.waiter === waiterFunc) {
this.waiter = null;
}
// Clean up abort handler
if (abortHandler && abortSignal) {
abortSignal.removeEventListener('abort', abortHandler);
}
resolve(hasMessages);
};
waiterFunc = (hasMessages: boolean) => {
finish(hasMessages);
};
// Set up abort handler
if (abortSignal) {
abortHandler = () => {
logger.debug('[MessageQueue2] Wait aborted');
finish(false);
};
abortSignal.addEventListener('abort', abortHandler);
}
// Set the waiter before checking the queue to avoid missed notifications
this.waiter = waiterFunc;
// Check again in case messages arrived or queue closed while setting up
if (this.queue.length > 0) {
finish(true);
return;
}
if (this.closed || abortSignal?.aborted) {
finish(false);
return;
}
logger.debug('[MessageQueue2] Waiting for messages...');
});
}
}