Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): prioritize explicit login over restore (R12-M1)
Josuéaa4283e2bd59
diff --git a/desktop-app/src/__tests__/appService.setupRestoreRace.test.ts b/desktop-app/src/__tests__/appService.setupRestoreRace.test.tsindex e3d554b59..48f1f96b9 100644--- a/desktop-app/src/__tests__/appService.setupRestoreRace.test.ts+++ b/desktop-app/src/__tests__/appService.setupRestoreRace.test.ts@@ -4,6 +4,7 @@ import {createNativeCommitTestHarness,deferred,} from '../../testSupport/appService.nativeCommitTestHarness.js'+import type { SessionMe } from '../types.js'afterEach(cleanupNativeCommitTestHarness)@@ -16,6 +17,64 @@ type SetupActivation = {}describe('AppService setup and saved-session ownership', () => {+ it.each(['Google', 'password'] as const)(+ 'lets a pending %s login win over a background saved-session restore',+ async provider => {+ const { service } = await createNativeCommitTestHarness()+ const restoredUser = {+ id: 'user-a',+ email: '[email protected]',+ name: 'User A',+ picture: null,+ teamId: 'team-a',+ teamName: 'Team A',+ role: 'member',+ }+ const loginResponse = deferred<{ token: string; me: SessionMe }>()+ const loginUser = {+ id: 'user-b',+ email: '[email protected]',+ name: 'User B',+ picture: null,+ teamId: 'team-b',+ teamName: 'Team B',+ role: 'member',+ }+ const app = service as unknown as {+ authClient: {+ googleLogin: ReturnType<typeof vi.fn>+ passwordLogin: ReturnType<typeof vi.fn>+ getMe: ReturnType<typeof vi.fn>+ }+ tokenStore: { getSessionToken: ReturnType<typeof vi.fn> }+ sessionToken: string | null+ me: { id: string } | null+ }+ app.authClient = {+ googleLogin: vi.fn(() => loginResponse.promise),+ passwordLogin: vi.fn(() => loginResponse.promise),+ getMe: vi.fn().mockResolvedValue(restoredUser),+ }+ app.tokenStore.getSessionToken.mockResolvedValue('synthetic-saved-session-a')++ const login =+ provider === 'Google'+ ? service.googleLogin('synthetic-google-login-b')+ : service.passwordLogin('[email protected]', 'synthetic-password-b')+ await expect(service.getSessionState()).resolves.toEqual({ authenticated: false, me: null })+ expect(app.authClient.getMe).not.toHaveBeenCalled()++ loginResponse.resolve({ token: 'synthetic-login-session-b', me: loginUser })+ await expect(login).resolves.toMatchObject({ authenticated: true, me: loginUser })+ await expect(service.getSessionState()).resolves.toMatchObject({+ authenticated: true,+ me: loginUser,+ })+ expect(app.sessionToken).toBe('synthetic-login-session-b')+ expect(app.me).toMatchObject({ id: 'user-b' })+ }+ )+it('keeps a restored session when its saved-session commit beats pending setup', async () => {const { service, runtimeConfig, restA, restB } = await createNativeCommitTestHarness()const setupActivation = deferred<SetupActivation>()diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex 1581a8de4..73292ef0e 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -894,6 +894,7 @@ export class AppService {private restoreSavedSessionInFlight: Promise<SessionState> | null = nullprivate savedSessionRestoreAttemptedEnvKey: string | null = nullprivate savedSessionRestoreAttemptedAtMs = 0+ private interactiveLoginAttempts = 0private logoutInProgress = falseprivate nativeAuthEnvironmentCommitQueue: Promise<void> = Promise.resolve()private gfsAuthEpoch = 0@@ -1560,7 +1561,7 @@ export class AppService {private async restoreSavedSessionOnce(options: { runLaunchMaintenance?: boolean } = {}) {const reservation = await this.withNativeAuthEnvironmentCommit(async () => {- if (this.logoutInProgress) return null+ if (this.logoutInProgress || this.interactiveLoginAttempts > 0) return nullhydrateDesktopRuntimeConfig()const environment = this.captureAuthEnvironmentBinding()const legacyEnvKeys = getActiveLegacyEnvKeys()@@ -1585,11 +1586,17 @@ export class AppService {tokenReadFailed,}})- if (!reservation) return { authenticated: false, me: null }+ if (!reservation) {+ return this.withNativeAuthEnvironmentCommit(async () => ({+ authenticated: Boolean(this.sessionToken && this.me),+ me: this.me,+ }))+ }const ownsRestore = () =>this.sessionGeneration === reservation.sessionGeneration &&!this.logoutInProgress &&+ this.interactiveLoginAttempts === 0 &&getActiveEnvKey() === reservation.environment.environmentKey &&normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl) ===reservation.environment.restBaseUrl@@ -1984,20 +1991,31 @@ export class AppService {async googleLogin(idToken: string): Promise<SessionState> {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)- })+ let registered = false+ try {+ 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)+ this.interactiveLoginAttempts += 1+ registered = true+ return { sessionGeneration, binding }+ })+ if (!loginRequest) throw new Error('Google login request was not dispatched')+ const result = await loginRequest+ return await this.withNativeAuthEnvironmentCommit(async () => {+ this.assertSessionGeneration(attempt.sessionGeneration)+ this.assertAuthEnvironmentBinding(attempt.binding)+ return this.installAuthenticatedLogin(result, attempt.sessionGeneration)+ })+ } finally {+ if (registered) {+ await this.withNativeAuthEnvironmentCommit(async () => {+ this.interactiveLoginAttempts = Math.max(0, this.interactiveLoginAttempts - 1)+ })+ }+ }}private async openProfileDesktopSetup(email: string): Promise<{@@ -2168,22 +2186,33 @@ export class AppService {}async passwordLogin(email: string, password: string): Promise<PasswordLoginResult> {- 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 () => {+ let registered = false+ try {+ 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')+ }+ this.interactiveLoginAttempts += 1+ registered = true+ return generation+ })+ await this.withNativeAuthEnvironmentCommit(async () => {+ this.assertSessionGeneration(loginGeneration)+ await this.resolveRuntimeConfigIfNeeded()+ })this.assertSessionGeneration(loginGeneration)- await this.resolveRuntimeConfigIfNeeded()- })- this.assertSessionGeneration(loginGeneration)- const normalizedEmail = email.trim().toLowerCase()- return this.completePasswordLogin(normalizedEmail, password, loginGeneration)+ const normalizedEmail = email.trim().toLowerCase()+ return await this.completePasswordLogin(normalizedEmail, password, loginGeneration)+ } finally {+ if (registered) {+ await this.withNativeAuthEnvironmentCommit(async () => {+ this.interactiveLoginAttempts = Math.max(0, this.interactiveLoginAttempts - 1)+ })+ }+ }}/**