feat(desktop): widen local-environment bootstrap IPC to return all instances

getLocalEnvironmentBootstrap used to hand back a single bootstrap for
the primary backend. With the WSL backend running as a second pool
instance, the renderer needs to learn about both so step 7 can
register them as separate local environments.

Rename the IPC to getLocalEnvironmentBootstraps (plural) and walk
pool.list, emitting one entry per instance that already has a config.
Instances that are registered but haven't produced a config yet (WSL
backend mid-registration before its first start cycle) are skipped
and will appear on the next call.

The bootstrap entry gains an id field that mirrors the backend
instance id (e.g. "primary" or "wsl:ubuntu"). The renderer uses that
to find the primary entry today (auth.ts, target.ts); step 7 keys
local environments off the same id. PRIMARY_LOCAL_ENVIRONMENT_ID is
exported from contracts so web code can reference the primary by name
without importing brand machinery from the desktop package. The
desktop side wraps the same constant in BackendInstanceId so the two
stay locked together.

Test bridge mocks updated. The DesktopBridge casts in
authBootstrap.test.ts went through `as DesktopBridge` previously; the
array-returning plural is structurally different enough that TS
flagged it, so they go through `as unknown as DesktopBridge` now.
This commit is contained in:
Josh Gratton committed 2026-05-17 13:04:56 -04:00
1 parent 3fe02a498b
commit bad66041a6
12 files changed
+119 -70

No files matched your search

@@ -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<BackendInstanceId>();
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
+2 -2
View File
@@ -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);
+1 -1
View File
@@ -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";
+20 -12
View File
@@ -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;
}),
});
+5 -5
View File
@@ -27,12 +27,12 @@ contextBridge.exposeInMainWorld("desktopBridge", {
}
return result as ReturnType<DesktopBridge["getAppBranding"]>;
},
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<DesktopBridge["getLocalEnvironmentBootstrap"]>;
return result as ReturnType<DesktopBridge["getLocalEnvironmentBootstraps"]>;
},
getClientSettings: () => ipcRenderer.invoke(IpcChannels.GET_CLIENT_SETTINGS_CHANNEL),
setClientSettings: (settings) =>
+29 -20
View File
@@ -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");
@@ -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([]),
+18 -13
View File
@@ -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;
}
@@ -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",
},
],
},
});
+6 -2
View File
@@ -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 {
+1 -1
View File
@@ -171,7 +171,7 @@ function createLocalStorageStub(): Storage {
function makeDesktopBridge(overrides: Partial<DesktopBridge> = {}): DesktopBridge {
return {
getAppBranding: () => null,
getLocalEnvironmentBootstrap: () => null,
getLocalEnvironmentBootstraps: () => [],
getClientSettings: async () => null,
setClientSettings: async () => undefined,
getSavedEnvironmentRegistry: async () => [],
+15 -1
View File
@@ -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<ClientSettings | null>;
setClientSettings: (settings: ClientSettings) => Promise<void>;
getSavedEnvironmentRegistry: () => Promise<readonly PersistedSavedEnvironmentRecord[]>;