From 360ceea3ac854c9adba45d92d230419132b0de74 Mon Sep 17 00:00:00 2001 From: ioplex Date: Mon, 19 Oct 2026 19:01:31 -0400 Subject: [PATCH] fix(servers): reconcile linked server statuses once the server list loads The browser stream usually connects before the linked server list resolves, so a fresh page never fetched remote active sessions and busy remote sessions showed as idle until their status changed. Reconcile each enabled linked server once per stream connection, including servers that arrive or are enabled after the stream connects. --- frontend/src/contexts/EventContext.test.tsx | 35 +++++++++++++++++++++ frontend/src/contexts/EventContext.tsx | 34 +++++++++++++++++--- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/frontend/src/contexts/EventContext.test.tsx b/frontend/src/contexts/EventContext.test.tsx index 98e8190e..c9a0191f 100644 --- a/frontend/src/contexts/EventContext.test.tsx +++ b/frontend/src/contexts/EventContext.test.tsx @@ -522,6 +522,41 @@ describe('EventProvider permissions and forms', () => { }) }) + it('reconciles a linked server enabled after the browser stream connects', async () => { + const busySessionID = 'session-remote-busy' + mocks.listActiveSessions.mockImplementation((serverId?: string) => + Promise.resolve(serverId === 'srv_remote' ? { [busySessionID]: {} } : {}), + ) + + const { rerender } = render(, { wrapper: createWrapper() }) + + await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled()) + const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1] + const handleStatusChange = lastSubscribeCall[0].onStatusChange as (connected: boolean) => void + + act(() => { + handleStatusChange(true) + }) + + expect(mocks.listActiveSessions).not.toHaveBeenCalledWith('srv_remote') + + mocks.servers = [{ id: 'srv_remote', enabled: true }] + rerender() + + await waitFor(() => { + expect(useSessionStatus.getState().statuses.get(busySessionID)).toEqual({ type: 'busy' }) + }) + const remoteFetches = () => + mocks.listActiveSessions.mock.calls.filter(([serverId]) => serverId === 'srv_remote') + expect(remoteFetches()).toHaveLength(1) + + await userEvent.click(screen.getByRole('button', { name: 'Sync' })) + mocks.servers = [{ id: 'srv_remote', enabled: true }] + rerender() + + expect(remoteFetches()).toHaveLength(1) + }) + it('clears settled pending actions on an upstream resync while the browser stream stays connected', async () => { mocks.listRepos.mockResolvedValue([{ id: 123, fullPath: '/repo' }]) mocks.listPendingPermissions.mockResolvedValue([]) diff --git a/frontend/src/contexts/EventContext.tsx b/frontend/src/contexts/EventContext.tsx index d24f1436..ac136853 100644 --- a/frontend/src/contexts/EventContext.tsx +++ b/frontend/src/contexts/EventContext.tsx @@ -311,6 +311,8 @@ export function EventProvider({ children }: { children: React.ReactNode }) { const initialFetchDoneRef = useRef(false) const statusSyncVersionRef = useRef(0) const remoteStatusSyncVersionsRef = useRef(new Map()) + const streamConnectedRef = useRef(false) + const reconciledRemoteServerIdsRef = useRef(new Set()) const statusPollIntervalRef = useRef | null>(null) const subscriptionRef = useRef | null>(null) const reposRef = useRef(null) @@ -776,6 +778,24 @@ export function EventProvider({ children }: { children: React.ReactNode }) { } }, [resolveSessionAncestry]) + const reconcileRemoteServersOnce = useCallback((serversToReconcile: Array<{ id: string }>) => { + const reconciled = reconciledRemoteServerIdsRef.current + for (const server of serversToReconcile) { + if (reconciled.has(server.id)) continue + reconciled.add(server.id) + void reconcileRemoteSessionStatuses(server.id) + } + }, [reconcileRemoteSessionStatuses]) + + useEffect(() => { + const enabledIds = new Set(enabledRemoteServers.map((server) => server.id)) + for (const serverId of reconciledRemoteServerIdsRef.current) { + if (!enabledIds.has(serverId)) reconciledRemoteServerIdsRef.current.delete(serverId) + } + if (!streamConnectedRef.current) return + reconcileRemoteServersOnce(enabledRemoteServers) + }, [enabledRemoteServers, reconcileRemoteServersOnce]) + const stopStatusPoll = useCallback(() => { if (statusPollIntervalRef.current === null) return clearInterval(statusPollIntervalRef.current) @@ -984,19 +1004,23 @@ export function EventProvider({ children }: { children: React.ReactNode }) { } } + const reconciledRemoteServerIds = reconciledRemoteServerIdsRef.current + const handleStatusChange = (connected: boolean) => { if (connected) { + streamConnectedRef.current = true + reconciledRemoteServerIdsRef.current.clear() initialFetchDoneRef.current = false fetchInitialPendingData() fetchInitialSessionStatuses() - for (const server of enabledRemoteServersRef.current) { - void reconcileRemoteSessionStatuses(server.id) - } + reconcileRemoteServersOnce(enabledRemoteServersRef.current) void syncRunningShells(collectTrackedDirectories()) invalidateChildSessionCaches(queryClient) invalidateQueryKeysDebounced(queryClient, LOCATION_CATALOG_QUERY_KEYS) startStatusPoll() } else { + streamConnectedRef.current = false + reconciledRemoteServerIdsRef.current.clear() statusSyncVersionRef.current += 1 invalidateRemoteStatusSyncs() stopStatusPoll() @@ -1041,13 +1065,15 @@ export function EventProvider({ children }: { children: React.ReactNode }) { subscriptionRef.current = subscription return () => { + streamConnectedRef.current = false + reconciledRemoteServerIds.clear() statusSyncVersionRef.current += 1 invalidateRemoteStatusSyncs() stopStatusPoll() subscription.dispose() subscriptionRef.current = null } - }, [addPermission, removePermission, addForm, removeForm, rememberSessionDirectory, fetchInitialPendingData, fetchInitialSessionStatuses, startStatusPoll, stopStatusPoll, queryClient, handleHealthChange, reconcilePendingActionsForDirectories, collectTrackedDirectories, syncRunningShells, resolveSessionAncestry, reconcileRemoteSessionStatuses, invalidateRemoteStatusSyncs]) + }, [addPermission, removePermission, addForm, removeForm, rememberSessionDirectory, fetchInitialPendingData, fetchInitialSessionStatuses, startStatusPoll, stopStatusPoll, queryClient, handleHealthChange, reconcilePendingActionsForDirectories, collectTrackedDirectories, syncRunningShells, resolveSessionAncestry, reconcileRemoteSessionStatuses, reconcileRemoteServersOnce, invalidateRemoteStatusSyncs]) useEffect(() => { reposRef.current = repos