Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): persist RPC discovery across auth generation changes (R13-L2)
Josué36db81caee58
diff --git a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.tsindex 4400a35a4..4e8db900c 100644--- a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts+++ b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts@@ -201,6 +201,70 @@ describe('AppService deliberate team transition ownership', () => {await expect(login).resolves.toEqual({ authenticated: true, me })})+ it('persists RPC discovery after a saved-session restore advances the generation', async () => {+ const { service, runtimeConfig, restA } = await createNativeCommitTestHarness()+ const discovery = deferred<{+ externalRestApiBaseUrl: string+ rpcProxyBaseUrl: string+ appName: string+ }>()+ const discoveryStarted = 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>+ saveRuntimeConfig(config: {+ externalRestApiBaseUrl: string+ rpcProxyBaseUrl: string+ appName: string+ }): Promise<unknown>+ }+ app.tokenStore.getSessionToken.mockResolvedValue('saved-session-a')+ const getMe = vi.fn().mockResolvedValue(me)+ 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 save = app.saveRuntimeConfig({+ externalRestApiBaseUrl: restA,+ rpcProxyBaseUrl: '',+ appName: 'Environment A',+ })+ 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 save+ await expect(restore).resolves.toEqual({ authenticated: true, me })+ expect(getDesktopEnvironment).toHaveBeenCalledOnce()++ expect(runtimeConfig.getDesktopRuntimeConfigState().currentConfig.rpcProxyBaseUrl).toBe(+ 'https://rpc-discovered.example.test'+ )+ })+it('keeps the public session generation stable through transient team hops', async () => {const { service } = await createNativeCommitTestHarness()const meA = {@@ -268,7 +332,7 @@ describe('AppService deliberate team transition ownership', () => {expect(runtimeConfig.getDesktopRuntimeConfigState().activeOptionId).toBe(optionA.id)})- it('does not let REST discovery block logout or overwrite a later session revision', async () => {+ it('keeps REST discovery for the same profile when logout advances session generation', async () => {const { service, runtimeConfig, restA } = await createNativeCommitTestHarness()await runtimeConfig.saveDesktopRuntimeConfig({externalRestApiBaseUrl: restA,@@ -318,7 +382,10 @@ describe('AppService deliberate team transition ownership', () => {})await healthRequest- expect(runtimeConfig.getDesktopRuntimeConfigState().currentConfig.rpcProxyBaseUrl).toBe('')+ expect(app.sessionToken).toBeNull()+ expect(runtimeConfig.getDesktopRuntimeConfigState().currentConfig.rpcProxyBaseUrl).toBe(+ 'https://rpc-discovered.example.test'+ )})it('lets logout commit while a same-team request is pending and rejects its stale result', async () => {diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex a36ee2589..1fa1ad6d7 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -892,6 +892,7 @@ export class AppService {private teamDirectoryCache: TeamDirectoryResult | null = nullprivate teamContextQueue: Promise<void> = Promise.resolve()private restoreSavedSessionInFlight: Promise<SessionState> | null = null+ private runtimeConfigResolutionInFlight = new Map<string, Promise<void>>()private savedSessionRestoreAttemptedEnvKey: string | null = nullprivate savedSessionRestoreAttemptedAtMs = 0private interactiveLoginAttempts = 0@@ -2367,12 +2368,38 @@ export class AppService {return {externalRestApiBaseUrl: canonicalizeDesktopRestEndpoint(config.externalRestApiBaseUrl),appName: config.appName,- sessionGeneration: this.sessionGeneration,environmentBinding: this.captureAuthEnvironmentBinding(),}})if (!request) return+ const resolutionKey = JSON.stringify([+ request.environmentBinding.profileId,+ request.externalRestApiBaseUrl,+ ])+ const inFlight = this.runtimeConfigResolutionInFlight.get(resolutionKey)+ if (inFlight) return inFlight++ const resolution = this.resolveRuntimeConfigRequest(request)+ this.runtimeConfigResolutionInFlight.set(resolutionKey, resolution)+ try {+ await resolution+ } finally {+ if (this.runtimeConfigResolutionInFlight.get(resolutionKey) === resolution) {+ this.runtimeConfigResolutionInFlight.delete(resolutionKey)+ }+ }+ }++ private async resolveRuntimeConfigRequest(request: {+ externalRestApiBaseUrl: string+ appName: string+ environmentBinding: {+ environmentKey: string+ profileId: string | null+ restBaseUrl: string+ }+ }): Promise<void> {const discovered = await this.authClient.getDesktopEnvironment()if (!sameDesktopRestEndpoint(discovered.externalRestApiBaseUrl, request.externalRestApiBaseUrl)@@ -2380,7 +2407,6 @@ export class AppService {throw new Error('Desktop environment discovery returned a different REST endpoint')}await this.withNativeAuthEnvironmentCommit(async () => {- this.assertSessionGeneration(request.sessionGeneration)this.assertAuthEnvironmentBinding(request.environmentBinding)hydrateDesktopRuntimeConfig()if (!isDesktopRuntimeConfigured() || config.rpcProxyBaseUrl?.trim()) return