Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): r11-h1 keep deliberate team switch owner
Josuée8bb642133ce
diff --git a/desktop-app/src/__tests__/appService.prewarm.test.ts b/desktop-app/src/__tests__/appService.prewarm.test.tsindex 1fc76dc11..0f8664bd6 100644--- a/desktop-app/src/__tests__/appService.prewarm.test.ts+++ b/desktop-app/src/__tests__/appService.prewarm.test.ts@@ -441,7 +441,8 @@ describe('AppService.prewarmHost — bounded wake re-emission', () => {const firstSwitch = svc.switchTeam('team-2')const secondSwitch = svc.switchTeam('team-3')- expect(rejectSwitches).toHaveLength(2)+ await vi.waitFor(() => expect(rejectSwitches).toHaveLength(1))+ expect(rejectSwitches).toHaveLength(1)await expect(svc.prewarmHost('chatllm')).resolves.toEqual({requested: false,skipped: 'auth-changed',@@ -449,6 +450,7 @@ describe('AppService.prewarmHost — bounded wake re-emission', () => {rejectSwitches[0](new Error('first switch rejected'))await expect(firstSwitch).rejects.toThrow('first switch rejected')+ await vi.waitFor(() => expect(rejectSwitches).toHaveLength(2))await expect(svc.prewarmHost('chatllm')).resolves.toEqual({requested: false,skipped: 'auth-changed',diff --git a/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.tsnew file mode 100644index 000000000..90915a09b--- /dev/null+++ b/desktop-app/src/__tests__/appService.teamTransitionOwnership.test.ts@@ -0,0 +1,89 @@+import { afterEach, describe, expect, it, vi } from 'vitest'+import {+ cleanupNativeCommitTestHarness,+ createNativeCommitTestHarness,+ deferred,+} from '../../testSupport/appService.nativeCommitTestHarness.js'++afterEach(cleanupNativeCommitTestHarness)++describe('AppService deliberate team transition ownership', () => {+ it('finishes GFS activation before a queued workflow read borrows another team', async () => {+ const { service, restA } = await createNativeCommitTestHarness()+ const teamBResponse = deferred<{+ token: string+ team: { id: string; name: string; role: string }+ }>()+ const switchStarted = deferred<void>()+ const tokenTeams = new Map<string, string>([['synthetic-session-a', 'team-a']])+ let issuedToken = 0+ const user = {+ id: 'user-a',+ email: '[email protected]',+ name: 'User A',+ picture: null,+ teamId: 'team-a',+ teamName: 'Team A',+ role: 'member',+ }+ const authClient = {+ googleLogin: vi.fn().mockResolvedValue({ token: 'synthetic-session-a', me: user }),+ switchTeam: vi.fn(async (_token: string, teamId: string) => {+ if (teamId === 'team-b') {+ switchStarted.resolve()+ const response = await teamBResponse.promise+ tokenTeams.set(response.token, teamId)+ return response+ }+ const token = `synthetic-${teamId}-${++issuedToken}`+ tokenTeams.set(token, teamId)+ return { token, team: { id: teamId, name: teamId, role: 'member' } }+ }),+ getMe: vi.fn(async (token: string) => {+ const teamId = tokenTeams.get(token) || 'team-a'+ return { ...user, teamId, teamName: teamId === 'team-b' ? 'Team B' : 'Team A' }+ }),+ readWorkflow: vi.fn(async (token: string) => ({ token })),+ }+ const app = service as unknown as {+ authClient: typeof authClient+ googleLogin: (token: string) => Promise<unknown>+ switchTeam: (teamId: string) => Promise<unknown>+ readWorkflow: (namespace: string, name: string) => Promise<unknown>+ workflowKey: (namespace: string, name: string) => string+ workflowTeamByKey: Map<string, string>+ activateGfsAuthScope: () => void+ me: { teamId: string } | null+ gfsScopeIdentity: { teamId: string | null; baseUrl: string } | null+ gfsDispatchBlocked: boolean+ gfsTransientTeamHopDepth: number+ }+ app.authClient = authClient+ const activateGfsAuthScope = vi.spyOn(app, 'activateGfsAuthScope')+ await app.googleLogin('synthetic-google-token')+ activateGfsAuthScope.mockClear()+ app.workflowTeamByKey.set(app.workflowKey('workflows', 'approval'), 'team-a')++ const switching = app.switchTeam('team-b')+ await switchStarted.promise+ const workflowRead = app.readWorkflow('workflows', 'approval')+ teamBResponse.resolve({+ token: 'synthetic-session-b',+ team: { id: 'team-b', name: 'Team B', role: 'member' },+ })++ const [switchResult, workflowResult] = await Promise.all([switching, workflowRead])++ expect(switchResult).toMatchObject({ authenticated: true, me: { teamId: 'team-b' } })+ expect(tokenTeams.get(authClient.readWorkflow.mock.calls[0][0])).toBe('team-a')+ expect(workflowResult).toMatchObject({ token: expect.any(String) })+ expect(activateGfsAuthScope).toHaveBeenCalledOnce()+ expect(activateGfsAuthScope.mock.invocationCallOrder[0]).toBeLessThan(+ authClient.readWorkflow.mock.invocationCallOrder[0]+ )+ expect(app.me?.teamId).toBe('team-b')+ expect(app.gfsScopeIdentity).toMatchObject({ teamId: 'team-b', baseUrl: restA })+ expect(app.gfsDispatchBlocked).toBe(false)+ expect(app.gfsTransientTeamHopDepth).toBe(0)+ })+})diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex 9c2f2adbd..a56ec22b6 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -3708,29 +3708,27 @@ export class AppService {// Explicit team changes fence opportunistic wake immediately. A failed// switch can safely leave prewarm canceled; the message path remains live.const releasePrewarm = this.beginPrewarmAuthTransition()- const previousIdentity =- this.gfsScopeIdentity ?? (this.me ? desktopGfsUploadIdentity(this.me) : undefined)// Keep the new team token fenced until the old GFS jobs have been- // suspended. `switchSessionToTeam` has its own nested gate, but releases- // it when the token exchange returns; this outer gate closes that small- // interval before the auth-boundary persistence completes.+ // suspended. The native owner spans the entire deliberate transition so+ // another team-context operation cannot commit between token installation+ // and GFS activation.const releaseTransientHop = this.enterGfsTransientTeamHop()try {- // §4.5-3 (GAP-N4 main half): a deliberate team switch tears down every live- // stream — the old team's progress/activity/status/notification sockets must- // not survive it. Done AFTER the switch SUCCEEDS (silently), mirroring the- // renderer, which only calls `resetChat()`/`releaseAll()` once the `team.switch`- // IPC resolves (so a FAILED switch leaves both the streams and the trackers- // intact rather than a frozen half-torn-down state). Tasks stay alive- // server-side and reconverge via reconcile when the user returns to that team.- // NOTE: only the user-initiated `switchTeam` closes streams — the transient- // per-operation team hops in `runWithTeamContext`/`switchSessionToTeam` (e.g.- // minting a cross-team RPC token to decide an approval) must NOT.- const switchedToken = await this.switchSessionToTeam(targetTeamId)- const switchedMe = this.me- const switchedGeneration = this.sessionGeneration- const switchedEnvironment = this.captureAuthEnvironmentBinding()return await this.withNativeAuthEnvironmentCommit(async () => {+ const previousIdentity =+ this.gfsScopeIdentity ?? (this.me ? desktopGfsUploadIdentity(this.me) : undefined)+ // §4.5-3 (GAP-N4 main half): a deliberate team switch tears down every+ // live stream — the old team's progress/activity/status/notification+ // sockets must not survive it. The transition owns the commit slot until+ // GFS activation and stream/cache publication complete. Transient+ // runWithTeamContext hops continue to reuse their existing owner and do+ // not suspend uploads or stop streams.+ const switchedToken = await this.switchSessionToTeam(targetTeamId, undefined, {+ commitOwnerHeld: true,+ })+ const switchedMe = this.me+ const switchedGeneration = this.sessionGeneration+ const switchedEnvironment = this.captureAuthEnvironmentBinding()this.assertSessionGeneration(switchedGeneration)this.assertAuthEnvironmentBinding(switchedEnvironment)if (this.sessionToken !== switchedToken || !switchedMe || this.me !== switchedMe) {