Skip to content

The agent that grows with you

fix(desktop): a new chat opened from outside the composer shows its first message

alt-glitchcefd15747d38
diff --git a/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx b/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx
new file mode 100644
index 00000000000..ef5d104c4f8
--- /dev/null
+++ b/apps/desktop/src/app/session/hooks/new-session-first-message.test.tsx
@@ -0,0 +1,199 @@
+import { useStore } from '@nanostores/react'
+import { act, cleanup, render, waitFor } from '@testing-library/react'
+import { useEffect, useRef } from 'react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { getLatestSessionMessages } from '@/hermes'
+import type { ChatMessage } from '@/lib/chat-messages'
+import { requestGatewayForProfile } from '@/store/gateway'
+import {
+ $activeSessionId,
+ $messages,
+ $selectedStoredSessionId,
+ setActiveSessionId,
+ setAwaitingResponse,
+ setBusy,
+ setMessages,
+ setSelectedStoredSessionId,
+ setSessions
+} from '@/store/session'
+
+import { useSessionActions } from './use-session-actions'
+import { useSessionStateCache } from './use-session-state-cache'
+
+// The persisted transcript is HTTP; the first user row is not readable there yet when the route lands.
+vi.mock('@/hermes', async importOriginal => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ getLatestSessionMessages: vi.fn(),
+ getSession: vi.fn()
+}))
+
+// Profile-owned rows route session RPCs over a per-profile socket; the test routes them to its fake.
+vi.mock('@/store/gateway', async importOriginal => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ requestGatewayForProfile: vi.fn()
+}))
+
+vi.mock('@/store/profile', async importOriginal => ({
+ ...(await importOriginal<Record<string, unknown>>()),
+ ensureGatewayProfile: vi.fn().mockResolvedValue(undefined)
+}))
+
+const PROMPT = 'Plan my week.\n\nAbout me:\n- Call me Sid.'
+
+interface Ready {
+ messagesOf: (runtimeId: string) => readonly ChatMessage[]
+ resume: ReturnType<typeof useSessionActions>['resumeSession']
+ submitNew: ReturnType<typeof useSessionActions>['submitTextToNewSession']
+}
+
+// Real session cache + real view sync: the user row has to survive the route's warm resume.
+function Harness({
+ onReady,
+ requestGateway
+}: {
+ onReady: (ready: Ready) => void
+ requestGateway: <T>(method: string, params?: Record<string, unknown>) => Promise<T>
+}) {
+ const activeSessionId = useStore($activeSessionId)
+ const selectedStoredSessionId = useStore($selectedStoredSessionId)
+ const busyRef = useRef(false)
+
+ const cache = useSessionStateCache({
+ activeSessionId,
+ busyRef,
+ selectedStoredSessionId,
+ setAwaitingResponse,
+ setBusy,
+ setMessages
+ })
+
+ const actions = useSessionActions({
+ activeSessionId,
+ activeSessionIdRef: cache.activeSessionIdRef,
+ busyRef,
+ creatingSessionRef: useRef(false),
+ ensureSessionState: cache.ensureSessionState,
+ getRouteToken: () => 'new-session',
+ getRoutedStoredSessionId: () => null,
+ holdSessionTranscriptView: cache.holdSessionTranscriptView,
+ navigate: vi.fn() as never,
+ requestGateway,
+ routedSessionId: null,
+ resetViewSync: cache.resetViewSync,
+ runtimeIdByStoredSessionIdRef: cache.runtimeIdByStoredSessionIdRef,
+ selectedStoredSessionId,
+ selectedStoredSessionIdRef: cache.selectedStoredSessionIdRef,
+ sessionStateByRuntimeIdRef: cache.sessionStateByRuntimeIdRef,
+ syncSessionStateToView: cache.syncSessionStateToView,
+ updateSessionState: cache.updateSessionState
+ })
+
+ useEffect(() => {
+ onReady({
+ messagesOf: runtimeId => cache.sessionStateByRuntimeIdRef.current.get(runtimeId)?.messages ?? [],
+ resume: actions.resumeSession,
+ submitNew: actions.submitTextToNewSession
+ })
+ }, [actions.resumeSession, actions.submitTextToNewSession, cache, onReady])
+
+ return null
+}
+
+const userRows = (messages: readonly ChatMessage[]) => messages.filter(message => message.role === 'user')
+
+async function mount({ refuse = false } = {}) {
+ const requestGateway = vi.fn(async (method: string, _params?: Record<string, unknown>) => {
+ if (method === 'session.create') {
+ return { session_id: 'rt-new', stored_session_id: 'stored-new' } as never
+ }
+
+ if (method === 'prompt.submit' && refuse) {
+ throw new Error('refused')
+ }
+
+ if (method === 'session.activate') {
+ return {
+ info: {},
+ message_count: 0,
+ messages: [],
+ messages_omitted: true,
+ resumed: 'stored-new',
+ running: true,
+ session_id: 'rt-new',
+ session_key: 'stored-new'
+ } as never
+ }
+
+ return {} as never
+ })
+
+ let ready!: Ready
+ render(<Harness onReady={value => (ready = value)} requestGateway={requestGateway} />)
+ await waitFor(() => expect(ready).toBeDefined())
+ vi.mocked(requestGatewayForProfile).mockImplementation((_profile, method, params) => requestGateway(method, params))
+
+ return { ready, requestGateway }
+}
+
+describe('submitTextToNewSession first message', () => {
+ beforeEach(() => {
+ vi.mocked(getLatestSessionMessages).mockResolvedValue({ messages: [], session_id: 'stored-new' } as never)
+ })
+
+ afterEach(() => {
+ cleanup()
+ setActiveSessionId(null)
+ setSelectedStoredSessionId(null)
+ setMessages([])
+ setSessions([])
+ vi.restoreAllMocks()
+ })
+
+ it('shows the submitted text as the first user message once the route opens the new chat', async () => {
+ const { ready, requestGateway } = await mount()
+
+ await act(async () => {
+ await ready.submitNew(PROMPT)
+ })
+
+ // The navigate to #/stored-new lands here: use-route-resume resumes the routed id.
+ await act(async () => {
+ await ready.resume('stored-new', true)
+ })
+
+ await waitFor(() => expect(userRows($messages.get())).toHaveLength(1))
+ expect($activeSessionId.get()).toBe('rt-new')
+ expect(userRows($messages.get())[0]?.parts).toEqual([expect.objectContaining({ text: PROMPT, type: 'text' })])
+ expect(requestGateway).toHaveBeenCalledWith('prompt.submit', { session_id: 'rt-new', text: PROMPT })
+ })
+
+ it('keeps one first message when the persisted transcript already has it', async () => {
+ vi.mocked(getLatestSessionMessages).mockResolvedValue({
+ messages: [{ content: PROMPT, role: 'user', timestamp: 1 }],
+ session_id: 'stored-new'
+ } as never)
+ const { ready } = await mount()
+
+ await act(async () => {
+ await ready.submitNew(PROMPT)
+ })
+
+ await act(async () => {
+ await ready.resume('stored-new', true)
+ })
+
+ await waitFor(() => expect(getLatestSessionMessages).toHaveBeenCalled())
+ await waitFor(() => expect(userRows($messages.get())).toHaveLength(1))
+ })
+
+ it('drops the first message when the prompt is refused', async () => {
+ const { ready } = await mount({ refuse: true })
+
+ await act(async () => {
+ await expect(ready.submitNew(PROMPT)).rejects.toThrow('refused')
+ })
+
+ expect(userRows(ready.messagesOf('rt-new'))).toHaveLength(0)
+ })
+})
diff --git a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
index ea65c313586..c3ca74f12d9 100644
--- a/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
+++ b/apps/desktop/src/app/session/hooks/use-session-actions/index.ts
@@ -26,6 +26,7 @@ import {
restorePendingClarifyToolCall,
settlePendingClarifyToolCall,
stripPendingClarifyProjectionForCache,
+ textPart,
toChatMessages
} from '@/lib/chat-messages'
import { markReasoningEffortPending } from '@/lib/chat-runtime'
@@ -980,9 +981,46 @@ export function useSessionActions({
runtimeIdByStoredSessionIdRef.current.set(stored, created.session_id)
ensureSessionState(created.session_id, stored)
upsertOptimisticSession(created, stored, null, text.trim())
+ // The route's warm resume paints this cached state as-is, so the first
+ // user message has to be in it, as a composer send seeds it.
+ const firstMessageId = `user-${created.session_id}`
+ updateSessionState(
+ created.session_id,
+ state => ({
+ ...state,
+ messages: [
+ ...state.messages,
+ {
+ id: firstMessageId,
+ role: 'user',
+ parts: [textPart(text.trim())],
+ timestamp: Date.now() / 1000
+ }
+ ],
+ awaitingResponse: true,
+ busy: true,
+ turnStartedAt: state.turnStartedAt ?? Date.now()
+ }),
+ stored
+ )
// Submit the exact runtime id returned by session.create so this
// atomic path cannot fall back to a route token (#85590).
- await requestGateway('prompt.submit', { session_id: created.session_id, text })
+ await requestGateway('prompt.submit', { session_id: created.session_id, text }).catch((error: unknown) => {
+ // A refused prompt leaves no turn behind, so its bubble and spinner go too.
+ updateSessionState(
+ created.session_id,
+ state => ({
+ ...state,
+ messages: state.messages.filter(message => message.id !== firstMessageId),
+ awaitingResponse: false,
+ busy: false,
+ turnStartedAt: null
+ }),
+ stored
+ )
+
+ throw error
+ })
navigate(sessionRoute(stored), { replace: true })
return { runtimeSessionId: created.session_id, sessionId: stored }
@@ -999,7 +1037,8 @@ export function useSessionActions({
navigate,
requestGateway,
runtimeIdByStoredSessionIdRef,
- selectedStoredSessionIdRef
+ selectedStoredSessionIdRef,
+ updateSessionState
]
)