From d3b5ca451b63bc53b628fd11f22d5a3489e92656 Mon Sep 17 00:00:00 2001 From: jubnl Date: Tue, 14 Apr 2026 23:16:32 +0200 Subject: [PATCH] fix(sync): monotonic createdAt in mutationQueue to prevent FIFO race on fast CI --- client/src/sync/mutationQueue.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/client/src/sync/mutationQueue.ts b/client/src/sync/mutationQueue.ts index 8ad6e643..0b68826c 100644 --- a/client/src/sync/mutationQueue.ts +++ b/client/src/sync/mutationQueue.ts @@ -36,6 +36,9 @@ export function generateUUID(): string { } let _flushing = false +// Monotonically increasing timestamp so same-millisecond enqueues +// still get a deterministic FIFO order when sorted by createdAt. +let _lastTs = 0 export const mutationQueue = { /** @@ -45,11 +48,13 @@ export const mutationQueue = { async enqueue( mutation: Omit, ): Promise { + const now = Date.now() + _lastTs = now > _lastTs ? now : _lastTs + 1 const item: QueuedMutation = { ...mutation, status: 'pending', attempts: 0, - createdAt: Date.now(), + createdAt: _lastTs, lastError: null, } await offlineDb.mutationQueue.put(item) @@ -155,8 +160,9 @@ export const mutationQueue = { .count() }, - /** Reset internal flushing flag — useful in tests. */ + /** Reset internal flushing flag and timestamp counter — useful in tests. */ _resetFlushing(): void { _flushing = false + _lastTs = 0 }, }