The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): migrate pre-session composer drafts
KoNit-Kec0f840db8a6
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsxindex 853faa0a512..5bee360192b 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.test.tsx@@ -3,7 +3,7 @@ import { useLayoutEffect } from 'react'import { afterEach, describe, expect, it, vi } from 'vitest'import { PaneVisibleContext } from '@/components/pane-shell/pane-visibility'-import { clearSessionDraft, type ComposerAttachment, mainComposerScope, stashSessionDraft } from '@/store/composer'+import { clearSessionDraft, type ComposerAttachment, mainComposerScope, stashSessionDraft, takeSessionDraft } from '@/store/composer'import { $connection } from '@/store/session'import { useComposerActions } from '../../hooks/use-composer-actions'@@ -94,6 +94,31 @@ describe('useComposerDraft — attachment scope stays coherent with the committeexpect(snapshots[0]).toEqual([])})+ it('moves a new-chat draft into the assigned session when its id arrives (#114122)', () => {+ const preSessionAttachment: ComposerAttachment = { id: 'file:new', kind: 'file', label: 'new.txt' }+ stashSessionDraft(null, 'do not lose this draft', [preSessionAttachment])++ const { rerender } = render(+ <ProbeHarness activeQueueSessionKey={null} onLayoutSnapshot={() => undefined} sessionId="" />+ )++ act(() => {+ rerender(+ <ProbeHarness+ activeQueueSessionKey="session-created"+ onLayoutSnapshot={() => undefined}+ sessionId="session-created"+ />+ )+ })++ expect(takeSessionDraft('session-created')).toEqual({+ attachments: [preSessionAttachment],+ text: 'do not lose this draft'+ })+ expect(takeSessionDraft(null)).toEqual({ attachments: [], text: '' })+ })+it('applies a delayed image preview when it resolves while its attachment draft is inactive', async () => {const fullResolution ='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+GkZcAAAAASUVORK5CYII='diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.tsindex 772f5e5c8e9..fb693387901 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.ts@@ -16,6 +16,7 @@ import { useStoreSelector } from '@/lib/use-session-slice'import {type ComposerAttachment,type ComposerDraftSyncMode,+ migrateSessionDraft,onComposerDraftSyncRequest,reloadPersistedDrafts,stashSessionDraft,@@ -127,6 +128,9 @@ export function useComposerDraft({const draftScopeRef = useRef(activeQueueSessionKey)const sessionIdRef = useRef(sessionId)sessionIdRef.current = sessionId+ // Updated by the draft-swap layout effect so it remains the previous committed+ // id during the render where a new chat receives its first session id.+ const committedSessionIdRef = useRef(sessionId)const queueEditStateRef = useRef<QueueEditState | null>(queueEditRef.current)queueEditStateRef.current = queueEditRef.current@@ -453,7 +457,19 @@ export function useComposerDraft({// fire later would just clobber with an older snapshot.window.clearTimeout(draftPersistTimerRef.current)pendingDraftPersistRef.current = null+ const previousDraftScope = draftScopeRef.current+ const previousSessionId = committedSessionIdRef.current++ // A new chat writes to the shared pre-session bucket until its first+ // runtime session id arrives. Move that draft at this handoff, before the+ // incoming scope is restored. Do not consume the bucket on an ordinary+ // initial mount of an existing session or on a cross-session switch.+ if (!previousSessionId && sessionId && !previousDraftScope && activeQueueSessionKey) {+ migrateSessionDraft(previousDraftScope, activeQueueSessionKey)+ }+draftScopeRef.current = activeQueueSessionKey+ committedSessionIdRef.current = sessionIdconst { attachments, text } = takeSessionDraft(activeQueueSessionKey)loadIntoComposer(text, attachments)diff --git a/apps/desktop/src/store/composer.test.ts b/apps/desktop/src/store/composer.test.tsindex 0fc034b1d9b..db389b09a68 100644--- a/apps/desktop/src/store/composer.test.ts+++ b/apps/desktop/src/store/composer.test.ts@@ -280,15 +280,30 @@ describe('session drafts', () => {clearSessionDraft(tipAfter)})- it('does not overwrite a non-empty destination draft during migration', () => {- stashSessionDraft('from', 'old tip draft', [])- stashSessionDraft('to', 'already typed on new tip', [])+ it('migrates a pre-session draft onto its assigned session key', () => {+ stashSessionDraft(null, 'typed before the session existed', [attachment({ id: 'file:new' })])- expect(migrateSessionDraft('from', 'to')).toBe(false)+ expect(migrateSessionDraft(null, 'session-created')).toBe(true)+ expect(takeSessionDraft('session-created')).toEqual({+ attachments: [attachment({ id: 'file:new' })],+ text: 'typed before the session existed'+ })+ expect(takeSessionDraft(null)).toEqual({ attachments: [], text: '' })++ clearSessionDraft('session-created')+ })++ it('does not overwrite a destination draft or its attachments during migration', () => {+ const destinationAttachment = attachment({ id: 'file:destination' })+ stashSessionDraft(null, 'new chat draft', [attachment({ id: 'file:source' })])+ stashSessionDraft('to', 'already typed on new tip', [destinationAttachment])++ expect(migrateSessionDraft(null, 'to')).toBe(false)expect(takeSessionDraft('to').text).toBe('already typed on new tip')- expect(takeSessionDraft('from').text).toBe('old tip draft')+ expect(takeSessionDraft('to').attachments).toEqual([destinationAttachment])+ expect(takeSessionDraft(null).text).toBe('new chat draft')- clearSessionDraft('from')+ clearSessionDraft(null)clearSessionDraft('to')})})diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.tsindex 3d7d96b99d9..0981d4bc310 100644--- a/apps/desktop/src/store/composer.ts+++ b/apps/desktop/src/store/composer.ts@@ -408,15 +408,16 @@ export const clearSessionDraft = (scope: string | null | undefined) => stashSess** Auto-compression rotates the live stored tip id (root → continuation) while* the user may still be typing. Drafts keyed on the obsolete tip would otherwise- * vanish from the composer when selection follows the new tip. No-op unless both- * keys resolve, differ, and the source has content. Does not overwrite a+ * vanish from the composer when selection follows the new tip. A new chat is+ * stored under the pre-session key until its first session id arrives. No-op+ * unless the destination resolves, the keys differ, and the source has content. Does not overwrite a* non-empty destination draft.*/export function migrateSessionDraft(fromKey: string | null | undefined, toKey: string | null | undefined): boolean {const from = draftKey(fromKey)const to = draftKey(toKey)- if (!fromKey || !toKey || from === to) {+ if (!toKey?.trim() || from === to) {return false}