hermes-agent/apps/desktop/electron/window-connection-route.ts
Austin Pickett e1fdfb32f1
fix(desktop): re-point the window route after a primary connection apply (#124816)
* fix(desktop): re-point the window route after a primary connection apply

sendConnectionApplied() notified the renderer with a bare
hermes:connection:applied event, so the renderer's soft switch re-dialed
profile-less and main answered it from the window's recorded route
(resolveDesktopConnectionRequest). That record names the source the user just
LEFT, so after an apply the renderer kept dialing the stale registry-scoped
gateway: the session list never re-scoped, the status bar kept the old
backend's version, the old socket stayed ESTAB, and only a full restart
recovered. A locally recorded route is unscoped and follows the new v1 config,
which is why local -> remote looked fine while remote -> local did not.

Re-point the applied window's recorded route at the newly applied primary in
the same step as the notify, so the profile-less re-dial targets the gateway
just applied (and a window launched from that route afterwards inherits it).
An applied registry source stays registry-scoped to that exact identity; a
This-device apply stays unscoped so the dial follows the freshly written v1
config. The route rewrite and the notify only run once the config/registry
write has committed, so a rolled-back apply cannot leave a record naming a
source that never landed.

Fixes #92352

* fix(desktop): re-point the window route only on a global primary apply

A profile-scoped apply (the v1 per-profile remote override, payload.profile
set) never reconciles the registry, so its primary is unchanged: re-pointing
the main window's route there would yank a window viewing another registry
source onto the primary for an apply that did not move it. Keep the bare
notify for scoped applies and re-point only when the registry primary was
actually reconciled.

Read the applied primary from the registry snapshot that was just written
(nextRegistry) instead of re-reading the file inside the notify.
2026-09-27 21:37:07 -04:00

117 lines
3.8 KiB
TypeScript

import { backendScopeKey, type ConnectionRegistry, LOCAL_CONNECTION_ID } from './connection-registry'
export interface WindowConnectionRoute {
connectionId: null | string
profile: string | undefined
registryScoped: boolean
}
export function normalizeWindowConnectionRoute(value: unknown): WindowConnectionRoute | null {
if (!value || typeof value !== 'object') {
return null
}
const input = value as Record<string, unknown>
const connectionId = typeof input.connectionId === 'string' ? input.connectionId.trim() : ''
const profile = typeof input.profile === 'string' && input.profile.trim() ? input.profile.trim() : undefined
return {
connectionId: connectionId || null,
profile,
registryScoped: input.registryScoped === true
}
}
export function registrySshScopeForWindowRoute(
route: WindowConnectionRoute | null | undefined,
registry: ConnectionRegistry
): null | string {
if (!route?.registryScoped || !route.connectionId) {
return null
}
const source = registry.connections.find(connection => connection.id === route.connectionId)
if (!source || source.kind !== 'ssh') {
return null
}
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
}
// The sshConnections pool has a single writer that publishes every tunnel under
// its per-profile bootstrap key while stamping the entry with the registry
// connection that owns it. A registry-scoped lookup under the composite
// backendScopeKey can therefore miss a live tunnel; this recovers the writer's
// actual key from the stamped identity, the same match managedSshScopeRole
// applies to pool entries.
export function registrySshPoolScopeByConnectionId(
entries: Iterable<readonly [string, RegistrySshPoolEntry | undefined]>,
connectionId: string
): null | string {
for (const [scope, entry] of entries) {
if (entry?.ssh && entry.registryConnectionId === connectionId) {
return scope
}
}
return null
}
export class WindowConnectionRouteRegistry {
private readonly routes = new Map<number, WindowConnectionRoute>()
set(webContentsId: number, value: unknown): WindowConnectionRoute | null {
const route = normalizeWindowConnectionRoute(value)
if (!route) {
this.routes.delete(webContentsId)
return null
}
this.routes.set(webContentsId, route)
return route
}
get(webContentsId: number): WindowConnectionRoute | null {
return this.routes.get(webContentsId) ?? null
}
delete(webContentsId: number): void {
this.routes.delete(webContentsId)
}
}