The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): pending-submission tracking never drops another window's entry
Teknium02036db5ed7f
diff --git a/apps/desktop/src/store/pending-submissions-windows.test.ts b/apps/desktop/src/store/pending-submissions-windows.test.tsnew file mode 100644index 00000000000..98080046e76--- /dev/null+++ b/apps/desktop/src/store/pending-submissions-windows.test.ts@@ -0,0 +1,51 @@+import { afterEach, expect, it } from 'vitest'++import { getQueuedPrompts } from './composer-queue'+import { reconcilePendingSubmissions, trackPendingSubmission } from './pending-submissions'++// Chromium gives each renderer process its own cached copy of the origin's localStorage and+// propagates writes asynchronously, last writer wins per key. Two windows: each writes against+// its own (stale) view, then the copies converge.+function storageView(shared: Map<string, string>) {+ const local = new Map(shared)+ const written = new Map<string, null | string>()++ const storage = {+ get length() { return local.size },+ key: (index: number) => [...local.keys()][index] ?? null,+ getItem: (key: string) => local.get(key) ?? null,+ setItem: (key: string, value: string) => { local.set(key, value); written.set(key, value) },+ removeItem: (key: string) => { local.delete(key); written.set(key, null) },+ clear: () => undefined+ }++ const flush = () => { for (const [key, value] of written) { if (value === null) { shared.delete(key) } else { shared.set(key, value) } } }++ return { storage, flush }+}++const real = window.localStorage++afterEach(() => { Object.defineProperty(window, 'localStorage', { configurable: true, value: real }); real.clear() })++it("one window's pending-submission write never drops another window's concurrent entry", () => {+ const shared = new Map<string, string>()+ const a = storageView(shared)+ const b = storageView(shared)++ Object.defineProperty(window, 'localStorage', { configurable: true, value: a.storage })+ trackPendingSubmission('chat', { id: 'from-a', text: 'A', displayText: '/a' })+ Object.defineProperty(window, 'localStorage', { configurable: true, value: b.storage })+ trackPendingSubmission('chat', { id: 'from-b', text: 'B', displayText: '/b' })+ a.flush()+ b.flush()++ const converged = storageView(shared)+ Object.defineProperty(window, 'localStorage', { configurable: true, value: converged.storage })+ // The server queue names both admissions; each keeps the display text its own window tracked.+ reconcilePendingSubmissions('chat', [+ { admission_id: 'from-a', status: 'queued', user: 'A' },+ { admission_id: 'from-b', status: 'queued', user: 'B' }+ ])+ expect(getQueuedPrompts('chat').map(entry => [entry.id, entry.displayText])).toEqual([['from-a', '/a'], ['from-b', '/b']])+})diff --git a/apps/desktop/src/store/pending-submissions.test.ts b/apps/desktop/src/store/pending-submissions.test.tsindex ca6f774926f..5b60e83fa84 100644--- a/apps/desktop/src/store/pending-submissions.test.ts+++ b/apps/desktop/src/store/pending-submissions.test.ts@@ -4,7 +4,7 @@ import { applyRuntimeInfo } from '@/app/session/hooks/use-session-actions/utils'import { isSteerableEntry } from '@/store/composer-queue'import { $queuedPromptsBySession, enqueueQueuedPrompt, getQueuedPrompts } from './composer-queue'-import { reconcilePendingSubmissions, trackPendingSubmission } from './pending-submissions'+import { readPendingSubmissions, reconcilePendingSubmissions, trackPendingSubmission } from './pending-submissions'beforeEach(() => {window.localStorage.clear()@@ -54,10 +54,10 @@ it('recovers remote pending text into the queue and journal without a local submreconcilePendingSubmissions('chat', snapshot)expect(getQueuedPrompts('chat').map(({ id, text, serverStatus }) => ({ id, text, serverStatus }))).toEqual(snapshot.map(({ admission_id, user, status }) => ({ id: admission_id, text: user, serverStatus: status })))- const journal = JSON.parse(window.localStorage.getItem('hermes.desktop.pendingSubmissions.v1')!)+ const journal = readPendingSubmissions('chat')for (const receipt of snapshot) {- expect(journal.chat[receipt.admission_id].text).toBe(receipt.user)+ expect(journal[receipt.admission_id].text).toBe(receipt.user)}})@@ -73,8 +73,7 @@ it('maps optimistic input identity to the admission identity before local drainit('persists identified direct submissions independently of the automatic local queue', () => {trackPendingSubmission('chat', { id: 'direct', text: 'hello' })- const stored = JSON.parse(window.localStorage.getItem('hermes.desktop.pendingSubmissions.v1')!)- expect(stored.chat.direct).toMatchObject({ id: 'direct', text: 'hello' })+ expect(readPendingSubmissions('chat').direct).toMatchObject({ id: 'direct', text: 'hello' })expect(getQueuedPrompts('chat')).toEqual([])})diff --git a/apps/desktop/src/store/pending-submissions.ts b/apps/desktop/src/store/pending-submissions.tsindex 2baf6be9318..9c361080879 100644--- a/apps/desktop/src/store/pending-submissions.ts+++ b/apps/desktop/src/store/pending-submissions.ts@@ -1,27 +1,70 @@import { getQueuedPrompts, type QueuedPromptEntry, writeSessionQueue } from './composer-queue'-const STORAGE_KEY = 'hermes.desktop.pendingSubmissions.v1'+// One localStorage key per (session, admission). Every Desktop window of the origin shares this+// storage with last-writer-wins per KEY, so a single journal blob let one window's+// read-modify-write silently drop an entry another window had just tracked. Per-entry keys make+// each write touch only the record it owns.+const ENTRY_PREFIX = 'hermes.desktop.pendingSubmissions.v2:'+// The pre-v2 single blob, read for upgrade and drained per session as entries are rewritten.+const LEGACY_KEY = 'hermes.desktop.pendingSubmissions.v1'interface PendingSubmission {id: stringtext: stringdisplayText?: stringstatus?: string}-type Journal = Record<string, Record<string, PendingSubmission>>-const readJournal = (): Journal => {+const entryKey = (session: string, id: string) => `${ENTRY_PREFIX}${JSON.stringify([session, id])}`++const parse = <T>(raw: null | string, fallback: T): T => {try {- return JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}') as Journal+ return raw ? (JSON.parse(raw) as T) : fallback} catch {- return {}+ return fallback+ }+}++/** Every tracked/observed pending submission of one session, keyed by id. */+export function readPendingSubmissions(session: string): Record<string, PendingSubmission> {+ const entries: Record<string, PendingSubmission> = { ...parse(window.localStorage.getItem(LEGACY_KEY), {} as Record<string, Record<string, PendingSubmission>>)[session] }++ for (let index = 0; index < window.localStorage.length; index += 1) {+ const key = window.localStorage.key(index)++ if (!key?.startsWith(ENTRY_PREFIX)) { continue }+ const [owner, id] = parse(key.slice(ENTRY_PREFIX.length), [] as unknown[])+ const entry = parse<null | PendingSubmission>(window.localStorage.getItem(key), null)++ if (owner === session && typeof id === 'string' && entry) { entries[id] = entry }+ }++ return entries+}++function writeEntries(session: string, before: Record<string, PendingSubmission>, after: Record<string, PendingSubmission>): void {+ for (const id of Object.keys(before)) {+ if (!(id in after)) { window.localStorage.removeItem(entryKey(session, id)) }+ }++ for (const [id, entry] of Object.entries(after)) {+ if (JSON.stringify(before[id]) !== JSON.stringify(entry)) { window.localStorage.setItem(entryKey(session, id), JSON.stringify(entry)) }+ }++ // Upgrade: once this session's entries live in v2 keys, drop its slice of the legacy blob.+ const legacy = parse(window.localStorage.getItem(LEGACY_KEY), {} as Record<string, unknown>)++ if (session in legacy) {+ for (const [id, entry] of Object.entries(after)) { window.localStorage.setItem(entryKey(session, id), JSON.stringify(entry)) }+ delete legacy[session]++ if (Object.keys(legacy).length) { window.localStorage.setItem(LEGACY_KEY, JSON.stringify(legacy)) }+ else { window.localStorage.removeItem(LEGACY_KEY) }}}// Not an outbox: an uncertain accepted input must never be replayed automatically.export function trackPendingSubmission(key: string, entry: PendingSubmission): void {- const journal = readJournal()- journal[key] = { ...journal[key], [entry.id]: entry }- window.localStorage.setItem(STORAGE_KEY, JSON.stringify(journal))+ window.localStorage.setItem(entryKey(key, entry.id), JSON.stringify(entry))}// Array.isArray narrows `unknown` to `any[]`; the helpers keep that wire shape.@@ -134,17 +177,15 @@ export function reconcilePendingSubmissions(key: string, value: unknown): void {return}- const journal = readJournal()- const known = journal[key] ?? {}+ const before = readPendingSubmissions(key)+ const known = { ...before }const { receipts, admissionByInput } = collectReceipts(value, known)const current = getQueuedPrompts(key)const next = projectQueue(current, receipts, admissionByInput)updateKnownReceipts(known, value)-- journal[key] = known- window.localStorage.setItem(STORAGE_KEY, JSON.stringify(journal))+ writeEntries(key, before, known)if (JSON.stringify(current) !== JSON.stringify(next)) {writeSessionQueue(key, next)