The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): prove registry-primary backend ownership electron-side; wait for the primary descriptor before boot restore
Michael Weismanfd031488aa0f
diff --git a/apps/desktop/electron/connection-registry.test.ts b/apps/desktop/electron/connection-registry.test.tsindex b07f6672d82..231eaa44613 100644--- a/apps/desktop/electron/connection-registry.test.ts+++ b/apps/desktop/electron/connection-registry.test.ts@@ -29,6 +29,7 @@ import {reconcileRegistryDrift,REGISTRY_VERSION,rememberSshEnumeration,+ registrySourceOwnsPrimaryBackend,removeConnection,resolvedConnectionId,resolveRegistryLocalRoute,@@ -238,6 +239,28 @@ test('primary SSH reuse rejects a descriptor with a different remote Hermes path)})+test('registry primary reuses a matching primary backend descriptor', () => {+ const registry = normalizeRegistry({+ version: REGISTRY_VERSION,+ primary: 'hermes-vps',+ launchMode: 'primary',+ lastUsed: 'hermes-vps',+ connections: [+ { id: LOCAL_CONNECTION_ID, kind: 'local', label: 'This device' },+ { id: 'hermes-vps', kind: 'ssh', label: 'Hermes VPS', host: 'hermes-vps' }+ ]+ })+ const descriptor = {+ connectionId: 'hermes-vps',+ mode: 'remote' as const,+ remoteKind: 'ssh' as const,+ ssh: { host: 'hermes-vps' }+ }++ assert.equal(registrySourceOwnsPrimaryBackend(registry, 'hermes-vps', descriptor), true)+ assert.equal(registrySourceOwnsPrimaryBackend(registry, LOCAL_CONNECTION_ID, descriptor), false)+})+test('resolvedConnectionId identifies local and migrated remote descriptors', () => {const registry = migrateV1ToRegistry({mode: 'local',diff --git a/apps/desktop/electron/connection-registry.ts b/apps/desktop/electron/connection-registry.tsindex 919366ecaa7..309ac2070f1 100644--- a/apps/desktop/electron/connection-registry.ts+++ b/apps/desktop/electron/connection-registry.ts@@ -397,6 +397,22 @@ export async function reuseMatchingPrimarySshBackend({return descriptor}+/**+ * Whether a registry-scoped request names the already-running primary backend.+ * Main uses this before opening a pooled registry backend so the registry's+ * primary SSH/remote source cannot spawn a second isolated server for the same+ * descriptor.+ */+export function registrySourceOwnsPrimaryBackend(+ registry: ConnectionRegistry,+ connectionId: null | string | undefined,+ descriptor: ResolvedConnectionDescriptor+): boolean {+ const id = String(connectionId ?? '').trim()++ return Boolean(id) && id === registry.primary && resolvedConnectionId(registry, descriptor) === id+}+function normalizedSshTarget(route: { host?: unknown; port?: unknown; user?: unknown }): null | string {const ssh = normalizeSshConfig({ ...route, mode: 'ssh' })diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex e3154b32cf7..7ed099d075f 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -134,6 +134,7 @@ import {reconcileAppliedGlobalConnection,reconcileRegistryDrift,rememberSshEnumeration,+ registrySourceOwnsPrimaryBackend,removeConnection,resolvedConnectionId,resolveRegistryLocalRoute,@@ -10572,6 +10573,24 @@ async function ensureRegistryBackend(connectionId, profile) {}}+ // The v1 primary and the registry primary can describe the same remote+ // backend beyond the SSH-fingerprint path above (cloud/url remotes have no+ // ssh -G identity). Reuse the already-running primary when the registry+ // resolves its live descriptor back to this exact source id; otherwise one+ // Desktop window starts two isolated servers whose transient runtime ids+ // are not interchangeable.+ if (id === registry.primary && source.kind !== 'local' && source.kind !== 'ssh') {+ const primaryDescriptor = await ensureBackend(profile)++ if (registrySourceOwnsPrimaryBackend(registry, id, primaryDescriptor)) {+ return {+ ...primaryDescriptor,+ profile: profileKey,+ connectionId: id+ }+ }+ }+if (source.kind === 'local') {// The registry's 'local' entry means THIS machine's runtime — always.// ensureBackend() follows the v1 routing table, which resolves to adiff --git a/apps/desktop/src/store/connections.test.ts b/apps/desktop/src/store/connections.test.tsindex 9d5ab7cb0ea..f7d40bd36f1 100644--- a/apps/desktop/src/store/connections.test.ts+++ b/apps/desktop/src/store/connections.test.ts@@ -723,15 +723,25 @@ describe('selectConnection', () => {}})- it('boot-time restore leaves "All profiles" browse mode on (#93197)', async () => {- // Fresh boot: nothing active yet, registry restores last-used. The- // persisted showAllProfiles=true must survive the silent restore.+ it('waits for the primary descriptor before restoring a source at boot', async () => {+ // The sidebar mounts while the primary gateway is still booting. Dialing+ // the preferred source before its descriptor is published can create a+ // second SSH backend for the exact same registered connection.list.mockResolvedValueOnce({ ...registry, lastUsed: 'homelab', launchMode: 'last-used' })$showAllProfiles.set(true)- await initializeConnectionsRegistry()+ const restoring = initializeConnectionsRegistry()- expect(ensureGatewayAgent).toHaveBeenCalledWith('homelab', 'default', expect.anything())+ await new Promise(resolve => setTimeout(resolve, 0))+ expect(openGatewayAgent).not.toHaveBeenCalled()+ expect(ensureGatewayAgent).not.toHaveBeenCalled()++ $connection.set({ connectionId: 'homelab', mode: 'remote' })+ await restoring++ expect(openGatewayAgent).not.toHaveBeenCalled()+ expect(ensureGatewayAgent).not.toHaveBeenCalled()+ expect(setLastUsed).toHaveBeenCalledWith('homelab')expect($showAllProfiles.get()).toBe(true)})diff --git a/apps/desktop/src/store/connections.ts b/apps/desktop/src/store/connections.tsindex 8a0e242ef2d..2330eb312e7 100644--- a/apps/desktop/src/store/connections.ts+++ b/apps/desktop/src/store/connections.ts@@ -135,6 +135,30 @@ async function rememberConnection(connectionId: string): Promise<void> {}}+/**+ * The sidebar registry initializes in parallel with the primary gateway boot.+ * Wait for main's resolved descriptor before deciding whether the preferred+ * source needs a secondary dial. Otherwise a remote primary can be opened a+ * second time through the registry while the identical primary SSH backend is+ * still publishing its connection identity.+ */+function waitForInitialConnection(): Promise<void> {+ if ($connection.get()) {+ return Promise.resolve()+ }++ return new Promise(resolve => {+ const unlisten = $connection.listen(connection => {+ if (!connection) {+ return+ }++ unlisten()+ resolve()+ })+ })+}+/*** Load the registry once for Sessions and restore the last successfully used* source. Later registry refreshes stay side-effect free, so editing Settings@@ -148,6 +172,7 @@ export async function initializeConnectionsRegistry(): Promise<DesktopConnection}restoreAttempted = true+ await waitForInitialConnection()// Residual drift: a window can be live on a source the registry cannot name// (a v1-configured remote that reconciliation has not repaired yet, e.g. a