fix(background-activity): reconcile shells for directories subscribed by the stream

The background activity indicator went stale for sessions living in a
directory that is not a registered repo. Running shells were only synced
for repo directories derived from the repos query, so session directories
subscribed over the event stream were never reconciled, leaving the
indicator visible after the shell had exited.

Report the directories actually subscribed (mid-connection updates and
pending directories flushed after connect) from the event stream through
onDirectoriesSubscribed, and reconcile running shells and pending
permissions/forms for them in the event context.
This commit is contained in:
Chris Scott committed 2026-10-15 13:00:08 +00:00
1 parent 59ffe6c586
commit 37b4ab89f0
5 files changed
+232 -29

No files matched your search

+62 -3
View File
@@ -1865,11 +1865,18 @@ describe('EventProvider permissions and forms', () => {
render(<Harness />, { wrapper: createWrapper() })
await waitFor(() => expect(mocks.listShells).toHaveBeenCalledWith('/repo'))
await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled())
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onDirectoriesSubscribed = lastSubscribeCall[0].onDirectoriesSubscribed as (directories: string[]) => void
const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void
act(() => {
onDirectoriesSubscribed(['/repo'])
})
await waitFor(() => expect(mocks.listShells).toHaveBeenCalledWith('/repo'))
act(() => {
onEvent({ type: 'shell.exited', data: { id: 'shell-1', status: 'exited' }, directory: '/repo' })
})
@@ -1889,11 +1896,18 @@ describe('EventProvider permissions and forms', () => {
render(<Harness />, { wrapper: createWrapper() })
await waitFor(() => expect(mocks.listShells).toHaveBeenCalledWith('/repo'))
await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled())
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onDirectoriesSubscribed = lastSubscribeCall[0].onDirectoriesSubscribed as (directories: string[]) => void
const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void
act(() => {
onDirectoriesSubscribed(['/repo'])
})
await waitFor(() => expect(mocks.listShells).toHaveBeenCalledWith('/repo'))
act(() => {
onEvent({
type: 'shell.created',
@@ -1913,7 +1927,7 @@ describe('EventProvider permissions and forms', () => {
})
})
it('resolves ancestry for a shell owner from the directory snapshot', async () => {
it('resolves ancestry for a shell owner from a newly subscribed directory snapshot', async () => {
mocks.listRepos.mockResolvedValue([{ id: 123, fullPath: '/repo' }])
mocks.listShells.mockResolvedValue([
{ id: 'shell-1', status: 'running', metadata: { sessionID: 'child-1' } },
@@ -1926,6 +1940,15 @@ describe('EventProvider permissions and forms', () => {
render(<Harness />, { wrapper: createWrapper() })
await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled())
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onDirectoriesSubscribed = lastSubscribeCall[0].onDirectoriesSubscribed as (directories: string[]) => void
act(() => {
onDirectoriesSubscribed(['/repo'])
})
await waitFor(() => {
expect(useSessionStatus.getState().parents.get('child-1')).toBe('parent-1')
})
@@ -1967,6 +1990,12 @@ describe('EventProvider permissions and forms', () => {
it('syncs running shells for a directory whose repo arrives after connect', async () => {
const reposDeferred = createDeferred<Array<{ id: number; fullPath: string }>>()
mocks.listRepos.mockReturnValueOnce(reposDeferred.promise)
mocks.subscribeGlobalMonitor.mockImplementation((input: { onDirectoriesSubscribed?: (directories: string[]) => void }) => ({
dispose: vi.fn(),
updateDirectories: (directories: string[]) => input.onDirectoriesSubscribed?.(directories),
reconnect: vi.fn(),
reportVisibility: vi.fn(),
}))
render(<Harness />, { wrapper: createWrapper() })
@@ -1988,4 +2017,34 @@ describe('EventProvider permissions and forms', () => {
await waitFor(() => expect(mocks.listShells).toHaveBeenCalledWith('/repo'))
})
it('reconciles newly subscribed directories reported by the stream', async () => {
mocks.listPendingPermissions.mockResolvedValue([pendingPermission])
mocks.listPendingForms.mockResolvedValue([pendingForm])
render(<Harness />, { wrapper: createWrapper() })
await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled())
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onDirectoriesSubscribed = lastSubscribeCall[0].onDirectoriesSubscribed as (directories: string[]) => void
act(() => {
useSessionStatus.getState().setShellRunning('shell-stale', 'session-1', '/session-dir')
})
expect(useSessionStatus.getState().runningShells.has('shell-stale')).toBe(true)
await act(async () => {
onDirectoriesSubscribed(['/session-dir'])
})
await waitFor(() => {
expect(mocks.listShells).toHaveBeenCalledWith('/session-dir')
expect(mocks.listPendingPermissions).toHaveBeenCalledWith('/session-dir')
expect(mocks.listPendingForms).toHaveBeenCalledWith('/session-dir')
})
expect(useSessionStatus.getState().runningShells.has('shell-stale')).toBe(false)
expect(screen.getByTestId('permission-count').textContent).toBe('1')
expect(screen.getByTestId('count').textContent).toBe('1')
})
})
+6 -11
View File
@@ -206,7 +206,6 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
const statusPollIntervalRef = useRef<ReturnType<typeof setInterval> | null>(null)
const subscriptionRef = useRef<ReturnType<typeof openCodeEventStream.subscribeGlobalMonitor> | null>(null)
const reposRef = useRef<typeof repos>(null)
const trackedRepoDirectoriesRef = useRef<Set<string>>(new Set())
const ancestryInFlightRef = useRef<Set<string>>(new Set())
const { data: repos } = useQuery({
@@ -725,6 +724,10 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
onStatusChange: handleStatusChange,
onHealthChange: handleHealthChange,
onResync: handleResync,
onDirectoriesSubscribed: (directories) => {
void reconcilePendingActionsForDirectories(directories)
void syncRunningShells(directories)
},
})
subscriptionRef.current = subscription
@@ -739,16 +742,8 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
useEffect(() => {
reposRef.current = repos
const directories = [...new Set((repos ?? []).map(r => r.fullPath))]
const previousDirectories = trackedRepoDirectoriesRef.current
const addedDirectories = directories.filter((directory) => !previousDirectories.has(directory))
trackedRepoDirectoriesRef.current = new Set(directories)
const sub = subscriptionRef.current
if (!sub) return
sub.updateDirectories(directories)
if (addedDirectories.length > 0) {
void syncRunningShells(addedDirectories)
}
}, [repos, syncRunningShells])
subscriptionRef.current?.updateDirectories(directories)
}, [repos])
useEffect(() => {
if (!repos || repos.length === 0) return