Skip to content

The agent that grows with you

fix(desktop): keep the HUD on the session it was opened for (#82360)

Ne0tericb105a3b057c8
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx
index 7e1de44664c..34eaecab0af 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.test.tsx
@@ -2,10 +2,25 @@ import { renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { _resetLegacyDiscardForTests } from '@/store/session'
+import type * as WindowsStore from '@/store/windows'
import type { SessionInfo } from '@/types/hermes'
import { useDesktopIntegrations } from './use-desktop-integrations'
+// Mutable HUD-window flag so the restore tests can flip the window kind the
+// hook believes it runs in. Default false keeps the pre-existing restore
+// coverage exercising the real main-window path.
+const { hudWindowMock } = vi.hoisted(() => ({ hudWindowMock: vi.fn(() => false) }))
+
+vi.mock('@/store/windows', async importOriginal => {
+ const actual = await importOriginal<typeof WindowsStore>()
+
+ return {
+ ...actual,
+ isHudWindow: () => hudWindowMock()
+ }
+})
+
// Pure-jsdom localStorage (no nanostores persistence module needed — the
// production functions write directly to window.localStorage through the
// persistString/storedString helpers in @/lib/storage, which in jsdom resolves
@@ -42,6 +57,8 @@ describe('useDesktopIntegrations', () => {
window.localStorage.clear()
_resetLegacyDiscardForTests()
navigate = vi.fn()
+ // Every test starts as a main window; only the HUD describe flips this.
+ hudWindowMock.mockReturnValue(false)
// Stub the desktop bridge so the hook's useEffect callbacks don't try to
// reach real Electron IPC. The established desktop-test pattern assigns a
@@ -238,6 +255,43 @@ describe('useDesktopIntegrations', () => {
})
})
+ describe('HUD window (win=hud)', () => {
+ beforeEach(() => {
+ hudWindowMock.mockReturnValue(true)
+ })
+
+ it('does NOT restore remembered navigation on a blank new-chat route', () => {
+ window.localStorage.setItem('hermes.desktop.lastSessionId.profile.default', 'remembered-session')
+ window.localStorage.setItem('hermes.desktop.lastRoute.profile.default', '/remembered-session')
+
+ render({ profileReady: true, sessions: [session({ id: 'remembered-session', profile: 'default' })] })
+
+ // The HUD is a fresh full renderer booting at the default route, but its
+ // destination was chosen explicitly by hudTargetSessionId() at open time
+ // — remembered-navigation restore must not hijack it to the last session.
+ expect(navigate).not.toHaveBeenCalled()
+ })
+
+ it('does NOT write remembered navigation while showing a session', () => {
+ render({
+ profileReady: true,
+ routedSessionId: 'live',
+ sessions: [session({ id: 'live', profile: 'default' })]
+ })
+
+ expect(window.localStorage.getItem('hermes.desktop.lastSessionId.profile.default')).toBeNull()
+ expect(window.localStorage.getItem('hermes.desktop.lastRoute.profile.default')).toBeNull()
+ })
+
+ it('does not restore the remembered session id either', () => {
+ window.localStorage.setItem('hermes.desktop.lastSessionId.profile.default', 'remembered-session')
+
+ render({ profileReady: true, sessions: [session({ id: 'remembered-session', profile: 'default' })] })
+
+ expect(navigate).not.toHaveBeenCalled()
+ })
+ })
+
describe('legacy key behavior', () => {
it('discards legacy global keys on read and does NOT restore from them', () => {
// Simulate a pre-per-profile install.
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
index 6a6638b1d1f..9793fab2776 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
@@ -14,7 +14,7 @@ import {
} from '@/store/session'
import { onSessionsChanged } from '@/store/session-sync'
import { openUpdatesWindow, startUpdatePoller, stopUpdatePoller } from '@/store/updates'
-import { isSecondaryWindow } from '@/store/windows'
+import { isHudWindow, isSecondaryWindow } from '@/store/windows'
import type { SessionInfo } from '@/types/hermes'
import { requestComposerFocus, requestComposerInsert } from '../../chat/composer/focus'
@@ -82,7 +82,7 @@ export function useDesktopIntegrations({
// This ref is a one-time lifecycle latch, not a mirror of reactive atom state.
// eslint-disable-next-line no-restricted-syntax
useEffect(() => {
- if (!profileReady) {
+ if (!profileReady || isHudWindow()) {
return
}