Skip to content

The agent that grows with you

fix(desktop): a setup chat finishing in the background no longer opens the tour

alt-glitch8fdca6b3c738
diff --git a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts
index 8b7f8e84ce0..e3ee6ae85a1 100644
--- a/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts
+++ b/apps/desktop/src/app/session/hooks/use-message-stream/gateway-event/tools.ts
@@ -41,7 +41,9 @@ function recordToolPreview(ctx: GatewayEventContext, sessionId: string, cwd: str
}
function reportStartChatHandoff(payload: GatewayEventPayload | undefined, sessionId: string): void {
- if (payload?.name !== 'start_chat' || readStartChatResult(payload.result)?.status !== 'started') {
+ const outcome = readStartChatResult(payload?.result)
+
+ if (payload?.name !== 'start_chat' || outcome?.status !== 'started') {
return
}
@@ -52,7 +54,7 @@ function reportStartChatHandoff(payload: GatewayEventPayload | undefined, sessio
}
// From the setup chat, the handoff completes the guided first run (no-op elsewhere).
- finishGuidedOnboarding(sessionId)
+ finishGuidedOnboarding(sessionId, outcome.sessionId)
}
/** tool.generating / tool.start / tool.complete / subagent.*. */
diff --git a/apps/desktop/src/components/onboarding-chat/intro.test.ts b/apps/desktop/src/components/onboarding-chat/intro.test.ts
new file mode 100644
index 00000000000..e3f8be8f26f
--- /dev/null
+++ b/apps/desktop/src/components/onboarding-chat/intro.test.ts
@@ -0,0 +1,57 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { stopTour } from '@/lib/tour'
+import { $tourActive } from '@/lib/tour/tour-active'
+import { $onboardingGate } from '@/store/onboarding-gate'
+import { $selectedStoredSessionId } from '@/store/session'
+import { $toursEnabled } from '@/store/tours'
+
+import { $chatOnboardingThreadIds } from './assembly'
+import { finishGuidedOnboarding } from './intro'
+
+const SETUP = 'setup-chat'
+const TASK = 'task-chat'
+
+describe('finishGuidedOnboarding handoff tour', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ // jsdom lays nothing out; the tour only targets a rail with a size.
+ vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue(new DOMRect(0, 0, 10, 10))
+ document.body.innerHTML = '<div data-tour="profile-rail"></div>'
+ $chatOnboardingThreadIds.set([SETUP])
+ $onboardingGate.set({ guideKickoff: 'idle', guideQueued: false, phase: 'left' })
+ $toursEnabled.set(true)
+ $selectedStoredSessionId.set(SETUP)
+ })
+
+ afterEach(async () => {
+ await stopTour()
+ vi.useRealTimers()
+ vi.restoreAllMocks()
+ })
+
+ it('opens the tour over the task chat the watched handoff opened', async () => {
+ finishGuidedOnboarding(SETUP, TASK)
+ $selectedStoredSessionId.set(TASK)
+ await vi.advanceTimersByTimeAsync(10_000)
+
+ expect($tourActive.get()).toBe(true)
+ })
+
+ it('opens no tour when the setup chat finished in the background', async () => {
+ $selectedStoredSessionId.set('other-chat')
+
+ finishGuidedOnboarding(SETUP, TASK)
+ await vi.advanceTimersByTimeAsync(10_000)
+
+ expect($tourActive.get()).toBe(false)
+ })
+
+ it('opens no tour when the user moved to another chat before the handoff opened', async () => {
+ finishGuidedOnboarding(SETUP, TASK)
+ $selectedStoredSessionId.set('other-chat')
+ await vi.advanceTimersByTimeAsync(10_000)
+
+ expect($tourActive.get()).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/components/onboarding-chat/intro.ts b/apps/desktop/src/components/onboarding-chat/intro.ts
index f346c54c7a7..37254efc7f4 100644
--- a/apps/desktop/src/components/onboarding-chat/intro.ts
+++ b/apps/desktop/src/components/onboarding-chat/intro.ts
@@ -18,6 +18,7 @@ import {
} from '@/store/profile'
import { $selectedStoredSessionId } from '@/store/session'
import { storedSessionIdForRuntimeId } from '@/store/session-states'
+import { isStartChatCallerWatched } from '@/store/start-chat'
import { retireTips } from '@/store/tips'
import { $toursEnabled } from '@/store/tours'
@@ -127,8 +128,8 @@ export function skipIntro(): void {
selectProfile(launch?.profile ?? 'default')
}
-/** A setup chat's `start_chat` started the task chat: the guided first run is complete. */
-export function finishGuidedOnboarding(runtimeId: string): void {
+/** A setup chat's `start_chat` started the task chat (`startedId`): the guided first run is complete. */
+export function finishGuidedOnboarding(runtimeId: string, startedId: string): void {
const threads = $chatOnboardingThreadIds.get()
const storedId = storedSessionIdForRuntimeId(runtimeId)
@@ -137,12 +138,15 @@ export function finishGuidedOnboarding(runtimeId: string): void {
}
const { phase } = $onboardingGate.get()
+ // The task chat opens itself only for a watched caller (start-chat-tool.tsx). A setup chat that
+ // finished in the background opens nothing, so it must not open a tour either.
+ const handoffOpens = isStartChatCallerWatched(storedId ?? runtimeId)
endIntroView()
completeGuide()
- if ((phase === 'guided' || phase === 'left') && $toursEnabled.get()) {
- void showHandoffTour()
+ if (handoffOpens && (phase === 'guided' || phase === 'left') && $toursEnabled.get()) {
+ void showHandoffTour(() => $selectedStoredSessionId.get() === startedId)
}
}
diff --git a/apps/desktop/src/components/onboarding-chat/signpost.ts b/apps/desktop/src/components/onboarding-chat/signpost.ts
index ede78c30469..1935f8cd0c1 100644
--- a/apps/desktop/src/components/onboarding-chat/signpost.ts
+++ b/apps/desktop/src/components/onboarding-chat/signpost.ts
@@ -12,11 +12,11 @@ const LOCAL_FIT_WAIT_MS = 1500
/** Waits for a visible node. The profile rail mounts a render or two after the handoff switches profiles, and
* the tour engine returns a no-match for a selector that is not in the DOM yet. Returns false on timeout. */
-async function waitFor(selector: string, timeoutMs = 6000): Promise<boolean> {
+async function waitFor(selector: string, timeoutMs = 6000, ready = () => true): Promise<boolean> {
const deadline = Date.now() + timeoutMs
while (Date.now() < deadline) {
- const visible = [...document.querySelectorAll(selector)].some(node => {
+ const visible = ready() && [...document.querySelectorAll(selector)].some(node => {
const { width, height } = node.getBoundingClientRect()
return width > 0 && height > 0 && !node.closest('[data-pane-hidden]')
@@ -32,9 +32,10 @@ async function waitFor(selector: string, timeoutMs = 6000): Promise<boolean> {
return false
}
-/** The caller does not await this, so the tour does not delay the handoff. */
-export async function showHandoffTour(): Promise<void> {
- if (!(await waitFor(RAIL))) {
+/** The caller does not await this, so the tour does not delay the handoff. `onHandoffChat` is true while the
+ * started task chat is the one on screen: the tour waits for it to open and drops if the user moves on. */
+export async function showHandoffTour(onHandoffChat: () => boolean): Promise<void> {
+ if (!(await waitFor(RAIL, 6000, onHandoffChat))) {
return
}
@@ -50,6 +51,10 @@ export async function showHandoffTour(): Promise<void> {
// (finishGuidedOnboarding), and run-tour.ts keeps driver.js and its stylesheet out of that path.
const { startTour } = await import('@/lib/tour')
+ if (!onHandoffChat()) {
+ return
+ }
+
await startTour([
{ accent: true, selector: RAIL, side: 'right', text: copy('profileText'), title: copy('profileTitle') },
...(sessionsVisible