Skip to content

Build your personal knowledge base with Trilium Notes

feat(setup): resume sync progress percentage across restart

Elian Doran2ce4a6a03fbb
diff --git a/apps/client/src/setup.tsx b/apps/client/src/setup.tsx
index 5b4398e939..40184a249e 100644
--- a/apps/client/src/setup.tsx
+++ b/apps/client/src/setup.tsx
@@ -247,11 +247,14 @@ function SyncInProgress({ device }: { device: "server" | "desktop" }) {
const currentIndex = steps.findIndex((s) => s.key === step);
const syncingDone = currentIndex > steps.findIndex((s) => s.key === "syncing");
+ // Pulled-so-far, clamped: the remote can gain changes mid-sync, briefly pushing the
+ // outstanding count above the frozen total, which would otherwise show a negative bar.
+ const pulled = stats.totalPullCount ? Math.max(0, stats.totalPullCount - stats.outstandingPullCount) : 0;
let progress = 0;
if (syncingDone) {
progress = 100;
} else if (stats.totalPullCount) {
- progress = Math.round(((stats.totalPullCount - stats.outstandingPullCount) / stats.totalPullCount) * 100);
+ progress = Math.min(100, Math.round((pulled / stats.totalPullCount) * 100));
}
return (
@@ -267,7 +270,7 @@ function SyncInProgress({ device }: { device: "server" | "desktop" }) {
{s.label}
{s.key === "syncing" && (
<div class="sync-progress">
- <progress value={syncingDone ? 1 : stats.totalPullCount! - stats.outstandingPullCount} max={syncingDone ? 1 : stats.totalPullCount!} />
+ <progress value={syncingDone ? 1 : pulled} max={syncingDone ? 1 : (stats.totalPullCount ?? 1)} />
<span>{progress}%</span>
</div>
)}
diff --git a/packages/trilium-core/src/services/bootstrap_utils.ts b/packages/trilium-core/src/services/bootstrap_utils.ts
index 0aab17aafe..2537c3142c 100644
--- a/packages/trilium-core/src/services/bootstrap_utils.ts
+++ b/packages/trilium-core/src/services/bootstrap_utils.ts
@@ -23,6 +23,10 @@ export default function getSharedBootstrapItems(assetPath: string, dbInitialized
layoutOrientation: "vertical" as const,
headingStyle: "plain" as const,
componentId: "",
+ // Schema present but not yet initialized means a sync was started and interrupted, so the
+ // setup screen resumes it instead of starting over. A fully initialized DB is never mid-sync.
+ // Kept in the common items so both payload shapes carry the flag uniformly.
+ syncInProgress: !dbInitialized && sqlInit.schemaExists(),
...getIconConfig(assetPath)
};
@@ -30,9 +34,6 @@ export default function getSharedBootstrapItems(assetPath: string, dbInitialized
if (!dbInitialized) {
return {
...commonItems,
- // Schema already present but not yet initialized means a sync was started
- // and interrupted, so the setup screen resumes it instead of starting over.
- syncInProgress: sqlInit.schemaExists(),
theme: "next",
themeCssUrl: false as const,
themeUseNextAsBase: "next" as const,
diff --git a/packages/trilium-core/src/services/sync.spec.ts b/packages/trilium-core/src/services/sync.spec.ts
index ff214f75a4..9565471726 100644
--- a/packages/trilium-core/src/services/sync.spec.ts
+++ b/packages/trilium-core/src/services/sync.spec.ts
@@ -31,6 +31,7 @@ interface FakeConfig {
changed?: ChangesResponse[];
check?: CheckResponse[];
onCheck?: (callIndex: number) => void;
+ onChanged?: (callIndex: number) => void;
}
let config: FakeConfig = {};
@@ -50,6 +51,7 @@ const fakeRequest: RequestProvider = {
return reply(config.login ?? { instanceId: "REMOTE_INSTANCE", maxEntityChangeId: 0 });
}
if (url.includes("/api/sync/changed")) {
+ config.onChanged?.(changedIdx);
const seq = config.changed ?? [{ entityChanges: [], lastEntityChangeId: 0, outstandingPullCount: 0 }];
return reply(seq[Math.min(changedIdx++, seq.length - 1)]);
}
@@ -113,6 +115,31 @@ describe("sync service", () => {
expect(urls.some((u) => u.includes("/api/sync/check"))).toBe(true);
});
+ it("counts already-pulled local changes into the pull total so a resumed sync keeps the grand total", async () => {
+ // On resume the remote only reports the *remaining* changes; the already-pulled ones live in
+ // the local entity_changes table. The total must be their sum, otherwise the setup progress
+ // bar rescales the leftover work to 0-100% and appears to restart from zero.
+ const countSynced = () => getSql().getValue<number>("SELECT COUNT(1) FROM entity_changes WHERE isSynced = 1") ?? 0;
+
+ let alreadyPulled = 0;
+ let capturedTotal: number | null = null;
+ config.onChanged = (callIndex) => {
+ // callIndex 0: same moment the sync samples the already-pulled count.
+ if (callIndex === 0) alreadyPulled = countSynced();
+ // callIndex 1: the total has now been frozen for this session — read it back.
+ if (callIndex === 1) capturedTotal = syncService.getTotalPullCount();
+ };
+ config.changed = [
+ { entityChanges: [pulledOptionChange()], lastEntityChangeId: 9, outstandingPullCount: 5 },
+ { entityChanges: [], lastEntityChangeId: 9, outstandingPullCount: 0 }
+ ];
+
+ await runSync();
+
+ // grand total = already-pulled (local) + this batch (1) + remaining reported by the remote (5)
+ expect(capturedTotal).toBe(alreadyPulled + 1 + 5);
+ });
+
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
diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.ts
index 1fb47a39fc..1f36b3635d 100644
--- a/packages/trilium-core/src/services/sync.ts
+++ b/packages/trilium-core/src/services/sync.ts
@@ -203,7 +203,12 @@ async function pullChanges(syncContext: SyncContext) {
}
if (totalPullCount === null) {
- totalPullCount = resp.entityChanges.length + outstandingPullCount;
+ // Keep the denominator as the *grand* total so the setup progress bar doesn't reset
+ // to 0% after a restart. Already-pulled changes persist in the local entity_changes
+ // table, so adding them to the remaining count reconstructs the original total —
+ // a resumed sync would otherwise only see the leftover work and rescale from zero.
+ const alreadyPulled = getSql().getValue<number>("SELECT COUNT(1) FROM entity_changes WHERE isSynced = 1") ?? 0;
+ totalPullCount = alreadyPulled + resp.entityChanges.length + outstandingPullCount;
}
if (resp.entityChanges.length === 0) {