Skip to content
Closed
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
22 changes: 17 additions & 5 deletions apps/desktop/src/app/session/hooks/use-model-controls.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { cleanup, render, renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { getGlobalModelInfo } from '@/hermes'
import { $activeSessionId, $currentModel, $currentProvider, setCurrentModel, setCurrentProvider } from '@/store/session'
import {
$activeSessionId,
$currentModel,
$currentProvider,
$draftModelOverridePending,
setCurrentModel,
setCurrentProvider,
setDraftModelOverridePending
} from '@/store/session'

import { useModelControls } from './use-model-controls'

Expand Down Expand Up @@ -56,6 +64,7 @@ describe('useModelControls', () => {
$activeSessionId.set(null)
setCurrentModel('')
setCurrentProvider('')
setDraftModelOverridePending(false)
})

afterEach(() => {
Expand All @@ -64,6 +73,7 @@ describe('useModelControls', () => {
$activeSessionId.set(null)
setCurrentModel('')
setCurrentProvider('')
setDraftModelOverridePending(false)
})

it('applies the global model when there is no active runtime session', async () => {
Expand Down Expand Up @@ -149,6 +159,7 @@ describe('useModelControls', () => {
// the gateway or the profile default here.
expect($currentModel.get()).toBe('claude-sonnet-4.6')
expect($currentProvider.get()).toBe('anthropic')
expect($draftModelOverridePending.get()).toBe(true)
expect(requestGateway).not.toHaveBeenCalled()
expect(setGlobalModel).not.toHaveBeenCalled()
})
Expand All @@ -168,15 +179,16 @@ describe('useModelControls', () => {
await result.current.refreshCurrentModel()
expect($currentModel.get()).toBe('openai/gpt-5.5')

// A user pick must survive the lifecycle refreshes that fire on boot / fresh
// draft / session events.
setCurrentModel('anthropic/claude-sonnet-4.6')
setCurrentProvider('anthropic')
// A deliberate draft pick must survive the lifecycle refreshes that fire on
// boot / fresh draft / session events.
await result.current.selectModel({ model: 'anthropic/claude-sonnet-4.6', provider: 'anthropic' })
await result.current.refreshCurrentModel()
expect($currentModel.get()).toBe('anthropic/claude-sonnet-4.6')

// A profile swap forces a reseed to the new profile's default.
setDraftModelOverridePending(true)
await result.current.refreshCurrentModel(true)
expect($currentModel.get()).toBe('openai/gpt-5.5')
expect($draftModelOverridePending.get()).toBe(false)
})
})
18 changes: 15 additions & 3 deletions apps/desktop/src/app/session/hooks/use-model-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import { useCallback } from 'react'
import { getGlobalModelInfo } from '@/hermes'
import { useI18n } from '@/i18n'
import { notifyError } from '@/store/notifications'
import { $activeSessionId, $currentModel, $currentProvider, setCurrentModel, setCurrentProvider } from '@/store/session'
import {
$activeSessionId,
$currentModel,
$currentProvider,
$draftModelOverridePending,
setCurrentModel,
setCurrentProvider,
setDraftModelOverridePending
} from '@/store/session'
import type { ModelOptionsResponse } from '@/types/hermes'

interface ModelSelection {
Expand Down Expand Up @@ -46,13 +54,13 @@ export function useModelControls({ activeSessionId, queryClient, requestGateway
return
}

if (!force && $currentModel.get()) {
if (!force && $currentModel.get() && $draftModelOverridePending.get()) {
return
}

const result = await getGlobalModelInfo()

if ($activeSessionId.get() || (!force && $currentModel.get())) {
if ($activeSessionId.get() || (!force && $currentModel.get() && $draftModelOverridePending.get())) {
return
}

Expand All @@ -63,6 +71,8 @@ export function useModelControls({ activeSessionId, queryClient, requestGateway
if (typeof result.provider === 'string') {
setCurrentProvider(result.provider)
}

setDraftModelOverridePending(false)
} catch {
// The delayed session.info event still updates this once the agent is ready.
}
Expand All @@ -89,6 +99,8 @@ export function useModelControls({ activeSessionId, queryClient, requestGateway
// No live session yet: the pick is pure UI state. session.create reads
// $currentModel/$currentProvider and applies it as that session's override.
if (!activeSessionId) {
setDraftModelOverridePending(true)

return true
}

Expand Down
29 changes: 29 additions & 0 deletions apps/desktop/src/app/session/hooks/use-session-actions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { $activeGatewayProfile, $newChatProfile } from '@/store/profile'
import {
$activeSessionId,
$currentCwd,
$draftModelOverridePending,
$messages,
$resumeFailedSessionId,
setActiveSessionId,
setCurrentModel,
setCurrentProvider,
setDraftModelOverridePending,
setMessages,
setResumeFailedSessionId,
setSessions
Expand Down Expand Up @@ -114,6 +118,9 @@ describe('createBackendSessionForSend profile routing', () => {
$newChatProfile.set(null)
$activeGatewayProfile.set('default')
$currentCwd.set('')
setCurrentModel('')
setCurrentProvider('')
setDraftModelOverridePending(false)
vi.restoreAllMocks()
})

Expand Down Expand Up @@ -155,6 +162,28 @@ describe('createBackendSessionForSend profile routing', () => {

expect(params).toMatchObject({ cwd: '/remote/worktree' })
})

it('does not carry the previous session model into a plain new chat', async () => {
const params = await createWith(() => {
setCurrentModel('stepfun/step-3.7-flash:free')
setCurrentProvider('nous')
setDraftModelOverridePending(false)
})

expect(params).not.toHaveProperty('model')
expect(params).not.toHaveProperty('provider')
})

it('ships an explicit no-session picker choice as a one-shot draft model override', async () => {
const params = await createWith(() => {
setCurrentModel('claude-sonnet-4.6')
setCurrentProvider('anthropic')
setDraftModelOverridePending(true)
})

expect(params).toMatchObject({ model: 'claude-sonnet-4.6', provider: 'anthropic' })
expect($draftModelOverridePending.get()).toBe(false)
})
})

// ── Resume failure recovery (the "stuck loading session window" bug) ──────────
Expand Down
19 changes: 6 additions & 13 deletions apps/desktop/src/app/session/hooks/use-session-actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
$currentModel,
$currentProvider,
$currentReasoningEffort,
$draftModelOverridePending,
$messages,
$sessions,
$yoloActive,
Expand All @@ -33,6 +34,7 @@ import {
setCurrentCwd,
setCurrentServiceTier,
setCurrentUsage,
setDraftModelOverridePending,
setFreshDraftReady,
setIntroSeed,
setMessages,
Expand Down Expand Up @@ -134,12 +136,6 @@ export function useSessionActions({
})
setSessionStartedAt(null)
setTurnStartedAt(null)
// The composer's model/effort/fast is sticky UI state (persisted in
// localStorage) β€” a new chat FOLLOWS your last pick instead of snapping
// back to the profile default, so we deliberately don't reset it here. The
// profile default still owns first-run seeding and profile switches (see
// refreshCurrentModel). Only $currentServiceTier (a live-session mirror)
// is cleared.
setCurrentServiceTier('')
setYoloActive(false)
// In a project β†’ the repo's default-branch (main worktree) checkout; not in
Expand Down Expand Up @@ -173,15 +169,12 @@ export function useSessionActions({
const newChatProfile = $newChatProfile.get() ?? normalizeProfileKey($activeGatewayProfile.get())
await ensureGatewayProfile(newChatProfile)
const cwd = $currentCwd.get().trim() || workspaceCwdForNewSession()
// The composer's model/effort/fast is sticky UI state ($currentModel,
// $currentProvider, $currentReasoningEffort, $currentFastMode). Ship it
// with every session.create so the new chat opens on whatever the picker
// shows β€” applied as per-session overrides, never written to the profile
// default (that lives in Settings β†’ Model).
const uiModel = $currentModel.get().trim()
const uiProvider = $currentProvider.get().trim()
const useDraftModelOverride = $draftModelOverridePending.get()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This gate reverses the current sticky-composer contract: current main deliberately includes the displayed picker model on every new session.create so selections follow across Cmd/Ctrl+N. That behavior was introduced in cb6b4127e795 and is documented at website/docs/user-guide/desktop.md:61; please obtain a maintainer decision before changing it.

const uiModel = useDraftModelOverride ? $currentModel.get().trim() : ''
const uiProvider = useDraftModelOverride ? $currentProvider.get().trim() : ''
const uiEffort = $currentReasoningEffort.get().trim()
const uiFast = $currentFastMode.get()
setDraftModelOverridePending(false)

const created = await requestGateway<SessionCreateResponse>('session.create', {
cols: 96,
Expand Down
7 changes: 2 additions & 5 deletions apps/desktop/src/store/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ type Updater<T> = T | ((current: T) => T)

const WORKSPACE_CWD_KEY = 'hermes.desktop.workspace-cwd'

// The composer's model/effort/fast is sticky UI state, NOT the profile default
// (that lives in Settings β†’ Model). Persisting it in localStorage makes a pick
// follow across Cmd+N and app restarts instead of snapping back to the default.
// It's deliberately global (not per-profile): a profile switch force-reseeds to
// that profile's default, while within a profile new chats keep your last pick.
const COMPOSER_MODEL_KEY = 'hermes.desktop.composer.model'
const COMPOSER_PROVIDER_KEY = 'hermes.desktop.composer.provider'
const COMPOSER_EFFORT_KEY = 'hermes.desktop.composer.reasoning-effort'
Expand Down Expand Up @@ -196,6 +191,7 @@ export function mergeSessionPage(

export const $connection = atom<HermesConnection | null>(null)
export const $gatewayState = atom('idle')
export const $draftModelOverridePending = atom(false)
export const $sessions = atom<SessionInfo[]>([])
export const $sessionsTotal = atom<number>(0)
// Cron-job sessions (source === 'cron') are fetched as their own list so the
Expand Down Expand Up @@ -301,6 +297,7 @@ export const setSessionProfileTotals = (next: Updater<Record<string, number>>) =
export const setSessionsLoading = (next: Updater<boolean>) => updateAtom($sessionsLoading, next)
export const setWorkingSessionIds = (next: Updater<string[]>) => updateAtom($workingSessionIds, next)
export const setActiveSessionId = (next: Updater<string | null>) => updateAtom($activeSessionId, next)
export const setDraftModelOverridePending = (next: Updater<boolean>) => updateAtom($draftModelOverridePending, next)
export const setSelectedStoredSessionId = (next: Updater<string | null>) => updateAtom($selectedStoredSessionId, next)
export const setMessages = (next: Updater<ChatMessage[]>) => updateAtom($messages, next)
export const setFreshDraftReady = (next: Updater<boolean>) => updateAtom($freshDraftReady, next)
Expand Down
Loading