diff --git a/src/main/claude/structured/claude-structured-execution-observations.ts b/src/main/claude/structured/claude-structured-execution-observations.ts index 5ea6bba7408..bd27887fdfd 100644 --- a/src/main/claude/structured/claude-structured-execution-observations.ts +++ b/src/main/claude/structured/claude-structured-execution-observations.ts @@ -28,6 +28,7 @@ export function beginClaudeOptionMutation(session: ClaudeSession, key: string): if (key === 'model') { delete session.appliedOptions delete session.reportedOptions.reportedModel + session.modelMutationDispatch = session.dispatchSequence } else { if (session.reportedModelMutation === previous) { session.reportedModelMutation = sequence @@ -76,7 +77,12 @@ function observeClaudeResponseModel( frame: Record ): boolean { const response = readClaudeResponseModel(frame) - if (!response) { + // Why: a turn running at a model pick keeps answering on the old model; it must not unseat the pick. + if ( + !response || + (session.modelMutationDispatch === session.dispatchSequence && + session.reportedModelMutation !== session.optionMutationSequence) + ) { return false } const model = diff --git a/src/main/claude/structured/claude-structured-response-model.test.ts b/src/main/claude/structured/claude-structured-response-model.test.ts index 8d14e17c4bb..e37af00c246 100644 --- a/src/main/claude/structured/claude-structured-response-model.test.ts +++ b/src/main/claude/structured/claude-structured-response-model.test.ts @@ -1,5 +1,10 @@ import { expect, it } from 'vitest' -import { acquired, claudeFrame, fakeClaude } from './claude-structured-session-test-support' +import { + acquired, + claudeFrame, + fakeClaude, + USER_MESSAGE +} from './claude-structured-session-test-support' import type { ClaudeStructuredSessionEvent } from './claude-structured-session-state' it('publishes the main response model after fallback and restoration, ignoring child, foreign and synthetic frames', async () => { @@ -74,6 +79,12 @@ it('retains the init context suffix when the response reports the same bare mode const adapter = await acquired(claude) const connection = claude.connections[0]! try { + await adapter.dispatch({ + sessionId: 'session-1', + clientMessageId: 'first-response', + body: USER_MESSAGE, + fence: 7 + }) claudeFrame(connection, { type: 'system', subtype: 'init', model: 'claude-opus-5-5[1m]' }) claudeFrame(connection, { type: 'assistant', @@ -84,6 +95,12 @@ it('retains the init context suffix when the response reports the same bare mode (await adapter.readOptions({ sessionId: 'session-1', fence: 7 })).current.reportedModel ).toBe('claude-opus-5-5[1m]') await adapter.setOption({ sessionId: 'session-1', fence: 7, key: 'model', value: 'opus' }) + await adapter.dispatch({ + sessionId: 'session-1', + clientMessageId: 'after-context-pick', + body: USER_MESSAGE, + fence: 7 + }) claudeFrame(connection, { type: 'assistant', uuid: 'after-context-pick', @@ -96,3 +113,42 @@ it('retains the init context suffix when the response reports the same bare mode await adapter.closeAll() } }) + +it('keeps a model picked during a turn over that turn’s later replies until a turn runs it', async () => { + const claude = fakeClaude({ initModel: 'claude-sonnet-5-5' }) + const adapter = await acquired(claude) + const connection = claude.connections[0]! + const read = async () => (await adapter.readOptions({ sessionId: 'session-1', fence: 7 })).current + const reply = (uuid: string, model: string) => + claudeFrame(connection, { + type: 'assistant', + uuid, + message: { role: 'assistant', model, content: [] } + }) + try { + await adapter.dispatch({ + sessionId: 'session-1', + clientMessageId: 'running', + body: USER_MESSAGE, + fence: 7 + }) + claudeFrame(connection, { type: 'system', subtype: 'init', model: 'claude-sonnet-5-5' }) + reply('before-pick', 'claude-sonnet-5-5') + await adapter.setOption({ sessionId: 'session-1', fence: 7, key: 'model', value: 'haiku' }) + reply('same-turn', 'claude-sonnet-5-5') + expect(await read()).toMatchObject({ model: 'haiku' }) + expect((await read()).reportedModel).toBeUndefined() + expect(connection.calls).toContainEqual({ subtype: 'set_model', params: { model: 'haiku' } }) + + await adapter.dispatch({ + sessionId: 'session-1', + clientMessageId: 'next', + body: USER_MESSAGE, + fence: 7 + }) + reply('next-turn', 'claude-haiku-5-5') + expect(await read()).toMatchObject({ model: 'haiku', reportedModel: 'claude-haiku-5-5' }) + } finally { + await adapter.closeAll() + } +}) diff --git a/src/main/claude/structured/claude-structured-session-state.ts b/src/main/claude/structured/claude-structured-session-state.ts index c0fb4d1f620..55959e4900f 100644 --- a/src/main/claude/structured/claude-structured-session-state.ts +++ b/src/main/claude/structured/claude-structured-session-state.ts @@ -135,6 +135,8 @@ export type ClaudeSession = { fastModePerSessionOptIn?: boolean /** Mutation sequence of the last model report; newer writes outrank it. */ reportedModelMutation: number + /** `dispatchSequence` at the last model write; that turn's replies predate the pick. */ + modelMutationDispatch?: number /** Options whose recorded value the provider reported, not merely accepted. */ confirmedOptions: Set /** Inputs carried by launch or accepted controls, independent of provider reports. */