Skip to content

The agent that grows with you

fix(desktop): the onboarding due check waits for its answer, and the boot overlay offers Continue

alt-glitch4ec10162e9a7
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
index 06641ecb48f..d7a09c47604 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/lifecycle.ts
@@ -4,6 +4,7 @@ import type { HermesSkin } from '@hermes/shared/skin'
import { invalidateContextBreakdown } from '@/app/shell/hooks/use-context-breakdown'
import { clearClarifyRequest } from '@/store/clarify'
import {
+ noteGatewayReady,
notifyCronChanged,
notifyPairingChanged,
notifyPetChanged,
@@ -36,6 +37,10 @@ export function handleLifecycleEvent(ctx: GatewayEventContext): boolean {
// events; consumers demote their legacy polls to slow backstops.
setChangeEventsAvailable(Boolean(ready?.change_events))
+ if (fromActiveSource()) {
+ noteGatewayReady()
+ }
+
return true
}
diff --git a/apps/desktop/src/components/gateway-connecting-overlay.tsx b/apps/desktop/src/components/gateway-connecting-overlay.tsx
index de7ad209215..d0db1e1a1c7 100644
--- a/apps/desktop/src/components/gateway-connecting-overlay.tsx
+++ b/apps/desktop/src/components/gateway-connecting-overlay.tsx
@@ -9,7 +9,7 @@ import { useI18n } from '@/i18n'
import { cn } from '@/lib/utils'
import { $desktopBoot, type DesktopBootState } from '@/store/boot'
import { $gatewaySwitching } from '@/store/gateway-switch'
-import { $introStartExit, $introView } from '@/store/onboarding-intro'
+import { $decideExit, $introStartExit, $introView } from '@/store/onboarding-intro'
import { $onboardingDecided } from '@/store/onboarding-presence'
import { $gatewayState } from '@/store/session'
import { isMainWindow } from '@/store/windows'
@@ -114,23 +114,26 @@ function ConnectingOverlayView({ decoding, onExit, phase }: ConnectingOverlayVie
}
/**
- * Held until the app knows whether onboarding runs, so the chat never flashes before it; the due
- * check has its own short deadline (`decideOnboarding`). Once onboarding runs, the first-run intro's
- * `starting` screen is this overlay and stays up until its chat is open, offering a way out when the
- * start is slow. A rerun from Settings brings that screen back after boot.
+ * Held until the app knows whether onboarding runs, so the chat never flashes before it. The due check
+ * has no deadline: past its budget (`decideOnboarding`) this overlay offers Continue, which releases the
+ * app for this launch. Once onboarding runs, the first-run intro's `starting` screen is this overlay and
+ * stays up until its chat is open, offering a way out when the start is slow. A rerun from Settings
+ * brings that screen back after boot.
*/
function useOnboardingHold(previewing: boolean) {
const onboardingDecided = useStore($onboardingDecided)
const introView = useStore($introView)
const introStartExit = useStore($introStartExit)
+ const decideExit = useStore($decideExit)
const watching = !previewing && isMainWindow()
const introStarting = watching && introView === 'starting'
+ const deciding = watching && !onboardingDecided
return {
- holds: introStarting || (watching && !onboardingDecided),
+ holds: introStarting || deciding,
introStarting,
- onExit: introStarting ? introStartExit : null
+ onExit: introStarting ? introStartExit : deciding ? decideExit : null
}
}
diff --git a/apps/desktop/src/store/live-sync.ts b/apps/desktop/src/store/live-sync.ts
index 74e4e9cc02d..991bc6425cf 100644
--- a/apps/desktop/src/store/live-sync.ts
+++ b/apps/desktop/src/store/live-sync.ts
@@ -48,6 +48,14 @@ export const $setupReadyTick = atom(0)
* same lifecycle seam. */
export const $gatewayBootGeneration = atom(0)
+/** When the active source's socket last said `gateway.ready` (the backend dispatches requests from
+ * then on); null before the first. The onboarding due check counts its way-out budget from it. */
+export const $gatewayReadyAt = atom<null | number>(null)
+
+export function noteGatewayReady(): void {
+ $gatewayReadyAt.set(Date.now())
+}
+
export function setChangeEventsAvailable(available: boolean): void {
$changeEventsAvailable.set(available)
}
diff --git a/apps/desktop/src/store/onboarding-decide.test.ts b/apps/desktop/src/store/onboarding-decide.test.ts
new file mode 100644
index 00000000000..2dd913741a7
--- /dev/null
+++ b/apps/desktop/src/store/onboarding-decide.test.ts
@@ -0,0 +1,105 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import type { OnboardingRequest } from './onboarding-run'
+
+// The launch's due check runs once per module (`decision ??= …`), so every test loads a fresh copy.
+async function freshStores() {
+ vi.resetModules()
+
+ const [run, presence, intro, liveSync, session] = await Promise.all([
+ import('./onboarding-run'),
+ import('./onboarding-presence'),
+ import('./onboarding-intro'),
+ import('./live-sync'),
+ import('./session')
+ ])
+
+ // SAFETY: factsSource reads only `mode`; a local connection skips the machine comparison.
+ session.$connection.set({ mode: 'local' } as never)
+
+ return { intro, liveSync, presence, run }
+}
+
+const ensureProfile = async (): Promise<void> => undefined
+const resume = async (): Promise<void> => undefined
+
+/** A requester that keeps the caller's deadline like the real gateway does, and answers
+ * `onboarding.state` only when the test says so. */
+function slowBackend() {
+ const calls: string[] = []
+ let answer: (state: { eligible: boolean; run: boolean }) => void = () => undefined
+ const firstState = new Promise<{ eligible: boolean; run: boolean }>(resolve => (answer = resolve))
+ let stateReads = 0
+
+ const request: OnboardingRequest = <T,>(
+ method: string,
+ _params?: Parameters<OnboardingRequest>[1],
+ timeoutMs?: number
+ ): Promise<T> => {
+ calls.push(method)
+
+ const reply: Promise<unknown> =
+ method === 'onboarding.state' && stateReads++ === 0
+ ? firstState.then(state => ({ ...state, run_id: 'run-1', session_id: 'st-1' }))
+ : Promise.resolve(
+ method === 'onboarding.start'
+ ? { resumed: false, run_id: 'run-1', session_id: 'rt-1', stored_session_id: 'st-1' }
+ : { eligible: true, run: true, run_id: 'run-1', session_id: 'st-1' }
+ )
+
+ const deadline =
+ timeoutMs && timeoutMs > 0
+ ? new Promise<never>((_resolve, reject) => setTimeout(() => reject(new Error('request timed out')), timeoutMs))
+ : null
+
+ // SAFETY: the fake answers each method with its contract result shape.
+ return (deadline ? Promise.race([reply, deadline]) : reply) as Promise<T>
+ }
+
+ return { answer, calls, request }
+}
+
+beforeEach(() => {
+ vi.useFakeTimers()
+})
+
+afterEach(() => {
+ vi.useRealTimers()
+})
+
+describe('the launch due check', () => {
+ it('waits for a slow answer with no client deadline and still opens onboarding', async () => {
+ const { run } = await freshStores()
+ const backend = slowBackend()
+
+ const decided = run.decideOnboarding(backend.request, resume, { ensureProfile })
+ await vi.advanceTimersByTimeAsync(10_000)
+ backend.answer({ eligible: true, run: true })
+ await decided
+
+ expect(backend.calls).toContain('onboarding.start')
+ })
+
+ it('offers Continue a budget after gateway.ready, and a late due answer after it opens nothing', async () => {
+ const { intro, liveSync, presence, run } = await freshStores()
+ const backend = slowBackend()
+
+ const decided = run.decideOnboarding(backend.request, resume, { ensureProfile })
+ // The socket opened 5 s before the backend said ready; the budget counts from ready.
+ await vi.advanceTimersByTimeAsync(5_000)
+ liveSync.noteGatewayReady()
+ await vi.advanceTimersByTimeAsync(14_000)
+ expect(intro.$decideExit.get()).toBeNull()
+
+ await vi.advanceTimersByTimeAsync(1_000)
+ intro.$decideExit.get()?.()
+
+ expect(presence.$onboardingDecided.get()).toBe(true)
+ expect(intro.$decideExit.get()).toBeNull()
+
+ backend.answer({ eligible: true, run: true })
+ await decided
+
+ expect(backend.calls).toEqual(['onboarding.state'])
+ })
+})
diff --git a/apps/desktop/src/store/onboarding-intro.ts b/apps/desktop/src/store/onboarding-intro.ts
index 70d331c3080..d490536b0ae 100644
--- a/apps/desktop/src/store/onboarding-intro.ts
+++ b/apps/desktop/src/store/onboarding-intro.ts
@@ -16,6 +16,10 @@ export const $introView = atom<IntroView>('off')
* this way out, which ends the start through the kickoff's failed path. Null otherwise. */
export const $introStartExit = atom<(() => void) | null>(null)
+/** Set once the launch's due check has outlasted its budget: the boot overlay keeps waiting and offers
+ * this way out, which releases the app for this launch. Null otherwise. */
+export const $decideExit = atom<(() => void) | null>(null)
+
/** The demo layout is on screen: the chat alone, narrower, minimal composer, no status bar. Kept here,
* free of layout imports, so the shell can read it; onboarding/first-run/assembly.ts owns the switch. */
export const $chatOnboardingSolo = atom(false)
diff --git a/apps/desktop/src/store/onboarding-presence.ts b/apps/desktop/src/store/onboarding-presence.ts
index 93a69055c68..573a04db167 100644
--- a/apps/desktop/src/store/onboarding-presence.ts
+++ b/apps/desktop/src/store/onboarding-presence.ts
@@ -9,13 +9,14 @@ const EMPTY: ReadonlySet<OnboardingSurface> = new Set()
export const $onboardingSurfaces = atom<ReadonlySet<OnboardingSurface>>(EMPTY)
/**
- * First-run onboarding's due check has answered (or timed out, or does not apply in this
- * window). Until it has, every "not during setup" surface waits: onboarding may still open.
+ * First-run onboarding's due check has answered (or the person went on without it, or it does not
+ * apply in this window). Until it has, every "not during setup" surface waits: onboarding may still open.
*/
export const $onboardingDecided = atom(false)
-/** How long anything waits on the due check before treating onboarding as not due. */
-export const ONBOARDING_DECIDE_DEADLINE_MS = 4_000
+/** How long after `gateway.ready` the boot overlay waits on the due check before it offers Continue.
+ * The backend waits up to 8 s on its boot record, then works the answer out live. */
+export const ONBOARDING_DECIDE_OFFER_MS = 15_000
export function markOnboardingDecided(): void {
$onboardingDecided.set(true)
diff --git a/apps/desktop/src/store/onboarding-run.ts b/apps/desktop/src/store/onboarding-run.ts
index 585adc04eed..7ef5d22b6a1 100644
--- a/apps/desktop/src/store/onboarding-run.ts
+++ b/apps/desktop/src/store/onboarding-run.ts
@@ -22,9 +22,11 @@ import type { GatewayRequester } from '@/lib/yolo-session'
import { ackFreeTierNotice } from './free-tier'
import { activeGatewayConnectionId } from './gateway'
+import { $gatewayReadyAt } from './live-sync'
import { clearFreeTierIntro } from './onboarding'
+import { $decideExit } from './onboarding-intro'
import { $onboardingPhase } from './onboarding-phase'
-import { markOnboardingDecided, ONBOARDING_DECIDE_DEADLINE_MS, setOnboardingSurfaceActive } from './onboarding-presence'
+import { markOnboardingDecided, ONBOARDING_DECIDE_OFFER_MS, setOnboardingSurfaceActive } from './onboarding-presence'
import { $onboardingSession } from './onboarding-session'
import { ensureGatewayProfile, newSessionInProfile } from './profile'
import { $connection, $gatewayState, $selectedStoredSessionId } from './session'
@@ -40,8 +42,9 @@ export type OnboardingRequest = <T = unknown>(
// The generated param interfaces carry no index signature; the requester's own param type supplies it.
type RequestParams = NonNullable<Parameters<OnboardingRequest>[1]>
-// A cold first boot answers onboarding.start late; the intro's way-out offer bounds the wait instead.
-// A start with no intro (Settings' Run again) has no way out on screen, so it keeps the request's default deadline.
+// A cold first boot answers onboarding.state and onboarding.start late; a way-out offer on screen bounds
+// the wait instead of a deadline. The launch and Settings' Run again both pass the intro, so both starts
+// get the intro's way out; a start with no intro keeps the request's default deadline.
const NO_DEADLINE = 0
/** Opens a stored session on screen, replacing the route (`useSessionActions().resumeSession`). */
@@ -230,6 +233,15 @@ export function decideOnboarding(
}
async function readAndStart(request: OnboardingRequest, resume: OnboardingResume, options?: StartOptions): Promise<void> {
+ // Continue on the boot overlay released this launch: a late answer opens nothing, and onboarding
+ // stays due on the backend for the next launch or Settings' Run onboarding again.
+ let released = false
+
+ const disarm = offerDecideExit(() => {
+ released = true
+ markOnboardingDecided()
+ })
+
try {
const params: OnboardingStateParams & RequestParams = {}
@@ -237,19 +249,48 @@ async function readAndStart(request: OnboardingRequest, resume: OnboardingResume
params.assume_fresh = true
}
- // One deadline for the due check: past it the app is released and a late answer opens nothing.
- const state = await request<OnboardingRunStateResult>('onboarding.state', params, ONBOARDING_DECIDE_DEADLINE_MS)
+ const state = await request<OnboardingRunStateResult>('onboarding.state', params, NO_DEADLINE)
+ disarm()
- if (state.run && state.eligible) {
+ if (!released && state.run && state.eligible) {
await startOnboarding(request, resume, { ...options, freshLaunch: true })
}
} catch (error) {
console.warn('[onboarding] state could not be read', error)
} finally {
+ disarm()
markOnboardingDecided()
}
}
+/** The due check's way out, armed like the intro's (`kickoff.ts`): `ONBOARDING_DECIDE_OFFER_MS` after
+ * the backend said ready, the boot overlay offers `release` through `$decideExit`. Returns the disarm. */
+function offerDecideExit(release: () => void): () => void {
+ let timer: ReturnType<typeof setTimeout> | undefined
+ let stopWatching = (): void => undefined
+
+ const disarm = (): void => {
+ stopWatching()
+ clearTimeout(timer)
+ $decideExit.set(null)
+ }
+
+ const takeExit = (): void => {
+ disarm()
+ release()
+ }
+
+ stopWatching = $gatewayReadyAt.subscribe(readyAt => {
+ clearTimeout(timer)
+
+ if (readyAt !== null) {
+ timer = setTimeout(() => $decideExit.set(takeExit), readyAt + ONBOARDING_DECIDE_OFFER_MS - Date.now())
+ }
+ })
+
+ return disarm
+}
+
/** The run ended: the model finished it or the user skipped. Later calls for the same run do nothing. */
export function endOnboarding(outcome: 'done' | 'skipped'): void {
if ($onboardingPhase.get() !== 'shown') {