Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(sync): honor declared isSynced when auto-creating a missing option
Elian Doran1924359e7d39
diff --git a/packages/trilium-core/src/services/options.spec.ts b/packages/trilium-core/src/services/options.spec.tsindex 6ddaa3de64..598ad21b12 100644--- a/packages/trilium-core/src/services/options.spec.ts+++ b/packages/trilium-core/src/services/options.spec.ts@@ -115,7 +115,7 @@ describe("options service (real DB)", () => {expect(readFromDb(name)).toBe("changed");});- it("creates the option (local-only) when it does not yet exist", () => {+ it("creates an unknown option as local-only when it does not yet exist", () => {const name = uniqueName();getContext().init(() => optionService.setOption(name as any, "created-by-set"));@@ -124,6 +124,27 @@ describe("options service (real DB)", () => {expect(becca.getOption(name)?.isSynced).toBe(false);expect(readFromDb(name)).toBe("created-by-set");});++ it("auto-creates a missing known option using its declared sync flag", () => {+ // searchEnableFuzzyMatching is declared as a synced option in defaultOptions.+ // Simulate the option being absent (e.g. a write landing before initStartupOptions)+ // so setOption falls through to its create branch.+ const name = "searchEnableFuzzyMatching";+ getContext().init(() => {+ getSql().execute("DELETE FROM options WHERE name = ?", [name]);+ delete becca.options[name];+ });++ getContext().init(() => optionService.setOption(name as any, "true"));++ // Must be recreated as synced, not silently downgraded to local-only.+ expect(becca.getOption(name)?.isSynced).toBe(true);+ const row = getSql().getRowOrNull<{ isSynced: number }>(+ "SELECT isSynced FROM options WHERE name = ?",+ [name]+ );+ expect(row?.isSynced).toBe(1);+ });});describe("getOptions / getOptionMap", () => {diff --git a/packages/trilium-core/src/services/options.ts b/packages/trilium-core/src/services/options.tsindex fdfe111d95..3e36a26a7c 100644--- a/packages/trilium-core/src/services/options.ts+++ b/packages/trilium-core/src/services/options.ts@@ -16,6 +16,7 @@ import becca from "../becca/becca.js";import BOption from "../becca/entities/boption.js";import type { OptionRow } from "@triliumnext/commons";import type { FilterOptionsByType, OptionDefinitions, OptionMap, OptionNames } from "@triliumnext/commons";+import { getDefaultOptionSyncedFlag } from "./options_init.js";import { getSql } from "./sql/index.js";function getOptionOrNull(name: OptionNames): string | null {@@ -80,7 +81,10 @@ function setOption<T extends OptionNames>(name: T, value: string | OptionDefinitoption.save();} else {- createOption(name, value, false);+ // The option row is missing (e.g. a write landing before initStartupOptions has created it).+ // Honor the option's declared sync flag so a synced option is not permanently created as+ // local-only, which would otherwise cause an unreconcilable sync content-hash mismatch.+ createOption(name, value, getDefaultOptionSyncedFlag(name) ?? false);}}diff --git a/packages/trilium-core/src/services/options_init.spec.ts b/packages/trilium-core/src/services/options_init.spec.tsindex ddad092446..d4e888670d 100644--- a/packages/trilium-core/src/services/options_init.spec.ts+++ b/packages/trilium-core/src/services/options_init.spec.ts@@ -1,5 +1,5 @@import { describe, expect, it } from "vitest";-import { migrateSyncTimeoutFromMilliseconds } from "./options_init.js";+import { getDefaultOptionSyncedFlag, migrateSyncTimeoutFromMilliseconds } from "./options_init.js";describe("migrateSyncTimeoutFromMilliseconds", () => {it("returns null when no migration is needed (values < 1000 are already in seconds)", () => {@@ -25,3 +25,21 @@ describe("migrateSyncTimeoutFromMilliseconds", () => {expect(migrateSyncTimeoutFromMilliseconds(120500)).toEqual({ value: 121, scale: 1 });});});++describe("getDefaultOptionSyncedFlag", () => {+ it("returns true for an option declared as synced", () => {+ expect(getDefaultOptionSyncedFlag("seenCallToActions")).toBe(true);+ expect(getDefaultOptionSyncedFlag("imageMaxWidthHeight")).toBe(true);+ });++ it("returns false for an option declared as local-only", () => {+ expect(getDefaultOptionSyncedFlag("zoomFactor")).toBe(false);+ expect(getDefaultOptionSyncedFlag("overrideThemeFonts")).toBe(false);+ });++ it("returns undefined for an option that is not part of the default definitions", () => {+ // `theme` is initialized in initNotSyncedOptions, not in the defaultOptions table.+ expect(getDefaultOptionSyncedFlag("theme")).toBeUndefined();+ expect(getDefaultOptionSyncedFlag("doesNotExistOption" as never)).toBeUndefined();+ });+});diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.tsindex 3029862db2..04920707f1 100644--- a/packages/trilium-core/src/services/options_init.ts+++ b/packages/trilium-core/src/services/options_init.ts@@ -324,3 +324,24 @@ function getKeyboardDefaultOptions() {})) as DefaultOption[];}+let syncedFlagByOption: Map<OptionNames, boolean> | null = null;++/**+ * Resolves the declared `isSynced` flag for one of the well-known default options.+ *+ * Returns `undefined` for names that are not part of {@link defaultOptions} (e.g. options created+ * directly in {@link initNotSyncedOptions}/{@link initDocumentOptions}, keyboard shortcuts, or unknown+ * names). Used by `setOption` so that auto-creating a missing default does not silently downgrade a+ * meant-to-be-synced option to local-only, which would otherwise produce an unreconcilable sync hash.+ *+ * The lookup is built lazily on first use to avoid evaluating the (cyclic) options module graph at+ * import time.+ */+export function getDefaultOptionSyncedFlag(name: OptionNames): boolean | undefined {+ if (!syncedFlagByOption) {+ syncedFlagByOption = new Map(defaultOptions.map((option) => [option.name, option.isSynced]));+ }++ return syncedFlagByOption.get(name);+}+