Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): r1-m2 retire the sandbox-ui session on a login over a live session
Diego Zacariasfc917c5d0d93
diff --git a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.tsindex 72bf951a5..a760e0642 100644--- a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts+++ b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts@@ -1,5 +1,5 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { shell } from 'electron'+import { app, shell } from 'electron'import { mkdtemp, rm } from 'node:fs/promises'import { tmpdir } from 'node:os'import path from 'node:path'@@ -787,3 +787,122 @@ describe('AppService sandbox-ui embed across a session clear', () => {await expectEmbedGoneWithoutRefresh(parentWindow, firstView, firstWebContentsId)})})++describe('AppService sandbox-ui embed across a login that replaces a live session', () => {+ // A is still logged in when B logs in: no logout runs in between, so only the+ // login itself can retire A's sandbox-ui work.+ beforeEach(() => {+ // The login replacement fences the outgoing user's GFS uploads on disk.+ vi.mocked(app.getPath).mockImplementation(() => userDataDir)+ __setChatStoreBaseDirForTests(userDataDir)+ })++ afterEach(() => {+ vi.mocked(app.getPath).mockImplementation(() => tmpdir())+ })++ async function loginAs(service: AppService, token: string, userId: string): Promise<void> {+ const authClient = (service as unknown as { authClient: Record<string, unknown> }).authClient+ authClient.googleLogin = vi.fn().mockResolvedValue({+ token,+ me: { id: userId, teamId: `team-${userId}` },+ })+ await service.googleLogin(`id-token-${userId}`)+ }++ async function drainSandboxUiQueue(service: AppService): Promise<void> {+ await (+ service as unknown as { enqueueSandboxUiLifecycle(op: () => Promise<void>): Promise<void> }+ ).enqueueSandboxUiLifecycle(async () => undefined)+ }++ function holdMint(recipeName: string) {+ let answer: (response: Response) => void = () => undefined+ const held = new Promise<Response>(resolve => {+ answer = resolve+ })+ mintResponders.set(recipeName, () => held)+ return (response: Response) => answer(response)+ }++ async function serviceLoggedInAsA(): Promise<AppService> {+ const service = new AppService()+ initPluginSdkRuntime({ service, getMainWindow: () => null, userDataDir })+ await loginAs(service, 'session-token-a', 'user-a')+ return service+ }++ it('an open whose mint lands after B logged in over A installs nothing and rejects', async () => {+ const onClosed = vi.fn()+ const service = await serviceLoggedInAsA()+ const parentWindow = new FakeParentWindow()+ const answerMint = holdMint('first-app')++ const outcome = service.openSandboxUi(openArgs('first-app', parentWindow, onClosed)).then(+ () => 'opened',+ (error: unknown) => error+ )+ await vi.waitFor(() => expect(mintCallsFor('first-app')).toBe(1), { timeout: 1_000 })+ await loginAs(service, 'session-token-b', 'user-b')+ answerMint(mintOk('first-app'))++ const settled = await outcome+ await drainSandboxUiQueue(service)+ expect(settled).toMatchObject({ message: expect.stringMatching(/session changed/) })+ expect(electronMocks.sessionObject.cookies.set).not.toHaveBeenCalled()+ expect(electronMocks.views).toHaveLength(0)+ expect(parentWindow.contentView.addChildView).not.toHaveBeenCalled()+ expect(getActiveSandboxUi()).toBeNull()+ expect(onClosed).not.toHaveBeenCalled()+ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1_000)+ expect(mintCallsFor('first-app')).toBe(1)+ })++ it("B logging in over A closes A's embed and stops its refresh loop", async () => {+ const service = await serviceLoggedInAsA()+ const parentWindow = new FakeParentWindow()+ await service.openSandboxUi(openArgs('first-app', parentWindow))+ const first = getActiveSandboxUi()+ expect(first?.appRef).toBe('sandbox-recipes/first-app')+ const firstView = electronMocks.views[0]++ await loginAs(service, 'session-token-b', 'user-b')++ await expectEmbedGoneWithoutRefresh(parentWindow, firstView, first!.webContentsId)+ })++ it("a refresh of A's embed whose mint lands after B logged in writes no cookie and reports nothing", async () => {+ const onRefreshError = vi.fn()+ const service = await serviceLoggedInAsA()+ const parentWindow = new FakeParentWindow()+ await service.openSandboxUi({ ...openArgs('first-app', parentWindow), onRefreshError })+ const answerMint = holdMint('first-app')+ electronMocks.sessionObject.cookies.set.mockClear()++ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1)+ await vi.waitFor(() => expect(mintCallsFor('first-app')).toBe(2), { timeout: 1_000 })+ await loginAs(service, 'session-token-b', 'user-b')+ answerMint(mintOk('first-app'))+ await vi.waitFor(() => expect(getActiveSandboxUi()).toBeNull(), { timeout: 1_000 })+ await drainSandboxUiQueue(service)++ expect(electronMocks.sessionObject.cookies.set).not.toHaveBeenCalled()+ expect(onRefreshError).not.toHaveBeenCalled()+ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1_000)+ expect(mintCallsFor('first-app')).toBe(2)+ })++ it("an open B issues right after logging in over A mounts B's embed", async () => {+ const service = await serviceLoggedInAsA()+ const parentWindow = new FakeParentWindow()+ await service.openSandboxUi(openArgs('first-app', parentWindow))+ const firstView = electronMocks.views[0]++ await loginAs(service, 'session-token-b', 'user-b')+ await service.openSandboxUi(openArgs('second-app', parentWindow))++ expect(getActiveSandboxUi()?.appRef).toBe('sandbox-recipes/second-app')+ expect(firstView?.webContents.isDestroyed()).toBe(true)+ expect(electronMocks.views).toHaveLength(2)+ })+})diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex c4ec8c3d0..c4e974570 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -1393,13 +1393,17 @@ export class AppService {this.workflowTeamByKey.clear()this.rpcTokenManager.clear()unbindChatStore()+ this.invalidateSandboxUiSession()+ }++ /** Retires the sandbox-ui work of a session that is being cleared or replaced. */+ private invalidateSandboxUiSession(): void {this.sandboxUiSessionEpoch += 1- // The embed and its refresh loop run on the session being cleared. Queued- // rather than awaited: callers are synchronous and must not wait on a- // pending open's mint; the serial queue still tears down whatever that- // open mounts.+ // The embed and its refresh loop run on the outgoing session. Queued rather+ // than awaited: callers must not wait on a pending open's mint; the serial+ // queue still tears down whatever that open mounts.void this.closeSandboxUi().catch(error => {- console.error('[SandboxUI] Could not close the embed on session clear:', error)+ console.error('[SandboxUI] Could not close the embed on session change:', error)})}@@ -1659,6 +1663,9 @@ export class AppService {}this.logoutInProgress = falsethis.sessionGeneration += 1+ // A login over a live session replaces it without a logout, so nothing+ // else retires the outgoing user's embed, refresh loop or in-flight open.+ if (hadAuthenticatedScope) this.invalidateSandboxUiSession()this.sessionToken = result.tokenthis.me = result.methis.updateEntityChangeSessionToken(result.token)