Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): r1-h1 gate stale sandbox close/persist behind an activation generation
Diego Zacariasa0bc688b83ba
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsxindex 0c02401ad..03332af95 100644--- a/desktop-app/ui/src/App.tsx+++ b/desktop-app/ui/src/App.tsx@@ -365,6 +365,14 @@ export function App() {// state after an UNsolicited close — crash/quit/GC — so a later tab switch// does not persist or re-close a dead embed).const liveSandboxUiTabIdRef = React.useRef<string | null>(null)+ // Monotonic generation bumped on EVERY transition of the active embed (the+ // deactivation effect and the deep-link handoff are the only emitters). Each+ // read-then-close continuation captures this at dispatch; anything reopened or+ // switched-to while its `getLocation()` IPC is in flight bumps the generation,+ // so the stale continuation can tell it no longer owns the active view and+ // 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)workspaceTabsRef.current = workspaceTabsconst leaveSandboxForChat = React.useCallback(() => {@@ -1134,6 +1142,10 @@ export function App() {const outgoingTabId = liveSandboxUiTabIdRef.currentif (outgoingTabId === activeSandboxUiTabId) returnliveSandboxUiTabIdRef.current = activeSandboxUiTabId+ // 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 += 1if (outgoingTabId === null) returnconst replacedByAnotherApp = activeSandboxUiTabId !== nullif (!replacedByAnotherApp) {@@ -1159,6 +1171,7 @@ export function App() {// so `getLocation` reaches main (and reads the still-live outgoing view)// strictly before `open()` tears it down. i.e. the read is captured before// the incoming open, not merely "usually first".+ const gen = sandboxUiActivationGenRef.currentvoid (async () => {let routePath: string | undefinedtry {@@ -1167,6 +1180,12 @@ export function App() {} catch {routePath = undefined}+ // A newer activation took over while the read was in flight (the same app+ // reopened, or another app/surface became active): the read value belongs+ // to a view that is no longer the active one, so abort BOTH the persist+ // (it would clobber the live tab's route with a stale one) and the close+ // (it would tear down the embed the user is now looking at).+ if (gen !== sandboxUiActivationGenRef.current) return// No-op when the outgoing tab was closed (§3: closing a tab does not save).setWorkspaceTabs(state => setAppTabSavedRoutePath(state, outgoingTabId, routePath))if (replacedByAnotherApp) return@@ -1315,6 +1334,13 @@ export function App() {// BEFORE `close()` destroys the webContents. `handleSandboxUiClosed` clears// the ref, so snapshot the outgoing tab id first.const outgoingTabId = liveSandboxUiTabIdRef.current+ // The handoff itself transitions the active embed; bump the generation (so an+ // older in-flight deactivation continuation aborts) and own the new one by+ // capturing AFTER the bump. If a newer activation supersedes this handoff+ // while the read below is in flight, gen no longer matches and both the+ // persist and the close are aborted.+ sandboxUiActivationGenRef.current += 1+ const gen = sandboxUiActivationGenRef.currentif (outgoingTabId !== null) {let routePath: string | undefinedtry {@@ -1323,8 +1349,10 @@ export function App() {} catch {routePath = undefined}+ if (gen !== sandboxUiActivationGenRef.current) returnsetWorkspaceTabs(state => setAppTabSavedRoutePath(state, outgoingTabId, routePath))}+ if (gen !== sandboxUiActivationGenRef.current) returnawait window.clerum.sandboxUi.close()handleSandboxUiClosed()}, [activeSandboxUiApp, handleSandboxUiClosed, setWorkspaceTabs])diff --git a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsxindex 94b20358c..e3be3721a 100644--- a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx+++ b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx@@ -1959,4 +1959,84 @@ describe('App deep-link orchestration', () => {expect(getSandboxUiLocation).toHaveBeenCalled()expect(closeSandboxUi).toHaveBeenCalled()})++ // R1-H1 (mini-spec 07): the deactivation continuation reads the outgoing+ // route over IPC and then closes the singleton embed. If the user reactivates+ // the SAME app tab before that read resolves, the stale continuation must not+ // tear down the just-reopened embed nor overwrite the live tab's route with+ // the value it read from the view that is already gone. The read is deferred+ // here so the reactivation lands while it is in flight. The resolved value uses+ // the same `{ appRef, routePath }` contract shape the rest of the suite uses+ // (pinned by sandboxUiDriver.test.ts); the gate aborts before routePath is read+ // on the raced path, so its exact value does not matter to this test.+ function createDeferred<T>(): { promise: Promise<T>; resolve: (value: T) => void } {+ let resolve!: (value: T) => void+ const promise = new Promise<T>(res => {+ resolve = res+ })+ return { promise, resolve }+ }++ it('does not close or overwrite an app tab reactivated before the stale getLocation resolves (R1-H1, T3)', async () => {+ currentController = makeController({ initialExperienceLoading: false })+ listApps.mockResolvedValue({ apps: [READY_APP] })+ render(<App />)+ await launchAppFromSidebar() // tabs: [chat, A]; A is live++ closeSandboxUi.mockClear()++ // Defer the deactivation read so App A can be reactivated while it is still+ // in flight. Only the deactivation continuation calls getLocation here+ // (reactivation early-returns before any read), so one deferred call is exact.+ const pendingLocation = createDeferred<{ appRef: string; routePath?: string }>()+ getSandboxUiLocation.mockReturnValueOnce(pendingLocation.promise)++ // Deactivate A → seeded chat tab: dispatches the read-then-close continuation.+ await selectTab(0)+ // Reactivate A before the old read resolves: a newer activation now owns the+ // active view and the embed is re-mounted for A.+ await selectTab(1)++ // The stale read finally resolves with A's earlier route.+ await act(async () => {+ pendingLocation.resolve({ appRef: 'ns/app', routePath: '/stale/route' })+ await Promise.resolve()+ await Promise.resolve()+ })++ // Observable result: the reopened embed was NOT torn down, and the live tab's+ // route was NOT clobbered by the stale value. (At the pre-fix head the stale+ // continuation closes the reopened embed and persists '/stale/route' onto A.)+ expect(closeSandboxUi).not.toHaveBeenCalled()+ expect(appTabs()[0]?.app?.savedRoutePath).not.toBe('/stale/route')+ })++ it('does not close a different app opened before the stale getLocation resolves (R1-H1, T3)', async () => {+ currentController = makeController({ initialExperienceLoading: false })+ listApps.mockResolvedValue({ apps: [READY_APP] })+ render(<App />)+ await launchAppFromSidebar() // tabs: [chat, A]; A is live++ closeSandboxUi.mockClear()++ const pendingLocation = createDeferred<{ appRef: string; routePath?: string }>()+ getSandboxUiLocation.mockReturnValueOnce(pendingLocation.promise)++ // Deactivate A → seeded chat tab (read in flight).+ await selectTab(0)+ // Open a DIFFERENT app tab (B) before the old read resolves; B becomes live.+ await launchAppFromSidebar()+ await waitFor(() => expect(appTabs()).toHaveLength(2))++ await act(async () => {+ pendingLocation.resolve({ appRef: 'ns/app', routePath: '/stale/route' })+ await Promise.resolve()+ await Promise.resolve()+ })++ // Observable result: B's embed survives (no close) and B's route is intact.+ // (At the pre-fix head the stale continuation closes the newly-opened B embed.)+ expect(closeSandboxUi).not.toHaveBeenCalled()+ expect(appTabs()[1]?.app?.savedRoutePath).toBeUndefined()+ })})