Skip to content

The agent that grows with you

fix(desktop): read the v3 draft list only before any window wrote the v4 stash

Tekniumdc23156e1ec4
diff --git a/apps/desktop/src/store/composer-draft-stash.ts b/apps/desktop/src/store/composer-draft-stash.ts
index a0c35c56bdc..7816bf1b98f 100644
--- a/apps/desktop/src/store/composer-draft-stash.ts
+++ b/apps/desktop/src/store/composer-draft-stash.ts
@@ -15,7 +15,8 @@
*
* The authoritative copy is one key (`DRAFT_STASH_STORAGE_KEY`) so a read never
* sees half of another window's write. The older `{ key: text }` list is still
- * written after it, for builds and readers that only know that list.
+ * written after it, for builds and readers that only know that list; two
+ * windows writing in the same instant can leave that mirror one write behind.
*/
export const DRAFT_STASH_STORAGE_KEY = 'hermes:composer-drafts:v4'
@@ -51,6 +52,14 @@ export function newerEntry(a: StashEntry | undefined, b: StashEntry | undefined)
return (a.text ?? '') > (b.text ?? '')
}
+export function hasDraftStash(): boolean {
+ try {
+ return window.localStorage.getItem(DRAFT_STASH_STORAGE_KEY) !== null
+ } catch {
+ return false
+ }
+}
+
export function readDraftStash(): StashEntries {
const entries: StashEntries = new Map()
diff --git a/apps/desktop/src/store/composer.ts b/apps/desktop/src/store/composer.ts
index 6de69f4859a..5478429984a 100644
--- a/apps/desktop/src/store/composer.ts
+++ b/apps/desktop/src/store/composer.ts
@@ -8,6 +8,7 @@ import { persistString, storedString } from '@/lib/storage'
import {
compactDraftStash,
DRAFT_STASH_STORAGE_KEY,
+ hasDraftStash,
newerEntry,
readDraftStash,
readLegacyDrafts,
@@ -315,14 +316,10 @@ const cloneDraft = (draft: SessionDraft): SessionDraft => ({
// What this window last read from or wrote to the shared stash, cleared
// markers included. A reload applies only entries newer than this, and a write
// puts back any entry here that storage has since lost to a stale writer.
-let persistedDrafts: StashEntries = readDraftStash()
-
-// A pre-v4 list is read once, never over a newer entry or a cleared marker.
-for (const [key, entry] of readLegacyDrafts(SESSION_DRAFTS_STORAGE_KEY)) {
- if (!persistedDrafts.has(key)) {
- persistedDrafts.set(key, entry)
- }
-}
+// The pre-v4 list seeds it only before any window has written v4: after
+// that the list is a mirror that can lag a moment behind, and reading it
+// could bring back a draft whose cleared marker has expired.
+let persistedDrafts: StashEntries = hasDraftStash() ? readDraftStash() : readLegacyDrafts(SESSION_DRAFTS_STORAGE_KEY)
const draftsBySession = new Map<string, SessionDraft>(
[...persistedDrafts]