git » sdk » commit 309d938

Never start a sync window at a franctional sortId

author Stephen Paul Weber
2026-11-10 02:48:00 UTC
committer Stephen Paul Weber
2026-11-10 20:29:12 UTC
parent 4e36eb37254decd0df506338bab2f6b389ac2fb2

Never start a sync window at a franctional sortId

Instead, advance to the next short/"integer" sortId as the bottom of the
window to keep ids shorter.

borogove/Chat.hx +2 -4
test/TestSortId.hx +104 -0

diff --git a/borogove/Chat.hx b/borogove/Chat.hx
index 440e624..2bca0fd 100644
--- a/borogove/Chat.hx
+++ b/borogove/Chat.hx
@@ -2024,10 +2024,8 @@ class Channel extends Chat {
 		}
 
 		// Sort into a window so live messages arriving concurrently can come after
-		final sortFrom = sortA ?? syncPoint?.sortId;
-		final sortNext = FractionalIndexing.between(sortFrom, null, FractionalIndexing.BASE_95_DIGITS);
-		final sortIdNext = FractionalIndexing.between(sortId ?? sortNext, null, FractionalIndexing.BASE_95_DIGITS);
-		final sortTo = sortNext < sortIdNext ? sortNext : sortIdNext;
+		final sortFrom = FractionalIndexing.between(sortA ?? syncPoint?.sortId, null, FractionalIndexing.BASE_95_DIGITS);
+		final sortTo = FractionalIndexing.between(sortFrom, null, FractionalIndexing.BASE_95_DIGITS);
 		if (sortId == null || sortId <= sortTo) sortId = FractionalIndexing.between(sortTo, null, FractionalIndexing.BASE_95_DIGITS);
 
 		var threeDaysAgo = Date.format(
diff --git a/test/TestSortId.hx b/test/TestSortId.hx
index 79c8427..5dcc47f 100644
--- a/test/TestSortId.hx
+++ b/test/TestSortId.hx
@@ -361,4 +361,108 @@ class TestSortId extends utest.Test {
 			);
 		});
 	}
+
+	public function testChannelSyncBatchesKeepSortIdsShort(async: Async) {
+		final persistence = new Dummy();
+		final client = new Client("test@example.com", persistence);
+		final disco = new Caps("", [], ["http://jabber.org/protocol/muc", "urn:xmpp:mam:2"], []);
+		final channel = new Channel(client, client.stream, persistence, "batchsync@example.com", Open, false, false, null, null, null, disco);
+		client.chats.push(channel);
+
+		final batchCount = 12;
+		final messagesPerBatch = 20;
+		final maxAllowedLength = 12;
+
+		var currentBatch = 0;
+		var completedBatches = 0;
+		var maxSortIdLength = 0;
+		var previousSortId: Null<String> = null;
+		var lastSyncedMessage: Null<ChatMessage> = null;
+		var syncPoint: Null<ChatMessage> = null;
+		var pendingIqId: Null<String> = null;
+		var pendingQueryId: Null<String> = null;
+
+		var startBatch:() -> Void = null;
+		var feedBatch:() -> Void = null;
+
+		client.addSyncMessageListener((message) -> {
+			final sortId = message.sortId;
+			if (sortId == null) return;
+
+			if (previousSortId != null) {
+				Assert.isTrue(previousSortId < sortId, 'sync sortIds should keep increasing ($previousSortId < $sortId)');
+			}
+			previousSortId = sortId;
+			if (sortId.length > maxSortIdLength) maxSortIdLength = sortId.length;
+			lastSyncedMessage = message;
+		});
+
+		client.on("chats/update", (chats) -> {
+			if (channel.sync != null) return EventHandled;
+
+			completedBatches++;
+			if (completedBatches >= batchCount) {
+				Assert.isTrue(maxSortIdLength <= maxAllowedLength, 'max sortId length $maxSortIdLength should be <= $maxAllowedLength after $batchCount sync batches');
+				async.done();
+				return EventHandled;
+			}
+
+			Assert.notNull(lastSyncedMessage, "Batch should have synced at least one message");
+			syncPoint = lastSyncedMessage;
+			lastSyncedMessage = null;
+			startBatch();
+			return EventHandled;
+		});
+
+		client.stream.on("sendStanza", (stanza: Stanza) -> {
+			if (stanza.name != "iq") return EventUnhandled;
+			final query = stanza.findChild("{urn:xmpp:mam:2}query");
+			if (query == null) return EventUnhandled;
+
+			pendingIqId = stanza.attr.get("id");
+			pendingQueryId = query.attr.get("queryid");
+			haxe.Timer.delay(() -> feedBatch(), 0);
+			return EventHandled;
+		});
+
+		startBatch = function() {
+			if (currentBatch >= batchCount) return;
+			currentBatch++;
+			channel.doSync(syncPoint);
+		};
+
+		feedBatch = function() {
+			final iqId = pendingIqId;
+			final queryId = pendingQueryId;
+			if (iqId == null || queryId == null) {
+				Assert.fail("MAM query was not sent");
+				return;
+			}
+
+			for (i in 0...messagesPerBatch) {
+				final serverId = 'batch${currentBatch}-msg$i';
+				client.stream.onStanza(new Stanza("message", { from: channel.chatId, xmlns: "jabber:client" })
+					.tag("result", { xmlns: "urn:xmpp:mam:2", queryid: queryId, id: serverId })
+						.tag("forwarded", { xmlns: "urn:xmpp:forward:0" })
+							.tag("delay", { xmlns: "urn:xmpp:delay", stamp: "2023-01-01T00:00:00Z" }).up()
+							.tag("message", { from: channel.chatId + "/user", to: "test@example.com", id: serverId, xmlns: "jabber:client" })
+								.textTag("body", 'message $serverId')
+							.up()
+						.up()
+					.up()
+				);
+			}
+
+			client.stream.onStanza(new Stanza("iq", { type: "result", id: iqId, from: channel.chatId, xmlns: "jabber:client" })
+				.tag("fin", { xmlns: "urn:xmpp:mam:2", complete: "true" })
+					.tag("set", { xmlns: "http://jabber.org/protocol/rsm" })
+						.textTag("first", 'batch${currentBatch}-msg0')
+						.textTag("last", 'batch${currentBatch}-msg${messagesPerBatch - 1}')
+					.up()
+				.up()
+			);
+		};
+
+		startBatch();
+	}
 }