Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
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
65 changes: 65 additions & 0 deletions src/main/codex/codex-structured-session-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CodexAcquisitionWindow } from './codex-structured-acquisition-window'
import {
applyCodexStructuredSessionOption,
readCodexStructuredSessionOptions,
readLiveCodexSessionOptions,
reportedCodexThreadOptions,
restoredCodexSessionOptions
} from './codex-structured-session-options'
Expand Down Expand Up @@ -123,6 +124,70 @@ 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('confirms a model the thread reported, but not a pick we restored', async () => {
// The display rule names an unlisted model only when the agent reported it, so the
// reader has to say which of the two answered. `session.options` is a restored pick
// or an unechoed write of ours; `reportedOptions` is the opened thread's own state.
const request = vi.fn(async () => ({
data: [{ model: 'gpt-live', displayName: 'GPT Live', isDefault: true }],
nextCursor: null
}))
const session = (
options: Map<string, string>,
reported: Record<string, string>
): CodexSession =>
({ connection: { request }, options, reportedOptions: reported }) as unknown as CodexSession

await expect(
readLiveCodexSessionOptions(session(new Map(), { model: 'gpt-unlisted' }), undefined)
).resolves.toMatchObject({ current: { model: 'gpt-unlisted', confirmed: ['model'] } })

const restored = await readLiveCodexSessionOptions(
session(new Map([['model', 'gpt-stale']]), {}),
undefined
)
expect(restored.current).toEqual({ model: 'gpt-stale' })
expect(restored.current.confirmed).toBeUndefined()
})

it('does not confirm a model it substituted rather than read', async () => {
// With nothing current the reader picks the default; that is our choice, not a report.
const request = vi.fn(async () => ({
data: [{ model: 'gpt-live', displayName: 'GPT Live', isDefault: true }],
nextCursor: null
}))

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

it('hydrates current values from thread start or resume', () => {
expect(
reportedCodexThreadOptions({
Expand Down
35 changes: 24 additions & 11 deletions src/main/codex/codex-structured-session-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ function modelOption(value: unknown): AgentSessionModelOption | null {
export async function readCodexStructuredSessionOptions(input: {
connection: Pick<CodexAppServerConnection, 'request'>
current: { model?: string; effort?: string }
/** Ids in `current` that came from the thread's own state rather than from a value we
* wrote. Only the caller knows which answered, so it must say. */
confirmed?: readonly string[]
timeoutMs?: number
}): Promise<AgentSessionOptionsResult> {
const models: AgentSessionModelOption[] = []
Expand All @@ -102,21 +105,21 @@ 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')
}
// Never pass on a confirmation for a model this function substituted rather than read.
const confirmed = input.confirmed?.filter((id) => id !== 'model' || model === input.current.model)
return {
models,
current: { model, ...(input.current.effort ? { effort: input.current.effort } : {}) }
current: {
model,
...(input.current.effort ? { effort: input.current.effort } : {}),
...(confirmed?.length ? { confirmed } : {})
}
}
}

Expand All @@ -133,11 +136,21 @@ export function readLiveCodexSessionOptions(
session: CodexSession,
timeoutMs: number | undefined
): Promise<AgentSessionOptionsResult> {
const model = session.options.get('model') ?? session.reportedOptions.model
const effort = session.options.get('effort') ?? session.reportedOptions.effort
const pendingModel = session.options.get('model')
const pendingEffort = session.options.get('effort')
const model = pendingModel ?? session.reportedOptions.model
const effort = pendingEffort ?? session.reportedOptions.effort
// Why: `session.options` holds a restored pick or a write of ours the thread has not
// echoed — neither is the agent speaking. A value that fell through to the opened
// thread's own state is, which is what lets an unlisted model still be named.
const confirmed = [
...(!pendingModel && model ? ['model'] : []),
...(!pendingEffort && effort ? ['effort'] : [])
]
return readCodexStructuredSessionOptions({
connection: session.connection,
current: { ...(model ? { model } : {}), ...(effort ? { effort } : {}) },
...(confirmed.length > 0 ? { confirmed } : {}),
timeoutMs
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,20 +593,47 @@ 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('still lets the terminal adjust a model it cannot name', async () => {
// New on this path: `main` fabricated the row with `options: []`, so an unlisted model
// was inert here while the structured transport could still adjust it. Both now draw
// the launch-safe set, and the model is offered as a choice on neither.
// This has to apply for real: a `settable: true` row the apply path cannot resolve
// renders a control that throws when clicked, which is worse than no control.
seedNativeChatAppliedSessionOptions('pty-1', 'claude', { model: 'future-model' })
const dispatch = vi.fn()
const surface = createNativeChatPtySessionOptions({
agent: 'claude',
scopeKey: 'pty-1',
mode: 'live',
dispatchCommand: dispatch
})!
expect(surface.getSnapshot().map(({ id }) => id)).toEqual(['model', 'effort'])

await surface.setOption('effort', 'high')

expect(dispatch).toHaveBeenCalledWith('/effort high')
expect(surface.getSnapshot().find(({ id }) => id === 'effort')).toMatchObject({
settable: true,
kind: { currentValue: 'high' }
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
findCatalogModel,
findCatalogOption,
resolveCatalogModelOptions,
type AgentSessionOptionCatalog,
type CatalogMidSessionApply,
type CatalogModel,
Expand Down Expand Up @@ -72,8 +72,11 @@ function currentApply(
if (optionId === 'model') {
return { apply: ctx.catalog.modelApply, modelId }
}
const model = modelId ? findCatalogModel({ ...ctx.catalog, models }, modelId) : undefined
const option = findCatalogOption(model, optionId)
// Why: the snapshot draws an unlisted model's rows from the launch-safe set, so
// resolving through the model list alone would reject the very rows it rendered.
const option = resolveCatalogModelOptions(ctx.catalog, models, modelId).find(
(candidate) => candidate.id === optionId
)
return option ? { apply: option.apply, modelId } : null
}

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,9 +50,55 @@ 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')
// `applied` is what seedNativeChatAppliedSessionOptions writes for a launch flag.
record.model = { value: 'claude-opus-5', source: 'applied' }
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('names a reported model the list cannot carry, end to end from the builder', () => {
// The other half of the rule: Claude's own header named this model, so the pill shows
// it even though no catalog carries it and the dropdown cannot offer it.
const record = createNativeChatSessionOptionRecord('claude')
record.model = { value: 'my-custom-model', 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('my-custom-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.
// showing "Model" while a real model is running. Not a hand-fed shape: this is
// exactly what the builder emits for a reported-but-unlisted model — the test
// above drives the same descriptor through `buildNativeChatSessionOptionSnapshot`.
expect(nativeChatModelPillLabel(modelDescriptor('reported', 'grok-build'))).toBe('grok-build')
})
})
Expand Down
Loading