Skip to content

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

TypeScript

fix(desktop-app): close the embed when an app tab without a launch is activated

Diego Zacarias4c3c945b0e52
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index 7244f90b2..cfa47c898 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -99,6 +99,7 @@ import { OnboardingPage } from '@pages/OnboardingPage'
import { SandboxUiPage } from '@pages/SandboxUiPage'
import type {
SandboxUiConversationOrigin,
+ SandboxUiPageAction,
SandboxUiShortcutOpenResult,
} from '@pages/SandboxUiPage.types'
import { SettingsPage } from '@pages/SettingsPage'
@@ -379,7 +380,7 @@ export function App() {
const [sandboxLocalSearchRequestId, setSandboxLocalSearchRequestId] = React.useState(0)
const [sandboxActionRequest, setSandboxActionRequest] = React.useState<{
id: number
- action: 'refresh' | 'back-to-apps'
+ action: SandboxUiPageAction
} | null>(null)
const [commandPaletteOpen, setCommandPaletteOpen] = React.useState(false)
const [commandPaletteReturnToSandbox, setCommandPaletteReturnToSandbox] = React.useState(false)
@@ -417,6 +418,11 @@ export function App() {
// must abort — it must neither close the newer embed nor persist the newer
// tab's route with a value read from a view that is already gone.
const sandboxUiActivationGenRef = React.useRef(0)
+ // Tab the latest `launchSandboxUiApp` targeted. Activating an app tab hands the
+ // embed over (app→app) only when a launch is about to `open()` into it: a tab
+ // whose app App cannot resolve gets no launch, and leaving the outgoing view
+ // up would paint it inside that tab.
+ const launchTargetTabIdRef = React.useRef<string | null>(null)
workspaceTabsRef.current = workspaceTabs
const leaveSandboxForChat = React.useCallback(() => {
@@ -1413,6 +1419,7 @@ export function App() {
// Compute the id only on the open-new branch so the sequence counter
// advances exactly as before: relaunch of an existing tab must not burn one.
const newAppTabId = existingTabId ? null : nextChatTabId()
+ launchTargetTabIdRef.current = existingTabId ?? newAppTabId
setWorkspaceTabs(state =>
existingTabId
? selectWorkspaceTab(state, existingTabId)
@@ -1452,15 +1459,26 @@ export function App() {
const activeSandboxUiTabId =
!vm.appsPickerActive && vm.activeWorkspaceTab?.kind === 'app' ? vm.activeWorkspaceTab.id : null
React.useEffect(() => {
+ const launchTargetTabId = launchTargetTabIdRef.current
+ launchTargetTabIdRef.current = null
const outgoingTabId = liveSandboxUiTabIdRef.current
if (outgoingTabId === activeSandboxUiTabId) return
- liveSandboxUiTabIdRef.current = activeSandboxUiTabId
+ const replacedByAnotherApp =
+ activeSandboxUiTabId !== null && launchTargetTabId === activeSandboxUiTabId
+ // An app tab activated without a launch hosts no embed: it stays unowned
+ // until a relaunch arms it (`handleSandboxUiOpening`).
+ liveSandboxUiTabIdRef.current = replacedByAnotherApp ? activeSandboxUiTabId : null
// This is the single point where the active embed transitions; bump the
// generation so any older in-flight continuation knows it no longer owns
// the active view.
sandboxUiActivationGenRef.current += 1
if (outgoingTabId === null) return
- const replacedByAnotherApp = activeSandboxUiTabId !== null
+ // The Apps route stays mounted when the incoming app tab gets no launch:
+ // drop the page's launch state too, or it keeps the outgoing app's chrome
+ // over an empty slot. The close below stays the only solicited one.
+ if (activeSandboxUiTabId !== null && !replacedByAnotherApp) {
+ setSandboxActionRequest(previous => ({ id: (previous?.id ?? 0) + 1, action: 'release' }))
+ }
if (!replacedByAnotherApp) {
// Deactivating to a non-app surface (chat/files/settings via strip, sidebar
// nav, or closing the tab): drop the embed's React state now so anything
diff --git a/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx b/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx
index d19dfd3c1..1bfcaba9d 100644
--- a/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx
+++ b/desktop-app/ui/src/__tests__/App.sandboxUiOwnership.test.tsx
@@ -546,4 +546,162 @@ describe('App sandbox-ui embed always has an owning app tab', () => {
expect(alphaAfter?.app?.appRef).toBe(ALPHA!.appRef)
expect(alphaAfter?.title).toBe(alphaTab!.title)
})
+ // App resolves app tabs against its own listing, refreshed on window focus and
+ // every 30 s. Firing the real focus listener drives that refresh.
+ async function refreshAppListingOnFocus() {
+ const calls = sandboxUi.listApps.mock.calls.length
+ act(() => {
+ window.dispatchEvent(new Event('focus'))
+ })
+ await waitFor(() => expect(sandboxUi.listApps.mock.calls.length).toBeGreaterThan(calls))
+ await act(async () => {
+ await new Promise(resolve => window.setTimeout(resolve, 0))
+ })
+ }
+
+ function emitTitleChanged(event: { appRef: string; title: string }) {
+ const listeners = sandboxUi.onTitleChanged.mock.calls as unknown as Array<
+ [(args: { appRef: string; title: string }) => void]
+ >
+ act(() => listeners.forEach(([listener]) => listener(event)))
+ }
+
+ function tabByAppRef(appRef: string) {
+ return currentController.workspaceTabs.tabs.find(tab => tab.app?.appRef === appRef)
+ }
+
+ function openedAppRef(openCall: number): string {
+ return closedEventFor(openCall).appRef
+ }
+
+ const BETA_RESTARTING: SandboxUiAppListing[] = APP_LISTING.map(app =>
+ app.appRef === BETA!.appRef ? { ...app, ready: false, phase: 'pending' } : app
+ )
+
+ it('closes the live embed and shows the picker when the incoming app tab cannot be resolved, then relaunches it once the app is back', async () => {
+ await renderWithLiveApp(BETA)
+ act(() => sidebarHarness.props?.onOpenSandboxUiApp?.(ALPHA!))
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(2))
+ expect(openedAppRef(1)).toBe(ALPHA!.appRef)
+ const betaTab = tabByAppRef(BETA!.appRef)!
+
+ // Beta's pod restarts: App's next refresh drops it from the resolvable list.
+ sandboxUi.listApps.mockResolvedValue({ apps: BETA_RESTARTING })
+ await refreshAppListingOnFocus()
+
+ sandboxUi.getLocation.mockResolvedValue({ appRef: ALPHA!.appRef, routePath: '/inbox' })
+ fireEvent.click(screen.getByRole('button', { name: 'Beta', pressed: false }))
+
+ await waitFor(() => expect(sandboxUi.close).toHaveBeenCalledTimes(1))
+ expect(sandboxUi.open).toHaveBeenCalledTimes(2)
+ expect(currentController.workspaceTabs.activeTabId).toBe(betaTab.id)
+ // Tab B shows the picker instead of Alpha's chrome over an empty slot.
+ expect(screen.queryByTestId('sandbox-ui-mounted')).toBeNull()
+ expect(await screen.findByRole('button', { name: 'Open Alpha' })).toBeTruthy()
+ // A late title from Alpha must not relabel B.
+ emitTitleChanged({ appRef: ALPHA!.appRef, title: 'Alpha Inbox' })
+ const betaAfter = currentController.workspaceTabs.tabs.find(tab => tab.id === betaTab.id)
+ expect(betaAfter?.title).toBe('Beta')
+ expect(betaAfter?.app?.appRef).toBe(BETA!.appRef)
+ expect(betaAfter?.app?.savedRoutePath).toBeUndefined()
+ // Alpha's own route is still persisted on its tab.
+ await waitFor(() => expect(tabByAppRef(ALPHA!.appRef)?.app?.savedRoutePath).toBe('/inbox'))
+
+ // Beta is ready again: reactivating its (still existing) tab relaunches it.
+ sandboxUi.listApps.mockResolvedValue({ apps: APP_LISTING })
+ await refreshAppListingOnFocus()
+ clickActiveStripTab('Beta')
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(3))
+ expect(openedAppRef(2)).toBe(BETA!.appRef)
+ expect(currentController.workspaceTabs.activeTabId).toBe(betaTab.id)
+ expect(await screen.findByTestId('sandbox-ui-mounted')).toBeTruthy()
+
+ // The relaunched Beta owns the embed again: leaving closes it.
+ await handOffMarkdownFromPlugin()
+ await waitFor(() => expect(sandboxUi.close).toHaveBeenCalledTimes(2))
+ })
+
+ it('does not hand the embed to a grid-opened app tab that App has not resolved yet when the user returns to it', async () => {
+ // App's listing predates Beta becoming ready.
+ sandboxUi.listApps.mockResolvedValue({ apps: BETA_RESTARTING })
+ await renderWithLiveApp(ALPHA)
+
+ // Beta turns ready between App refreshes; the picker grid loads its own list.
+ sandboxUi.listApps.mockResolvedValue({ apps: APP_LISTING })
+ fireEvent.click(screen.getByRole('button', { name: 'New chat', pressed: false }))
+ await waitFor(() => expect(sandboxUi.close).toHaveBeenCalledTimes(1))
+ act(() => sidebarHarness.props?.onSelect?.(DESKTOP_ROUTES.apps))
+ fireEvent.click(await screen.findByRole('button', { name: 'Open Beta' }))
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(2))
+ expect(openedAppRef(1)).toBe(BETA!.appRef)
+ const betaTab = activeWorkspaceTab(currentController.workspaceTabs)!
+ expect(betaTab.app?.appRef).toBe(BETA!.appRef)
+ // The grid launch itself is a real app tab, not torn down on the way in.
+ expect(sandboxUi.close).toHaveBeenCalledTimes(1)
+
+ // Alpha (resolvable) takes the embed over from Beta.
+ fireEvent.click(screen.getByRole('button', { name: 'Alpha', pressed: false }))
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(3))
+ expect(openedAppRef(2)).toBe(ALPHA!.appRef)
+ expect(sandboxUi.close).toHaveBeenCalledTimes(1)
+
+ // Back to Beta before App's next refresh: it cannot be relaunched yet.
+ fireEvent.click(screen.getByRole('button', { name: 'Beta', pressed: false }))
+ await waitFor(() => expect(sandboxUi.close).toHaveBeenCalledTimes(2))
+ expect(sandboxUi.open).toHaveBeenCalledTimes(3)
+ expect(currentController.workspaceTabs.activeTabId).toBe(betaTab.id)
+ expect(screen.queryByTestId('sandbox-ui-mounted')).toBeNull()
+
+ await refreshAppListingOnFocus()
+ clickActiveStripTab('Beta')
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(4))
+ expect(openedAppRef(3)).toBe(BETA!.appRef)
+ })
+ it('hands the embed to a grid pick that App has not resolved yet while the app tab it replaces still owns the embed', async () => {
+ // App's own listing predates Beta becoming ready; the page's grid sees it ready.
+ sandboxUi.listApps.mockResolvedValueOnce({ apps: BETA_RESTARTING })
+ await renderWithLiveApp(ALPHA)
+ const alphaTab = activeWorkspaceTab(currentController.workspaceTabs)!
+
+ // A failed relaunch leaves the Alpha tab owning the embed and shows the grid.
+ sandboxUi.open.mockRejectedValueOnce(OPEN_REJECTED)
+ clickActiveStripTab('Alpha')
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(2))
+ await waitForLaunchFailure(ALPHA!.appRef)
+
+ fireEvent.click(await screen.findByRole('button', { name: 'Open Beta' }))
+
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(3))
+ expect(openedAppRef(2)).toBe(BETA!.appRef)
+ const activeTab = activeWorkspaceTab(currentController.workspaceTabs)
+ expect(activeTab?.id).not.toBe(alphaTab.id)
+ expect(activeTab?.app?.appRef).toBe(BETA!.appRef)
+ expect(await screen.findByTestId('sandbox-ui-mounted')).toBeTruthy()
+ // app→app: the incoming open replaces the view; nothing closes it.
+ await act(async () => {
+ await new Promise(resolve => window.setTimeout(resolve, 0))
+ })
+ expect(sandboxUi.close).not.toHaveBeenCalled()
+ })
+
+ it('drops a failed relaunch banner when the user moves to an app tab that cannot be resolved', async () => {
+ await renderWithLiveApp(BETA)
+ act(() => sidebarHarness.props?.onOpenSandboxUiApp?.(ALPHA!))
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(2))
+ sandboxUi.listApps.mockResolvedValue({ apps: BETA_RESTARTING })
+ await refreshAppListingOnFocus()
+
+ sandboxUi.open.mockRejectedValueOnce(OPEN_REJECTED)
+ clickActiveStripTab('Alpha')
+ await waitFor(() => expect(sandboxUi.open).toHaveBeenCalledTimes(3))
+ await waitForLaunchFailure(ALPHA!.appRef)
+
+ fireEvent.click(screen.getByRole('button', { name: 'Beta', pressed: false }))
+
+ // The Alpha tab still owned the embed after its failed relaunch.
+ await waitFor(() => expect(sandboxUi.close).toHaveBeenCalledTimes(1))
+ const alphaPrefix = `${ALPHA!.appRef}: `
+ await waitFor(() => expect(screen.queryByText(text => text.startsWith(alphaPrefix))).toBeNull())
+ expect(await screen.findByRole('button', { name: 'Open Alpha' })).toBeTruthy()
+ })
})
diff --git a/desktop-app/ui/src/pages/SandboxUiPage.tsx b/desktop-app/ui/src/pages/SandboxUiPage.tsx
index e731fb480..907e966ca 100644
--- a/desktop-app/ui/src/pages/SandboxUiPage.tsx
+++ b/desktop-app/ui/src/pages/SandboxUiPage.tsx
@@ -497,13 +497,21 @@ export function SandboxUiPage({
useEffect(() => {
if (!actionRequest || actionRequest.id <= lastActionRequestIdRef.current) return
lastActionRequestIdRef.current = actionRequest.id
+ if (actionRequest.action === 'release') {
+ // Any launch state counts: a minting open must not write its outcome, and
+ // a failed one must not leave its banner on a tab it never belonged to.
+ launchSeqRef.current += 1
+ setLaunch({ kind: 'idle' })
+ setRefreshError(null)
+ return
+ }
if (launch.kind !== 'mounted') return
if (actionRequest.action === 'refresh') {
onRefresh()
} else if (actionRequest.action === 'back-to-apps') {
void onBackToApps()
}
- }, [actionRequest, launch.kind, onBackToApps, onRefresh])
+ }, [actionRequest, launch.kind, onBackToApps, onRefresh, setLaunch])
const onCopyDeepLink = useCallback(async () => {
try {
diff --git a/desktop-app/ui/src/pages/SandboxUiPage.types.ts b/desktop-app/ui/src/pages/SandboxUiPage.types.ts
index ca8754ea4..0d3858f09 100644
--- a/desktop-app/ui/src/pages/SandboxUiPage.types.ts
+++ b/desktop-app/ui/src/pages/SandboxUiPage.types.ts
@@ -11,10 +11,14 @@ export type SandboxUiShortcutOpenResult =
| { status: 'mounted' }
| { status: 'failed'; message: string }
+// `release` returns the page to its picker without closing the embed: the
+// caller already owns (and emits) the close.
+export type SandboxUiPageAction = 'refresh' | 'back-to-apps' | 'release'
+
export type SandboxUiPageProps = {
actionRequest?: {
id: number
- action: 'refresh' | 'back-to-apps'
+ action: SandboxUiPageAction
} | null
boundsRefreshKey?: string | number
currentTeamId?: string