Skip to content

Commit cdfb798

Browse files
only21milclaude
andcommitted
fix(web): stashed images storage rejected stay restorable in the same tab
When browser storage rejects an entry's images, the stash now writes the entry without them, listing them as dropped, and keeps the images in this tab until it reloads. Restoring in the same session returns the images, and a later stash or another tab's write no longer brings back "saving…". Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 927fc7b commit cdfb798

2 files changed

Lines changed: 139 additions & 14 deletions

File tree

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

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22
import { EnvironmentId, MessageId, ThreadId } from "@t3tools/contracts";
33
import {
44
collectAssistantCitations,
@@ -248,6 +248,80 @@ describe("promptStashStore", () => {
248248
expect(usePromptStashStore.getState().entries[1]).toEqual(entry);
249249
});
250250

251+
it("keeps images storage rejected for this session and settles the stored entry", async () => {
252+
// A localStorage that rejects large writes, as a full origin quota does.
253+
const stored = new Map<string, string>();
254+
vi.stubGlobal("localStorage", {
255+
getItem: (key: string) => stored.get(key) ?? null,
256+
setItem: (key: string, value: string) => {
257+
if (value.length > 2_000) {
258+
throw new DOMException("Quota exceeded", "QuotaExceededError");
259+
}
260+
stored.set(key, value);
261+
},
262+
removeItem: (key: string) => {
263+
stored.delete(key);
264+
},
265+
});
266+
vi.resetModules();
267+
try {
268+
const { usePromptStashStore: store } = await import("./promptStashStore");
269+
const { stashEntry, finalizeEntryImages, takeEntry } = store.getState();
270+
const bigImage = {
271+
id: "big-img",
272+
name: "big.png",
273+
mimeType: "image/png",
274+
sizeBytes: 5_000,
275+
dataUrl: "x".repeat(5_000),
276+
};
277+
const rejectedImages = {
278+
attachments: [bigImage],
279+
droppedImageNames: [],
280+
unreadableImageNames: [],
281+
};
282+
283+
stashEntry({ ...makeEntry({ id: "rejected" }), pendingImageCount: 1 });
284+
expect(finalizeEntryImages("rejected", rejectedImages)).toEqual({
285+
attached: true,
286+
durable: false,
287+
});
288+
289+
// After a reload, and in other tabs, the entry reports the image missing.
290+
expect(JSON.parse(stored.get(PROMPT_STASH_STORAGE_KEY) ?? "null")).toMatchObject({
291+
state: {
292+
entries: [
293+
{
294+
id: "rejected",
295+
attachments: [],
296+
droppedImageNames: ["big.png"],
297+
pendingImageCount: 0,
298+
},
299+
],
300+
},
301+
});
302+
303+
// A later write re-reads storage, yet this tab still holds the image.
304+
stashEntry(makeEntry({ id: "later" }));
305+
expect(store.getState().entries[1]).toMatchObject({
306+
id: "rejected",
307+
attachments: [{ name: "big.png" }],
308+
pendingImageCount: 0,
309+
});
310+
expect(takeEntry("rejected").entry?.attachments).toEqual([bigImage]);
311+
312+
// An entry another tab already restored stays gone.
313+
stashEntry({ ...makeEntry({ id: "restored-elsewhere" }), pendingImageCount: 1 });
314+
finalizeEntryImages("restored-elsewhere", rejectedImages);
315+
stored.set(
316+
PROMPT_STASH_STORAGE_KEY,
317+
JSON.stringify({ version: 2, state: { entries: [makeEntry({ id: "later" })] } }),
318+
);
319+
expect(takeEntry("restored-elsewhere").entry).toBeNull();
320+
} finally {
321+
vi.unstubAllGlobals();
322+
}
323+
});
324+
251325
it("ignores an unreadable v1 payload seeded under the current key", () => {
252326
writePromptStashStorageForTest(
253327
JSON.stringify({

‎apps/web/src/promptStashStore.ts‎

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,48 @@ function resolveBaseStorage(): { storage: StateStorage; durable: boolean } {
104104

105105
const { storage: baseStashStorage, durable: storageIsDurable } = resolveBaseStorage();
106106

107+
interface StashEntryImages {
108+
attachments: ReadonlyArray<PersistedComposerImageAttachment>;
109+
droppedImageNames: ReadonlyArray<string>;
110+
unreadableImageNames: ReadonlyArray<string>;
111+
}
112+
113+
/**
114+
* Finalized images storage refused (quota), by entry id. Storage gets the
115+
* entry with those images listed as dropped, so a reload or another tab
116+
* reports them missing; this tab keeps them until it reloads, as the stash
117+
* toast promises.
118+
*/
119+
const unsavedImagesByEntryId = new Map<string, StashEntryImages>();
120+
121+
function withUnsavedImages(entry: PromptStashEntry): PromptStashEntry {
122+
const images = unsavedImagesByEntryId.get(entry.id);
123+
return images ? { ...entry, ...images, pendingImageCount: 0 } : entry;
124+
}
125+
126+
function withoutUnsavedImages(entry: PromptStashEntry): PromptStashEntry {
127+
const images = unsavedImagesByEntryId.get(entry.id);
128+
if (!images) return entry;
129+
return {
130+
...entry,
131+
attachments: [],
132+
droppedImageNames: [
133+
...images.droppedImageNames,
134+
...images.attachments.map((attachment) => attachment.name),
135+
],
136+
unreadableImageNames: images.unreadableImageNames,
137+
pendingImageCount: 0,
138+
};
139+
}
140+
141+
/** Storage's entries as this tab shows them, forgetting images of entries now gone. */
142+
function visibleEntries(entries: ReadonlyArray<PromptStashEntry>): ReadonlyArray<PromptStashEntry> {
143+
for (const entryId of unsavedImagesByEntryId.keys()) {
144+
if (!entries.some((entry) => entry.id === entryId)) unsavedImagesByEntryId.delete(entryId);
145+
}
146+
return unsavedImagesByEntryId.size === 0 ? entries : entries.map(withUnsavedImages);
147+
}
148+
107149
/**
108150
* Persists the queue, immediately rather than debounced. Stashing is a
109151
* deliberate, infrequent keystroke — not a per-character autosave — so there
@@ -119,7 +161,9 @@ function persistEntries(entries: ReadonlyArray<PromptStashEntry>): {
119161
PROMPT_STASH_STORAGE_KEY,
120162
JSON.stringify({
121163
version: PROMPT_STASH_STORAGE_VERSION,
122-
state: { entries },
164+
state: {
165+
entries: unsavedImagesByEntryId.size === 0 ? entries : entries.map(withoutUnsavedImages),
166+
},
123167
}),
124168
);
125169
return { written: true, durable: storageIsDurable };
@@ -184,11 +228,7 @@ interface PromptStashStoreState {
184228
*/
185229
finalizeEntryImages: (
186230
entryId: string,
187-
images: {
188-
attachments: ReadonlyArray<PersistedComposerImageAttachment>;
189-
droppedImageNames: ReadonlyArray<string>;
190-
unreadableImageNames: ReadonlyArray<string>;
191-
},
231+
images: StashEntryImages,
192232
) => { attached: boolean; durable: boolean };
193233
}
194234

@@ -204,16 +244,17 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
204244
if (!written) {
205245
return { evicted: null, written: false, durable: false };
206246
}
207-
set(() => ({ entries: nextEntries }));
247+
set(() => ({ entries: visibleEntries(nextEntries) }));
208248
return { evicted, written: true, durable };
209249
},
210250
takeEntry: (entryId) => {
211251
const entries = latestEntries(get().entries);
212-
const entry = entries.find((candidate) => candidate.id === entryId) ?? null;
213-
if (!entry) return { entry: null, durable: true };
252+
const stored = entries.find((candidate) => candidate.id === entryId);
253+
if (!stored) return { entry: null, durable: true };
254+
const entry = withUnsavedImages(stored);
214255
const nextEntries = entries.filter((candidate) => candidate.id !== entryId);
215256
const { durable } = persistEntries(nextEntries);
216-
set(() => ({ entries: nextEntries }));
257+
set(() => ({ entries: visibleEntries(nextEntries) }));
217258
return { entry, durable };
218259
},
219260
finalizeEntryImages: (entryId, images) => {
@@ -229,8 +270,18 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
229270
unreadableImageNames: images.unreadableImageNames,
230271
pendingImageCount: 0,
231272
};
232-
const { durable } = persistEntries(nextEntries);
233-
set(() => ({ entries: nextEntries }));
273+
const { written, durable } = persistEntries(nextEntries);
274+
if (!written) {
275+
// The smaller write without the images settles the stored entry, so it
276+
// never shows "saving…" again with no encoder left to finish it.
277+
unsavedImagesByEntryId.set(entryId, {
278+
attachments: images.attachments,
279+
droppedImageNames: images.droppedImageNames,
280+
unreadableImageNames: images.unreadableImageNames,
281+
});
282+
persistEntries(nextEntries);
283+
}
284+
set(() => ({ entries: visibleEntries(nextEntries) }));
234285
return { attached: true, durable };
235286
},
236287
}));
@@ -247,7 +298,7 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
247298
if (storageIsDurable && typeof window !== "undefined") {
248299
window.addEventListener("storage", (event) => {
249300
if (event.key !== PROMPT_STASH_STORAGE_KEY && event.key !== null) return;
250-
usePromptStashStore.setState({ entries: readPersistedEntries() ?? [] });
301+
usePromptStashStore.setState({ entries: visibleEntries(readPersistedEntries() ?? []) });
251302
});
252303
}
253304
}

0 commit comments

Comments
 (0)