From dc7e53b1f2bad2d7677a3eae496dbfc9f72c971f Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Thu, 13 Aug 2026 22:17:23 +0200 Subject: [PATCH] fix(sliding-sync): preserve local echo server order --- src/client/slidingSync.ts | 5 ++- .../slidingSyncTimelineReconciliation.test.ts | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/client/slidingSync.ts b/src/client/slidingSync.ts index aea104cf1..3fac8b2ff 100644 --- a/src/client/slidingSync.ts +++ b/src/client/slidingSync.ts @@ -400,7 +400,10 @@ export const prepareSlidingSyncTimelines = ( if (shouldMarkLimited) timelineData.limited = true; - const isGapped = firstKnownResponseIndex < 0 || hasSparseOverlap; + // The SDK only appends a non-duplicate event from an overlapping response. + // Reset when the server window starts before its first known event so those + // earlier events are not incorrectly placed at the live tail. + const isGapped = firstKnownResponseIndex !== 0; if ( knownEventIds.size > 0 && room && diff --git a/src/client/slidingSyncTimelineReconciliation.test.ts b/src/client/slidingSyncTimelineReconciliation.test.ts index 7ea3284ba..bd5656de8 100644 --- a/src/client/slidingSyncTimelineReconciliation.test.ts +++ b/src/client/slidingSyncTimelineReconciliation.test.ts @@ -358,6 +358,43 @@ describe('timeline reconciliation in matrix-js-sdk', () => { expect(localEcho.status).toBeNull(); }); + it('places a remote echo before an overlapping newer event', async () => { + const { mx, deliver } = makeSdk(); + await deliver(roomId, initialRoomData(message('$e6', 600))); + const room = mx.getRoom(roomId)!; + const localEcho = new MatrixEvent({ + ...message('~local', 500), + room_id: roomId, + sender: userId, + }); + localEcho.setStatus(EventStatus.SENDING); + room.addPendingEvent(localEcho, 'txn'); + + const resp = { + required_state: [], + timeline: [ + { + ...message('$mine', 500), + sender: userId, + unsigned: { transaction_id: 'txn' }, + }, + message('$e6', 600), + ], + limited: true, + prev_batch: 'gap-token', + } as unknown as MSC3575RoomData; + const completeTimelineReset = prepareSlidingSyncTimelines( + { + rooms: { [roomId]: resp }, + } as unknown as MSC3575SlidingSyncResponse, + mx + ); + await deliver(roomId, resp); + completeTimelineReset?.(); + + expect(timelineIds(mx)).toEqual(['$mine', '$e6']); + }); + it('starts a new live timeline when an expanded catch-up has no overlap', async () => { const { mx, deliver } = makeSdk(); await deliver(roomId, initialRoomData(newest));