The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(desktop): switching sessions re-selects the terminal tab in its cwd
Brooklyn Nicholsonc450fb931d46
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.tsindex eb99e7a7821..9a35fcc926d 100644--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts@@ -4,11 +4,13 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'const STORAGE_KEY = 'hermes.desktop.terminals.v1'async function loadTerminalStore() {+ const $currentCwd = atom('/workspace')+vi.doMock('@/store/session', () => ({- $currentCwd: atom('/workspace')+ $currentCwd}))- return import('./terminals')+ return { ...(await import('./terminals')), $currentCwd }}describe('terminal store persistence', () => {@@ -121,3 +123,68 @@ describe('terminal store persistence', () => {expect($terminals.get().find(term => term.id === userId)?.restoreCwd).toBeUndefined()})})++describe('session cwd → terminal tab linking', () => {+ beforeEach(() => {+ window.localStorage.clear()+ vi.resetModules()+ })++ it('re-selects the tab already pointed at the new session cwd (trailing slash tolerated)', async () => {+ const { $activeTerminalId, $currentCwd, createTerminal } = await loadTerminalStore()++ const repoTab = createTerminal('/repo')+ const otherTab = createTerminal('/elsewhere')+ expect($activeTerminalId.get()).toBe(otherTab)++ $currentCwd.set('/repo/')+ expect($activeTerminalId.get()).toBe(repoTab)+ })++ it('matches the live shell cwd (restoreCwd) over the launch dir', async () => {+ const { $activeTerminalId, $currentCwd, createTerminal, updateTerminalRestoreCwd } = await loadTerminalStore()++ const movedTab = createTerminal('/repo')+ updateTerminalRestoreCwd(movedTab, '/repo/packages/api')+ const otherTab = createTerminal('/elsewhere')+ expect($activeTerminalId.get()).toBe(otherTab)++ $currentCwd.set('/repo/packages/api')+ expect($activeTerminalId.get()).toBe(movedTab)++ // The launch dir no longer describes where that shell lives.+ $currentCwd.set('/repo')+ expect($activeTerminalId.get()).toBe(movedTab)+ })++ it('leaves the active tab alone when no tab lives in the session cwd or the cwd is empty', async () => {+ const { $activeTerminalId, $currentCwd, createTerminal } = await loadTerminalStore()++ createTerminal('/repo')+ const activeTab = createTerminal('/elsewhere')++ $currentCwd.set('/unrelated')+ expect($activeTerminalId.get()).toBe(activeTab)++ $currentCwd.set('')+ expect($activeTerminalId.get()).toBe(activeTab)+ })++ it('stays put when the active tab already lives in the target cwd, and never matches agent tabs', async () => {+ const { $activeTerminalId, $currentCwd, createTerminal, ensureAgentTerminal, selectTerminal } =+ await loadTerminalStore()++ const first = createTerminal('/repo')+ const second = createTerminal('/repo')+ ensureAgentTerminal('proc-1', 'background task')+ selectTerminal(second)++ // Both tabs match; the one already active keeps focus (no first-match steal).+ $currentCwd.set('/repo')+ expect($activeTerminalId.get()).toBe(second)++ selectTerminal(first)+ $currentCwd.set('/repo')+ expect($activeTerminalId.get()).toBe(first)+ })+})diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.tsindex c86f57aeeae..19b560f49d1 100644--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts@@ -17,7 +17,8 @@ export interface TerminalEntry {/** Working directory, snapshotted once at creation. Terminals live outside* session/project state — the only thing they inherit is this initial cwd* (the project root if opened in one, else the backend's default). Switching- * sessions never moves or recreates a terminal. */+ * sessions never moves or recreates a terminal; at most it re-SELECTS a tab+ * already pointed at the session's cwd (see the $currentCwd listener). */cwd: string/** Last observed working directory of the live shell (tracked via the PTY* cwd probe / OSC 7). Used to reopen the tab where the user last `cd`'d@@ -223,6 +224,45 @@ export function selectTerminal(id: string): void {}}+// Compare-ready form of a directory path: trimmed, trailing separators dropped+// (keeping a bare root intact) so `/repo/` and `/repo` are the same place.+const normalizePath = (value: string) => {+ const trimmed = value.trim()++ return trimmed.length > 1 ? trimmed.replace(/[\\/]+$/, '') || trimmed : trimmed+}++/** The directory a tab points at right now — the live shell cwd once observed+ * (survives a `cd`), else the launch dir. */+const terminalCwd = (term: TerminalEntry) => normalizePath(term.restoreCwd || term.cwd)++// Session ↔ terminal linking. Entering a session whose cwd already has a user+// terminal pointed at it re-selects that tab, so the terminal pane follows the+// workspace you're in. Selection ONLY — it never creates a shell, never closes+// one, and never reveals the pane; a detached session (empty cwd) or a cwd no+// tab lives in leaves the tabs exactly where they were. `listen` (not+// `subscribe`) so boot keeps the persisted active tab.+$currentCwd.listen(cwd => {+ const target = normalizePath(cwd)++ if (!target) {+ return+ }++ const list = $terminals.get()+ const active = list.find(term => term.id === $activeTerminalId.get())++ if (active?.kind === 'user' && terminalCwd(active) === target) {+ return+ }++ const match = list.find(term => term.kind === 'user' && terminalCwd(term) === target)++ if (match) {+ $activeTerminalId.set(match.id)+ }+})+/** Move the active tab by `direction` (+1 next / -1 prev), wrapping around. */export function cycleTerminal(direction: 1 | -1): void {const list = $terminals.get()