Repository navigation
fix(native-chat): remember structured chat model and effort picks #19147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
mobile/src/session/mobile-native-chat-session-option-persistence.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' }] | ||
| } | ||
| ) | ||
| }) | ||
| }) |
25 changes: 25 additions & 0 deletions
25
mobile/src/session/mobile-native-chat-session-option-persistence.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/runtime/rpc/methods/client-native-chat-settings.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
setStructuredOptioncapturesoptionStatebefore awaitingmutate. Two calls from the same render can therefore overlap. If a model update and an effort update both succeed, the effort response can contain onlyeffort.structuredAgentSessionOptionPicksthen resolvesmodelIdfrom 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. UpdatingactiveOptionRecordRefalone does not prevent this race.