Skip to content

The agent that grows with you

fix(desktop): each prepared-input tombstone lives under its own storage key

Teknium42903ddb42b5
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-spent.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-spent.test.ts
index 0d1e5209705..74082b3bd7c 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-spent.test.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-spent.test.ts
@@ -45,3 +45,65 @@ test('an admitted identity whose journal removal failed stays spent across a rel
await reloaded.listPreparedImageDrafts('stored', 'local::default')
expect(journal.file()[intent]).toBeUndefined()
})
+
+// Chromium gives each renderer its own cached copy of the origin's localStorage and propagates
+// writes asynchronously, last writer wins per key: each window writes against its own view.
+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 }
+}
+
+test('two windows retiring different admitted entries keep both tombstones across a reload', async () => {
+ const realStorage = window.localStorage
+ const inWindow = (storage: Storage) => Object.defineProperty(window, 'localStorage', { configurable: true, value: storage })
+ const journal = nativeJournal()
+ vi.stubGlobal('hermesDesktop', { preparedSubmissions: journal.native })
+ const owner = { connectionId: 'local', profile: 'default' }
+ const intent = (text: string) => JSON.stringify(['local::default', 'stored', text, [], null, false, null])
+ const entry = (id: string, text: string) => ({ id, text, attachments: [], params: { submission_id: id }, owner })
+
+ try {
+ const shared = new Map<string, string>()
+ const a = storageView(shared)
+ const b = storageView(shared)
+ const windowA = await import('./prepared-submissions')
+ vi.resetModules()
+ const windowB = await import('./prepared-submissions')
+
+ inWindow(a.storage as unknown as Storage)
+ await windowA.writePreparedSubmission(intent('from A'), entry('admitted-a', 'from A'))
+ inWindow(b.storage as unknown as Storage)
+ await windowB.writePreparedSubmission(intent('from B'), entry('admitted-b', 'from B'))
+
+ // Both admissions are ACKed while the native journal cannot delete either entry.
+ journal.state.failing = true
+ inWindow(a.storage as unknown as Storage)
+ await expect(windowA.removePreparedSubmission(intent('from A'), 'admitted-a')).rejects.toThrow('ENOSPC')
+ inWindow(b.storage as unknown as Storage)
+ await expect(windowB.removePreparedSubmission(intent('from B'), 'admitted-b')).rejects.toThrow('ENOSPC')
+ a.flush()
+ b.flush()
+
+ // A fresh window over the converged storage adopts neither admitted input again.
+ inWindow(storageView(shared).storage as unknown as Storage)
+ vi.resetModules()
+ const reloaded = await import('./prepared-submissions')
+ expect(await reloaded.adoptPreparedSubmission(intent('from A'))).toBeUndefined()
+ expect(await reloaded.adoptPreparedSubmission(intent('from B'))).toBeUndefined()
+ } finally {
+ inWindow(realStorage)
+ }
+})
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.ts
index 5ecfac79f10..5f8c1e8029f 100644
--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.ts
+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.ts
@@ -22,28 +22,50 @@ export interface PreparedSubmission {
// separate store and read on every journal access, so a reload (fresh module memory) still honors
// it; module memory only covers a tombstone write that itself failed. A tombstone hides only the
// entry carrying that exact id, never a newer send that reused the journal key.
-const SPENT_KEY = 'hermes.desktop.preparedSubmissions.spent.v1'
+// One localStorage key per tombstone: every window of the origin shares this storage with
+// last-writer-wins per KEY, so a single blob let one window's read-modify-write drop the tombstone
+// another window had just written for a different entry (and a reload then re-adopted that
+// admitted input). Each retirement now writes only the record it owns.
+const SPENT_PREFIX = 'hermes.desktop.preparedSubmissions.spent.v2:'
+// The pre-v2 single blob: still honored, drained entry by entry as each tombstone clears.
+const LEGACY_SPENT_KEY = 'hermes.desktop.preparedSubmissions.spent.v1'
const retired = new Map<string, string>()
-function readSpent(): Record<string, string> {
- const raw = window.localStorage.getItem(SPENT_KEY)
+function readLegacySpent(): Record<string, string> {
+ const raw = window.localStorage.getItem(LEGACY_SPENT_KEY)
const parsed: unknown = raw ? JSON.parse(raw) : {}
- return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
- ? { ...(parsed as Record<string, string>), ...Object.fromEntries(retired) }
- : Object.fromEntries(retired)
+ return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, string> : {}
+}
+
+function readSpent(): Record<string, string> {
+ const spent = readLegacySpent()
+
+ for (let index = 0; index < window.localStorage.length; index += 1) {
+ const key = window.localStorage.key(index)
+ const id = key?.startsWith(SPENT_PREFIX) ? window.localStorage.getItem(key) : null
+
+ if (key && id) { spent[key.slice(SPENT_PREFIX.length)] = id }
+ }
+
+ return { ...spent, ...Object.fromEntries(retired) }
}
function writeSpent(key: string, id: string | undefined): void {
- const raw = window.localStorage.getItem(SPENT_KEY)
- const spent: Record<string, string> = raw ? JSON.parse(raw) : {}
+ if (id !== undefined) {
+ window.localStorage.setItem(SPENT_PREFIX + key, id)
+
+ return
+ }
- if (id === undefined && !(key in spent)) { return }
+ window.localStorage.removeItem(SPENT_PREFIX + key)
+ const legacy = readLegacySpent()
- if (id === undefined) { delete spent[key] } else { spent[key] = id }
+ if (!(key in legacy)) { return }
+ delete legacy[key]
- if (Object.keys(spent).length) { window.localStorage.setItem(SPENT_KEY, JSON.stringify(spent)) }
- else { window.localStorage.removeItem(SPENT_KEY) }
+ if (Object.keys(legacy).length) { window.localStorage.setItem(LEGACY_SPENT_KEY, JSON.stringify(legacy)) }
+ else { window.localStorage.removeItem(LEGACY_SPENT_KEY) }
}
// A journal, not an automatic outbox. Only an explicit retry may reuse an