The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a window saves a draft back only when it edited it, on every save path
Teknium2406af9920ac
diff --git a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.persist-window.test.tsx b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.persist-window.test.tsxindex fbbdda91925..87503d91c3d 100644--- a/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.persist-window.test.tsx+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.persist-window.test.tsx@@ -40,19 +40,21 @@ vi.mock('@assistant-ui/react', () => ({useAui: () => ({ composer: () => runtime.composer }),useAuiState: (selector: (state: { composer: { text: string } }) => unknown) =>selector({ composer: runtime.getState() }),- useComposerRuntime: () => ({ getState: runtime.getState, subscribe: runtime.subscribe })+ useComposerRuntime: () => runtime}))const SCOPE = 'session-persist-window'+const OTHER_SCOPE = 'session-persist-window-other'let draft: ReturnType<typeof useComposerDraft>+const queueEditRef = { current: null as QueueEditState | null }-function Harness() {+function Harness({ scope = SCOPE }: { scope?: string }) {draft = useComposerDraft({- activeQueueSessionKey: SCOPE,+ activeQueueSessionKey: scope,focusKey: null,inputDisabled: false,- queueEditRef: { current: null as QueueEditState | null },- sessionId: SCOPE+ queueEditRef,+ sessionId: scope})return (@@ -80,6 +82,7 @@ describe('useComposerDraft — the draft stash window starts at the keystroke',vi.useRealTimers()mainComposerScope.clear()clearSessionDraft(SCOPE)+ clearSessionDraft(OTHER_SCOPE)})it('stashes typed text one persist debounce after the keystroke, while its store write still coalesces', () => {@@ -133,4 +136,27 @@ describe('useComposerDraft — the draft stash window starts at the keystroke',expect(takeSessionDraft(SCOPE).text).toBe('')})++ it('writes back neither by its delayed save nor on a session switch a draft it never edited', () => {+ const { rerender } = render(<Harness scope={OTHER_SCOPE} />)+ stashSessionDraft(SCOPE, 'sent from the other window', [])+ rerender(<Harness scope={SCOPE} />)++ // The other window sends it inside this window's post-load save delay.+ clearSessionDraft(SCOPE)+ act(() => {+ vi.advanceTimersByTime(DRAFT_PERSIST_DEBOUNCE_MS)+ })+ expect(takeSessionDraft(SCOPE).text).toBe('')++ rerender(<Harness scope={OTHER_SCOPE} />)+ expect(takeSessionDraft(SCOPE).text).toBe('')++ // An attachment-only edit is this window's own: the switch still saves it.+ act(() => {+ mainComposerScope.$attachments.set([{ id: 'notes.md', kind: 'file', label: 'notes.md', occurrenceId: 'occ-1' }])+ })+ rerender(<Harness scope={SCOPE} />)+ expect(takeSessionDraft(OTHER_SCOPE).attachments.map(a => a.occurrenceId)).toEqual(['occ-1'])+ })})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 db9f0b61b4d..52b0b712c76 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@@ -62,6 +62,24 @@ import { useComposerScope } from '../scope'import type { ChatBarProps } from '../types'import { useComposerVisible } from '../visibility'+/** A draft as this window last loaded or stashed it; attachments by occurrence. */+interface SavedDraft {+ attachments: string[]+ text: string+}++const attachmentOccurrence = (attachment: ComposerAttachment) => attachment.occurrenceId ?? attachment.id++const savedDraft = (text: string, attachments: ComposerAttachment[]): SavedDraft => ({+ attachments: attachments.map(attachmentOccurrence),+ text+})++const isSavedDraft = (saved: SavedDraft, text: string, attachments: ComposerAttachment[]) =>+ text === saved.text &&+ attachments.length === saved.attachments.length &&+ attachments.every((attachment, index) => attachmentOccurrence(attachment) === saved.attachments[index])+interface UseComposerDraftArgs {activeQueueSessionKey: string | nullfocusKey: ChatBarProps['focusKey']@@ -143,10 +161,11 @@ export function useComposerDraft({// session switch mid-flight files one session's draft under another's key// (#54527).const draftScopeRef = useRef(activeQueueSessionKey)- // The text this window last loaded from or stashed for draftScopeRef's scope.- // Another window can send (clear) that draft while this editor still shows- // it, so the unload flush saves only text that differs from this baseline.- const savedDraftTextRef = useRef('')+ // The draft (text + attachment occurrences) this window last loaded from or+ // stashed for draftScopeRef's scope. Another window can send (clear) or+ // rewrite that draft while this editor still shows it, so every save path+ // stashes only state that differs from this baseline: this window's own edits.+ const savedDraftRef = useRef<SavedDraft>({ attachments: [], text: '' })const sessionIdRef = useRef(sessionId)sessionIdRef.current = sessionIdconst queueEditStateRef = useRef<QueueEditState | null>(queueEditRef.current)@@ -324,12 +343,23 @@ export function useComposerDraft({const stashAt = (scope: string | null, text = draftRef.current, attachments = attachmentScope.$attachments.get()) => {if (sameDraftScope(scope, draftScopeRef.current)) {- savedDraftTextRef.current = text+ savedDraftRef.current = savedDraft(text, attachments)}stashSessionDraft(scope, text, attachments)}+ // The automatic saves (debounce, session switch, HUD flush): a draft this+ // window has not edited since it loaded or saved it is left to the stash,+ // which may hold a newer value (or none, once sent) from another window.+ const stashOwnEdits = (scope: string | null, text: string, attachments = attachmentScope.$attachments.get()) => {+ if (sameDraftScope(scope, draftScopeRef.current) && isSavedDraft(savedDraftRef.current, text, attachments)) {+ return+ }++ stashAt(scope, text, attachments)+ }+// A rejected or unconfirmed send is restored with its identity, so Enter on that exact draft is// the explicit retry. Recorded per draft scope (it survives a reload with the draft text).const rememberDraftRetry = (scope: string | null, retry: ComposerDraftRetry) => {@@ -479,7 +509,7 @@ export function useComposerDraft({}pendingDraftPersistRef.current = null- stashAt(scope, text)+ stashOwnEdits(scope, text)}, DRAFT_PERSIST_DEBOUNCE_MS)}@@ -660,7 +690,7 @@ export function useComposerDraft({draftRetryRef.current = readDraftRetry(activeQueueSessionKey)const { attachments, text } = takeSessionDraft(activeQueueSessionKey)- savedDraftTextRef.current = text+ savedDraftRef.current = savedDraft(text, attachments)loadIntoComposer(text, attachments, true)return () => {@@ -668,9 +698,9 @@ export function useComposerDraft({const editing = queueEditStateRef.currentif (editing?.sessionKey === activeQueueSessionKey) {- stashAt(activeQueueSessionKey, editing.draft, editing.attachments)+ stashOwnEdits(activeQueueSessionKey, editing.draft, editing.attachments)} else if (!isBrowsingHistory(sessionId)) {- stashAt(activeQueueSessionKey, latestText)+ stashOwnEdits(activeQueueSessionKey, latestText)}}}, [activeQueueSessionKey]) // eslint-disable-line react-hooks/exhaustive-deps@@ -684,7 +714,7 @@ export function useComposerDraft({if (mode === 'flush') {window.clearTimeout(draftPersistTimerRef.current)pendingDraftPersistRef.current = null- stashAt(draftScopeRef.current, syncDraftFromEditor())+ stashOwnEdits(draftScopeRef.current, syncDraftFromEditor())return}@@ -692,7 +722,7 @@ export function useComposerDraft({reloadPersistedDrafts()draftRetryRef.current = readDraftRetry(draftScopeRef.current)const stashed = takeSessionDraft(draftScopeRef.current)- savedDraftTextRef.current = stashed.text+ savedDraftRef.current = savedDraft(stashed.text, stashed.attachments)loadIntoComposer(stashed.text, stashed.attachments)}@@ -729,7 +759,8 @@ export function useComposerDraft({// Only this window's own unsaved edits: an untouched editor may still// show a draft another window has since sent, and must not restore it.- if (latestText === savedDraftTextRef.current) {+ // Text alone: attachments are memory-only and close with the window.+ if (latestText === savedDraftRef.current.text) {return}