The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): an admitted submission's spent identity survives a reload
Tekniumf2795ac8c500
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.tsnew file mode 100644index 00000000000..0d1e5209705--- /dev/null+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-spent.test.ts@@ -0,0 +1,47 @@+import { afterEach, expect, test, vi } from 'vitest'++afterEach(() => { vi.unstubAllGlobals(); vi.resetModules(); localStorage.clear() })++// The native file journal of one origin, shared by every renderer load. Removal fails like+// ENOSPC/EIO while `failing` is set; reads keep working.+function nativeJournal() {+ let file = '{}'+ const state = { failing: false }++ const native = {+ read: async () => file,+ update: async (key: string, entry: string | null) => {+ if (state.failing) { throw new Error('ENOSPC: no space left on device') }+ const journal = JSON.parse(file)++ if (entry === null) { delete journal[key] } else { journal[key] = JSON.parse(entry) }+ file = JSON.stringify(journal)+ }+ }++ return { native, state, file: () => JSON.parse(file) }+}++test('an admitted identity whose journal removal failed stays spent across a reload', async () => {+ const journal = nativeJournal()+ vi.stubGlobal('hermesDesktop', { preparedSubmissions: journal.native })+ const intent = JSON.stringify(['local::default', 'stored', 'same text', [], null, false, null])+ const entry = { id: 'admitted-id', text: 'same text', attachments: [], params: { submission_id: 'admitted-id' }, owner: { connectionId: 'local', profile: 'default' } }++ const first = await import('./prepared-submissions')+ await first.writePreparedSubmission(intent, entry)+ journal.state.failing = true+ await expect(first.removePreparedSubmission(intent, 'admitted-id')).rejects.toThrow('ENOSPC')+ expect(journal.file()[intent]?.id).toBe('admitted-id')++ // Renderer reload: module memory is gone, the file entry is not.+ vi.resetModules()+ const reloaded = await import('./prepared-submissions')+ expect(await reloaded.adoptPreparedSubmission(intent)).toBeUndefined()+ expect(await reloaded.readPreparedSubmission(intent)).toBeUndefined()++ // Storage recovers: the stale file entry is retired instead of lingering.+ journal.state.failing = false+ await reloaded.listPreparedImageDrafts('stored', 'local::default')+ expect(journal.file()[intent]).toBeUndefined()+})diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.test.tsindex e5a8663f87c..1b334681211 100644--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.test.ts+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions.test.ts@@ -39,11 +39,11 @@ test('native preparation waits for acknowledgement and never downgrades a writenative.update.mockRejectedValueOnce(new Error('disk full'))await expect(writePreparedSubmission('key', entry)).rejects.toThrow('disk full')expect(localStorage.length).toBe(0)- await removePreparedSubmission('key')+ await removePreparedSubmission('key', 'a')expect(native.update).toHaveBeenLastCalledWith('key', null)vi.stubGlobal('hermesDesktop', undefined)await writePreparedSubmission('key', entry)expect(await readPreparedSubmission('key')).toEqual(entry)- await removePreparedSubmission('key')+ await removePreparedSubmission('key', 'a')expect(await readPreparedSubmission('key')).toBeUndefined()})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.tsindex a939485fc27..5ecfac79f10 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@@ -16,10 +16,35 @@ export interface PreparedSubmission {legacyAttempted?: boolean}-// Admitted entries whose durable removal failed (ENOSPC/EIO). Their identity is spent: this-// window never adopts, lists or slots them again, so a later send is never deduplicated-// into an earlier admission. The entry's Web Lock stays held until the removal lands.-const retired = new Set<string>()+// Admitted entries whose durable removal failed (ENOSPC/EIO): journal key -> spent submission id.+// Their identity is spent: no window adopts, lists or slots them again, so a later send is never+// deduplicated into an earlier admission. The tombstone is written BEFORE the journal removal to a+// 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'+const retired = new Map<string, string>()++function readSpent(): Record<string, string> {+ const raw = window.localStorage.getItem(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)+}++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 && !(key in spent)) { return }++ if (id === undefined) { delete spent[key] } else { spent[key] = id }++ if (Object.keys(spent).length) { window.localStorage.setItem(SPENT_KEY, JSON.stringify(spent)) }+ else { window.localStorage.removeItem(SPENT_KEY) }+}// A journal, not an automatic outbox. Only an explicit retry may reuse an// uncertain admission. Read storage each time so a remount cannot lose it.@@ -34,9 +59,39 @@ async function readJournal(): Promise<Record<string, PreparedSubmission>> {throw new Error('Invalid prepared submission journal')}- for (const key of retired) {delete (parsed as Record<string, PreparedSubmission>)[key]}+ const journal = parsed as Record<string, PreparedSubmission>++ for (const [key, id] of Object.entries(readSpent())) {+ if (journal[key]?.id === id) {+ delete journal[key]+ // Storage may have recovered since: finish the retirement so the stale entry stops lingering.+ await retireJournalEntry(key).then(() => clearSpent(key), error => console.warn('[prepared-submission-retire]', error))+ } else {+ // Gone, or replaced by a newer send under the same key: the spent entry no longer exists.+ clearSpent(key)+ }+ }- return parsed as Record<string, PreparedSubmission>+ return journal+}++function clearSpent(key: string): void {+ retired.delete(key)+ writeSpent(key, undefined)+}++async function retireJournalEntry(key: string): Promise<void> {+ const native = window.hermesDesktop?.preparedSubmissions++ if (native) {+ await native.update(key, null)++ return+ }++ const journal: Record<string, PreparedSubmission> = JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}')+ delete journal[key]+ window.localStorage.setItem(STORAGE_KEY, JSON.stringify(journal))}export function preparedSubmissionKey(@@ -140,7 +195,7 @@ export async function writePreparedSubmission(key: string, entry: PreparedSubmisif (native) {await native.update(key, JSON.stringify(entry))- retired.delete(key)+ clearSpent(key)return}@@ -150,24 +205,19 @@ export async function writePreparedSubmission(key: string, entry: PreparedSubmis// Browser-only clients retain reload recovery, not a process-crash guarantee.// Native write failures never fall back here: sending requires their ACK.window.localStorage.setItem(STORAGE_KEY, JSON.stringify(journal))- retired.delete(key)+ clearSpent(key)}-/** Retire an admitted entry. Its identity is spent before the durable write is attempted, so a- * failed removal can only leave a stale file entry, never a reusable one. */-export async function removePreparedSubmission(key: string): Promise<void> {- const native = window.hermesDesktop?.preparedSubmissions- retired.add(key)+/** Retire an admitted entry. Its identity is spent — durably, in a separate store — before the+ * journal removal is attempted, so a failed removal can only leave a stale file entry that every+ * later read (this window, a reload, another window) hides and retries, never a reusable one. */+export async function removePreparedSubmission(key: string, id: string): Promise<void> {+ retired.set(key, id)- if (native) {- await native.update(key, null)- } else {- const journal: Record<string, PreparedSubmission> = JSON.parse(window.localStorage.getItem(STORAGE_KEY) || '{}')- delete journal[key]- window.localStorage.setItem(STORAGE_KEY, JSON.stringify(journal))- }-- retired.delete(key)+ // The tombstone store failing too (quota) leaves the in-memory mark; the removal still runs.+ try { writeSpent(key, id) } catch (error) { console.warn('[prepared-submission-tombstone]', error) }+ await retireJournalEntry(key)+ clearSpent(key)owned.get(key)?.()owned.delete(key)}diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.tsindex 4a046af3c4f..48b29f83894 100644--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/submit.ts@@ -1265,8 +1265,9 @@ export function useSubmitPrompt(deps: SubmitPromptDeps) {}// The gateway admitted it: a failed journal retirement (ENOSPC/EIO) must not report a- // delivered prompt as failed and invite a resend. The identity is retired in memory.- await removePreparedSubmission(retryKey).catch(error => console.warn('[prepared-submission-retire]', error))+ // delivered prompt as failed and invite a resend. The identity is tombstoned first, so the+ // stale entry is never adopted again — not after a reload either.+ await removePreparedSubmission(retryKey, prepared.id).catch(error => console.warn('[prepared-submission-retire]', error))// The prompt is now accepted. Report the EXACT identity it landed on// (recovered id included) so a caller that must prove delivery — the