Skip to content

The agent that grows with you

fix(desktop): refresh readiness on OAuth success so a stale credential failure clears while unfocused

rainbowgits518575750b19
diff --git a/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts b/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
index 33d69d9c520..f54f8c7b1df 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.test.ts
@@ -7,6 +7,7 @@ import { NotificationStack } from '@/components/notifications'
import { getStatus } from '@/hermes'
import { I18nProvider, type Locale, TRANSLATIONS, type Translations } from '@/i18n'
import { $setupReadyTick, notifySetupReady } from '@/store/live-sync'
+import { $desktopOnboarding } from '@/store/onboarding'
import { clearNotifications } from '@/store/notifications'
import { deferred } from '../../../test/deferred'
@@ -37,6 +38,7 @@ beforeEach(() => {
.mockReset()
.mockResolvedValue({} as never)
$setupReadyTick.set(0)
+ $desktopOnboarding.set({ ...$desktopOnboarding.get(), flow: { status: 'idle' } })
clearNotifications()
})
@@ -371,4 +373,60 @@ describe('useStatusSnapshot', () => {
})
expect(result.current.statusSnapshot).toEqual({ version: '1.0.1' })
})
+
+ it('clears a stale readiness failure when OAuth completes while unfocused', async () => {
+ let healthy = false
+
+ const requestGateway = vi.fn(async (method: string) => {
+ if (method === 'setup.runtime_check') {
+ return (healthy ? { ok: true } : { error: 'No usable credentials found for nous.', ok: false }) as never
+ }
+
+ return { provider_configured: healthy } as never
+ }) as unknown as GatewayRequester
+
+ // Focused: seed the authoritative credential failure.
+ const { result } = renderHook(() => useStatusSnapshot('open', requestGateway))
+ await flushAsync()
+ expect(result.current.inferenceStatus).toMatchObject({ ready: false, source: 'runtime_check' })
+
+ // User leaves Hermes for the browser to complete OAuth; no focus event fires.
+ vi.mocked(document.hasFocus).mockReturnValue(false)
+ healthy = true
+
+ // OAuth completes: the onboarding flow transitions to success while unfocused.
+ await act(async () => {
+ $desktopOnboarding.set({
+ ...$desktopOnboarding.get(),
+ flow: { status: 'success', provider: { id: 'nous', name: 'Nous' } as never }
+ })
+ await vi.advanceTimersByTimeAsync(0)
+ })
+
+ // The stale failure is replaced by the healthy result without a focus event.
+ expect(result.current.inferenceStatus).toMatchObject({ ready: true, source: 'runtime_check' })
+ })
+
+ it('does not force a background poll on non-success onboarding transitions while unfocused', async () => {
+ const requestGateway = vi.fn(
+ async (method: string) => (method === 'setup.runtime_check' ? { ok: true } : { provider_configured: true }) as never
+ ) as unknown as GatewayRequester
+
+ vi.mocked(document.hasFocus).mockReturnValue(false)
+ renderHook(() => useStatusSnapshot('open', requestGateway))
+ await flushAsync()
+ expect(requestGateway).not.toHaveBeenCalled()
+
+ // A non-success flow change (e.g. entering the polling state) must not
+ // trigger the focus-bypassing refresh — only auth success does.
+ await act(async () => {
+ $desktopOnboarding.set({
+ ...$desktopOnboarding.get(),
+ flow: { status: 'starting', provider: { id: 'nous', name: 'Nous' } as never }
+ })
+ await vi.advanceTimersByTimeAsync(0)
+ })
+
+ expect(requestGateway).not.toHaveBeenCalled()
+ })
})
diff --git a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
index 92082982d85..19a716761fa 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
@@ -4,6 +4,7 @@ import { getStatus } from '@/hermes'
import { type I18nContextValue, useI18n } from '@/i18n'
import { evaluateRuntimeReadiness, type RuntimeReadinessResult } from '@/lib/runtime-readiness'
import { refreshFreeTierStatus, setFreeTierRoute } from '@/store/free-tier'
+import { $desktopOnboarding } from '@/store/onboarding'
import { $setupReadyTick } from '@/store/live-sync'
import { dismissNotification, notify } from '@/store/notifications'
import type { StatusResponse } from '@/types/hermes'
@@ -90,8 +91,8 @@ export function useStatusSnapshot(
}
}
- const refresh = async ({ readiness }: { readiness: boolean }) => {
- if (!isViewed()) {
+ const refresh = async ({ readiness, force = false }: { readiness: boolean; force?: boolean }) => {
+ if (!force && !isViewed()) {
scheduleRefresh()
return
@@ -150,12 +151,36 @@ export function useStatusSnapshot(
// outside the status tick so it neither resets nor waits on the timer.
const unsubscribeSetupReady = $setupReadyTick.listen(() => void refreshReadiness())
+ // An OAuth sign-in updates backend readiness, but ambient polling is
+ // skipped while the window is unfocused — so a stale credential failure can
+ // linger indefinitely if the user stays in the browser after completing the
+ // flow. Watch the onboarding store for the auth-success transition and force
+ // one readiness refresh that bypasses the focus gate. This does not resume
+ // background polling: the forced refresh reschedules the normal (focus-gated)
+ // cadence in its `finally`.
+ let lastFlowStatus = $desktopOnboarding.get().flow.status
+
+ const unsubscribeOnboarding = $desktopOnboarding.listen(state => {
+ const status = state.flow.status
+ const becameSuccess = status === 'success' && lastFlowStatus !== 'success'
+ lastFlowStatus = status
+
+ if (becameSuccess && !cancelled) {
+ if (timer !== undefined) {
+ window.clearTimeout(timer)
+ }
+
+ void refresh({ readiness: true, force: true })
+ }
+ })
+
document.addEventListener('visibilitychange', onReturn)
window.addEventListener('focus', onReturn)
void refresh({ readiness: true })
return () => {
cancelled = true
+ unsubscribeOnboarding()
unsubscribeSetupReady()
document.removeEventListener('visibilitychange', onReturn)
window.removeEventListener('focus', onReturn)