The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): move the main-is-occupied check into the session door
Brooklyn Nicholsonfb120f850dfc
diff --git a/apps/desktop/src/app/contrib/wiring.tsx b/apps/desktop/src/app/contrib/wiring.tsxindex 190e4b4acb5..559ccc90e0b 100644--- a/apps/desktop/src/app/contrib/wiring.tsx+++ b/apps/desktop/src/app/contrib/wiring.tsx@@ -76,7 +76,7 @@ import { useGatewayRequest } from '../gateway/hooks/use-gateway-request'import { useKeybinds } from '../hooks/use-keybinds'import { ModelPickerOverlay } from '../model-picker-overlay'import { ModelVisibilityOverlay } from '../model-visibility-overlay'-import { openSession } from '../open-session'+import { mainChatOccupied, openSession } 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'@@ -98,7 +98,7 @@ import { useRouteResume } from '../session/hooks/use-route-resume'import { useSessionActions } from '../session/hooks/use-session-actions'import { useSessionListActions } from '../session/hooks/use-session-list-actions'import { useSessionStateCache } from '../session/hooks/use-session-state-cache'-import { newSessionOpensTab, startWorkspaceSession } from '../session/workspace-session-target'+import { startWorkspaceSession } from '../session/workspace-session-target'import { useOverlayRouting } from '../shell/hooks/use-overlay-routing'import { useWindowControlsOverlayWidth } from '../shell/hooks/use-window-controls-overlay-width'import { titlebarControlsPosition } from '../shell/titlebar'@@ -493,12 +493,12 @@ export function ContribWiring({ children }: { children: ReactNode }) {// project so the new lane is visible.//// `openTab` is the sidebar "+" behavior: once a chat is loaded, stack a new- // tab instead of replacing it (see newSessionOpensTab). The composer's+ // tab instead of replacing it (see mainChatOccupied). The composer's// "branch off into a new worktree" flow keeps the fresh-draft path — it// prefills the MAIN composer right after, so it has to own that surface.const startSessionInWorkspace = useCallback((path: null | string, options?: { openTab?: boolean }) => {- if (options?.openTab && newSessionOpensTab(activeSessionIdRef.current, $selectedStoredSessionId.get())) {+ if (options?.openTab && mainChatOccupied(activeSessionIdRef.current, $selectedStoredSessionId.get())) {void openNewSessionTile('center', { cwd: path, listed: false })returndiff --git a/apps/desktop/src/app/open-session.test.ts b/apps/desktop/src/app/open-session.test.tsindex 5345cd8b24b..63d6ec5374f 100644--- a/apps/desktop/src/app/open-session.test.ts+++ b/apps/desktop/src/app/open-session.test.ts@@ -23,7 +23,30 @@ vi.mock('./routes', () => ({sessionRoute: (id: string) => `/c/${encodeURIComponent(id)}`}))-import { openSession, openSessionIntentFromModifiers } from './open-session'+import { openSession, openSessionIntentFromModifiers, mainChatOccupied } from './open-session'++/**+ * The question behind both the sidebar "+" and a palette open: is there a+ * conversation on main that must not be discarded? A create affordance stacks a+ * tab rather than replacing a chat that may still be mid-turn.+ */+describe('mainChatOccupied', () => {+ it('is occupied once a conversation is on screen', () => {+ expect(mainChatOccupied('runtime-a', 'stored-a')).toBe(true)+ })++ it('is occupied by a live runtime whose stored id has not landed yet', () => {+ expect(mainChatOccupied('runtime-a', null)).toBe(true)+ })++ it('is occupied by a selected session still resuming into a runtime', () => {+ expect(mainChatOccupied(null, 'stored-a')).toBe(true)+ })++ it('is free when nothing is open', () => {+ expect(mainChatOccupied(null, null)).toBe(false)+ })+})describe('openSessionIntentFromModifiers', () => {it('defaults to in-place', () => {diff --git a/apps/desktop/src/app/open-session.ts b/apps/desktop/src/app/open-session.tsindex e6582fe4e9e..a7db7efca3e 100644--- a/apps/desktop/src/app/open-session.ts+++ b/apps/desktop/src/app/open-session.ts@@ -20,6 +20,18 @@ export type OpenSessionIntent = 'in-place' | 'tab' | 'window'export type OpenSessionNavigate = (to: string, options?: { replace?: boolean }) => void+/**+ * Is the main tab holding a conversation worth preserving?+ *+ * A loaded chat may still be mid-turn, so replacing it with something else+ * throws away work the user can see. A blank draft has nothing to lose, which+ * is what lets the sidebar "+" take the cheaper main path instead of stacking+ * a tab nobody asked for.+ */+export function mainChatOccupied(activeSessionId: null | string, selectedStoredSessionId: null | string): boolean {+ return Boolean(activeSessionId || selectedStoredSessionId)+}+/** Read modifiers the way session rows do — meta OR ctrl for tab, +shift for window. */export function openSessionIntentFromModifiers(event?: null | { ctrlKey?: boolean; metaKey?: boolean; shiftKey?: boolean }diff --git a/apps/desktop/src/app/session/workspace-session-target.test.ts b/apps/desktop/src/app/session/workspace-session-target.test.tsindex 614227585c0..3a83f605a32 100644--- a/apps/desktop/src/app/session/workspace-session-target.test.ts+++ b/apps/desktop/src/app/session/workspace-session-target.test.ts@@ -9,7 +9,7 @@ import {setNewChatWorkspaceTarget} from '@/store/session'-import { newSessionOpensTab, startWorkspaceSession } from './workspace-session-target'+import { startWorkspaceSession } from './workspace-session-target'function deferred<T>() {let resolve!: (value: T) => void@@ -21,29 +21,6 @@ function deferred<T>() {return { promise, resolve }}-/**- * The sidebar "+" is a create affordance, not a discard one: with a chat- * already loaded it must stack a tab (⌘T) rather than replace the surface- * (⌘N), which would throw away a conversation that may still be mid-turn.- */-describe('newSessionOpensTab', () => {- it('opens a tab once a conversation is on screen', () => {- expect(newSessionOpensTab('runtime-a', 'stored-a')).toBe(true)- })-- it('opens a tab for a live runtime whose stored id has not landed yet', () => {- expect(newSessionOpensTab('runtime-a', null)).toBe(true)- })-- it('opens a tab for a selected session still resuming into a runtime', () => {- expect(newSessionOpensTab(null, 'stored-a')).toBe(true)- })-- it('replaces the empty surface when nothing is open', () => {- expect(newSessionOpensTab(null, null)).toBe(false)- })-})-describe('startWorkspaceSession', () => {afterEach(() => {setCurrentBranch('')diff --git a/apps/desktop/src/app/session/workspace-session-target.ts b/apps/desktop/src/app/session/workspace-session-target.tsindex 9a87d95c009..da5028502a1 100644--- a/apps/desktop/src/app/session/workspace-session-target.ts+++ b/apps/desktop/src/app/session/workspace-session-target.ts@@ -17,19 +17,6 @@ interface WorkspaceSessionOptions {startFreshSessionDraft: (options?: { workspaceTarget: string }) => void}-/**- * Should the sidebar "+" open a NEW TAB rather than replace what's on screen?- *- * "+" is a create affordance, never a discard one. Once a conversation is- * loaded, replacing it with a blank draft would throw away a chat that may- * still be mid-turn, so the button stacks a tab beside it (⌘T) instead of- * taking over the surface (⌘N). With nothing open there is no tab worth- * preserving and the cheaper fresh-draft path applies.- */-export function newSessionOpensTab(activeSessionId: null | string, selectedStoredSessionId: null | string): boolean {- return Boolean(activeSessionId || selectedStoredSessionId)-}-export function startWorkspaceSession({activeSessionIdRef,followActiveSessionCwd: followCwd = followActiveSessionCwd,