Skip to content

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

TypeScript

fix(desktop): stop the drawer toggle from stacking blank chats (§1)

Diego Zacarias992ede090a98
diff --git a/desktop-app/ui/src/App.tsx b/desktop-app/ui/src/App.tsx
index f483c5822..fd03d866f 100644
--- a/desktop-app/ui/src/App.tsx
+++ b/desktop-app/ui/src/App.tsx
@@ -524,6 +524,17 @@ export function App() {
// tab). The reconcile effect adds the chat tab to the switcher.
const origin = sandboxUiConversationOriginRef.current
const hasActiveChat = Boolean(vm.selectedAgent && vm.activeChatId)
+ // §1: with no active chat and no launch origin (the drawer opened over a
+ // files/settings tab), restore the LAST active chat tab instead of seeding a
+ // blank — pointing the controller at its persisted (agentRef, chatId). Only a
+ // persisted chat is restored; a blank last-tab falls through to the blank
+ // seed below (which the reconcile then reuses, never duplicates).
+ const lastChatTab =
+ !hasActiveChat && !origin && vm.lastActiveChatTabId
+ ? workspaceTabsRef.current.tabs.find(
+ tab => tab.id === vm.lastActiveChatTabId && tab.kind === 'chat'
+ )
+ : undefined
if (origin && !hasActiveChat) {
vm.handleSelectChatAgent(origin.agentName, {
chatId: origin.chatId,
@@ -531,6 +542,13 @@ export function App() {
selectLatest: false,
keepNavItem: true,
})
+ } else if (lastChatTab?.chat?.agentRef && lastChatTab.chat.chatId) {
+ vm.handleSelectChatAgent(lastChatTab.chat.agentRef, {
+ chatId: lastChatTab.chat.chatId,
+ title: lastChatTab.title,
+ selectLatest: false,
+ keepNavItem: true,
+ })
} else if (vm.selectedAgent) {
// Re-reveal the current drawer chat in place.
vm.handleSelectChatAgent(vm.selectedAgent, {
@@ -540,7 +558,7 @@ export function App() {
})
}
setComposerFocusRequestId(value => value + 1)
- }, [vm.activeChatId, vm.handleSelectChatAgent, vm.selectedAgent])
+ }, [vm.activeChatId, vm.handleSelectChatAgent, vm.lastActiveChatTabId, vm.selectedAgent])
const closeChatDrawer = React.useCallback(() => {
setChatDrawerOpen(false)
diff --git a/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx b/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
index 874b87492..bad70024c 100644
--- a/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
+++ b/desktop-app/ui/src/__tests__/App.chatDrawer.test.tsx
@@ -1385,6 +1385,40 @@ describe('App chat drawer — universal availability (mini-spec 04a)', () => {
expect(rail.style.getPropertyValue('--rail-top')).toBe('128px')
})
+ // §1 (mini-spec 06): toggling the drawer over a non-chat tab must not spawn a
+ // fresh "New chat" tab each time. The reconcile runs with the app/files tab
+ // kept active, so the blank chat is never the active tab — without the reuse
+ // fix every toggle appends another blank. Assert the observable strip's blank
+ // chat count (T4), not an intermediate effect.
+ it('never stacks blank chat tabs when the drawer is toggled over a non-chat tab (§1)', () => {
+ currentController = makeController({
+ selectedAgent: 'alpha',
+ activeChatId: null,
+ navItem: DESKTOP_ROUTES.files,
+ chatList: CHAT_LIST,
+ } as Partial<AppController>)
+ render(<App />)
+
+ const blankChatCount = () =>
+ currentController.workspaceTabs.tabs.filter(
+ t => t.kind === 'chat' && (t.chat?.chatId ?? null) === null
+ ).length
+
+ // Boot seeds exactly one blank chat tab; opening files adds none.
+ expect(blankChatCount()).toBe(1)
+
+ // Toggle open + closed three times over the files tab, then leave it open.
+ for (let i = 0; i < 3; i += 1) {
+ act(() => appHeaderHarness.props?.onToggleChatDrawer?.())
+ act(() => appHeaderHarness.props?.onToggleChatDrawer?.())
+ }
+ act(() => appHeaderHarness.props?.onToggleChatDrawer?.())
+ expect(appHeaderHarness.props?.chatDrawerOpen).toBe(true)
+
+ // Still exactly one blank chat tab — the toggles never stacked new ones.
+ expect(blankChatCount()).toBe(1)
+ })
+
// R5 + DEC-2: the drawer is global; switching between non-chat kinds keeps it
// open and never lets the chat reconcile steal focus onto a chat tab.
it('keeps the drawer intact across non-chat tab switches without stealing focus (R5/DEC-2)', () => {
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useNavigationController.test.ts b/desktop-app/ui/src/hooks/domain/__tests__/useNavigationController.test.ts
index 4eee6fdfd..d9489d12c 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useNavigationController.test.ts
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useNavigationController.test.ts
@@ -2,6 +2,7 @@
import { describe, expect, it } from 'vitest'
import { act, renderHook } from '@testing-library/react'
import { AGENT_WORKSPACE_ROUTES, DESKTOP_ROUTES } from '../../../constants/navigation'
+import { closeWorkspaceTab, openChatTab } from '../../../lib/workspaceTabs'
import { useNavigationController } from '../useNavigationController'
describe('useNavigationController — agent-centric navigation (Fase 2)', () => {
@@ -26,6 +27,39 @@ describe('useNavigationController — agent-centric navigation (Fase 2)', () =>
expect(result.current.selectedAgentRoute).toBe('mcp-servers')
})
+ it('tracks the last active chat tab and keeps it across nav to a non-chat tab (§1)', () => {
+ const { result } = renderHook(() => useNavigationController())
+ // Boot seeds an active blank chat tab -> it is the last active chat tab.
+ expect(result.current.lastActiveChatTabId).toBe('chat-tab-1')
+
+ act(() => result.current.handleNavSelect(DESKTOP_ROUTES.files))
+ expect(result.current.activeTab?.kind).toBe('files')
+ // Navigating to files does NOT forget the chat to return to.
+ expect(result.current.lastActiveChatTabId).toBe('chat-tab-1')
+ })
+
+ it('updates to the newly active chat and forgets it only when that tab closes (§1)', () => {
+ const { result } = renderHook(() => useNavigationController())
+ // Go to files first so the persisted chat opens as its OWN tab (no collapse
+ // into the boot blank), then activate it.
+ act(() => result.current.handleNavSelect(DESKTOP_ROUTES.files))
+ act(() =>
+ result.current.setWorkspaceTabs(state =>
+ openChatTab(state, { id: 'persisted', agentRef: 'alpha', chatId: 'c1' })
+ )
+ )
+ expect(result.current.lastActiveChatTabId).toBe('persisted')
+
+ // Back to files: still remembered.
+ act(() => result.current.handleNavSelect(DESKTOP_ROUTES.files))
+ expect(result.current.activeTab?.kind).toBe('files')
+ expect(result.current.lastActiveChatTabId).toBe('persisted')
+
+ // Closing the remembered (non-active) chat tab forgets it.
+ act(() => result.current.setWorkspaceTabs(state => closeWorkspaceTab(state, 'persisted')))
+ expect(result.current.lastActiveChatTabId).toBeNull()
+ })
+
it('no longer exposes the removed context/teams handlers or state', () => {
const { result } = renderHook(() => useNavigationController())
const controller = result.current as Record<string, unknown>
diff --git a/desktop-app/ui/src/hooks/domain/useNavigationController.ts b/desktop-app/ui/src/hooks/domain/useNavigationController.ts
index 7387e64a6..f72ca8042 100644
--- a/desktop-app/ui/src/hooks/domain/useNavigationController.ts
+++ b/desktop-app/ui/src/hooks/domain/useNavigationController.ts
@@ -1,4 +1,4 @@
-import { useCallback, useMemo, useRef, useState } from 'react'
+import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { AGENT_WORKSPACE_ROUTES, DESKTOP_ROUTES } from '../../constants/navigation'
import {
activeWorkspaceTab,
@@ -47,6 +47,20 @@ export function useNavigationController() {
const activeTab = useMemo(() => activeWorkspaceTab(workspaceTabs), [workspaceTabs])
const navItem: NavItem = appsPickerActive ? DESKTOP_ROUTES.apps : mapKindToRoute(activeTab)
+ // The last chat tab that was active — the "chat to return to" when the drawer
+ // is reopened over a non-chat tab (mini-spec 06 §1). It is NOT cleared by
+ // navigating to files/app/settings (that is the whole point: the drawer
+ // restores it); it is forgotten only when that tab is closed. Derived from the
+ // single store, so every writer (nav, reveal, reconcile) updates it uniformly.
+ const [lastActiveChatTabId, setLastActiveChatTabId] = useState<string | null>(null)
+ useEffect(() => {
+ setLastActiveChatTabId(prev => {
+ if (activeTab?.kind === 'chat') return activeTab.id
+ if (prev !== null && !workspaceTabs.tabs.some(tab => tab.id === prev)) return null
+ return prev
+ })
+ }, [activeTab, workspaceTabs])
+
const clearAppsPicker = useCallback(() => setAppsPickerActive(false), [])
// Apps route with no launched app → the picker residual. Redundant (and thus
@@ -174,6 +188,7 @@ export function useNavigationController() {
showAppsPicker,
clearAppsPicker,
activateChatTab,
+ lastActiveChatTabId,
// Agent/chat selection state (stays here through this slice; §4).
selectedAgent,
selectedAgentRoute,
diff --git a/desktop-app/ui/src/hooks/useAppController.ts b/desktop-app/ui/src/hooks/useAppController.ts
index a000b6c2a..d5440d2db 100644
--- a/desktop-app/ui/src/hooks/useAppController.ts
+++ b/desktop-app/ui/src/hooks/useAppController.ts
@@ -1349,6 +1349,7 @@ export function useAppController() {
showAppsPicker: nav.showAppsPicker,
clearAppsPicker: nav.clearAppsPicker,
activateWorkspaceChatTab: nav.activateChatTab,
+ lastActiveChatTabId: nav.lastActiveChatTabId,
handleOpenAgentWorkspace,
handleSelectChatAgent,
handleEnsureTeamContext: ensureTeamContext,
diff --git a/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts b/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts
index 9838e9739..0adb9871d 100644
--- a/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/workspaceTabs.test.ts
@@ -563,6 +563,44 @@ describe('workspaceTabs — reconcile (chat sub-slice, §3)', () => {
)
})
+ // §1 (mini-spec 06): the drawer-toggle duplication. In drawer mode App wraps
+ // reconcile so the app/files tab STAYS active (activating a chat tab would tear
+ // the embed down). Model that seam by resetting `activeTabId` to the non-chat
+ // tab after each reconcile: a chatId:null reconcile must then reuse the single
+ // blank chat tab, never append a new one per toggle. Assert the observable tab
+ // list (T4). Against the parent this appends one blank per event and blows the
+ // cap.
+ it('the drawer seam never stacks blank chat tabs across repeated blank reconciles (§1)', () => {
+ fc.assert(
+ fc.property(
+ fc.boolean(),
+ fc.array(fc.constantFrom('alpha', 'beta', 'gamma'), { minLength: 1, maxLength: 30 }),
+ (startWithBlank, agents) => {
+ let state = createEmptyWorkspaceTabsState()
+ if (startWithBlank) state = newChatTab(state, 'boot-blank', 'alpha')
+ state = openFilesTab(state, { id: 'files' }) // files becomes the active tab
+ const countBlanks = (s: WorkspaceTabsState) =>
+ s.tabs.filter(t => t.kind === 'chat' && (t.chat?.chatId ?? null) === null).length
+ // Invariant: at most one blank, and a new one only if none existed.
+ const cap = Math.max(1, countBlanks(state))
+ let counter = 0
+ for (const agent of agents) {
+ state = reconcileWorkspaceChatTab(
+ state,
+ { agentRef: agent, chatId: null },
+ `g-${counter++}`
+ )
+ // The drawer seam keeps the non-chat tab active.
+ state = { ...state, activeTabId: 'files' }
+ expect(countBlanks(state)).toBeLessThanOrEqual(cap)
+ }
+ // The non-chat tab is never dropped by blank chat reconciliation.
+ expect(state.tabs.some(t => t.id === 'files' && t.kind === 'files')).toBe(true)
+ }
+ )
+ )
+ })
+
it('reconcile leaves non-chat tabs untouched and re-homes focus to the chat tab', () => {
let state = createEmptyWorkspaceTabsState()
state = openAppTab(state, { id: 'app-1', appRef: 'x' })
diff --git a/desktop-app/ui/src/lib/workspaceTabs.ts b/desktop-app/ui/src/lib/workspaceTabs.ts
index 21e164754..9061a5165 100644
--- a/desktop-app/ui/src/lib/workspaceTabs.ts
+++ b/desktop-app/ui/src/lib/workspaceTabs.ts
@@ -345,6 +345,27 @@ export function reconcileWorkspaceChatTab(
activeTabId: current.id,
}
}
+ // The active tab is NOT a blank chat (a non-chat tab, or a persisted chat).
+ // Reuse an existing blank chat tab instead of spawning another (mini-spec 06
+ // §1): in drawer mode the effect keeps the app/files tab active, so the
+ // blank is never the active tab and, without this, every reconcile would
+ // `appendBlankChatTab` and stack "New chat" tabs. Only append when there is
+ // no blank chat tab at all — the invariant is "+1 blank at most, and only if
+ // none existed". Focus the reused blank (as append activates the new one);
+ // the drawer seam then keeps its non-chat tab active.
+ const existingBlank = state.tabs.find(isBlankChatTab)
+ if (existingBlank) {
+ return existingBlank.chat?.agentRef === active.agentRef
+ ? { ...state, activeTabId: existingBlank.id }
+ : {
+ tabs: state.tabs.map(tab =>
+ tab.id === existingBlank.id
+ ? { ...tab, chat: { agentRef: active.agentRef, chatId: null } }
+ : tab
+ ),
+ activeTabId: existingBlank.id,
+ }
+ }
return appendBlankChatTab(state, newTabId, active.agentRef)
}