Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): close the sandbox-ui embed when the trusted renderer or session goes away
Diego Zacarias041bf7d634f4
diff --git a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.tsindex 80f775609..0de5f800a 100644--- a/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts+++ b/desktop-app/src/__tests__/appService.sandboxUiLifecycle.test.ts@@ -3,6 +3,7 @@ import { mkdtemp, rm } from 'node:fs/promises'import { tmpdir } from 'node:os'import path from 'node:path'import { AppService } from '../appService.js'+import { wireMainWindowRendererReadiness } from '../mainWindowReadiness.js'import { _resetPluginSdkRuntimeForTests, initPluginSdkRuntime } from '../pluginSdkRuntime.js'import { _resetPluginSurfacesForTests, resolvePluginSurface } from '../pluginSurfaceRegistry.js'import { SANDBOX_UI_MINT_TIMEOUT_MS } from '../rpcProxyClient.js'@@ -117,6 +118,7 @@ vi.mock('electron', () => ({vi.mock('../config.js', () => ({getActiveEnvKey: () => 'test-env',+ getActiveLegacyEnvKeys: () => [],config: {rpcProxyBaseUrl: 'https://rpc.example',externalRestApiBaseUrl: 'http://rest',@@ -477,3 +479,173 @@ describe('AppService sandbox-ui mint against a hung rpc-proxy', () => {expect(mintCallsFor('first-app')).toBe(mintsBefore)})})++// The trusted renderer's webContents, wired exactly as main.ts wires the main+// window: through the real readiness wiring into the real AppService close.+class FakeMainWebContents {+ private readonly listeners = new Map<string, Set<Listener>>()+ reload = vi.fn()++ on(event: string, handler: Listener): this {+ if (!this.listeners.has(event)) this.listeners.set(event, new Set())+ this.listeners.get(event)!.add(handler)+ return this+ }++ emit(event: string, ...args: unknown[]): void {+ this.listeners.get(event)?.forEach(handler => handler(...args))+ }++ isDestroyed(): boolean {+ return false+ }+}++function wireMainWindow(service: AppService): FakeMainWebContents {+ const mainWebContents = new FakeMainWebContents()+ wireMainWindowRendererReadiness({+ webContents: mainWebContents as never,+ isCurrentWindow: () => true,+ markNotReady: vi.fn(),+ closeSandboxUi: () => service.closeSandboxUi(),+ })+ return mainWebContents+}++async function expectEmbedGoneWithoutRefresh(+ parentWindow: FakeParentWindow,+ firstView: InstanceType<typeof electronMocks.FakeWebContentsView> | undefined,+ firstWebContentsId: number+): Promise<void> {+ await vi.waitFor(() => expect(getActiveSandboxUi()).toBeNull(), { timeout: 1_000 })+ expect(parentWindow.contentView.removeChildView).toHaveBeenCalledWith(firstView)+ expect(firstView?.webContents.isDestroyed()).toBe(true)+ expect(resolvePluginSurface(firstWebContentsId)).toBeNull()+ const mintsBefore = mintCallsFor('first-app')+ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1_000)+ expect(mintCallsFor('first-app')).toBe(mintsBefore)+}++describe('AppService sandbox-ui embed whose trusted renderer is replaced', () => {+ it('a committed document navigation of the main window closes the embed', async () => {+ const onClosed = vi.fn()+ const { service, parentWindow, firstWebContentsId, firstView } =+ await setupWithLiveFirstApp(onClosed)+ const mainWebContents = wireMainWindow(service)++ // A reload (Cmd+R / View > Reload with the renderer focused) or a new+ // document committed in the main frame. A load that ends on an error page+ // emits no did-navigate and is not covered here.+ mainWebContents.emit('did-navigate', {}, 'file:///ui-dist/index.html', 200, 'OK')++ await expectEmbedGoneWithoutRefresh(parentWindow, firstView, firstWebContentsId)+ // The replacement renderer starts without state: nothing to notify.+ expect(onClosed).not.toHaveBeenCalled()+ })++ it('in-page, subframe and cancelled navigations of the main window leave the embed', async () => {+ const { service, firstWebContentsId } = await setupWithLiveFirstApp()+ const mainWebContents = wireMainWindow(service)++ // An external link is started and then cancelled by will-navigate: the+ // renderer that owns the embed stays.+ mainWebContents.emit('did-start-navigation', {+ url: 'https://example.com/',+ isMainFrame: true,+ isSameDocument: false,+ })+ mainWebContents.emit('did-navigate-in-page', {}, 'file:///ui-dist/index.html#/apps', true)+ mainWebContents.emit('did-frame-navigate', {}, 'about:blank', 200, 'OK', false)+ // Drain the serial lifecycle queue: a close enqueued by any of those events+ // has run once this barrier settles.+ await (+ service as unknown as { enqueueSandboxUiLifecycle(op: () => Promise<void>): Promise<void> }+ ).enqueueSandboxUiLifecycle(async () => undefined)++ expect(getActiveSandboxUi()?.webContentsId).toBe(firstWebContentsId)+ })++ it('a crash of the main renderer closes the embed', async () => {+ const { service, parentWindow, firstWebContentsId, firstView } = await setupWithLiveFirstApp()+ const mainWebContents = wireMainWindow(service)++ mainWebContents.emit('render-process-gone', {}, { reason: 'crashed', exitCode: 139 })++ expect(mainWebContents.reload).toHaveBeenCalledOnce()+ await expectEmbedGoneWithoutRefresh(parentWindow, firstView, firstWebContentsId)+ })+})++describe('AppService sandbox-ui embed across a session clear', () => {+ // rpc-proxy holds the mint until the test answers it.+ 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)+ }++ it('an open whose mint lands after logout is torn down with no refresh loop', async () => {+ const onClosed = vi.fn()+ const service = makeService()+ initPluginSdkRuntime({ service, getMainWindow: () => null, userDataDir })+ const parentWindow = new FakeParentWindow()+ const answerMint = holdMint('first-app')++ const open = service.openSandboxUi(openArgs('first-app', parentWindow, onClosed))+ await vi.waitFor(() => expect(mintCallsFor('first-app')).toBe(1), { timeout: 1_000 })+ await service.logout()+ answerMint(mintOk('first-app'))+ await open++ const mountedView = electronMocks.views[0]+ expect(mountedView).toBeDefined()+ await vi.waitFor(() => expect(getActiveSandboxUi()).toBeNull(), { timeout: 1_000 })+ expect(parentWindow.contentView.removeChildView).toHaveBeenCalledWith(mountedView)+ expect(mountedView?.webContents.isDestroyed()).toBe(true)+ expect(resolvePluginSurface(mountedView!.webContents.id)).toBeNull()+ expect(onClosed).not.toHaveBeenCalled()+ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1_000)+ expect(mintCallsFor('first-app')).toBe(1)+ })++ it('an open whose mint fails after logout leaves nothing behind and rejects once', async () => {+ const onClosed = vi.fn()+ const service = makeService()+ initPluginSdkRuntime({ service, getMainWindow: () => null, userDataDir })+ const parentWindow = new FakeParentWindow()+ const answerMint = holdMint('first-app')++ const open = service.openSandboxUi(openArgs('first-app', parentWindow, onClosed))+ const outcome = open.then(+ () => 'opened',+ (error: unknown) => error+ )+ await vi.waitFor(() => expect(mintCallsFor('first-app')).toBe(1), { timeout: 1_000 })+ await service.logout()+ answerMint(mintFails(409, 'app is starting'))++ await expect(outcome).resolves.toMatchObject({ message: expect.stringMatching(/\(409\)/) })+ // Drain the serial queue so the close queued by logout has run too.+ await (+ service as unknown as { enqueueSandboxUiLifecycle(op: () => Promise<void>): Promise<void> }+ ).enqueueSandboxUiLifecycle(async () => undefined)+ expect(electronMocks.views).toHaveLength(0)+ expect(getActiveSandboxUi()).toBeNull()+ expect(parentWindow.contentView.addChildView).not.toHaveBeenCalled()+ expect(parentWindow.contentView.removeChildView).not.toHaveBeenCalled()+ expect(onClosed).not.toHaveBeenCalled()+ await vi.advanceTimersByTimeAsync(SANDBOX_UI_REFRESH_INTERVAL_MS + 1_000)+ expect(mintCallsFor('first-app')).toBe(1)+ })++ it('logout closes the embed and stops its refresh loop', async () => {+ const { service, parentWindow, firstWebContentsId, firstView } = await setupWithLiveFirstApp()++ await service.logout()++ await expectEmbedGoneWithoutRefresh(parentWindow, firstView, firstWebContentsId)+ })+})diff --git a/desktop-app/src/__tests__/mainWindowReadiness.test.ts b/desktop-app/src/__tests__/mainWindowReadiness.test.tsindex 7a8df826f..0326997cf 100644--- a/desktop-app/src/__tests__/mainWindowReadiness.test.ts+++ b/desktop-app/src/__tests__/mainWindowReadiness.test.ts@@ -25,6 +25,7 @@ describe('main window renderer readiness', () => {webContents: webContents as never,isCurrentWindow: () => true,markNotReady,+ closeSandboxUi: vi.fn(async () => undefined),})expect(listeners.has('did-start-navigation')).toBe(false)@@ -42,6 +43,7 @@ describe('main window renderer readiness', () => {webContents: webContents as never,isCurrentWindow: () => true,markNotReady,+ closeSandboxUi: vi.fn(async () => undefined),})listeners.get('render-process-gone')?.({}, { reason: 'crashed' })@@ -56,6 +58,7 @@ describe('main window renderer readiness', () => {webContents: clean.webContents as never,isCurrentWindow: () => true,markNotReady: vi.fn(),+ closeSandboxUi: vi.fn(async () => undefined),})clean.listeners.get('render-process-gone')?.({}, { reason: 'clean-exit' })expect(clean.webContents.reload).not.toHaveBeenCalled()@@ -66,9 +69,52 @@ describe('main window renderer readiness', () => {webContents: obsolete.webContents as never,isCurrentWindow: () => false,markNotReady,+ closeSandboxUi: vi.fn(async () => undefined),})obsolete.listeners.get('render-process-gone')?.({}, { reason: 'crashed' })expect(markNotReady).not.toHaveBeenCalled()expect(obsolete.webContents.reload).not.toHaveBeenCalled()})++ it('closes the sandbox-ui embed once per replaced renderer of the current window', () => {+ const navigated = makeWebContents()+ const closeOnNavigate = vi.fn(async () => undefined)+ wireMainWindowRendererReadiness({+ webContents: navigated.webContents as never,+ isCurrentWindow: () => true,+ markNotReady: vi.fn(),+ closeSandboxUi: closeOnNavigate,+ })+ navigated.listeners.get('did-navigate')?.()+ expect(closeOnNavigate).toHaveBeenCalledOnce()++ // A clean exit is not reloaded, but the renderer that owned the embed is gone.+ const exited = makeWebContents()+ const closeOnExit = vi.fn(async () => undefined)+ wireMainWindowRendererReadiness({+ webContents: exited.webContents as never,+ isCurrentWindow: () => true,+ markNotReady: vi.fn(),+ closeSandboxUi: closeOnExit,+ })+ exited.listeners.get('render-process-gone')?.({}, { reason: 'clean-exit' })+ expect(closeOnExit).toHaveBeenCalledOnce()+ })++ it('never closes the sandbox-ui embed for an obsolete window', () => {+ const obsolete = makeWebContents()+ const closeSandboxUi = vi.fn(async () => undefined)+ wireMainWindowRendererReadiness({+ webContents: obsolete.webContents as never,+ isCurrentWindow: () => false,+ markNotReady: vi.fn(),+ closeSandboxUi,+ })++ obsolete.listeners.get('did-navigate')?.()+ obsolete.listeners.get('render-process-gone')?.({}, { reason: 'crashed' })+ obsolete.listeners.get('render-process-gone')?.({}, { reason: 'clean-exit' })++ expect(closeSandboxUi).not.toHaveBeenCalled()+ })})diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.tsindex ef49e403c..f72e964a4 100644--- a/desktop-app/src/appService.ts+++ b/desktop-app/src/appService.ts@@ -1378,6 +1378,13 @@ export class AppService {this.workflowTeamByKey.clear()this.rpcTokenManager.clear()unbindChatStore()+ // 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.+ void this.closeSandboxUi().catch(error => {+ console.error('[SandboxUI] Could not close the embed on session clear:', error)+ })}private activateGfsAuthScope(identityOverride?: DesktopGfsUploadIdentity): void {diff --git a/desktop-app/src/main.ts b/desktop-app/src/main.tsindex 170f0b462..e07dc21a5 100644--- a/desktop-app/src/main.ts+++ b/desktop-app/src/main.ts@@ -506,6 +506,7 @@ async function createWindow(): Promise<void> {markNotReady: () => {mainWindowRendererReady = false},+ closeSandboxUi: () => appService.closeSandboxUi(),})wireWindowVisibility(window)diff --git a/desktop-app/src/mainWindowReadiness.ts b/desktop-app/src/mainWindowReadiness.tsindex d72934f2b..aea977a4a 100644--- a/desktop-app/src/mainWindowReadiness.ts+++ b/desktop-app/src/mainWindowReadiness.ts@@ -4,20 +4,34 @@ export function wireMainWindowRendererReadiness(input: {webContents: Pick<WebContents, 'isDestroyed' | 'on' | 'reload'>isCurrentWindow: () => booleanmarkNotReady: () => void+ closeSandboxUi: () => Promise<void>}): void {- const { webContents, isCurrentWindow, markNotReady } = input+ const { webContents, isCurrentWindow, markNotReady, closeSandboxUi } = input++ // The sandbox-ui embed is a native view painted over this renderer and owned+ // by renderer state. A replacement renderer starts with no owner for it, so+ // main drops the embed itself; the new renderer needs no notice.+ const discardSandboxUi = () => {+ void closeSandboxUi().catch(error => {+ console.error('[Desktop] Could not close the sandbox-ui embed of a replaced renderer:', error)+ })+ }// did-navigate only fires after a main-frame, cross-document navigation// commits. did-start-navigation also fires for attempts later cancelled by// will-navigate, which would leave readiness false with no replacement- // renderer available to perform the ready handshake.+ // renderer available to perform the ready handshake, and would close the+ // embed under a renderer that is still alive and still owns it.webContents.on('did-navigate', () => {- if (isCurrentWindow()) markNotReady()+ if (!isCurrentWindow()) return+ markNotReady()+ discardSandboxUi()})webContents.on('render-process-gone', (_event, details) => {if (!isCurrentWindow()) returnmarkNotReady()+ discardSandboxUi()if (details.reason !== 'clean-exit' && !webContents.isDestroyed()) {webContents.reload()}