Skip to content

The agent that grows with you

fix(desktop): scope primary auth recovery to its foreground

brooklyn!10555c287bbe
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 d20e0cb89c4..4051dacb39d 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
@@ -360,6 +360,214 @@ async function advanceBackoff() {
})
}
+describe('primary failure foreground isolation', () => {
+ it('ignores a boot snapshot superseded by a successful connection', async () => {
+ const snapshot = deferred<Awaited<ReturnType<ReturnType<typeof fakeDesktop>['getBootProgress']>>>()
+ const desktop = fakeDesktop()
+ desktop.getBootProgress.mockReturnValue(snapshot.promise)
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+ render(<Harness />)
+ await flushAsync()
+ expect($gatewayState.get()).toBe('open')
+
+ act(() => snapshot.resolve({
+ error: 'Your remote gateway session has expired.',
+ fakeMode: false,
+ message: 'previous rejection',
+ phase: 'backend.error',
+ progress: 94,
+ retryable: false,
+ running: false,
+ timestamp: Date.now()
+ }))
+ await flushAsync()
+ expect($desktopBoot.get().error).toBeNull()
+ expect($desktopBoot.get().visible).toBe(false)
+ })
+ it('ignores a boot snapshot superseded by a newer progress event', async () => {
+ const snapshot = deferred<Awaited<ReturnType<ReturnType<typeof fakeDesktop>['getBootProgress']>>>()
+ const connection = deferred<typeof primaryConn>()
+ const desktop = fakeDesktop()
+ desktop.getBootProgress.mockReturnValue(snapshot.promise)
+ desktop.getConnection.mockReturnValue(connection.promise)
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+ render(<Harness />)
+ await flushAsync()
+ const newer = {
+ error: null,
+ fakeMode: false,
+ message: 'Connecting after sign-in',
+ retryable: false,
+ phase: 'backend.remote',
+ progress: 24,
+ running: true,
+ timestamp: Date.now()
+ }
+ act(() => desktop.emitBootProgress(newer))
+ act(() => snapshot.resolve({ ...newer, error: 'Your remote gateway session has expired.', running: false }))
+ await flushAsync()
+ expect($desktopBoot.get().error).toBeNull()
+ act(() => connection.resolve(primaryConn))
+ await flushAsync()
+ })
+
+ it.each(['local', 'remote'] as const)(
+ 'ordinary %s primary outage leaves the other foreground usable',
+ async primaryMode => {
+ const foregroundId = primaryMode === 'local' ? 'healthy-remote' : 'local'
+
+ const desktop = Object.assign(fakeDesktop(), {
+ getGatewayWsUrlFor: vi.fn(async () => coderConn.wsUrl),
+ getConnectionFor: vi.fn(async () => ({
+ ...coderConn,
+ connectionId: foregroundId,
+ profile: 'default',
+ mode: primaryMode === 'local' ? 'remote' : 'local',
+ remoteKind: primaryMode === 'local' ? 'cloud' : undefined,
+ authMode: primaryMode === 'local' ? 'oauth' : 'token'
+ }))
+ })
+
+ desktop.getConnection.mockResolvedValue({
+ ...primaryConn,
+ connectionId: primaryMode === 'local' ? 'local' : 'primary-vps',
+ mode: primaryMode
+ } as typeof primaryConn)
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+ render(<Harness />)
+ await flushAsync()
+ let opening!: Promise<boolean>
+ act(() => {
+ opening = ensureGatewayForAgent(foregroundId, 'default')
+ })
+ await flushAsync()
+ expect(await opening).toBe(true)
+ desktop.getConnection.mockRejectedValue(new Error('ECONNREFUSED'))
+ act(() => FakeWebSocket.instances[0].drop())
+ await advanceBackoff()
+ expect(isActivePrimary()).toBe(false)
+ expect($gatewayState.get()).toBe('open')
+ await expect(requestGatewayForAgent(foregroundId, 'default', 'ping')).resolves.toEqual({ pong: true })
+ expect($desktopBoot.get().error).toBeNull()
+ expect($desktopBoot.get().visible).toBe(false)
+ }
+ )
+ it.each(['progress', 'reconnect'] as const)(
+ 'primary auth via %s follows the foreground, including a latched error',
+ async path => {
+ const error = 'Your remote gateway session has expired.'
+
+ const cloud = {
+ ...primaryConn,
+ mode: 'remote' as const,
+ remoteKind: 'cloud' as const,
+ authMode: 'oauth' as const
+ }
+
+ const desktop = Object.assign(fakeDesktop(), {
+ getRecentLogs: vi.fn(async () => ({ lines: [] })),
+ getConnectionConfig: vi.fn(async () => ({ mode: 'cloud', remoteAuthMode: 'oauth', remoteUrl: cloud.baseUrl })),
+ getConnectionFor: vi.fn(async () => ({
+ ...coderConn,
+ connectionId: 'local',
+ profile: 'default',
+ mode: 'local',
+ baseUrl: 'http://127.0.0.1:9191',
+ wsUrl: 'ws://127.0.0.1:9191/api/ws?token=c'
+ }))
+ })
+
+ desktop.getConnection.mockResolvedValue(cloud as typeof primaryConn)
+
+ ;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
+ const { BootFailureOverlay } = await import('@/components/boot-failure-overlay')
+
+ const overlay = render(
+ <>
+ <Harness />
+ <BootFailureOverlay />
+ </>
+ )
+
+ await flushAsync()
+ expect($desktopBoot.get().visible).toBe(false)
+
+ const selectLocal = async () => {
+ let opening!: Promise<boolean>
+ act(() => {
+ opening = ensureGatewayForAgent('local', 'default')
+ })
+ await flushAsync()
+ expect(await opening).toBe(true)
+ }
+
+ await selectLocal()
+ const foreground = activeGateway()
+
+ const progress = {
+ error,
+ fakeMode: false,
+ message: error,
+ phase: 'backend.error',
+ progress: 94,
+ retryable: false,
+ running: false,
+ timestamp: Date.now()
+ }
+
+ if (path === 'reconnect') {
+ desktop.getGatewayWsUrl.mockRejectedValue(Object.assign(new Error(error), { needsOauthLogin: true }))
+ act(() => FakeWebSocket.instances[0].drop())
+ await advanceBackoff()
+ } else {
+ act(() => desktop.emitBootProgress(progress))
+ await flushAsync()
+ }
+
+ expect(activeGateway()).toBe(foreground)
+ expect($connection.get()?.connectionId).toBe('local')
+ expect($gatewayState.get()).toBe('open')
+ await expect(requestGatewayForAgent('local', 'default', 'ping')).resolves.toEqual({ pong: true })
+ expect(overlay.queryByRole('heading')).toBeNull()
+ expect($desktopBoot.get().error).toBeNull()
+ expect($desktopBoot.get().visible).toBe(false)
+ expect(notifyError).not.toHaveBeenCalled()
+
+ await act(async () => {
+ await ensureGatewayForProfile('default')
+ })
+ await flushAsync()
+ expect(isActivePrimary()).toBe(true)
+ expect($desktopBoot.get().error).toContain(error)
+ expect(overlay.getByRole('heading', { name: /sign-in required/i })).toBeTruthy()
+ expect(overlay.getByRole('button', { name: /sign in/i })).toBeTruthy()
+
+ // A now-latched foreground error must release boot readiness when leaving,
+ // but returning to its primary must retain the authentication recovery.
+ await selectLocal()
+ expect($desktopBoot.get().error).toBeNull()
+ expect($desktopBoot.get().visible).toBe(false)
+ expect(overlay.queryByRole('heading')).toBeNull()
+ await act(async () => {
+ await ensureGatewayForProfile('default')
+ })
+ expect($desktopBoot.get().error).toContain(error)
+
+ desktop.getGatewayWsUrl.mockImplementation(async conn => conn?.wsUrl ?? primaryConn.wsUrl)
+ act(() => FakeWebSocket.instances[0].drop())
+ await advanceBackoff()
+ expect($gatewayState.get()).toBe('open')
+ expect($desktopBoot.get().error).toBeNull()
+ expect(overlay.queryByRole('heading')).toBeNull()
+ await selectLocal()
+ await act(async () => {
+ await ensureGatewayForProfile('default')
+ })
+ expect($desktopBoot.get().error).toBeNull()
+ }
+ )
+})
+
describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () => {
it('INITIAL boot against a dead VPS: getConnection hangs (waitForHermes) → app sits in the connecting combo, then fails', async () => {
// The report's actual path: a fresh launch pointed at an unreachable VPS.
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 c0932b00e68..411f22f9ce2 100644
--- a/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
+++ b/apps/desktop/src/app/gateway/hooks/use-gateway-boot.ts
@@ -7,7 +7,7 @@ import {
import { useEffect, useRef } from 'react'
import { shouldApplyPostBootProgressError } from '@/components/boot-failure-reauth'
-import type { HermesConnection } from '@/global'
+import type { DesktopBootProgress, HermesConnection } from '@/global'
import { HermesGateway } from '@/hermes'
import { translateNow } from '@/i18n'
import { desktopDefaultCwd } from '@/lib/desktop-fs'
@@ -64,6 +64,7 @@ import {
$activeSessionId,
$connection,
$currentCwd,
+ $gatewayState,
$selectedStoredSessionId,
$sessions,
ensureDefaultWorkspaceCwd,
@@ -243,6 +244,21 @@ export function useGatewayBoot({
// tick — a stale OAuth ticket fails every attempt and would otherwise stack
// identical error toasts (and their haptics). Reset on the next clean open.
let reauthNotified = false
+ // After boot, a primary auth failure must not cover a healthy secondary.
+ let primaryReauthError: string | null = null
+
+ const syncPrimaryReauthError = () => {
+ if (!bootCompleted || !primaryReauthError) {
+ return
+ }
+
+ if (isActivePrimary()) {
+ failDesktopBoot(primaryReauthError)
+ } else if (activeGateway()?.connectionState === 'open' && $desktopBoot.get().error === primaryReauthError) {
+ completeDesktopBoot()
+ }
+ }
+
// Raised once the reconnect loop has been failing for
// RECONNECT_ESCALATE_AFTER_MS so we fire a single non-blocking toast.
// Reset on a clean open or a manual/wake-driven reconnect.
@@ -407,10 +423,13 @@ export function useGatewayBoot({
// through to the backoff in the finally block below — they must NOT
// take the full-screen "couldn't start" path (locks reading/drafting).
if (!cancelled && isGatewayReauthRequired(err) && !reauthNotified) {
- reauthNotified = true
- const message = err instanceof Error ? err.message : String(err)
- failDesktopBoot(message)
- notifyError(err, translateNow('boot.errors.gatewaySignInRequired'))
+ primaryReauthError = err instanceof Error ? err.message : String(err)
+ syncPrimaryReauthError()
+
+ if (isActivePrimary()) {
+ reauthNotified = true
+ notifyError(err, translateNow('boot.errors.gatewaySignInRequired'))
+ }
}
} finally {
reconnecting = false
@@ -604,6 +623,7 @@ export function useGatewayBoot({
reconnectFailingSince = null
escalated = false
reauthNotified = false
+ primaryReauthError = null
gateway.close()
// The primary mode is changing, but registered v2 sources remain
@@ -709,7 +729,11 @@ export function useGatewayBoot({
}
}
- const offBootProgress = desktop.onBootProgress(payload => {
+ const onBootProgress = (payload: DesktopBootProgress) => {
+ if (cancelled) {
+ return
+ }
+
// Soft switch / post-boot startHermes re-emits progress — ignore so the
// cold-boot CONNECTING overlay stays down. Post-boot errors are gated:
// only confirmed reauth takes the full-screen recovery surface. Transient
@@ -717,18 +741,35 @@ export function useGatewayBoot({
// (otherwise a 1–3 min blip bricks reading/drafting behind "couldn't start").
if ($gatewaySwitching.get() || bootCompleted) {
if (payload.error && shouldApplyPostBootProgressError(payload.error)) {
- applyDesktopBootProgress(payload)
+ primaryReauthError = payload.error
+
+ if (bootCompleted) {
+ syncPrimaryReauthError()
+ } else {
+ applyDesktopBootProgress(payload)
+ }
}
return
}
applyDesktopBootProgress(payload)
+ }
+
+ let bootSnapshotSuperseded = false
+
+ const offBootProgress = desktop.onBootProgress(payload => {
+ bootSnapshotSuperseded = true
+ onBootProgress(payload)
})
void desktop
.getBootProgress()
- .then(snapshot => applyDesktopBootProgress(snapshot))
+ .then(snapshot => {
+ if (!bootSnapshotSuperseded && !gatewayOpen()) {
+ onBootProgress(snapshot)
+ }
+ })
.catch(() => undefined)
setDesktopBootStep({
@@ -820,15 +861,20 @@ export function useGatewayBoot({
}
})
+ const offActiveGatewayReauth = $gateway.listen(syncPrimaryReauthError)
+ const offActiveStateReauth = $gatewayState.listen(syncPrimaryReauthError)
+
const offState = gateway.onState(st => {
// Mirror to the composer only while the primary is the active profile —
// a background secondary reconnect mustn't flip the foreground state.
reportPrimaryGatewayState(st)
if (st === 'open') {
+ bootSnapshotSuperseded = true
reconnectAttempt = 0
reconnectFailingSince = null
reauthNotified = false
+ primaryReauthError = null
escalated = false
livenessProbeFailures = 0
clearReconnectTimer()
@@ -1224,6 +1270,8 @@ export function useGatewayBoot({
offConnectionApplied?.()
offConnectionsChanged?.()
offGatewayReconnect()
+ offActiveGatewayReauth()
+ offActiveStateReauth()
offState()
offEvent()
offExit()