Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): serialize native auth commits

Josué3d6de973c988
diff --git a/desktop-app/src/__tests__/appService.nativeCommit.test.ts b/desktop-app/src/__tests__/appService.nativeCommit.test.ts
new file mode 100644
index 000000000..cbd92e282
--- /dev/null
+++ b/desktop-app/src/__tests__/appService.nativeCommit.test.ts
@@ -0,0 +1,63 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import {
+ cleanupNativeCommitTestHarness,
+ createNativeCommitTestHarness,
+ deferred,
+} from './appService.nativeCommitTestHarness.js'
+
+afterEach(cleanupNativeCommitTestHarness)
+
+describe('AppService native auth and environment commit ordering', () => {
+ it('keeps a pending environment selection ahead of a login commit', async () => {
+ const { service, runtimeConfig, restA, restB, optionB } = await createNativeCommitTestHarness()
+ const loginStarted = deferred<void>()
+ const releaseLogin = deferred<void>()
+ let handoffRequestRest = ''
+ service.authClient = {
+ googleLogin: vi.fn(async (token: string) => {
+ const requestRest = runtimeConfig.config.externalRestApiBaseUrl
+ if (token === 'handoff-login') {
+ handoffRequestRest = requestRest
+ loginStarted.resolve()
+ await releaseLogin.promise
+ return requestRest === restA
+ ? { token: 'session-a-next', me: { id: 'user-a' } }
+ : { token: 'session-b', me: { id: 'user-b' } }
+ }
+ return { token: 'session-a', me: { id: 'user-a' } }
+ }),
+ } as never
+ await service.googleLogin('initial-login')
+
+ let handoffLogin: Promise<unknown> | null = null
+ const selectReal = runtimeConfig.selectDesktopRuntimeConfigOption.bind(runtimeConfig)
+ vi.spyOn(runtimeConfig, 'selectDesktopRuntimeConfigOption').mockImplementation(async id => {
+ if (id === optionB.id) handoffLogin = service.googleLogin('handoff-login')
+ await selectReal(id)
+ })
+
+ const selection = service.selectRuntimeConfig(optionB.id)
+ const selectionOutcome = await selection.then(
+ value => ({ status: 'fulfilled' as const, value }),
+ reason => ({ status: 'rejected' as const, reason })
+ )
+ await loginStarted.promise
+ releaseLogin.resolve()
+ const loginOutcome = await handoffLogin
+
+ expect(selectionOutcome.status).toBe('fulfilled')
+ expect(handoffRequestRest).toBe(restB)
+ expect(loginOutcome).toMatchObject({ authenticated: true, me: { id: 'user-b' } })
+ expect(runtimeConfig.getDesktopRuntimeConfigState().activeOptionId).toBe(optionB.id)
+ await expect(service.getSessionState()).resolves.toMatchObject({
+ authenticated: true,
+ me: { id: 'user-b' },
+ })
+ await expect(service.listGfsUploadSessions()).resolves.toEqual([])
+ expect(service.gfsScopeIdentity).toMatchObject({
+ ownerId: 'user-b',
+ environmentKey: runtimeConfig.getDesktopRuntimeConfigState().envKey,
+ baseUrl: restB,
+ })
+ })
+})
diff --git a/desktop-app/src/__tests__/appService.nativeCommitTestHarness.ts b/desktop-app/src/__tests__/appService.nativeCommitTestHarness.ts
new file mode 100644
index 000000000..cd1520f87
--- /dev/null
+++ b/desktop-app/src/__tests__/appService.nativeCommitTestHarness.ts
@@ -0,0 +1,125 @@
+import { vi } from 'vitest'
+import fs from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+
+vi.mock('../chatStoreBinding.js', () => ({
+ bindChatStoreForUser: vi.fn(),
+ unbindChatStore: vi.fn(),
+ __setChatStoreBaseDirForTests: vi.fn(),
+}))
+
+export type Deferred<T> = {
+ promise: Promise<T>
+ resolve: (value: T) => void
+}
+
+export function deferred<T>(): Deferred<T> {
+ let resolve!: (value: T) => void
+ const promise = new Promise<T>(done => {
+ resolve = done
+ })
+ return { promise, resolve }
+}
+
+export type NativeCommitTestService = {
+ authClient: { googleLogin: ReturnType<typeof vi.fn> }
+ tokenStore: {
+ clearSessionToken: ReturnType<typeof vi.fn>
+ getSessionToken: ReturnType<typeof vi.fn>
+ setSessionToken: ReturnType<typeof vi.fn>
+ }
+ sessionToken: string | null
+ me: { id: string } | null
+ gfsScopeIdentity: { ownerId: string; environmentKey: string; baseUrl: string } | null
+ updateDesktopGfsUploadState: ReturnType<typeof vi.fn>
+ readDesktopGfsUploadState: ReturnType<typeof vi.fn>
+ googleLogin: (token: string) => Promise<unknown>
+ logout: () => Promise<number>
+ selectRuntimeConfig: (optionId: string) => Promise<unknown>
+ getSessionState: () => Promise<unknown>
+ getCachedUserId: () => string | null
+ listGfsUploadSessions: () => Promise<unknown>
+}
+
+const originalEnvironment = {
+ appData: process.env.APPDATA,
+ configPath: process.env.CLERUM_DESKTOP_CONFIG_PATH,
+ externalRest: process.env.EXTERNAL_REST_API_BASE_URL,
+ rpcProxy: process.env.RPC_PROXY_BASE_URL,
+ profileUi: process.env.PROFILE_UI_BASE_URL,
+}
+const tempDirs = new Set<string>()
+
+export async function createNativeCommitTestHarness() {
+ const userDataDir = await fs.mkdtemp(path.join(os.tmpdir(), 'evenfire-native-commit-'))
+ tempDirs.add(userDataDir)
+ delete process.env.CLERUM_DESKTOP_CONFIG_PATH
+ delete process.env.EXTERNAL_REST_API_BASE_URL
+ delete process.env.RPC_PROXY_BASE_URL
+ delete process.env.PROFILE_UI_BASE_URL
+ vi.resetModules()
+ vi.doMock('electron', () => ({
+ app: {
+ getPath: vi.fn((name: string) =>
+ name === 'appData' ? path.dirname(userDataDir) : userDataDir
+ ),
+ isPackaged: false,
+ isReady: vi.fn(() => true),
+ setName: vi.fn(),
+ setPath: vi.fn(),
+ },
+ }))
+
+ const [{ AppService }, runtimeConfig] = await Promise.all([
+ import('../appService.js'),
+ import('../config.js'),
+ ])
+ const restA = 'https://api-a.example.test'
+ const restB = 'https://api-b.example.test'
+ await runtimeConfig.saveDesktopRuntimeConfig({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: 'https://rpc-a.example.test',
+ appName: 'Environment A',
+ })
+ const optionA = runtimeConfig
+ .getDesktopRuntimeConfigState()
+ .options.find(option => option.externalRestApiBaseUrl === restA)
+ await runtimeConfig.saveDesktopRuntimeConfig({
+ externalRestApiBaseUrl: restB,
+ rpcProxyBaseUrl: 'https://rpc-b.example.test',
+ appName: 'Environment B',
+ })
+ const optionB = runtimeConfig
+ .getDesktopRuntimeConfigState()
+ .options.find(option => option.externalRestApiBaseUrl === restB)
+ if (!optionA || !optionB) throw new Error('test runtime profiles were not created')
+ await runtimeConfig.selectDesktopRuntimeConfigOption(optionA.id)
+
+ const service = new AppService() as unknown as NativeCommitTestService
+ service.tokenStore = {
+ clearSessionToken: vi.fn().mockResolvedValue(undefined),
+ getSessionToken: vi.fn().mockResolvedValue(null),
+ setSessionToken: vi.fn().mockResolvedValue(undefined),
+ } as never
+ service.updateDesktopGfsUploadState = vi.fn().mockResolvedValue(undefined)
+ service.readDesktopGfsUploadState = vi.fn().mockResolvedValue({ version: 1, records: [] })
+
+ return { service, runtimeConfig, restA, restB, optionA, optionB }
+}
+
+export async function cleanupNativeCommitTestHarness(): Promise<void> {
+ for (const [key, value] of Object.entries({
+ APPDATA: originalEnvironment.appData,
+ CLERUM_DESKTOP_CONFIG_PATH: originalEnvironment.configPath,
+ EXTERNAL_REST_API_BASE_URL: originalEnvironment.externalRest,
+ RPC_PROXY_BASE_URL: originalEnvironment.rpcProxy,
+ PROFILE_UI_BASE_URL: originalEnvironment.profileUi,
+ })) {
+ if (value === undefined) delete process.env[key]
+ else process.env[key] = value
+ }
+ vi.resetModules()
+ await Promise.all([...tempDirs].map(dir => fs.rm(dir, { recursive: true, force: true })))
+ tempDirs.clear()
+}
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index 1d35c6cab..c896efc0e 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -891,6 +891,7 @@ export class AppService {
private savedSessionRestoreAttemptedEnvKey: string | null = null
private savedSessionRestoreAttemptedAtMs = 0
private logoutInProgress = false
+ private nativeAuthEnvironmentCommitQueue: Promise<void> = Promise.resolve()
private gfsAuthEpoch = 0
private gfsDispatchBlocked = true
// A transient runWithTeamContext hop temporarily installs another team's
@@ -1063,6 +1064,40 @@ export class AppService {
})
}
+ /**
+ * Serialize native operations that publish runtime configuration, session,
+ * token-store, or GFS authorization state. The queue recovers after either
+ * success or failure so one rejected transition cannot strand later work.
+ */
+ private withNativeAuthEnvironmentCommit<T>(operation: () => Promise<T>): Promise<T> {
+ const result = this.nativeAuthEnvironmentCommitQueue.then(operation, operation)
+ this.nativeAuthEnvironmentCommitQueue = result.then(
+ () => undefined,
+ () => undefined
+ )
+ return result
+ }
+
+ private captureAuthEnvironmentBinding(): { environmentKey: string; restBaseUrl: string } {
+ return {
+ environmentKey: getActiveEnvKey(),
+ restBaseUrl: normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl),
+ }
+ }
+
+ private assertAuthEnvironmentBinding(binding: {
+ environmentKey: string
+ restBaseUrl: string
+ }): void {
+ const current = this.captureAuthEnvironmentBinding()
+ if (
+ current.environmentKey !== binding.environmentKey ||
+ current.restBaseUrl !== binding.restBaseUrl
+ ) {
+ throw new Error('stale_runtime_environment')
+ }
+ }
+
getChatDeletionFenceAuthority(): {
authorityScope: ChatAuthorityScope
sessionGeneration: number
@@ -1195,11 +1230,19 @@ export class AppService {
}
}
- private async runWithTeamContext<T>(
+ private runWithTeamContext<T>(
teamId: string | null | undefined,
operation: (sessionToken: string) => Promise<T>
): Promise<T> {
const targetTeamId = String(teamId || '').trim()
+ const run = () => this.runWithTeamContextInTeamQueue(targetTeamId, operation)
+ return targetTeamId ? this.withNativeAuthEnvironmentCommit(run) : run()
+ }
+
+ private async runWithTeamContextInTeamQueue<T>(
+ targetTeamId: string,
+ operation: (sessionToken: string) => Promise<T>
+ ): Promise<T> {
if (!targetTeamId) {
await this.teamContextQueue.catch(() => undefined)
return operation(this.requireSessionToken())
@@ -1494,87 +1537,120 @@ export class AppService {
}
private async restoreSavedSessionOnce(options: { runLaunchMaintenance?: boolean } = {}) {
- if (this.logoutInProgress) return { authenticated: false, me: null }
- const restoreGeneration = this.sessionGeneration
- hydrateDesktopRuntimeConfig()
- const envKey = getActiveEnvKey()
- const legacyEnvKeys = getActiveLegacyEnvKeys()
- this.savedSessionRestoreAttemptedEnvKey = envKey
- this.savedSessionRestoreAttemptedAtMs = Date.now()
- let token: string | null
- try {
- token = await this.tokenStore.getSessionToken(envKey, { legacyEnvKeys })
- } catch (error) {
- console.warn('[AppService] Failed to read the saved session token:', error)
- if (
- this.sessionGeneration === restoreGeneration &&
- (this.sessionToken !== null || this.me !== null)
- ) {
- this.clearAuthenticatedSessionState()
+ const reservation = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (this.logoutInProgress) return null
+ hydrateDesktopRuntimeConfig()
+ const environment = this.captureAuthEnvironmentBinding()
+ const legacyEnvKeys = getActiveLegacyEnvKeys()
+ this.savedSessionRestoreAttemptedEnvKey = environment.environmentKey
+ this.savedSessionRestoreAttemptedAtMs = Date.now()
+ let token: string | null = null
+ let tokenReadFailed = false
+ let tokenReadError: unknown
+ try {
+ // getSessionToken can migrate a legacy token into a new durable slot,
+ // so keep the native owner until its read/migration has settled.
+ token = await this.tokenStore.getSessionToken(environment.environmentKey, {
+ legacyEnvKeys,
+ })
+ } catch (error) {
+ tokenReadFailed = true
+ tokenReadError = error
+ }
+ return {
+ sessionGeneration: this.sessionGeneration,
+ environment,
+ legacyEnvKeys,
+ token,
+ tokenReadFailed,
+ tokenReadError,
}
- return { authenticated: false, me: null }
+ })
+ if (!reservation) return { authenticated: false, me: null }
+
+ const ownsRestore = () =>
+ this.sessionGeneration === reservation.sessionGeneration &&
+ !this.logoutInProgress &&
+ getActiveEnvKey() === reservation.environment.environmentKey &&
+ normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl) ===
+ reservation.environment.restBaseUrl
+ const currentSession = () => ({
+ authenticated: Boolean(this.sessionToken && this.me),
+ me: this.me,
+ })
+
+ if (reservation.tokenReadFailed) {
+ console.warn(
+ '[AppService] Failed to read the saved session token:',
+ reservation.tokenReadError
+ )
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ if (!ownsRestore()) return currentSession()
+ if (this.sessionToken !== null || this.me !== null) this.clearAuthenticatedSessionState()
+ return { authenticated: false, me: null }
+ })
}
- if (this.logoutInProgress) return { authenticated: false, me: null }
+
+ const token = reservation.token
if (!token) {
- if (
- this.sessionGeneration === restoreGeneration &&
- (this.sessionToken !== null || this.me !== null)
- ) {
- this.clearAuthenticatedSessionState()
- }
- return { authenticated: false, me: null }
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ if (!ownsRestore()) return currentSession()
+ if (this.sessionToken !== null || this.me !== null) this.clearAuthenticatedSessionState()
+ return { authenticated: false, me: null }
+ })
}
- if (this.sessionGeneration !== restoreGeneration) {
- return { authenticated: Boolean(this.sessionToken && this.me), me: this.me }
+ let getMeRequest: ReturnType<AuthClient['getMe']> | undefined
+ const requestStarted = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (!ownsRestore()) return false
+ getMeRequest = this.authClient.getMe(token)
+ return true
+ })
+ if (!requestStarted || !getMeRequest) {
+ return this.withNativeAuthEnvironmentCommit(async () => currentSession())
}
- this.sessionToken = token
+
try {
- const restoredMe = await this.authClient.getMe(token)
- if (this.sessionGeneration !== restoreGeneration || this.sessionToken !== token) {
- return { authenticated: Boolean(this.sessionToken && this.me), me: this.me }
- }
- await bindChatStoreForUser(restoredMe.id, envKey, {
- legacyEnvKeys,
- teamId: restoredMe.teamId,
+ const restoredMe = await getMeRequest
+ const session = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (!ownsRestore()) return currentSession()
+ this.sessionToken = token
+ await bindChatStoreForUser(restoredMe.id, reservation.environment.environmentKey, {
+ legacyEnvKeys: reservation.legacyEnvKeys,
+ teamId: restoredMe.teamId,
+ })
+ this.me = restoredMe
+ this.updateEntityChangeSessionToken(token)
+ this.restartEntityChangeStreamForSessionReplacement()
+ this.accessCatalog = null
+ this.teamDirectoryCache = null
+ this.workflowApprovalTeamById.clear()
+ this.workflowTeamByKey.clear()
+ // Persisted uploads are never auto-resumed. The active owner/team/env can
+ // list only its own scoped records and must explicitly resume one, at
+ // which point the record is rebound to this newly activated auth epoch.
+ this.activateGfsAuthScope()
+ return { authenticated: true, me: restoredMe }
})
- if (this.sessionGeneration !== restoreGeneration || this.sessionToken !== token) {
- if (this.me) {
- await this.bindCurrentChatStore(this.me.id)
- } else {
- unbindChatStore()
- }
- return { authenticated: Boolean(this.sessionToken && this.me), me: this.me }
- }
- this.me = restoredMe
- this.updateEntityChangeSessionToken(token)
- this.restartEntityChangeStreamForSessionReplacement()
- this.accessCatalog = null
- this.teamDirectoryCache = null
- this.workflowApprovalTeamById.clear()
- this.workflowTeamByKey.clear()
- // Persisted uploads are never auto-resumed. The active owner/team/env can
- // list only its own scoped records and must explicitly resume one, at
- // which point the record is rebound to this newly activated auth epoch.
- this.activateGfsAuthScope()
- // Launch-time sandbox-ui partition GC. Fire-and-forget: a failure
- // here must not block the user from logging in. Any network or fs
- // error is logged inside the module.
- if (options.runLaunchMaintenance) {
+ if (session.authenticated && options.runLaunchMaintenance) {
+ // Launch-time sandbox-ui partition GC is best-effort and must not delay
+ // publishing the saved session.
void this.runSandboxUiPartitionGcSafely()
}
- return { authenticated: true, me: this.me }
+ return session
} catch (error) {
- if (this.sessionGeneration !== restoreGeneration || this.sessionToken !== token) {
- return { authenticated: Boolean(this.sessionToken && this.me), me: this.me }
- }
- this.clearAuthenticatedSessionState()
- if (AppService.isRejectedStoredSessionError(error)) {
- await this.tokenStore.clearSessionToken(envKey, { legacyEnvKeys })
- } else {
- console.warn('[AppService] Saved session restore failed; keeping token for retry:', error)
- }
- return { authenticated: false, me: null }
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ if (!ownsRestore()) return currentSession()
+ this.clearAuthenticatedSessionState()
+ if (AppService.isRejectedStoredSessionError(error)) {
+ await this.tokenStore.clearSessionToken(reservation.environment.environmentKey, {
+ legacyEnvKeys: reservation.legacyEnvKeys,
+ })
+ } else {
+ console.warn('[AppService] Saved session restore failed; keeping token for retry:', error)
+ }
+ return { authenticated: false, me: null }
+ })
}
}
@@ -1660,13 +1736,26 @@ export class AppService {
password: string,
expectedSessionGeneration: number
): Promise<SessionState> {
- this.assertSessionGeneration(expectedSessionGeneration)
- const result = await this.authClient.passwordLogin(email, password)
- return this.installAuthenticatedLogin(result, expectedSessionGeneration)
+ let loginRequest: ReturnType<AuthClient['passwordLogin']> | undefined
+ const binding = await this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(expectedSessionGeneration)
+ const currentBinding = this.captureAuthEnvironmentBinding()
+ loginRequest = this.authClient.passwordLogin(email, password)
+ return currentBinding
+ })
+ if (!loginRequest) throw new Error('password login request was not dispatched')
+ const result = await loginRequest
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(expectedSessionGeneration)
+ this.assertAuthEnvironmentBinding(binding)
+ return this.installAuthenticatedLogin(result, expectedSessionGeneration)
+ })
}
async getDependenciesHealth(): Promise<DependencyHealth> {
- await this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
+ await this.withNativeAuthEnvironmentCommit(() =>
+ this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
+ )
const [externalHealth, rpcHealth] = await Promise.allSettled([
this.authClient.health(),
this.rpcClient.health(),
@@ -1719,6 +1808,8 @@ export class AppService {
const oldEnvKey = getActiveEnvKey()
const oldBaseUrl = normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
const oldLegacyEnvKeys = getActiveLegacyEnvKeys()
+ const oldSessionToken = this.sessionToken
+ const oldMe = this.me
const releasePrewarm = this.beginPrewarmAuthTransition()
try {
const hadAuthenticatedScope = Boolean(this.sessionToken && this.me)
@@ -1734,14 +1825,37 @@ export class AppService {
await operation()
this.assertSessionGeneration(transitionGeneration)
} catch (error) {
- if (this.sessionGeneration !== transitionGeneration) {
- throw new Error('stale_session_generation')
- }
const environmentUnchanged =
getActiveEnvKey() === oldEnvKey &&
normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl) === oldBaseUrl
- if (!environmentUnchanged) this.clearAuthenticatedSessionState()
- else if (hadAuthenticatedScope && this.sessionToken && this.me) this.activateGfsAuthScope()
+ if (this.sessionGeneration !== transitionGeneration) {
+ if (!environmentUnchanged) {
+ this.clearAuthenticatedSessionState()
+ await this.tokenStore
+ .clearSessionToken(oldEnvKey, { legacyEnvKeys: oldLegacyEnvKeys })
+ .catch(() => undefined)
+ } else if (this.sessionToken === oldSessionToken && this.me === oldMe) {
+ this.clearAuthenticatedSessionState()
+ await this.tokenStore
+ .clearSessionToken(oldEnvKey, { legacyEnvKeys: oldLegacyEnvKeys })
+ .catch(() => undefined)
+ }
+ throw new Error('stale_session_generation')
+ }
+ if (!environmentUnchanged) {
+ this.clearAuthenticatedSessionState()
+ await this.tokenStore
+ .clearSessionToken(oldEnvKey, { legacyEnvKeys: oldLegacyEnvKeys })
+ .catch(() => undefined)
+ } else if (
+ hadAuthenticatedScope &&
+ this.sessionToken === oldSessionToken &&
+ this.me === oldMe &&
+ this.sessionToken &&
+ this.me
+ ) {
+ this.activateGfsAuthScope()
+ }
throw error
}
const boundaryChanged =
@@ -1787,67 +1901,86 @@ export class AppService {
optionId: string,
expectedSessionGeneration?: number
): Promise<DesktopRuntimeConfigHandoffSelection> {
- if (expectedSessionGeneration !== undefined) {
- this.assertSessionGeneration(expectedSessionGeneration)
- }
- const state = getDesktopRuntimeConfigState()
- const selected = state.options.find(option => option.id === String(optionId || '').trim())
- const nextEnvKey = selected
- ? resolveEnvKey(selected.externalRestApiBaseUrl, selected.rpcProxyBaseUrl)
- : null
- const sameUploadBoundary =
- nextEnvKey === state.envKey &&
- selected !== undefined &&
- normalizeDesktopUploadBaseUrl(selected.externalRestApiBaseUrl) ===
- normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
- if (sameUploadBoundary) {
- this.sessionGeneration += 1
- const transitionGeneration = this.sessionGeneration
- await selectDesktopRuntimeConfigOption(optionId)
- this.assertSessionGeneration(transitionGeneration)
- this.sessionGeneration += 1
- } else {
- await this.applyRuntimeEnvironmentChange(
- () => selectDesktopRuntimeConfigOption(optionId),
- expectedSessionGeneration
- )
- }
- return {
- runtimeConfigState: getDesktopRuntimeConfigState(),
- sessionGeneration: this.sessionGeneration,
- }
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ if (expectedSessionGeneration !== undefined) {
+ this.assertSessionGeneration(expectedSessionGeneration)
+ }
+ const state = getDesktopRuntimeConfigState()
+ const selected = state.options.find(option => option.id === String(optionId || '').trim())
+ const nextEnvKey = selected
+ ? resolveEnvKey(selected.externalRestApiBaseUrl, selected.rpcProxyBaseUrl)
+ : null
+ const sameUploadBoundary =
+ nextEnvKey === state.envKey &&
+ selected !== undefined &&
+ normalizeDesktopUploadBaseUrl(selected.externalRestApiBaseUrl) ===
+ normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
+ if (sameUploadBoundary) {
+ this.sessionGeneration += 1
+ const transitionGeneration = this.sessionGeneration
+ await selectDesktopRuntimeConfigOption(optionId)
+ this.assertSessionGeneration(transitionGeneration)
+ this.sessionGeneration += 1
+ } else {
+ await this.applyRuntimeEnvironmentChange(
+ () => selectDesktopRuntimeConfigOption(optionId),
+ expectedSessionGeneration
+ )
+ }
+ return {
+ runtimeConfigState: getDesktopRuntimeConfigState(),
+ sessionGeneration: this.sessionGeneration,
+ }
+ })
}
async clearRuntimeConfigSelection() {
- await this.applyRuntimeEnvironmentChange(clearDesktopRuntimeConfigSelection)
- return getDesktopRuntimeConfigState()
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ await this.applyRuntimeEnvironmentChange(clearDesktopRuntimeConfigSelection)
+ return getDesktopRuntimeConfigState()
+ })
}
async saveRuntimeConfig(next: DesktopRuntimeConfig) {
- const nextEnvKey = resolveEnvKey(next.externalRestApiBaseUrl, next.rpcProxyBaseUrl || '')
- const sameUploadBoundary =
- nextEnvKey === getActiveEnvKey() &&
- normalizeDesktopUploadBaseUrl(next.externalRestApiBaseUrl) ===
- normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
- if (sameUploadBoundary) await saveDesktopRuntimeConfig(next)
- else await this.applyRuntimeEnvironmentChange(() => saveDesktopRuntimeConfig(next))
- await this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
- return getDesktopRuntimeConfigState()
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ const nextEnvKey = resolveEnvKey(next.externalRestApiBaseUrl, next.rpcProxyBaseUrl || '')
+ const sameUploadBoundary =
+ nextEnvKey === getActiveEnvKey() &&
+ normalizeDesktopUploadBaseUrl(next.externalRestApiBaseUrl) ===
+ normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl)
+ if (sameUploadBoundary) await saveDesktopRuntimeConfig(next)
+ else await this.applyRuntimeEnvironmentChange(() => saveDesktopRuntimeConfig(next))
+ await this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
+ return getDesktopRuntimeConfigState()
+ })
}
async deleteRuntimeConfig(optionId: string) {
- const state = getDesktopRuntimeConfigState()
- if (state.activeOptionId === String(optionId || '').trim())
- await this.applyRuntimeEnvironmentChange(() => deleteDesktopRuntimeConfigOption(optionId))
- else await deleteDesktopRuntimeConfigOption(optionId)
- return getDesktopRuntimeConfigState()
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ const state = getDesktopRuntimeConfigState()
+ if (state.activeOptionId === String(optionId || '').trim())
+ await this.applyRuntimeEnvironmentChange(() => deleteDesktopRuntimeConfigOption(optionId))
+ else await deleteDesktopRuntimeConfigOption(optionId)
+ return getDesktopRuntimeConfigState()
+ })
}
async googleLogin(idToken: string): Promise<SessionState> {
- if (this.logoutInProgress) throw new Error('auth_transition_in_progress')
- const loginGeneration = ++this.sessionGeneration
- const result = await this.authClient.googleLogin(idToken)
- return this.installAuthenticatedLogin(result, loginGeneration)
+ let loginRequest: ReturnType<AuthClient['googleLogin']> | undefined
+ const attempt = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (this.logoutInProgress) throw new Error('auth_transition_in_progress')
+ const sessionGeneration = ++this.sessionGeneration
+ const binding = this.captureAuthEnvironmentBinding()
+ loginRequest = this.authClient.googleLogin(idToken)
+ return { sessionGeneration, binding }
+ })
+ if (!loginRequest) throw new Error('Google login request was not dispatched')
+ const result = await loginRequest
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(attempt.sessionGeneration)
+ this.assertAuthEnvironmentBinding(attempt.binding)
+ return this.installAuthenticatedLogin(result, attempt.sessionGeneration)
+ })
}
private async openProfileDesktopSetup(email: string): Promise<{
@@ -1961,29 +2094,47 @@ export class AppService {
}
async completeDesktopSetup(email: string, authorizationToken: string) {
- const setupGeneration = ++this.sessionGeneration
const normalizedEmail = email.trim().toLowerCase()
if (!normalizedEmail || !authorizationToken.trim()) {
throw new Error('email and authorization token are required')
}
- try {
- const activation = await this.memberRegistrationServiceClient.completeDesktopSetup(
+ const setupGeneration = await this.withNativeAuthEnvironmentCommit(
+ async () => ++this.sessionGeneration
+ )
+ let setupRequest:
+ | ReturnType<MemberRegistrationServiceClient['completeDesktopSetup']>
+ | undefined
+ await this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(setupGeneration)
+ setupRequest = this.memberRegistrationServiceClient.completeDesktopSetup(
normalizedEmail,
authorizationToken
)
+ })
+ if (!setupRequest) throw new Error('desktop setup request was not dispatched')
+
+ try {
+ const activation = await setupRequest
if (activation.email.trim().toLowerCase() !== normalizedEmail) {
throw new Error('desktop setup configuration mismatch')
}
- this.assertSessionGeneration(setupGeneration)
- await saveDesktopRuntimeConfig({
- externalRestApiBaseUrl: activation.externalRestApiBaseUrl,
- rpcProxyBaseUrl: '',
- appName: activation.appName,
+ const committedGeneration = await this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(setupGeneration)
+ await saveDesktopRuntimeConfig({
+ externalRestApiBaseUrl: activation.externalRestApiBaseUrl,
+ rpcProxyBaseUrl: '',
+ appName: activation.appName,
+ })
+ this.assertSessionGeneration(setupGeneration)
+ this.sessionGeneration += 1
+ return this.sessionGeneration
+ })
+ await this.withNativeAuthEnvironmentCommit(async () => {
+ if (this.sessionGeneration === committedGeneration) {
+ await this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
+ }
})
- this.assertSessionGeneration(setupGeneration)
- this.sessionGeneration += 1
- await this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
return getDesktopRuntimeConfigState()
} catch (error) {
if (error instanceof ApiError && error.status === 404) {
@@ -1994,16 +2145,21 @@ export class AppService {
}
async passwordLogin(email: string, password: string): Promise<PasswordLoginResult> {
- if (this.logoutInProgress) throw new Error('auth_transition_in_progress')
- const loginGeneration = ++this.sessionGeneration
- hydrateDesktopRuntimeConfig()
- const normalizedEmail = email.trim().toLowerCase()
- if (!isDesktopRuntimeConfigured()) {
- throw new Error('desktop_setup_required')
- }
- await this.resolveRuntimeConfigIfNeeded()
+ const loginGeneration = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (this.logoutInProgress) throw new Error('auth_transition_in_progress')
+ const generation = ++this.sessionGeneration
+ hydrateDesktopRuntimeConfig()
+ if (!isDesktopRuntimeConfigured()) {
+ throw new Error('desktop_setup_required')
+ }
+ return generation
+ })
+ await this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(loginGeneration)
+ await this.resolveRuntimeConfigIfNeeded()
+ })
this.assertSessionGeneration(loginGeneration)
-
+ const normalizedEmail = email.trim().toLowerCase()
return this.completePasswordLogin(normalizedEmail, password, loginGeneration)
}
@@ -2154,31 +2310,33 @@ export class AppService {
}
async logout(): Promise<number> {
- this.logoutInProgress = true
- this.sessionGeneration += 1
- const logoutGeneration = this.sessionGeneration
- const releasePrewarm = this.beginPrewarmAuthTransition()
- try {
- const envKey = getActiveEnvKey()
- const legacyEnvKeys = getActiveLegacyEnvKeys()
- await this.suspendDesktopGfsUploadsForAuthBoundary()
- this.assertSessionGeneration(logoutGeneration)
+ return this.withNativeAuthEnvironmentCommit(async () => {
+ this.logoutInProgress = true
+ this.sessionGeneration += 1
+ const logoutGeneration = this.sessionGeneration
+ const releasePrewarm = this.beginPrewarmAuthTransition()
try {
- await this.tokenStore.clearSessionToken(envKey, { legacyEnvKeys })
+ const envKey = getActiveEnvKey()
+ const legacyEnvKeys = getActiveLegacyEnvKeys()
+ await this.suspendDesktopGfsUploadsForAuthBoundary()
this.assertSessionGeneration(logoutGeneration)
- } catch (error) {
- if (this.sessionToken && this.me) this.activateGfsAuthScope()
- throw error
+ try {
+ await this.tokenStore.clearSessionToken(envKey, { legacyEnvKeys })
+ this.assertSessionGeneration(logoutGeneration)
+ } catch (error) {
+ if (this.sessionToken && this.me) this.activateGfsAuthScope()
+ throw error
+ }
+ this.clearAuthenticatedSessionState()
+ // Grants survive logout (they are keyed by userId), but every cached SDK
+ // result must not: the next user of this machine gets nothing of this one's.
+ tryGetPluginSdkRuntime()?.notifySessionChanged(false)
+ return this.sessionGeneration
+ } finally {
+ releasePrewarm()
+ this.logoutInProgress = false
}
- this.clearAuthenticatedSessionState()
- // Grants survive logout (they are keyed by userId), but every cached SDK
- // result must not: the next user of this machine gets nothing of this one's.
- tryGetPluginSdkRuntime()?.notifySessionChanged(false)
- return this.sessionGeneration
- } finally {
- releasePrewarm()
- this.logoutInProgress = false
- }
+ })
}
/** Resolve a gfs:// URI to its current resource via the API (no local mirror). */
@@ -3512,6 +3670,10 @@ export class AppService {
}
async switchTeam(teamId: string): Promise<SessionState> {
+ return this.withNativeAuthEnvironmentCommit(() => this.switchTeamWithCommitOwner(teamId))
+ }
+
+ private async switchTeamWithCommitOwner(teamId: string): Promise<SessionState> {
const targetTeamId = String(teamId || '').trim()
if (!targetTeamId) throw new Error('teamId is required')
// Explicit team changes fence opportunistic wake immediately. A failed