The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 16:00 UTC
fix(web): a stale tab no longer erases finalized stashed images (#36574)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 14, 2026 at 09:36 UTC
fd0ffd0b0a43f174c2087ea0dd7a6c4ea07395b0
2 files changed
+119
-24
apps/web/src/promptStashStore.test.ts
+73
@@ -143,6 +143,11 @@ describe("promptStashStore", () => {
143
store.stashEntry({ ...makeEntry({ id: "pending-restore" }), pendingImageCount: 1 });
144
const menuSnapshot = usePromptStashStore.getState().entries[0];
145
expect(menuSnapshot?.attachments).toEqual([]);
146
+ store.stashEntry(makeEntry({ id: "stashed-while-encoding" }));
147
+ expect(
148
+ usePromptStashStore.getState().entries.find((entry) => entry.id === "pending-restore")
149
+ ?.pendingImageCount,
150
+ ).toBe(1);
151
152
store.finalizeEntryImages("pending-restore", {
153
attachments: [
@@ -259,11 +264,79 @@ describe("promptStashStore", () => {
264
"this-tab",
265
"other-tab",
266
]);
267
+
268
+ thisTab.usePromptStashStore.getState().stashEntry({
269
+ ...makeEntry({ id: "encoding-image" }),
270
+ pendingImageCount: 1,
271
+ });
272
+ vi.resetModules();
273
+ const secondTab = await import("./promptStashStore");
274
+ const finalized = makeEntry({ id: "encoding-image", attachmentChars: 12 }).attachments;
275
+ thisTab.usePromptStashStore.getState().finalizeEntryImages("encoding-image", {
276
+ attachments: finalized,
277
+ droppedImageNames: [],
278
+ unreadableImageNames: [],
279
+ });
280
+ secondTab.usePromptStashStore.getState().stashEntry(makeEntry({ id: "second-tab-new" }));
281
+ const afterFinalization = JSON.parse(
282
+ sharedStorage.getItem(PROMPT_STASH_STORAGE_KEY) as string,
283
+ );
284
+ expect(
285
+ afterFinalization.state.entries.find(
286
+ (entry: PromptStashEntry) => entry.id === "encoding-image",
287
+ )?.attachments,
288
+ ).toEqual(finalized);
289
} finally {
290
vi.unstubAllGlobals();
291
vi.resetModules();
292
}
293
});
294
+
295
+ it.each(["restore", "take"] as const)(
296
+ "keeps a failed finalize after another tab stashes, then handles %s",
297
+ async (action) => {
298
+ const storage = createMemoryStorage();
299
+ vi.stubGlobal("localStorage", {
300
+ ...storage,
301
+ setItem: (key: string, value: string) => {
302
+ if (value.includes("rejected-image")) throw new Error("QuotaExceededError");
303
+ storage.setItem(key, value);
304
+ },
305
+ });
306
+ vi.resetModules();
307
+ try {
308
+ const { usePromptStashStore: store } = await import("./promptStashStore");
309
+ store.getState().stashEntry({ ...makeEntry({ id: "too-big" }), pendingImageCount: 1 });
310
+ vi.resetModules();
311
+ const { usePromptStashStore: otherTab } = await import("./promptStashStore");
312
+ const { attachments } = makeEntry({ id: "rejected-image", attachmentChars: 4 });
313
+ const images = { attachments, droppedImageNames: [], unreadableImageNames: [] };
314
+ expect(store.getState().finalizeEntryImages("too-big", images)).toEqual({
315
+ attached: true,
316
+ durable: false,
317
+ });
318
+ otherTab.getState().stashEntry(makeEntry({ id: "unrelated" }));
319
+
320
+ if (action === "take") {
321
+ otherTab.getState().takeEntry("too-big");
322
+ expect(store.getState().finalizeEntryImages("too-big", images)).toEqual({
323
+ attached: false,
324
+ durable: true,
325
+ });
326
+ expect(store.getState().takeEntry("too-big").entry).toBeNull();
327
+ return;
328
+ }
329
+
330
+ expect(store.getState().takeEntry("too-big").entry).toMatchObject({
331
+ attachments,
332
+ unreadableImageNames: [],
333
+ });
334
+ } finally {
335
+ vi.unstubAllGlobals();
336
+ vi.resetModules();
337
+ }
338
+ },
339
+ );
340
});
341
342
describe("prompt stash context records", () => {
apps/web/src/promptStashStore.ts
+46
-24
@@ -37,24 +37,19 @@ type PersistedPromptStashState = typeof PersistedPromptStashState.Type;
37
const decodePersistedPromptStashState = Schema.decodeUnknownSync(PersistedPromptStashState);
38
39
// Encoders cannot survive reload; clear orphaned counts so entries remain restorable.
40
-function clearOrphanedPendingImages(
41
- entries: ReadonlyArray<PromptStashEntry>,
42
-): ReadonlyArray<PromptStashEntry> {
43
- return entries.map((entry) => {
44
- if (!entry.pendingImageCount) return entry;
45
- const lostCount = entry.pendingImageCount;
46
- return {
47
- ...entry,
48
- pendingImageCount: 0,
49
- unreadableImageNames: [
50
- ...(entry.unreadableImageNames ?? []),
51
- ...Array.from(
52
- { length: lostCount },
53
- (_, index) => `image ${index + 1} (not saved before reload)`,
54
- ),
55
- ],
56
- };
57
- });
40
+function clearOrphanedPendingImages(entry: PromptStashEntry): PromptStashEntry {
41
+ if (!entry.pendingImageCount) return entry;
42
+ return {
43
+ ...entry,
44
+ pendingImageCount: 0,
45
+ unreadableImageNames: [
46
+ ...(entry.unreadableImageNames ?? []),
47
+ ...Array.from(
48
+ { length: entry.pendingImageCount },
49
+ (_, index) => `image ${index + 1} (not saved before reload)`,
50
+ ),
51
+ ],
52
+ };
53
}
54
55
export function partitionStashAttachments(
@@ -89,6 +84,8 @@ function resolveBaseStorage(): { storage: StateStorage; durable: boolean } {
84
85
const { storage: baseStashStorage, durable: storageIsDurable } = resolveBaseStorage();
86
87
+const unpersistedFinalizedEntryIds = new Set<string>();
88
+
89
// Clear the composer only after a durable write; debounce cannot confirm durability.
90
function persistEntries(entries: ReadonlyArray<PromptStashEntry>): {
91
written: boolean;
@@ -102,6 +99,7 @@ function persistEntries(entries: ReadonlyArray<PromptStashEntry>): {
99
state: { entries },
100
}),
101
);
102
+ unpersistedFinalizedEntryIds.clear();
103
return { written: true, durable: storageIsDurable };
104
} catch (error) {
105
console.error("[PROMPT-STASH] Could not persist stash (storage quota?).", error);
@@ -109,25 +107,47 @@ function persistEntries(entries: ReadonlyArray<PromptStashEntry>): {
107
}
108
}
109
112
-function readPersistedEntries(): ReadonlyArray<PromptStashEntry> | null {
110
+function readStoredEntries(): ReadonlyArray<PromptStashEntry> | null {
111
try {
112
const raw = baseStashStorage.getItem(PROMPT_STASH_STORAGE_KEY);
113
if (typeof raw !== "string" || raw.length === 0) return null;
114
const parsed: unknown = JSON.parse(raw);
115
const state = (parsed as { state?: unknown } | null)?.state;
116
if (!state) return null;
119
- return clearOrphanedPendingImages(decodePersistedPromptStashState(state).entries);
117
+ return decodePersistedPromptStashState(state).entries;
118
} catch {
119
return null;
120
}
121
}
122
125
-/** Starts each write from storage so entries other tabs stashed or took are not overwritten. */
123
+function readPersistedEntries(): ReadonlyArray<PromptStashEntry> | null {
124
+ return readStoredEntries()?.map(clearOrphanedPendingImages) ?? null;
125
+}
126
+
127
+/**
128
+ * Starts each write from storage so other tabs' stashes, takes and finalized images survive.
129
+ * Local encoding and failed finalize writes retain ownership while the stored entry exists.
130
+ */
131
function latestEntries(memory: ReadonlyArray<PromptStashEntry>): ReadonlyArray<PromptStashEntry> {
127
- const stored = readPersistedEntries();
132
+ const stored = readStoredEntries();
133
if (!stored) return memory;
134
+ const storedIds = new Set(stored.map((entry) => entry.id));
135
+ for (const id of unpersistedFinalizedEntryIds) {
136
+ if (!storedIds.has(id)) unpersistedFinalizedEntryIds.delete(id);
137
+ }
138
const memoryById = new Map(memory.map((entry) => [entry.id, entry]));
130
- return stored.map((entry) => memoryById.get(entry.id) ?? entry);
139
+ return stored.map((entry) => {
140
+ const local = memoryById.get(entry.id);
141
+ if (
142
+ local &&
143
+ (local.pendingImageCount ||
144
+ entry.pendingImageCount ||
145
+ unpersistedFinalizedEntryIds.has(entry.id))
146
+ ) {
147
+ return local;
148
+ }
149
+ return clearOrphanedPendingImages(entry);
150
+ });
151
}
152
153
interface PromptStashStoreState {
@@ -171,6 +191,7 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
191
if (!entry) return { entry: null, durable: true };
192
const nextEntries = entries.filter((candidate) => candidate.id !== entryId);
193
const { durable } = persistEntries(nextEntries);
194
+ unpersistedFinalizedEntryIds.delete(entryId);
195
set(() => ({ entries: nextEntries }));
196
return { entry, durable };
197
},
@@ -187,7 +208,8 @@ export const usePromptStashStore = create<PromptStashStoreState>()((set, get) =>
208
unreadableImageNames: images.unreadableImageNames,
209
pendingImageCount: 0,
210
};
190
- const { durable } = persistEntries(nextEntries);
211
+ const { written, durable } = persistEntries(nextEntries);
212
+ if (!written) unpersistedFinalizedEntryIds.add(entryId);
213
set(() => ({ entries: nextEntries }));
214
return { attached: true, durable };
215
},