update message entires fix for race conditions

This commit is contained in:
dal 2025-09-03 09:09:36 -06:00
parent badf379b8f
commit 55e50d124e
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
1 changed files with 8 additions and 10 deletions

View File

@ -63,6 +63,9 @@ export async function updateMessageEntries({
rawLlmMessages: mergedRawLlmMessages,
};
// Update cache immediately (cache is source of truth during streaming)
messageEntriesCache.set(messageId, mergedEntries);
// Build update data
const updateData: Record<string, unknown> = {
updatedAt: new Date().toISOString(),
@ -80,16 +83,11 @@ export async function updateMessageEntries({
updateData.rawLlmMessages = mergedEntries.rawLlmMessages;
}
// Update cache and database concurrently
await Promise.all([
// Update cache immediately (cache is source of truth during streaming)
Promise.resolve(messageEntriesCache.set(messageId, mergedEntries)),
// Update database for persistence
db
.update(messages)
.set(updateData)
.where(and(eq(messages.id, messageId), isNull(messages.deletedAt))),
]);
// Update database for persistence (after cache is updated)
await db
.update(messages)
.set(updateData)
.where(and(eq(messages.id, messageId), isNull(messages.deletedAt)));
return { success: true };
} catch (error) {