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.
This commit is contained in:
Josh Gratton committed 2026-05-17 14:00:13 -04:00
1 parent 38e8477a45
commit c17897bd07
1 file changed
+8 -1
+8 -1
View File
@@ -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,