Skip to content

The agent that grows with you

fix(desktop): re-point the window route after a primary connection apply (#124816)

Austin Pickette1fdfb32f175
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 91822574999..b0863bca45c 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -570,6 +570,7 @@ import { windowAcceleratorAction } from './window-accelerator'
import { enumerateWindowsFrontToBack, enumerationFailed, readWindowBelow } from './window-below'
import { bindWindowChromeEvents } from './window-chrome-events'
import {
+ appliedPrimaryWindowRoute,
registrySshPoolScopeByConnectionId,
registrySshScopeForWindowRoute,
WindowConnectionRouteRegistry
@@ -16712,6 +16713,32 @@ ipcMain.handle('hermes:connection-config:apply', async (_event, payload) => {
const scope = key || ''
const nextRegistry = key ? previousRegistry : reconcileAppliedGlobalConnection(previousRegistry, config)
+ // Primary apply: the applied window's recorded route still names the source
+ // it just LEFT, and a profile-less re-dial is answered from that record
+ // (resolveDesktopConnectionRequest), so the renderer kept dialing the gateway
+ // the user switched away from and every later reconnect re-asked the same
+ // stale question (#92352). Re-point the record at the newly applied primary
+ // in the same step as the notify: both run only after the config/registry
+ // write has committed, so a rolled-back apply can never leave a record
+ // naming a source that did not land. A profile-scoped apply (v1 per-profile
+ // override) leaves the registry primary untouched, so it keeps the bare notify.
+ const applyPrimaryRoute = () => {
+ const win = mainWindow
+
+ if (win && !win.isDestroyed() && win.webContents && !win.webContents.isDestroyed()) {
+ const previous = windowConnectionRoutes.get(win.webContents.id)
+
+ recordWindowConnectionRoute(
+ win.webContents,
+ appliedPrimaryWindowRoute(nextRegistry, previous?.profile ?? primaryProfileKey())
+ )
+ }
+
+ sendConnectionApplied()
+ }
+
+ const notifyApplied = key ? sendConnectionApplied : applyPrimaryRoute
+
await applyConnectionConfigAtomically({
previousConfig,
previousRegistry,
@@ -16735,12 +16762,12 @@ ipcMain.handle('hermes:connection-config:apply', async (_event, payload) => {
bootstrapFailure = null
},
mode: config.mode,
- notifyConnectionApplied: sendConnectionApplied,
+ notifyConnectionApplied: notifyApplied,
resumeFirstRunRemote: abandonFirstRunSetupChoiceForRemoteApply,
teardownPrimaryBackend: teardownPrimaryBackendAndWait
}),
scope,
- sendApplied: sendConnectionApplied,
+ sendApplied: notifyApplied,
stopPool: stopPoolBackend,
teardownPrimary: () => teardownPrimaryBackendAndWait({ soft: true }),
teardownSsh: value => teardownSshConnection(value || null)
diff --git a/apps/desktop/electron/window-connection-route.test.ts b/apps/desktop/electron/window-connection-route.test.ts
index 400f151db7d..f08ac1b2a03 100644
--- a/apps/desktop/electron/window-connection-route.test.ts
+++ b/apps/desktop/electron/window-connection-route.test.ts
@@ -2,7 +2,9 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
+import { resolveDesktopConnectionRequest } from './desktop-profile'
import {
+ appliedPrimaryWindowRoute,
normalizeWindowConnectionRoute,
registrySshPoolScopeByConnectionId,
registrySshScopeForWindowRoute,
@@ -138,3 +140,66 @@ test('does not match another connection, an unlabelled entry, or a torn-down tun
assert.equal(registrySshPoolScopeByConnectionId(pool, 'source-b'), null)
assert.equal(registrySshPoolScopeByConnectionId(pool, 'source-c'), null)
})
+
+test('a primary apply re-points the window route so its re-dial leaves the gateway it just left', () => {
+ // #92352: the window was on a registered remote (registry-scoped) and the
+ // user applied This device. Its recorded route still names the remote, and
+ // the apply re-dial is profile-less, so main answered it from that record and
+ // the renderer kept dialing the gateway it had just left.
+ const stale = { connectionId: 'macmini', profile: 'default', registryScoped: true }
+
+ const registry = {
+ primary: 'local',
+ connections: [
+ { id: 'local', kind: 'local' },
+ { id: 'macmini', kind: 'remote' }
+ ]
+ } as never
+
+ assert.deepEqual(resolveDesktopConnectionRequest(undefined, stale, 'default'), {
+ connectionId: 'macmini',
+ profile: 'default'
+ })
+
+ const applied = appliedPrimaryWindowRoute(registry, stale.profile)
+
+ assert.deepEqual(applied, { connectionId: null, profile: 'default', registryScoped: false })
+ // The same profile-less re-dial now resolves to the freshly applied primary
+ // instead of the source it left.
+ assert.deepEqual(resolveDesktopConnectionRequest(undefined, applied, 'default'), {
+ connectionId: null,
+ profile: 'default'
+ })
+})
+
+test('a remote apply stays registry-scoped to the applied source and keeps the viewed profile', () => {
+ const registry = {
+ primary: 'macmini',
+ connections: [
+ { id: 'local', kind: 'local' },
+ { id: 'macmini', kind: 'remote' }
+ ]
+ } as never
+
+ const applied = appliedPrimaryWindowRoute(registry, 'work')
+
+ assert.deepEqual(applied, { connectionId: 'macmini', profile: 'work', registryScoped: true })
+ // A window launched from this route afterwards inherits the applied source.
+ assert.deepEqual(resolveDesktopConnectionRequest(undefined, applied, 'default'), {
+ connectionId: 'macmini',
+ profile: 'work'
+ })
+})
+
+test('falls back to the canonical profile when the applied window had no route to keep', () => {
+ assert.deepEqual(appliedPrimaryWindowRoute({ primary: 'local' } as never, undefined), {
+ connectionId: null,
+ profile: 'default',
+ registryScoped: false
+ })
+ assert.deepEqual(appliedPrimaryWindowRoute({ primary: '' } as never, ' '), {
+ connectionId: null,
+ profile: 'default',
+ registryScoped: false
+ })
+})
diff --git a/apps/desktop/electron/window-connection-route.ts b/apps/desktop/electron/window-connection-route.ts
index 806f690508f..c9c73716f89 100644
--- a/apps/desktop/electron/window-connection-route.ts
+++ b/apps/desktop/electron/window-connection-route.ts
@@ -1,4 +1,4 @@
-import { backendScopeKey, type ConnectionRegistry } from './connection-registry'
+import { backendScopeKey, type ConnectionRegistry, LOCAL_CONNECTION_ID } from './connection-registry'
export interface WindowConnectionRoute {
connectionId: null | string
@@ -40,6 +40,32 @@ export function registrySshScopeForWindowRoute(
return backendScopeKey(route.connectionId, route.profile)
}
+/**
+ * The route a window must re-dial after a PRIMARY connection apply (#92352).
+ *
+ * An apply re-homes the primary, but the window's recorded route still names
+ * the source it just LEFT. `resolveDesktopConnectionRequest` answers the
+ * renderer's profile-less apply re-dial from that record, so the renderer kept
+ * dialing the stale registry-scoped gateway — and every later reconnect, plus
+ * any window launched from that route, re-asked the same stale question until a
+ * restart dropped the in-memory record. Re-point the record at the newly
+ * applied primary, keeping the profile the window was viewing: an applied
+ * registry source (remote/cloud/ssh) stays registry-scoped to that exact
+ * identity, while a This-device apply stays unscoped so the dial follows the
+ * freshly written v1 config.
+ */
+export function appliedPrimaryWindowRoute(
+ registry: ConnectionRegistry,
+ profile: null | string | undefined
+): WindowConnectionRoute {
+ const key = String(profile ?? '').trim() || 'default'
+ const primary = String(registry?.primary ?? '').trim()
+
+ return primary && primary !== LOCAL_CONNECTION_ID
+ ? { connectionId: primary, profile: key, registryScoped: true }
+ : { connectionId: null, profile: key, registryScoped: false }
+}
+
export interface RegistrySshPoolEntry {
registryConnectionId?: null | string
ssh?: unknown