Skip to content

The agent that grows with you

fix(desktop): stop ⌘K and notification clicks stealing the main tab

Brooklyn Nicholson93e54139c444
diff --git a/apps/desktop/src/app/command-palette/index.tsx b/apps/desktop/src/app/command-palette/index.tsx
index 59f8ba0cbae..d53b3199dc1 100644
--- a/apps/desktop/src/app/command-palette/index.tsx
+++ b/apps/desktop/src/app/command-palette/index.tsx
@@ -451,11 +451,13 @@ export function CommandPalette() {
const go = useCallback((path: string) => () => navigateToWorkspacePage(navigate, path), [navigate])
- // Sessions: plain select = open in-place (focus existing tile/main, else main);
- // ⌘/⌃-select / ⌘-Enter = new tab; ⇧⌘ = own window. Same door as the sidebar.
+ // Sessions: plain select = open beside what's already loaded (focus existing
+ // tile/main, else a new tab — main only when it's a blank draft);
+ // ⌘/⌃-select / ⌘-Enter = force a new tab; ⇧⌘ = own window. Same door as the
+ // sidebar, minus the sidebar's licence to spend main.
const goSession = useCallback(
(sessionId: string) => (event?: { ctrlKey?: boolean; metaKey?: boolean; shiftKey?: boolean }) => {
- openSession(sessionId, navigate, openSessionIntentFromModifiers(event))
+ openSession(sessionId, navigate, openSessionIntentFromModifiers(event, 'stack'))
},
[navigate]
)
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 0c61eb1f795..d64a3a6fe79 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
@@ -128,12 +128,14 @@ export function useDesktopIntegrations({
}, [resumeExhaustedSessionId])
// Native-notification click -> jump to the session WHERE IT ALREADY IS (open
- // tile / main) instead of forcing main. Runtime id is translated to the
- // stored id the chat route is keyed by; action buttons resolve in place.
+ // tile / main), else beside what's loaded rather than over it — the click
+ // came from outside the app and shouldn't cost the user the chat they left
+ // on screen. Runtime id is translated to the stored id the chat route is
+ // keyed by; action buttons resolve in place.
useEffect(() => {
const unsubscribe = window.hermesDesktop?.onFocusSession?.(sessionId => {
if (sessionId) {
- openSession(storedSessionIdForNotification(sessionId, runtimeIdByStoredSessionId.current), navigate)
+ openSession(storedSessionIdForNotification(sessionId, runtimeIdByStoredSessionId.current), navigate, 'stack')
}
})
diff --git a/apps/desktop/src/app/open-session.test.ts b/apps/desktop/src/app/open-session.test.ts
index 63d6ec5374f..5f790faee01 100644
--- a/apps/desktop/src/app/open-session.test.ts
+++ b/apps/desktop/src/app/open-session.test.ts
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
const focusOpenSession = vi.fn()
const openSessionTile = vi.fn()
+const reuseBlankDraftTile = vi.fn()
const openSessionInNewWindow = vi.fn()
const canOpenSessionWindow = vi.fn(() => true)
const workspaceIsPageGet = vi.fn(() => false)
@@ -10,7 +11,8 @@ vi.mock('@/store/session-states', () => ({
focusedSessionNeedsRoute: (focused: 'main' | 'tile' | null, workspaceIsPage: boolean) =>
!focused || (focused === 'main' && workspaceIsPage),
focusOpenSession: (...args: unknown[]) => focusOpenSession(...args),
- openSessionTile: (...args: unknown[]) => openSessionTile(...args)
+ openSessionTile: (...args: unknown[]) => openSessionTile(...args),
+ reuseBlankDraftTile: (...args: unknown[]) => reuseBlankDraftTile(...args)
}))
vi.mock('@/store/windows', () => ({
@@ -23,12 +25,15 @@ vi.mock('./routes', () => ({
sessionRoute: (id: string) => `/c/${encodeURIComponent(id)}`
}))
-import { openSession, openSessionIntentFromModifiers, mainChatOccupied } from './open-session'
+import { $activeSessionId, $selectedStoredSessionId } from '@/store/session'
+
+import { mainChatOccupied, openSession, openSessionIntentFromModifiers } from './open-session'
/**
* The question behind both the sidebar "+" and a palette open: is there a
* conversation on main that must not be discarded? A create affordance stacks a
- * tab rather than replacing a chat that may still be mid-turn.
+ * tab rather than replacing a chat that may still be mid-turn, and an open from
+ * nowhere does the same.
*/
describe('mainChatOccupied', () => {
it('is occupied once a conversation is on screen', () => {
@@ -55,12 +60,22 @@ describe('openSessionIntentFromModifiers', () => {
expect(openSessionIntentFromModifiers({})).toBe('in-place')
})
+ it('returns the caller base for an unmodified select', () => {
+ expect(openSessionIntentFromModifiers(undefined, 'stack')).toBe('stack')
+ expect(openSessionIntentFromModifiers({}, 'stack')).toBe('stack')
+ })
+
it('reads ⌘/⌃ as tab and ⇧+mod as window', () => {
expect(openSessionIntentFromModifiers({ metaKey: true })).toBe('tab')
expect(openSessionIntentFromModifiers({ ctrlKey: true })).toBe('tab')
expect(openSessionIntentFromModifiers({ metaKey: true, shiftKey: true })).toBe('window')
expect(openSessionIntentFromModifiers({ shiftKey: true })).toBe('in-place')
})
+
+ it('lets modifiers override the base', () => {
+ expect(openSessionIntentFromModifiers({ metaKey: true }, 'stack')).toBe('tab')
+ expect(openSessionIntentFromModifiers({ metaKey: true, shiftKey: true }, 'stack')).toBe('window')
+ })
})
describe('openSession', () => {
@@ -73,6 +88,9 @@ describe('openSession', () => {
openSessionInNewWindow.mockReset()
canOpenSessionWindow.mockReturnValue(true)
workspaceIsPageGet.mockReturnValue(false)
+ reuseBlankDraftTile.mockReset()
+ $activeSessionId.set(null)
+ $selectedStoredSessionId.set(null)
})
it('in-place focuses an existing tile and does not navigate', () => {
@@ -117,6 +135,55 @@ describe('openSession', () => {
expect(navigate).not.toHaveBeenCalled()
})
+ it('stack focuses a session that is already on screen', () => {
+ $selectedStoredSessionId.set('s0')
+ focusOpenSession.mockReturnValue('tile')
+ openSession('s1', navigate, 'stack')
+ expect(openSessionTile).not.toHaveBeenCalled()
+ expect(navigate).not.toHaveBeenCalled()
+ })
+
+ it('stack opens a tab rather than taking main from a loaded chat', () => {
+ $selectedStoredSessionId.set('s0')
+ focusOpenSession.mockReturnValue(null)
+ openSession('s1', navigate, 'stack')
+ expect(openSessionTile).toHaveBeenCalledWith('s1', 'center')
+ expect(navigate).not.toHaveBeenCalled()
+ })
+
+ it('stack spends an open blank draft tab before stacking a new one', () => {
+ $selectedStoredSessionId.set('s0')
+ focusOpenSession.mockReturnValue(null)
+ reuseBlankDraftTile.mockReturnValue(true)
+ openSession('s1', navigate, 'stack')
+ expect(reuseBlankDraftTile).toHaveBeenCalledWith('s1')
+ expect(openSessionTile).not.toHaveBeenCalled()
+ expect(navigate).not.toHaveBeenCalled()
+ })
+
+ it('stack prefers the session already on screen over a blank draft tab', () => {
+ $selectedStoredSessionId.set('s0')
+ focusOpenSession.mockReturnValue('tile')
+ reuseBlankDraftTile.mockReturnValue(true)
+ openSession('s1', navigate, 'stack')
+ expect(reuseBlankDraftTile).not.toHaveBeenCalled()
+ expect(openSessionTile).not.toHaveBeenCalled()
+ })
+
+ it('stack opens a tab while main is mid-turn on an unsaved session', () => {
+ $activeSessionId.set('runtime-a')
+ focusOpenSession.mockReturnValue(null)
+ openSession('s1', navigate, 'stack')
+ expect(openSessionTile).toHaveBeenCalledWith('s1', 'center')
+ })
+
+ it('stack loads into main when it holds only a blank draft', () => {
+ focusOpenSession.mockReturnValue(null)
+ openSession('s1', navigate, 'stack')
+ expect(navigate).toHaveBeenCalledWith('/c/s1')
+ expect(openSessionTile).not.toHaveBeenCalled()
+ })
+
it('window pops out when the bridge supports it', () => {
openSession('s1', navigate, 'window')
expect(openSessionInNewWindow).toHaveBeenCalledWith('s1')
diff --git a/apps/desktop/src/app/open-session.ts b/apps/desktop/src/app/open-session.ts
index a7db7efca3e..94cec331f30 100644
--- a/apps/desktop/src/app/open-session.ts
+++ b/apps/desktop/src/app/open-session.ts
@@ -4,19 +4,28 @@
* already a tile (or the main tab) is JUMPED TO instead of yanked into main.
*
* Intents:
- * - `in-place` (click / Enter) — focus existing tile/main if on screen; else
- * load into main (same as the left sessions sidebar).
+ * - `in-place` (sidebar click / Enter) — focus existing tile/main if on
+ * screen; else load into main (same as the left sessions sidebar).
+ * - `stack` (⌘K, notifications — anything that opens a chat from outside the
+ * workspace) — like `tab`, but may spend main or an open blank draft tab
+ * when either is empty.
* - `tab` (⌘/⌃-click / ⌘-Enter / session refs) — focus if already on screen,
* else open as a stacked session tab (never steals main from under you).
* - `window` (⇧⌘-click) — pop into its own window; falls back to `tab` when
* the bridge has no session-window support.
*/
-import { focusedSessionNeedsRoute, focusOpenSession, openSessionTile } from '@/store/session-states'
+import { $activeSessionId, $selectedStoredSessionId } from '@/store/session'
+import {
+ focusedSessionNeedsRoute,
+ focusOpenSession,
+ openSessionTile,
+ reuseBlankDraftTile
+} from '@/store/session-states'
import { canOpenSessionWindow, openSessionInNewWindow } from '@/store/windows'
import { $workspaceIsPage, sessionRoute } from './routes'
-export type OpenSessionIntent = 'in-place' | 'tab' | 'window'
+export type OpenSessionIntent = 'in-place' | 'stack' | 'tab' | 'window'
export type OpenSessionNavigate = (to: string, options?: { replace?: boolean }) => void
@@ -25,19 +34,22 @@ export type OpenSessionNavigate = (to: string, options?: { replace?: boolean })
*
* A loaded chat may still be mid-turn, so replacing it with something else
* throws away work the user can see. A blank draft has nothing to lose, which
- * is what lets the sidebar "+" take the cheaper main path instead of stacking
- * a tab nobody asked for.
+ * is what lets the sidebar "+" and a `stack` open take the cheaper main path
+ * instead of stacking a tab nobody asked for.
*/
export function mainChatOccupied(activeSessionId: null | string, selectedStoredSessionId: null | string): boolean {
return Boolean(activeSessionId || selectedStoredSessionId)
}
-/** Read modifiers the way session rows do — meta OR ctrl for tab, +shift for window. */
+/** Read modifiers the way session rows do — meta OR ctrl for tab, +shift for
+ * window. `base` is what an unmodified select means for the caller: the
+ * sidebar spends main (`in-place`), a palette-style open doesn't (`stack`). */
export function openSessionIntentFromModifiers(
- event?: null | { ctrlKey?: boolean; metaKey?: boolean; shiftKey?: boolean }
+ event?: null | { ctrlKey?: boolean; metaKey?: boolean; shiftKey?: boolean },
+ base: OpenSessionIntent = 'in-place'
): OpenSessionIntent {
if (!event) {
- return 'in-place'
+ return base
}
const mod = Boolean(event.metaKey || event.ctrlKey)
@@ -50,7 +62,7 @@ export function openSessionIntentFromModifiers(
return 'tab'
}
- return 'in-place'
+ return base
}
/**
@@ -79,6 +91,17 @@ export function openSession(
resolved = 'tab'
}
+ // A `stack` open arrives from outside the workspace, so unlike a sidebar
+ // click it can't assume main is spendable: it behaves like `tab`, except main
+ // IS fair game while it's only a blank draft, and an already-open blank draft
+ // tab is spent before a new one is stacked.
+ let spendBlankDraft = false
+
+ if (resolved === 'stack') {
+ spendBlankDraft = mainChatOccupied($activeSessionId.get(), $selectedStoredSessionId.get())
+ resolved = spendBlankDraft ? 'tab' : 'in-place'
+ }
+
if (resolved === 'tab') {
// Already on screen? Front it. openSessionTile would no-op on main without
// focusing, or try to relocate an existing tile — neither is right for a
@@ -87,6 +110,13 @@ export function openSession(
return
}
+ // Nothing to jump to, but an open tab may still be an empty "New session" —
+ // that's the tab the user would have typed into, so spend it rather than
+ // stacking a second blank one beside it.
+ if (spendBlankDraft && reuseBlankDraftTile(storedSessionId)) {
+ return
+ }
+
openSessionTile(storedSessionId, 'center')
return