Files
orca/src/shared/native-chat-session-options.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

125 lines
5.0 KiB
TypeScript

import type { AgentType } from './agent-status-types'
export type SessionOptionValue = string | boolean
export type SessionOptionSelectChoice = {
value: string
label: string
description?: string
}
/** `default` is the catalog's own value shown before anything is observed —
* truthful to display, but never evidence about a running agent. `dispatched`
* is sent-but-unread: the pill shows it, and a later report that disagrees is
* what corrects it. Both transports emit it, so it alone names neither — see
* `transport` on the descriptor. */
export type SessionOptionValueSource = 'applied' | 'dispatched' | 'reported' | 'default' | 'unknown'
/** How a live value reaches the agent. `catalog` types the catalog's command into
* the agent's terminal and can only learn the outcome by parsing the screen back;
* `agent-session` writes over the structured protocol, which reports every turn. */
export type NativeChatLiveOptionTransport = 'catalog' | 'agent-session'
/** Closed set of reasons an option is not settable in the current mode. A key
* (not free English) so the producer and the localized label stay in sync —
* an exhaustive switch turns any drift into a type error instead of leaking
* untranslated text. */
export type SessionOptionDisabledReason =
| 'available-after-session-start'
| 'set-when-session-starts'
export type SessionOptionDescriptor = {
id: string
label: string
description?: string
category?: 'model' | 'thought_level' | 'model_config' | 'mode'
kind:
| {
type: 'select'
currentValue?: string
choices: SessionOptionSelectChoice[]
}
/** Required, unlike the select's: a switch has no third position, so a value
* the producer left unset would render as `false` and assert the opposite of
* the catalog default. Whether anything confirmed it is `valueSource`'s job. */
| {
type: 'boolean'
currentValue: boolean
}
valueSource: SessionOptionValueSource
/** Required so a new producer cannot inherit the wrong lane's rendering by
* omission — `dispatched` is emitted identically by both and cannot discriminate. */
transport: NativeChatLiveOptionTransport
settable: boolean
disabledReason?: SessionOptionDisabledReason
/** The host is still listing this option's choices: the pill keeps its value but cannot open. */
choicesPending?: true
/** Why: picker-only and toggle-only PTY commands cannot be represented as
* a truthful radio/checkbox state, so the producer exposes an action row. */
action?: { type: 'agent-picker' | 'toggle-command' }
}
/** A value we typed at the agent and have never read back. Both lanes write
* `dispatched` on a set, so the source alone does not name one — the transport
* check is what limits the caption to the terminal, where reading the screen
* back is the only confirmation available. */
export function sessionOptionDispatchUnconfirmed(
descriptor: Pick<SessionOptionDescriptor, 'valueSource' | 'transport'>
): boolean {
return descriptor.valueSource === 'dispatched' && descriptor.transport === 'catalog'
}
/** Why a boolean row needs a marker at all: the switch always renders a value, so
* the row is the only place that can say where the value came from. `default` and
* `unreported` are opposite claims — the first says the catalog value is what a
* launch will send, the second says nothing has told us anything and the agent may
* be running something else entirely — so they never share one label. */
export type SessionOptionValueMarker = 'default' | 'unreported'
/** Display-only: it labels a rendered value and never gates what is sent, which
* stays sourced from tracked picks. */
export function sessionOptionValueMarker(
descriptor: Pick<SessionOptionDescriptor, 'valueSource'>
): SessionOptionValueMarker | null {
if (descriptor.valueSource === 'default') {
return 'default'
}
return descriptor.valueSource === 'unknown' ? 'unreported' : null
}
export type SessionOptionSetResult = {
snapshot: SessionOptionDescriptor[]
}
export type PersistedNativeChatSessionOptions = Partial<
Record<
string,
{
model?: string
valuesByModel?: Record<string, Record<string, SessionOptionValue>>
}
>
>
export type NativeChatSessionOptionSettingsMutation =
| {
type: 'apply-picks'
agent: AgentType
picks: readonly {
modelId: string
optionId: string
value: SessionOptionValue
adoptModelAsLaunchDefault?: boolean
}[]
}
| { type: 'clear-model-if-missing'; agent: AgentType; availableModelIds: readonly string[] }
export type SessionOptionsSurface = {
getSnapshot(): SessionOptionDescriptor[]
/** Apply an absolute target; known flip-only options use their tracked baseline. */
setOption(id: string, value: SessionOptionValue): Promise<SessionOptionSetResult>
/** Invoke the value-less action exposed by the current descriptor. */
invokeAction(id: string): Promise<SessionOptionSetResult>
subscribe(listener: (snapshot: SessionOptionDescriptor[]) => void): () => void
}