Skip to content

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

TypeScript

fix(desktop): preserve restores across RPC discovery (R13-M2)

Josuécc7e8b248bf9
diff --git a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts
index 72357ca0a..c99d1a7ae 100644
--- a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts
+++ b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts
@@ -58,6 +58,77 @@ describe('AppService deliberate team transition ownership', () => {
await expect(logout).resolves.toBeTypeOf('number')
})
+ it('keeps a saved-session restore when RPC discovery completes before getMe', async () => {
+ const { service, runtimeConfig, restA } = await createNativeCommitTestHarness()
+ await runtimeConfig.saveDesktopRuntimeConfig({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: '',
+ appName: 'Environment A',
+ })
+ const discovery = deferred<{
+ externalRestApiBaseUrl: string
+ rpcProxyBaseUrl: string
+ appName: string
+ }>()
+ const discoveryStarted = deferred<void>()
+ const getMeResponse = deferred<{
+ id: string
+ email: string
+ name: string
+ picture: null
+ teamId: string
+ teamName: string
+ role: string
+ }>()
+ const getMeStarted = deferred<void>()
+ const me = {
+ id: 'user-a',
+ email: '[email protected]',
+ name: 'User A',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ }
+ const app = service as unknown as {
+ authClient: unknown
+ rpcClient: unknown
+ tokenStore: { getSessionToken: ReturnType<typeof vi.fn> }
+ getDependenciesHealth(): Promise<unknown>
+ }
+ app.tokenStore.getSessionToken.mockResolvedValue('saved-session-a')
+ const getMe = vi.fn(() => {
+ getMeStarted.resolve()
+ return getMeResponse.promise
+ })
+ const getDesktopEnvironment = vi.fn(() => {
+ discoveryStarted.resolve()
+ return discovery.promise
+ })
+ app.authClient = {
+ getDesktopEnvironment,
+ getMe,
+ health: vi.fn().mockResolvedValue({ status: 'ok' }),
+ }
+ app.rpcClient = { health: vi.fn().mockResolvedValue({ status: 'ok' }) }
+
+ const healthRequest = app.getDependenciesHealth()
+ await discoveryStarted.promise
+ const restore = service.getSessionState()
+ await new Promise(resolve => setTimeout(resolve, 0))
+ expect(getMe).not.toHaveBeenCalled()
+ discovery.resolve({
+ externalRestApiBaseUrl: restA,
+ rpcProxyBaseUrl: 'https://rpc-discovered.example.test',
+ appName: 'Environment A',
+ })
+ await healthRequest
+ await getMeStarted.promise
+ getMeResponse.resolve(me)
+
+ await expect(restore).resolves.toEqual({ authenticated: true, me })
+ })
+
it('rejects a stale handoff generation after a public login advances the session', async () => {
const { service, runtimeConfig, optionA } = await createNativeCommitTestHarness()
const app = service as unknown as {
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index 3836b702d..5b267fa6d 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -1086,21 +1086,28 @@ export class AppService {
return result
}
- private captureAuthEnvironmentBinding(): { environmentKey: string; restBaseUrl: string } {
+ private captureAuthEnvironmentBinding(): {
+ environmentKey: string
+ profileId: string | null
+ restBaseUrl: string
+ } {
+ const runtimeConfig = getDesktopRuntimeConfigState()
return {
environmentKey: getActiveEnvKey(),
- restBaseUrl: normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl),
+ profileId: runtimeConfig.activeOptionId,
+ restBaseUrl: canonicalizeDesktopRestEndpoint(config.externalRestApiBaseUrl),
}
}
private assertAuthEnvironmentBinding(binding: {
environmentKey: string
+ profileId: string | null
restBaseUrl: string
}): void {
const current = this.captureAuthEnvironmentBinding()
if (
- current.environmentKey !== binding.environmentKey ||
- current.restBaseUrl !== binding.restBaseUrl
+ current.profileId !== binding.profileId ||
+ !sameDesktopRestEndpoint(current.restBaseUrl, binding.restBaseUrl)
) {
throw new Error('stale_runtime_environment')
}
@@ -1583,7 +1590,10 @@ export class AppService {
if (this.restoreSavedSessionInFlight) {
return await this.restoreSavedSessionInFlight
}
- const restore = this.restoreSavedSessionOnce(options)
+ const restore = (async () => {
+ await this.resolveRuntimeConfigIfNeeded().catch(() => undefined)
+ return this.restoreSavedSessionOnce(options)
+ })()
this.restoreSavedSessionInFlight = restore
try {
return await restore
@@ -1631,9 +1641,14 @@ export class AppService {
const ownsRestore = () =>
this.sessionGeneration === reservation.sessionGeneration &&
this.interactiveLoginAttempts === 0 &&
- getActiveEnvKey() === reservation.environment.environmentKey &&
- normalizeDesktopUploadBaseUrl(config.externalRestApiBaseUrl) ===
- reservation.environment.restBaseUrl
+ (() => {
+ try {
+ this.assertAuthEnvironmentBinding(reservation.environment)
+ return true
+ } catch {
+ return false
+ }
+ })()
const currentSession = () => ({
authenticated: Boolean(this.sessionToken && this.me),
me: this.me,
@@ -1671,11 +1686,13 @@ export class AppService {
const session = await this.withNativeAuthEnvironmentCommit(async () => {
if (!ownsRestore()) return currentSession()
this.sessionToken = token
- await bindChatStoreForUser(restoredMe.id, reservation.environment.environmentKey, {
- legacyEnvKeys: reservation.legacyEnvKeys,
+ const environmentKey = getActiveEnvKey()
+ await bindChatStoreForUser(restoredMe.id, environmentKey, {
+ legacyEnvKeys: getActiveLegacyEnvKeys(),
teamId: restoredMe.teamId,
})
this.me = restoredMe
+ this.savedSessionRestoreAttemptedEnvKey = environmentKey
this.updateEntityChangeSessionToken(token)
this.restartEntityChangeStreamForSessionReplacement()
this.accessCatalog = null
@@ -1703,8 +1720,8 @@ export class AppService {
if (!ownsRestore()) return currentSession()
this.clearAuthenticatedSessionState()
if (AppService.isRejectedStoredSessionError(error)) {
- await this.tokenStore.clearSessionToken(reservation.environment.environmentKey, {
- legacyEnvKeys: reservation.legacyEnvKeys,
+ await this.tokenStore.clearSessionToken(getActiveEnvKey(), {
+ legacyEnvKeys: getActiveLegacyEnvKeys(),
})
}
return { authenticated: false, me: null }