Skip to content

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

TypeScript

fix(desktop): fail closed on team fence errors

Alfredo Lopez8e88e1da56b8
diff --git a/desktop-app/src/__tests__/appService.rpc.test.ts b/desktop-app/src/__tests__/appService.rpc.test.ts
index 622e9131f..26de93281 100644
--- a/desktop-app/src/__tests__/appService.rpc.test.ts
+++ b/desktop-app/src/__tests__/appService.rpc.test.ts
@@ -391,6 +391,61 @@ describe('AppService.invokeHostMessage', () => {
).toBeGreaterThan(service.authClient.switchTeam.mock.invocationCallOrder[0])
})
+ it('clears the replacement team session when explicit GFS fence persistence fails', async () => {
+ const service = new AppService() as any
+ service.sessionToken = 'team-a-token'
+ service.me = {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'Test User',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ }
+ service.gfsScopeIdentity = {
+ ownerId: 'user-1',
+ teamId: 'team-a',
+ environmentKey: 'test-env',
+ baseUrl: 'https://external.example',
+ }
+ service.gfsDispatchBlocked = false
+ service.authClient = {
+ switchTeam: vi.fn().mockResolvedValue({
+ token: 'team-b-token',
+ team: { id: 'team-b', name: 'Team B', role: 'member' },
+ }),
+ getMe: vi.fn().mockResolvedValue({ ...service.me, teamId: 'team-b', teamName: 'Team B' }),
+ }
+ service.tokenStore = {
+ setSessionToken: vi.fn(),
+ clearSessionToken: vi.fn(),
+ }
+ service.rpcTokenManager = { clear: vi.fn() }
+ service.suspendDesktopGfsUploadsForAuthBoundary = vi
+ .fn()
+ .mockRejectedValue(new Error('state file is read-only'))
+ const clearAuthenticatedSessionState = service.clearAuthenticatedSessionState.bind(service)
+ let depthDuringCleanup = 0
+ service.clearAuthenticatedSessionState = vi.fn(() => {
+ depthDuringCleanup = service.gfsTransientTeamHopDepth
+ clearAuthenticatedSessionState()
+ })
+
+ await expect(service.switchTeam('team-b')).rejects.toThrow('state file is read-only')
+
+ expect(service.clearAuthenticatedSessionState).toHaveBeenCalledTimes(1)
+ expect(service.tokenStore.clearSessionToken).toHaveBeenCalledWith(expect.any(String), {
+ legacyEnvKeys: expect.any(Array),
+ })
+ expect(depthDuringCleanup).toBeGreaterThan(0)
+ expect(service.sessionToken).toBeNull()
+ expect(service.me).toBeNull()
+ expect(service.gfsScopeIdentity).toBeNull()
+ expect(service.gfsDispatchBlocked).toBe(true)
+ expect(service.gfsTransientTeamHopDepth).toBe(0)
+ })
+
it('does not fence GFS uploads when an explicit switchTeam is rejected', async () => {
const service = new AppService() as any
service.sessionToken = 'team-a-token'
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index b7f2656be..405e2fee6 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -2842,7 +2842,26 @@ export class AppService {
// A deliberate user switch is the only team-context boundary that fences
// in-flight uploads. Transient runWithTeamContext hops intentionally use
// switchSessionToTeam directly and must leave those jobs untouched.
- await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
+ try {
+ await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
+ } catch (error) {
+ // The replacement session is already installed at this point. If the
+ // auth-boundary state write fails, do not leave that token/me paired
+ // with a blocked or stale GFS scope. Cleanup remains inside the outer
+ // transient-hop gate, and the original persistence error is preserved.
+ this.clearAuthenticatedSessionState()
+ try {
+ await this.tokenStore.clearSessionToken(getActiveEnvKey(), {
+ legacyEnvKeys: getActiveLegacyEnvKeys(),
+ })
+ } catch (clearError) {
+ console.warn(
+ '[AppService] Failed to clear a partially switched team session:',
+ clearError
+ )
+ }
+ throw error
+ }
this.activateGfsAuthScope()
this.stopAllStreams()
// Grants are keyed by userId, not by team, so they carry over — but every