Skip to content

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

TypeScript

fix(desktop): r4-m1 preserve the outgoing tab route on a foreign location read

Diego Zacariasecadffc14fb4
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index e12c2d97e..0df02e5cc 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -124,6 +124,30 @@ const TRANSIENT_TEAM_CONTEXT_ERROR_CODES = new Set([
const SANDBOX_UI_DEEP_LINK_MANUAL_TEAM_CHANGE_MESSAGE =
'App link paused because you switched teams. Retry to open it from the current team, or dismiss it.'
+// §3 (mini-spec 08): what a `getLocation()` read means for the OUTGOING app
+// tab's saved route. `set`/`clear` come only from the tab's OWN app; `preserve`
+// (foreign read within one activation generation, or a failed read) leaves the
+// stored route untouched — the caller must NOT invoke the setter, because
+// passing `undefined` to it would erase A's previously-saved route.
+type OutgoingRouteDecision =
+ | { action: 'set'; routePath: string }
+ | { action: 'clear' }
+ | { action: 'preserve' }
+
+async function readOutgoingRouteDecision(
+ outgoingAppRef: string | undefined
+): Promise<OutgoingRouteDecision> {
+ try {
+ const location = await window.clerum.sandboxUi.getLocation()
+ if (!location || location.appRef !== outgoingAppRef) return { action: 'preserve' }
+ return location.routePath
+ ? { action: 'set', routePath: location.routePath }
+ : { action: 'clear' }
+ } catch {
+ return { action: 'preserve' }
+ }
+}
+
function errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error || 'Unknown error')
}
@@ -1148,28 +1172,26 @@ export function App() {
tab => tab.id === outgoingTabId && tab.kind === 'app'
)?.app?.appRef
void (async () => {
- let routePath: string | undefined
- try {
- const location = await window.clerum.sandboxUi.getLocation()
- // Persist the route ONLY when the read belongs to the outgoing tab's own
- // app. On an app→app switch the incoming `open()` can reach main before
- // this `getLocation()` resolves, so the read may surface the INCOMING
- // app's location; persisting it would save app B's route onto tab A. When
- // the appRef doesn't match, fall back to undefined (default route) rather
- // than clobber A with a foreign route. The generation gate below covers
- // the racing-reopen case; this covers a mismatched read within one gen.
- routePath = location && location.appRef === outgoingAppRef ? location.routePath : undefined
- } catch {
- routePath = undefined
- }
+ // §3 (mini-spec 08): only the outgoing tab's OWN app decides its saved
+ // route. On an app→app switch the incoming `open()` can reach main before
+ // this `getLocation()` resolves, so the read may surface the INCOMING app's
+ // location; a foreign or failed read must PRESERVE tab A's route (leave the
+ // store untouched), NOT clear it — passing `undefined` to the setter would
+ // erase A's previously-saved route. Persist only when the read is A's own:
+ // its routePath (set), or clear when A is back on its default path.
+ const decision = await readOutgoingRouteDecision(outgoingAppRef)
// 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))
+ // A foreign/unknown read owns no route on this tab — leave it as it was.
+ if (decision.action !== 'preserve') {
+ const nextRoutePath = decision.action === 'set' ? decision.routePath : undefined
+ // No-op when the outgoing tab was closed (§3: closing a tab does not save).
+ setWorkspaceTabs(state => setAppTabSavedRoutePath(state, outgoingTabId, nextRoutePath))
+ }
if (replacedByAnotherApp) return
try {
await window.clerum.sandboxUi.close()
@@ -1324,21 +1346,20 @@ export function App() {
sandboxUiActivationGenRef.current += 1
const gen = sandboxUiActivationGenRef.current
if (outgoingTabId !== null) {
- // Persist the route ONLY when the read belongs to the outgoing tab's own
- // app (same guard as the deactivation effect): a mismatched read must not
- // save another app's route onto this tab. appRef is immutable for the tab.
+ // §3 (mini-spec 08), identical to the deactivation effect: only the
+ // outgoing tab's OWN app may set/clear its saved route. A foreign or failed
+ // read PRESERVES it (the setter is not called) rather than clobbering it
+ // with another app's route or erasing it with `undefined`. appRef is
+ // immutable for the tab, so capturing it before the read is safe.
const outgoingAppRef = workspaceTabsRef.current.tabs.find(
tab => tab.id === outgoingTabId && tab.kind === 'app'
)?.app?.appRef
- let routePath: string | undefined
- try {
- const location = await window.clerum.sandboxUi.getLocation()
- routePath = location && location.appRef === outgoingAppRef ? location.routePath : undefined
- } catch {
- routePath = undefined
- }
+ const decision = await readOutgoingRouteDecision(outgoingAppRef)
if (gen !== sandboxUiActivationGenRef.current) return
- setWorkspaceTabs(state => setAppTabSavedRoutePath(state, outgoingTabId, routePath))
+ if (decision.action !== 'preserve') {
+ const nextRoutePath = decision.action === 'set' ? decision.routePath : undefined
+ setWorkspaceTabs(state => setAppTabSavedRoutePath(state, outgoingTabId, nextRoutePath))
+ }
}
if (gen !== sandboxUiActivationGenRef.current) return
await window.clerum.sandboxUi.close()
diff --git a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx
index 5b04e83a3..77fb57680 100644
--- a/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx
+++ b/desktop-app/ui/src/__tests__/App.deepLinks.test.tsx
@@ -11,6 +11,7 @@ import {
openChatTab,
openFilesTab,
openSettingsTab,
+ setAppTabSavedRoutePath,
} from '@lib/workspaceTabs'
import { mapKindToRoute, settingsSectionForRoute } from '@lib/workspaceTabsRoute'
import { App } from '@/App'
@@ -2146,4 +2147,83 @@ describe('App deep-link orchestration', () => {
expect(appA?.app?.savedRoutePath).not.toBe('/b/route')
expect(appA?.app?.savedRoutePath).toBeUndefined()
})
+
+ // R4-M1 (regression of R3-M1's fix 8a9c45f5d): the appRef guard fell back to
+ // `undefined` on a foreign read, and setAppTabSavedRoutePath treats `undefined`
+ // as an authoritative CLEAR — so a foreign/stale read did not merely "skip
+ // saving B", it ERASED A's own previously-saved route. §3 Option A: a foreign
+ // or failed read must PRESERVE (never call the setter). Both seam sites.
+ it('preserves the outgoing tab route when the deactivation read surfaces a foreign app (R4-M1, §3, T3)', async () => {
+ currentController = makeController({ initialExperienceLoading: false })
+ listApps.mockResolvedValue({ apps: [READY_APP, READY_APP_B] })
+ render(<App />)
+ await launchAppFromSidebar('ns/app') // tabs: [chat, A]; A live & active
+ const outgoingId = appTabs()[0]!.id
+
+ // Seed A's OWN saved route through the real store producer.
+ act(() => {
+ currentController.setWorkspaceTabs(state =>
+ setAppTabSavedRoutePath(state, outgoingId, '/a/own')
+ )
+ })
+ expect(appTabs()[0]?.app?.savedRoutePath).toBe('/a/own')
+
+ // app→app switch whose deactivation read resolves the INCOMING app's location
+ // within one activation generation (deferred so it lands after B is active).
+ getSandboxUiLocation.mockClear()
+ const pendingLocation = createDeferred<{ appRef: string; routePath?: string }>()
+ getSandboxUiLocation.mockReturnValueOnce(pendingLocation.promise)
+ await launchAppFromSidebar('ns/app-b') // deactivates A, reads (deferred)
+ await waitFor(() => expect(getSandboxUiLocation).toHaveBeenCalledTimes(1))
+ await act(async () => {
+ pendingLocation.resolve({ appRef: 'ns/app-b', routePath: '/b/route' })
+ await Promise.resolve()
+ await Promise.resolve()
+ })
+
+ // Liveness witness (M4): A's deactivation read genuinely ran.
+ expect(getSandboxUiLocation).toHaveBeenCalledTimes(1)
+ // Observable: A keeps its OWN route — the foreign read must not clear it.
+ // (Pre-fix, the mismatch fell back to undefined → setter CLEARED '/a/own'.)
+ const appA = currentController.workspaceTabs.tabs.find(tab => tab.id === outgoingId)
+ expect(appA?.app?.savedRoutePath).toBe('/a/own')
+ })
+
+ it('preserves the outgoing tab route when the handoff read surfaces a foreign app (R4-M1, §3, T3)', async () => {
+ const ensureTeamContext = vi.fn(async (): Promise<boolean> => true)
+ currentController = makeController({
+ initialExperienceLoading: false,
+ handleEnsureTeamContext: ensureTeamContext,
+ })
+ listApps.mockResolvedValue({ apps: [READY_APP] })
+ render(<App />)
+ await launchAppFromSidebar('ns/app') // A live & active
+ const outgoingId = appTabs()[0]!.id
+
+ act(() => {
+ currentController.setWorkspaceTabs(state =>
+ setAppTabSavedRoutePath(state, outgoingId, '/a/own')
+ )
+ })
+ expect(appTabs()[0]?.app?.savedRoutePath).toBe('/a/own')
+
+ // A cross-team deep link drives closeActiveSandboxUiEmbedForHandoff; its read
+ // of the outgoing embed surfaces a FOREIGN app's location.
+ getSandboxUiLocation.mockResolvedValue({ appRef: 'ns/app-b', routePath: '/b/route' })
+ await waitFor(() => expect(emitDeepLink).not.toBeNull())
+ act(() => {
+ emitDeepLink?.({ id: 1, appRef: 'ns/missing', teamId: 'team-b' })
+ })
+ await confirmPendingAppLink()
+ await waitFor(() => expect(getSandboxUiLocation).toHaveBeenCalled())
+ await act(async () => {
+ await Promise.resolve()
+ await Promise.resolve()
+ })
+
+ // Observable: the handoff must not erase A's own route on a foreign read.
+ // (Pre-fix, the mismatch fell back to undefined → setter CLEARED '/a/own'.)
+ const appA = currentController.workspaceTabs.tabs.find(tab => tab.id === outgoingId)
+ expect(appA?.app?.savedRoutePath).toBe('/a/own')
+ })
})