feat(session): surface background shell and subagent work (#360)

* feat(session): surface background shell and subagent work

* fix(session): address review feedback on background work UI
This commit is contained in:
Chris Scott authored and GitHub committed 2026-09-26 15:49:49 -04:00
1 parent 8b806e865f
commit 1389e8e59c
20 files changed
+802 -13

No files matched your search

+10
View File
@@ -8,6 +8,7 @@ import {
renameSession,
activateSkill,
interruptSession,
backgroundSession,
listAgents,
runShell,
sendPrompt,
@@ -375,6 +376,15 @@ export const useSendPrompt = (directory?: string) => {
});
};
export const useBackgroundSession = () => {
return useMutation({
mutationFn: (sessionID: string) => backgroundSession(sessionID),
onError: (error) => {
showToast.error(error instanceof Error ? error.message : "Failed to move work to the background");
},
});
};
const ABORT_RETRY_INTERVAL_MS = 3000;
const MAX_ABORT_RETRIES = 10;
+57
View File
@@ -491,6 +491,63 @@ describe('useSSE', () => {
expect(invalidateQueries).not.toHaveBeenCalledWith({
queryKey: ['opencode', 'pending-actions', 'session-1', '/repo'],
})
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'shells'] })
unmount()
})
it('tracks background shells from shell lifecycle events', async () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
const running = (id: string) => ({
id,
status: 'running',
command: `sleep ${id}`,
cwd: '/repo',
shell: 'zsh',
file: `/tmp/${id}.log`,
metadata: { sessionID: 'session-1' },
time: { started: 1 },
})
queryClient.setQueryData(['opencode', 'shells', '/repo'], [running('shell-1')])
const { result, unmount } = renderHook(
() => useSSE('/repo', 'session-1'),
{ wrapper: createWrapper(queryClient) }
)
await waitFor(() => expect(MockEventSource.instances).toHaveLength(1))
await connect(0, 'client-1')
await waitFor(() => expect(result.current.isConnected).toBe(true))
act(() => {
MockEventSource.instances[0].emit('message', {
type: 'shell.created',
directory: '/repo',
data: { info: running('shell-2') },
})
MockEventSource.instances[0].emit('message', {
type: 'shell.exited',
directory: '/repo',
data: { id: 'shell-1', exit: 0, status: 'exited' },
})
})
expect(queryClient.getQueryData<Array<{ id: string }>>(['opencode', 'shells', '/repo'])?.map((shell) => shell.id))
.toEqual(['shell-2'])
act(() => {
MockEventSource.instances[0].emit('message', {
type: 'shell.deleted',
directory: '/repo',
data: { id: 'shell-2' },
})
})
expect(queryClient.getQueryData(['opencode', 'shells', '/repo'])).toEqual([])
unmount()
})
+20 -2
View File
@@ -1,13 +1,13 @@
import { useEffect, useRef, useState, useCallback, useMemo } from 'react'
import { useQueryClient } from '@tanstack/react-query'
import type { SessionInfo, V2Event } from '@opencode-manager/shared/opencode'
import { invalidateSessionListCaches, invalidateSessionListCachesDebounced } from '@/lib/queryInvalidation'
import { invalidateSessionListCaches, invalidateSessionListCachesDebounced, shellsQueryKey } from '@/lib/queryInvalidation'
import { showToast } from '@/lib/toast'
import { useSessionStatus } from '@/stores/sessionStatusStore'
import { useSendErrorStore } from '@/stores/sendErrorStore'
import { openCodeEventStream } from '@/lib/opencode-event-stream'
import type { EventStreamSubscription } from '@/lib/opencode-event-stream'
import { listActiveSessions } from '@/api/opencode'
import { listActiveSessions, type ShellInfo } from '@/api/opencode'
const STATUS_POLL_INTERVAL_MS = 5000
@@ -141,6 +141,23 @@ export const useSSE = (directory?: string | string[], currentSessionId?: string)
invalidateSessionListCachesDebounced(queryClient)
break
case 'shell.created': {
const info = event.data.info
queryClient.setQueryData<ShellInfo[]>(shellsQueryKey(cacheDirectory), (current) =>
current ? [...current.filter((shell) => shell.id !== info.id), info] : current,
)
break
}
case 'shell.exited':
case 'shell.deleted': {
const id = event.data.id
queryClient.setQueryData<ShellInfo[]>(shellsQueryKey(cacheDirectory), (current) =>
current?.filter((shell) => shell.id !== id),
)
break
}
case 'installation.updated':
showToast.success(`OpenCode updated to v${event.data.version}`, {
description: 'The server has been successfully upgraded.',
@@ -235,6 +252,7 @@ export const useSSE = (directory?: string | string[], currentSessionId?: string)
const handleResync = () => {
if (!mountedRef.current) return
invalidateSessionListCaches(queryClient)
queryClient.invalidateQueries({ queryKey: ['opencode', 'shells'] })
refreshCurrentSession()
}
+40
View File
@@ -0,0 +1,40 @@
import { useMemo } from 'react'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { listShells, removeShell, type ShellInfo } from '@/api/opencode'
import { shellsQueryKey } from '@/lib/queryInvalidation'
import { showToast } from '@/lib/toast'
export function useSessionShells(sessionID: string | undefined, directory: string | undefined) {
const query = useQuery({
queryKey: shellsQueryKey(directory),
queryFn: () => listShells(directory ?? ''),
enabled: Boolean(directory),
staleTime: Infinity,
})
const shells = useMemo(
() =>
(query.data ?? []).filter(
(shell) => shell.status === 'running' && shell.metadata.sessionID === sessionID,
),
[query.data, sessionID],
)
return shells
}
export function useKillShell(directory: string | undefined) {
const queryClient = useQueryClient()
return useMutation({
mutationFn: (id: string) => removeShell(id, directory ?? ''),
onSuccess: (_result, id) => {
queryClient.setQueryData<ShellInfo[]>(shellsQueryKey(directory), (current) =>
current?.filter((shell) => shell.id !== id),
)
},
onError: (error) => {
showToast.error(error instanceof Error ? error.message : 'Failed to kill shell command')
},
})
}