Skip to content

The agent that grows with you

fix(desktop): retain focused tile cwd when clicking sidebar chrome and expose $focusedWorkspaceCwd

Mohammad2cfb32447a32
diff --git a/apps/desktop/src/app/contrib/panes.tsx b/apps/desktop/src/app/contrib/panes.tsx
index c4a737aa675..8a6c40b485b 100644
--- a/apps/desktop/src/app/contrib/panes.tsx
+++ b/apps/desktop/src/app/contrib/panes.tsx
@@ -26,7 +26,7 @@ import { getLogs } from '@/hermes'
import { normalizeOrLocalPreviewTarget } from '@/lib/local-preview'
import { cn } from '@/lib/utils'
import { openPreview } from '@/store/preview'
-import { $currentCwd } from '@/store/session'
+import { $focusedWorkspaceCwd } from '@/store/session-states'
// ---------------------------------------------------------------------------
// Logs — live agent-log tail. ⌘K-only chrome: the pane contribution exists
@@ -72,7 +72,7 @@ export const $restartPreviewServer = atom<((url: string, context?: string) => Pr
/** Open a file from the tree in the real preview pipeline. */
function previewFile(path: string) {
- void normalizeOrLocalPreviewTarget(path, $currentCwd.get() || undefined)
+ void normalizeOrLocalPreviewTarget(path, $focusedWorkspaceCwd.get() || undefined)
.then(target => {
if (target) {
openPreview(target)
@@ -99,7 +99,7 @@ export function FilesPane() {
// ---------------------------------------------------------------------------
export function ReviewPaneContent() {
- const cwd = useStore($currentCwd)
+ const cwd = useStore($focusedWorkspaceCwd)
// Keyed by cwd like DesktopController so switching projects rebuilds the
// diff state instead of showing the previous repo's files.
diff --git a/apps/desktop/src/app/right-sidebar/index.test.tsx b/apps/desktop/src/app/right-sidebar/index.test.tsx
index 2485f8981fd..fd0484694ce 100644
--- a/apps/desktop/src/app/right-sidebar/index.test.tsx
+++ b/apps/desktop/src/app/right-sidebar/index.test.tsx
@@ -98,4 +98,32 @@ describe('RightSidebarPane', () => {
fireEvent.click(refresh)
await waitFor(() => expect(readDir).toHaveBeenCalledWith('/repo-tile'))
})
+
+ it('keeps the focused tile workspace cwd even when clicking into the sidebar group', async () => {
+ $selectedStoredSessionId.set('main-session')
+ $workspaceCwdOwner.set('main-session')
+ setCurrentCwd('/repo-main')
+
+ $sessions.set([
+ { cwd: '/repo-tile', id: 'tile-session' } as any
+ ])
+ $sessionTiles.set([
+ { storedSessionId: 'tile-session', runtimeId: 'rt-tile', workspaceMode: 'sessions' } as any
+ ])
+ $layoutTree.set({
+ id: 'grp-main',
+ type: 'group',
+ panes: ['workspace', 'session-tile:tile-session'],
+ active: 'session-tile:tile-session'
+ } as any)
+ // User clicks into the files sidebar group:
+ $activeTreeGroup.set('grp-files')
+
+ render(<RightSidebarPane onActivateFile={vi.fn()} onActivateFolder={vi.fn()} />)
+
+ const refresh = await screen.findByRole('button', { name: 'Refresh tree' })
+ readDir.mockClear()
+ fireEvent.click(refresh)
+ await waitFor(() => expect(readDir).toHaveBeenCalledWith('/repo-tile'))
+ })
})
diff --git a/apps/desktop/src/app/right-sidebar/index.tsx b/apps/desktop/src/app/right-sidebar/index.tsx
index 76ec2720bd9..4822ed25dc0 100644
--- a/apps/desktop/src/app/right-sidebar/index.tsx
+++ b/apps/desktop/src/app/right-sidebar/index.tsx
@@ -14,8 +14,7 @@ import { $panesFlipped } from '@/store/layout'
import { notifyError } from '@/store/notifications'
import { openPreview } from '@/store/preview'
import { openFolderAsProject } from '@/store/projects'
-import { $currentCwd, $selectedStoredSessionId, $sessions, $workspaceCwdOwner, sessionMatchesStoredId } from '@/store/session'
-import { $focusedSessionState, $focusedStoredSessionId } from '@/store/session-states'
+import { $focusedWorkspaceCwd } from '@/store/session-states'
import { SidebarPanelLabel } from '../shell/sidebar-label'
@@ -31,29 +30,8 @@ export function RightSidebarPane({ onActivateFile, onActivateFolder }: RightSide
const { t } = useI18n()
const r = t.rightSidebar
const panesFlipped = useStore($panesFlipped)
- const currentCwd = useStore($currentCwd).trim()
- const selectedStoredSessionId = useStore($selectedStoredSessionId)
- const workspaceCwdOwner = useStore($workspaceCwdOwner)
- const focusedStoredSessionId = useStore($focusedStoredSessionId)
- const focusedSessionState = useStore($focusedSessionState)
- const sessions = useStore($sessions)
-
- // When a tile tab is focused, use the focused tile's workspace cwd;
- // otherwise fall back to the primary session's cwd.
- const isTileFocused = Boolean(focusedStoredSessionId && focusedStoredSessionId !== selectedStoredSessionId)
- const tileCwd =
- isTileFocused && focusedStoredSessionId
- ? (
- focusedSessionState?.cwd ||
- sessions.find(s => sessionMatchesStoredId(s, focusedStoredSessionId))?.cwd ||
- ''
- ).trim()
- : ''
-
- const targetCwd = isTileFocused ? tileCwd : currentCwd
- const hasWorkspace = isTileFocused
- ? Boolean(tileCwd)
- : Boolean(currentCwd) && (workspaceCwdOwner ?? null) === (selectedStoredSessionId ?? null)
+ const targetCwd = useStore($focusedWorkspaceCwd)
+ const hasWorkspace = Boolean(targetCwd)
const {
collapseAll,
diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.ts
index 56d03d4eaff..b3d457edc72 100644
--- a/apps/desktop/src/store/session-states.test.ts
+++ b/apps/desktop/src/store/session-states.test.ts
@@ -25,6 +25,7 @@ import type { SessionTile } from '@/store/session-states'
import type * as SessionStatesModule from '@/store/session-states'
import {
$focusedStoredSessionId,
+ $focusedWorkspaceCwd,
$sessionStates,
$sessionTiles,
blankDraftTile,
@@ -1224,7 +1225,7 @@ describe('$focusedStoredSessionId in Bot Mode (#96062)', () => {
expect($focusedStoredSessionId.get()).toBeNull()
})
- it('sessions-sidebar focus follows the visible main tab instead of a hidden primary selection', () => {
+ it('sessions mode retains the active main-zone tile when the tracker sits on side chrome', () => {
$selectedStoredSessionId.set('primary-1')
$layoutTree.set(
split('row', [
@@ -1237,6 +1238,27 @@ describe('$focusedStoredSessionId in Bot Mode (#96062)', () => {
expect($workspaceMode.get()).toBe('sessions')
expect($focusedStoredSessionId.get()).toBe('stacked')
})
+
+ it('computes $focusedWorkspaceCwd from the focused tile session state or sessions list', () => {
+ $selectedStoredSessionId.set('primary-1')
+ setSessions([{ cwd: '/repo-stacked', id: 'stacked' } as any])
+ $sessionTiles.set([
+ { storedSessionId: 'stacked', runtimeId: 'rt-stacked', workspaceMode: 'sessions' } as any
+ ])
+ $sessionStates.set({
+ 'rt-stacked': { cwd: '/repo-stacked' } as any
+ })
+ $layoutTree.set(
+ split('row', [
+ group(['files'], { active: 'files', id: 'grp-files' }),
+ group(['workspace', tilePane('stacked')], { active: tilePane('stacked'), id: 'grp-main' })
+ ])
+ )
+ noteActiveTreeGroup('grp-files')
+
+ expect($focusedStoredSessionId.get()).toBe('stacked')
+ expect($focusedWorkspaceCwd.get()).toBe('/repo-stacked')
+ })
})
describe('focusedSessionNeedsRoute', () => {
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index 80fc8522c4b..754da6a7bf9 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -22,7 +22,7 @@ import { atom, computed } from 'nanostores'
import { setSessionOwnerResolver } from '@/api/client'
import { routeSessionId } from '@/app/routes'
import type { ClientSessionState } from '@/app/types'
-import { findGroupOfPane, type LayoutNode } from '@/components/pane-shell/tree/model'
+import { findGroup, findGroupOfPane, type LayoutNode } from '@/components/pane-shell/tree/model'
import {
$layoutTree,
focusedSessionTabAnchor,
@@ -51,9 +51,11 @@ import { clearAllProviderWaits, clearSessionProviderWait } from './provider-wait
import {
$activeSessionId,
$connection,
+ $currentCwd,
$lastReadAtBySessionId,
$selectedStoredSessionId,
$sessions,
+ $workspaceCwdOwner,
clearReadBaseline,
getSessionOwnerHint,
knownSessionOwner,
@@ -3021,8 +3023,29 @@ export const $focusedSessionIsTile = computed($focusedTreePaneId, active =>
Boolean(active?.startsWith(TILE_PANE_PREFIX))
)
-export const $focusedStoredSessionId = computed([$focusedTreePaneId, $selectedStoredSessionId], (active, selected) =>
- active?.startsWith(TILE_PANE_PREFIX) ? active.slice(TILE_PANE_PREFIX.length) : selected
+export const $focusedStoredSessionId = computed(
+ [$activeTreeGroup, $layoutTree, $selectedStoredSessionId],
+ (groupId, tree, selected) => {
+ const active = groupId && tree ? findGroup(tree, groupId)?.active : undefined
+
+ if (active?.startsWith(TILE_PANE_PREFIX)) {
+ return active.slice(TILE_PANE_PREFIX.length)
+ }
+
+ // The interaction tracker can point at sidebar or tool CHROME (files,
+ // terminal, sessions list, bots roster) while a chat still holds the main
+ // zone's active tab. In both sessions and Bot Mode, the main zone's active
+ // tile answers so clicks in side chrome do not drop the on-screen session.
+ if (tree) {
+ const mainActive = findGroupOfPane(tree, 'workspace')?.active
+
+ if (mainActive?.startsWith(TILE_PANE_PREFIX)) {
+ return mainActive.slice(TILE_PANE_PREFIX.length)
+ }
+ }
+
+ return selected
+ }
)
/** Every session currently OPEN as a surface: the primary's selection plus
@@ -3052,6 +3075,36 @@ export const $focusedSessionState = computed([$focusedRuntimeId, $sessionStates]
runtimeId ? states[runtimeId] : undefined
)
+/** The workspace CWD of the currently focused session (the focused tile's cwd,
+ * else the primary session's confirmed workspace cwd). */
+export const $focusedWorkspaceCwd = computed(
+ [
+ $focusedStoredSessionId,
+ $selectedStoredSessionId,
+ $focusedSessionState,
+ $sessions,
+ $currentCwd,
+ $workspaceCwdOwner
+ ],
+ (focusedStoredId, selectedStoredId, focusedSessionState, sessions: readonly SessionInfo[], currentCwd, workspaceCwdOwner) => {
+ const isTile = Boolean(focusedStoredId && focusedStoredId !== selectedStoredId)
+
+ if (isTile && focusedStoredId) {
+ const tileCwd = (
+ focusedSessionState?.cwd ||
+ sessions.find(s => sessionMatchesStoredId(s, focusedStoredId))?.cwd ||
+ ''
+ ).trim()
+
+ return tileCwd
+ }
+
+ const hasPrimaryWorkspace = Boolean(currentCwd) && (workspaceCwdOwner ?? null) === (selectedStoredId ?? null)
+
+ return hasPrimaryWorkspace ? currentCwd.trim() : ''
+ }
+)
+
/** A PRIMARY navigation (sidebar resume, route change, new chat) homes focus to
* the workspace — UNLESS the selected id is already an open TILE, where
* `focusOpenSession` owns the move and homing would yank every stacked tile