Skip to content

The agent that grows with you

fix(desktop): another window's draft save no longer deletes this window's image-only draft

Teknium3a818997d7e9
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
index 87503d91c3d..69c48116fc4 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
@@ -1,7 +1,13 @@
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 {
+ clearSessionDraft,
+ mainComposerScope,
+ SESSION_DRAFTS_STORAGE_KEY,
+ stashSessionDraft,
+ takeSessionDraft
+} from '@/store/composer'
import { TYPING_STORE_IDLE_MS } from '../composer-store-writer'
import { DRAFT_PERSIST_DEBOUNCE_MS, type QueueEditState } from '../composer-utils'
@@ -45,6 +51,7 @@ vi.mock('@assistant-ui/react', () => ({
const SCOPE = 'session-persist-window'
const OTHER_SCOPE = 'session-persist-window-other'
+const THIRD_SCOPE = 'session-persist-window-third'
let draft: ReturnType<typeof useComposerDraft>
const queueEditRef = { current: null as QueueEditState | null }
@@ -83,6 +90,7 @@ describe('useComposerDraft — the draft stash window starts at the keystroke',
mainComposerScope.clear()
clearSessionDraft(SCOPE)
clearSessionDraft(OTHER_SCOPE)
+ clearSessionDraft(THIRD_SCOPE)
})
it('stashes typed text one persist debounce after the keystroke, while its store write still coalesces', () => {
@@ -159,4 +167,23 @@ describe('useComposerDraft — the draft stash window starts at the keystroke',
rerender(<Harness scope={SCOPE} />)
expect(takeSessionDraft(OTHER_SCOPE).attachments.map(a => a.occurrenceId)).toEqual(['occ-1'])
})
+
+ it("keeps this window's image-only draft when another window saves or sends a different draft", () => {
+ stashSessionDraft(OTHER_SCOPE, 'sent from the other window', [])
+ const { rerender } = render(<Harness scope={SCOPE} />)
+ act(() => {
+ mainComposerScope.$attachments.set([{ id: 'shot.png', kind: 'image', label: 'shot.png', occurrenceId: 'occ-img' }])
+ })
+ rerender(<Harness scope={OTHER_SCOPE} />)
+
+ // The other window sends OTHER_SCOPE's draft and saves one in a third session.
+ window.localStorage.setItem(SESSION_DRAFTS_STORAGE_KEY, JSON.stringify({ [THIRD_SCOPE]: 'typed elsewhere' }))
+ window.dispatchEvent(new StorageEvent('storage', { key: SESSION_DRAFTS_STORAGE_KEY }))
+ expect(takeSessionDraft(THIRD_SCOPE).text).toBe('typed elsewhere')
+ expect(takeSessionDraft(OTHER_SCOPE).text).toBe('')
+
+ // No window ever saw the image-only draft, so none of them could have sent it.
+ rerender(<Harness scope={SCOPE} />)
+ expect(mainComposerScope.$attachments.get().map(a => a.occurrenceId)).toEqual(['occ-img'])
+ })
})
diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.ts
index f2a312f380d..da4a9521f36 100644
--- a/apps/desktop/src/store/composer.ts
+++ b/apps/desktop/src/store/composer.ts
@@ -302,24 +302,25 @@ const cloneDraft = (draft: SessionDraft): SessionDraft => ({
text: draft.text
})
-function loadPersistedDraftTexts(): [string, SessionDraft][] {
+function loadPersistedDraftTexts(): Map<string, string> {
try {
const raw = window.localStorage.getItem(SESSION_DRAFTS_STORAGE_KEY)
- if (!raw) {
- return []
- }
-
- return Object.entries(JSON.parse(raw) as Record<string, string>).map(([key, text]) => [
- key,
- { attachments: [], text }
- ])
+ return new Map(raw ? Object.entries(JSON.parse(raw) as Record<string, string>) : [])
} catch {
- return []
+ return new Map()
}
}
-const draftsBySession = new Map<string, SessionDraft>(loadPersistedDraftTexts())
+// The draft texts as this window last read or wrote them in the shared stash.
+// A cross-window reload applies only what changed there since: a key absent
+// from both snapshots was never persisted (an attachment-only draft, one past
+// MAX_PERSISTED_DRAFTS), so nobody deleted it.
+let persistedDraftTexts = loadPersistedDraftTexts()
+
+const draftsBySession = new Map<string, SessionDraft>(
+ [...persistedDraftTexts].map(([key, text]) => [key, { attachments: [], text }])
+)
/**
* Patch one asynchronous attachment occurrence wherever the main composer owns
@@ -403,25 +404,40 @@ function publishDraftTitle(key: string, title: string): void {
* whichever window mounted first keeps its stale copy forever, so text typed
* in the HUD is simply gone when you return to the app.
*
- * Merge, don't clobber — the local map may hold attachments (never persisted)
- * that the incoming text-only snapshot can't know about.
+ * Merge, don't clobber: only texts the stash changed since this window last
+ * saw it move, and attachments (memory-only, so no other window can have seen
+ * or sent them) stay put. An image-only draft is still a draft.
*/
export function reloadPersistedDrafts(): void {
- const incoming = new Map(loadPersistedDraftTexts())
+ const incoming = loadPersistedDraftTexts()
+
+ for (const [key, text] of incoming) {
+ if (persistedDraftTexts.get(key) === text) {
+ continue
+ }
- for (const [key, draft] of incoming) {
- const local = draftsBySession.get(key)
- draftsBySession.set(key, local?.attachments.length ? { ...local, text: draft.text } : draft)
- publishDraftTitle(key, deriveDraftTitle(draft.text))
+ draftsBySession.set(key, { attachments: draftsBySession.get(key)?.attachments ?? [], text })
+ publishDraftTitle(key, deriveDraftTitle(text))
}
// A key that vanished from storage was cleared (sent) in the other window.
- for (const key of [...draftsBySession.keys()]) {
- if (!incoming.has(key)) {
+ for (const key of persistedDraftTexts.keys()) {
+ if (incoming.has(key)) {
+ continue
+ }
+
+ const attachments = draftsBySession.get(key)?.attachments ?? []
+
+ if (attachments.length > 0) {
+ draftsBySession.set(key, { attachments, text: '' })
+ } else {
draftsBySession.delete(key)
- publishDraftTitle(key, '')
}
+
+ publishDraftTitle(key, '')
}
+
+ persistedDraftTexts = incoming
}
// localStorage `storage` events fire across Electron BrowserWindows of the
@@ -486,6 +502,8 @@ function persistDraftTexts() {
} else {
window.localStorage.setItem(SESSION_DRAFTS_STORAGE_KEY, JSON.stringify(Object.fromEntries(entries)))
}
+
+ persistedDraftTexts = new Map(entries)
} catch {
// Best-effort only — quota/private-mode must never break typing.
}