Skip to content

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

TypeScript

fix: clean up sandbox app parent close listeners

Josué8c38b067f8ff
diff --git a/desktop-app/package-lock.json b/desktop-app/package-lock.json
index 7767ee58d..3da38e378 100644
--- a/desktop-app/package-lock.json
+++ b/desktop-app/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "evenfire",
- "version": "0.1.331",
+ "version": "0.1.332",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "evenfire",
- "version": "0.1.331",
+ "version": "0.1.332",
"license": "MPL-2.0",
"dependencies": {
"@clerum/desktop-app-links": "file:../packages/desktop-app-links",
diff --git a/desktop-app/package.json b/desktop-app/package.json
index ffb63dc71..5a266fefc 100644
--- a/desktop-app/package.json
+++ b/desktop-app/package.json
@@ -1,7 +1,7 @@
{
"name": "evenfire",
"productName": "Evenfire",
- "version": "0.1.331",
+ "version": "0.1.332",
"private": true,
"license": "MPL-2.0",
"description": "Evenfire desktop client for secure agent and workflow access",
diff --git a/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts b/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts
new file mode 100644
index 000000000..c8f50854f
--- /dev/null
+++ b/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts
@@ -0,0 +1,181 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { mountSandboxUiView, unmountSandboxUiView } from '../sandboxUiDriver.js'
+
+type Listener = () => void
+
+const electronMocks = vi.hoisted(() => {
+ let nextWebContentsId = 1
+
+ class FakeWebContents {
+ id = nextWebContentsId++
+ currentUrl = ''
+ destroyed = false
+ private readonly listeners = new Map<string, Set<(...args: unknown[]) => void>>()
+ executeJavaScript = vi.fn()
+ loadURL = vi.fn(async (url: string) => {
+ this.currentUrl = url
+ })
+ setWindowOpenHandler = vi.fn()
+
+ on(event: string, handler: (...args: unknown[]) => void): this {
+ if (!this.listeners.has(event)) this.listeners.set(event, new Set())
+ this.listeners.get(event)!.add(handler)
+ return this
+ }
+
+ removeListener(event: string, handler: (...args: unknown[]) => void): this {
+ this.listeners.get(event)?.delete(handler)
+ return this
+ }
+
+ getURL(): string {
+ return this.currentUrl
+ }
+
+ isDestroyed(): boolean {
+ return this.destroyed
+ }
+
+ close(): void {
+ this.destroyed = true
+ }
+ }
+
+ class FakeWebContentsView {
+ webContents = new FakeWebContents()
+ setBounds = vi.fn()
+ setVisible = vi.fn()
+ }
+
+ const sessionObject = {
+ cookies: { set: vi.fn(async () => undefined) },
+ setPermissionRequestHandler: vi.fn(),
+ setPermissionCheckHandler: vi.fn(),
+ removeAllListeners: vi.fn(),
+ on: vi.fn(),
+ clearCache: vi.fn(async () => undefined),
+ }
+
+ return {
+ FakeWebContentsView,
+ fromPartition: vi.fn(() => sessionObject),
+ getDisplayMatching: vi.fn(() => ({ scaleFactor: 1 })),
+ touchSandboxUiPartition: vi.fn(async () => undefined),
+ sessionObject,
+ }
+})
+
+vi.mock('electron', () => ({
+ WebContentsView: electronMocks.FakeWebContentsView,
+ screen: { getDisplayMatching: electronMocks.getDisplayMatching },
+ session: { fromPartition: electronMocks.fromPartition },
+ shell: { openExternal: vi.fn() },
+}))
+
+vi.mock('../config.js', () => ({
+ getActiveEnvKey: () => 'test-env',
+}))
+
+vi.mock('../sandboxUiPartitionGc.js', () => ({
+ touchSandboxUiPartition: electronMocks.touchSandboxUiPartition,
+}))
+
+class FakeParentWindow {
+ destroyed = false
+ contentView = {
+ addChildView: vi.fn(),
+ removeChildView: vi.fn(),
+ }
+ private readonly listeners = new Map<string, Set<Listener>>()
+
+ isDestroyed(): boolean {
+ return this.destroyed
+ }
+
+ getBounds(): Electron.Rectangle {
+ return { x: 0, y: 0, width: 800, height: 600 }
+ }
+
+ once(event: string, handler: Listener): this {
+ if (!this.listeners.has(event)) this.listeners.set(event, new Set())
+ this.listeners.get(event)!.add(handler)
+ return this
+ }
+
+ removeListener(event: string, handler: Listener): this {
+ this.listeners.get(event)?.delete(handler)
+ return this
+ }
+
+ emit(event: string): void {
+ const handlers = [...(this.listeners.get(event) ?? [])]
+ this.listeners.delete(event)
+ handlers.forEach(handler => handler())
+ }
+
+ listenerCount(event: string): number {
+ return this.listeners.get(event)?.size ?? 0
+ }
+}
+
+type MountSandboxUiViewArgs = Parameters<typeof mountSandboxUiView>[0]
+type MountSandboxUiViewOverrides = Omit<Partial<MountSandboxUiViewArgs>, 'parentWindow'> & {
+ parentWindow: FakeParentWindow
+}
+
+function mountArgs(overrides: MountSandboxUiViewOverrides): MountSandboxUiViewArgs {
+ return {
+ recipeNs: 'sandbox-recipes',
+ recipeName: 'task-board',
+ setCookie:
+ 'clerum_sandbox_ui_session=tok;' +
+ ' Path=/api/v1/sandbox-ui/sandbox-recipes/task-board/; HttpOnly',
+ rpcProxyUrl: 'https://rpc.example',
+ defaultPath: '/',
+ bounds: { x: 0, y: 0, width: 400, height: 300 },
+ ...overrides,
+ parentWindow: overrides.parentWindow as unknown as MountSandboxUiViewArgs['parentWindow'],
+ }
+}
+
+beforeEach(() => {
+ vi.clearAllMocks()
+})
+
+afterEach(async () => {
+ await unmountSandboxUiView()
+})
+
+describe('mountSandboxUiView lifecycle cleanup', () => {
+ it('removes the old parent close listener when a view is replaced', async () => {
+ const parentWindow = new FakeParentWindow()
+ const firstClosed = vi.fn()
+ const secondClosed = vi.fn()
+
+ await mountSandboxUiView(mountArgs({ parentWindow, onClosed: firstClosed }))
+ await mountSandboxUiView(
+ mountArgs({ parentWindow, recipeName: 'support-desk', onClosed: secondClosed })
+ )
+
+ expect(parentWindow.listenerCount('closed')).toBe(1)
+
+ parentWindow.emit('closed')
+
+ expect(firstClosed).not.toHaveBeenCalled()
+ expect(secondClosed).toHaveBeenCalledOnce()
+ })
+
+ it('removes the parent close listener when the active view is unmounted', async () => {
+ const parentWindow = new FakeParentWindow()
+ const onClosed = vi.fn()
+
+ await mountSandboxUiView(mountArgs({ parentWindow, onClosed }))
+ expect(parentWindow.listenerCount('closed')).toBe(1)
+
+ await unmountSandboxUiView()
+
+ expect(parentWindow.listenerCount('closed')).toBe(0)
+ parentWindow.emit('closed')
+ expect(onClosed).not.toHaveBeenCalled()
+ })
+})
diff --git a/desktop-app/src/sandboxUiDriver.ts b/desktop-app/src/sandboxUiDriver.ts
index 471ec23fa..331657d96 100644
--- a/desktop-app/src/sandboxUiDriver.ts
+++ b/desktop-app/src/sandboxUiDriver.ts
@@ -1,4 +1,4 @@
-import { BrowserWindow, WebContentsView, session } from 'electron'
+import { BrowserWindow, WebContentsView, screen, session } from 'electron'
import path from 'node:path'
import { getActiveEnvKey } from './config.js'
import { extractSandboxUiViewRoute, normalizeSandboxUiRoute } from './sandboxUiDeepLinks.js'
@@ -56,8 +56,6 @@ function toViewBounds(
// to the renderer while the screen's scaleFactor stays at 2.0).
// WebContentsView.setBounds expects points, so multiply by dpr/scaleFactor
// — a no-op when they match (the common 1× / 2× case).
- // eslint-disable-next-line @typescript-eslint/no-require-imports
- const { screen } = require('electron') as typeof import('electron')
const sf = screen.getDisplayMatching(parentWindow.getBounds()).scaleFactor
const dpr = bounds.dpr ?? sf
if (Math.abs(dpr - sf) < 0.01) {
@@ -100,6 +98,7 @@ type ActiveView = {
parentWindow: BrowserWindow
rpcProxyOrigin: string
cleanupClientRouteHandoff?: () => void
+ cleanupParentClosed?: () => void
}
let active: ActiveView | null = null
@@ -262,6 +261,7 @@ async function teardownActive(reason: 'replaced' | 'closed' | 'parent_closed'):
active = null
try {
current.cleanupClientRouteHandoff?.()
+ current.cleanupParentClosed?.()
// contentView.removeChildView is the documented teardown step; it both
// detaches the view from layout AND severs the parent's ownership ref.
if (!current.parentWindow.isDestroyed()) {
@@ -369,6 +369,9 @@ export async function mountSandboxUiView(args: MountSandboxUiArgs): Promise<void
onClosed?.()
}
parentWindow.once('closed', onParentClosed)
+ const cleanupParentClosed = (): void => {
+ parentWindow.removeListener('closed', onParentClosed)
+ }
active = {
view,
@@ -378,6 +381,7 @@ export async function mountSandboxUiView(args: MountSandboxUiArgs): Promise<void
partition,
parentWindow,
rpcProxyOrigin: proxyOriginUrl,
+ cleanupParentClosed,
}
parentWindow.contentView.addChildView(view)