From f4c80e4243d4616e7210e53cbeaf1e1492bb668c Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Sun, 16 Aug 2026 01:51:35 -0700 Subject: [PATCH] fix(desktop): recycle live backends and sockets when a connection edit changes its target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit saveRegistryConnection only rewrote the registry file: editing a connection's URL/token/host left pooled backend descriptors under 'conn:::*' and open renderer sockets pointing at the OLD endpoint — the UI showed the new target while traffic kept flowing to the old one until idle-reap. When a save MATERIALLY changes an existing connection (endpoint / auth / ssh routing fields, via the new connectionDialFieldsChanged helper), main now stops that connection's pooled backends and tunnels (stopRegistryConnectionBackends, same teardown as removal) and broadcasts 'hermes:connections:changed' with reason 'updated' so renderers dispose and re-dial their secondaries at the new target. Label-only renames do not recycle. --- .../electron/connection-registry.test.ts | 29 +++++++++++++++ apps/desktop/electron/connection-registry.ts | 37 +++++++++++++++++++ apps/desktop/electron/main.ts | 13 ++++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/apps/desktop/electron/connection-registry.test.ts b/apps/desktop/electron/connection-registry.test.ts index 67ff99c92a..6567616184 100644 --- a/apps/desktop/electron/connection-registry.test.ts +++ b/apps/desktop/electron/connection-registry.test.ts @@ -15,6 +15,7 @@ import { backendScopeKey, backendScopePrefix, buildAgentRoster, + connectionDialFieldsChanged, connectionIdForLabel, labelKey, labelSlug, @@ -523,3 +524,31 @@ test('upsertConnection replaces by id and appends new ids', () => { assert.equal(registry.connections.filter(c => c.id === a.id).length, 1) assert.equal(registry.connections.find(c => c.id === a.id)?.url, 'http://a:2') }) + +// --- connectionDialFieldsChanged (edit → recycle decision) --- + +test('connectionDialFieldsChanged: label-only edits do not recycle', () => { + const before = { id: 'homelab', kind: 'remote', label: 'Homelab', url: 'http://10.0.0.5:9119', authMode: 'token', token: { encoding: 'safeStorage', value: 'abc' } } as const + + assert.equal(connectionDialFieldsChanged(before, { ...before, label: 'Home lab (renamed)' }), false) + // Identity edit is also a no-op. + assert.equal(connectionDialFieldsChanged(before, { ...before }), false) +}) + +test('connectionDialFieldsChanged: url / auth / token changes recycle', () => { + const before = { id: 'homelab', kind: 'remote', label: 'Homelab', url: 'http://10.0.0.5:9119', authMode: 'token', token: { encoding: 'safeStorage', value: 'abc' } } as const + + assert.equal(connectionDialFieldsChanged(before, { ...before, url: 'http://10.0.0.9:9119' }), true) + assert.equal(connectionDialFieldsChanged(before, { ...before, authMode: 'oauth', token: undefined }), true) + assert.equal(connectionDialFieldsChanged(before, { ...before, token: { encoding: 'safeStorage', value: 'NEW' } }), true) +}) + +test('connectionDialFieldsChanged: ssh routing fields recycle, kind change recycles', () => { + const before = { id: 'box', kind: 'ssh', label: 'Box', host: 'box.lan', user: 'me', port: 22 } as const + + assert.equal(connectionDialFieldsChanged(before, { ...before, label: 'Box 2' }), false) + assert.equal(connectionDialFieldsChanged(before, { ...before, host: 'other.lan' }), true) + assert.equal(connectionDialFieldsChanged(before, { ...before, port: 2222 }), true) + assert.equal(connectionDialFieldsChanged(before, { ...before, remoteProfile: 'work' }), true) + assert.equal(connectionDialFieldsChanged(before, { id: 'box', kind: 'remote', label: 'Box', url: 'http://x:1' }), true) +}) diff --git a/apps/desktop/electron/connection-registry.ts b/apps/desktop/electron/connection-registry.ts index ca3c789d63..08296d2a71 100644 --- a/apps/desktop/electron/connection-registry.ts +++ b/apps/desktop/electron/connection-registry.ts @@ -414,6 +414,43 @@ export function mergeConnectionInput(input: ConnectionInput, existing?: null | R return merged } +/** + * True when an edit changes how a connection is DIALED — endpoint, auth, or + * ssh routing fields — as opposed to a cosmetic label rename. Callers use + * this to decide whether live pooled backends / renderer sockets for the + * connection must be recycled after a save: a label-only edit keeps traffic + * flowing, while a url/token/host change means everything currently open + * points at the OLD target and must be torn down and re-dialed. + */ +export function connectionDialFieldsChanged(before: RegistryConnection, after: RegistryConnection): boolean { + if (before.kind !== after.kind) { + return true + } + + const fields: (keyof RegistryConnection)[] = [ + 'url', + 'authMode', + 'org', + 'host', + 'user', + 'port', + 'keyPath', + 'remoteHermesPath', + 'remoteProfile' + ] + + for (const field of fields) { + if ((before[field] ?? null) !== (after[field] ?? null)) { + return true + } + } + + // Token envelopes are opaque here (main.ts encrypts). An edit that carries + // no new token inherits the stored envelope verbatim, so structural + // equality is exact for the label-only case. + return JSON.stringify(before.token ?? null) !== JSON.stringify(after.token ?? null) +} + // ── Registry-level operations (all pure: return a new registry) ──────────── function localEntry(label = 'This device'): RegistryConnection { diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index e1e22a0730..529c2d8ea3 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -86,6 +86,7 @@ import { backendScopeKey, backendScopePrefix, buildAgentRoster, + connectionDialFieldsChanged, mergeConnectionInput, migrateV1ToRegistry, normalizeConnectionInput, @@ -108,7 +109,6 @@ import { shouldRemoveAppBundle, uninstallArgsForMode } from './desktop-uninstall' -import { selectPoolEvictions } from './pool-eviction' import { describeDevCdpDecision, resolveDevCdpPort } from './dev-cdp' import { installEmbedReferer } from './embed-referer' import { createEventDeduper } from './event-dedupe' @@ -213,6 +213,7 @@ import { electronProcessStartMarker, parentWatchdogEnv } from './parent-process-identity' +import { selectPoolEvictions } from './pool-eviction' import { createKeepAwake } from './power-save' import { FirstRunSetupResetError, runPrimaryBackendStartup } from './primary-backend-startup' import { rehomePrimaryConnection } from './primary-connection-rehome' @@ -8062,6 +8063,16 @@ function saveRegistryConnection(input: any = {}) { writeDesktopConnectionsRegistry(upsertConnection(registry, entry)) + // A dial-material edit (endpoint/auth/ssh routing — NOT a label rename) + // leaves pooled backends under `conn:::*` and renderer sockets pointing + // at the OLD target while the UI shows the new one. Recycle them: stop this + // connection's pooled backends/tunnels and tell renderers to dispose+redial + // their secondaries for this connection id. + if (existing && connectionDialFieldsChanged(existing, entry)) { + stopRegistryConnectionBackends(entry.id) + broadcastConnectionsChanged({ connectionId: entry.id, reason: 'updated' }) + } + return sanitizeRegistryConnection(entry) }