The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): heal v1/v2 connection drift instead of re-homing onto local
Brooklyn Nicholsonaaf63220ca4d
diff --git a/apps/desktop/electron/connection-registry.test.ts b/apps/desktop/electron/connection-registry.test.tsindex a2eb602d5e9..3ad9a9d4a47 100644--- a/apps/desktop/electron/connection-registry.test.ts+++ b/apps/desktop/electron/connection-registry.test.ts@@ -26,6 +26,7 @@ import {normalizeRegistry,parseRemoteProfileListing,reconcileAppliedGlobalConnection,+ reconcileRegistryDrift,REGISTRY_VERSION,rememberSshEnumeration,removeConnection,@@ -1232,7 +1233,9 @@ test('Apply remote inserts into an existing local-only registry and becomes primassert.equal(registry.lastUsed, remote.id)assert.equal(resolvedConnectionId(registry, {+ authMode: 'oauth',baseUrl: 'https://gateway.example.com',+ headers: {},mode: 'remote',remoteKind: 'url'}),@@ -1303,6 +1306,112 @@ test('Apply between two remotes keeps each real registration once and activatesassert.equal(second.lastUsed, second.primary)})+// --- reconcileRegistryDrift (v1 ↔ v2 healing) ---++test('drift heal registers a v1 remote the registry never learned about and makes it primary', () => {+ // The exact shape users keep reporting: registry migrated while local-only,+ // then Settings → Gateway pointed v1 at a remote. connections.json still+ // says primary 'local', so every launch force-switches off the live remote.+ const drifted = reconcileRegistryDrift(emptyRegistry(), {+ mode: 'remote',+ remote: { url: 'https://agent.example.com:4443', authMode: 'oauth' }+ })++ assert.equal(drifted.changed, true)++ const remote = drifted.registry.connections.find(connection => connection.kind === 'remote')++ assert.ok(remote)+ assert.equal(drifted.registry.primary, remote.id)+ assert.equal(drifted.registry.lastUsed, remote.id)+ // The whole point: the live v1 descriptor can now be named, so the boot pick+ // resolves to the remote instead of re-homing to 'local'. Descriptor shape+ // matches what buildRemoteConnection emits for an oauth remote.+ assert.equal(+ resolvedConnectionId(drifted.registry, {+ authMode: 'oauth',+ baseUrl: 'https://agent.example.com:4443',+ headers: {},+ mode: 'remote',+ remoteKind: 'url'+ }),+ remote.id+ )+})++test('drift heal leaves a registry that already knows the v1 route untouched', () => {+ const registered = reconcileAppliedGlobalConnection(emptyRegistry(), {+ mode: 'remote',+ remote: { url: 'https://agent.example.com', authMode: 'oauth' }+ })++ const drifted = reconcileRegistryDrift(registered, {+ mode: 'remote',+ remote: { url: 'https://AGENT.example.com/', authMode: 'oauth' }+ })++ assert.equal(drifted.changed, false)+ assert.equal(drifted.registry, registered)+})++test('drift heal respects a deliberate primary pick on a registered route', () => {+ // Route IS registered, but the user chose This device in the Connections+ // panel. That is a choice, not drift — never override it.+ let registry = reconcileAppliedGlobalConnection(emptyRegistry(), {+ mode: 'remote',+ remote: { url: 'https://agent.example.com', authMode: 'oauth' }+ })++ registry = setPrimaryConnection(registry, LOCAL_CONNECTION_ID)++ const drifted = reconcileRegistryDrift(registry, {+ mode: 'remote',+ remote: { url: 'https://agent.example.com', authMode: 'oauth' }+ })++ assert.equal(drifted.changed, false)+ assert.equal(drifted.registry.primary, LOCAL_CONNECTION_ID)+})++test('drift heal ignores local, ssh, and unparseable v1 routes', () => {+ const registry = emptyRegistry()++ for (const v1 of [+ { mode: 'local', remote: {} },+ { mode: 'ssh', remote: { host: 'box' } },+ { mode: 'remote', remote: { url: 'not a url' } },+ { mode: 'remote', remote: {} },+ null+ ]) {+ const drifted = reconcileRegistryDrift(registry, v1)++ assert.equal(drifted.changed, false, `expected no heal for ${JSON.stringify(v1)}`)+ assert.equal(drifted.registry, registry)+ }+})++test('drift heal adds the missing remote without disturbing other registered sources', () => {+ let registry = emptyRegistry()++ registry = upsertConnection(registry, {+ id: 'homelab',+ kind: 'remote',+ label: 'Homelab',+ url: 'https://homelab.example.com',+ authMode: 'token',+ token: { keep: true }+ })++ const drifted = reconcileRegistryDrift(registry, {+ mode: 'remote',+ remote: { url: 'https://agent.example.com:4443', authMode: 'oauth' }+ })++ assert.equal(drifted.changed, true)+ assert.equal(drifted.registry.connections.filter(connection => connection.kind === 'remote').length, 2)+ assert.ok(drifted.registry.connections.some(connection => connection.id === 'homelab'))+})+// --- connectionDialFieldsChanged (edit → recycle decision) ---test('connectionDialFieldsChanged: label-only edits do not recycle', () => {diff --git a/apps/desktop/electron/connection-registry.ts b/apps/desktop/electron/connection-registry.tsindex a05c54d3cc2..a98a175eacb 100644--- a/apps/desktop/electron/connection-registry.ts+++ b/apps/desktop/electron/connection-registry.ts@@ -1280,6 +1280,69 @@ export function reconcileAppliedGlobalConnection(}}+/**+ * Heal an already-created registry that never learned about the v1 route it is+ * supposed to be serving.+ *+ * `migrateV1ToRegistry` runs exactly once — only when connections.json does not+ * exist. A user who was local at that moment and configured a remote gateway+ * afterwards (Settings -> Gateway writes connection.json alone) ends up with a+ * live remote that the registry cannot name: `resolvedConnectionId` returns+ * null, `primary` still says `local`, and every launch force-switches the+ * window onto a fresh local backend seconds after boot. Deleting+ * connections.json by hand is the only recovery today.+ *+ * Deliberately narrow: heal ONLY when the v1 global route has no matching+ * registry entry at all. That is the drift state and nothing else. If the+ * route is already registered but `primary` names another source, the user+ * chose that in the Connections panel and we leave it alone.+ */+export function reconcileRegistryDrift(+ registry: ConnectionRegistry,+ v1: unknown+): { changed: boolean; registry: ConnectionRegistry } {+ const config = v1 && typeof v1 === 'object' ? (v1 as Record<string, any>) : {}+ const unchanged = { changed: false, registry }++ if (!modeIsRemoteLike(config.mode)) {+ return unchanged+ }++ const block = config.remote && typeof config.remote === 'object' ? config.remote : {}++ let url = ''++ try {+ url = normalizeRemoteBaseUrl(block.url)+ } catch {+ // An unparseable v1 URL is not a route we can register. The v1 path keeps+ // failing the way it already does; do not corrupt the registry over it.+ return unchanged+ }++ if (!url) {+ return unchanged+ }++ const alreadyRegistered = registry.connections.some(connection => {+ if (connection.kind !== 'remote' && connection.kind !== 'cloud') {+ return false+ }++ try {+ return normalizeRemoteBaseUrl(connection.url) === url+ } catch {+ return false+ }+ })++ if (alreadyRegistered) {+ return unchanged+ }++ return { changed: true, registry: reconcileAppliedGlobalConnection(registry, config) }+}+/** Choose whether launch restores the explicit primary or the last-used source. */export function setConnectionLaunchMode(registry: ConnectionRegistry, launchMode: string): ConnectionRegistry {if (launchMode !== 'last-used' && launchMode !== 'primary') {diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 1c0baa9fd0a..11ee5452b0b 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -113,6 +113,7 @@ import {normalizeConnectionInput,normalizeRegistry,reconcileAppliedGlobalConnection,+ reconcileRegistryDrift,rememberSshEnumeration,removeConnection,resolvedConnectionId,@@ -8533,6 +8534,11 @@ function writeDesktopConnectionConfig(config) {* connections.json does not exist yet). Same mtime-cache + tighten-mode* discipline as readDesktopConnectionConfig; a corrupt registry degrades to* local-only via normalizeRegistry rather than throwing at boot.+ *+ * An EXISTING registry is additionally reconciled against v1 when the two have+ * drifted — see reconcileRegistryDrift. The one-shot migration cannot cover a+ * user who registered nothing and then pointed Settings -> Gateway at a remote,+ * and until that heals, every launch re-homes them onto a local backend.*/function readDesktopConnectionsRegistry() {let mtime = null@@ -8579,6 +8585,28 @@ function readDesktopConnectionsRegistry() {registry = normalizeRegistry(null)}+ // Heal v1 -> v2 drift: the v1 global route names a remote this registry has+ // never heard of, so the live descriptor resolves to no connectionId and the+ // launch pick sends the window somewhere else. Persist so the repair is a+ // one-time event rather than a recomputation on every read; a failed write+ // still returns the healed registry for this session.+ const reconciled = reconcileRegistryDrift(registry, readDesktopConnectionConfig())++ if (reconciled.changed) {+ registry = reconciled.registry++ try {+ writeDesktopConnectionsRegistry(registry)++ return connectionRegistryCache+ } catch {+ connectionRegistryCache = registry+ connectionRegistryCacheMtime = null++ return registry+ }+ }+connectionRegistryCache = registryconnectionRegistryCacheMtime = mtime