From cb8fbe568ffe083daf1014fe405204afc6b00a8f Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Mon, 11 May 2026 12:17:03 -0400 Subject: [PATCH] fix(desktop): baseline welcome env-id from the atom to avoid immediate-fire false resolve onWelcome subscribes with `immediate: true`, so the listener fires synchronously with whatever welcome payload is already in the atom. The previous code compared against `previousPrimaryEnvId` (descriptor-derived); if the descriptor hadn't loaded yet, that was null and any non-null current welcome would resolve the promise instantly, completing the "syncing" stage before the new backend's welcome actually arrived. Capture the current welcome's env-id from the atom as the baseline instead so the immediate fire never matches the "new welcome arrived" predicate. --- .../src/components/settings/ConnectionsSettings.tsx | 13 ++++++++++--- apps/web/src/rpc/serverState.ts | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/web/src/components/settings/ConnectionsSettings.tsx b/apps/web/src/components/settings/ConnectionsSettings.tsx index 0ba7586416..7a167d53b8 100644 --- a/apps/web/src/components/settings/ConnectionsSettings.tsx +++ b/apps/web/src/components/settings/ConnectionsSettings.tsx @@ -86,7 +86,7 @@ import { type ServerClientSessionRecord, type ServerPairingLinkRecord, } from "~/environments/primary"; -import { onWelcome } from "~/rpc/serverState"; +import { getWelcomePayload, onWelcome } from "~/rpc/serverState"; import { suppressReconnect } from "~/rpc/wsConnectionState"; import { useStore } from "~/store"; import type { WsRpcClient } from "~/rpc/wsRpcClient"; @@ -2403,13 +2403,20 @@ export function ConnectionsSettings() { const previousPrimaryEnvId = readPrimaryEnvironmentDescriptor()?.environmentId ?? null; // Subscribe to welcomes BEFORE the swap so we can wait for the new - // backend's welcome (env-id !== previous) before declaring the swap done. + // backend's welcome before declaring the swap done. Capture the current + // welcome's env-id as the baseline instead of comparing against + // `previousPrimaryEnvId`: `onWelcome` is `immediate: true` and fires + // synchronously with the atom's current value, so if the descriptor + // hasn't loaded (`previousPrimaryEnvId === null`) any non-null payload + // would otherwise resolve the promise instantly. Using the live welcome + // atom as the baseline closes that race. + const baselineWelcomeEnvId = getWelcomePayload()?.environment.environmentId ?? null; let resolveNewWelcome: (() => void) | null = null; const newWelcomePromise = new Promise((resolve) => { resolveNewWelcome = resolve; }); const unsubscribeWelcome = onWelcome((payload) => { - if (payload.environment.environmentId !== previousPrimaryEnvId) { + if (payload.environment.environmentId !== baselineWelcomeEnvId) { resolveNewWelcome?.(); } }); diff --git a/apps/web/src/rpc/serverState.ts b/apps/web/src/rpc/serverState.ts index 0a49ee1e22..bbe0778e9e 100644 --- a/apps/web/src/rpc/serverState.ts +++ b/apps/web/src/rpc/serverState.ts @@ -75,6 +75,10 @@ export function getServerConfig(): ServerConfig | null { return appAtomRegistry.get(serverConfigAtom); } +export function getWelcomePayload(): ServerLifecycleWelcomePayload | null { + return appAtomRegistry.get(welcomeAtom); +} + export function getServerKeybindings(): ServerConfig["keybindings"] { return selectKeybindings(getServerConfig()); }