Files
orca/src/shared/native-chat-session-options.ts
T
Brennan Benson c287a5d9b7 feat(native-chat): add provider-aware Fast mode (#20506)
* feat(native-chat): add provider-aware fast mode

* chore: drop unrelated formatter churn from the merge

pnpm format reflowed pnpm-workspace.yaml quoting and a source-scan test
that this PR does not otherwise touch.

* fix(native-chat): review fixes for provider-aware fast mode

Review pass over the Fast mode work.

Claude reads its model catalog once per option write. The admit check, the
effort guard and the Fast guard each took their own `list_models`, so a model
write with Fast on paid two round trips for one list and let two guards answer
from two different catalogs. The guards are now pure over a single read.

Claude no longer refuses a Fast enable when the catalog identified nothing at
all. An empty list is not evidence against a model -- the same rule the model
admit-check already applies -- so a CLI that cannot answer would otherwise have
Fast refused on every model. A catalog that did list the model and stayed silent
about Fast is still not positive evidence and keeps refusing.

Codex refuses a direct `serviceTier` write instead of accepting one the next
turn discards. The turn derives the tier from `fastMode`; the key still restores
so a session persisted before Fast existed migrates.

Both option surfaces return a cached snapshot again. `SessionOptionsSurface` is
read through `useSyncExternalStore`, whose contract is a stable snapshot, and
rebuilding it per call breaks that for any consumer wired that way.

Also records two decisions that were emergent rather than stated: routing
Standard when Fast is on but no tier is named yet, and what a readback
disagreement does and does not prove.

Quality gate: merges the duplicate imports static analysis flagged, adds SAFETY
rationales for two pre-existing casts the changed-code gate now sees, and drops
a new assertion in favour of a checked narrowing.

* fix(native-chat): read Claude Fast state from the session frame

A fresh Claude session reports `fastModeState` while the settings readback still
has no `fastMode` boolean, so the two are not redundant -- the frame answers at a
moment the boolean has none. The picker fell back to "value unknown" and asked
the user to disambiguate what the provider had already reported, and the state it
reported had no reader at all.

Falls back to the frame only when neither a pick nor the settings readback
answers. `cooldown` throttles routing rather than clearing the pick, so it reads
as on; reading it as off would flip a control nobody touched.

Display only. The launch seed is untouched: an unset Fast preference still seeds
nothing, which its own guard continues to pin.

* perf(native-chat): skip the model catalog read when turning Fast off

Turning Fast off needs no support evidence, so the read only cost a
round trip — and restore replays a stored `false` on every acquire.

Also narrows the alias-matcher comment: the effort and admit guards
match on alias and resolved id only, so calling it the sole matcher
overstated it.

* fix(native-chat): clear a Claude Fast block once the child stops reporting it

The child omits fast_mode_disabled_reason entirely when nothing blocks Fast
and never sends a null, so requiring the key back latched the first reason
for the session's life: switching to a model that disallows Fast and back
retired the control for good, leaving a session running Fast with no way to
turn it off. A frame that reports state without a reason is the all-clear.

* test(native-chat): cover the mobile structured option hook

useMobileStructuredAgentOptions gained generation fencing, a pending-write
guard and a post-write options refresh with no test file. Pins the concurrency
contract and the fast mode round trip:

- a superseded options read is dropped instead of overwriting newer state
- an overlapping write is refused and the pending guard is released after
- an accepted same-fence write reads options back and applies the result,
  and a different-fence write does not
- a boolean fastMode pick reaches the wire encoded and is remembered decoded
- no Fast row when session support, catalog support or the model capability
  is missing

Each behaviour was ablated against the production logic to confirm it fails
without it. No production code changed.

* feat(native-chat): render a boolean session option as one toggle

On and Off were two radio rows under a header repeating the option name,
so a binary choice cost three lines and two clicks to read. It is now a
single switch row that owns its label, on desktop and mobile.

An unknown value keeps its caption: a switch cannot say "unset".

* fix(native-chat): resolve a boolean option's display value at the producer

A boolean session option reached the UI in three states while its control had
only two, so the renderer apologised for the gap with a "Current value unknown"
caption beside a switch that had already collapsed to off. For `thinking`, whose
catalog default is on, that caption sat next to a switch asserting the opposite
of what every composed dispatch assumes.

One expression fed both the displayed value and the option's provenance. Split
them: the boolean descriptor now always carries a value, resolved to the same
`values[id] ?? defaultValue` that buildNativeChatSessionOptionCommand already
composes, while `valueSource` is untouched and still records whether anything
confirmed it. `kind.currentValue` is required on the boolean arm so the third
state cannot come back.

The launch path is unaffected: resolveAgentSessionOptionLaunch and
buildNativeChatSessionOptionCommand build the composed `--model` argument from
the caller's picks and the catalog, never from a descriptor.

Both surfaces mark an unconfirmed value instead of captioning it, and the two
reasons stay distinct — `default` says the catalog value is what a launch will
send, `unreported` says nothing has told us anything. Only `unreported` is
reachable in the structured lane, where the agent may be routing a tier we have
never been told about, so the two never share a label.

* fix(native-chat): let assistive tech read the option value marker

The marker was aria-hidden next to an explicit aria-label, so the label
already won the accessible name and hiding it only cost screen reader
users the default-vs-unreported distinction that sighted users get. It is
now referenced by aria-describedby, which keeps the name Fast mode.

Mobile's summary row said "Not set" for a boolean while the sheet behind
it showed the switch on, so the two screens disagreed. A boolean always
has a value; the summary states it and the sheet's marker qualifies it.

* chore(i18n): drop the On/Off option strings the switch row retired

Replacing the On/Off radio pair removed the only call sites for these two
keys. i18next cannot rebuild a key with no call-site default, so leaving
them in the catalogs forced them into the boot bundle as dead weight.
Removing them shrinks it by two entries instead.
2026-09-13 21:58:32 -07:00

123 lines
4.9 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
/** 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
}