Files
orca/src/shared/runtime-rpc-call-queue.ts
T
Brennan Benson b1e12d7bb4 fix(native-chat): a new structured chat's model list comes from the machine that runs it (#25143)
* fix(native-chat): a new structured chat's model list comes from the host that runs it

agentSession.modelCatalog builds the structured-session host like agentSession.options,
so a host with no saved chats since it started answers instead of refusing. When the
host answers unknown because its first listing runs in the background, the picker
re-reads on a bounded schedule until that listing lands. Local terminal-backed chat
skips the structured catalog when a custom launch command is configured.

* fix(native-chat): wait on the host's first model listing instead of re-reading on a timer

A new structured chat's picker read the host catalog once; on an account the
host had never listed, the answer was "unknown" while a background listing ran,
and the client re-read on a 1-30 s schedule. Replace the schedule with the
host's own completion signal:

- The host answers a cold read with `listingInProgress: true` (new optional
  field) once it has started or joined that listing. A read that passes the new
  optional `waitForListing` param awaits the same joined listing and answers
  with it, or a plain "unknown" if it failed. The at-rest options read never
  waits. A host that predates the field never sends it, so the client never
  sends the param to a host that would refuse it.
- The picker reads once per open and attach; after the host's report it sends
  one waiting read, with a 90 s client timeout above the slowest listing.
  While that read is out, the model pill keeps its label but cannot open or be
  set (typed /model included). An answer, failure, timeout, hide, attach or
  the provider's own list releases it.
- `agentSession.modelCatalog` builds the structured-session host only for a
  read that names a session (a structured chat). Terminal-backed chat's
  session-less read keeps the non-building gate, so a desktop that never runs
  structured chat never opens the session journal.

* fix(native-chat): one waiting model-list read per chat, and no late menu open

The waiting catalog read was owned by one run of the picker's effect. Attach
(a new fence), hide/show or a send re-ran the effect: the cleanup released the
model picker onto the built-in list for a round trip, and the new run sent a
second waiting read while the first, which cannot be withdrawn, kept a remote
call slot until the listing ended.

The waiting read now belongs to the chat (runtime target + agent + session):
a small registry keeps one in flight per chat, every re-run or remount joins
it, and the entry is deleted when the read settles. The picker hold is derived
from that entry being in flight, so it lasts across attach and hide/show and
ends when the read settles, the provider reports its own list, or the pane
switches to another session. Answers still pass the stale and record checks.

A bare /model typed while the list loads no longer opens the model menu by
itself when the list lands: the menu stays keyed on the request, and only its
initial open is suppressed while pending, so the request is spent shut and the
end of the pending period never remounts it.

* fix(native-chat): release the model picker in the same commit as the host list

When the waiting catalog read settled in the chat that started it, the
registry dropped its entry and told subscribers first, and the host list was
applied a few microtasks later. React committed once with the picker enabled
on the built-in list, then again with the host's list.

Joiners now hand the registry their apply callback, and the registry runs
every joiner (with the answer, or nothing when the read failed or timed out)
before it deletes the entry and notifies. The release and the list land in one
commit. An effect cleanup leaves the wait instead of flagging itself stale.

* fix(runtime): queue model catalog reads in the long-wait lane

A model catalog read that waits on a host's first listing replies only when
that listing ends, yet it took one of the 8 foreground call slots for its
server. Enough chats opened during one cold listing would stall that server's
sends and interrupts until a wait settled.

agentSession.modelCatalog now joins worktree.rm in the long-wait lane: same
concurrency, counted apart from the foreground calls. The queue classifies by
method only, and a warm catalog read answers at once, so the whole method
moves.
2026-10-04 14:37:40 -07:00

266 lines
8.9 KiB
TypeScript

