Skip to content

Commit 3bee846

Browse files
committed
fix(web): keep orphan stash settlement local to the tab
1 parent 8c091d5 commit 3bee846

2 files changed

Lines changed: 39 additions & 25 deletions

File tree

‎apps/web/src/promptStashStore.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,14 +369,25 @@ describe("promptStashStore", () => {
369369
expect(JSON.parse(shared.stored.get(PROMPT_STASH_STORAGE_KEY) ?? "null")).toMatchObject({
370370
state: {
371371
entries: [
372-
{ id: "orphan", pendingImageCount: 0, unreadableImageNames: [expect.any(String)] },
372+
{ id: "orphan", pendingImageCount: 1 },
373373
{ id: "encoding", attachments: [encodedImage], pendingImageCount: 0 },
374374
],
375375
},
376376
});
377377
expect(tab.usePromptStashStore.getState().takeEntry("encoding").entry?.attachments).toEqual([
378378
encodedImage,
379379
]);
380+
381+
const settling = tab.writePromptStashStorageForTest(
382+
JSON.stringify({ version: 2, state: { entries: [orphan] } }),
383+
);
384+
shared.afterNextRead = () => {
385+
tab.usePromptStashStore.getState().stashEntry(makeEntry({ id: "new-text" }));
386+
};
387+
await settling;
388+
expect(tab.usePromptStashStore.getState().takeEntry("new-text").entry?.prompt).toBe(
389+
"prompt new-text",
390+
);
380391
} finally {
381392
vi.unstubAllGlobals();
382393
}

‎apps/web/src/promptStashStore.ts‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ const pendingEntryIds = (entries: ReadonlyArray<PromptStashEntry>) =>
9393
new Set(entries.filter((entry) => entry.pendingImageCount).map((entry) => entry.id));
9494

9595
const SETTLE_LOCK_ATTEMPTS = 3;
96+
const settledPendingEntryIds = new Set<string>();
9697

9798
/**
9899
* Only the stashing tab's encode clears `pendingImageCount`, so an entry nobody encodes would
99-
* show "saving…" forever. Settling writes only while holding the encode lock of every entry
100-
* pending in its final read, and an encoder frees its lock only after finalizing.
100+
* show "saving…" forever. Check ownership under encode locks, then settle locally;
101+
* writing a startup snapshot could overwrite a prompt another tab just stashed.
101102
*/
102103
async function settleOrphanedPendingImages(loaded: ReadonlyArray<PromptStashEntry>) {
103104
let pendingIds = pendingEntryIds(loaded);
@@ -135,26 +136,25 @@ function settlePendingImages(
135136
latest: ReadonlyArray<PromptStashEntry>,
136137
orphanIds: ReadonlySet<string>,
137138
): void {
138-
let settledAny = false;
139-
const settled = latest.map((entry) => {
140-
if (!entry.pendingImageCount || !orphanIds.has(entry.id)) return entry;
141-
settledAny = true;
142-
const lostCount = entry.pendingImageCount;
143-
return {
144-
...entry,
145-
pendingImageCount: 0,
146-
unreadableImageNames: [
147-
...(entry.unreadableImageNames ?? []),
148-
...Array.from(
149-
{ length: lostCount },
150-
(_, index) => `image ${index + 1} (not saved before reload)`,
151-
),
152-
],
153-
};
154-
});
155-
if (!settledAny) return;
156-
persistEntries(settled);
157-
usePromptStashStore.setState({ entries: visibleEntries(settled) });
139+
for (const entry of latest) {
140+
if (entry.pendingImageCount && orphanIds.has(entry.id)) settledPendingEntryIds.add(entry.id);
141+
}
142+
usePromptStashStore.setState({ entries: visibleEntries(readPersistedEntries() ?? latest) });
143+
}
144+
145+
function withSettledPendingImages(entry: PromptStashEntry): PromptStashEntry {
146+
if (!entry.pendingImageCount || !settledPendingEntryIds.has(entry.id)) return entry;
147+
return {
148+
...entry,
149+
pendingImageCount: 0,
150+
unreadableImageNames: [
151+
...(entry.unreadableImageNames ?? []),
152+
...Array.from(
153+
{ length: entry.pendingImageCount },
154+
(_, index) => `image ${index + 1} (not saved before reload)`,
155+
),
156+
],
157+
};
158158
}
159159

160160
export function partitionStashAttachments(
@@ -228,7 +228,10 @@ function visibleEntries(entries: ReadonlyArray<PromptStashEntry>): ReadonlyArray
228228
for (const entryId of unsavedImagesByEntryId.keys()) {
229229
if (!entries.some((entry) => entry.id === entryId)) unsavedImagesByEntryId.delete(entryId);
230230
}
231-
return unsavedImagesByEntryId.size === 0 ? entries : entries.map(withUnsavedImages);
231+
for (const entryId of settledPendingEntryIds) {
232+
if (!entries.some((entry) => entry.id === entryId)) settledPendingEntryIds.delete(entryId);
233+
}
234+
return entries.map((entry) => withUnsavedImages(withSettledPendingImages(entry)));
232235
}
233236

234237
// Clear the composer only after a durable write; debounce cannot confirm durability.
@@ -272,7 +275,7 @@ function readPersistedEntries(): ReadonlyArray<PromptStashEntry> | null {
272275
* entry another tab stashed since this tab last read.
273276
*/
274277
function latestEntries(current: ReadonlyArray<PromptStashEntry>): ReadonlyArray<PromptStashEntry> {
275-
return readPersistedEntries() ?? current;
278+
return visibleEntries(readPersistedEntries() ?? current);
276279
}
277280

278281
interface PromptStashStoreState {

0 commit comments

Comments
 (0)