Skip to content

The agent that grows with you

fix(desktop): session/terminal linking follows the (host, cwd) pair

Teknium8bf447a7291e
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts
index 1ce01a33999..de4f9b4e6ed 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-ownership.test.ts
@@ -103,4 +103,28 @@ describe('a terminal tab owns the gateway it was opened on', () => {
expect(start.mock.calls[1]![0].connectionId).toBe('nas')
})
+ it('the same literal cwd on another host selects that host’s tab', async () => {
+ const stores = await loadStores()
+ stores.$connectionsRegistry.set(registryWith({ host: 'nas.example', id: 'nas' }))
+ stores.$connection.set({ connectionId: 'nas' } as never)
+ stores.$currentCwd.set('/elsewhere')
+
+ const nas = stores.createTerminal('/repo', { connectionId: 'nas', profile: 'default' })
+ const local = stores.createTerminal('/repo', { connectionId: 'local' })
+ stores.selectTerminal(nas)
+ stores.$currentCwd.set('/repo')
+ expect(stores.$activeTerminalId.get()).toBe(nas)
+
+ stores.$connection.set({ connectionId: 'local' } as never)
+ stores.$currentCwd.set('/repo')
+ expect(stores.$activeTerminalId.get()).toBe(local)
+
+ stores.$connection.set({ connectionId: 'nas' } as never)
+ expect(stores.$activeTerminalId.get()).toBe(nas)
+
+ // A manual pick survives while neither host nor cwd changes.
+ stores.selectTerminal(local)
+ stores.$connectionsRegistry.set(registryWith({ host: 'nas.example', id: 'nas' }))
+ expect(stores.$activeTerminalId.get()).toBe(local)
+ })
})
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 1d10ff86657..ce2ebf3832c 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.test.ts
@@ -259,9 +259,10 @@ describe('session cwd → terminal tab linking', () => {
$currentCwd.set('/repo')
expect($activeTerminalId.get()).toBe(nasTab)
- // An active tab on the wrong host with the same path does not keep focus.
+ // An active tab on the wrong host with the same path does not keep focus,
+ // even though the path string never changes.
$host.set({ connectionId: 'local' })
- $currentCwd.set('/repo/')
+ $currentCwd.set('/repo')
expect($activeTerminalId.get()).toBe(localTab)
$host.set({ connectionId: 'nas' })
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
index 8f426bb69a9..66c0f5af335 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminals.ts
@@ -395,11 +395,19 @@ const terminalCwd = (term: TerminalEntry) => normalizePath(term.restoreCwd || te
//
// 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.
+// one yet) can match. The link follows the (host, cwd) pair, so picking another
+// gateway re-selects even when both hosts report the same path string.
export function bindTerminalDefaultGateway(source: ReadableAtom<TerminalGateway | undefined>): void {
unbindDefaultGateway()
$defaultGateway = source
- unbindDefaultGateway = () => {}
+ let host = source.get()?.connectionId
+
+ unbindDefaultGateway = source.listen(gateway => {
+ if (gateway?.connectionId !== host) {
+ host = gateway?.connectionId
+ followSessionCwd($currentCwd.get())
+ }
+ })
}
function followSessionCwd(cwd: string) {