The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): a journal read lists keys once but reads tombstones after the entries
unsupportedpastels9ab5fd2ffb64
diff --git a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-tabs.test.ts b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-tabs.test.tsindex eda77072e3c..e91490e88bf 100644--- a/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-tabs.test.ts+++ b/apps/desktop/src/app/session/hooks/use-prompt-actions/prepared-submissions-tabs.test.ts@@ -123,3 +123,65 @@ test('retiring an entry never hides a different send the earlier build left undeawait journal.removePreparedSubmission(key, 'new')expect(await journal.adoptPreparedSubmission(key, 'earlier')).toEqual({ key, entry: entry('earlier', 'same words') })})++const ENTRY = 'hermes.desktop.preparedSubmissions.v2:'+const SPENT = 'hermes.desktop.preparedSubmissions.spent.v2:'++// Storage over `stored` that runs `onKey` on every key() call and `onGet` on every getItem(): another+// tab's writes landing while a journal read is part way through.+function interleavedStorage(stored: Map<string, string>, onKey: () => void, onGet: (key: string) => void) {+ return {+ get length() { return stored.size },+ key: (index: number) => {+ const key = [...stored.keys()][index] ?? null+ onKey()++ return key+ },+ getItem: (key: string) => {+ const value = stored.get(key) ?? null+ onGet(key)++ return value+ },+ setItem: (key: string, value: string) => { stored.set(key, value) },+ removeItem: (key: string) => { stored.delete(key) },+ clear: () => stored.clear()+ } as unknown as Storage+}++async function readWhile(stored: Map<string, string>, { onKey = () => undefined, onGet = () => undefined }: { onKey?: () => void; onGet?: (key: string) => void }) {+ const journal: Journal = await import('./prepared-submissions')+ installStorage(interleavedStorage(stored, onKey, onGet))++ return journal.readPreparedSubmission('a')+}++test('an entry read while another tab retires it is hidden even when its key already held an older tombstone', async () => {+ const stored = new Map([[SPENT + 'a', 'older'], [ENTRY + 'a', JSON.stringify(entry('a', 'a'))]])++ // The other tab's retirement tombstones 'a' under the same key just as this read takes the entry.+ const onGet = (key: string) => { if (key === ENTRY + 'a') { stored.set(SPENT + 'a', 'a') } }++ expect(await readWhile(stored, { onGet })).toBeUndefined()+ expect([...stored.keys()]).toEqual([])+})++test('an entry read while another tab retires it is hidden when its tombstone key appears after the key list', async () => {+ // This time the key held no tombstone at all when the read listed the keys.+ const stored = new Map([[ENTRY + 'a', JSON.stringify(entry('a', 'a'))]])+ const onGet = (key: string) => { if (key === ENTRY + 'a') { stored.set(SPENT + 'a', 'a') } }++ expect(await readWhile(stored, { onGet })).toBeUndefined()+ expect([...stored.keys()]).toEqual([])+})++test('a tombstone a concurrent removal shifts out of the key list still hides its entry', async () => {+ const stored = new Map([['unrelated', 'x'], [SPENT + 'a', 'a'], [ENTRY + 'a', JSON.stringify(entry('a', 'a'))]])++ // Another tab removes the first key just after it is listed: every later key moves down one index.+ const onKey = () => { stored.delete('unrelated') }++ expect(await readWhile(stored, { onKey })).toBeUndefined()+ expect([...stored.keys()]).toEqual([])+})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 2d7c9d32eda..b27260e9527 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@@ -49,27 +49,44 @@ function readLegacySpent(): Record<string, string> {return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed as Record<string, string> : {}}-const PREFIXES = [SPENT_PREFIX, LEGACY_RETIRED_PREFIX, ENTRY_PREFIX] as const-type Prefixed = Record<(typeof PREFIXES)[number], Record<string, string>>+const PREFIXES = [SPENT_PREFIX, ENTRY_PREFIX] as const+type Listed = Record<(typeof PREFIXES)[number], string[]>-// Every value stored under a key starting with one of the prefixes, by prefix and then by the-// rest of that key, in one pass over localStorage.-function readPrefixed(): Prefixed {- const values = Object.fromEntries(PREFIXES.map(prefix => [prefix, {}])) as Prefixed+// The rest of every key starting with one of the prefixes, by prefix, in one pass over the key+// list. No value is read here: each store is read afterwards, in the order readJournal needs.+function listPrefixed(): Listed {+ const listed = Object.fromEntries(PREFIXES.map(prefix => [prefix, []])) as unknown as Listedfor (let index = 0; index < window.localStorage.length; index += 1) {const key = window.localStorage.key(index)const prefix = PREFIXES.find(candidate => key?.startsWith(candidate))- const value = key && prefix ? window.localStorage.getItem(key) : null- if (key && prefix && value) { values[prefix][key.slice(prefix.length)] = value }+ if (key && prefix) { listed[prefix].push(key.slice(prefix.length)) }+ }++ return listed+}++// The value stored under `prefix` + each of `keys`, by key, where there is one.+function readPrefixed(prefix: string, keys: Iterable<string>): Record<string, string> {+ const values: Record<string, string> = {}++ for (const key of keys) {+ const value = window.localStorage.getItem(prefix + key)++ if (value) { values[key] = value }}return values}-function readSpent(prefixed: Prefixed): Record<string, string> {- return { ...readLegacySpent(), ...prefixed[SPENT_PREFIX], ...Object.fromEntries(retired) }+// Besides the listed tombstones, the one under every key the journal read holds: the key list is+// no snapshot, so a tombstone another tab wrote after it was taken, or that a removal shifted out+// of it, still hides its entry.+function readSpent(listed: string[], journal: Record<string, PreparedSubmission>): Record<string, string> {+ const keys = new Set([...listed, ...Object.keys(journal)])++ return { ...readLegacySpent(), ...readPrefixed(SPENT_PREFIX, keys), ...Object.fromEntries(retired) }}function writeSpent(key: string, id: string | undefined): void {@@ -103,10 +120,12 @@ async function readJournal(): Promise<Record<string, PreparedSubmission>> {}const stored = parsed as Record<string, PreparedSubmission>- const prefixed = readPrefixed()- const journal = native ? stored : readBrowserJournal(stored, prefixed)+ const listed = listPrefixed()+ const journal = native ? stored : readBrowserJournal(stored, listed[ENTRY_PREFIX])- for (const [key, id] of Object.entries(readSpent(prefixed))) {+ // Tombstones are read only after the entries: a retirement tombstones its entry before removing+ // it, so a tombstone written before an entry was read here is seen and hides that entry.+ for (const [key, id] of Object.entries(readSpent(listed[SPENT_PREFIX], journal))) {if (journal[key]?.id === id) {delete journal[key]// Storage may have recovered since: finish the retirement so the stale entry stops lingering.@@ -122,12 +141,13 @@ async function readJournal(): Promise<Record<string, PreparedSubmission>> {}// The entries under their own keys, over the legacy blob's entries not yet retired.-function readBrowserJournal(legacy: Record<string, PreparedSubmission>, prefixed: Prefixed): Record<string, PreparedSubmission> {- for (const [key, id] of Object.entries(prefixed[LEGACY_RETIRED_PREFIX])) {+function readBrowserJournal(legacy: Record<string, PreparedSubmission>, listed: string[]): Record<string, PreparedSubmission> {+ // Only a key the blob holds can carry a marker that matters, so each is read by name, not listed.+ for (const [key, id] of Object.entries(readPrefixed(LEGACY_RETIRED_PREFIX, Object.keys(legacy)))) {if (legacy[key]?.id === id) { delete legacy[key] }}- const entries = Object.entries(prefixed[ENTRY_PREFIX]).map(([key, entry]) => [key, JSON.parse(entry)])+ const entries = Object.entries(readPrefixed(ENTRY_PREFIX, listed)).map(([key, entry]) => [key, JSON.parse(entry)])return { ...legacy, ...Object.fromEntries(entries) }}