Skip to content

Commit b6aa8ec

Browse files
t3dotggclaude
andcommitted
fix(web): distinguish memory-only stash writes from failed ones
With localStorage blocked, the store's in-memory fallback accepts the write but reported it identically to a quota rejection, so the composer showed a false error while the entry sat visible in the stash. stashEntry now returns written and durable separately; the composer clears on any successful write and warns when it is session-only. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 6569fe6 commit b6aa8ec

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
20732073
// mid-encode still leaves the prompt recoverable, while clearing before
20742074
// the async image work means edits typed during encoding are not wiped.
20752075
// Images are appended to the stored entry as they finish encoding.
2076-
const { evicted, durable } = stashEntryToQueue({
2076+
const { evicted, written, durable } = stashEntryToQueue({
20772077
id: entryId,
20782078
createdAt: new Date().toISOString(),
20792079
prompt,
@@ -2083,11 +2083,11 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
20832083
pendingImageCount: images.length,
20842084
});
20852085

2086-
// Clearing the composer is only safe once the entry is durable. If the
2087-
// write was rejected (quota, blocked storage) the store has already
2088-
// rolled itself back, so leave the composer untouched rather than
2089-
// making it the second casualty of a reload.
2090-
if (!durable) {
2086+
// Clearing the composer is only safe once the write actually landed.
2087+
// If it was rejected (quota) the store has already rolled itself back,
2088+
// so leave the composer untouched rather than making it the second
2089+
// casualty of a reload.
2090+
if (!written) {
20912091
toastManager.add({
20922092
type: "error",
20932093
title: "Could not stash this prompt",
@@ -2097,6 +2097,18 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
20972097
});
20982098
return;
20992099
}
2100+
// Written but only into the in-memory fallback (localStorage blocked):
2101+
// the entry is visible and restorable this session, so proceed with the
2102+
// clear, but say it won't survive a reload.
2103+
if (!durable) {
2104+
toastManager.add({
2105+
type: "warning",
2106+
title: "Stashed prompt will not survive a reload",
2107+
description:
2108+
"Browser storage is unavailable, so this stash is kept in memory only for this session.",
2109+
data: { hideCopyButton: true },
2110+
});
2111+
}
21002112

21012113
// Only the prompt and images are cleared — terminal/element contexts,
21022114
// preview annotations, and review comments are not stashable, so

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ describe("promptStashStore", () => {
114114
expect(entries[0]?.id).toBe("overflow");
115115
});
116116

117+
// This test environment has no `localStorage`, so the store runs on its
118+
// in-memory fallback — the exact "kept for this session, gone on reload"
119+
// case the composer must distinguish from an outright write failure.
120+
it("distinguishes a memory-only write (written, not durable) from a failed one", () => {
121+
const store = usePromptStashStore.getState();
122+
const result = store.stashEntry(makeEntry({ id: "memory-only" }));
123+
expect(result.written).toBe(true);
124+
expect(result.durable).toBe(false);
125+
expect(usePromptStashStore.getState().entries.map((entry) => entry.id)).toEqual([
126+
"memory-only",
127+
]);
128+
});
129+
117130
it("takeEntry removes and returns the entry; second take returns null", () => {
118131
const store = usePromptStashStore.getState();
119132
store.stashEntry(makeEntry({ id: "keep" }));

‎apps/web/src/promptStashStore.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,12 @@ interface PromptStashStoreState {
193193
*/
194194
stashEntry: (entry: PromptStashEntry) => {
195195
evicted: PromptStashEntry | null;
196-
/** False when the write did not reach durable storage; nothing was kept. */
196+
/** False when the write failed outright (e.g. quota); nothing was kept. */
197+
written: boolean;
198+
/**
199+
* False when the write will not survive a reload: either it failed, or it
200+
* landed only in the in-memory fallback because localStorage is blocked.
201+
*/
197202
durable: boolean;
198203
};
199204
/**
@@ -228,10 +233,10 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
228233
// keeps the composer intact on failure, so a stashed copy would
229234
// duplicate the prompt. Eviction likewise only sticks on success.
230235
if (!written) {
231-
return { evicted: null, durable: false };
236+
return { evicted: null, written: false, durable: false };
232237
}
233238
set(() => ({ entries: nextEntries }));
234-
return { evicted, durable };
239+
return { evicted, written: true, durable };
235240
},
236241
takeEntry: (entryId) => {
237242
const entries = get().entries;

0 commit comments

Comments
 (0)