fix(desktop): recycle live backends and sockets when a connection edit changes its target

saveRegistryConnection only rewrote the registry file: editing a
connection's URL/token/host left pooled backend descriptors under
'conn:<id>::*' 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.
This commit is contained in:
Teknium
2026-08-16 01:51:35 -07:00
parent 28c729e68f
commit f4c80e4243
3 changed files with 78 additions and 1 deletions
@@ -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)
})
@@ -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 {
+12 -1
View File
@@ -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:<id>::*` 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)
}