diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx index 30f2e109b4..8daf185775 100644 --- a/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx +++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/index.test.tsx @@ -349,6 +349,69 @@ describe('usePromptActions /title', () => { }) }) +describe('usePromptActions /stop', () => { + afterEach(() => { + cleanup() + vi.restoreAllMocks() + }) + + it('interrupts the target desktop turn before keeping the background-process cleanup', async () => { + const calls: Array<{ method: string; params?: Record }> = [] + + const requestGateway = vi.fn(async (method: string, params?: Record) => { + calls.push({ method, params }) + + if (method === 'session.interrupt') { + return { status: 'interrupted' } as never + } + + if (method === 'process.stop') { + return { killed: 0 } as never + } + + return {} as never + }) + + let handle: HarnessHandle | null = null + await actRender( + (handle = h)} refreshSessions={async () => undefined} requestGateway={requestGateway} /> + ) + + await handle!.submitText('/stop', { sessionId: 'rt-target-session' }) + + expect(calls).toEqual([ + { method: 'session.interrupt', params: { session_id: 'rt-target-session' } }, + { method: 'process.stop', params: {} } + ]) + expect(requestGateway).not.toHaveBeenCalledWith('slash.exec', expect.anything()) + expect(requestGateway).not.toHaveBeenCalledWith('command.dispatch', expect.anything()) + }) + + it('still stops background processes when the turn interrupt fails', async () => { + const requestGateway = vi.fn(async (method: string) => { + if (method === 'session.interrupt') { + throw new Error('interrupt failed') + } + + if (method === 'process.stop') { + return { killed: 2 } as never + } + + return {} as never + }) + + let handle: HarnessHandle | null = null + await actRender( + (handle = h)} refreshSessions={async () => undefined} requestGateway={requestGateway} /> + ) + + await handle!.submitText('/stop') + + expect(requestGateway).toHaveBeenCalledWith('session.interrupt', { session_id: RUNTIME_SESSION_ID }) + expect(requestGateway).toHaveBeenCalledWith('process.stop', {}) + }) +}) + // Helper: extract rendered text parts from captured updateSessionState seeds. function renderedSeedTexts(seeds: Record[]): string[] { return seeds.flatMap(state => { diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts index 1ee2bbe0a3..770f02ae9b 100644 --- a/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts +++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/slash.ts @@ -497,6 +497,53 @@ export function useSlashCommand(deps: SlashCommandDeps) { branch: async () => { await branchCurrentSession() }, + // Desktop owns the active turn, while the historical slash worker + // only stops background terminal processes. Interrupt the exact chat + // first (the same backend path as the composer Stop button), then keep + // the existing process cleanup so /stop retains both meanings. + stop: async ctx => { + const resolved = await withSlashOutput(ctx) + + if (!resolved) { + return + } + + const { render: renderSlashOutput, sessionId: initialSessionId, storedSessionId } = resolved + const lines: string[] = [] + + try { + await withSessionNotFoundResume( + initialSessionId, + storedSessionId, + liveId => requestGateway('session.interrupt', { session_id: liveId }), + { + requestGateway, + onRecovered: recoveredId => { + if (activeSessionIdRef.current === initialSessionId) { + activeSessionIdRef.current = recoveredId + setActiveSessionId(recoveredId) + } + } + } + ) + lines.push('Stopped the active turn.') + } catch (err) { + lines.push(`Could not stop the active turn: ${err instanceof Error ? err.message : String(err)}`) + } + + try { + const result = await requestGateway('process.stop', {}) + const processMessage = renderRpcResult(result, ctx.name) + + if (processMessage) { + lines.push(processMessage) + } + } catch (err) { + lines.push(`Could not stop background processes: ${err instanceof Error ? err.message : String(err)}`) + } + + renderSlashOutput(lines.join('\n')) + }, // /btw uses prompt.btw (the TUI's path). It must NOT go through // runExec: the slash worker prints the answer after process_command // returns, past the stdout capture window (#99065). The RPC replies diff --git a/apps/desktop/src/lib/desktop-slash-commands.test.ts b/apps/desktop/src/lib/desktop-slash-commands.test.ts index 004cfa85f7..a68e37a3c5 100644 --- a/apps/desktop/src/lib/desktop-slash-commands.test.ts +++ b/apps/desktop/src/lib/desktop-slash-commands.test.ts @@ -179,6 +179,13 @@ describe('desktop slash command curation', () => { expect(desktopSlashUnavailableMessage('/wake')).toBeNull() }) + it('routes /stop through the desktop action that cancels the active turn', () => { + expect(resolveDesktopCommand('/stop')?.surface).toEqual({ kind: 'action', action: 'stop' }) + expect(isDesktopSlashSuggestion('/stop')).toBe(true) + expect(isDesktopSlashCommand('/stop')).toBe(true) + expect(desktopSlashUnavailableMessage('/stop')).toBeNull() + }) + it('treats /browser as an executable action command (local-gateway connect)', () => { // /browser used to be terminal-only; it now resolves to a desktop action // handler that routes browser.manage RPC when the gateway is local. @@ -226,7 +233,7 @@ describe('desktop slash command curation', () => { }) it('keeps commands with richer CLI semantics on the slash worker', () => { - for (const name of ['/agents', '/steer', '/stop', '/usage']) { + for (const name of ['/agents', '/steer', '/usage']) { expect(resolveDesktopCommand(name)?.surface).toEqual({ kind: 'exec' }) } }) diff --git a/apps/desktop/src/lib/desktop-slash-commands.ts b/apps/desktop/src/lib/desktop-slash-commands.ts index d06495c0dc..79f295c780 100644 --- a/apps/desktop/src/lib/desktop-slash-commands.ts +++ b/apps/desktop/src/lib/desktop-slash-commands.ts @@ -65,6 +65,7 @@ export type DesktopActionId = | 'pet' | 'profile' | 'skin' + | 'stop' | 'title' | 'wake' | 'yolo' @@ -173,6 +174,11 @@ const rpc = ( const DESKTOP_COMMAND_SPECS: readonly DesktopCommandSpec[] = [ // Local client actions { name: '/new', description: 'Start a new desktop chat', aliases: ['/reset'], surface: action('new') }, + { + name: '/stop', + description: 'Stop the active turn and background processes', + surface: action('stop') + }, { name: '/branch', description: 'Branch the latest message into a new chat',