Skip to content

The agent that grows with you

fix(desktop): back off re-checking a local model server that stays down

Teknium8d8a4a0365cb
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 a431968d362..e9c74110d12 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
@@ -11,6 +11,7 @@ import { $setupReadyTick, notifySetupReady } from '@/store/live-sync'
import { $notifications, clearNotifications } from '@/store/notifications'
import { $desktopOnboarding, refreshOnboarding } from '@/store/onboarding'
import { markOnboardingDecided } from '@/store/onboarding-presence'
+import { $currentModel } from '@/store/session'
import { deferred } from '../../../test/deferred'
@@ -444,6 +445,54 @@ describe('useStatusSnapshot', () => {
expect(notice()).toBeUndefined()
})
+ it('backs off re-checking a local model server that stays down, and resets on a model switch', async () => {
+ // Enabled but unable to boot (no staged model): every check kicks a boot and blocks for seconds.
+ const requestGatewayMock = vi.fn(
+ async (method: string) =>
+ (method === 'setup.runtime_check'
+ ? { error: 'The local model server is not running.', error_code: 'local_runtime_unavailable', ok: false }
+ : { provider_configured: true }) as never
+ )
+
+ const requestGateway = requestGatewayMock as unknown as GatewayRequester
+ const checks = () => requestGatewayMock.mock.calls.filter(([method]) => method === 'setup.runtime_check').length
+
+ const advance = async (ms: number) => {
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(ms)
+ })
+ }
+
+ renderHook(() => useStatusSnapshot('open', requestGateway))
+ await flushAsync()
+ expect(checks()).toBe(1)
+
+ await advance(60_000) // first gap: 60 s
+ expect(checks()).toBe(2)
+
+ await advance(60_000) // second gap: 2 min
+ expect(checks()).toBe(2)
+ await advance(60_000)
+ expect(checks()).toBe(3)
+
+ await advance(240_000) // then a 5-minute cap
+ expect(checks()).toBe(3)
+ await advance(60_000)
+ expect(checks()).toBe(4)
+ await advance(240_000)
+ expect(checks()).toBe(4)
+ await advance(60_000)
+ expect(checks()).toBe(5)
+
+ // The status tick itself never slowed down.
+ expect(getStatus).toHaveBeenCalledTimes(14)
+
+ // A model switch asks again on the next tick.
+ $currentModel.set('another/model')
+ await advance(60_000)
+ expect(checks()).toBe(6)
+ })
+
it('re-reads readiness and the free-tier verdict once per setup.ready, off the status tick', async () => {
const requestGatewayMock = vi.fn(
async (method: string) =>
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 3933c7613b5..0451d8e2573 100644
--- a/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
+++ b/apps/desktop/src/app/shell/hooks/use-status-snapshot.ts
@@ -8,6 +8,7 @@ import { $freeTierStatus, refreshFreeTierStatus, setFreeTierRoute } from '@/stor
import { $setupReadyTick } from '@/store/live-sync'
import { dismissNotification, notify } from '@/store/notifications'
import { $desktopOnboarding, isLocalRuntimeUnavailable, settleLocalRuntimeNotice } from '@/store/onboarding'
+import { $currentModel } from '@/store/session'
import type { StatusResponse } from '@/types/hermes'
// Statusbar health is ambient chrome, not live data — nothing the user acts on
@@ -15,6 +16,12 @@ import type { StatusResponse } from '@/types/hermes'
// visibility listeners refresh immediately on return.
const REFRESH_MS = 60_000
+// Re-check delays while the local model server stays down. Each check of an enabled server asks the
+// backend to boot it and waits up to ~8 s; a server that cannot boot (no staged model, crashing
+// engine) would pay that every tick, so the gap grows to a 5-minute cap. A model switch or a ready
+// answer resets it; focus returns and `setup.ready` still check at once.
+const LOCAL_DOWN_RECHECK_MS = [60_000, 120_000, 300_000]
+
// The scope the cached free-tier verdict was read under. Module-level because
// $freeTierStatus is one app-wide atom, not per hook instance.
let freeTierScope: string | undefined
@@ -41,13 +48,33 @@ export function useStatusSnapshot(
// lifetime is exactly this run's — the same run that clears the status on
// entry — so the two can never disagree.
let readinessAnswered = false
+ // Consecutive "local model server down" verdicts, and when the next re-check is due.
+ let localDownStreak = 0
+ let localRecheckAt = 0
+
+ const resetLocalBackoff = () => {
+ localDownStreak = 0
+ localRecheckAt = 0
+ }
const publishInferenceStatus = (next: RuntimeReadinessResult | null): void => {
- // A down local model server is not settled: it may be starting, so the tick keeps checking.
- readinessAnswered = next !== null && !isLocalRuntimeUnavailable(next)
+ // A down local model server is not settled: it may be starting, so the tick keeps checking,
+ // on a growing gap.
+ const localDown = next !== null && isLocalRuntimeUnavailable(next)
+ readinessAnswered = next !== null && !localDown
+
+ if (localDown) {
+ localRecheckAt = Date.now() + LOCAL_DOWN_RECHECK_MS[Math.min(localDownStreak, LOCAL_DOWN_RECHECK_MS.length - 1)]
+ localDownStreak += 1
+ } else {
+ resetLocalBackoff()
+ }
+
setInferenceStatus(next)
}
+ const readinessDue = () => !readinessAnswered && Date.now() >= localRecheckAt
+
// Status and inference readiness belong to one backend. A source switch
// can keep gatewayState="open" throughout, so clear the previous source's
// snapshot and start a fresh scoped request explicitly.
@@ -77,7 +104,7 @@ export function useStatusSnapshot(
// are seams this window may never cross again, so a gateway flap
// outliving one seam would pin the chip for good. Once a verdict
// exists the tick stays status-only, keeping the ambient poll cheap.
- timer = window.setTimeout(() => void refresh({ readiness: !readinessAnswered }), REFRESH_MS)
+ timer = window.setTimeout(() => void refresh({ readiness: readinessDue() }), REFRESH_MS)
}
}
@@ -186,6 +213,8 @@ export function useStatusSnapshot(
// readiness round now, so the chip/strip/onboarding move at once. Rides
// outside the status tick so it neither resets nor waits on the timer.
const unsubscribeSetupReady = $setupReadyTick.listen(() => void refreshReadiness())
+ // Another model may not be local at all: the next tick asks again instead of waiting out the gap.
+ const unsubscribeModel = $currentModel.listen(resetLocalBackoff)
// An OAuth sign-in updates backend readiness, but ambient polling is
// skipped while the window is unfocused — so a stale credential failure can
@@ -218,6 +247,7 @@ export function useStatusSnapshot(
cancelled = true
unsubscribeOnboarding()
unsubscribeSetupReady()
+ unsubscribeModel()
document.removeEventListener('visibilitychange', onReturn)
window.removeEventListener('focus', onReturn)