The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): sync the composer queue across windows
Austin Pickett9d2975398fb8
diff --git a/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx b/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsxindex c102e08f89d..fa37b7eeca1 100644--- a/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx+++ b/apps/desktop/src/app/session/hooks/use-background-queue-drain.test.tsx@@ -62,6 +62,10 @@ describe('useBackgroundQueueDrain', () => {beforeEach(() => {vi.useRealTimers()clearAllSessionStates()+ // The queue store merges over live localStorage on save (cross-window sync,+ // #46732) — stale persisted entries from an earlier test would be adopted+ // into the atom and drained here as if they were fresh queue state.+ window.localStorage.removeItem('hermes.desktop.composerQueue.v1')// Production drain waits for the sidebar list. Tests that assert drain// behavior are post-load unless they opt into the loading gate.setSessionsLoading(false)diff --git a/apps/desktop/src/store/composer-queue.test.ts b/apps/desktop/src/store/composer-queue.test.tsindex f06da26238b..4fadda8f7db 100644--- a/apps/desktop/src/store/composer-queue.test.ts+++ b/apps/desktop/src/store/composer-queue.test.ts@@ -253,3 +253,69 @@ describe('hidden entries', () => {])})})++describe('cross-window sync (#46732)', () => {+ beforeEach(() => {+ window.localStorage.removeItem(QUEUE_STORAGE_KEY)+ $queuedPromptsBySession.set({})+ })++ const storedEntry = (id: string, text: string) => ({ id, text, attachments: [], queuedAt: 1 })++ const dispatchStorage = (key: null | string, newValue: null | string) => {+ window.dispatchEvent(new StorageEvent('storage', { key, newValue }))+ }++ it('adopts another window\'s write from the storage event', () => {+ window.localStorage.setItem(+ QUEUE_STORAGE_KEY,+ JSON.stringify({ 'session-other': [storedEntry('q1', 'from other window')] })+ )++ dispatchStorage(QUEUE_STORAGE_KEY, window.localStorage.getItem(QUEUE_STORAGE_KEY))++ expect(getQueuedPrompts('session-other').map(entry => entry.text)).toEqual(['from other window'])+ })++ it('does not clobber another window\'s entries when saving its own (same-frame race)', () => {+ enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'mine first' })++ // Another window queues into its own session directly in storage, faster+ // than any storage event could reach us.+ window.localStorage.setItem(+ QUEUE_STORAGE_KEY,+ JSON.stringify({ ...JSON.parse(window.localStorage.getItem(QUEUE_STORAGE_KEY)!), 'session-other': [storedEntry('q2', 'theirs')] })+ )++ enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'mine second' })++ expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['mine first', 'mine second'])+ expect(getQueuedPrompts('session-other').map(entry => entry.text)).toEqual(['theirs'])+ })++ it('drops entries locally once another window drains them', () => {+ enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'drained elsewhere' })++ window.localStorage.setItem(QUEUE_STORAGE_KEY, JSON.stringify({}))+ dispatchStorage(QUEUE_STORAGE_KEY, '{}')++ expect(getQueuedPrompts(SESSION_KEY)).toEqual([])+ })++ it('resyncs on a full storage clear (event.key === null)', () => {+ enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'wiped' })++ window.localStorage.removeItem(QUEUE_STORAGE_KEY)+ dispatchStorage(null, null)++ expect(getQueuedPrompts(SESSION_KEY)).toEqual([])+ })++ it('ignores storage events for unrelated keys', () => {+ enqueueQueuedPrompt(SESSION_KEY, { attachments: [], text: 'kept' })++ dispatchStorage('unrelated.key', '{}')++ expect(getQueuedPrompts(SESSION_KEY).map(entry => entry.text)).toEqual(['kept'])+ })+})diff --git a/apps/desktop/src/store/composer-queue.ts b/apps/desktop/src/store/composer-queue.tsindex 09e64832b84..0afa76a5cfa 100644--- a/apps/desktop/src/store/composer-queue.ts+++ b/apps/desktop/src/store/composer-queue.ts@@ -91,20 +91,42 @@ const setParked = (sid: string, parked: boolean) => {}const writeSession = (sid: string, queue: QueuedPromptEntry[]) => {- const current = $queuedPromptsBySession.get()- const next = { ...current }+ // Merge over the LIVE persisted map, not the in-memory atom: another window+ // may have written its own sessions' queues between our last sync and now,+ // and writing our whole snapshot back would clobber those entries (#46732).+ const live = load()+ const next: QueueState = { ...live }if (queue.length === 0) {delete next[sid]- // An empty queue has nothing to hold back — drop the park so it can't- // linger as stale state and silently gate entries queued much later.- setParked(sid, false)} else {next[sid] = queue}$queuedPromptsBySession.set(next)save(next)++ if (queue.length === 0) {+ // An empty queue has nothing to hold back — drop the park so it can't+ // linger as stale state and silently gate entries queued much later.+ setParked(sid, false)+ }+}++if (typeof window !== 'undefined') {+ // Cross-window sync (#46732): every desktop window boots the queue atom from+ // the same localStorage key. The `storage` event fires in every window EXCEPT+ // the writer, so there is no self-echo to guard — adopting the fresh map here+ // keeps the other windows' entries from vanishing (their writes clobbered+ // ours) or resurrecting (our stale snapshot re-queued what they drained).+ // `event.key === null` is the full-clear signal (localStorage.clear()).+ window.addEventListener('storage', event => {+ if (event.key !== null && event.key !== STORAGE_KEY) {+ return+ }++ $queuedPromptsBySession.set(load())+ })}const sidOf = (key: string | null | undefined): null | string => {@@ -288,9 +310,12 @@ export const migrateQueuedPrompts = (fromKey: string | null | undefined, toKey:return false}- const next = { ...$queuedPromptsBySession.get() }+ // Merge over the live persisted map (see writeSession) so the migration can't+ // clobber entries another window queued meanwhile — including into `to`.+ const live = load()+ const next: QueueState = { ...live }delete next[from]- next[to] = [...queueFor(to), ...pending]+ next[to] = [...(live[to] ?? queueFor(to)), ...pending]$queuedPromptsBySession.set(next)save(next)