Skip to content

The agent that grows with you

fix(desktop): save the composer draft on the keystroke's clock and on window close

Tekniuma950eff764e1
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.tsx
new file mode 100644
index 00000000000..5d428616bf3
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/hooks/use-composer-draft.persist-window.test.tsx
@@ -0,0 +1,122 @@
+import { act, cleanup, render } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { clearSessionDraft, mainComposerScope, stashSessionDraft, takeSessionDraft } from '@/store/composer'
+
+import { TYPING_STORE_IDLE_MS } from '../composer-store-writer'
+import { DRAFT_PERSIST_DEBOUNCE_MS, type QueueEditState } from '../composer-utils'
+import { RICH_INPUT_SLOT } from '../rich-editor'
+
+import { useComposerDraft } from './use-composer-draft'
+
+// A composer runtime that behaves like assistant-ui's: setText updates the
+// state and notifies subscribers synchronously.
+const runtime = vi.hoisted(() => {
+ let text = ''
+ const listeners = new Set<() => void>()
+
+ const setText = vi.fn((next: string) => {
+ text = next
+ listeners.forEach(listener => listener())
+ })
+
+ return {
+ composer: { setText },
+ getState: () => ({ text }),
+ reset: () => {
+ text = ''
+ listeners.clear()
+ setText.mockClear()
+ },
+ subscribe: (listener: () => void) => {
+ listeners.add(listener)
+
+ return () => listeners.delete(listener)
+ }
+ }
+})
+
+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 })
+}))
+
+const SCOPE = 'session-persist-window'
+let draft: ReturnType<typeof useComposerDraft>
+
+function Harness() {
+ draft = useComposerDraft({
+ activeQueueSessionKey: SCOPE,
+ focusKey: null,
+ inputDisabled: false,
+ queueEditRef: { current: null as QueueEditState | null },
+ sessionId: SCOPE
+ })
+
+ return (
+ <div
+ contentEditable
+ data-slot={RICH_INPUT_SLOT}
+ data-testid="editor"
+ ref={draft.editorRef}
+ suppressContentEditableWarning
+ />
+ )
+}
+
+const editorText = (editor: HTMLElement) =>
+ editor.ownerDocument.createTreeWalker(editor, NodeFilter.SHOW_TEXT).nextNode() as Text
+
+describe('useComposerDraft — the draft stash window starts at the keystroke', () => {
+ beforeEach(() => {
+ vi.useFakeTimers()
+ runtime.reset()
+ })
+
+ afterEach(() => {
+ cleanup()
+ vi.useRealTimers()
+ mainComposerScope.clear()
+ clearSessionDraft(SCOPE)
+ })
+
+ it('stashes typed text one persist debounce after the keystroke, while its store write still coalesces', () => {
+ stashSessionDraft(SCOPE, 'hello', [])
+ const { getByTestId } = render(<Harness />)
+ const editor = getByTestId('editor')
+
+ // A keystroke that flips no composer-text edge: the store write waits for a typing pause.
+ const typed = editorText(editor)
+ typed.appendData(' draft')
+ act(() => {
+ draft.syncDraftFromEditor(true)
+ })
+ expect(runtime.composer.setText).not.toHaveBeenCalledWith('hello draft')
+
+ act(() => {
+ vi.advanceTimersByTime(TYPING_STORE_IDLE_MS)
+ })
+ expect(runtime.composer.setText).toHaveBeenLastCalledWith('hello draft')
+
+ // Closing the window right after the debounce must find the draft saved;
+ // the coalesced store write landing later does not restart the window.
+ act(() => {
+ vi.advanceTimersByTime(DRAFT_PERSIST_DEBOUNCE_MS - TYPING_STORE_IDLE_MS)
+ })
+ expect(takeSessionDraft(SCOPE).text).toBe('hello draft')
+ })
+
+ it('stashes the latest keystroke synchronously when the window starts to close', () => {
+ stashSessionDraft(SCOPE, 'hello', [])
+ const { getByTestId } = render(<Harness />)
+ const typed = editorText(getByTestId('editor'))
+
+ // Typed, and not even the rAF flush has run: only the DOM has the text.
+ typed.appendData(' closing')
+ window.dispatchEvent(new Event('beforeunload'))
+
+ expect(takeSessionDraft(SCOPE).text).toBe('hello closing')
+ })
+})
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.ts
index 84c26184595..5cfe4b7dd90 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
@@ -126,7 +126,6 @@ export function useComposerDraft({
const setComposerText = useCallback((value: string) => storeWriter.now(value), [storeWriter])
-
useEffect(() => () => storeWriter.cancel(), [storeWriter])
const editorRef = useRef<HTMLDivElement | null>(null)
@@ -433,6 +432,51 @@ export function useComposerDraft({
}
}, [setComposerText, target])
+ // Schedule the debounced per-session stash of `text`. Browsing history /
+ // editing a queued prompt suppress it so recalled text never clobbers the
+ // draft. A text already scheduled for this scope keeps its timer: typed text
+ // is scheduled at the keystroke, and the coalesced store write that lands it
+ // later must not restart the window (a window closed inside it loses the draft).
+ const scheduleDraftPersist = (text: string) => {
+ if (isBrowsingHistory(sessionIdRef.current) || queueEditRef.current) {
+ return
+ }
+
+ const scope = draftScopeRef.current
+
+ // An edited (or cleared) restored draft is a new message: it no longer retries the old send.
+ if (draftRetryRef.current && !draftRetryTextMatches(draftRetryRef.current, text)) {
+ draftRetryRef.current = undefined
+ writeDraftRetry(scope, undefined)
+ }
+
+ const pending = pendingDraftPersistRef.current
+
+ if (pending && pending.scope === scope && pending.text === text) {
+ return
+ }
+
+ const entry = { scope, text }
+ pendingDraftPersistRef.current = entry
+ window.clearTimeout(draftPersistTimerRef.current)
+ draftPersistTimerRef.current = window.setTimeout(() => {
+ // Integrity guard (defense-in-depth, #54527): only commit if this is
+ // still the pending write on file. A session swap or a newer
+ // keystroke clears/replaces it before firing in the normal case; this
+ // catches any future call site that skips that bookkeeping instead of
+ // silently filing text under the wrong session.
+ if (!isPendingDraftPersistCurrent(pendingDraftPersistRef.current, entry)) {
+ return
+ }
+
+ pendingDraftPersistRef.current = null
+ stashAt(scope, text)
+ }, DRAFT_PERSIST_DEBOUNCE_MS)
+ }
+
+ const scheduleDraftPersistRef = useRef(scheduleDraftPersist)
+ scheduleDraftPersistRef.current = scheduleDraftPersist
+
// Read the editor's current plain text into draftRef + composer state. This
// closes the "queued rAF flush hasn't run yet" window so scope-swap/pagehide
// persistence captures the latest keystrokes. `typing` marks ChatBar's
@@ -457,6 +501,12 @@ export function useComposerDraft({
if (text !== draftRef.current) {
draftRef.current = text
storeWriter[typing ? 'typing' : 'now'](text)
+
+ // The runtime subscription schedules the stash once the store sees
+ // the text; a deferred typing write must not delay it past the keystroke.
+ if (storeWriter.pending()) {
+ scheduleDraftPersistRef.current(text)
+ }
} else if (!typing) {
storeWriter.flush()
}
@@ -473,8 +523,6 @@ export function useComposerDraft({
// text into draftRef, (2) repaint the editor only when the change came from
// OUTSIDE it (programmatic clear/restore/insert; the focused editor is the
// source otherwise), and (3) schedule the debounced per-session stash.
- // Browsing history / editing a queued prompt suppress the stash so recalled
- // text never clobbers the draft.
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
useEffect(() => {
const sync = () => {
@@ -495,34 +543,7 @@ export function useComposerDraft({
renderComposerContents(editor, text, { trailingCommitted: true })
}
- if (isBrowsingHistory(sessionIdRef.current) || queueEditRef.current) {
- return
- }
-
- const scope = draftScopeRef.current
-
- // An edited (or cleared) restored draft is a new message: it no longer retries the old send.
- if (draftRetryRef.current && !draftRetryTextMatches(draftRetryRef.current, text)) {
- draftRetryRef.current = undefined
- writeDraftRetry(scope, undefined)
- }
-
- const entry = { scope, text }
- pendingDraftPersistRef.current = entry
- window.clearTimeout(draftPersistTimerRef.current)
- draftPersistTimerRef.current = window.setTimeout(() => {
- // Integrity guard (defense-in-depth, #54527): only commit if this is
- // still the pending write on file. A session swap or a newer
- // keystroke clears/replaces it before firing in the normal case; this
- // catches any future call site that skips that bookkeeping instead of
- // silently filing text under the wrong session.
- if (!isPendingDraftPersistCurrent(pendingDraftPersistRef.current, entry)) {
- return
- }
-
- pendingDraftPersistRef.current = null
- stashAt(scope, text)
- }, DRAFT_PERSIST_DEBOUNCE_MS)
+ scheduleDraftPersistRef.current(text)
}
const unsubscribe = composerRuntime.subscribe(sync)
@@ -679,6 +700,10 @@ export function useComposerDraft({
// pagehide is load-bearing: React skips effect cleanups on reload, so Cmd+R
// inside the debounce/rAF window would drop trailing keystrokes without this.
+ // beforeunload covers closing the window: Electron tears the renderer down
+ // before a pagehide-time localStorage write lands (live: a close right after
+ // the last keystroke kept the beforeunload write and lost the pagehide one).
+ // The handler never cancels the unload.
// eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
useEffect(() => {
const flushPendingDraftPersist = () => {
@@ -694,9 +719,11 @@ export function useComposerDraft({
stashAt(scope, latestText)
}
+ window.addEventListener('beforeunload', flushPendingDraftPersist)
window.addEventListener('pagehide', flushPendingDraftPersist)
return () => {
+ window.removeEventListener('beforeunload', flushPendingDraftPersist)
window.removeEventListener('pagehide', flushPendingDraftPersist)
flushPendingDraftPersist()
}