Skip to content

The agent that grows with you

fix(desktop): new terminal follows Sessions gateway on live switch

janviernine931207328459
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 5d5a921b9f8..63841621227 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -9502,6 +9502,37 @@ function activeSshTerminalTarget(webContentsId?: number) {
return state && state.ssh ? { ssh: state.ssh, scope } : 'pending'
}
+// Sessions-active gateway → SSH tunnel for a NEW terminal tab. Same lookup as
+// the window-route path, but keyed by the (connectionId, profile) the renderer
+// was looking at when it spawned the tab — a Sessions switch doesn't re-point
+// the window-backend route, so the window-route lookup alone keeps spawning a
+// local shell after a live switch back to SSH.
+function resolveSshScopeForConnectionId(connectionId: string, profile?: string) {
+ const id = String(connectionId || '').trim()
+
+ if (!id) {
+ return null
+ }
+
+ const route = { connectionId: id, profile: String(profile || '').trim() || 'default', registryScoped: true as const }
+ const scope = registrySshScopeForWindowRoute(route, readDesktopConnectionsRegistry())
+
+ if (!scope) {
+ return null
+ }
+
+ const state = sshConnections.get(scope)
+
+ if (state && state.ssh) {
+ return { ssh: state.ssh, scope }
+ }
+
+ const pooledScope = registrySshPoolScopeByConnectionId(sshConnections, id)
+ const pooledState = pooledScope === null ? null : sshConnections.get(pooledScope)
+
+ return pooledState && pooledState.ssh ? { ssh: pooledState.ssh, scope: pooledScope } : 'pending'
+}
+
async function ensureTerminalBackend(webContentsId: number) {
const windowRoute = windowConnectionRoutes.get(webContentsId)
@@ -17120,7 +17151,9 @@ const terminalIpc = registerTerminalIpc({
activeSshTerminalTarget,
sshBinary: desktopSshBinary,
ensureBackend: webContentsId => ensureTerminalBackend(webContentsId),
- getSshConnectionState: scope => sshConnections.get(scope)
+ getSshConnectionState: scope => sshConnections.get(scope),
+ resolveSshScopeForConnection: (connectionId, profile) => resolveSshScopeForConnectionId(connectionId, profile),
+ ensureConnectionBackend: (connectionId, profile) => ensureRegistryBackend(connectionId, profile)
})
const disposeTerminalSession = terminalIpc.disposeTerminalSession
diff --git a/apps/desktop/electron/terminal-ipc.ts b/apps/desktop/electron/terminal-ipc.ts
index 3a59d9968ea..e44da05a292 100644
--- a/apps/desktop/electron/terminal-ipc.ts
+++ b/apps/desktop/electron/terminal-ipc.ts
@@ -10,7 +10,7 @@ import path from 'node:path'
import { app, ipcMain } from 'electron'
import nodePty from 'node-pty'
-import { resolveTerminalConnectionForSender } from './connection-apply'
+import { resolveTerminalConnection, resolveTerminalConnectionForSender } from './connection-apply'
import { ensureSpawnHelperExecutable } from './spawn-helper-perms'
import { buildInteractiveSshArgs } from './ssh-connection'
import { createTerminalOutputGate } from './terminal-output-gate'
@@ -26,6 +26,11 @@ export interface TerminalIpcDeps {
sshBinary: () => string
ensureBackend: (webContentsId: number) => Promise<unknown>
getSshConnectionState: (scope: string) => undefined | { remotePlatform?: string }
+ /** Sessions-active (gateway, profile) → live SSH tunnel, if that gateway is SSH.
+ * Lets a new PTY follow the Sessions switcher instead of the window-backend
+ * route (which a Sessions-switch leaves pointing at the previous backend). */
+ resolveSshScopeForConnection?: (connectionId: string, profile?: string) => unknown
+ ensureConnectionBackend?: (connectionId: string, profile?: string) => Promise<unknown>
}
export interface TerminalIpcApi {
@@ -56,7 +61,9 @@ export function registerTerminalIpc({
activeSshTerminalTarget,
sshBinary,
ensureBackend,
- getSshConnectionState
+ getSshConnectionState,
+ resolveSshScopeForConnection,
+ ensureConnectionBackend
}: TerminalIpcDeps): TerminalIpcApi {
const terminalSessions = new Map()
@@ -312,7 +319,25 @@ export function registerTerminalIpc({
const cols = Math.max(2, Number.parseInt(String(payload?.cols || 80), 10) || 80)
const rows = Math.max(2, Number.parseInt(String(payload?.rows || 24), 10) || 24)
- const sshTarget = await resolveTerminalConnectionForSender(event.sender.id, activeSshTerminalTarget, ensureBackend)
+ const requestedConnectionId = typeof payload?.connectionId === 'string' ? payload.connectionId.trim() : ''
+ const requestedProfile = typeof payload?.profile === 'string' ? payload.profile : undefined
+
+ // Prefer the Sessions-active gateway the renderer was looking at. The
+ // window-backend route isn't re-pointed by a Sessions switch, so without
+ // this a live switch back to SSH keeps spawning a local shell.
+ let sessionTarget = null as null | { scope: string; ssh: unknown } | string
+ if (requestedConnectionId && resolveSshScopeForConnection) {
+ try {
+ sessionTarget = (await resolveTerminalConnection(
+ () => resolveSshScopeForConnection(requestedConnectionId, requestedProfile),
+ () => (ensureConnectionBackend ? ensureConnectionBackend(requestedConnectionId, requestedProfile) : Promise.resolve())
+ )) as null | { scope: string; ssh: unknown } | string
+ } catch {
+ sessionTarget = null
+ }
+ }
+
+ const sshTarget = sessionTarget || (await resolveTerminalConnectionForSender(event.sender.id, activeSshTerminalTarget, ensureBackend))
const remote = Boolean(sshTarget)
const remoteState = remote ? getSshConnectionState(sshTarget.scope) : null
diff --git a/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts b/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts
index 881e495a934..e300aa9b0a0 100644
--- a/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts
+++ b/apps/desktop/src/app/right-sidebar/terminal/terminal-session-attempt.ts
@@ -3,6 +3,8 @@ import type { RefObject } from 'react'
import type { HermesTerminalSession } from '@/global'
import { hostHas } from '@/lib/host-capabilities'
+import { $activeConnectionId } from '@/store/connections'
+import { $activeGatewayProfile } from '@/store/profile'
import { $terminals, markTerminalPersistent, removeExitedTerminal } from './terminals'
@@ -130,7 +132,18 @@ export function createSessionAttemptController(
void terminalApi
// Prefer the last observed cwd so retry/relaunch stays in the same directory.
- .start({ cols: term.cols, cwd: resolveCwd(), rows: term.rows, restoreKey: id, resumeOnly })
+ // Carry the Sessions active (gateway, profile) so a new PTY resolves
+ // the SSH tunnel for the gateway the user is LOOKING at — not the
+ // window-backend route, which a Sessions-switch doesn't re-point.
+ .start({
+ cols: term.cols,
+ connectionId: $activeConnectionId.get() ?? null,
+ cwd: resolveCwd(),
+ profile: $activeGatewayProfile.get(),
+ restoreKey: id,
+ resumeOnly,
+ rows: term.rows
+ })
.then(async session => {
persistent = Boolean(session.persistent)
resumeOnly = persistent
diff --git a/apps/desktop/src/global.d.ts b/apps/desktop/src/global.d.ts
index 115ddd6b974..331799726b5 100644
--- a/apps/desktop/src/global.d.ts
+++ b/apps/desktop/src/global.d.ts
@@ -630,7 +630,9 @@ declare global {
resize: (id: string, size: { cols: number; rows: number }) => Promise<boolean>
start: (options?: {
cols?: number
+ connectionId?: null | string
cwd?: string
+ profile?: string
rows?: number
restoreKey?: string
resumeOnly?: boolean