Skip to content

The agent that grows with you

fix: preserve bot scope when resuming sessions

Vitor Cepeda Lopes8c26ca2a5ee8
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsx
index 25fbb64a9a0..02777b7cf65 100644
--- a/apps/desktop/src/app/contrib/wiring.tsx
+++ b/apps/desktop/src/app/contrib/wiring.tsx
@@ -107,7 +107,7 @@ import { useKeybinds } from '../hooks/use-keybinds'
import { useHudHandoff } from '../hud/handoff'
import { ModelPickerOverlay } from '../model-picker-overlay'
import { ModelVisibilityOverlay } from '../model-visibility-overlay'
-import { mainChatOccupied, openSession } from '../open-session'
+import { mainChatOccupied, openSession, openSessionFromPicker } from '../open-session'
import { PetGenerateOverlay } from '../pet-generate/pet-generate-overlay'
import { FileActionDialogs } from '../right-sidebar/file-actions'
import { RemoteFolderPicker } from '../right-sidebar/files/remote-picker'
@@ -1358,7 +1358,7 @@ export function ContribWiring({ children }: { children: ReactNode }) {
profile={activeGatewayProfile}
requestGateway={requestGateway}
/>
- <SessionPickerOverlay onResume={sessionId => openSession(sessionId, navigate)} />
+ <SessionPickerOverlay onResume={sessionId => openSessionFromPicker(sessionId, navigate)} />
<ModelVisibilityOverlay
gateway={gateway || undefined}
onOpenProviders={openProviderSettings}
diff --git a/apps/desktop/src/app/open-session.test.ts b/apps/desktop/src/app/open-session.test.ts
index bbc99fa1aa4..9be492a136a 100644
--- a/apps/desktop/src/app/open-session.test.ts
+++ b/apps/desktop/src/app/open-session.test.ts
@@ -4,6 +4,9 @@ const focusOpenSession = vi.fn()
const openSessionTile = vi.fn()
const reuseBlankDraftTile = vi.fn()
const setSessionTileWorkspaceScope = vi.fn()
+const focusedSessionWorkspaceScope = vi.fn<
+ () => { workspaceMode: 'bots' | 'sessions'; workspaceOwnerKey?: string }
+>(() => ({ workspaceMode: 'sessions' }))
const openSessionInNewWindow = vi.fn()
const canOpenSessionWindow = vi.fn(() => true)
const workspaceIsPageGet = vi.fn(() => false)
@@ -11,6 +14,7 @@ const workspaceIsPageGet = vi.fn(() => false)
vi.mock('@/store/session-states', () => ({
focusedSessionNeedsRoute: (focused: 'main' | 'tile' | null, workspaceIsPage: boolean) =>
!focused || (focused === 'main' && workspaceIsPage),
+ focusedSessionWorkspaceScope: () => focusedSessionWorkspaceScope(),
focusOpenSession: (...args: unknown[]) => focusOpenSession(...args),
openSessionTile: (...args: unknown[]) => openSessionTile(...args),
reuseBlankDraftTile: (...args: unknown[]) => reuseBlankDraftTile(...args),
@@ -29,7 +33,7 @@ vi.mock('./routes', () => ({
import { $activeSessionId, $selectedStoredSessionId } from '@/store/session'
-import { mainChatOccupied, openSession, openSessionIntentFromModifiers } from './open-session'
+import { mainChatOccupied, openSession, openSessionFromPicker, openSessionIntentFromModifiers } from './open-session'
/**
* The question behind both the sidebar "+" and a palette open: is there a
@@ -92,6 +96,8 @@ describe('openSession', () => {
workspaceIsPageGet.mockReturnValue(false)
reuseBlankDraftTile.mockReset()
setSessionTileWorkspaceScope.mockReset()
+ focusedSessionWorkspaceScope.mockReset()
+ focusedSessionWorkspaceScope.mockReturnValue({ workspaceMode: 'sessions' })
$activeSessionId.set(null)
$selectedStoredSessionId.set(null)
})
@@ -217,6 +223,42 @@ describe('openSession', () => {
expect(openSessionTile).not.toHaveBeenCalled()
})
+ it('stack spends the focused Bot-scoped blank draft even when main is empty', () => {
+ const scope = { workspaceMode: 'bots' as const, workspaceOwnerKey: 'connection-a::default' }
+
+ focusOpenSession.mockReturnValue(null)
+ reuseBlankDraftTile.mockReturnValue(true)
+ openSession('s1', navigate, 'stack', scope)
+
+ expect(reuseBlankDraftTile).toHaveBeenCalledWith('s1', scope)
+ expect(openSessionTile).not.toHaveBeenCalled()
+ expect(navigate).not.toHaveBeenCalled()
+ })
+
+ it('picker resumes into the focused Bot workspace instead of the Sessions main', () => {
+ const scope = { workspaceMode: 'bots' as const, workspaceOwnerKey: 'connection-a::default' }
+
+ focusedSessionWorkspaceScope.mockReturnValue(scope)
+ focusOpenSession.mockReturnValue(null)
+ reuseBlankDraftTile.mockReturnValue(true)
+ openSessionFromPicker('s1', navigate)
+
+ expect(focusedSessionWorkspaceScope).toHaveBeenCalledOnce()
+ expect(reuseBlankDraftTile).toHaveBeenCalledWith('s1', scope)
+ expect(navigate).not.toHaveBeenCalled()
+ })
+
+ it('picker keeps its existing in-place behavior in the Sessions workspace', () => {
+ $activeSessionId.set('runtime-current')
+ focusedSessionWorkspaceScope.mockReturnValue({ workspaceMode: 'sessions' })
+ focusOpenSession.mockReturnValue(null)
+
+ openSessionFromPicker('s1', navigate)
+
+ expect(openSessionTile).not.toHaveBeenCalled()
+ expect(navigate).toHaveBeenCalledWith('/c/s1')
+ })
+
it('window pops out when the bridge supports it', () => {
openSession('s1', navigate, 'window')
expect(openSessionInNewWindow).toHaveBeenCalledWith('s1')
diff --git a/apps/desktop/src/app/open-session.ts b/apps/desktop/src/app/open-session.ts
index 5fa34b09b58..3173aa7499c 100644
--- a/apps/desktop/src/app/open-session.ts
+++ b/apps/desktop/src/app/open-session.ts
@@ -19,6 +19,7 @@ import { $activeSessionId, $selectedStoredSessionId, markSessionRead } from '@/s
import type { SessionProfileRoute } from '@/store/session-request-router'
import {
focusedSessionNeedsRoute,
+ focusedSessionWorkspaceScope,
focusOpenSession,
openSessionTile,
reuseBlankDraftTile,
@@ -75,6 +76,15 @@ export function openSessionIntentFromModifiers(
return base
}
+/** Picker selection preserves the workspace of the tab that opened it without
+ * changing the established Sessions behavior. */
+export function openSessionFromPicker(storedSessionId: string, navigate: OpenSessionNavigate): void {
+ const workspaceScope = focusedSessionWorkspaceScope()
+ const intent = workspaceScope.workspaceMode === 'bots' ? 'stack' : 'in-place'
+
+ openSession(storedSessionId, navigate, intent, workspaceScope)
+}
+
/**
* @param navigate Required for `in-place` (route into main when not on screen).
* `tab` / `window` ignore it — pass a no-op when you don't have a router handle.
@@ -126,7 +136,13 @@ export function openSession(
let spendBlankDraft = false
if (resolved === 'stack') {
- spendBlankDraft = mainChatOccupied($activeSessionId.get(), $selectedStoredSessionId.get())
+ // A Bot-scoped picker is already inside a session tab, so its blank draft
+ // is the surface the user expects `/resume` to replace. Main may be empty
+ // or hidden behind the Bots workspace; treating that as an in-place open
+ // routes the saved chat elsewhere while leaving the visible blank tab
+ // active. Force the tab path so it first focuses an existing target, then
+ // spends the scoped blank draft before stacking a new tab.
+ spendBlankDraft = Boolean(botWorkspaceScope) || mainChatOccupied($activeSessionId.get(), $selectedStoredSessionId.get())
resolved = spendBlankDraft ? 'tab' : 'in-place'
}
diff --git a/apps/desktop/src/store/session-states.test.ts b/apps/desktop/src/store/session-states.test.ts
index cd78911b3b0..c2828a661a4 100644
--- a/apps/desktop/src/store/session-states.test.ts
+++ b/apps/desktop/src/store/session-states.test.ts
@@ -29,6 +29,7 @@ import {
clearAllSessionStates,
closeAllOpenSessionTiles,
focusedSessionNeedsRoute,
+ focusedSessionWorkspaceScope,
focusOpenSession,
focusWorkspaceOwnerSessionTile,
foregroundSessionScopes,
@@ -355,6 +356,20 @@ describe('SessionTile workspace scope', () => {
setSessions([])
})
+ it('reports the workspace scope of the focused Bot session tab', () => {
+ const scope = {
+ ownerRoute: { connectionId: 'connection-a', profile: 'default' },
+ workspaceMode: 'bots' as const,
+ workspaceOwnerKey: 'connection-a::default',
+ workspaceTabTitle: 'Bot Chat'
+ }
+
+ openSessionTile('bot-chat', 'center', undefined, undefined, scope)
+ $layoutTree.set(group(['workspace', tilePane('bot-chat')], { active: tilePane('bot-chat'), id: 'main' }))
+
+ expect(focusedSessionWorkspaceScope()).toEqual(scope)
+ })
+
it('keeps Bot tabs while a profile publication swaps the Sessions bucket', () => {
const scope = { workspaceMode: 'bots' as const, workspaceOwnerKey: 'connection-a::writer' }
diff --git a/apps/desktop/src/store/session-states.ts b/apps/desktop/src/store/session-states.ts
index f64618c328e..41c8967abe1 100644
--- a/apps/desktop/src/store/session-states.ts
+++ b/apps/desktop/src/store/session-states.ts
@@ -1802,6 +1802,29 @@ export function focusedSessionNeedsRoute(focused: 'main' | 'tile' | null, worksp
return !focused || (focused === 'main' && workspaceIsPage)
}
+/** Presentation scope of the session tab the user is currently acting from.
+ * Picker actions must preserve this scope: a `/resume` opened from Bot Mode is
+ * still a Bot tab with its exact owner route, not a Sessions-main navigation. */
+export function focusedSessionWorkspaceScope(): SessionTileWorkspaceScope {
+ const paneId = focusedSessionTabAnchor()
+
+ if (paneId?.startsWith(TILE_PANE_PREFIX)) {
+ const storedSessionId = paneId.slice(TILE_PANE_PREFIX.length)
+ const tile = $sessionTiles.get().find(candidate => candidate.storedSessionId === storedSessionId)
+
+ if (tile?.workspaceMode === 'bots') {
+ return {
+ ...(tile.ownerRoute ? { ownerRoute: tile.ownerRoute } : {}),
+ workspaceMode: 'bots',
+ ...(tile.workspaceOwnerKey ? { workspaceOwnerKey: tile.workspaceOwnerKey } : {}),
+ ...(tile.workspaceTabTitle ? { workspaceTabTitle: tile.workspaceTabTitle } : {})
+ }
+ }
+ }
+
+ return { workspaceMode: 'sessions' }
+}
+
/** The open tab that's still an empty "New session" draft, if there is one.
* That tab is the one the user would have typed into, so an open-from-nowhere
* spends it instead of stacking a second blank tab beside it. Most recent