Skip to content

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

TypeScript

fix(desktop-app): rp978-n02 fence setup against restored session

Josué1c89202347f4
diff --git a/desktop-app/src/__tests__/appService.setupRestoreRace.test.ts b/desktop-app/src/__tests__/appService.setupRestoreRace.test.ts
new file mode 100644
index 000000000..e3d554b59
--- /dev/null
+++ b/desktop-app/src/__tests__/appService.setupRestoreRace.test.ts
@@ -0,0 +1,133 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import {
+ cleanupNativeCommitTestHarness,
+ createNativeCommitTestHarness,
+ deferred,
+} from '../../testSupport/appService.nativeCommitTestHarness.js'
+
+afterEach(cleanupNativeCommitTestHarness)
+
+type SetupActivation = {
+ valid: boolean
+ email: string
+ externalRestApiBaseUrl: string
+ rpcProxyBaseUrl: string
+ appName: string
+}
+
+describe('AppService setup and saved-session ownership', () => {
+ 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>()
+ const setupStarted = deferred<void>()
+ const user = {
+ id: 'user-a',
+ email: '[email protected]',
+ name: 'User A',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ }
+ const setupRequest = vi.fn(() => {
+ setupStarted.resolve()
+ return setupActivation.promise
+ })
+ const app = service as unknown as {
+ authClient: { getMe: ReturnType<typeof vi.fn> }
+ tokenStore: { getSessionToken: ReturnType<typeof vi.fn> }
+ memberRegistrationServiceClient: { completeDesktopSetup: typeof setupRequest }
+ completeDesktopSetup: (email: string, authorizationToken: string) => Promise<unknown>
+ getSessionState: () => Promise<unknown>
+ getSessionGeneration: () => number
+ resolveRuntimeConfigIfNeeded: () => Promise<void>
+ sessionToken: string | null
+ me: { id: string; teamId: string } | null
+ gfsScopeIdentity: { ownerId: string; teamId: string | null; baseUrl: string } | null
+ }
+ app.authClient = { getMe: vi.fn().mockResolvedValue(user) }
+ app.tokenStore.getSessionToken.mockResolvedValue('synthetic-session-a')
+ app.memberRegistrationServiceClient = { completeDesktopSetup: setupRequest }
+ app.resolveRuntimeConfigIfNeeded = vi.fn().mockResolvedValue(undefined)
+
+ const setup = app.completeDesktopSetup('[email protected]', 'synthetic-setup-token')
+ await setupStarted.promise
+ await expect(app.getSessionState()).resolves.toMatchObject({ authenticated: true, me: user })
+ const restoredGeneration = app.getSessionGeneration()
+
+ setupActivation.resolve({
+ valid: true,
+ email: '[email protected]',
+ externalRestApiBaseUrl: restB,
+ rpcProxyBaseUrl: '',
+ appName: 'Environment B',
+ })
+
+ await expect(setup).rejects.toThrow('desktop_setup_requires_signout')
+ expect(setupRequest).toHaveBeenCalledOnce()
+ expect(runtimeConfig.config.externalRestApiBaseUrl).toBe(restA)
+ expect(app.getSessionGeneration()).toBe(restoredGeneration)
+ expect(app.sessionToken).toBe('synthetic-session-a')
+ expect(app.me).toMatchObject({ id: 'user-a', teamId: 'team-a' })
+ expect(app.gfsScopeIdentity).toMatchObject({
+ ownerId: 'user-a',
+ teamId: 'team-a',
+ baseUrl: restA,
+ })
+ })
+
+ it('discards a saved-session response superseded by a completed setup', async () => {
+ const { service, runtimeConfig, restB } = await createNativeCommitTestHarness()
+ const setupActivation = deferred<SetupActivation>()
+ const setupStarted = deferred<void>()
+ const user = {
+ id: 'user-a',
+ email: '[email protected]',
+ name: 'User A',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ }
+ const restoredUser = deferred<typeof user>()
+ const setupRequest = vi.fn(() => {
+ setupStarted.resolve()
+ return setupActivation.promise
+ })
+ const app = service as unknown as {
+ authClient: { getMe: ReturnType<typeof vi.fn> }
+ tokenStore: { getSessionToken: ReturnType<typeof vi.fn> }
+ memberRegistrationServiceClient: { completeDesktopSetup: typeof setupRequest }
+ completeDesktopSetup: (email: string, authorizationToken: string) => Promise<unknown>
+ getSessionState: () => Promise<unknown>
+ resolveRuntimeConfigIfNeeded: () => Promise<void>
+ sessionToken: string | null
+ me: { id: string; teamId: string } | null
+ }
+ app.authClient = {
+ getMe: vi.fn().mockReturnValue(restoredUser.promise),
+ }
+ app.tokenStore.getSessionToken.mockResolvedValue('synthetic-session-a')
+ app.memberRegistrationServiceClient = { completeDesktopSetup: setupRequest }
+ app.resolveRuntimeConfigIfNeeded = vi.fn().mockResolvedValue(undefined)
+
+ const setup = app.completeDesktopSetup('[email protected]', 'synthetic-setup-token')
+ await setupStarted.promise
+ const restore = app.getSessionState()
+
+ setupActivation.resolve({
+ valid: true,
+ email: '[email protected]',
+ externalRestApiBaseUrl: restB,
+ rpcProxyBaseUrl: '',
+ appName: 'Environment B',
+ })
+ await setup
+ restoredUser.resolve(user)
+
+ await expect(restore).resolves.toEqual({ authenticated: false, me: null })
+ expect(runtimeConfig.config.externalRestApiBaseUrl).toBe(restB)
+ expect(app.sessionToken).toBeNull()
+ expect(app.me).toBeNull()
+ })
+})
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index a56ec22b6..1581a8de4 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -1645,6 +1645,10 @@ export class AppService {
// 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()
+ // A successful restore publishes a new auth owner. Invalidate pending
+ // setup and other work prepared against the signed-out generation only
+ // after the restored session and its GFS scope are coherent.
+ this.sessionGeneration += 1
return { authenticated: true, me: restoredMe }
})
if (session.authenticated && options.runLaunchMaintenance) {
@@ -2136,6 +2140,9 @@ export class AppService {
}
const committedGeneration = await this.withNativeAuthEnvironmentCommit(async () => {
+ if (this.sessionToken && this.me) {
+ throw new Error('desktop_setup_requires_signout')
+ }
this.assertSessionGeneration(setupGeneration)
await saveDesktopRuntimeConfig({
externalRestApiBaseUrl: activation.externalRestApiBaseUrl,