diff --git a/packages/app/src/runtime/host-runtime.ts b/packages/app/src/runtime/host-runtime.ts index 6fb009cb3..a026f306a 100644 --- a/packages/app/src/runtime/host-runtime.ts +++ b/packages/app/src/runtime/host-runtime.ts @@ -11,6 +11,7 @@ import { connectionFromListen, normalizeStoredHostProfile, upsertHostConnectionInProfiles, + registryHasConnection, type HostConnection, type HostProfile, } from "@/types/host-connection"; @@ -1265,20 +1266,20 @@ export class HostRuntimeStore { private async bootstrapLocalhost(): Promise { try { - try { - const { client, serverId, hostname } = await connectToDaemon( - { - id: `bootstrap:${DEFAULT_LOCALHOST_ENDPOINT}`, - type: "directTcp", - endpoint: DEFAULT_LOCALHOST_ENDPOINT, - }, - { timeoutMs: DEFAULT_LOCALHOST_BOOTSTRAP_TIMEOUT_MS }, - ); + const connection = connectionFromListen(DEFAULT_LOCALHOST_ENDPOINT); + if (!connection || registryHasConnection(this.hosts, connection)) { + return; + } - await this.upsertDirectConnection({ + try { + const { client, serverId, hostname } = await connectToDaemon(connection, { + timeoutMs: DEFAULT_LOCALHOST_BOOTSTRAP_TIMEOUT_MS, + }); + + await this.upsertHostConnection({ serverId, - endpoint: DEFAULT_LOCALHOST_ENDPOINT, label: hostname ?? undefined, + connection, existingClient: client, }); } catch { diff --git a/packages/app/src/types/host-connection.ts b/packages/app/src/types/host-connection.ts index 0b808e318..d470df2a2 100644 --- a/packages/app/src/types/host-connection.ts +++ b/packages/app/src/types/host-connection.ts @@ -310,24 +310,10 @@ export function normalizeStoredHostProfile(entry: unknown): HostProfile | null { }; } -export function normalizeEndpointOrNull(endpoint: string): string | null { - try { - return normalizeHostPort(endpoint); - } catch { - return null; - } +export function hostHasConnection(host: HostProfile, connection: HostConnection): boolean { + return host.connections.some((existing) => hostConnectionEquals(existing, connection)); } -export function hostHasDirectEndpoint(host: HostProfile, endpoint: string): boolean { - const normalized = normalizeEndpointOrNull(endpoint); - if (!normalized) { - return false; - } - return host.connections.some( - (connection) => connection.type === "directTcp" && connection.endpoint === normalized, - ); -} - -export function registryHasDirectEndpoint(hosts: HostProfile[], endpoint: string): boolean { - return hosts.some((host) => hostHasDirectEndpoint(host, endpoint)); +export function registryHasConnection(hosts: HostProfile[], connection: HostConnection): boolean { + return hosts.some((host) => hostHasConnection(host, connection)); }