From c17897bd07122b808c38b4f770118d80dde9e53d Mon Sep 17 00:00:00 2001 From: Josh Gratton Date: Sun, 17 May 2026 14:00:13 -0400 Subject: [PATCH] fix(web): write WSL bearer before upserting record to avoid auth race The saved-env registry subscriber kicks off a sync as soon as upsert lands, and that path reads the bearer back via readSavedEnvironmentBearerToken. Writing the bearer first means whichever path connects first finds the credential. --- apps/web/src/environments/local/index.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/web/src/environments/local/index.ts b/apps/web/src/environments/local/index.ts index 90131727ac..e598c68787 100644 --- a/apps/web/src/environments/local/index.ts +++ b/apps/web/src/environments/local/index.ts @@ -125,8 +125,15 @@ async function registerSecondaryLocalEnvironment( desktopLocal: { instanceId: bootstrap.id }, }; - useSavedEnvironmentRegistryStore.getState().upsert(record); + // Order is load-bearing: write the bearer to the secret store before + // upserting the record. The zustand subscriber on the registry fires + // a saved-env sync as soon as upsert lands, and that path reads the + // bearer back out of the secret store via readSavedEnvironmentBearerToken. + // If we upserted first, the sync could race ahead, find no bearer, + // and flip the runtime state to "requires-auth" before our explicit + // ensureSavedEnvironmentConnection call below runs. await writeSavedEnvironmentBearerToken(environmentId, bearerSession.sessionToken); + useSavedEnvironmentRegistryStore.getState().upsert(record); await ensureSavedEnvironmentConnection(record, { bearerToken: bearerSession.sessionToken, role: bearerSession.role,