diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx index 5603d70219..c0ca28cde4 100644 --- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx +++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.test.tsx @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { $desktopBoot } from '@/store/boot' import { closeSecondaryGateways, isActivePrimary } from '@/store/gateway' +import { reconnectGateway } from '@/store/gateway-reconnect' import { $activeGatewayProfile, $profiles, ensureGatewayProfile } from '@/store/profile' import { $connection, $currentCwd, $gatewayState } from '@/store/session' @@ -121,6 +122,7 @@ function fakeDesktop() { } }), onPowerResume: vi.fn(() => () => undefined), + revalidateConnection: vi.fn(async () => ({ ok: true, rebuilt: false })), onWindowStateChanged: vi.fn(() => () => undefined), touchBackend: vi.fn(async () => undefined), profile: { get: vi.fn(async () => ({ profile: 'default' })) } @@ -391,6 +393,31 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () => expect($desktopBoot.get().error).toBeNull() }) + it('manual reconnect revalidates, re-resolves, re-mints, and re-dials the dropped socket', async () => { + const desktop = fakeDesktop() + + ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop + + render() + await flushAsync() + + expect($gatewayState.get()).toBe('open') + act(() => FakeWebSocket.instances[0].drop()) + FakeWebSocket.mode = 'open' + + await act(async () => { + const reconnect = reconnectGateway() + await vi.advanceTimersByTimeAsync(0) + await reconnect + }) + + expect(desktop.revalidateConnection).toHaveBeenCalledOnce() + expect(desktop.getConnection).toHaveBeenLastCalledWith('default') + expect(desktop.getGatewayWsUrl).toHaveBeenCalledTimes(2) + expect(FakeWebSocket.instances).toHaveLength(2) + expect($gatewayState.get()).toBe('open') + }) + it('FIX: a failed session-list fetch during boot is non-fatal — the app still boots', async () => { // The version-skew report: gateway WS connects fine, but refreshSessions() // rejects (e.g. older backend 404s an endpoint the fallback didn't cover, diff --git a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts index 8c23f60bd6..73981edcb7 100644 --- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts +++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts @@ -27,6 +27,7 @@ import { setPrimaryGateway, touchSecondaryGateways } from '@/store/gateway' +import { registerGatewayReconnect } from '@/store/gateway-reconnect' import { $gatewaySwitching, wipeSessionListsForGatewaySwitch } from '@/store/gateway-switch' import { notify, notifyError } from '@/store/notifications' import { @@ -259,7 +260,7 @@ export function useGatewayBoot({ }, delay) } - const reconnectNow = () => { + const reconnectNow = async () => { if (cancelled || !bootCompleted || $gatewaySwitching.get()) { return } @@ -271,7 +272,7 @@ export function useGatewayBoot({ reconnectSecondaryGateways() if (!gatewayOpen()) { - void attemptReconnect() + await attemptReconnect() } } @@ -486,8 +487,9 @@ export function useGatewayBoot({ // Wake signals: power resume (macOS/Windows), network coming back, and the // window regaining focus/visibility. Each nudges an immediate reconnect. - const offPowerResume = desktop.onPowerResume?.(() => reconnectNow()) + const offPowerResume = desktop.onPowerResume?.(() => void reconnectNow()) const offConnectionApplied = desktop.onConnectionApplied?.(() => void softSwitch()) + const offGatewayReconnect = registerGatewayReconnect(reconnectNow) // Registry lifecycle: a removed connection's secondaries must close NOW // (remote/cloud have no local process whose death would drop the socket — @@ -501,11 +503,11 @@ export function useGatewayBoot({ disposeSecondariesForConnection(payload.connectionId, { redial: payload.reason === 'updated' }) }) - const onOnline = () => reconnectNow() + const onOnline = () => void reconnectNow() const onVisible = () => { if (document.visibilityState === 'visible') { - reconnectNow() + void reconnectNow() } } @@ -708,6 +710,7 @@ export function useGatewayBoot({ offPowerResume?.() offConnectionApplied?.() offConnectionsChanged?.() + offGatewayReconnect() offState() offEvent() offExit() diff --git a/apps/desktop/src/app/shell/gateway-menu-panel.test.tsx b/apps/desktop/src/app/shell/gateway-menu-panel.test.tsx new file mode 100644 index 0000000000..b33e6291f9 --- /dev/null +++ b/apps/desktop/src/app/shell/gateway-menu-panel.test.tsx @@ -0,0 +1,101 @@ +import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const mocks = vi.hoisted(() => ({ + notifyError: vi.fn(), + reconnectGateway: vi.fn<() => Promise>() +})) + +vi.mock('@/components/ui/tooltip', () => ({ + Tip: ({ children }: { children: React.ReactNode }) => <>{children} +})) + +vi.mock('@/hermes', () => ({ + getLogs: vi.fn().mockResolvedValue({ lines: [] }) +})) + +vi.mock('@/i18n', () => ({ + useI18n: () => ({ + t: { + commandCenter: { restartGateway: 'Restart gateway' }, + shell: { + gatewayMenu: { + checkingInference: 'Checking inference', + connected: 'Connected', + connecting: 'Connecting', + disconnected: 'Disconnected', + inferenceNotReady: 'Inference not ready', + inferenceReady: 'Inference ready', + messagingPlatforms: 'Messaging platforms', + offline: 'Offline', + openSystem: 'Open system panel', + recentActivity: 'Recent activity', + reconnectGateway: 'Reconnect gateway', + viewAllLogs: 'View all logs' + } + } + } + }) +})) + +vi.mock('@/store/gateway-reconnect', () => ({ + reconnectGateway: mocks.reconnectGateway +})) + +vi.mock('@/store/notifications', () => ({ + notifyError: mocks.notifyError +})) + +vi.mock('@/store/system-actions', () => ({ + runGatewayRestart: vi.fn() +})) + +import { GatewayMenuPanel } from './gateway-menu-panel' + +const renderPanel = (gatewayState: string) => + render( + + ) + +describe('GatewayMenuPanel reconnect action', () => { + beforeEach(() => { + mocks.reconnectGateway.mockReset().mockResolvedValue(undefined) + mocks.notifyError.mockReset() + }) + + afterEach(() => cleanup()) + + it('shows reconnect only while disconnected and disables it in flight', async () => { + let finish: (() => void) | undefined + mocks.reconnectGateway.mockImplementation( + () => + new Promise(resolve => { + finish = resolve + }) + ) + + renderPanel('closed') + + const reconnect = screen.getByRole('button', { name: 'Reconnect gateway' }) + fireEvent.click(reconnect) + fireEvent.click(reconnect) + + expect(mocks.reconnectGateway).toHaveBeenCalledOnce() + expect((reconnect as HTMLButtonElement).disabled).toBe(true) + + await act(async () => finish?.()) + }) + + it('hides reconnect while the socket is open', async () => { + renderPanel('open') + await act(async () => undefined) + + expect(screen.queryByRole('button', { name: 'Reconnect gateway' })).toBeNull() + }) +}) diff --git a/apps/desktop/src/app/shell/gateway-menu-panel.tsx b/apps/desktop/src/app/shell/gateway-menu-panel.tsx index bb753670bb..8e1516b7bd 100644 --- a/apps/desktop/src/app/shell/gateway-menu-panel.tsx +++ b/apps/desktop/src/app/shell/gateway-menu-panel.tsx @@ -9,6 +9,8 @@ import { useI18n } from '@/i18n' import { LayoutDashboard, RefreshCw } from '@/lib/icons' import type { RuntimeReadinessResult } from '@/lib/runtime-readiness' import { cn } from '@/lib/utils' +import { reconnectGateway } from '@/store/gateway-reconnect' +import { notifyError } from '@/store/notifications' import { runGatewayRestart } from '@/store/system-actions' import type { StatusResponse } from '@/types/hermes' @@ -96,6 +98,7 @@ export function GatewayMenuPanel({ }: GatewayMenuPanelProps) { const { t } = useI18n() const copy = t.shell.gatewayMenu + const [reconnecting, setReconnecting] = useState(false) // Both jumps open the system panel, which owns the full view — so dismiss the // little status popover on the way out. @@ -111,6 +114,17 @@ export function GatewayMenuPanel({ void runGatewayRestart() } + const reconnect = () => { + if (reconnecting) { + return + } + + setReconnecting(true) + void reconnectGateway() + .catch(err => notifyError(err, copy.reconnectGateway)) + .finally(() => setReconnecting(false)) + } + const gatewayOpen = gatewayState === 'open' const gatewayConnecting = gatewayState === 'connecting' const inferenceReady = gatewayOpen && inferenceStatus?.ready === true @@ -157,6 +171,20 @@ export function GatewayMenuPanel({
+ {!gatewayOpen && ( + + + + )}