Skip to content

The agent that grows with you

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

Tekniumf4c80e4243d4
diff --git a/apps/desktop/electron/connection-registry.test.ts b/apps/desktop/electron/connection-registry.test.ts
index 67ff99c92a1..65676161844 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 ca3c789d63c..08296d2a717 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 e1e22a07309..529c2d8ea36 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:<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)
}