From d38d240e86caeef213bb65b81b8f52f033344c66 Mon Sep 17 00:00:00 2001 From: Jinwoo Hong <73622457+Jinwoo-H@users.noreply.github.com> Date: Wed, 8 Apr 2026 18:18:42 -0400 Subject: [PATCH] feat: persist last-active terminal tab per worktree (#392) When switching between worktrees, the active terminal tab was reset to the first tab because activeTabId was global. This adds a per-worktree map (activeTabIdByWorktree) mirroring the existing pattern used for editor files, so users return to the same terminal tab they left. --- src/renderer/src/App.tsx | 6 ++- src/renderer/src/store/slices/terminals.ts | 51 +++++++++++++++++++++- src/renderer/src/store/slices/worktrees.ts | 10 +++++ src/shared/types.ts | 2 + 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index 2744fcc54..c9e8df6e1 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -114,6 +114,7 @@ function App(): React.JSX.Element { const openFiles = useAppStore((s) => s.openFiles) const activeFileIdByWorktree = useAppStore((s) => s.activeFileIdByWorktree) const activeTabTypeByWorktree = useAppStore((s) => s.activeTabTypeByWorktree) + const activeTabIdByWorktree = useAppStore((s) => s.activeTabIdByWorktree) // Right sidebar + editor state const toggleRightSidebar = useAppStore((s) => s.toggleRightSidebar) @@ -240,6 +241,7 @@ function App(): React.JSX.Element { tabsByWorktree, terminalLayoutsByTabId, activeWorktreeIdsOnShutdown, + activeTabIdByWorktree, ...buildEditorSessionData(openFiles, activeFileIdByWorktree, activeTabTypeByWorktree) }) }, 150) @@ -254,7 +256,8 @@ function App(): React.JSX.Element { terminalLayoutsByTabId, openFiles, activeFileIdByWorktree, - activeTabTypeByWorktree + activeTabTypeByWorktree, + activeTabIdByWorktree ]) // On shutdown, capture terminal scrollback buffers and flush to disk. @@ -282,6 +285,7 @@ function App(): React.JSX.Element { tabsByWorktree: state.tabsByWorktree, terminalLayoutsByTabId: state.terminalLayoutsByTabId, activeWorktreeIdsOnShutdown, + activeTabIdByWorktree: state.activeTabIdByWorktree, ...buildEditorSessionData( state.openFiles, state.activeFileIdByWorktree, diff --git a/src/renderer/src/store/slices/terminals.ts b/src/renderer/src/store/slices/terminals.ts index 38c931257..0ebf644ef 100644 --- a/src/renderer/src/store/slices/terminals.ts +++ b/src/renderer/src/store/slices/terminals.ts @@ -17,6 +17,9 @@ import { export type TerminalSlice = { tabsByWorktree: Record activeTabId: string | null + /** Per-worktree last-active terminal tab — restored on worktree switch so + * the user returns to the same tab they left, not always tabs[0]. */ + activeTabIdByWorktree: Record ptyIdsByTabId: Record suppressedPtyExitIds: Record expandedPaneByTabId: Record @@ -64,6 +67,7 @@ export type TerminalSlice = { export const createTerminalSlice: StateCreator = (set, get) => ({ tabsByWorktree: {}, activeTabId: null, + activeTabIdByWorktree: {}, ptyIdsByTabId: {}, suppressedPtyExitIds: {}, expandedPaneByTabId: {}, @@ -150,6 +154,7 @@ export const createTerminalSlice: StateCreator [worktreeId]: [...existing, tab] }, activeTabId: tab.id, + activeTabIdByWorktree: { ...s.activeTabIdByWorktree, [worktreeId]: tab.id }, ptyIdsByTabId: { ...s.ptyIdsByTabId, [tab.id]: [] }, terminalLayoutsByTabId: { ...s.terminalLayoutsByTabId, [tab.id]: emptyLayoutSnapshot() } } @@ -185,9 +190,20 @@ export const createTerminalSlice: StateCreator delete nextCacheTimer[key] } } + // Why: keep activeTabIdByWorktree in sync when a tab is closed in a + // background worktree. Without this, the remembered tab becomes stale + // and restoring it on worktree switch falls back to tabs[0]. + const nextActiveTabIdByWorktree = { ...s.activeTabIdByWorktree } + for (const [wId, tabs] of Object.entries(next)) { + if (nextActiveTabIdByWorktree[wId] === tabId) { + nextActiveTabIdByWorktree[wId] = tabs[0]?.id ?? null + } + } + return { tabsByWorktree: next, activeTabId: s.activeTabId === tabId ? null : s.activeTabId, + activeTabIdByWorktree: nextActiveTabIdByWorktree, ptyIdsByTabId: nextPtyIdsByTabId, expandedPaneByTabId: nextExpanded, canExpandPaneByTabId: nextCanExpand, @@ -244,7 +260,16 @@ export const createTerminalSlice: StateCreator }) }, - setActiveTab: (tabId) => set({ activeTabId: tabId }), + setActiveTab: (tabId) => + set((s) => { + const worktreeId = s.activeWorktreeId + return { + activeTabId: tabId, + activeTabIdByWorktree: worktreeId + ? { ...s.activeTabIdByWorktree, [worktreeId]: tabId } + : s.activeTabIdByWorktree + } + }), updateTabTitle: (tabId, title) => { set((s) => { @@ -531,10 +556,34 @@ export const createTerminalSlice: StateCreator } } + // Why: restore per-worktree active terminal tab from session. + // If the session has the map, validate that each tab ID still exists. + // Otherwise, derive it: the active worktree gets activeTabId, others + // default to their first tab. + let activeTabIdByWorktree: Record = {} + if (session.activeTabIdByWorktree) { + for (const [wId, tabId] of Object.entries(session.activeTabIdByWorktree)) { + if (validWorktreeIds.has(wId) && tabId && validTabIds.has(tabId)) { + activeTabIdByWorktree[wId] = tabId + } + } + } else { + // Legacy sessions: best-effort derivation + if (activeWorktreeId && activeTabId) { + activeTabIdByWorktree[activeWorktreeId] = activeTabId + } + for (const [wId, tabs] of Object.entries(tabsByWorktree)) { + if (!activeTabIdByWorktree[wId] && tabs.length > 0) { + activeTabIdByWorktree[wId] = tabs[0].id + } + } + } + return { activeRepoId, activeWorktreeId, activeTabId, + activeTabIdByWorktree, tabsByWorktree, pendingReconnectWorktreeIds, pendingReconnectTabByWorktree, diff --git a/src/renderer/src/store/slices/worktrees.ts b/src/renderer/src/store/slices/worktrees.ts index d38f36124..9b2da9c43 100644 --- a/src/renderer/src/store/slices/worktrees.ts +++ b/src/renderer/src/store/slices/worktrees.ts @@ -301,10 +301,20 @@ export const createWorktreeSlice: StateCreator activeTabType = fallbackFile ? 'editor' : 'terminal' } + // Why: restore the last-active terminal tab for this worktree so the + // user returns to the same tab they left, not always the first one. + const restoredTabId = s.activeTabIdByWorktree[worktreeId] ?? null + const worktreeTabs = s.tabsByWorktree[worktreeId] ?? [] + const tabStillExists = restoredTabId + ? worktreeTabs.some((t) => t.id === restoredTabId) + : false + const activeTabId = tabStillExists ? restoredTabId : (worktreeTabs[0]?.id ?? null) + return { activeWorktreeId: worktreeId, activeFileId, activeTabType, + activeTabId, worktreesByRepo: applyWorktreeUpdates( s.worktreesByRepo, worktreeId, diff --git a/src/shared/types.ts b/src/shared/types.ts index 7a77a5b12..4af4d85d4 100644 --- a/src/shared/types.ts +++ b/src/shared/types.ts @@ -149,6 +149,8 @@ export type WorkspaceSessionState = { activeFileIdByWorktree?: Record /** Per-worktree active tab type (terminal vs editor) at shutdown. */ activeTabTypeByWorktree?: Record + /** Per-worktree last-active terminal tab ID at shutdown. */ + activeTabIdByWorktree?: Record /** Unified tab model — present when saved by a build that includes TabsSlice. * Read-path checks for this first; falls back to legacy fields if absent. */ unifiedTabs?: Record