diff --git a/apps/desktop/src/backend/DesktopBackendManager.ts b/apps/desktop/src/backend/DesktopBackendManager.ts index 161856d254..84ccefd00a 100644 --- a/apps/desktop/src/backend/DesktopBackendManager.ts +++ b/apps/desktop/src/backend/DesktopBackendManager.ts @@ -46,6 +46,7 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { DesktopBackendBootstrap, type DesktopBackendBootstrap as DesktopBackendBootstrapValue, + PRIMARY_LOCAL_ENVIRONMENT_ID, } from "@t3tools/contracts"; import * as DesktopObservability from "../app/DesktopObservability.ts"; @@ -144,7 +145,9 @@ export interface DesktopBackendSnapshot { export type BackendInstanceId = string & Brand.Brand<"BackendInstanceId">; export const BackendInstanceId = Brand.nominal(); -export const PRIMARY_INSTANCE_ID: BackendInstanceId = BackendInstanceId("primary"); +export const PRIMARY_INSTANCE_ID: BackendInstanceId = BackendInstanceId( + PRIMARY_LOCAL_ENVIRONMENT_ID, +); // One pooled backend instance. Same lifecycle surface as the legacy // `DesktopBackendManagerShape`; the id and label give the pool registry diff --git a/apps/desktop/src/ipc/DesktopIpcHandlers.ts b/apps/desktop/src/ipc/DesktopIpcHandlers.ts index e663228961..fe2ebebc55 100644 --- a/apps/desktop/src/ipc/DesktopIpcHandlers.ts +++ b/apps/desktop/src/ipc/DesktopIpcHandlers.ts @@ -35,7 +35,7 @@ import { import { confirm, getAppBranding, - getLocalEnvironmentBootstrap, + getLocalEnvironmentBootstraps, openExternal, pickFolder, setTheme, @@ -47,7 +47,7 @@ export const installDesktopIpcHandlers = Effect.gen(function* () { const ipc = yield* DesktopIpc.DesktopIpc; yield* ipc.handleSync(getAppBranding); - yield* ipc.handleSync(getLocalEnvironmentBootstrap); + yield* ipc.handleSync(getLocalEnvironmentBootstraps); yield* ipc.handle(getClientSettings); yield* ipc.handle(setClientSettings); diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts index 8410c7d999..9109a04e21 100644 --- a/apps/desktop/src/ipc/channels.ts +++ b/apps/desktop/src/ipc/channels.ts @@ -11,7 +11,7 @@ export const UPDATE_DOWNLOAD_CHANNEL = "desktop:update-download"; export const UPDATE_INSTALL_CHANNEL = "desktop:update-install"; export const UPDATE_CHECK_CHANNEL = "desktop:update-check"; export const GET_APP_BRANDING_CHANNEL = "desktop:get-app-branding"; -export const GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL = "desktop:get-local-environment-bootstrap"; +export const GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL = "desktop:get-local-environment-bootstraps"; export const GET_CLIENT_SETTINGS_CHANNEL = "desktop:get-client-settings"; export const SET_CLIENT_SETTINGS_CHANNEL = "desktop:set-client-settings"; export const GET_SAVED_ENVIRONMENT_REGISTRY_CHANNEL = "desktop:get-saved-environment-registry"; diff --git a/apps/desktop/src/ipc/methods/window.ts b/apps/desktop/src/ipc/methods/window.ts index 1721ddac3f..23f8adfbea 100644 --- a/apps/desktop/src/ipc/methods/window.ts +++ b/apps/desktop/src/ipc/methods/window.ts @@ -4,6 +4,7 @@ import { DesktopEnvironmentBootstrapSchema, DesktopThemeSchema, PickFolderOptionsSchema, + type DesktopEnvironmentBootstrap, } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; @@ -47,24 +48,31 @@ export const getAppBranding = makeSyncIpcMethod({ }), }); -export const getLocalEnvironmentBootstrap = makeSyncIpcMethod({ - channel: IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL, - result: Schema.NullOr(DesktopEnvironmentBootstrapSchema), - handler: Effect.fn("desktop.ipc.window.getLocalEnvironmentBootstrap")(function* () { +export const getLocalEnvironmentBootstraps = makeSyncIpcMethod({ + channel: IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL, + result: Schema.Array(DesktopEnvironmentBootstrapSchema), + handler: Effect.fn("desktop.ipc.window.getLocalEnvironmentBootstraps")(function* () { const pool = yield* DesktopBackendPool.DesktopBackendPool; - const primaryBackend = yield* pool.primary; - const config = yield* primaryBackend.currentConfig; - return Option.match(config, { - onNone: () => null, - onSome: ({ bootstrap, httpBaseUrl }) => ({ - label: "Local environment", + const instances = yield* pool.list; + const bootstraps: DesktopEnvironmentBootstrap[] = []; + for (const instance of instances) { + const config = yield* instance.currentConfig; + // Skip instances that haven't produced a config yet (e.g. WSL + // backend mid-registration, before its first start cycle). They'll + // appear on the next IPC call once they've started. + if (Option.isNone(config)) continue; + const { bootstrap, httpBaseUrl } = config.value; + bootstraps.push({ + id: instance.id, + label: instance.label, httpBaseUrl: httpBaseUrl.href, wsBaseUrl: toWebSocketBaseUrl(httpBaseUrl), ...(bootstrap.desktopBootstrapToken ? { bootstrapToken: bootstrap.desktopBootstrapToken } : {}), - }), - }); + }); + } + return bootstraps; }), }); diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index 72bcd71a72..b47e741cd9 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -27,12 +27,12 @@ contextBridge.exposeInMainWorld("desktopBridge", { } return result as ReturnType; }, - getLocalEnvironmentBootstrap: () => { - const result = ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAP_CHANNEL); - if (typeof result !== "object" || result === null) { - return null; + getLocalEnvironmentBootstraps: () => { + const result = ipcRenderer.sendSync(IpcChannels.GET_LOCAL_ENVIRONMENT_BOOTSTRAPS_CHANNEL); + if (!Array.isArray(result)) { + return []; } - return result as ReturnType; + return result as ReturnType; }, getClientSettings: () => ipcRenderer.invoke(IpcChannels.GET_CLIENT_SETTINGS_CHANNEL), setClientSettings: (settings) => diff --git a/apps/web/src/authBootstrap.test.ts b/apps/web/src/authBootstrap.test.ts index e6f08f147c..515fb03849 100644 --- a/apps/web/src/authBootstrap.test.ts +++ b/apps/web/src/authBootstrap.test.ts @@ -92,13 +92,16 @@ describe("resolveInitialServerAuthGateState", () => { const testWindow = installTestBrowser("http://localhost/"); testWindow.desktopBridge = { - getLocalEnvironmentBootstrap: () => ({ - label: "Local environment", - httpBaseUrl: "http://localhost:3773", - wsBaseUrl: "ws://localhost:3773", - bootstrapToken: "desktop-bootstrap-token", - }), - } as DesktopBridge; + getLocalEnvironmentBootstraps: () => [ + { + id: "primary", + label: "Windows", + httpBaseUrl: "http://localhost:3773", + wsBaseUrl: "ws://localhost:3773", + bootstrapToken: "desktop-bootstrap-token", + }, + ], + } as unknown as DesktopBridge; const { resolveInitialServerAuthGateState } = await import("./environments/primary"); @@ -192,12 +195,15 @@ describe("resolveInitialServerAuthGateState", () => { const testWindow = installTestBrowser("http://127.0.0.1:5733/"); testWindow.desktopBridge = { - getLocalEnvironmentBootstrap: () => ({ - label: "Local environment", - httpBaseUrl: "http://127.0.0.1:3773", - wsBaseUrl: "ws://127.0.0.1:3773", - }), - } as DesktopBridge; + getLocalEnvironmentBootstraps: () => [ + { + id: "primary", + label: "Windows", + httpBaseUrl: "http://127.0.0.1:3773", + wsBaseUrl: "ws://127.0.0.1:3773", + }, + ], + } as unknown as DesktopBridge; const { resolveInitialServerAuthGateState } = await import("./environments/primary"); @@ -431,13 +437,16 @@ describe("resolveInitialServerAuthGateState", () => { const testWindow = installTestBrowser("http://localhost/"); testWindow.desktopBridge = { - getLocalEnvironmentBootstrap: () => ({ - label: "Local environment", - httpBaseUrl: "http://localhost:3773", - wsBaseUrl: "ws://localhost:3773", - bootstrapToken: "desktop-bootstrap-token", - }), - } as DesktopBridge; + getLocalEnvironmentBootstraps: () => [ + { + id: "primary", + label: "Windows", + httpBaseUrl: "http://localhost:3773", + wsBaseUrl: "ws://localhost:3773", + bootstrapToken: "desktop-bootstrap-token", + }, + ], + } as unknown as DesktopBridge; const { resolveInitialServerAuthGateState } = await import("./environments/primary"); diff --git a/apps/web/src/components/settings/SettingsPanels.browser.tsx b/apps/web/src/components/settings/SettingsPanels.browser.tsx index c06d800eac..c98075fd59 100644 --- a/apps/web/src/components/settings/SettingsPanels.browser.tsx +++ b/apps/web/src/components/settings/SettingsPanels.browser.tsx @@ -360,12 +360,15 @@ const createDesktopBridgeStub = (overrides?: { return { getAppBranding: vi.fn().mockReturnValue(null), - getLocalEnvironmentBootstrap: () => ({ - label: "Local environment", - httpBaseUrl: "http://127.0.0.1:3773", - wsBaseUrl: "ws://127.0.0.1:3773", - bootstrapToken: "desktop-bootstrap-token", - }), + getLocalEnvironmentBootstraps: () => [ + { + id: "primary", + label: "Windows", + httpBaseUrl: "http://127.0.0.1:3773", + wsBaseUrl: "ws://127.0.0.1:3773", + bootstrapToken: "desktop-bootstrap-token", + }, + ], getClientSettings: vi.fn().mockResolvedValue(null), setClientSettings: vi.fn().mockResolvedValue(undefined), getSavedEnvironmentRegistry: vi.fn().mockResolvedValue([]), diff --git a/apps/web/src/environments/primary/auth.ts b/apps/web/src/environments/primary/auth.ts index daa5d19c49..3e92b830ce 100644 --- a/apps/web/src/environments/primary/auth.ts +++ b/apps/web/src/environments/primary/auth.ts @@ -1,13 +1,14 @@ -import type { - AuthBootstrapInput, - AuthBootstrapResult, - AuthClientMetadata, - AuthCreatePairingCredentialInput, - AuthPairingCredentialResult, - AuthRevokeClientSessionInput, - AuthRevokePairingLinkInput, - AuthSessionId, - AuthSessionState, +import { + PRIMARY_LOCAL_ENVIRONMENT_ID, + type AuthBootstrapInput, + type AuthBootstrapResult, + type AuthClientMetadata, + type AuthCreatePairingCredentialInput, + type AuthPairingCredentialResult, + type AuthRevokeClientSessionInput, + type AuthRevokePairingLinkInput, + type AuthSessionId, + type AuthSessionState, } from "@t3tools/contracts"; import { @@ -85,9 +86,13 @@ export function takePairingTokenFromUrl(): string | null { } function getDesktopBootstrapCredential(): string | null { - const bootstrap = window.desktopBridge?.getLocalEnvironmentBootstrap(); - return typeof bootstrap?.bootstrapToken === "string" && bootstrap.bootstrapToken.length > 0 - ? bootstrap.bootstrapToken + // Both backends share the same bootstrap token (DesktopBackendConfiguration + // mints one tokenRef and feeds it to both resolvers), so picking the + // primary entry is fine even when the WSL backend is also registered. + const bootstraps = window.desktopBridge?.getLocalEnvironmentBootstraps() ?? []; + const primary = bootstraps.find((entry) => entry.id === PRIMARY_LOCAL_ENVIRONMENT_ID); + return typeof primary?.bootstrapToken === "string" && primary.bootstrapToken.length > 0 + ? primary.bootstrapToken : null; } diff --git a/apps/web/src/environments/primary/bootstrap.test.ts b/apps/web/src/environments/primary/bootstrap.test.ts index 6081e425e9..d4689902d6 100644 --- a/apps/web/src/environments/primary/bootstrap.test.ts +++ b/apps/web/src/environments/primary/bootstrap.test.ts @@ -155,12 +155,15 @@ describe("environmentBootstrap", () => { replaceState: vi.fn(), }, desktopBridge: { - getLocalEnvironmentBootstrap: () => ({ - label: "Local environment", - httpBaseUrl: "http://127.0.0.1:3773", - wsBaseUrl: "ws://127.0.0.1:3773", - bootstrapToken: "desktop-bootstrap-token", - }), + getLocalEnvironmentBootstraps: () => [ + { + id: "primary", + label: "Windows", + httpBaseUrl: "http://127.0.0.1:3773", + wsBaseUrl: "ws://127.0.0.1:3773", + bootstrapToken: "desktop-bootstrap-token", + }, + ], }, }); diff --git a/apps/web/src/environments/primary/target.ts b/apps/web/src/environments/primary/target.ts index 04b7d903d4..c811de89d8 100644 --- a/apps/web/src/environments/primary/target.ts +++ b/apps/web/src/environments/primary/target.ts @@ -1,4 +1,4 @@ -import type { DesktopEnvironmentBootstrap } from "@t3tools/contracts"; +import { PRIMARY_LOCAL_ENVIRONMENT_ID, type DesktopEnvironmentBootstrap } from "@t3tools/contracts"; import type { KnownEnvironment } from "@t3tools/client-runtime"; export interface PrimaryEnvironmentTarget { @@ -9,7 +9,11 @@ export interface PrimaryEnvironmentTarget { const LOOPBACK_HOSTNAMES = new Set(["127.0.0.1", "::1", "localhost"]); function getDesktopLocalEnvironmentBootstrap(): DesktopEnvironmentBootstrap | null { - return window.desktopBridge?.getLocalEnvironmentBootstrap() ?? null; + // The primary (Windows-native) backend keeps the "primary" id. The + // plural list may include a second WSL entry; the primary-target + // resolver only cares about the primary, so just find it. + const bootstraps = window.desktopBridge?.getLocalEnvironmentBootstraps() ?? []; + return bootstraps.find((entry) => entry.id === PRIMARY_LOCAL_ENVIRONMENT_ID) ?? null; } function normalizeBaseUrl(rawValue: string): string { diff --git a/apps/web/src/localApi.test.ts b/apps/web/src/localApi.test.ts index 7b4cfcdc72..39f3690da4 100644 --- a/apps/web/src/localApi.test.ts +++ b/apps/web/src/localApi.test.ts @@ -171,7 +171,7 @@ function createLocalStorageStub(): Storage { function makeDesktopBridge(overrides: Partial = {}): DesktopBridge { return { getAppBranding: () => null, - getLocalEnvironmentBootstrap: () => null, + getLocalEnvironmentBootstraps: () => [], getClientSettings: async () => null, setClientSettings: async () => undefined, getSavedEnvironmentRegistry: async () => [], diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 8d306f01ac..cbd993a519 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -216,7 +216,17 @@ export const DesktopUpdateCheckResultSchema = Schema.Struct({ state: DesktopUpdateStateSchema, }); +// Stable id for the Windows-native primary backend. Desktop side wraps +// this with a brand inside DesktopBackendManager; web side keeps it as +// a plain string so the env-runtime can compare against it without +// importing brand machinery from the desktop package. +export const PRIMARY_LOCAL_ENVIRONMENT_ID = "primary"; + export interface DesktopEnvironmentBootstrap { + // Stable backend instance id (e.g. "primary" or "wsl:ubuntu"). The + // web env runtime keys local environments off this so projects + // routed to a specific backend reopen against the same one. + id: string; label: string; httpBaseUrl: string | null; wsBaseUrl: string | null; @@ -224,6 +234,7 @@ export interface DesktopEnvironmentBootstrap { } export const DesktopEnvironmentBootstrapSchema = Schema.Struct({ + id: Schema.String, label: Schema.String, httpBaseUrl: Schema.NullOr(Schema.String), wsBaseUrl: Schema.NullOr(Schema.String), @@ -401,7 +412,10 @@ export const DesktopWslStateSchema = Schema.Struct({ export interface DesktopBridge { getAppBranding: () => DesktopAppBranding | null; - getLocalEnvironmentBootstrap: () => DesktopEnvironmentBootstrap | null; + // One bootstrap per pool instance currently registered with bootstrap + // info (omits instances whose backend hasn't produced a config yet). + // The primary backend is identified by id === PRIMARY_LOCAL_ENVIRONMENT_ID. + getLocalEnvironmentBootstraps: () => readonly DesktopEnvironmentBootstrap[]; getClientSettings: () => Promise; setClientSettings: (settings: ClientSettings) => Promise; getSavedEnvironmentRegistry: () => Promise;