fix(ui): keep manual model override after delegated subtask completes

Synthetic subagent-completion nudges were treated as the latest user model
choice and rehydrated the agent default, while setAgent preferred the agent
pin over the session override. Skip synthetic prompts for restore, preserve
manual selection-store overrides, and prefer session agent models in setAgent.

Closes openchamber/openchamber#2404

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-08-04 12:46:48 +00:00
co-authored by Serhii Dziupin
parent f47110c66f
commit 65a1eec782
7 changed files with 579 additions and 68 deletions
@@ -0,0 +1,145 @@
import { describe, expect, test } from 'bun:test'
import type { Message, Part } from '@opencode-ai/sdk/v2'
import {
extractUserModelChoice,
findLatestUserModelChoice,
shouldPreserveManualModelOverride,
} from './userModelChoice'
const userMessage = (
id: string,
model: { providerID: string; modelID: string },
agent = 'custom-agent',
): Message => ({
id,
sessionID: 'ses_1',
role: 'user',
time: { created: 1 },
agent,
model,
} as Message)
const assistantMessage = (id: string): Message => ({
id,
sessionID: 'ses_1',
role: 'assistant',
time: { created: 2 },
parentID: 'u1',
modelID: 'model-a',
providerID: 'provider',
} as Message)
const textPart = (id: string, text: string, synthetic = false): Part => ({
id,
sessionID: 'ses_1',
messageID: 'u1',
type: 'text',
text,
...(synthetic ? { synthetic: true } : {}),
} as Part)
describe('findLatestUserModelChoice', () => {
test('returns the latest real user prompt model', () => {
const messages = [
userMessage('u1', { providerID: 'provider', modelID: 'model-a' }),
assistantMessage('a1'),
userMessage('u2', { providerID: 'provider', modelID: 'model-b' }),
]
const partsById: Record<string, Part[]> = {
u1: [textPart('p1', 'first')],
u2: [textPart('p2', 'second')],
}
const choice = findLatestUserModelChoice(messages, (id) => partsById[id])
expect(choice?.id).toBe('u2')
expect(choice?.modelID).toBe('model-b')
expect(choice?.providerID).toBe('provider')
expect(choice?.agent).toBe('custom-agent')
})
test('[issue-2404] skips synthetic subagent-completion nudges so manual override is not clobbered', () => {
// Real prompt sent with the manual override (model-b).
const realPrompt = userMessage('u-real', { providerID: 'provider', modelID: 'model-b' })
// After a delegated child session goes idle, OpenCode injects a synthetic
// user nudge that often carries the agent default model (model-a).
const syntheticNudge = userMessage('u-nudge', { providerID: 'provider', modelID: 'model-a' })
const messages = [realPrompt, assistantMessage('a1'), syntheticNudge]
const partsById: Record<string, Part[]> = {
'u-real': [textPart('p-real', 'please investigate', false)],
'u-nudge': [textPart('p-nudge', 'Subagent finished.', true)],
}
const choice = findLatestUserModelChoice(messages, (id) => partsById[id])
expect(choice?.id).toBe('u-real')
expect(choice?.modelID).toBe('model-b')
})
test('skips user messages whose parts have not loaded yet', () => {
const messages = [
userMessage('u1', { providerID: 'provider', modelID: 'model-a' }),
userMessage('u2', { providerID: 'provider', modelID: 'model-b' }),
]
const partsById: Record<string, Part[]> = {
u1: [textPart('p1', 'first')],
// u2 parts missing
}
const choice = findLatestUserModelChoice(messages, (id) => partsById[id])
expect(choice?.id).toBe('u1')
expect(choice?.modelID).toBe('model-a')
})
test('returns null when only synthetic user messages exist', () => {
const messages = [userMessage('u-nudge', { providerID: 'provider', modelID: 'model-a' })]
const partsById: Record<string, Part[]> = {
'u-nudge': [textPart('p-nudge', 'Subagent finished.', true)],
}
expect(findLatestUserModelChoice(messages, (id) => partsById[id])).toBeNull()
})
})
describe('shouldPreserveManualModelOverride', () => {
test('preserves manual override when it differs from the candidate message model', () => {
expect(shouldPreserveManualModelOverride({
selectionSource: 'manual',
savedSessionModel: { providerId: 'provider', modelId: 'model-b' },
candidate: { providerID: 'provider', modelID: 'model-a' },
})).toBe(true)
})
test('does not preserve when selection matches the candidate', () => {
expect(shouldPreserveManualModelOverride({
selectionSource: 'manual',
savedSessionModel: { providerId: 'provider', modelId: 'model-b' },
candidate: { providerID: 'provider', modelID: 'model-b' },
})).toBe(false)
})
test('does not preserve auto selections', () => {
expect(shouldPreserveManualModelOverride({
selectionSource: 'auto',
savedSessionModel: { providerId: 'provider', modelId: 'model-b' },
candidate: { providerID: 'provider', modelID: 'model-a' },
})).toBe(false)
})
test('preserves manual override when candidate has no model', () => {
expect(shouldPreserveManualModelOverride({
selectionSource: 'manual',
savedSessionModel: { providerId: 'provider', modelId: 'model-b' },
candidate: { providerID: undefined, modelID: undefined },
})).toBe(true)
})
})
describe('extractUserModelChoice', () => {
test('reads variant from model.variant', () => {
const message = {
...userMessage('u1', { providerID: 'provider', modelID: 'model-b' }),
model: { providerID: 'provider', modelID: 'model-b', variant: 'high' },
} as Message
expect(extractUserModelChoice(message as never)?.variant).toBe('high')
})
})
@@ -0,0 +1,103 @@
import type { Message, Part } from '@opencode-ai/sdk/v2'
import { isFullySyntheticMessage } from './synthetic'
type UserModelChoice = {
id: string
agent?: string
providerID?: string
modelID?: string
variant?: string
}
type MessageLike = Message & {
model?: { providerID?: string; modelID?: string; variant?: string }
variant?: string
mode?: string
}
/**
* Extract agent/model selection metadata from a user message, if present.
*/
export const extractUserModelChoice = (message: MessageLike): UserModelChoice | null => {
if (message.role !== 'user') {
return null
}
const providerID = typeof message.model?.providerID === 'string' && message.model.providerID.trim().length > 0
? message.model.providerID
: undefined
const modelID = typeof message.model?.modelID === 'string' && message.model.modelID.trim().length > 0
? message.model.modelID
: undefined
const agent = typeof message.agent === 'string' && message.agent.trim().length > 0
? message.agent
: (typeof message.mode === 'string' && message.mode.trim().length > 0 ? message.mode : undefined)
// OpenCode 1.4.0 moved variant from top-level to model.variant.
const variantCandidate = message.model?.variant ?? message.variant
const variant = typeof variantCandidate === 'string' && variantCandidate.trim().length > 0
? variantCandidate
: undefined
return { id: message.id, agent, providerID, modelID, variant }
}
/**
* Find the latest *real* user prompt's model/agent choice.
*
* Synthetic user messages (e.g. subagent-completion nudges injected when a
* delegated child session goes idle) must not drive the composer model
* selector — restoring from them clobber a manual session override and reset
* to the agent default.
*
* Messages whose parts have not been loaded yet are skipped so an incomplete
* snapshot cannot be treated as authoritative.
*/
export const findLatestUserModelChoice = (
messages: readonly MessageLike[],
getParts: (messageId: string) => Part[] | undefined,
): UserModelChoice | null => {
for (let i = messages.length - 1; i >= 0; i -= 1) {
const message = messages[i]
if (message.role !== 'user') {
continue
}
const parts = getParts(message.id)
if (!Array.isArray(parts) || parts.length === 0) {
continue
}
if (isFullySyntheticMessage(parts)) {
continue
}
return extractUserModelChoice(message)
}
return null
}
/**
* When the user has a manual session model override, historical (or synthetic)
* user-message metadata must not overwrite it. After a real send the selection
* store is updated to match the message, so a conflict means the picker was
* changed after the last prompt — keep the override.
*/
export const shouldPreserveManualModelOverride = ({
selectionSource,
savedSessionModel,
candidate,
}: {
selectionSource: 'auto' | 'manual' | undefined
savedSessionModel: { providerId: string; modelId: string } | null | undefined
candidate: Pick<UserModelChoice, 'providerID' | 'modelID'> | null | undefined
}): boolean => {
if (selectionSource !== 'manual' || !savedSessionModel?.providerId || !savedSessionModel.modelId) {
return false
}
if (!candidate?.providerID || !candidate.modelID) {
return true
}
return savedSessionModel.providerId !== candidate.providerID
|| savedSessionModel.modelId !== candidate.modelID
}