Files
orca/src/main/codex/codex-structured-session-options.test.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

485 lines
16 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { CodexAppServerConnection } from './codex-app-server-connection'
import { CodexAcquisitionWindow } from './codex-structured-acquisition-window'
import {
applyCodexStructuredSessionOption,
readCodexStructuredSessionOptions,
readLiveCodexSessionOptions,
restoredCodexSessionOptions
} from './codex-structured-session-options'
import { reportedCodexThreadOptions } from './codex-structured-fast-mode'
import { CodexBackgroundTaskTracker } from './codex-background-task-tracker'
import type { CodexSession } from './codex-structured-session-state'
import { startCodexTurn } from './codex-structured-turn-start'
function optionSession(request: CodexAppServerConnection['request']): CodexSession {
return {
connection: {
pid: 1,
closed: false,
request,
notify: () => {},
respond: () => {},
respondWithError: () => {},
close: async () => true
},
backgroundTasks: new CodexBackgroundTaskTracker('thread-1'),
ended: false,
requestedClose: false,
fence: 1,
acquisitionGeneration: 'generation-1',
threadId: 'thread-1',
historyPath: null,
prompts: new CodexAcquisitionWindow().prompts,
options: new Map(),
reportedOptions: { model: 'gpt-live', effort: 'high' },
fastModeTierByModel: new Map(),
turnIdWaiters: [],
translator: null
}
}
describe('structured Codex session options', () => {
it('filters restored records to recognized turn options', () => {
expect(
Object.fromEntries(
restoredCodexSessionOptions({
model: 'gpt-live',
effort: 'high',
threadId: 'thread-injected',
input: 'input-injected'
})
)
).toEqual({ model: 'gpt-live', effort: 'high' })
expect(Object.fromEntries(restoredCodexSessionOptions({ serviceTier: 'default' }))).toEqual({
fastMode: 'false'
})
})
it('hydrates paged provider models and their supported efforts', async () => {
const request = vi.fn(async (_method: string, params?: Record<string, unknown>) =>
params?.cursor
? {
data: [
{
model: 'gpt-second',
displayName: 'GPT Second',
description: 'Fast',
hidden: false,
supportedReasoningEfforts: [
{ reasoningEffort: 'low', description: 'Quick reasoning' }
],
defaultReasoningEffort: 'low',
isDefault: false
}
],
nextCursor: null
}
: {
data: [
{
model: 'gpt-live',
displayName: 'GPT Live',
hidden: false,
supportedReasoningEfforts: [
{ reasoningEffort: 'medium', description: 'Balanced' },
{ reasoningEffort: 'high', description: 'Deep reasoning' }
],
defaultReasoningEffort: 'medium',
isDefault: true
}
],
nextCursor: 'page-2'
}
)
await expect(
readCodexStructuredSessionOptions({
connection: { request } as never,
current: { model: 'gpt-live', effort: 'medium' }
})
).resolves.toEqual({
models: [
{
id: 'gpt-live',
label: 'GPT Live',
isDefault: true,
defaultEffort: 'medium',
efforts: [
{ value: 'medium', label: 'Medium', description: 'Balanced' },
{ value: 'high', label: 'High', description: 'Deep reasoning' }
]
},
{
id: 'gpt-second',
label: 'GPT Second',
description: 'Fast',
isDefault: false,
defaultEffort: 'low',
efforts: [{ value: 'low', label: 'Low', description: 'Quick reasoning' }]
}
],
current: { model: 'gpt-live', effort: 'medium' }
})
expect(request).toHaveBeenNthCalledWith(
2,
'model/list',
{ limit: 100, includeHidden: false, cursor: 'page-2' },
{ timeoutMs: undefined }
)
})
it('hydrates current values from thread start or resume', () => {
expect(
reportedCodexThreadOptions({
threadId: 'thread-1',
historyPath: null,
model: 'gpt-live',
effort: 'high'
})
).toEqual({ model: 'gpt-live', effort: 'high' })
})
it('reconciles an incompatible effort when only the model changes', async () => {
const session = optionSession(
vi.fn(async () => ({
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [{ reasoningEffort: 'high' }],
defaultReasoningEffort: 'high'
},
{
model: 'gpt-fast',
supportedReasoningEfforts: [{ reasoningEffort: 'low' }],
defaultReasoningEffort: 'low'
}
],
nextCursor: null
}))
)
await expect(
applyCodexStructuredSessionOption(session, 'model', 'gpt-fast', undefined)
).resolves.toEqual({ model: 'gpt-fast', effort: 'low' })
})
it('rejects values absent from the provider catalog', async () => {
const session = optionSession(
vi.fn(async () => ({
data: [{ model: 'gpt-live', supportedReasoningEfforts: [] }],
nextCursor: null
}))
)
await expect(
applyCodexStructuredSessionOption(session, 'model', 'not-entitled', undefined)
).rejects.toThrow('does not offer model not-entitled')
await expect(
applyCodexStructuredSessionOption(session, 'effort', 'high', undefined)
).rejects.toThrow('does not support high')
})
it('maps canonical Fast on and off to the exact advertised tier and Standard', async () => {
const requests: { method: string; params?: Record<string, unknown> }[] = []
const request = vi.fn(async (method: string, params?: Record<string, unknown>) => {
requests.push({ method, params })
return method === 'model/list'
? {
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
serviceTiers: [
{ id: 'rush-v7', name: 'Fast', description: 'Provider-routed Fast tier' }
]
}
],
nextCursor: null
}
: { turn: { id: `turn-${requests.length}` } }
})
const session = optionSession(request)
await expect(
applyCodexStructuredSessionOption(session, 'fastMode', 'true', undefined)
).resolves.toMatchObject({ fastMode: 'true' })
await startCodexTurn(session, {
clientMessageId: 'message-on',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'on' }] }
})
expect(requests.find((entry) => entry.method === 'turn/start')?.params).toMatchObject({
serviceTier: 'rush-v7'
})
await applyCodexStructuredSessionOption(session, 'fastMode', 'false', undefined)
await startCodexTurn(session, {
clientMessageId: 'message-off',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'off' }] }
})
expect(requests.filter((entry) => entry.method === 'turn/start')[1]?.params).toMatchObject({
serviceTier: 'default'
})
})
it('reports the current Fast value only when the opened thread tier matches the catalog', async () => {
const connection = {
request: vi.fn(async () => ({
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
serviceTiers: [{ id: 'priority-current', name: 'Fast' }]
}
],
nextCursor: null
}))
}
await expect(
readCodexStructuredSessionOptions({
connection,
current: { model: 'gpt-live' },
reportedServiceTier: 'priority-current',
reportedServiceTierKnown: true
})
).resolves.toMatchObject({ current: { fastMode: true, confirmed: ['fastMode'] } })
const unknown = await readCodexStructuredSessionOptions({
connection,
current: { model: 'gpt-live' },
reportedServiceTier: 'unrecognized-tier',
reportedServiceTierKnown: true
})
expect(unknown.current).toEqual({ model: 'gpt-live' })
})
it('hides and rejects Fast mode when the running catalog does not advertise it', async () => {
const session = optionSession(
vi.fn(async () => ({
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
serviceTiers: []
}
],
nextCursor: null
}))
)
await expect(
readCodexStructuredSessionOptions({
connection: session.connection,
current: { model: 'gpt-live' }
})
).resolves.toMatchObject({
models: [expect.objectContaining({ supportsFastMode: false })],
fastModeSupport: { supported: false }
})
await expect(
applyCodexStructuredSessionOption(session, 'fastMode', 'true', undefined)
).rejects.toThrow('does not support Fast mode')
})
it('reconciles restored Fast on to explicit Standard when the selected model lost support', async () => {
const requests: { method: string; params?: Record<string, unknown> }[] = []
const session = optionSession(
vi.fn(async (method: string, params?: Record<string, unknown>) => {
requests.push({ method, params })
return method === 'model/list'
? {
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
serviceTiers: []
}
],
nextCursor: null
}
: { turn: { id: 'turn-standard' } }
})
)
session.options.set('fastMode', 'true')
await expect(readLiveCodexSessionOptions(session, undefined)).resolves.toMatchObject({
current: { fastMode: false }
})
expect(Object.fromEntries(session.options)).toEqual({ fastMode: 'false' })
await startCodexTurn(session, {
clientMessageId: 'message-standard',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'standard' }] }
})
expect(requests.find((entry) => entry.method === 'turn/start')?.params).toMatchObject({
serviceTier: 'default'
})
})
it('uses Standard until a missing Fast catalog recovers without losing restored intent', async () => {
const requests: { method: string; params?: Record<string, unknown> }[] = []
let catalogRecovered = false
const request = vi.fn(async (method: string, params?: Record<string, unknown>) => {
requests.push({ method, params })
if (method === 'turn/start') {
return { turn: { id: `turn-${requests.length}` } }
}
return {
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
...(catalogRecovered
? { serviceTiers: [{ id: 'priority-recovered', name: 'Fast' }] }
: {})
}
],
nextCursor: null
}
})
const session = optionSession(request)
session.options.set('fastMode', 'true')
const unknown = await readLiveCodexSessionOptions(session, undefined)
expect(unknown).toMatchObject({
current: { fastMode: true }
})
expect(unknown.fastModeSupport).toBeUndefined()
expect(unknown.models[0]?.supportsFastMode).toBeUndefined()
expect(session.options.get('fastMode')).toBe('true')
await startCodexTurn(session, {
clientMessageId: 'message-unverified',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'unverified' }] }
})
expect(requests.find((entry) => entry.method === 'turn/start')?.params).toMatchObject({
serviceTier: 'default'
})
expect(session.options.get('fastMode')).toBe('true')
catalogRecovered = true
await expect(readLiveCodexSessionOptions(session, undefined)).resolves.toMatchObject({
models: [expect.objectContaining({ supportsFastMode: true })],
fastModeSupport: { supported: true },
current: { fastMode: true }
})
await startCodexTurn(session, {
clientMessageId: 'message-recovered',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'recovered' }] }
})
expect(requests.filter((entry) => entry.method === 'turn/start')[1]?.params).toMatchObject({
serviceTier: 'priority-recovered'
})
})
it('allows explicit Fast off without positive model support', async () => {
const requests: { method: string; params?: Record<string, unknown> }[] = []
const session = optionSession(
vi.fn(async (method: string, params?: Record<string, unknown>) => {
requests.push({ method, params })
return method === 'model/list'
? {
data: [{ model: 'gpt-live', supportedReasoningEfforts: [] }],
nextCursor: null
}
: { turn: { id: 'turn-standard' } }
})
)
await expect(
applyCodexStructuredSessionOption(session, 'fastMode', 'false', undefined)
).resolves.toMatchObject({ fastMode: 'false' })
await startCodexTurn(session, {
clientMessageId: 'message-standard',
body: { kind: 'message', role: 'user', blocks: [{ type: 'text', text: 'standard' }] }
})
expect(requests.find((entry) => entry.method === 'turn/start')?.params).toMatchObject({
serviceTier: 'default'
})
})
it('uses only the bounded legacy Fast tier value the provider advertised', async () => {
const result = await readCodexStructuredSessionOptions({
connection: {
request: vi.fn(async () => ({
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
additionalSpeedTiers: ['fast']
}
],
nextCursor: null
}))
},
current: { model: 'gpt-live' }
})
expect(result.models[0]).toMatchObject({ supportsFastMode: true })
expect(result.fastModeSupport).toEqual({ supported: true })
})
it('normalizes a legacy durable tier while preserving a canonical explicit choice', async () => {
const request = vi.fn(async () => ({
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
serviceTiers: [{ id: 'priority-migrated', name: 'Fast' }]
}
],
nextCursor: null
}))
const migrated = optionSession(request)
migrated.options.set('serviceTier', 'priority-migrated')
await expect(readLiveCodexSessionOptions(migrated, undefined)).resolves.toMatchObject({
current: { fastMode: true }
})
expect(Object.fromEntries(migrated.options)).toEqual({ fastMode: 'true' })
const canonical = optionSession(request)
canonical.options.set('fastMode', 'false')
canonical.options.set('serviceTier', 'priority-migrated')
await readLiveCodexSessionOptions(canonical, undefined)
expect(Object.fromEntries(canonical.options)).toEqual({ fastMode: 'false' })
})
it('reconciles Fast off when switching to an unsupported model', async () => {
const session = optionSession(
vi.fn(async () => ({
data: [
{
model: 'gpt-live',
supportedReasoningEfforts: [],
serviceTiers: [{ id: 'priority-x', name: 'Fast', description: 'Fast' }]
},
{ model: 'gpt-standard', supportedReasoningEfforts: [], serviceTiers: [] }
],
nextCursor: null
}))
)
session.options.set('fastMode', 'true')
await expect(
applyCodexStructuredSessionOption(session, 'model', 'gpt-standard', undefined)
).resolves.toMatchObject({ model: 'gpt-standard', fastMode: 'false' })
})
})
describe('Codex service tier is not a settable option', () => {
/** The turn derives the tier from `fastMode`, so accepting a direct write would
* report success for a value the next turn discards. Restore still reads the key
* so a session persisted before Fast existed migrates. */
it('refuses a direct serviceTier write while still restoring a legacy one', async () => {
const session = optionSession(async () => ({ data: [] }))
await expect(
applyCodexStructuredSessionOption(session, 'serviceTier', 'priority', undefined)
).rejects.toThrow('cannot be set directly')
expect(session.options.has('serviceTier')).toBe(false)
expect(Object.fromEntries(restoredCodexSessionOptions({ serviceTier: 'default' }))).toEqual({
fastMode: 'false'
})
})
})