Skip to content

Build your personal knowledge base with Trilium Notes

fix(sync): persist advanced pull cursor on an empty change batch

Elian Doranda758e38aefd
diff --git a/packages/trilium-core/src/services/sync.spec.ts b/packages/trilium-core/src/services/sync.spec.ts
index e49447625f..ff214f75a4 100644
--- a/packages/trilium-core/src/services/sync.spec.ts
+++ b/packages/trilium-core/src/services/sync.spec.ts
@@ -113,6 +113,19 @@ describe("sync service", () => {
expect(urls.some((u) => u.includes("/api/sync/check"))).toBe(true);
});
+ it("persists an advanced cursor even when the server returns an empty change batch", async () => {
+ // The server may skip past changes owned by this instance and return no entity changes but an
+ // advanced lastEntityChangeId. The client must persist that advance, otherwise every subsequent
+ // sync re-requests (and the server re-scans) the same skipped range.
+ config.login = { instanceId: "REMOTE_INSTANCE", maxEntityChangeId: 100 }; // don't lower our cursor on login
+ config.changed = [{ entityChanges: [], lastEntityChangeId: 20, outstandingPullCount: 0 }];
+ cls.init(() => options.setOption("lastSyncedPull", "5"));
+
+ await runSync();
+
+ expect(Number(options.getOption("lastSyncedPull"))).toBe(20);
+ });
+
it("re-runs the content check while the server reports outstanding pulls", async () => {
config.check = [
{ maxEntityChangeId: 999_999_999, entityHashes: {} }, // lastSyncedPull < max -> loop again
diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.ts
index d94e55d5f7..6d7be8d923 100644
--- a/packages/trilium-core/src/services/sync.ts
+++ b/packages/trilium-core/src/services/sync.ts
@@ -189,6 +189,12 @@ async function pullChanges(syncContext: SyncContext) {
outstandingPullCount = resp.outstandingPullCount;
+ // Advance the cursor to whatever the server has processed — even when it returns no entity
+ // changes, since it may have skipped past changes owned by this instance. Capturing it here
+ // (before the empty-response break) lets the apply phase persist the advance, so we don't
+ // re-request the skipped range on every subsequent sync.
+ cursor = resp.lastEntityChangeId;
+
const hasPendingChanges = resp.entityChanges.length > 0 || outstandingPullCount > 0;
if (hasPendingChanges && optionService.getOptionOrNull("syncIncomplete") !== "true") {
optionService.setOption("syncIncomplete", "true");
@@ -206,12 +212,14 @@ async function pullChanges(syncContext: SyncContext) {
}
batch.push(resp);
- cursor = resp.lastEntityChangeId;
batchBytes += estimatePullResponseBytes(resp);
} while (batchBytes < maxBatchBytes);
- if (batch.length === 0) {
- break; // nothing left to pull
+ // Nothing to apply and the cursor didn't move → fully caught up. If the cursor DID advance (the
+ // server skipped this instance's own changes and returned nothing), fall through so the apply
+ // phase persists the advance.
+ if (batch.length === 0 && getLastSyncedPull() === cursor) {
+ break;
}
// Apply phase: all fetched chunks in a single transaction.
@@ -232,9 +240,11 @@ async function pullChanges(syncContext: SyncContext) {
}
});
- log.info(
- `Sync: pulled ${batch.length} chunk(s) (${batchChanges} changes, ~${Math.round(batchBytes / 1_048_576)} MB) in ${applyStart - fetchStart}ms and applied them in ${Date.now() - applyStart}ms, ${outstandingPullCount} outstanding pulls`
- );
+ if (batch.length > 0) {
+ log.info(
+ `Sync: pulled ${batch.length} chunk(s) (${batchChanges} changes, ~${Math.round(batchBytes / 1_048_576)} MB) in ${applyStart - fetchStart}ms and applied them in ${Date.now() - applyStart}ms, ${outstandingPullCount} outstanding pulls`
+ );
+ }
if (noMoreChanges) {
break;