fix(background-activity): address review on resync scope and stale subscribe responses

A directory subscribed by the stream but neither registered as a repo nor
remembered as a session directory was reconciled on subscribe but skipped on
resync, so a missed shell exit could leave the indicator stale. Pass the
stream's subscribed directories to onResync and reconcile their pending
actions and running shells alongside the tracked ones.

Subscribe responses that arrive after the connection was replaced or the
directory was unsubscribed could also apply snapshots to state the client no
longer owns. Ignore responses from a superseded connection and report only
directories still subscribed.

The stale-indicator e2e test now waits for the successful unsubscribe response
and asserts the subscribe responses succeeded instead of sleeping.
This commit is contained in:
Chris Scott committed 2026-10-15 10:18:06 -04:00
1 parent 74a2f6fb6e
commit 1b2665bd2b
5 files changed
+131 -26

No files matched your search

+42 -12
View File
@@ -457,7 +457,7 @@ describe('EventProvider permissions and forms', () => {
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
const onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
onEvent({ type: 'permission.asked', data: pendingPermission, directory: '/repo' })
@@ -470,7 +470,7 @@ describe('EventProvider permissions and forms', () => {
})
act(() => {
onResync?.()
onResync?.([])
})
await waitFor(() => {
@@ -492,7 +492,7 @@ describe('EventProvider permissions and forms', () => {
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
const onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
onEvent({ type: 'permission.asked', data: pendingPermission, directory: '/repo' })
@@ -505,7 +505,7 @@ describe('EventProvider permissions and forms', () => {
})
act(() => {
onResync?.()
onResync?.([])
})
await waitFor(() => {
@@ -531,7 +531,7 @@ describe('EventProvider permissions and forms', () => {
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
const onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
onEvent({ type: 'permission.asked', data: pendingPermission, directory: '/worktrees/wt-1' })
@@ -542,7 +542,7 @@ describe('EventProvider permissions and forms', () => {
})
act(() => {
onResync?.()
onResync?.([])
})
await waitFor(() => {
@@ -852,10 +852,10 @@ describe('EventProvider permissions and forms', () => {
invalidateQueries.mockClear()
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
const onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
onResync?.()
onResync?.([])
})
await waitFor(() => {
@@ -939,10 +939,10 @@ describe('EventProvider permissions and forms', () => {
mocks.listPendingForms.mockClear()
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
const onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
onResync?.()
onResync?.([])
})
expect(mocks.listPendingPermissions).toHaveBeenCalledWith('/repo-a')
@@ -1210,7 +1210,7 @@ describe('EventProvider permissions and forms', () => {
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onEvent = lastSubscribeCall[0].onEvent as (data: unknown) => void
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
const onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
onEvent({ type: 'session.execution.started', data: { sessionID: 'child-1' }, directory: '/repo' })
@@ -1218,7 +1218,7 @@ describe('EventProvider permissions and forms', () => {
expect(useSessionStatus.getState().getStatus('child-1')).toEqual({ type: 'busy' })
act(() => {
onResync?.()
onResync?.([])
})
await waitFor(() => {
@@ -2047,4 +2047,34 @@ describe('EventProvider permissions and forms', () => {
expect(screen.getByTestId('permission-count').textContent).toBe('1')
expect(screen.getByTestId('count').textContent).toBe('1')
})
it('reconciles subscribed directories reported by a resync even when they are not tracked', 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 onResync = lastSubscribeCall[0].onResync as ((directories: string[]) => void) | undefined
act(() => {
useSessionStatus.getState().setShellRunning('shell-stale', 'session-1', '/unregistered-dir')
})
expect(useSessionStatus.getState().runningShells.has('shell-stale')).toBe(true)
await act(async () => {
onResync?.(['/unregistered-dir'])
})
await waitFor(() => {
expect(mocks.listShells).toHaveBeenCalledWith('/unregistered-dir')
expect(mocks.listPendingPermissions).toHaveBeenCalledWith('/unregistered-dir')
expect(mocks.listPendingForms).toHaveBeenCalledWith('/unregistered-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')
})
})
+4 -3
View File
@@ -709,10 +709,11 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
}
}
const handleResync = () => {
void reconcilePendingActionsForDirectories(collectTrackedDirectories())
const handleResync = (subscribedDirectories: string[]) => {
const directories = [...new Set([...collectTrackedDirectories(), ...subscribedDirectories])]
void reconcilePendingActionsForDirectories(directories)
void fetchInitialSessionStatuses()
void syncRunningShells(collectTrackedDirectories())
void syncRunningShells(directories)
invalidateChildSessionCaches(queryClient)
invalidateQueryKeysDebounced(queryClient, LOCATION_CATALOG_QUERY_KEYS)
}