Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { describe, expect, it, vi } from 'vitest'
import {
applyNativeChatSessionOptionSettingsMutation,
resolveStructuredLaunchSeedOptions
} from '../../../src/shared/native-chat-session-option-defaults'
import type { PersistedNativeChatSessionOptions } from '../../../src/shared/native-chat-session-options'
import type { RpcClient } from '../transport/rpc-client'
import { persistMobileStructuredOptionPicks } from './mobile-native-chat-session-option-persistence'

function hostClient(initial?: PersistedNativeChatSessionOptions) {
let stored = initial
const sendRequest = vi.fn(async (method: string, params?: unknown) => {
expect(method).toBe('settings.mutateNativeChatSessionOptions')
const next = applyNativeChatSessionOptionSettingsMutation(
stored,
params as Parameters<typeof applyNativeChatSessionOptionSettingsMutation>[1]
)
stored = next ?? stored
return { id: '2', ok: true as const, result: null, _meta: { runtimeId: 'host' } }
})
return { client: { sendRequest } as unknown as RpcClient, sendRequest, read: () => stored }
}

describe('persistMobileStructuredOptionPicks', () => {
it('writes the pick to the host record a later launch seeds from', async () => {
const host = hostClient()
await persistMobileStructuredOptionPicks({
client: host.client,
agent: 'codex',
picks: [{ modelId: 'gpt-fast', optionId: 'effort', value: 'low' }]
})
expect(resolveStructuredLaunchSeedOptions(host.read(), 'codex')).toEqual({
model: 'gpt-fast',
effort: 'low'
})
})

it('merges onto the host record instead of replacing another agent', async () => {
const host = hostClient({
claude: { model: 'opus', valuesByModel: { opus: { effort: 'high' } } }
})
await persistMobileStructuredOptionPicks({
client: host.client,
agent: 'codex',
picks: [{ modelId: 'gpt-fast', optionId: 'model', value: 'gpt-fast' }]
})
expect(resolveStructuredLaunchSeedOptions(host.read(), 'claude')).toEqual({
model: 'opus',
effort: 'high'
})
})

it('sends concurrent deltas that preserve both picks on the host', async () => {
const host = hostClient()
const first = persistMobileStructuredOptionPicks({
client: host.client,
agent: 'codex',
picks: [{ modelId: 'gpt-fast', optionId: 'effort', value: 'low' }]
})
const second = persistMobileStructuredOptionPicks({
client: host.client,
agent: 'claude',
picks: [{ modelId: 'opus', optionId: 'effort', value: 'high' }]
})
await Promise.all([first, second])
expect(resolveStructuredLaunchSeedOptions(host.read(), 'codex')).toEqual({
model: 'gpt-fast',
effort: 'low'
})
expect(resolveStructuredLaunchSeedOptions(host.read(), 'claude')).toEqual({
model: 'opus',
effort: 'high'
})
})

it('stays silent without a host or without picks', async () => {
const host = hostClient()
await persistMobileStructuredOptionPicks({ client: null, agent: 'codex', picks: [] })
await persistMobileStructuredOptionPicks({ client: host.client, agent: 'codex', picks: [] })
expect(host.sendRequest).not.toHaveBeenCalled()
})

it('uses one targeted host mutation instead of a settings read-modify-write', async () => {
const host = hostClient()
await persistMobileStructuredOptionPicks({
client: host.client,
agent: 'codex',
picks: [{ modelId: 'gpt-fast', optionId: 'model', value: 'gpt-fast' }]
})
expect(host.sendRequest).toHaveBeenCalledExactlyOnceWith(
'settings.mutateNativeChatSessionOptions',
{
type: 'apply-picks',
agent: 'codex',
picks: [{ modelId: 'gpt-fast', optionId: 'model', value: 'gpt-fast' }]
}
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { AgentType } from '../../../src/shared/agent-status-types'
import type { StructuredSessionOptionPick } from '../../../src/shared/structured-agent-session-options'
import type { RpcClient } from '../transport/rpc-client'

/** The host owns the record a later launch seeds from, so a phone-side pick writes there
* rather than to any client-local store. Best-effort: a failed write only costs the
* next session its remembered start. */
export function persistMobileStructuredOptionPicks(args: {
client: RpcClient | null
agent: AgentType
picks: readonly StructuredSessionOptionPick[]
}): Promise<void> {
const { agent, client, picks } = args
if (!client || picks.length === 0) {
return Promise.resolve()
}
return client
.sendRequest('settings.mutateNativeChatSessionOptions', {
type: 'apply-picks',
agent,
picks
})
.then(() => undefined)
.catch(() => undefined)
}
20 changes: 15 additions & 5 deletions mobile/src/session/use-mobile-structured-agent-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import {
commitStructuredAgentSessionOption,
commitStructuredAgentSessionOptionValues,
createStructuredAgentSessionOptionState,
structuredAgentSessionOptionPicks,
structuredAgentSessionOptionSnapshot
} from '../../../src/shared/structured-agent-session-options'
import type { RpcClient } from '../transport/rpc-client'
import {
callAgentSession,
type StructuredAgentSessionMutate
} from './mobile-structured-agent-session-rpc'
import { persistMobileStructuredOptionPicks } from './mobile-native-chat-session-option-persistence'

type StructuredOptionsController = {
optionSnapshot: SessionOptionDescriptor[]
Expand Down Expand Up @@ -101,14 +103,22 @@ export function useMobileStructuredAgentOptions(args: {
return result.status !== 'rejected'
}
if (result.status === 'accepted') {
const committed = result.value.options ?? { [id]: value }
setOptionState((current) =>
current.record === targetRecord && result.sameFence
? commitStructuredAgentSessionOptionValues(
current,
result.value.options ?? { [id]: value }
)
? commitStructuredAgentSessionOptionValues(current, committed)
: current
)
// Only an accepted pick: an `unknown` outcome commits optimistically to the
// visible record, and remembering one the provider refused would seed a
// launch the user never chose.
if (agent === 'claude' || agent === 'codex') {
void persistMobileStructuredOptionPicks({
client,
agent,
picks: structuredAgentSessionOptionPicks(optionState, committed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Serialize same-session option mutations before persisting picks.

setStructuredOption captures optionState before awaiting mutate. Two calls from the same render can therefore overlap. If a model update and an effort update both succeed, the effort response can contain only effort. structuredAgentSessionOptionPicks then resolves modelId from the pre-mutation record and can persist the effort under the previous model. The next launch can select the new model without that effort. Serialize mutations per session and derive each persistence payload from the ordered current record after the accepted mutation commits. Updating activeOptionRecordRef alone does not prevent this race.

})
}
return true
}
if (result.status === 'unknown') {
Expand All @@ -128,7 +138,7 @@ export function useMobileStructuredAgentOptions(args: {
)
}
},
[mutate, optionState]
[agent, client, mutate, optionState]
)

const invokeStructuredOption = useCallback(async () => false, [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function runningStatusItem(): AgentJournalRenderItem {
}
}

function defaultSendRequest(method: string, params?: Record<string, unknown>) {
async function defaultSendRequest(method: string, params?: Record<string, unknown>) {
if (method === 'agentSession.send') {
return ok({
ok: true,
Expand Down Expand Up @@ -420,6 +420,11 @@ describe('useMobileStructuredAgentSession', () => {
}),
expect.any(Object)
)
expect(sendRequest).toHaveBeenCalledWith('settings.mutateNativeChatSessionOptions', {
type: 'apply-picks',
agent: 'codex',
picks: [{ modelId: 'gpt-fast', optionId: 'model', value: 'gpt-fast' }]
})

await act(async () => {
expect(await hook.respondPermission(hook.permission!.options[0]!.send)).toBe(true)
Expand Down
5 changes: 5 additions & 0 deletions src/main/runtime/orca-runtime-pty-foreground-process-reads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { FeatureInteractionId } from '../../shared/feature-interactions'
import type { RuntimeClientSettingsUpdate } from './runtime-client-settings'
import type { TerminalQuickCommand } from '../../shared/terminal-quick-command-types'
import type { TerminalQuickCommandMutation } from '../../shared/terminal-quick-commands'
import type { NativeChatSessionOptionSettingsMutation } from '../../shared/native-chat-session-options'
import type { Automation } from '../../shared/automations-types'

export class OrcaRuntimeWithPtyForegroundProcessReads extends OrcaRuntimeWithStateFields {
Expand Down Expand Up @@ -214,6 +215,10 @@ export class OrcaRuntimeWithPtyForegroundProcessReads extends OrcaRuntimeWithSta
return this.clientSettings.updatePRBotAuthorOverride(args)
}

updateClientNativeChatSessionOptions(mutation: NativeChatSessionOptionSettingsMutation): void {
this.clientSettings.updateNativeChatSessionOptions(mutation)
}

listAutomations(): Automation[] {
return this.automation.list()
}
Expand Down
72 changes: 72 additions & 0 deletions src/main/runtime/orca-runtime-tests/paired-settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,78 @@ describe('OrcaRuntimeService', () => {
expect(runtime.getClientSettings()).not.toHaveProperty('terminalQuickCommands')
})

it('applies native-chat option deltas atomically on the runtime host', () => {
let settings = {
...store.getSettings(),
nativeChatSessionOptions: {
claude: { model: 'opus', valuesByModel: { opus: { effort: 'high' } } }
}
}
const updateSettings = vi.fn((updates: Partial<typeof settings>) => {
settings = { ...settings, ...updates }
})
const runtime = new OrcaRuntimeService({
...store,
getSettings: () => settings,
updateSettings
} as never)

runtime.updateClientNativeChatSessionOptions({
type: 'apply-picks',
agent: 'codex',
picks: [
{ modelId: 'gpt-fast', optionId: 'model', value: 'gpt-fast' },
{ modelId: 'gpt-fast', optionId: 'effort', value: 'low' }
]
})
runtime.updateClientNativeChatSessionOptions({
type: 'apply-picks',
agent: 'claude',
picks: [{ modelId: 'sonnet', optionId: 'model', value: 'sonnet' }]
})

expect(settings.nativeChatSessionOptions).toEqual({
claude: {
model: 'sonnet',
valuesByModel: { opus: { effort: 'high' } }
},
codex: {
model: 'gpt-fast',
valuesByModel: { 'gpt-fast': { effort: 'low' } }
}
})
expect(updateSettings).toHaveBeenCalledTimes(2)
})

it('compares retired models against the host record at mutation time', () => {
let settings = {
...store.getSettings(),
nativeChatSessionOptions: { grok: { model: 'grok-5' } }
}
const updateSettings = vi.fn((updates: Partial<typeof settings>) => {
settings = { ...settings, ...updates }
})
const runtime = new OrcaRuntimeService({
...store,
getSettings: () => settings,
updateSettings
} as never)

runtime.updateClientNativeChatSessionOptions({
type: 'clear-model-if-missing',
agent: 'grok',
availableModelIds: ['grok-5']
})
expect(updateSettings).not.toHaveBeenCalled()

runtime.updateClientNativeChatSessionOptions({
type: 'clear-model-if-missing',
agent: 'grok',
availableModelIds: ['grok-4.5']
})
expect(settings.nativeChatSessionOptions).toEqual({ grok: {} })
})

it('rejects a concurrent add after the quick command limit is reached', () => {
const terminalQuickCommands = Array.from({ length: MAX_QUICK_COMMANDS }, (_, index) => ({
id: `command-${index}`,
Expand Down
78 changes: 78 additions & 0 deletions src/main/runtime/rpc/methods/client-native-chat-settings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { describe, expect, it, vi } from 'vitest'
import type { OrcaRuntimeService } from '../../orca-runtime'
import type { RpcRequest } from '../core'
import { RpcDispatcher } from '../dispatcher'
import { CLIENT_UI_METHODS } from './client-ui'

const request = (params: unknown): RpcRequest => ({
id: 'req-1',
authToken: 'tok',
method: 'settings.mutateNativeChatSessionOptions',
params
})

describe('native-chat settings RPC', () => {
it('routes option deltas to the runtime-owned atomic update', async () => {
const updateClientNativeChatSessionOptions = vi.fn()
const runtime = {
getRuntimeId: () => 'test-runtime',
updateClientNativeChatSessionOptions
} as unknown as OrcaRuntimeService
const dispatcher = new RpcDispatcher({ runtime, methods: CLIENT_UI_METHODS })
const mutation = {
type: 'apply-picks' as const,
agent: 'codex' as const,
picks: [
{ modelId: 'gpt-fast', optionId: 'model' as const, value: 'gpt-fast' },
{ modelId: 'gpt-fast', optionId: 'effort' as const, value: 'low' }
]
}

const response = await dispatcher.dispatch(request(mutation))

expect(updateClientNativeChatSessionOptions).toHaveBeenCalledExactlyOnceWith(mutation)
expect(response).toMatchObject({ ok: true, result: { ok: true } })
})

it('rejects malformed option deltas', async () => {
const updateClientNativeChatSessionOptions = vi.fn()
const runtime = {
getRuntimeId: () => 'test-runtime',
updateClientNativeChatSessionOptions
} as unknown as OrcaRuntimeService
const dispatcher = new RpcDispatcher({ runtime, methods: CLIENT_UI_METHODS })

for (const mutation of [
{ type: 'apply-picks', agent: 'codex', picks: [] },
{
type: 'apply-picks',
agent: 'opencode',
picks: [{ modelId: 'model', optionId: 'model', value: 'model' }]
},
{
type: 'apply-picks',
agent: 'codex',
picks: [{ modelId: 'model', optionId: 'arbitrary', value: 'value' }]
},
{
type: 'apply-picks',
agent: 'codex',
picks: [{ modelId: 'model', optionId: 'effort', value: true }]
},
{
type: 'apply-picks',
agent: 'cursor',
picks: [{ modelId: 'model', optionId: 'fastMode', value: 'true' }]
},
{
type: 'clear-model-if-missing',
agent: 'grok',
availableModelIds: []
}
]) {
const response = await dispatcher.dispatch(request(mutation))
expect(response).toMatchObject({ ok: false, error: { code: 'invalid_argument' } })
}
expect(updateClientNativeChatSessionOptions).not.toHaveBeenCalled()
})
})
Loading
Loading