Skip to content

The agent that grows with you

fix(desktop): Skip setup goes away when the onboarding run ends

alt-glitch4bcaa838df5e
diff --git a/apps/desktop/src/components/onboarding/skip-setup.test.tsx b/apps/desktop/src/components/onboarding/skip-setup.test.tsx
new file mode 100644
index 00000000000..cf2e6adc7f1
--- /dev/null
+++ b/apps/desktop/src/components/onboarding/skip-setup.test.tsx
@@ -0,0 +1,62 @@
+import { act, cleanup, render, screen } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { $onboardingPhase } from '@/store/onboarding-phase'
+import { noteOnboardingEnded, startOnboarding } from '@/store/onboarding-run'
+import { isOnboardingChat } from '@/store/onboarding-session'
+import { $connection, $selectedStoredSessionId } from '@/store/session'
+
+import { OnboardingSkip } from './skip-setup'
+
+vi.mock('@/app/gateway/hooks/use-gateway-request', () => ({
+ useGatewayRequest: () => ({ requestGateway: vi.fn() })
+}))
+
+const started = { resumed: false, run_id: 'run-1', session_id: 'rt-1', stored_session_id: 'st-1' }
+const ended = { outcome: 'done', run_id: 'run-1', stored_session_id: 'st-1' }
+const ensureProfile = async (): Promise<void> => undefined
+const resume = async (storedId: string) => $selectedStoredSessionId.set(storedId)
+
+// SAFETY: the fake answers onboarding.start and onboarding.state with the fields startOnboarding reads.
+const request = async <T,>(method: string): Promise<T> =>
+ (method === 'onboarding.state' ? { eligible: true, run: true, run_id: 'run-1', session_id: 'st-1' } : started) as T
+
+async function onboardingChatOnScreen() {
+ await startOnboarding(request, resume, { ensureProfile })
+ render(<OnboardingSkip sessionId="rt-1" />)
+ expect(screen.getByRole('button', { name: 'Skip setup' })).toBeTruthy()
+}
+
+beforeEach(() => {
+ // SAFETY: factsSource reads only `mode`.
+ $connection.set({ mode: 'local' } as never)
+ $onboardingPhase.set('idle')
+})
+
+afterEach(cleanup)
+
+describe('Skip setup', () => {
+ it('goes away when the backend ends the run', async () => {
+ await onboardingChatOnScreen()
+
+ act(() => noteOnboardingEnded(ended))
+
+ expect(screen.queryByRole('button', { name: 'Skip setup' })).toBeNull()
+ })
+
+ // Last in the file: a throwing listener leaves nanostores' shared listener queue undrained.
+ it('goes away even when a listener of the ending throws', async () => {
+ await onboardingChatOnScreen()
+ $onboardingPhase.listen(phase => {
+ if (phase === 'done') {
+ throw new Error('listener failed')
+ }
+ })
+
+ act(() => expect(() => noteOnboardingEnded(ended)).toThrow('listener failed'))
+
+ expect($onboardingPhase.get()).toBe('done')
+ expect(isOnboardingChat('rt-1')).toBe(false)
+ expect(screen.queryByRole('button', { name: 'Skip setup' })).toBeNull()
+ })
+})
diff --git a/apps/desktop/src/components/onboarding/skip-setup.tsx b/apps/desktop/src/components/onboarding/skip-setup.tsx
index c0657c63691..637b0712af7 100644
--- a/apps/desktop/src/components/onboarding/skip-setup.tsx
+++ b/apps/desktop/src/components/onboarding/skip-setup.tsx
@@ -1,4 +1,5 @@
import { useStore } from '@nanostores/react'
+import { useMemo } from 'react'
import { useGatewayRequest } from '@/app/gateway/hooks/use-gateway-request'
import { skipIntro } from '@/components/onboarding/first-run/intro'
@@ -6,15 +7,15 @@ import { Tip } from '@/components/ui/tooltip'
import { useI18n } from '@/i18n'
import { notifyError } from '@/store/notifications'
import { skipOnboarding } from '@/store/onboarding-run'
-import { $onboardingSession, isOnboardingChat } from '@/store/onboarding-session'
+import { onboardingChatStore } from '@/store/onboarding-session'
/** Skip setup, shown in the composer of the onboarding chat only. Once the backend has ended the run,
* like main's: the intro ends on the previous layout at normal size and the tutorial tips stop. */
export function OnboardingSkip({ sessionId }: { sessionId: null | string }) {
- useStore($onboardingSession)
+ const onboardingChat = useStore(useMemo(() => onboardingChatStore(sessionId), [sessionId]))
// The chat's current runtime id: a resume after a reconnect gives it a new one.
- return sessionId && isOnboardingChat(sessionId) ? <SkipButton runtimeId={sessionId} /> : null
+ return sessionId && onboardingChat ? <SkipButton runtimeId={sessionId} /> : null
}
// Split out so the gateway hook mounts only in the onboarding chat, not in every composer.
diff --git a/apps/desktop/src/store/onboarding-run.ts b/apps/desktop/src/store/onboarding-run.ts
index 0ea681a650c..a76909620b6 100644
--- a/apps/desktop/src/store/onboarding-run.ts
+++ b/apps/desktop/src/store/onboarding-run.ts
@@ -217,9 +217,11 @@ export function endOnboarding(outcome: 'done' | 'skipped'): void {
return
}
+ // The chat is let go first: a listener that throws on the ending below must not leave this window
+ // treating it as the onboarding chat (Skip setup stayed up after a run ended).
+ $onboardingSession.set(null)
$onboardingPhase.set(outcome)
$onboardingEnded.set(outcome)
- $onboardingSession.set(null)
$onboardingSettleFrom.set($onboardingSettleFrom.get() ?? Date.now())
}
diff --git a/apps/desktop/src/store/onboarding-session.ts b/apps/desktop/src/store/onboarding-session.ts
index 78514ffd1a1..e47f39546d1 100644
--- a/apps/desktop/src/store/onboarding-session.ts
+++ b/apps/desktop/src/store/onboarding-session.ts
@@ -1,6 +1,7 @@
-import { atom } from 'nanostores'
+import { atom, computed, type ReadableAtom } from 'nanostores'
-import { storedSessionIdForRuntimeId } from './session-states'
+import { $onboardingPhase } from './onboarding-phase'
+import { $sessionTiles, storedSessionIdForRuntimeId } from './session-states'
export interface OnboardingSession {
/** Names the run in `onboarding.ended` and `onboarding.state`. */
@@ -13,11 +14,21 @@ export interface OnboardingSession {
export const $onboardingSession = atom<null | OnboardingSession>(null)
/** Whether runtime id `sessionId` shows the onboarding chat in progress. Matched by the stored id too:
- * a resume after a reconnect or a backend restart gives the chat a new runtime id. */
+ * a resume after a reconnect or a backend restart gives the chat a new runtime id. Only while the run
+ * is shown: an ended run is never in progress, whatever the session atom still holds. */
export function isOnboardingChat(sessionId: null | string | undefined): boolean {
const session = $onboardingSession.get()
return Boolean(
- sessionId && session && (session.runtimeId === sessionId || storedSessionIdForRuntimeId(sessionId) === session.storedId)
+ sessionId &&
+ session &&
+ $onboardingPhase.get() === 'shown' &&
+ (session.runtimeId === sessionId || storedSessionIdForRuntimeId(sessionId) === session.storedId)
)
}
+
+/** `isOnboardingChat` as a store, for a render. The React Compiler memoizes a plain call on its
+ * arguments, so a component that calls `isOnboardingChat(sessionId)` keeps its first answer while the
+ * session id stays the same (Skip setup outlived the run that way). */
+export const onboardingChatStore = (sessionId: null | string): ReadableAtom<boolean> =>
+ computed([$onboardingSession, $onboardingPhase, $sessionTiles], () => isOnboardingChat(sessionId))
diff --git a/apps/desktop/src/store/parked-card-answers.test.ts b/apps/desktop/src/store/parked-card-answers.test.ts
index 5ce3f00795d..9f5788407ae 100644
--- a/apps/desktop/src/store/parked-card-answers.test.ts
+++ b/apps/desktop/src/store/parked-card-answers.test.ts
@@ -1,6 +1,7 @@
import { atom } from 'nanostores'
import { afterEach, describe, expect, it, vi } from 'vitest'
+import { $onboardingPhase } from './onboarding-phase'
import { $onboardingSession } from './onboarding-session'
import {
answerParkedCard,
@@ -17,6 +18,7 @@ describe('parked card answers', () => {
afterEach(() => {
unregister.splice(0).forEach(off => off())
$onboardingSession.set(null)
+ $onboardingPhase.set('idle')
})
it('stops at the first card that takes the text, and forgets a card once it unregisters', () => {
@@ -55,6 +57,8 @@ describe('parked card answers', () => {
expect(answerParkedCard('session-a', 'hello')).toBe(false)
expect($waiting.get()).toBe(false)
+ // A run is shown whenever its chat is named (startOnboarding).
+ $onboardingPhase.set('shown')
$onboardingSession.set({ runId: 'run-1', runtimeId: 'session-a', storedId: 'stored-a' })
expect($waiting.get()).toBe(true)
expect(answerParkedCard('session-a', 'hello')).toBe(true)
diff --git a/apps/desktop/src/store/parked-card-answers.ts b/apps/desktop/src/store/parked-card-answers.ts
index 4e77031a199..11da4664c7a 100644
--- a/apps/desktop/src/store/parked-card-answers.ts
+++ b/apps/desktop/src/store/parked-card-answers.ts
@@ -1,6 +1,6 @@
import { atom, computed, type ReadableAtom } from 'nanostores'
-import { $onboardingSession, isOnboardingChat } from './onboarding-session'
+import { isOnboardingChat, onboardingChatStore } from './onboarding-session'
/** Answers a card parked on `sessionId` with composer text: null when no card of this kind takes it,
* else a promise of whether the answer reached the backend. */
@@ -105,4 +105,4 @@ export function parkedCardWaiting(sessionId: string): boolean {
/** Whether a card waits in the onboarding chat, as a store: the composer offers Send for typed
* answers only there; every other chat keeps its normal controls. */
export const sessionOnboardingCardWaiting = (sessionId: null | string) =>
- computed([$revision, $onboardingSession], () => isOnboardingChat(sessionId) && parkedCardWaiting(sessionId ?? ''))
+ computed([$revision, onboardingChatStore(sessionId)], (_, onChat) => onChat && parkedCardWaiting(sessionId ?? ''))