import { abortSignalReason } from './abort-signal-reason'
import { REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES } from './remote-runtime-memory-limits'
const DEFAULT_REMOTE_RUNTIME_CALL_CONCURRENCY = 8
const DEFAULT_REMOTE_RUNTIME_BACKGROUND_CALL_CONCURRENCY = 2
export const RUNTIME_RPC_MAX_QUEUED_CALLS_PER_SELECTOR = 256
export const RUNTIME_RPC_MAX_QUEUED_CALLS_TOTAL = 2_048
export const RUNTIME_RPC_QUEUE_OVERLOAD_CODE = 'runtime_rpc_queue_overloaded'
export class RuntimeRpcCallQueueOverloadError extends Error {
readonly code = RUNTIME_RPC_QUEUE_OVERLOAD_CODE
constructor(readonly scope: 'selector' | 'global' | 'memory') {
super('Remote runtime call queue is full; retry after current calls finish.')
this.name = 'RuntimeRpcCallQueueOverloadError'
}
}
type QueuedRuntimeCall<T> = {
background: boolean
retainedBytes: number
run: () => Promise<T>
resolve: (value: T) => void
reject: (error: unknown) => void
started: boolean
signal?: AbortSignal
onAbort?: () => void
}
type RuntimeCallQueue = {
active: number
backgroundActive: number
foreground: (QueuedRuntimeCall<unknown> | undefined)[]
foregroundHead: number
background: (QueuedRuntimeCall<unknown> | undefined)[]
backgroundHead: number
}
export function isBackgroundRuntimeMethod(method: string): boolean {
return (
method === 'hostedReview.forBranch' ||
method === 'github.prForBranch' ||
method === 'github.listWorkItems' ||
method === 'github.countWorkItems' ||
method === 'git.status' ||
method === 'git.history' ||
method === 'git.conflictOperation' ||
method === 'git.branchCompare' ||
method === 'git.upstreamStatus' ||
method === 'worktree.prefetchCreateBase'
)
}
// Why its own lane: these reply only when slow host work finishes (worktree.rm when Git has deleted
// the checkout, a model catalog read that waits on the first listing), so they would hold the
// foreground slots listing refreshes and sends need; the background lane's slots belong to status.
function isLongWaitRuntimeMethod(method: string): boolean {
return method === 'worktree.rm' || method === 'agentSession.modelCatalog'
}
export class RuntimeRpcCallQueuePool {
private readonly queues = new Map<string, RuntimeCallQueue>()
private queuedCallCount = 0
private retainedCallBytes = 0
constructor(
private readonly concurrency = DEFAULT_REMOTE_RUNTIME_CALL_CONCURRENCY,
private readonly backgroundConcurrency = DEFAULT_REMOTE_RUNTIME_BACKGROUND_CALL_CONCURRENCY,
private readonly maxQueuedPerSelector = RUNTIME_RPC_MAX_QUEUED_CALLS_PER_SELECTOR,
private readonly maxQueuedTotal = RUNTIME_RPC_MAX_QUEUED_CALLS_TOTAL,
private readonly maxRetainedBytes = REMOTE_RUNTIME_MAX_PREPARED_RPC_BYTES
) {}
enqueue<T>(
selector: string,
method: string,
run: () => Promise<T>,
retainedBytes = 0,
signal?: AbortSignal
): Promise<T> {
if (signal?.aborted) {
return Promise.reject(abortSignalReason(signal))
}
// Same concurrency bound, counted apart from the selector's other calls; global caps still apply.
const queueKey = isLongWaitRuntimeMethod(method) ? `${selector}\u0000long-wait` : selector
if (this.queuedCallCount >= this.maxQueuedTotal) {
return Promise.reject(new RuntimeRpcCallQueueOverloadError('global'))
}
const existingQueue = this.queues.get(queueKey)
if (existingQueue && this.queuedCount(existingQueue) >= this.maxQueuedPerSelector) {
return Promise.reject(new RuntimeRpcCallQueueOverloadError('selector'))
}
if (
!Number.isSafeInteger(retainedBytes) ||
retainedBytes < 0 ||
this.retainedCallBytes + retainedBytes > this.maxRetainedBytes
) {
return Promise.reject(new RuntimeRpcCallQueueOverloadError('memory'))
}
const queue = this.getQueue(queueKey)
return new Promise<T>((resolve, reject) => {
const call: QueuedRuntimeCall<T> = {
background: isBackgroundRuntimeMethod(method),
retainedBytes,
run,
resolve,
reject,
started: false,
signal
}
const targetQueue = call.background ? queue.background : queue.foreground
targetQueue.push(call as QueuedRuntimeCall<unknown>)
this.queuedCallCount += 1
this.retainedCallBytes += retainedBytes
if (signal) {
call.onAbort = () => this.cancelQueuedCall(queueKey, queue, call)
signal.addEventListener('abort', call.onAbort, { once: true })
}
this.pump(queueKey, queue)
})
}
private getQueue(selector: string): RuntimeCallQueue {
let queue = this.queues.get(selector)
if (!queue) {
queue = {
active: 0,
backgroundActive: 0,
foreground: [],
foregroundHead: 0,
background: [],
backgroundHead: 0
}
this.queues.set(selector, queue)
}
return queue
}
private pump(selector: string, queue: RuntimeCallQueue): void {
while (queue.active < this.concurrency) {
let call = this.takeForeground(queue)
if (!call && queue.backgroundActive < this.backgroundConcurrency) {
call = this.takeBackground(queue)
}
if (!call) {
break
}
call.started = true
if (call.signal && call.onAbort) {
call.signal.removeEventListener('abort', call.onAbort)
}
queue.active += 1
if (call.background) {
queue.backgroundActive += 1
}
// Why: runtime streams and worktree actions share transport capacity with
// per-card status refreshes, so decorative calls must not stampede it.
let runPromise: Promise<unknown>
try {
runPromise = call.run()
} catch (error) {
// Why: callers rely on queued work starting immediately, but sync
// validation errors must still flow through the cleanup path.
runPromise = Promise.reject(error)
}
void runPromise.then(call.resolve, call.reject).finally(() => {
this.retainedCallBytes = Math.max(0, this.retainedCallBytes - call.retainedBytes)
queue.active = Math.max(0, queue.active - 1)
if (call.background) {
queue.backgroundActive = Math.max(0, queue.backgroundActive - 1)
}
if (queue.active === 0 && this.isEmpty(queue)) {
this.queues.delete(selector)
return
}
this.pump(selector, queue)
})
}
}
private cancelQueuedCall<T>(
selector: string,
queue: RuntimeCallQueue,
call: QueuedRuntimeCall<T>
): void {
if (call.started || !call.signal) {
return
}
const targetQueue = call.background ? queue.background : queue.foreground
const head = call.background ? queue.backgroundHead : queue.foregroundHead
const index = targetQueue.indexOf(call as QueuedRuntimeCall<unknown>, head)
if (index === -1) {
return
}
targetQueue.splice(index, 1)
this.queuedCallCount = Math.max(0, this.queuedCallCount - 1)
this.retainedCallBytes = Math.max(0, this.retainedCallBytes - call.retainedBytes)
call.reject(abortSignalReason(call.signal))
if (queue.active === 0 && this.isEmpty(queue)) {
this.queues.delete(selector)
return
}
this.pump(selector, queue)
}
private takeForeground(queue: RuntimeCallQueue): QueuedRuntimeCall<unknown> | undefined {
if (queue.foregroundHead >= queue.foreground.length) {
return undefined
}
const call = queue.foreground[queue.foregroundHead]
queue.foreground[queue.foregroundHead] = undefined
queue.foregroundHead += 1
this.queuedCallCount = Math.max(0, this.queuedCallCount - 1)
this.compactForeground(queue)
return call
}
private takeBackground(queue: RuntimeCallQueue): QueuedRuntimeCall<unknown> | undefined {
if (queue.backgroundHead >= queue.background.length) {
return undefined
}
const call = queue.background[queue.backgroundHead]
queue.background[queue.backgroundHead] = undefined
queue.backgroundHead += 1
this.queuedCallCount = Math.max(0, this.queuedCallCount - 1)
this.compactBackground(queue)
return call
}
private compactForeground(queue: RuntimeCallQueue): void {
if (queue.foregroundHead <= 32 || queue.foregroundHead * 2 < queue.foreground.length) {
return
}
// Head indexes avoid repeated shifts; compaction bounds the consumed prefix.
queue.foreground.splice(0, queue.foregroundHead)
queue.foregroundHead = 0
}
private compactBackground(queue: RuntimeCallQueue): void {
if (queue.backgroundHead <= 32 || queue.backgroundHead * 2 < queue.background.length) {
return
}
queue.background.splice(0, queue.backgroundHead)
queue.backgroundHead = 0
}
private isEmpty(queue: RuntimeCallQueue): boolean {
return (
queue.foregroundHead >= queue.foreground.length &&
queue.backgroundHead >= queue.background.length
)
}
private queuedCount(queue: RuntimeCallQueue): number {
return (
queue.foreground.length -
queue.foregroundHead +
queue.background.length -
queue.backgroundHead
)
}
}