The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): resolve sessions from sidebar caches
David Dudok de Wit58a9062f6084
diff --git a/apps/desktop/src/app/chat/sidebar/index.tsx b/apps/desktop/src/app/chat/sidebar/index.tsxindex 5166c968062..82ac170dcb0 100644--- a/apps/desktop/src/app/chat/sidebar/index.tsx+++ b/apps/desktop/src/app/chat/sidebar/index.tsx@@ -78,9 +78,9 @@ import {$profiles,$profileScope,ALL_PROFILES,- messagingProfileFor,messagingTotalsKey,- normalizeProfileKey+ normalizeProfileKey,+ sidebarProfileForScope} from '@/store/profile'import {$activeProjectId,@@ -398,7 +398,7 @@ export function ChatSidebar({// profile while scope is still ALL (persisted), the rail is hidden and they'd// otherwise be stuck in the grouped view with no way out.const showAllProfiles = multiProfile && profileScope === ALL_PROFILES- const messagingProfile = messagingProfileFor(profileScope)+ const messagingProfile = sidebarProfileForScope(profileScope)const agentOrderIds = useStore($sidebarSessionOrderIds)const agentOrderManual = useStore($sidebarSessionOrderManual)const workspaceOrderIds = useStore($sidebarWorkspaceOrderIds)diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/resolve-stored-session.test.ts b/apps/desktop/src/app/session/hooks/use-session-actions/resolve-stored-session.test.tsindex 41b6117d544..4e7513a95b7 100644--- a/apps/desktop/src/app/session/hooks/use-session-actions/resolve-stored-session.test.ts+++ b/apps/desktop/src/app/session/hooks/use-session-actions/resolve-stored-session.test.ts@@ -3,7 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import type * as HermesModule from '@/hermes'import { getSession } from '@/hermes'import { $activeGatewayProfile, $profiles } from '@/store/profile'-import { $sessions } from '@/store/session'+import { $cronSessions, $messagingSessions, $sessions } from '@/store/session'import type { SessionInfo } from '@/types/hermes'import { resolveSessionProfile, resolveStoredSession } from './utils'@@ -21,6 +21,8 @@ const profiles = (...names: string[]) => names.map(name => ({ name }) as never)describe('resolveStoredSession profile ownership', () => {beforeEach(() => {+ $cronSessions.set([])+ $messagingSessions.set([])$sessions.set([])$profiles.set(profiles('default', 'meta'))$activeGatewayProfile.set('meta')@@ -28,6 +30,8 @@ describe('resolveStoredSession profile ownership', () => {})afterEach(() => {+ $cronSessions.set([])+ $messagingSessions.set([])$sessions.set([])$profiles.set([])$activeGatewayProfile.set('default')@@ -42,6 +46,19 @@ describe('resolveStoredSession profile ownership', () => {expect(mockGetSession).not.toHaveBeenCalled()})+ it.each([+ ['cron', $cronSessions],+ ['messaging', $messagingSessions]+ ])('resolves a %s sidebar row without duplicating it into regular sessions', async (_source, store) => {+ store.set([session({ id: 's1', profile: 'default' })])++ const resolved = await resolveStoredSession('s1')++ expect(resolved?.profile).toBe('default')+ expect(mockGetSession).not.toHaveBeenCalled()+ expect($sessions.get()).toEqual([])+ })+it('treats a profile-less cache hit as unresolved when multiple profiles exist', async () => {$sessions.set([session({ id: 's1' })])mockGetSession.mockRejectedValueOnce(new Error('404: Session not found'))diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts b/apps/desktop/src/app/session/hooks/use-session-actions/utils.tsindex 533f0e9ca90..5fede6010d5 100644--- a/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts+++ b/apps/desktop/src/app/session/hooks/use-session-actions/utils.ts@@ -7,7 +7,9 @@ import { reconcileApprovalModeForProfile } from '@/store/approval-mode'import { requestDesktopOnboardingForCredentialWarning } from '@/store/onboarding'import { $activeGatewayProfile, $profiles, normalizeProfileKey } from '@/store/profile'import {+ $cronSessions,$currentCwd,+ $messagingSessions,$sessions,commitWorkspaceCwdForSelectedSession,releaseWorkspaceCwdOwner,@@ -1291,7 +1293,9 @@ function upsertResolvedSession(session: SessionInfo, storedSessionId: string) {}export async function resolveStoredSession(storedSessionId: string): Promise<SessionInfo | undefined> {- const cached = $sessions.get().find(session => sessionMatchesStoredId(session, storedSessionId))+ const cached = [...$sessions.get(), ...$cronSessions.get(), ...$messagingSessions.get()].find(session =>+ sessionMatchesStoredId(session, storedSessionId)+ )// A row with no owning profile can't route a resume when more than one// profile exists — a resume without a profile lands on whichever gateway isdiff --git a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts b/apps/desktop/src/app/session/hooks/use-session-list-actions.tsindex bdff1ddb1ff..a3416032f2a 100644--- a/apps/desktop/src/app/session/hooks/use-session-list-actions.ts+++ b/apps/desktop/src/app/session/hooks/use-session-list-actions.ts@@ -17,7 +17,7 @@ import {SIDEBAR_FILTERED_PAGE_SIZE,SIDEBAR_SESSIONS_PAGE_SIZE} from '@/store/layout'-import { messagingProfileFor, messagingTotalsKey, normalizeProfileKey } from '@/store/profile'+import { messagingTotalsKey, normalizeProfileKey, sidebarProfileForScope } from '@/store/profile'import { $removedSessionIds } from '@/store/projects'import {$messagingSessions,@@ -111,11 +111,11 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg/** Refresh the active profile's messaging-platform sidebar slice. */const refreshMessagingSessions = useCallback(async () => {- const sessionProfile = messagingProfileFor(profileScope)+ const sessionProfile = sidebarProfileForScope(profileScope)// A callback captured before a profile switch may still be queued by an// event subscription. Do not let it start a request against the old scope.- if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {+ if (sidebarProfileForScope(profileScopeRef.current) !== sessionProfile) {return}@@ -129,7 +129,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArgif (refreshMessagingSessionsRequestRef.current !== requestId ||- messagingProfileFor(profileScopeRef.current) !== sessionProfile+ sidebarProfileForScope(profileScopeRef.current) !== sessionProfile) {return}@@ -150,9 +150,9 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg/** Page one messaging platform without replacing another platform's rows. */const loadMoreMessagingForPlatform = useCallback(async (platform: string) => {- const sessionProfile = messagingProfileFor(profileScope)+ const sessionProfile = sidebarProfileForScope(profileScope)- if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {+ if (sidebarProfileForScope(profileScopeRef.current) !== sessionProfile) {return}@@ -184,7 +184,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArgif (loadMoreMessagingRequestRef.current[requestKey] !== requestId ||- messagingProfileFor(profileScopeRef.current) !== sessionProfile+ sidebarProfileForScope(profileScopeRef.current) !== sessionProfile) {return}@@ -205,9 +205,9 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg/** Refresh cron jobs only while the profile that requested them remains active. */const refreshCronJobs = useCallback(async () => {- const sessionProfile = messagingProfileFor(profileScope)+ const sessionProfile = sidebarProfileForScope(profileScope)- if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {+ if (sidebarProfileForScope(profileScopeRef.current) !== sessionProfile) {return}@@ -220,9 +220,9 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg/** Refresh every sidebar session slice without committing an obsolete profile response. */const refreshSessions = useCallback(async () => {- const sessionProfile = messagingProfileFor(profileScope)+ const sessionProfile = sidebarProfileForScope(profileScope)- if (messagingProfileFor(profileScopeRef.current) !== sessionProfile) {+ if (sidebarProfileForScope(profileScopeRef.current) !== sessionProfile) {return}@@ -265,7 +265,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArgif (refreshSessionsRequestRef.current === requestId &&- messagingProfileFor(profileScopeRef.current) === sessionProfile+ sidebarProfileForScope(profileScopeRef.current) === sessionProfile) {const recents = result.recents@@ -331,7 +331,7 @@ export function useSessionListActions({ profileScope }: UseSessionListActionsArg}// Cron *jobs* are a distinct API (getCronJobs), not a session slice.- if (messagingProfileFor(profileScopeRef.current) === sessionProfile) {+ if (sidebarProfileForScope(profileScopeRef.current) === sessionProfile) {void refreshCronJobs()}}, [profileScope, refreshCronJobs])diff --git a/apps/desktop/src/store/profile.ts b/apps/desktop/src/store/profile.tsindex 09b0b29cbf0..3d8cf0a630a 100644--- a/apps/desktop/src/store/profile.ts+++ b/apps/desktop/src/store/profile.ts@@ -315,8 +315,8 @@ export async function ensureGatewayProfile(profile: string | null | undefined):export const ALL_PROFILES = '__all__'-/** Normalize a sidebar scope to the profile key used by messaging session queries. */-export const messagingProfileFor = (profileScope: string): string =>+/** Normalize a sidebar scope to the profile key used by session and cron queries. */+export const sidebarProfileForScope = (profileScope: string): string =>profileScope === ALL_PROFILES ? 'all' : normalizeProfileKey(profileScope)/** Key a platform total by its Desktop profile route so counts cannot leak across profiles. */