The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): a window's close saves only its own unsaved draft edits
Tekniume3d5e8c5591e
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 5d428616bf3..fbbdda91925 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@@ -119,4 +119,18 @@ describe('useComposerDraft — the draft stash window starts at the keystroke',expect(takeSessionDraft(SCOPE).text).toBe('hello closing')})++ it('does not write a draft another window sent back when this untouched window closes', () => {+ stashSessionDraft(SCOPE, 'sent from the other window', [])+ render(<Harness />)+ act(() => {+ vi.advanceTimersByTime(DRAFT_PERSIST_DEBOUNCE_MS)+ })++ // The other window sends it: the shared stash clears, this editor still shows the old text.+ clearSessionDraft(SCOPE)+ window.dispatchEvent(new Event('beforeunload'))++ expect(takeSessionDraft(SCOPE).text).toBe('')+ })})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 5cfe4b7dd90..db9f0b61b4d 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@@ -143,6 +143,10 @@ 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('')const sessionIdRef = useRef(sessionId)sessionIdRef.current = sessionIdconst queueEditStateRef = useRef<QueueEditState | null>(queueEditRef.current)@@ -315,12 +319,17 @@ export function useComposerDraft({}}, [appendExternalText, focusInput, inputDisabled, paintDraft, target])- const stashAt = (scope: string | null, text = draftRef.current, attachments = attachmentScope.$attachments.get()) =>- stashSessionDraft(scope, text, attachments)-const sameDraftScope = (a: string | null | undefined, b: string | null | undefined) =>(isFreshDraftScope(a) || !a ? null : a) === (isFreshDraftScope(b) || !b ? null : b)+ const stashAt = (scope: string | null, text = draftRef.current, attachments = attachmentScope.$attachments.get()) => {+ if (sameDraftScope(scope, draftScopeRef.current)) {+ savedDraftTextRef.current = text+ }++ stashSessionDraft(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) => {@@ -651,6 +660,7 @@ export function useComposerDraft({draftRetryRef.current = readDraftRetry(activeQueueSessionKey)const { attachments, text } = takeSessionDraft(activeQueueSessionKey)+ savedDraftTextRef.current = textloadIntoComposer(text, attachments, true)return () => {@@ -682,6 +692,7 @@ export function useComposerDraft({reloadPersistedDrafts()draftRetryRef.current = readDraftRetry(draftScopeRef.current)const stashed = takeSessionDraft(draftScopeRef.current)+ savedDraftTextRef.current = stashed.textloadIntoComposer(stashed.text, stashed.attachments)}@@ -715,6 +726,13 @@ export function useComposerDraft({}const latestText = syncDraftFromEditor()++ // 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) {+ return+ }+pendingDraftPersistRef.current = nullstashAt(scope, latestText)}