Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/main/claude/claude-structured-session-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,13 +592,9 @@ describe('ClaudeStructuredSessionAdapter turns and controls', () => {
const adapter = await acquired(claude)
const result = await adapter.readOptions({ sessionId: 'session-1', fence: 7 })

expect(result.models.map((model) => model.id)).toEqual([
'fable',
'opus',
'sonnet',
'haiku',
'custom-model'
])
// Was: a trailing `custom-model` row. The seed is a list of available ids, and the
// init frame's unlisted model is reported as current without being offered.
expect(result.models.map((model) => model.id)).toEqual(['fable', 'opus', 'sonnet', 'haiku'])
expect(result.current).toEqual({
model: 'custom-model',
effort: 'high',
Expand Down
65 changes: 65 additions & 0 deletions src/main/claude/claude-structured-session-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, it } from 'vitest'
import { readClaudeStructuredSessionOptions } from './claude-structured-session-options'
import type { ClaudeSession } from './claude-structured-session-state'

// `list_models` rows as the CLI reports them: the `default` row carries only the
// resolved id, which is what marks the listed alias that resolves to the same model.
const LISTED_ROWS = [
{ value: 'default', resolvedModel: 'claude-opus-5[1m]' },
{
value: 'opus[1m]',
displayName: 'Opus (1M context)',
resolvedModel: 'claude-opus-5[1m]',
supportsEffort: true,
supportedEffortLevels: ['low', 'high']
},
{ value: 'sonnet', displayName: 'Sonnet', resolvedModel: 'claude-sonnet-5' }
]

function optionsSession(reportedModel: string): ClaudeSession {
return {
connection: { supportedModels: async () => LISTED_ROWS },
options: new Map(),
reportedOptions: { model: reportedModel },
optionMutationSequence: 0,
reportedModelMutation: 0,
confirmedOptions: new Set()
} as unknown as ClaudeSession
}

describe('readClaudeStructuredSessionOptions', () => {
it('names the listed alias the init frame’s resolved id belongs to', async () => {
// The pill shows a picked Opus as `opus[1m]`, not as the resolved id the session
// reports — the mapping, not a fabricated row, is what keeps that model nameable.
const options = await readClaudeStructuredSessionOptions(
optionsSession('claude-opus-5[1m]'),
undefined
)

expect(options.current).toEqual({ model: 'opus[1m]', confirmed: ['model'] })
expect(options.models).toEqual([
{
id: 'opus[1m]',
label: 'Opus (1M context)',
isDefault: true,
efforts: [
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' }
]
},
{ id: 'sonnet', label: 'Sonnet', isDefault: false, efforts: [] }
])
})

it('reports an unlisted current model without listing it', async () => {
// Was: a fabricated `{ id: model, label: model, efforts: [] }` row, which offered a
// raw launch flag (`worker-start --model claude-opus-5`) as if the CLI listed it.
const options = await readClaudeStructuredSessionOptions(
optionsSession('claude-opus-5'),
undefined
)

expect(options.current.model).toBe('claude-opus-5')
expect(options.models.map((model) => model.id)).toEqual(['opus[1m]', 'sonnet'])
})
})
6 changes: 3 additions & 3 deletions src/main/claude/claude-structured-session-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ export async function readClaudeStructuredSessionOptions(
const discovered = listedModels(catalog ? { models: catalog } : null)
const models = discovered.length > 0 ? discovered : seedModels()
const current = readClaudeCurrentModel(session)
// `models` stays the CLI's own list (or the seed): `currentModelId` already maps a
// resolved id back onto a listed row, so what survives unmatched is an id the CLI
// never offered. It is reported as current — the session runs it — but not listed.
const model = currentModelId(models, current.id)
if (!models.some((entry) => entry.id === model)) {
models.push({ id: model, label: model, isDefault: false, efforts: [], resolvedModel: null })
}
const effort = session.options.get('effort') ?? session.reportedOptions.effort
const confirmed = [
...(current.confirmed ? ['model'] : []),
Expand Down
19 changes: 19 additions & 0 deletions src/main/codex/codex-structured-session-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ describe('structured Codex session options', () => {
)
})

it('reports an unlisted current model without offering it as a choice', async () => {
// Was: a fabricated `{ id, label: id, efforts: [] }` row, which offered a raw launch
// id in the picker as though the account were entitled to it.
const request = vi.fn(async () => ({
data: [{ model: 'gpt-live', displayName: 'GPT Live', isDefault: true }],
nextCursor: null
}))

await expect(
readCodexStructuredSessionOptions({
connection: { request } as never,
current: { model: 'gpt-unlisted' }
})
).resolves.toEqual({
models: [{ id: 'gpt-live', label: 'GPT Live', isDefault: true, efforts: [] }],
current: { model: 'gpt-unlisted' }
})
})

it('hydrates current values from thread start or resume', () => {
expect(
reportedCodexThreadOptions({
Expand Down
10 changes: 2 additions & 8 deletions src/main/codex/codex-structured-session-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,8 @@ export async function readCodexStructuredSessionOptions(input: {
break
}
}
if (input.current.model && !models.some((model) => model.id === input.current.model)) {
models.push({
id: input.current.model,
label: input.current.model,
isDefault: false,
efforts: []
})
}
// `models` stays what `model/list` offered: a current id the account cannot select is
// still reported (the thread runs it) but never listed, so no raw id is offerable.
const model = input.current.model ?? models.find((entry) => entry.isDefault)?.id ?? models[0]?.id
if (!model) {
throw new Error('codex app-server returned no available models')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,21 +593,23 @@ describe('native chat PTY session options', () => {
expect(surface.getSnapshot()[0]).toMatchObject({ valueSource: 'unknown' })
})

it('passes an unknown persisted model through as a literal choice', () => {
seedNativeChatAppliedSessionOptions('pty-1', 'claude', {
model: 'future-model'
})
it('withholds an unknown persisted model instead of offering it as a choice', () => {
// Was: a literal `{ value: id, label: id }` row. A launch flag the CLI never listed
// (`worker-start --model claude-opus-5`) names no model we can offer, so the pill
// reads `unknown` and the dropdown stays available-models-only.
seedNativeChatAppliedSessionOptions('pty-1', 'claude', { model: 'future-model' })
const surface = createNativeChatPtySessionOptions({
agent: 'claude',
scopeKey: 'pty-1',
mode: 'live',
dispatchCommand: vi.fn()
})!
const model = surface.getSnapshot()[0]
expect(model.kind).toMatchObject({
currentValue: 'future-model',
choices: expect.arrayContaining([{ value: 'future-model', label: 'future-model' }])
})
expect(model).toMatchObject({ valueSource: 'unknown', kind: { type: 'select' } })
expect(model.kind.type === 'select' ? model.kind.currentValue : 'set').toBeUndefined()
expect(
model.kind.type === 'select' ? model.kind.choices.map(({ value }) => value) : []
).not.toContain('future-model')
})

it('keeps a tracked alias selectable when the host catalog omits it', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import {
nativeChatSessionChoiceLabel
} from './native-chat-session-option-labels'
import type { SessionOptionDescriptor } from '../../../../shared/native-chat-session-options'
import { CLAUDE_SESSION_OPTION_CATALOG } from '../../../../shared/agent-session-option-catalog-claude-codex'
import {
buildNativeChatSessionOptionSnapshot,
withTrackedNativeChatModel
} from '../../../../shared/native-chat-session-option-snapshot'
import { createNativeChatSessionOptionRecord } from '../../../../shared/native-chat-session-option-state'

vi.mock('@/i18n/i18n', () => ({
translate: vi.fn((_key: string, fallback: string) => fallback)
Expand Down Expand Up @@ -44,6 +50,28 @@ describe('nativeChatModelPillLabel', () => {
expect(nativeChatModelPillLabel(modelDescriptor('default'))).toBe('Model')
})

it('withholds a launch flag no list carried, end to end from the builder', () => {
// `worker-start --model claude-opus-5` tracks an id neither the discovered list nor
// the seed carries, so the composer's pill must read the neutral category rather
// than echoing back the string the launch was typed with.
const record = createNativeChatSessionOptionRecord('claude')
record.model = { value: 'claude-opus-5', source: 'reported' }
const snapshot = buildNativeChatSessionOptionSnapshot({
catalog: CLAUDE_SESSION_OPTION_CATALOG,
models: withTrackedNativeChatModel(
CLAUDE_SESSION_OPTION_CATALOG,
CLAUDE_SESSION_OPTION_CATALOG.models,
record
),
record,
mode: 'live',
modelLabel: 'Model',
liveTransport: 'catalog'
})

expect(nativeChatModelPillLabel(snapshot[0]!)).toBe('Model')
})

it('falls back to the raw id when the list no longer offers it', () => {
// A discovered list can drop an id the record still tracks; showing the id beats
// showing "Model" while a real model is running.
Expand Down
56 changes: 49 additions & 7 deletions src/shared/native-chat-session-option-snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,12 @@ describe('buildNativeChatSessionOptionSnapshot', () => {
expect(model.kind.choices.map((choice) => choice.value)).toEqual(
CLAUDE_SESSION_OPTION_CATALOG.models.map((catalogModel) => catalogModel.id)
)
// Nor does it name one it cannot offer: the trigger would show a raw id.
expect(model.kind.currentValue).toBeUndefined()
expect(model).toMatchObject({ valueSource: 'unknown' })
})

it('is empty when the model list is empty', () => {
it('is empty when the model list is empty and nothing is tracked', () => {
expect(
buildNativeChatSessionOptionSnapshot({
catalog: CLAUDE_SESSION_OPTION_CATALOG,
Expand All @@ -119,6 +122,27 @@ describe('buildNativeChatSessionOptionSnapshot', () => {
).toEqual([])
})

it('keeps the model row when the provider listed no models at all', () => {
// A restored Codex thread whose `model/list` came back empty still runs a model —
// `readCodexStructuredSessionOptions` refuses only when nothing resolves at all — so
// the row has to survive the blank picker instead of taking the pill with it.
const record = createNativeChatSessionOptionRecord('codex')
record.model = { value: 'gpt-5.9-secret', source: 'reported' }
const snapshot = buildNativeChatSessionOptionSnapshot({
catalog: { ...CODEX_SESSION_OPTION_CATALOG, models: [], defaultModelIsCliDefault: true },
models: [],
record,
mode: 'live',
modelLabel: 'Model',
liveTransport: 'agent-session'
})
expect(snapshot.map((descriptor) => descriptor.id)).toEqual(['model'])
const model = snapshot[0]!
// No id is offerable, and the raw one never reaches the trigger.
expect(model.kind.type === 'select' ? model.kind.choices : null).toEqual([])
expect(model).toMatchObject({ valueSource: 'unknown' })
})

describe('sortNativeChatSessionOptions', () => {
it('drops the model row and orders effort before model config before modes', () => {
const descriptor = (id: string, category?: string): SessionOptionDescriptor =>
Expand Down Expand Up @@ -178,19 +202,37 @@ describe('buildNativeChatSessionOptionSnapshot', () => {
expect(snapshot.length).toBeGreaterThan(1)
})

it('labels a wholly unknown tracked model by its id rather than dropping it', () => {
it('offers no row for a tracked id neither the discovered list nor the seed carries', () => {
// Was: a fabricated `{ id, label: id, options: [] }` row, which put a raw launch
// flag (`worker-start --model claude-opus-5`) in the picker and in the pill as
// though the CLI had listed it. Only available models may be offered or named.
const record = claudeRecord()
record.model = { value: 'experimental-model', source: 'reported' }
record.model = { value: 'claude-opus-5', source: 'reported' }
const reconciled = withTrackedNativeChatModel(
CLAUDE_SESSION_OPTION_CATALOG,
CLAUDE_SESSION_OPTION_CATALOG.models,
record
)
expect(reconciled.at(-1)).toEqual({
id: 'experimental-model',
label: 'experimental-model',
options: []
expect(reconciled).toEqual([...CLAUDE_SESSION_OPTION_CATALOG.models])

const snapshot = buildNativeChatSessionOptionSnapshot({
catalog: CLAUDE_SESSION_OPTION_CATALOG,
models: reconciled,
record,
mode: 'live',
modelLabel: 'Model',
liveTransport: 'catalog'
})
const model = snapshot[0]!
if (model.kind.type !== 'select') {
throw new Error('model descriptor must be a select')
}
// `unknown` is what nativeChatModelPillLabel reads to render the neutral "Model".
expect(model).toMatchObject({ valueSource: 'unknown' })
expect(model.kind.currentValue).toBeUndefined()
expect(model.kind.choices.some((choice) => choice.value === 'claude-opus-5')).toBe(false)
// Restoring this session's effort row from `unknownModelOptions` is a follow-up.
expect(snapshot.map((descriptor) => descriptor.id)).toEqual(['model'])
})

it('leaves the list alone when the tracked model is already listed', () => {
Expand Down
39 changes: 23 additions & 16 deletions src/shared/native-chat-session-option-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ export function sortNativeChatSessionOptions(
}

/**
* Why: the tracked model can sit outside the active list — a persisted default,
* or an alias this host's CLI no longer lists. Keeping a row for it preserves
* the labelled selection and the model's own options instead of blanking both.
* Why: an alias this host's CLI no longer lists is still a real, seeded model, so its
* seed row preserves the labelled selection and the model's own options instead of
* blanking both. An id neither list carries names no model we can offer — the picker
* lists only available models, so it gets no row at all.
* Shared so mobile satisfies the same caller contract the desktop surface does.
*/
export function withTrackedNativeChatModel(
Expand All @@ -172,7 +173,7 @@ export function withTrackedNativeChatModel(
return [...models]
}
const seeded = catalog.models.find((model) => model.id === trackedId)
return [...models, seeded ?? { id: trackedId, label: trackedId, options: [] }]
return seeded ? [...models, seeded] : [...models]
}

/** Why: no tracked model means no `-m` was ever emitted, so the CLI is running its
Expand Down Expand Up @@ -210,21 +211,24 @@ export function buildNativeChatSessionOptionSnapshot(args: {
liveTransport: NativeChatLiveOptionTransport
}): SessionOptionDescriptor[] {
const { catalog, models, record, mode, modelLabel, liveTransport } = args
if (models.length === 0) {
const modelTracked = record.model
const trackedModelId = typeof modelTracked?.value === 'string' ? modelTracked.value : null
const defaultModelId = cliDefaultModelId(catalog, models, trackedModelId)
const effectiveModelId = trackedModelId ?? defaultModelId
// Why: an empty list is a provider that offered nothing, not a session without a model.
// A tracked id still names what the session runs, so its row survives the blank picker.
if (models.length === 0 && !effectiveModelId) {
return []
}
const modelTracked = record.model
// Why: callers reconcile the tracked model into `models` (see
// withTrackedNativeChatModel), so every listed row is a real choice and the
// trigger never shows a value without one.
// Why: every row is an available model — the catalog's or a probe's. A tracked id
// outside both (a raw launch flag, a stale persisted pick) names none of them, so it
// is never offered and never shown as the value.
const modelChoices = models.map(({ id, label, description }) => ({
value: id,
label,
...(description ? { description } : {})
}))
const trackedModelId = typeof modelTracked?.value === 'string' ? modelTracked.value : null
const defaultModelId = cliDefaultModelId(catalog, models, trackedModelId)
const effectiveModelId = trackedModelId ?? defaultModelId
const listedModel = models.find((candidate) => candidate.id === effectiveModelId)
const modelAction = actionForApply(catalog.modelApply, modelTracked, mode, liveTransport)
const snapshot: SessionOptionDescriptor[] = [
{
Expand All @@ -233,10 +237,14 @@ export function buildNativeChatSessionOptionSnapshot(args: {
category: 'model',
kind: {
type: 'select',
...(effectiveModelId ? { currentValue: effectiveModelId } : {}),
...(listedModel ? { currentValue: listedModel.id } : {}),
choices: modelChoices
},
valueSource: modelTracked?.source ?? (defaultModelId ? 'default' : 'unknown'),
// An unlisted id is tracked but unnameable: `unknown` is what the pill reads to
// withhold it, keeping the raw string out of the trigger.
valueSource: listedModel
? (modelTracked?.source ?? (defaultModelId ? 'default' : 'unknown'))
: 'unknown',
transport: liveTransport,
...settableState({ mode, liveTransport, apply: catalog.modelApply }),
...(modelAction ? { action: modelAction } : {})
Expand All @@ -245,9 +253,8 @@ export function buildNativeChatSessionOptionSnapshot(args: {
if (!effectiveModelId) {
return snapshot
}
const model = models.find((candidate) => candidate.id === effectiveModelId)
const trackedValues = record.valuesByModel[effectiveModelId] ?? {}
for (const option of model?.options ?? []) {
for (const option of listedModel?.options ?? []) {
const descriptor = optionDescriptor({
option,
tracked: trackedValues[option.id],
Expand Down
Loading
Loading