@@ -93,11 +93,12 @@ const pendingEntryIds = (entries: ReadonlyArray<PromptStashEntry>) =>
9393 new Set ( entries . filter ( ( entry ) => entry . pendingImageCount ) . map ( ( entry ) => entry . id ) ) ;
9494
9595const 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 */
102103async 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
160160export 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 */
274277function latestEntries ( current : ReadonlyArray < PromptStashEntry > ) : ReadonlyArray < PromptStashEntry > {
275- return readPersistedEntries ( ) ?? current ;
278+ return visibleEntries ( readPersistedEntries ( ) ?? current ) ;
276279}
277280
278281interface PromptStashStoreState {
0 commit comments