Skip to content

The agent that grows with you

fix(desktop): treat a boot-window ok:false as a hydration race, not a verdict

Brooklyn Nicholsonf886d133fe5e
diff --git a/apps/desktop/src/store/live-sync.ts b/apps/desktop/src/store/live-sync.ts
index b2bdb509307..74e4e9cc02d 100644
--- a/apps/desktop/src/store/live-sync.ts
+++ b/apps/desktop/src/store/live-sync.ts
@@ -40,6 +40,14 @@ export const $petChange = atom<{ meta?: PetChangeMeta; tick: number }>({ tick: 0
* waiting for its next ambient tick. */
export const $setupReadyTick = atom(0)
+/** Monotonic clock for "how fresh is this backend boot?" — bumped on every
+ * `setup.ready` from the active source and on every gateway switch/wipe, so
+ * onboarding can tell a round that answered inside the boot window (secrets
+ * may still be hydrating; a `runtime_check` ok:false is not yet evidence)
+ * from a steady-state one. Kept here, beside the tick, so both come from the
+ * same lifecycle seam. */
+export const $gatewayBootGeneration = atom(0)
+
export function setChangeEventsAvailable(available: boolean): void {
$changeEventsAvailable.set(available)
}
@@ -70,6 +78,7 @@ export function notifyPairingChanged(): void {
export function notifySetupReady(): void {
$setupReadyTick.set($setupReadyTick.get() + 1)
+ $gatewayBootGeneration.set($gatewayBootGeneration.get() + 1)
}
/** Reset on gateway wipe/reconnect — a new backend re-advertises capability on
@@ -77,4 +86,5 @@ export function notifySetupReady(): void {
* the wipe just cleared. */
export function resetLiveSync(): void {
$changeEventsAvailable.set(false)
+ $gatewayBootGeneration.set($gatewayBootGeneration.get() + 1)
}
diff --git a/apps/desktop/src/store/onboarding.test.ts b/apps/desktop/src/store/onboarding.test.ts
index da0e77c767f..3215e89f00f 100644
--- a/apps/desktop/src/store/onboarding.test.ts
+++ b/apps/desktop/src/store/onboarding.test.ts
@@ -12,6 +12,7 @@ import {
type OnboardingContext,
refreshOnboarding,
requestDesktopOnboarding,
+ resetBootRaceWindowForTests,
saveOnboardingLocalEndpoint,
setOnboardingModel,
submitOnboardingCode
@@ -172,6 +173,7 @@ describe('refreshOnboarding', () => {
afterEach(() => {
window.localStorage.clear()
$desktopOnboarding.set(baseState())
+ resetBootRaceWindowForTests()
vi.restoreAllMocks()
})
@@ -293,6 +295,49 @@ describe('refreshOnboarding', () => {
expect(notifySpy).not.toHaveBeenCalled()
})
+ it('does not downgrade a configured install when the boot race answers ok:false', async () => {
+ const { notifySetupReady } = await import('@/store/live-sync')
+
+ installApiMock(vi.fn())
+ // Fully configured install (durable cache present), backend just booted:
+ // setup.ready bumped the boot generation moments ago. The runtime_check
+ // answers ok:false because the external secret source (BWS) has not
+ // hydrated yet — a hydration race, not a credential verdict (#124939).
+ window.localStorage.setItem('hermes-desktop-onboarded-v1', '1')
+ $desktopOnboarding.set(baseState({ configured: true, providers: null, requested: false }))
+
+ notifySetupReady()
+
+ const ready = await refreshOnboarding(onboardingContext(emptyOpenRouterGateway()))
+
+ expect(ready).toBe(false)
+ expect($desktopOnboarding.get().configured).toBe(true)
+ expect(window.localStorage.getItem('hermes-desktop-onboarded-v1')).toBe('1')
+ })
+
+ it('still downgrades when the same ok:false arrives long after boot', async () => {
+ const { notifySetupReady } = await import('@/store/live-sync')
+
+ installApiMock(vi.fn())
+ window.localStorage.setItem('hermes-desktop-onboarded-v1', '1')
+ $desktopOnboarding.set(baseState({ configured: true, providers: null, requested: false }))
+
+ // Boot happened, then the grace window elapsed: an ok:false now is a real
+ // verdict (the secret source had its chance), so onboarding must surface.
+ notifySetupReady()
+ const clock = vi.spyOn(Date, 'now').mockReturnValue(Date.now() + 60_000)
+
+ try {
+ const ready = await refreshOnboarding(onboardingContext(emptyOpenRouterGateway()))
+
+ expect(ready).toBe(false)
+ expect($desktopOnboarding.get().configured).toBe(false)
+ expect(window.localStorage.getItem('hermes-desktop-onboarded-v1')).toBeNull()
+ } finally {
+ clock.mockRestore()
+ }
+ })
+
it('keeps a persisted "choose later" when a passive round completes onboarding', () => {
window.localStorage.setItem('hermes-onboarding-skipped-v1', '1')
$desktopOnboarding.set(baseState({ configured: null, firstRunSkipped: true }))
@@ -314,6 +359,8 @@ describe('refreshOnboarding', () => {
})
it('enters setup when the selected OpenRouter credential is genuinely empty', async () => {
+ // Outside the boot window: no setup.ready bump precedes the round, so an
+ // answered ok:false is a real verdict, not a hydration race.
installApiMock(vi.fn())
window.localStorage.setItem('hermes-desktop-onboarded-v1', '1')
$desktopOnboarding.set(
@@ -438,6 +485,7 @@ describe('OAuth onboarding', () => {
afterEach(() => {
window.localStorage.clear()
$desktopOnboarding.set(baseState())
+ resetBootRaceWindowForTests()
vi.restoreAllMocks()
})
@@ -606,6 +654,7 @@ describe('saveOnboardingLocalEndpoint', () => {
afterEach(() => {
window.localStorage.clear()
$desktopOnboarding.set(baseState())
+ resetBootRaceWindowForTests()
vi.restoreAllMocks()
})
@@ -996,6 +1045,7 @@ describe('setOnboardingModel', () => {
afterEach(() => {
window.localStorage.clear()
$desktopOnboarding.set(baseState())
+ resetBootRaceWindowForTests()
vi.restoreAllMocks()
})
diff --git a/apps/desktop/src/store/onboarding.ts b/apps/desktop/src/store/onboarding.ts
index 1176efae6d4..7fa387c5367 100644
--- a/apps/desktop/src/store/onboarding.ts
+++ b/apps/desktop/src/store/onboarding.ts
@@ -17,6 +17,7 @@ import { translateNow } from '@/i18n'
import { isProviderSetupErrorMessage } from '@/lib/provider-setup-errors'
import { evaluateRuntimeReadiness, type RuntimeReadinessResult } from '@/lib/runtime-readiness'
import { ackFreeTierNotice, freeTierReadyPending, refreshFreeTierStatus, setFreeTierRoute } from '@/store/free-tier'
+import { $gatewayBootGeneration } from '@/store/live-sync'
import { setMainModelAssignment } from '@/store/model-assignment'
import { dismissNotification, notify, notifyError } from '@/store/notifications'
import { guidedOnboardingActive } from '@/store/onboarding-gate'
@@ -251,6 +252,48 @@ function shouldPreserveConfiguredOnFallback(runtime: RuntimeReadinessResult, sta
return runtime.source === 'fallback' && state.configured !== false && !state.requested
}
+// How long after the last boot-generation bump a runtime_check ok:false is
+// treated as a boot race rather than a verdict. The backend announces
+// `setup.ready` when its boot bootstrap finishes resolving the inference
+// route; external secret sources (BWS-backed .env) hydrate a few seconds
+// AFTER the port opens, so an answered ok:false inside this window names a
+// hydration gap, not a missing credential (#124939). Sized to comfortably
+// cover the observed ~2-5 s hydration while staying short enough that a real
+// misconfiguration surfaces on the next ambient refresh.
+const BOOT_RACE_GRACE_MS = 15_000
+
+// Timestamp of the last `$gatewayBootGeneration` bump (setup.ready from the
+// active source, or a gateway switch/wipe). Null until the first bump, so a
+// steady-state session long after boot never treats an ok:false as a race.
+let lastBootGenerationAt: number | null = null
+
+$gatewayBootGeneration.listen(() => {
+ lastBootGenerationAt = Date.now()
+})
+
+/** Test/dev seam: forget any witnessed boot, so the next round is judged as
+ * steady-state. Mirrors how a real session far from boot behaves. */
+export function resetBootRaceWindowForTests(): void {
+ lastBootGenerationAt = null
+}
+
+function isInsideBootRaceWindow(): boolean {
+ return lastBootGenerationAt !== null && Date.now() - lastBootGenerationAt < BOOT_RACE_GRACE_MS
+}
+
+function shouldPreserveConfiguredOnBootRace(runtime: RuntimeReadinessResult, state: DesktopOnboardingState): boolean {
+ // The probes ANSWERED this time, but the round ran inside the backend's boot
+ // window: the runtime_check resolved a route whose external secret source
+ // (BWS) had not hydrated yet and reported ok:false for it. That is a
+ // retryable not-ready, not an auth verdict — downgrading configured:false
+ // here was the flash of "No usable credentials found for openrouter" on
+ // every update restart of a fully configured install (#124939). The state
+ // must already be configured (verified earlier or the durable cache) so a
+ // genuinely unconfigured install still enters onboarding on boot.
+ return runtime.source === 'runtime_check' && !runtime.ready &&
+ state.configured === true && !state.requested && isInsideBootRaceWindow()
+}
+
function notifyReady(provider: string) {
notify({ kind: 'success', title: 'Hermes is ready', message: `${provider} connected.` })
}
@@ -719,6 +762,9 @@ export async function refreshOnboarding(ctx: OnboardingContext, stillWanted?: ()
return false
}
+ // A boot-race round (see shouldPreserveConfiguredOnBootRace) is recognized
+ // from the module-level boot-generation clock, so there is nothing to
+ // seed here — the round below just reads it.
const runtime = await checkRuntime(ctx)
if (stillWanted && !stillWanted()) {
@@ -758,6 +804,14 @@ export async function refreshOnboarding(ctx: OnboardingContext, stillWanted?: ()
const reason = runtime.reason || state.reason || DEFAULT_ONBOARDING_REASON
+ if (shouldPreserveConfiguredOnBootRace(runtime, state)) {
+ // The backend answered inside its boot window with a not-ready that is
+ // explained by external secrets still hydrating (#124939). Do not write
+ // the downgrade: the durable cache stays configured and the setup.ready
+ // tick (or the next ambient refresh) re-checks once the route is real.
+ return false
+ }
+
writeCachedConfigured(false)
patch({ configured: false, reason })