Skip to content

The agent that grows with you

fix(desktop): retain chat pane ownership through preview focus

Vago776488e03bfa
diff --git a/apps/desktop/src/app/chat/preview-session-focus.test.ts b/apps/desktop/src/app/chat/preview-session-focus.test.ts
new file mode 100644
index 00000000000..df5002c680e
--- /dev/null
+++ b/apps/desktop/src/app/chat/preview-session-focus.test.ts
@@ -0,0 +1,100 @@
+import { afterEach, beforeAll, expect, it, vi } from 'vitest'
+
+vi.mock('./right-rail/preview', () => ({ PreviewTilePane: () => null }))
+vi.mock('./right-rail/preview-console-store', () => ({ forgetPreviewConsole: () => undefined }))
+
+import '@/store/session-states'
+
+import { findGroupOfPane, group } from '@/components/pane-shell/tree/model'
+import {
+ $layoutTree,
+ activateTreePane,
+ declareDefaultTree,
+ noteActiveTreeGroup,
+ revealTreePane,
+ watchContributedPanes
+} from '@/components/pane-shell/tree/store'
+import { registry } from '@/contrib/registry'
+import { $previewTabs, $visiblePreviewTabs, openPreview } from '@/store/preview'
+import { $selectedStoredSessionId } from '@/store/session'
+import { $focusedStoredSessionId } from '@/store/session-focus'
+import { $sessionTiles } from '@/store/session-states'
+
+import { paneMirror } from './pane-mirror'
+import { watchPreviewTiles } from './preview-tile'
+
+beforeAll(() => {
+ watchContributedPanes()
+ paneMirror({
+ source: $sessionTiles,
+ key: tile => tile.storedSessionId,
+ prefix: 'session-tile',
+ title: id => id,
+ minWidth: '22vw',
+ dir: () => 'center',
+ anchor: () => 'workspace',
+ render: () => null,
+ close: () => undefined
+ })()
+ watchPreviewTiles()
+})
+
+afterEach(() => {
+ noteActiveTreeGroup(null)
+ $previewTabs.set([])
+ $sessionTiles.set([])
+ $layoutTree.set(null)
+ $selectedStoredSessionId.set(null)
+})
+
+it('settles session-owned preview reveals in the same group without losing chat ownership', () => {
+ registry.register({
+ area: 'panes',
+ data: { placement: 'main', uncloseable: true },
+ id: 'workspace',
+ render: () => null,
+ title: 'workspace'
+ })
+ declareDefaultTree(group(['workspace'], { active: 'workspace', id: 'main' }))
+ $selectedStoredSessionId.set('primary')
+ $sessionTiles.set([{ storedSessionId: 'tile-a' }, { storedSessionId: 'tile-b' }])
+
+ let focusChanges = 0
+
+ const stop = $focusedStoredSessionId.listen(() => {
+ if (++focusChanges > 20) {
+ throw new Error('Session/preview focus did not settle')
+ }
+ })
+
+ try {
+ revealTreePane('session-tile:tile-a')
+ noteActiveTreeGroup('main')
+ openPreview({ kind: 'url', label: 'Browser', source: 'about:blank', url: 'about:blank' })
+ const tabId = $previewTabs.get()[0]!.id
+ $layoutTree.set(
+ group(['workspace', 'session-tile:tile-a', 'session-tile:tile-b', `preview-tile:${tabId}`], {
+ active: 'session-tile:tile-a',
+ id: 'main'
+ })
+ )
+ openPreview({ kind: 'url', label: 'Browser', source: 'about:blank', url: 'about:blank' })
+ expect($focusedStoredSessionId.get()).toBe('tile-a')
+ expect($visiblePreviewTabs.get().map(tab => tab.id)).toContain(tabId)
+
+ const tree = $layoutTree.get()!
+ expect(findGroupOfPane(tree, `preview-tile:${tabId}`)?.id).toBe('main')
+ expect(findGroupOfPane(tree, `preview-tile:${tabId}`)?.active).toBe(`preview-tile:${tabId}`)
+
+ for (let index = 0; index < 3; index++) {
+ activateTreePane('main', 'session-tile:tile-b')
+ expect($focusedStoredSessionId.get()).toBe('tile-b')
+ expect($visiblePreviewTabs.get()).toEqual([])
+ activateTreePane('main', 'session-tile:tile-a')
+ expect($focusedStoredSessionId.get()).toBe('tile-a')
+ expect($visiblePreviewTabs.get().map(tab => tab.id)).toContain(tabId)
+ }
+ } finally {
+ stop()
+ }
+})
diff --git a/apps/desktop/src/store/session-focus.ts b/apps/desktop/src/store/session-focus.ts
index e558ad0a891..9454b9b04f9 100644
--- a/apps/desktop/src/store/session-focus.ts
+++ b/apps/desktop/src/store/session-focus.ts
@@ -14,25 +14,32 @@ const isChatPane = (paneId?: string): boolean => paneId === 'workspace' || Boole
// list, browsing Files, typing in Terminal) must not replace the chat being
// worked in with the route's (possibly hidden) primary — the Files rail and
// statusbar follow this chat, so they would jump projects mid-click.
-const $lastContentGroup = atom<null | string>(null)
+// Remember the pane: a preview can replace its group's active chat tab.
+const $lastContentPane = atom<null | string>(null)
-$activeTreeGroup.subscribe(groupId => {
+const rememberContentPane = () => {
+ const groupId = $activeTreeGroup.get()
const tree = $layoutTree.get()
const active = groupId && tree ? findGroup(tree, groupId)?.active : undefined
if (!groupId || isChatPane(active)) {
- $lastContentGroup.set(groupId)
+ $lastContentPane.set(active ?? null)
}
-})
+}
+
+$activeTreeGroup.subscribe(rememberContentPane)
+$layoutTree.listen(rememberContentPane)
export const $focusedTreePaneId = computed(
- [$activeTreeGroup, $layoutTree, $workspaceMode, $lastContentGroup],
- (groupId, tree, workspaceMode, lastContentGroup) => {
+ [$activeTreeGroup, $layoutTree, $workspaceMode, $lastContentPane],
+ (groupId, tree, workspaceMode, lastContentPane) => {
let active = groupId && tree ? findGroup(tree, groupId)?.active : undefined
if (groupId && tree && !isChatPane(active)) {
- const content = lastContentGroup ? findGroup(tree, lastContentGroup) : null
- active = (content ?? findGroupOfPane(tree, 'workspace'))?.active
+ active =
+ lastContentPane && findGroupOfPane(tree, lastContentPane)
+ ? lastContentPane
+ : findGroupOfPane(tree, 'workspace')?.active
}
if (active?.startsWith('session-tile:')) {
diff --git a/apps/desktop/src/store/session-sidebar-focus.test.ts b/apps/desktop/src/store/session-sidebar-focus.test.ts
index 3b1e3129b93..497f8248235 100644
--- a/apps/desktop/src/store/session-sidebar-focus.test.ts
+++ b/apps/desktop/src/store/session-sidebar-focus.test.ts
@@ -82,6 +82,33 @@ describe('session focus while interacting with the sidebar', () => {
expect($focusedStoredSessionId.get()).toBe('split')
})
+ it('retains the chat when a preview replaces its active tab in the same group', () => {
+ $layoutTree.set(
+ group(['workspace', pane('a'), pane('b'), 'preview-tile:file:test'], { active: pane('a'), id: 'main' })
+ )
+ target('main').focus()
+ expect($focusedStoredSessionId.get()).toBe('a')
+
+ $layoutTree.set(
+ group(['workspace', pane('a'), pane('b'), 'preview-tile:file:test'], {
+ active: 'preview-tile:file:test',
+ id: 'main'
+ })
+ )
+ expect($focusedStoredSessionId.get()).toBe('a')
+
+ $layoutTree.set(
+ group(['workspace', pane('a'), pane('b'), 'preview-tile:file:test'], { active: pane('b'), id: 'main' })
+ )
+ $layoutTree.set(
+ group(['workspace', pane('a'), pane('b'), 'preview-tile:file:test'], {
+ active: 'preview-tile:file:test',
+ id: 'main'
+ })
+ )
+ expect($focusedStoredSessionId.get()).toBe('b')
+ })
+
it('uses the visible main tab on restore and after the remembered split closes', () => {
const sidebar = target('sidebar')
sidebar.focus()