diff --git a/client/src/components/Chat/Input/CodeWorkspaceMenu.tsx b/client/src/components/Chat/Input/CodeWorkspaceMenu.tsx index 8fb84b56db..35638c973d 100644 --- a/client/src/components/Chat/Input/CodeWorkspaceMenu.tsx +++ b/client/src/components/Chat/Input/CodeWorkspaceMenu.tsx @@ -57,6 +57,12 @@ function describeTransition( transition: CodeWorkspaceTransition, localize: ReturnType, ): { label: string; info: string } { + if (transition.kind === 'detach') { + return { + label: localize('com_ui_code_workspace_detach'), + info: localize('com_ui_code_workspace_detach_info'), + }; + } if (transition.targets.some(({ state }) => state === 'missing')) { return { label: localize('com_ui_code_workspace_recover'), @@ -337,7 +343,9 @@ export default function CodeWorkspaceMenu({ * own item: this one only confirms a decision that still names at least one workspace. */ const proposed = transition == null ? [] : [...transition.retained, ...chosenTargets]; const offersMove = - transition != null && (transition.targets.length > 0 || transition.retained.length > 0); + transition != null && + transition.kind !== 'detach' && + (transition.targets.length > 0 || transition.retained.length > 0); const moveReady = transition != null && chosenTargets.length === transition.targets.length && proposed.length > 0; const applyTransition = (to: CodeWorkspaceSelection[]) => { diff --git a/client/src/components/Chat/Input/__tests__/CodeWorkspaceMenu.spec.tsx b/client/src/components/Chat/Input/__tests__/CodeWorkspaceMenu.spec.tsx index 8bbb5076a6..b1b7026cbe 100644 --- a/client/src/components/Chat/Input/__tests__/CodeWorkspaceMenu.spec.tsx +++ b/client/src/components/Chat/Input/__tests__/CodeWorkspaceMenu.spec.tsx @@ -683,7 +683,7 @@ describe('CodeWorkspaceMenu', () => { }); }); - describe('a chat sealed to a machine it can no longer reach', () => { + describe('a chat leaving its sealed workspace decision', () => { const mac = { environmentId: 'mac', workspaceId: 'primary' }; const sealed = { ...conversation, conversationId: 'existing' } as TConversation; @@ -691,60 +691,71 @@ describe('CodeWorkspaceMenu', () => { jest.restoreAllMocks(); }); - test.each(['offline', 'no-longer-used'])( - 'continues without the %s workspace', - async (scenario) => { - const moveSpy = jest - .spyOn(dataService, 'moveConversationCodeEnvironment') - .mockResolvedValue({ - conversationId: 'existing', - codeEnvironmentMode: 'without_attached', - }); - const setConversation = jest.fn(); - renderMenu( - , - ); + test.each<[string, CodeWorkspaceResult['state'], CodeWorkspaceResult['environments']]>([ + [ + 'offline', + 'unavailable', + [{ environment, state: 'unavailable', workspaces: [], selected: undefined }], + ], + ['no-longer-used', 'not_required', []], + [ + 'healthy', + 'ready', + [ + { + environment: { ...environment, id: 'mac' }, + state: 'ready', + workspaces: [{ id: 'primary' }], + selected: mac, + }, + ], + ], + ])('continues without the %s workspace', async (scenario, state, environments) => { + const moveSpy = jest.spyOn(dataService, 'moveConversationCodeEnvironment').mockResolvedValue({ + conversationId: 'existing', + codeEnvironmentMode: 'without_attached', + }); + const setConversation = jest.fn(); + renderMenu( + , + ); - await userEvent.click(screen.getByTestId('code-workspace')); - /** Nothing to move onto, so the only decision left is to stop waiting for the machine. */ - expect( - screen.queryByRole('menuitem', { name: /com_ui_code_workspace_move/ }), - ).not.toBeInTheDocument(); - await userEvent.click(screen.getByTestId('code-workspace-detach')); + await userEvent.click(screen.getByTestId('code-workspace')); + /** Detach is explicit; it must not offer a redundant move to the same workspace. */ + expect( + screen.queryByRole('menuitem', { name: /com_ui_code_workspace_move/ }), + ).not.toBeInTheDocument(); + await userEvent.click(screen.getByTestId('code-workspace-detach')); - await waitFor(() => expect(moveSpy).toHaveBeenCalledTimes(1)); - expect(moveSpy).toHaveBeenCalledWith({ conversationId: 'existing', from: [mac], to: [] }); - const update = setConversation.mock.calls[0][0]; - expect(update(sealed)).toEqual({ - ...sealed, - codeEnvironmentMode: 'without_attached', - codeWorkspaces: undefined, - }); - }, - ); + await waitFor(() => expect(moveSpy).toHaveBeenCalledTimes(1)); + expect(moveSpy).toHaveBeenCalledWith({ conversationId: 'existing', from: [mac], to: [] }); + const update = setConversation.mock.calls[0][0]; + expect(update(sealed)).toEqual({ + ...sealed, + codeEnvironmentMode: 'without_attached', + codeWorkspaces: undefined, + }); + }); }); describe('a chat sealed to a machine its agent no longer uses', () => { diff --git a/client/src/hooks/Agents/__tests__/useCodeWorkspace.test.tsx b/client/src/hooks/Agents/__tests__/useCodeWorkspace.test.tsx index 8c2fc27a78..0788826421 100644 --- a/client/src/hooks/Agents/__tests__/useCodeWorkspace.test.tsx +++ b/client/src/hooks/Agents/__tests__/useCodeWorkspace.test.tsx @@ -1550,13 +1550,34 @@ describe('useCodeWorkspace', () => { }); }); - it('keeps a reachable sealed workspace out of the composer', () => { + it('keeps a healthy attached workspace visible with an explicit detach action', () => { const kept = { environmentId: 'personal-vm', workspaceId: 'project-a' }; const { result } = renderHook(() => useCodeWorkspace(sealed([kept]))); expect(result.current.state).toBe('ready'); expect(result.current.canSubmit).toBe(true); + expect(result.current.transition).toMatchObject({ + kind: 'detach', + from: [kept], + retained: [kept], + targets: [], + detachable: true, + }); + expect(result.current.visible).toBe(true); + expect(result.current.resolveSubmission([kept], 'attached')).toEqual({ + codeEnvironmentMode: 'attached', + codeWorkspaces: [kept], + }); + }); + + it('does not offer healthy detach to a replica that only supports moves', () => { + mockStartupConfig.mockReturnValue({ + codeEnvironmentDecisionVersion: 1, + codeEnvironmentMoveVersion: 1, + }); + const kept = { environmentId: 'personal-vm', workspaceId: 'project-a' }; + const { result } = renderHook(() => useCodeWorkspace(sealed([kept]))); expect(result.current.transition).toBeUndefined(); expect(result.current.visible).toBe(false); }); diff --git a/client/src/hooks/Agents/useCodeWorkspace.ts b/client/src/hooks/Agents/useCodeWorkspace.ts index c19fe6a6b2..d517254933 100644 --- a/client/src/hooks/Agents/useCodeWorkspace.ts +++ b/client/src/hooks/Agents/useCodeWorkspace.ts @@ -74,9 +74,10 @@ export interface CodeWorkspaceEnvironmentResult { * often because an agent was pointed at a different machine or its saved workspace disappeared. * - `attach`: the chat has been running without an attached environment and can now take one, so * switching a saved chat to a coding agent is a transition rather than a dead end. + * - `detach`: the attached workspace is healthy, but its owner wants to continue ordinary chat. */ export interface CodeWorkspaceTransition { - kind: 'move' | 'attach'; + kind: 'move' | 'attach' | 'detach'; conversationId: string; /** The persisted selections this replaces, exactly as the conversation stores them; empty for a * chat that has been running without an attached environment. */ @@ -89,9 +90,7 @@ export interface CodeWorkspaceTransition { retained: CodeWorkspaceSelection[]; /** Environments needing a new selection, including those whose workspace disappeared. */ targets: CodeWorkspaceEnvironmentResult[]; - /** Whether the chat may leave attached execution and continue without a workspace. Offered when - * the machine it sealed is no longer usable, so an unreachable worker never silently becomes a - * changed execution mode and never strands the composer either. */ + /** Leaving attached execution is always explicit, whether its machine is healthy or unavailable. */ detachable: boolean; } @@ -634,10 +633,16 @@ export default function useCodeWorkspace( ) state = 'relocatable'; } + } else if ( + supportsEnvironmentTransitions && + inferredMode === 'attached' && + (storedSelections?.length ?? 0) > 0 && + state === 'ready' + ) { + transition = { ...base, kind: 'detach', detachable: true }; } } - /** A sealed chat hides the control once its decision needs nothing from its owner, except while - * it runs without a workspace: that state is worth naming, and attaching one starts here. */ + /** Keep explicit transitions visible without offering mutable picks for a sealed decision. */ const visible = recovery != null || transition != null ||