The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a branched chat no longer gets stuck on a loading spinner
Teknium38241883765f
diff --git a/apps/desktop/src/app/session/hooks/resume-route-commit.test.tsx b/apps/desktop/src/app/session/hooks/resume-route-commit.test.tsxnew file mode 100644index 00000000000..0fb34c5840d--- /dev/null+++ b/apps/desktop/src/app/session/hooks/resume-route-commit.test.tsx@@ -0,0 +1,91 @@+import { cleanup, renderHook } from '@testing-library/react'+import { useRef } from 'react'+import { afterEach, expect, it, vi } from 'vitest'++import { getLatestSessionMessages } from '@/hermes'+import { ensureGatewayProfile } from '@/store/profile'+import {+ $activeSessionId,+ setActiveSessionId,+ setConnection,+ setMessages,+ setSelectedStoredSessionId,+ setSessions+} from '@/store/session'+import { clearAllSessionStates } from '@/store/session-states'++import { sessionRoute } from '../../routes'++import { useSessionActions } from './use-session-actions'+import { useSessionStateCache } from './use-session-state-cache'++vi.mock('@/hermes', async original => ({+ ...(await original<Record<string, unknown>>()),+ getLatestSessionMessages: vi.fn()+}))+vi.mock('@/store/profile', async original => ({+ ...(await original<Record<string, unknown>>()),+ ensureGatewayProfile: vi.fn().mockResolvedValue(undefined)+}))++afterEach(() => {+ cleanup()+ clearAllSessionStates()+ setSessions([])+ setMessages([])+ setActiveSessionId(null)+ setSelectedStoredSessionId(null)+ vi.restoreAllMocks()+})++it('binds the resumed session when the route commits onto it mid-resume', async () => {+ // Branch/create call navigate(child) and then resume(child): the route+ // token still names the parent at entry and only flips to the child while+ // the resume is awaiting the gateway.+ let routeToken = `${sessionRoute('parent')}::`+ setConnection(null)+ setSessions([])+ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'child' })+ vi.mocked(ensureGatewayProfile).mockImplementationOnce(async () => {+ routeToken = `${sessionRoute('child')}::`+ })++ const requestGateway = vi.fn().mockResolvedValue({+ info: {},+ message_count: 0,+ messages: [],+ resumed: 'child',+ session_id: 'child-runtime',+ session_key: 'child'+ })++ const { result } = renderHook(() => {+ const busyRef = useRef(false)++ const cache = useSessionStateCache({+ activeSessionId: null,+ busyRef,+ selectedStoredSessionId: null,+ setAwaitingResponse: vi.fn(),+ setBusy: vi.fn(),+ setMessages+ })++ return useSessionActions({+ ...cache,+ activeSessionId: null,+ busyRef,+ creatingSessionRef: useRef(false),+ getRouteToken: () => routeToken,+ getRoutedStoredSessionId: () => null,+ navigate: vi.fn(),+ requestGateway,+ routedSessionId: null,+ selectedStoredSessionId: null+ })+ })++ await result.current.resumeSession('child', true)++ expect($activeSessionId.get()).toBe('child-runtime')+})diff --git a/apps/desktop/src/app/session/hooks/session-context-drift.ts b/apps/desktop/src/app/session/hooks/session-context-drift.tsindex 9f9c038505b..3b62817c606 100644--- a/apps/desktop/src/app/session/hooks/session-context-drift.ts+++ b/apps/desktop/src/app/session/hooks/session-context-drift.ts@@ -24,6 +24,17 @@ export function routeTargetFromToken(token: string): RouteTarget {return routeSessionId(pathname) ?? (isNewChatRoute(pathname) ? '__new__' : null)}+/**+ * Whether a resume begun under `startRouteToken` still owns the view. The route+ * arriving at the resumed session itself is not a switch: create/branch call+ * navigate(target) and then resume(target) before the token re-renders, so the+ * token captured at entry can still name the previous chat. Reading that commit+ * as drift abandoned the branch child's resume and left its pane on the loader.+ */+export function resumeRouteStillCurrent(startRouteToken: string, nowRouteToken: string, storedSessionId: string) {+ return nowRouteToken === startRouteToken || routeTargetFromToken(nowRouteToken) === storedSessionId+}+interface SessionContextDriftArgs {startRouteToken: stringnowRouteToken: stringdiff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.tsindex 12fd9180ba5..40bff29e2d0 100644--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts@@ -158,7 +158,12 @@ import type {import { navigateToWorkspacePage, NEW_CHAT_ROUTE, sessionRoute, SETTINGS_ROUTE } from '../../../routes'import type { ClientSessionState, SidebarNavItem } from '../../../types'-import { pinStoredSessionForOwner, releaseStoredSessionPins, sessionContextDrift } from '../session-context-drift'+import {+ pinStoredSessionForOwner,+ releaseStoredSessionPins,+ resumeRouteStillCurrent,+ sessionContextDrift+} from '../session-context-drift'import { singleFlightSessionResume } from '../use-prompt-actions/single-flight-resume'import { sessionCreateOverrideParams, type SessionCreateOverrides, type SessionSeedMessage } from './create-overrides'@@ -1296,7 +1301,7 @@ export function useSessionActions({const isCurrentResume = () =>resumeRequestRef.current === requestId &&selectedStoredSessionIdRef.current === storedSessionId &&- getRouteToken() === routeToken+ resumeRouteStillCurrent(routeToken, getRouteToken(), storedSessionId)// A reconnect re-resumes the runtime this view is streaming. Let its// replay land while that runtime still owns the view. Otherwise the REST