Skip to content
5 changes: 4 additions & 1 deletion mobile/src/session/MobileNativeChatSessionOptionPickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ChevronLeft, X } from 'lucide-react-native'
import { BottomDrawer } from '../components/BottomDrawer'
import { colors, radii, spacing, typography } from '../theme/mobile-theme'
import {
isReadOnlySessionOptionValue,
sessionOptionDispatchUnconfirmed,
type SessionOptionDescriptor,
type SessionOptionValue
Expand Down Expand Up @@ -260,7 +261,9 @@ function OptionRows({
valueColor={isPermission ? permissionColor : undefined}
disabled={disabled}
divided={index < descriptors.length - 1}
onPress={() => onNavigate(descriptor.id)}
onPress={
isReadOnlySessionOptionValue(descriptor) ? undefined : () => onNavigate(descriptor.id)
}
/>
)
})}
Expand Down
17 changes: 16 additions & 1 deletion mobile/src/session/MobileNativeChatSessionOptionRows.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,23 @@ export function SessionOptionSummaryRow({
valueColor?: string
disabled: boolean
divided: boolean
onPress: () => void
/** Absent for a value with no choice to make: the row reads as text, with no page behind it. */
onPress?: () => void
}): React.JSX.Element {
if (!onPress) {
return (
<View
accessible
accessibilityLabel={`${label}, ${value}`}
style={[styles.summaryRow, divided && styles.rowDivided]}
>
<Text style={styles.summaryLabel}>{label}</Text>
<Text style={styles.summaryValue} numberOfLines={1}>
{value}
</Text>
</View>
)
}
return (
<Pressable
accessibilityLabel={`${label}, ${value}`}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async function mount(initial: Props) {
enabled: true,
fence: props.fence,
mutate,
hostSupport: { sendOptions: true, permissionModes: true }
hostSupport: { permissionModes: true }
})
frames.push(modelChoices(options.optionSnapshot))
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function mountOptions(props: ProbeProps) {
hostSupport:
probe.hostAdvertisesPermissionModes === null
? null
: { sendOptions: true, permissionModes: probe.hostAdvertisesPermissionModes ?? true }
: { permissionModes: probe.hostAdvertisesPermissionModes ?? true }
})
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function Probe(props: ProbeProps): null {
enabled: true,
fence: props.fence,
mutate: props.mutate,
hostSupport: { sendOptions: true, permissionModes: true }
hostSupport: { permissionModes: true }
})
)
return null
Expand Down
14 changes: 6 additions & 8 deletions mobile/src/session/use-mobile-structured-agent-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ export function useMobileStructuredAgentOptions(args: {
/** Moves when the host's status says a session option changed; each move re-reads them. */
optionStamp?: string
/** What the host advertised; null until its status probe answers. */
hostSupport: Pick<StructuredAgentSessionHostSupport, 'sendOptions' | 'permissionModes'> | null
hostSupport: Pick<StructuredAgentSessionHostSupport, 'permissionModes'> | null
}) {
const { agent, client, enabled, fence, mutate, optionStamp, sessionId, hostSupport } = args
const takesSendOptions = hostSupport?.sendOptions === true
// Every agent's seed, as on the desktop: a built-in list or the provider-default pill.
const optionCatalog = useMemo(
() => (agent ? structuredAgentSessionSeedCatalog(agent) : null),
Expand Down Expand Up @@ -181,9 +180,9 @@ export function useMobileStructuredAgentOptions(args: {
!imageSupport.statesPermissionMode &&
optionState.permissionMode === null
const optionSnapshot = useMemo(() => {
const shown = structuredAgentSessionShownSnapshot(optionState, takesSendOptions)
const shown = structuredAgentSessionShownSnapshot(optionState)
return legacyHostPermissions ? [...shown, legacyHostPermissionDescriptor()] : shown
}, [legacyHostPermissions, optionState, takesSendOptions])
}, [legacyHostPermissions, optionState])

const setStructuredOption = useCallback(
async (id: string, value: SessionOptionValue): Promise<boolean> => {
Expand All @@ -196,7 +195,7 @@ export function useMobileStructuredAgentOptions(args: {
!optionCatalog ||
encoded === null ||
!canSetStructuredAgentSessionOption(
structuredAgentSessionOptionView(currentState, undefined, {}, takesSendOptions),
structuredAgentSessionOptionView(currentState, undefined, {}),
id,
value
)
Expand Down Expand Up @@ -287,15 +286,14 @@ export function useMobileStructuredAgentOptions(args: {
}
}
},
[agent, client, mutate, optionCatalog, sessionId, takesSendOptions, updateOptionState]
[agent, client, mutate, optionCatalog, sessionId, updateOptionState]
)

const { optionPickerRequest, invokeStructuredOption, optionSurface } =
useMobileStructuredOptionSurface({
optionSnapshot,
setStructuredOption,
optionStateRef,
takesSendOptions
optionStateRef
})

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,31 +198,29 @@ describe('mobile structured send actions', () => {
}
})

it('takes an effort the replacement it shows offers, and sends on that replacement', async () => {
it("keeps a chat's model its CLI stopped listing, and sends on it and its effort", async () => {
agent = 'claude'
hostSupport = structuredAgentSessionHostSupport([AGENT_SESSION_SEND_OPTIONS_RUNTIME_CAPABILITY])
const efforts = [
{ value: 'low', label: 'Low' },
{ value: 'high', label: 'High' }
]
const opus = {
id: 'opus',
label: 'Opus',
isDefault: true,
efforts: [{ value: 'high', label: 'High' }]
}
sendRequest.mockImplementation(async (method) =>
method === 'agentSession.send'
? sendResult('accepted')
: method === 'agentSession.modelCatalog'
? ok({
origin: 'probe',
fetchedAt: 1,
models: [{ id: 'opus[1m]', label: 'Opus', isDefault: true, efforts }]
})
? ok({ origin: 'probe', fetchedAt: 1, models: [opus] })
: method === 'agentSession.options'
? ok({
// A stopped chat lists the host's rows and its own model.
models: [
{ id: 'opus[1m]', label: 'Opus', isDefault: true, efforts },
{ id: 'claude-gone', label: 'claude-gone', isDefault: false, efforts: [] }
opus,
{ id: 'opus[1m]', label: 'opus[1m]', isDefault: false, efforts: [] }
],
listedModelIds: ['opus[1m]'],
current: { model: 'claude-gone', effort: 'high' }
listedModelIds: ['opus'],
current: { model: 'opus[1m]', effort: 'max' }
})
: ok({})
)
Expand All @@ -233,15 +231,14 @@ describe('mobile structured send actions', () => {
hook!.optionSnapshot.find((descriptor) => descriptor.id === 'model')?.kind
).toMatchObject({ currentValue: 'opus[1m]' })
})
await act(async () => {
await hook!.setStructuredOption('effort', 'low')
expect(hook!.optionSnapshot.find((descriptor) => descriptor.id === 'effort')).toMatchObject({
settable: false,
kind: { currentValue: 'max' }
})
const picks = sendRequest.mock.calls.filter(([method]) => method === 'agentSession.setOption')
expect(picks.map(([, params]) => fieldsOf(params).value)).toEqual(['low'])
await act(async () => {
expect(await hook!.sendWithOutcome('hello')).toBe('accepted')
})
expect(fieldsOf(calls()[0]![1]).options).toMatchObject({ model: 'opus[1m]' })
expect(fieldsOf(calls()[0]![1]).options).toEqual({ model: 'opus[1m]', effort: 'max' })
})

it("sends a command the chat's own agent runs as a message, though Codex's host claims it", async () => {
Expand Down
9 changes: 3 additions & 6 deletions mobile/src/session/use-mobile-structured-option-surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export function useMobileStructuredOptionSurface(args: {
setStructuredOption: (id: string, value: SessionOptionValue) => Promise<boolean>
/** The latest option state, which a settled pick's snapshot is read from. */
optionStateRef: RefObject<StructuredAgentSessionOptionState>
takesSendOptions: boolean
}) {
const { optionSnapshot, optionStateRef, setStructuredOption, takesSendOptions } = args
const { optionSnapshot, optionStateRef, setStructuredOption } = args
const [optionPickerRequest, setOptionPickerRequest] = useState<{
id: string
sequence: number
Expand All @@ -35,11 +34,9 @@ export function useMobileStructuredOptionSurface(args: {
const setOption = useCallback(
async (id: string, value: SessionOptionValue) => {
await setStructuredOption(id, value)
return {
snapshot: structuredAgentSessionShownSnapshot(optionStateRef.current, takesSendOptions)
}
return { snapshot: structuredAgentSessionShownSnapshot(optionStateRef.current) }
},
[optionStateRef, setStructuredOption, takesSendOptions]
[optionStateRef, setStructuredOption]
)

const optionSurface = useMemo<SessionOptionsSurface>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,20 @@ export async function readStructuredAgentSessionOptions(
const permissionMode = record
? await structuredAgentSessionLaunchPermission(context.deps, record)
: null
// The saved pick shows, except one the running child could not take (a model its provider says
// does not exist): that shows what the child runs instead.
const refused = live ? (adapter.readOptionRestoreFailures?.(sessionId) ?? []) : []
const saved = record?.options
const shownSaved = (key: 'model' | 'effort'): boolean =>
saved?.[key] !== undefined && !refused.includes(key)
const selected =
live && (saved?.model !== undefined || saved?.effort !== undefined)
live && (shownSaved('model') || shownSaved('effort'))
? await readStructuredAgentSessionOptionsAtRest(context.deps, sessionId, reader)
: options
const current = {
...options.current,
...(saved?.model !== undefined ? { model: selected.current.model } : {}),
...(saved?.effort !== undefined ? { effort: selected.current.effort } : {})
...(shownSaved('model') ? { model: selected.current.model } : {}),
...(shownSaved('effort') ? { effort: selected.current.effort } : {})
}
const confirmed = current.confirmed?.filter((key) =>
key === 'model'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ describe("a Claude chat whose saved model the CLI says doesn't exist", () => {
model: RETIRED,
effort: 'high'
})
// The picker shows what the child runs now, so the next send moves the chat off the model.
await vi.waitFor(async () =>
expect((await host.readOptions(SESSION, { readsWithoutModel: true })).current.model).not.toBe(
RETIRED
)
)
})

it("keeps a model the reply did not come from: a subagent's own error", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,43 @@ describe("agentSession.modelCatalog with what the running agent's start said", (
expect(reply).not.toMatchObject({ result: { unavailable: expect.anything() } })
})
})

describe('agentSession.modelCatalog for a chat on a model its CLI stopped listing', () => {
const opus = { id: 'opus', label: 'Opus', isDefault: true, efforts: [] }
const read = vi.fn(async (): Promise<AgentSessionModelCatalogResult> => ({
origin: 'probe',
fetchedAt: 1,
models: [opus]
}))
const record: AgentSessionRecord = {
...agentSessionRecordFixture(),
provider: 'claude',
options: { model: 'opus[1m]', effort: 'max' }
}

beforeEach(() => {
setStructuredAgentSessionHost(
Object.assign(hostStub(), {
deps: { modelCatalog: { read }, store: { getRecord: () => record }, adapter: {} }
})
)
})

it("lists the chat's own model, so an older client that replaces an unlisted one keeps it", async () => {
const own = { id: 'opus[1m]', label: 'opus[1m]', isDefault: false, efforts: [] }
const forChat = { agent: 'claude', sessionId: record.sessionId }
expect(await call('agentSession.modelCatalog', forChat, STRUCTURED_CLIENT)).toMatchObject({
ok: true,
result: { models: [opus, own] }
})
// A new chat's read, which seeds the next new chat's list, and the account's name only the CLI's.
const newChat = { ...forChat, worktree: 'id:wt-1' }
const runtime = { resolveStructuredAgentSessionLocalWorkspacePath: async () => '/repo' }
for (const reply of [
await call('agentSession.modelCatalog', newChat, STRUCTURED_CLIENT, runtime),
await call('agentSession.modelCatalog', { agent: 'claude' }, STRUCTURED_CLIENT)
]) {
expect(reply).toMatchObject({ ok: true, result: { models: [opus] } })
}
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { agentSessionPinnedLaunchDirectory } from '../../agent-session-record-la
import { agentSessionRefusalError } from '../../../../shared/agent-session-wire-refusals'
import { clientReadsOptionsWithoutModel } from './structured-agent-session-policy'
import type { AgentSessionModelCatalogResult } from '../../../../shared/agent-session-wire'
import { structuredAgentSessionOptionModels } from '../../../native-chat/agent-session-wire/structured-agent-session-option-models'
import { finishedListingIds } from '../../../../shared/structured-agent-session-options'

export const STRUCTURED_AGENT_SESSION_OPTIONS_READ_METHODS = [
defineMethod({
Expand Down Expand Up @@ -88,12 +90,23 @@ export const STRUCTURED_AGENT_SESSION_OPTIONS_READ_METHODS = [
.catch(() => null)
return catalog.read({ ...params, workspacePath })
}
const answer = await read()
const listing = await read()
const own = record?.provider === params.agent ? record : null
// A reopened chat's own model stays a row, so a client that replaces an unlisted one keeps
// it. Not a new chat's read (it names a worktree): that answer seeds the next new chat's list.
const answer =
own && worktree === undefined && finishedListingIds(listing) && listing.origin !== 'unknown'
? {
...listing,
models: structuredAgentSessionOptionModels(
listing.models,
own.options?.model,
(row) => row
)
}
: listing
// The chat's running agent can say what no listing does: a Pi that started with no model.
const started =
record?.provider === params.agent
? host.deps.adapter.startUnavailable?.(record.sessionId)
: undefined
const started = own ? host.deps.adapter.startUnavailable?.(own.sessionId) : undefined
return started && !answer.unavailable ? { ...answer, unavailable: started } : answer
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Switch } from '@/components/ui/switch'
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'
import { sortNativeChatSessionOptions } from '../../../../shared/native-chat-session-option-snapshot'
import type {
SessionOptionDescriptor,
SessionOptionSelectChoice,
SessionOptionValue
import {
isReadOnlySessionOptionValue,
type SessionOptionDescriptor,
type SessionOptionSelectChoice,
type SessionOptionValue
} from '../../../../shared/native-chat-session-options'
import {
nativeChatSessionChoiceLabel,
Expand Down Expand Up @@ -125,6 +126,15 @@ function OptionRow(props: {
)
}
const current = descriptor.kind.currentValue
const only = descriptor.kind.choices[0]
if (only && isReadOnlySessionOptionValue(descriptor)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return (
<div className="flex min-h-7 items-center justify-between gap-3 px-2 text-xs">
<span className="text-muted-foreground">{label}</span>
<span className="text-foreground">{nativeChatSessionChoiceLabel(only, descriptor.id)}</span>
</div>
)
}
return (
<div className="flex min-h-7 flex-wrap items-center justify-between gap-x-3 gap-y-1 pl-2">
<span id={labelId} className="text-xs text-muted-foreground">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,10 @@ export function useStructuredAgentSessionOptionState(args: {
target,
AGENT_SESSION_SEND_OPTIONS_RUNTIME_CAPABILITY
)
// What the picker shows and a send carries, for this session's host.
const optionView = useCallback(
(
state: StructuredAgentSessionOptionState,
seed: Readonly<Record<string, string>> | undefined,
held: Readonly<Record<string, string>>
) => structuredAgentSessionOptionView(state, seed, held, takesSendOptions),
[takesSendOptions]
)

return {
optionView,
// What the picker shows and a send carries.
optionView: structuredAgentSessionOptionView,
takesSendOptions,
conversationSupport,
rereadOptions,
Expand Down
Loading
Loading