Skip to content

The agent that grows with you

fix(desktop): re-enumerate the fleet roster when the gateway opens

Tekniumdb8bf327d7ee
diff --git a/apps/desktop/src/app/chat/sidebar/use-fleet-roster.test.tsx b/apps/desktop/src/app/chat/sidebar/use-fleet-roster.test.tsx
new file mode 100644
index 00000000000..cb1301c8587
--- /dev/null
+++ b/apps/desktop/src/app/chat/sidebar/use-fleet-roster.test.tsx
@@ -0,0 +1,40 @@
+import { cleanup, renderHook } from '@testing-library/react'
+import { act } from 'react'
+import { afterEach, expect, it, vi } from 'vitest'
+
+import type { DesktopAgentRoster } from '@/global'
+import { _resetFleetRosterForTests } from '@/store/fleet-roster'
+import { $gatewayState } from '@/store/session'
+
+import { useFleetRoster } from './use-fleet-roster'
+
+afterEach(() => {
+ cleanup()
+ _resetFleetRosterForTests()
+ $gatewayState.set('idle')
+ vi.unstubAllGlobals()
+})
+
+it('re-enumerates a roster that timed out while the local gateway was still booting', async () => {
+ // A cold `gateway ensure` outlives the roster's per-source deadline, so the
+ // first enumeration reports This device unreachable. Nothing else refetches
+ // it until a window focus, and the rail counted This device as one square.
+ const booting: DesktopAgentRoster = {
+ agents: [],
+ sources: [{ connectionId: 'local', label: 'This device', kind: 'local', reachable: false, error: 'timed out' }]
+ }
+
+ const getAgentRoster = vi.fn().mockResolvedValue(booting)
+ window.hermesDesktop = { getAgentRoster } as never
+
+ $gatewayState.set('connecting')
+ renderHook(() => useFleetRoster(true))
+ await act(async () => undefined)
+ expect(getAgentRoster).toHaveBeenCalledTimes(1)
+
+ await act(async () => {
+ $gatewayState.set('open')
+ })
+
+ expect(getAgentRoster).toHaveBeenCalledTimes(2)
+})
diff --git a/apps/desktop/src/app/chat/sidebar/use-fleet-roster.ts b/apps/desktop/src/app/chat/sidebar/use-fleet-roster.ts
index 018c7cac981..1b402de11b7 100644
--- a/apps/desktop/src/app/chat/sidebar/use-fleet-roster.ts
+++ b/apps/desktop/src/app/chat/sidebar/use-fleet-roster.ts
@@ -1,13 +1,20 @@
import { useEffect } from 'react'
import { refreshFleetRoster } from '@/store/fleet-roster'
+import { $gatewayState } from '@/store/session'
/**
* Keep the fleet roster fresh for the profile rail while more than one
* gateway is registered: pull on mount, when the window regains focus or
- * visibility, and immediately when the connection registry changes. No timer
- * — the multi-connection contract rules out periodic fleet polling from the
- * sidebar, and a 60s stale window in the store absorbs focus churn.
+ * visibility, immediately when the connection registry changes, and when the
+ * gateway (re)opens. No timer — the multi-connection contract rules out
+ * periodic fleet polling from the sidebar, and a 60s stale window in the store
+ * absorbs focus churn.
+ *
+ * The open edge matters on a cold start: the first pull races `gateway
+ * ensure`, whose boot outlives the roster's per-source deadline, so This
+ * device enumerates as unreachable (one square instead of its profiles) and
+ * nothing else would ask again until a window focus.
*/
export function useFleetRoster(enabled: boolean): void {
useEffect(() => {
@@ -28,11 +35,23 @@ export function useFleetRoster(enabled: boolean): void {
window.addEventListener('focus', onFocus)
document.addEventListener('visibilitychange', onVisibility)
const offRegistry = window.hermesDesktop?.connections?.onChanged?.(() => void refreshFleetRoster({ force: true }))
+ let wasOpen = $gatewayState.get() === 'open'
+
+ const offGateway = $gatewayState.listen(state => {
+ const open = state === 'open'
+
+ if (open && !wasOpen) {
+ void refreshFleetRoster({ force: true })
+ }
+
+ wasOpen = open
+ })
return () => {
window.removeEventListener('focus', onFocus)
document.removeEventListener('visibilitychange', onVisibility)
offRegistry?.()
+ offGateway()
}
}, [enabled])
}