Skip to content

The agent that grows with you

fix(desktop): reveal the notification session without replacing its tab

brooklyn!85571c16b621
diff --git a/apps/desktop/src/app/contrib/hooks/notification-focus.test.tsx b/apps/desktop/src/app/contrib/hooks/notification-focus.test.tsx
new file mode 100644
index 00000000000..d76956dcdd4
--- /dev/null
+++ b/apps/desktop/src/app/contrib/hooks/notification-focus.test.tsx
@@ -0,0 +1,101 @@
+import { act, renderHook } from '@testing-library/react'
+import { afterEach, beforeAll, beforeEach, expect, it, vi } from 'vitest'
+
+import { paneMirror } from '@/app/chat/pane-mirror'
+import { sessionRoute, syncWorkspaceRoute } from '@/app/routes'
+import { group } from '@/components/pane-shell/tree/model'
+import * as tree from '@/components/pane-shell/tree/store'
+import { registry } from '@/contrib/registry'
+import { $selectedStoredSessionId } from '@/store/session'
+import { $sessionTiles, closeSessionTile, discardSessionTile, openSessionTile, patchSessionTile } from '@/store/session-states'
+
+import { useDesktopIntegrations } from './use-desktop-integrations'
+
+const originalBridge = window.hermesDesktop
+
+beforeAll(() => {
+ const dispose = registry.register({
+ area: 'panes',
+ data: { placement: 'main', uncloseable: true },
+ id: 'workspace',
+ render: () => null,
+ title: 'Chat'
+ })
+ tree.watchContributedPanes()
+ paneMirror({
+ source: $sessionTiles,
+ key: tile => tile.storedSessionId,
+ prefix: 'session-tile',
+ dir: () => 'center',
+ minWidth: '20rem',
+ title: id => id,
+ render: () => null,
+ close: closeSessionTile
+ })()
+
+ return dispose
+})
+
+beforeEach(() => {
+ window.localStorage.clear()
+ tree.declareDefaultTree(group(['workspace'], { active: 'workspace', id: 'main' }))
+ tree.$layoutTree.set(group(['workspace'], { active: 'workspace', id: 'main' }))
+ $selectedStoredSessionId.set('main-chat')
+ syncWorkspaceRoute('/main-chat')
+})
+
+afterEach(() => {
+ for (const tile of $sessionTiles.get()) {
+ discardSessionTile(tile.storedSessionId)
+ }
+
+ $selectedStoredSessionId.set(null)
+ window.hermesDesktop = originalBridge
+ syncWorkspaceRoute('/')
+})
+
+it('a native click reveals the existing remote Bot tab without changing its owner or duplicating main', () => {
+ let fire!: (id: string) => void
+ window.hermesDesktop = {
+ onFocusSession: callback => {
+ fire = callback
+
+ return () => undefined
+ }
+ } as Window['hermesDesktop']
+ const navigate = vi.fn()
+ renderHook(() =>
+ useDesktopIntegrations({
+ activeProfile: 'default',
+ chatOpen: false,
+ hasPreview: false,
+ locationPathname: '/settings',
+ navigate,
+ profileReady: false,
+ refreshSessions: vi.fn(),
+ resumeLastSession: false,
+ resumeExhaustedSessionId: null,
+ routedSessionId: null,
+ runtimeIdByStoredSessionId: { current: new Map() },
+ sessions: []
+ })
+ )
+ const scope = {
+ ownerRoute: { connectionId: 'remote-writer', profile: 'writer', mode: 'remote' as const },
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'remote-writer::writer',
+ workspaceTabTitle: 'Bot Chat'
+ }
+ openSessionTile('bot-chat', 'center', 'workspace', undefined, scope)
+ patchSessionTile('bot-chat', { runtimeId: 'bot-runtime' })
+ tree.revealTreePane('workspace')
+ const before = $sessionTiles.get()
+
+ act(() => fire('bot-runtime'))
+
+ expect(tree.isPaneVisible('session-tile:bot-chat')).toBe(true)
+ expect($sessionTiles.get()).toEqual(before)
+ expect($selectedStoredSessionId.get()).toBe('main-chat')
+ // Close the overlay without navigating main onto the tile's conversation.
+ expect(navigate).toHaveBeenCalledWith(sessionRoute('main-chat'), { replace: true })
+})
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 8bc399e67fb..f6586c1fe16 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
@@ -1,12 +1,15 @@
import { renderHook } from '@testing-library/react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { createClientSessionState } from '@/lib/chat-runtime'
import { requestMcpInstallFromDeepLink } from '@/store/mcp-deeplink-install'
import { _resetLegacyDiscardForTests } from '@/store/session'
+import { dropSessionState, publishSessionState } from '@/store/session-states'
import type * as WindowsStore from '@/store/windows'
import type { SessionInfo } from '@/types/hermes'
import { makeSessionInfo } from '../../../test/session-info'
+import { sessionRoute } from '../../routes'
import { useDesktopIntegrations } from './use-desktop-integrations'
@@ -567,4 +570,67 @@ describe('useDesktopIntegrations', () => {
expect(navigate).not.toHaveBeenCalled()
})
})
+
+ describe('notification click -> focus-session id translation', () => {
+ function withFocusSession(): (sessionId: string) => void {
+ let handler: ((sessionId: string) => void) | undefined
+ desktopWindow.hermesDesktop = {
+ ...desktopWindow.hermesDesktop,
+ onFocusSession: (cb: (sessionId: string) => void) => {
+ handler = cb
+
+ return () => undefined
+ }
+ } as unknown as Window['hermesDesktop']
+
+ return sessionId => handler?.(sessionId)
+ }
+
+ function renderWithRuntimeMap(map: Map<string, string>) {
+ return renderHook(
+ ({ sessions }: { sessions: readonly SessionInfo[] }) =>
+ useDesktopIntegrations({
+ activeProfile: 'default',
+ chatOpen: false,
+ hasPreview: false,
+ locationPathname: '/',
+ navigate,
+ profileReady: true,
+ refreshSessions: vi.fn(),
+ resumeExhaustedSessionId: null,
+ resumeLastSession: false,
+ routedSessionId: null,
+ runtimeIdByStoredSessionId: { current: map },
+ sessions
+ }),
+ { initialProps: { sessions: [] as readonly SessionInfo[] } }
+ )
+ }
+
+ it('translates a runtime id via the window map before navigating', () => {
+ const fire = withFocusSession()
+
+ renderWithRuntimeMap(new Map([['stored-abc', 'runtime-123']]))
+ fire('runtime-123')
+
+ // 'stack' intent spends the unoccupied main draft → in-place navigate.
+ expect(navigate).toHaveBeenCalledWith(sessionRoute('stored-abc'))
+ })
+
+ it('falls back to the durable per-runtime state mirror when the window map has no binding', () => {
+ const fire = withFocusSession()
+ const { unmount } = renderWithRuntimeMap(new Map())
+
+ // Simulate a main-pane runtime whose ensureSessionState binding lives in
+ // the shared store mirror, not this window's map (window reload /
+ // pop-out window / gateway respawn).
+ publishSessionState('runtime-999', createClientSessionState('stored-xyz'))
+ fire('runtime-999')
+
+ expect(navigate).toHaveBeenCalledWith(sessionRoute('stored-xyz'))
+
+ unmount()
+ dropSessionState('runtime-999')
+ })
+ })
})
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 6d8cef2ec16..ba4c223a748 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
@@ -17,12 +17,14 @@ import {
import { openPluginInstallRequest } from '@/store/plugin-install-request'
import { openFolderAsProject } from '@/store/projects'
import {
+ $selectedStoredSessionId,
getRememberedRoute,
getRememberedSessionId,
sessionBelongsToProfile,
setRememberedRoute,
setRememberedSessionId
} from '@/store/session'
+import { $botChatScopes, $sessionTiles, storedSessionIdForRuntimeId } from '@/store/session-states'
import { onSessionsChanged } from '@/store/session-sync'
import { openUpdatesWindow, startUpdatePoller, stopUpdatePoller } from '@/store/updates'
import { isBrowserWindow, isHudWindow, isSecondaryWindow } from '@/store/windows'
@@ -209,12 +211,23 @@ export function useDesktopIntegrations({
useEffect(() => {
const unsubscribe = window.hermesDesktop?.onFocusSession?.(sessionId => {
if (sessionId) {
- openSession(storedSessionIdForNotification(sessionId, runtimeIdByStoredSessionId.current), navigate, 'stack')
+ // Reloads and runtime recovery can leave only the shared mirror bound.
+ const viaLocalMap = storedSessionIdForNotification(sessionId, runtimeIdByStoredSessionId.current)
+ const storedId = viaLocalMap !== sessionId ? viaLocalMap : (storedSessionIdForRuntimeId(sessionId) ?? sessionId)
+
+ // A notification reveals a tab; it must not reclassify a Bot chat.
+ const scope = $sessionTiles.get().find(tile => tile.storedSessionId === storedId) ?? $botChatScopes.get()[storedId]
+
+ if (isOverlayView(appViewForPath(locationPathname))) {
+ navigate(sessionRoute($selectedStoredSessionId.get() ?? ''), { replace: true })
+ }
+
+ openSession(storedId, navigate, 'stack', scope && { ...scope, workspaceMode: scope.workspaceMode ?? 'sessions' })
}
})
return () => unsubscribe?.()
- }, [navigate, runtimeIdByStoredSessionId])
+ }, [locationPathname, navigate, runtimeIdByStoredSessionId])
useEffect(() => {
const unsubscribe = window.hermesDesktop?.onNotificationAction?.(({ actionId, sessionId }) => {
diff --git a/apps/desktop/src/app/open-session.test.ts b/apps/desktop/src/app/open-session.test.ts
index 4a3f95d7df0..bbc99fa1aa4 100644
--- a/apps/desktop/src/app/open-session.test.ts
+++ b/apps/desktop/src/app/open-session.test.ts
@@ -165,6 +165,17 @@ describe('openSession', () => {
expect(navigate).not.toHaveBeenCalled()
})
+ it.each(['stack', 'tab'] as const)('%s uncovers the existing main chat when a page is showing', intent => {
+ $selectedStoredSessionId.set('s1')
+ focusOpenSession.mockReturnValue('main')
+ workspaceIsPageGet.mockReturnValue(true)
+
+ openSession('s1', navigate, intent)
+
+ expect(navigate).toHaveBeenCalledWith('/c/s1')
+ expect(openSessionTile).not.toHaveBeenCalled()
+ })
+
it('stack opens a tab rather than taking main from a loaded chat', () => {
$selectedStoredSessionId.set('s0')
focusOpenSession.mockReturnValue(null)
diff --git a/apps/desktop/src/app/open-session.ts b/apps/desktop/src/app/open-session.ts
index e76a9face20..b759b1c60b0 100644
--- a/apps/desktop/src/app/open-session.ts
+++ b/apps/desktop/src/app/open-session.ts
@@ -137,6 +137,10 @@ export function openSession(
const focused = focusOpenSession(storedSessionId, workspaceScope)
if (focused) {
+ if (focusedSessionNeedsRoute(focused, $workspaceIsPage.get())) {
+ navigate(sessionRoute(storedSessionId))
+ }
+
return
}