Skip to content

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

TypeScript

fix(desktop-app): move team lookup outside auth owner (R12-M5)

Josué852e74d35496
diff --git a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts
index 484062a42..81cce972b 100644
--- a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts
+++ b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts
@@ -132,6 +132,77 @@ describe('AppService deliberate team transition ownership', () => {
expect(app.sessionToken).toBeNull()
})
+ it('lets logout commit during teamless session scope discovery', async () => {
+ const { service } = await createNativeCommitTestHarness()
+ const meResponse = deferred<{
+ id: string
+ email: string
+ name: string
+ picture: null
+ teamId: string
+ teamName: string
+ role: string
+ }>()
+ const discoveryStarted = deferred<void>()
+ const logoutStarted = deferred<void>()
+ const app = service as unknown as {
+ sessionToken: string | null
+ me: {
+ id: string
+ email: string
+ name: string
+ picture: null
+ teamId: string | null
+ teamName: string | null
+ role: string
+ } | null
+ authClient: unknown
+ runWithTeamContext<T>(teamId: string, operation: (token: string) => Promise<T>): Promise<T>
+ suspendDesktopGfsUploadsForAuthBoundary: () => Promise<void>
+ }
+ app.sessionToken = 'session-a'
+ app.me = {
+ id: 'user-a',
+ email: '[email protected]',
+ name: 'User A',
+ picture: null,
+ teamId: null,
+ teamName: null,
+ role: 'member',
+ }
+ app.authClient = {
+ getMe: vi.fn(() => {
+ discoveryStarted.resolve()
+ return meResponse.promise
+ }),
+ }
+ app.suspendDesktopGfsUploadsForAuthBoundary = vi.fn(async () => logoutStarted.resolve())
+ const operation = vi.fn(async () => 'team result')
+ const contextRequest = app.runWithTeamContext('team-a', operation)
+ await discoveryStarted.promise
+
+ const logout = service.logout()
+ const logoutWasResponsive = await Promise.race([
+ logoutStarted.promise.then(() => true),
+ new Promise<boolean>(resolve => setTimeout(() => resolve(false), 50)),
+ ])
+ meResponse.resolve({
+ id: 'user-a',
+ email: '[email protected]',
+ name: 'User A',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ })
+
+ await expect(contextRequest).rejects.toThrow(/stale_(auth_epoch|session_generation)/)
+ await expect(logout).resolves.toBeTypeOf('number')
+ expect(logoutWasResponsive).toBe(true)
+ expect(operation).not.toHaveBeenCalled()
+ expect(app.sessionToken).toBeNull()
+ })
+
it('finishes GFS activation before a queued workflow read borrows another team', async () => {
const { service, restA } = await createNativeCommitTestHarness()
const teamBResponse = deferred<{
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index cee0e1456..73ab62356 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -1151,14 +1151,6 @@ export class AppService {
}
}
- private async getCurrentSessionTeamId(token: string): Promise<string> {
- if (this.me?.teamId) return this.me.teamId
- this.me = await this.authClient.getMe(token)
- await this.bindCurrentChatStore(this.me.id)
- this.updateCachedCurrentTeam(this.me.teamId)
- return this.me.teamId || ''
- }
-
/** Caller must hold the native auth/environment commit owner. */
private async switchSessionToTeam(teamId: string, token: string) {
const releaseTransientHop = this.enterGfsTransientTeamHop()
@@ -1271,17 +1263,32 @@ export class AppService {
await previousQueue.catch(() => undefined)
try {
- const context = await this.withNativeAuthEnvironmentCommit(async () => {
+ const session = await this.withNativeAuthEnvironmentCommit(async () => {
const sessionToken = this.requireSessionToken()
- const teamId = await this.getCurrentSessionTeamId(sessionToken)
return {
sessionToken,
- teamId,
+ teamId: String(this.me?.teamId || '').trim(),
sessionGeneration: this.sessionGeneration,
environmentBinding: this.captureAuthEnvironmentBinding(),
}
})
+ let context = session
+ if (!context.teamId) {
+ const me = await this.authClient.getMe(session.sessionToken)
+ context = await this.withNativeAuthEnvironmentCommit(async () => {
+ this.assertSessionGeneration(session.sessionGeneration)
+ if (this.sessionToken !== session.sessionToken) {
+ throw new Error('stale_auth_epoch: authenticated team scope changed during discovery')
+ }
+ this.assertAuthEnvironmentBinding(session.environmentBinding)
+ this.me = me
+ await this.bindCurrentChatStore(me.id)
+ this.updateCachedCurrentTeam(me.teamId)
+ return { ...session, teamId: String(me.teamId || '').trim() }
+ })
+ }
+
const assertContextIsCurrent = () => {
this.assertSessionGeneration(context.sessionGeneration)
const currentTeamId = String(this.me?.teamId || '').trim()