Skip to content

The agent that grows with you

fix(desktop): match a session's cwd only to terminal tabs on its own host

Teknium2a2b26b20e3a
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts
index 0ce5f22becf..9834d33a753 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-gateway.ts
@@ -5,7 +5,7 @@ import type { DesktopConnectionsRegistry, DesktopRegistryConnection } from '@/gl
import { $activeConnectionId, $connectionsRegistry } from '@/store/connections'
import { $activeGatewayProfile, normalizeProfileKey } from '@/store/profile'
-import type { TerminalEntry } from './terminals'
+import { bindTerminalSessionHost, type TerminalEntry } from './terminals'
/** The gateway a terminal tab was opened on. Only This device and SSH gateways
* carry a shell; the SSH tunnel is keyed by (connection, profile). */
@@ -43,6 +43,9 @@ export const defaultTerminalGateway = () =>
export const runsOverSsh = (gateway: TerminalGateway | undefined): boolean =>
Boolean(gateway && gateway.connectionId !== LOCAL_CONNECTION_ID)
+// Entering a session re-selects a tab in its cwd only on the session's host.
+bindTerminalSessionHost(() => defaultTerminalGateway()?.connectionId)
+
export const terminalGatewayOf = (term: TerminalEntry | undefined): TerminalGateway | undefined =>
term?.connectionId ? { connectionId: term.connectionId, ...(term.profile ? { profile: term.profile } : {}) } : undefined
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
index c1e97139baf..e654241d2fa 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
@@ -228,6 +228,29 @@ describe('session cwd → terminal tab linking', () => {
$currentCwd.set('/repo')
expect($activeTerminalId.get()).toBe(first)
})
+
+ it("selects the session's own host's tab when two hosts share the path", async () => {
+ const { $activeTerminalId, $currentCwd, bindTerminalSessionHost, createTerminal } = await loadTerminalStore()
+ let host = 'nas'
+ bindTerminalSessionHost(() => host)
+
+ const nasTab = createTerminal('/repo', { connectionId: 'nas', profile: 'default' })
+ const localTab = createTerminal('/repo', { connectionId: 'local' })
+ createTerminal('/elsewhere', { connectionId: 'local' })
+
+ // The NAS session's /repo is the NAS tab, never This device's /repo.
+ $currentCwd.set('/repo')
+ expect($activeTerminalId.get()).toBe(nasTab)
+
+ // An active tab on the wrong host with the same path does not keep focus.
+ host = 'local'
+ $currentCwd.set('/repo/')
+ expect($activeTerminalId.get()).toBe(localTab)
+
+ host = 'nas'
+ $currentCwd.set('/repo')
+ expect($activeTerminalId.get()).toBe(nasTab)
+ })
})
describe('shared WebGL atlas refresh fan-out', () => {
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
index 07cc7ffc0a1..f64ad41f94a 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
@@ -383,6 +383,21 @@ const terminalCwd = (term: TerminalEntry) => normalizePath(term.restoreCwd || te
// 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.
+//
+// A path names a directory on one host: `/repo` on the NAS is not `/repo` here,
+// so only tabs on the session's gateway (or a fresh tab that has not recorded
+// one yet) can match.
+type SessionShellHost = () => string | undefined
+
+// The registry-backed resolver lives in terminal-gateway.ts, which binds it on
+// load; this module stays free of the connection stores. Until then (or with no
+// registry, like the Webapp) there is one host and the path alone decides.
+let sessionShellHost: SessionShellHost = () => undefined
+
+export function bindTerminalSessionHost(resolve: SessionShellHost): void {
+ sessionShellHost = resolve
+}
+
$currentCwd.listen(cwd => {
const target = normalizePath(cwd)
@@ -390,14 +405,20 @@ $currentCwd.listen(cwd => {
return
}
+ const host = sessionShellHost()
+
+ const linked = (term: TerminalEntry | undefined) =>
+ term?.kind === 'user' &&
+ terminalCwd(term) === target &&
+ (!host || !term.connectionId || term.connectionId === host)
+
const list = $terminals.get()
- const active = list.find(term => term.id === $activeTerminalId.get())
- if (active?.kind === 'user' && terminalCwd(active) === target) {
+ if (linked(list.find(term => term.id === $activeTerminalId.get()))) {
return
}
- const match = list.find(term => term.kind === 'user' && terminalCwd(term) === target)
+ const match = list.find(linked)
if (match) {
$activeTerminalId.set(match.id)