An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Resolve sync checkout save and refresh races https://github.com/siyuan-note/siyuan/issues/20763
Daniel1eceea2d4c0e
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.tsindex 523f5112fb..74a3d3ccae 100644--- a/app/src/protyle/index.ts+++ b/app/src/protyle/index.ts@@ -9,6 +9,7 @@ import {LocalUndo, Undo} from "./undo";import {Upload} from "./upload";import {Options} from "./util/Options";import {destroy} from "./util/destroy";+import {registerSyncEditorSave} from "./util/editorSave";import {Scroll} from "./scroll";import {Model} from "../layout/Model";import {genUUID} from "../util/genID";@@ -174,6 +175,7 @@ export class Protyle {}};registerProtyleRuntimeCapabilities(this.protyle, runtimeCapabilities);+ registerSyncEditorSave(this.protyle);if (runtimeCapabilities.upload === false) {disableProtyleUpload(this.protyle);}diff --git a/app/src/protyle/util/destroy.ts b/app/src/protyle/util/destroy.tsindex 735c99b8bf..198652e220 100644--- a/app/src/protyle/util/destroy.ts+++ b/app/src/protyle/util/destroy.ts@@ -14,11 +14,14 @@ import {areProtylePluginExtensionsEnabled} from "../runtimeCapabilities";import {invalidateFocusFoldRequests} from "./focusFold";import {unregisterViewFoldContext} from "./viewFold";import {destroyAutoDirection} from "../render/autoDirection";+import {disposeSyncEditorSave} from "./editorSave";export const destroy = (protyle: IProtyle) => {if (!protyle) {return;}+ protyle.title?.cancelPendingInput();+ disposeSyncEditorSave(protyle);destroyFoldedRender(protyle);destroyListMindmaps(protyle);cancelLegacyMindmapMigration(protyle);diff --git a/app/src/protyle/util/editorSave.test.ts b/app/src/protyle/util/editorSave.test.tsindex 49a03c1db0..64ac9fd9bf 100644--- a/app/src/protyle/util/editorSave.test.ts+++ b/app/src/protyle/util/editorSave.test.ts@@ -48,7 +48,8 @@ const response = (data: unknown) => new Response(JSON.stringify(data), {headers:const loadSyncSaves = () => {const exports = {} as Pick<typeof import("./editorSave"),- "trackEditorSaveRequest" | "flushPendingEditorSaves" | "getEditorSaveFailureVersion">;+ "trackEditorSaveRequest" | "flushPendingEditorSaves" | "getEditorSaveFailureVersion" |+ "registerSyncEditorSave" | "disposeSyncEditorSave">;runInNewContext(source("editorSave.ts"), {exports,window: {siyuan: {languages: {settingsPendingSaveError: "Save failed"}}}});return exports;@@ -86,6 +87,84 @@ test("sync save confirmation preserves structural failures and ignores supersedeawait assert.rejects(saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion()), /Save failed/);});+test("closing an editor clears only its failed text and structural saves, including late responses", async () => {+ for (const action of ["update", "move"]) {+ const saves = loadSyncSaves();+ const closed = {id: "closed"} as IProtyle;+ const open = {id: "open"} as IProtyle;+ saves.registerSyncEditorSave(closed);+ saves.registerSyncEditorSave(open);+ await saves.trackEditorSaveRequest("/api/transactions", request(closed.id, action), Promise.resolve({code: -1}));+ await saves.trackEditorSaveRequest("/api/transactions", request(open.id), Promise.resolve({code: -1}));+ saves.disposeSyncEditorSave(closed);+ await assert.rejects(saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion()), /Save failed/);+ await saves.trackEditorSaveRequest("/api/transactions", request(open.id), Promise.resolve(success));+ await saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion());++ let finish: (value: unknown) => void;+ const pending = saves.trackEditorSaveRequest("/api/transactions", request(open.id, action), new Promise(resolve => finish = resolve));+ const failureVersion = saves.getEditorSaveFailureVersion();+ saves.disposeSyncEditorSave(open);+ finish({code: -1});+ await pending;+ await saves.flushPendingEditorSaves(failureVersion);++ const reopened = {id: "reopened"} as IProtyle;+ saves.registerSyncEditorSave(reopened);+ await saves.trackEditorSaveRequest("/api/transactions", request(reopened.id), Promise.resolve(success));+ await saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion());+ }+});++test("failed title saves remain protected until the final editor for that document is destroyed", async () => {+ const saves = loadSyncSaves();+ const first = {id: "first", notebookId: "box", path: "/root.sy"} as IProtyle;+ const second = {...first, id: "second"};+ const other = {...first, id: "other", path: "/other.sy"};+ [first, second, other].forEach(saves.registerSyncEditorSave);+ const title = {notebook: "box", path: "/root.sy", title: "unconfirmed"};+ await saves.trackEditorSaveRequest("/api/filetree/renameDoc", title, Promise.resolve({code: -1}));+ saves.disposeSyncEditorSave(first);+ await assert.rejects(saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion()), /Save failed/);+ let finish: (value: unknown) => void;+ const pending = saves.trackEditorSaveRequest("/api/filetree/renameDoc", title, new Promise(resolve => finish = resolve));+ const failureVersion = saves.getEditorSaveFailureVersion();+ saves.disposeSyncEditorSave(second);+ finish({code: -1});+ await pending;+ await saves.flushPendingEditorSaves(failureVersion);+});++test("destroying a moved editor clears title failures associated with its previous path", async () => {+ const saves = loadSyncSaves();+ const protyle = {id: "moved", notebookId: "box", path: "/before.sy"} as IProtyle;+ saves.registerSyncEditorSave(protyle);+ await saves.trackEditorSaveRequest("/api/filetree/renameDoc",+ {notebook: "box", path: protyle.path, title: "unconfirmed"}, Promise.resolve({code: -1}));+ protyle.path = "/parent/before.sy";+ saves.disposeSyncEditorSave(protyle);+ await saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion());+});++test("editor destruction cancels pending title input and removes its sync save failures", async () => {+ const saves = loadSyncSaves();+ let cancelled = false;+ const noop = () => {};+ const protyle = {id: "destroyed", notebookId: "box", path: "/root.sy",+ title: {cancelPendingInput: () => cancelled = true},+ element: {classList: {remove: noop}, removeAttribute: noop},+ } as unknown as IProtyle;+ saves.registerSyncEditorSave(protyle);+ await saves.trackEditorSaveRequest("/api/transactions", request(protyle.id), Promise.resolve({code: -1}));+ await assert.rejects(saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion()), /Save failed/);+ const exports = {} as Pick<typeof import("./destroy"), "destroy">;+ runInNewContext(source("destroy.ts"), {exports, require: (name: string) =>+ name === "./editorSave" ? saves : new Proxy({}, {get: () => noop})});+ exports.destroy(protyle);+ assert.equal(cancelled, true);+ await saves.flushPendingEditorSaves(saves.getEditorSaveFailureVersion());+});+test("tracks actual fetchPost business, network, HTTP and malformed save failures despite swallowed errors", async () => {for (const failure of ["negative", "positive", "network", "http", "missing-result", "missing-operations"]) {const guard = registerEditorSave(editor());diff --git a/app/src/protyle/util/editorSave.ts b/app/src/protyle/util/editorSave.tsindex cde87499d0..765e28ebc3 100644--- a/app/src/protyle/util/editorSave.ts+++ b/app/src/protyle/util/editorSave.ts@@ -10,15 +10,47 @@ const editors = new Map<IProtyle, EditorSaveRecord>();const pendingEditorSaves = new Set<Promise<boolean>>();let editorSaveFailureVersion = 0;let saveVersion = 0;-const syncSaveResults = new Map<string, {version: number, failed: boolean}>();-const syncFailedStructure = new Set<string>();+interface SyncSaveScope {+ results: Map<string, {version: number, failed: boolean}>;+ failedStructure: boolean;+ disposed: boolean;+ pending: number;+ editors?: Set<IProtyle>;+}+const syncSessions = new Map<string, SyncSaveScope>();+const syncTitles = new Map<string, SyncSaveScope>();+const syncEditors = new Set<IProtyle>();++export const registerSyncEditorSave = (protyle: IProtyle) => syncEditors.add(protyle);++export const disposeSyncEditorSave = (protyle: IProtyle) => {+ syncEditors.delete(protyle);+ const dispose = (scopes: Map<string, SyncSaveScope>, key: string) => {+ const scope = scopes.get(key);+ if (scope) {+ scope.disposed = true;+ scopes.delete(key);+ }+ };+ dispose(syncSessions, protyle.id);+ syncTitles.forEach((scope, key) => {+ if (scope.editors?.delete(protyle) && !scope.editors.size) {+ dispose(syncTitles, key);+ }+ });+ // 同一文档的其他编辑器仍可能有未确认的标题,最后一个编辑器销毁后再清理。+ if (protyle.notebookId && protyle.path && ![...syncEditors].some(editor =>+ editor.notebookId === protyle.notebookId && editor.path === protyle.path)) {+ dispose(syncTitles, `${protyle.notebookId}:${protyle.path}`);+ }+};export const getEditorSaveFailureVersion = () => editorSaveFailureVersion;export const flushPendingEditorSaves = async (failureVersion: number) => {while (pendingEditorSaves.size) await Promise.all([...pendingEditorSaves]);- if (failureVersion !== editorSaveFailureVersion || syncFailedStructure.size ||- [...syncSaveResults.values()].some(result => result.failed)) {+ if (failureVersion !== editorSaveFailureVersion || [...syncSessions.values(), ...syncTitles.values()]+ .some(scope => scope.failedStructure || [...scope.results.values()].some(result => result.failed))) {throw new Error(window.siyuan.languages.settingsPendingSaveError);}};@@ -64,23 +96,41 @@ export const trackEditorSaveRequest = <T>(url: string, data: any, promise: Promi[...new Set<string>(operations.map((item: IOperation) => `block:${item.id}`))] : [] : ["title"];const transactionCount = transactionList.length;const saveScope = transactions ? data?.session : `${data?.notebook}:${data?.path}`;- const syncKeys = keys.map(key => `${saveScope}:${key}`);+ const scopes = transactions ? syncSessions : syncTitles;+ let scope = scopes.get(saveScope);+ if (!scope) {+ scope = {results: new Map(), failedStructure: false, disposed: false, pending: 0};+ scopes.set(saveScope, scope);+ }+ if (!transactions) {+ scope.editors ||= new Set();+ syncEditors.forEach(editor => {+ if (editor.notebookId === data?.notebook && editor.path === data?.path) scope.editors.add(editor);+ });+ }+ scope.pending++;const currentVersion = ++saveVersion;- syncKeys.forEach(key => syncSaveResults.set(key, {version: currentVersion, failed: false}));+ keys.forEach(key => scope.results.set(key, {version: currentVersion, failed: false}));// 同步准备覆盖所有编辑器,包括尚未启用关闭保存保护的普通页签。const pendingSave = promise.then((response: any) => response?.code === 0 && (!transactions ||transactionCount > 0 && Array.isArray(response.data) && response.data.length === transactionCount &&response.data.every((item: {doOperations?: IOperation[]}) => Array.isArray(item?.doOperations))), () => false);pendingEditorSaves.add(pendingSave);void pendingSave.then(saved => {- if (!saved) editorSaveFailureVersion++;- if (!syncKeys.length && !saved) syncFailedStructure.add(saveScope);- syncKeys.forEach(key => {- if (syncSaveResults.get(key)?.version === currentVersion) {- if (saved) syncSaveResults.delete(key);- else syncSaveResults.set(key, {version: currentVersion, failed: true});- }- });+ scope.pending--;+ if (!scope.disposed) {+ if (!saved) editorSaveFailureVersion++;+ if (!keys.length && !saved) scope.failedStructure = true;+ keys.forEach(key => {+ if (scope.results.get(key)?.version === currentVersion) {+ if (saved) scope.results.delete(key);+ else scope.results.set(key, {version: currentVersion, failed: true});+ }+ });+ }+ if (!scope.pending && !scope.failedStructure && !scope.results.size && scopes.get(saveScope) === scope) {+ scopes.delete(saveScope);+ }pendingEditorSaves.delete(pendingSave);});editors.forEach((record, protyle) => {diff --git a/app/src/protyle/util/onGet.ts b/app/src/protyle/util/onGet.tsindex aa94dd88ed..f0512d797f 100644--- a/app/src/protyle/util/onGet.ts+++ b/app/src/protyle/util/onGet.ts@@ -70,6 +70,7 @@ export const onGet = (options: {suppressFocus?: boolean,}) => {if (options.isValid && !options.isValid()) {+ options.onAVRenderError?.();return;}if (!options.action) {@@ -201,6 +202,7 @@ export const onGet = (options: {const renderDoc = (response: IWebSocketData) => {if (options.isValid && !options.isValid()) {+ options.onAVRenderError?.();return;}if (options.protyle.options.render.title) {@@ -267,6 +269,7 @@ const setHTML = (options: {isValid?: () => boolean,}, protyle: IProtyle) => {if (options.isValid && !options.isValid()) {+ options.onAVRenderError?.();return;}if (!options.isSyncing && migrateLegacyMindmapsBeforeRender(protyle, options.content, options.action || [], content => {@@ -275,6 +278,7 @@ const setHTML = (options: {return;}if (protyle.contentElement.classList.contains("fn__none") && protyle.wysiwyg.element.innerHTML !== "") {+ options.onAVRenderError?.();return;}const timing = beginPerformance("editor.content");@@ -415,6 +419,7 @@ const setHTML = (options: {if (rendered && (!options.isValid || options.isValid())) {return options.afterAVRender?.();}+ options.onAVRenderError?.();}).catch(error => {console.error(error);options.onAVRenderError?.();diff --git a/app/src/protyle/util/reload.test.ts b/app/src/protyle/util/reload.test.tsindex 6866a0beb4..b90ae9db3b 100644--- a/app/src/protyle/util/reload.test.ts+++ b/app/src/protyle/util/reload.test.ts@@ -5,9 +5,11 @@ import {runInNewContext} from "node:vm";import {ModuleKind, ScriptTarget, transpileModule} from "typescript";const loadReload = () => {- let doc: {cb: (keys: string[]) => void, onRenderError: () => void};+ let doc: {cb: (keys: string[]) => void, onRenderError: () => void, afterAVRender: () => Promise<void>};let finishBacklink: () => void;const backlink = new Promise<void>(resolve => finishBacklink = resolve);+ let finishScroll: () => void;+ const scroll = new Promise<void>(resolve => finishScroll = resolve);const noop = () => {};const exports = {} as Pick<typeof import("./reload"), "reloadProtyleAndWait">;const defaults = new Proxy({}, {get: () => noop});@@ -18,7 +20,7 @@ const loadReload = () => {require: (name: string) => {if (name === "./reloadState") return {shouldReloadProtyle: () => true};if (name === "../scroll/saveScroll") return {saveScroll: noop, getDocByScroll: (options: typeof doc) => doc = options};- if (name === "./reloadScroll") return {preserveReloadScroll: () => ({cancel: noop})};+ if (name === "./reloadScroll") return {preserveReloadScroll: () => ({cancel: noop, afterAVRender: () => scroll})};if (name === "./hasClosest") return {hasClosestByClassName: () => ({querySelectorAll: () => [{value: ""}, {value: ""}]})};if (name === "../wysiwyg/renderBacklink") return {renderBacklink: () => backlink};if (name === "../../util/fetch") return {fetchPost: (_url: string, _data: unknown, callback: (response: unknown) => void) => {@@ -34,22 +36,40 @@ const loadReload = () => {contentElement: {scrollTop: 0}, block: {rootID: "root"},preview: {element: {classList: {contains: () => true}}},} as unknown as IProtyle;- return {...exports, protyle, getDoc: () => doc, finishBacklink: () => finishBacklink()};+ return {...exports, protyle, getDoc: () => doc, finishBacklink: () => finishBacklink(), finishScroll: () => finishScroll()};};-test("sync reload waits for the actual document render and reports rendering errors", async () => {+test("sync reload waits beyond document insertion for database rendering and scroll restoration", async () => {const reload = loadReload();let done = false;const pending = reload.reloadProtyleAndWait(reload.protyle).then(() => done = true);await Promise.resolve();assert.equal(done, false);reload.getDoc().cb([]);+ await Promise.resolve();+ assert.equal(done, false, "inserting document HTML does not finish database rendering");+ const restoringScroll = reload.getDoc().afterAVRender();+ await Promise.resolve();+ assert.equal(done, false, "database rendering does not finish scroll restoration");+ reload.finishScroll();+ await restoringScroll;await pending;const failed = reload.reloadProtyleAndWait(reload.protyle);reload.getDoc().onRenderError();await assert.rejects(failed, /Could not refresh/);});+test("invalidated document renders report failure instead of leaving sync reload pending", () => {+ const exports = {} as Pick<typeof import("./onGet"), "onGet">;+ runInNewContext(transpileModule(readFileSync("src/protyle/util/onGet.ts", "utf8"), {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText, {exports, require: () => ({})});+ let failures = 0;+ exports.onGet({protyle: {} as IProtyle, data: {} as IWebSocketData,+ isValid: () => false, onAVRenderError: () => failures++});+ assert.equal(failures, 1);+});+test("sync reload waits for editable backlink database rendering", async () => {const reload = loadReload();reload.protyle.options.backlinkData = [];diff --git a/app/src/protyle/util/reload.ts b/app/src/protyle/util/reload.tsindex c93e96518a..a8fefbcbfe 100644--- a/app/src/protyle/util/reload.ts+++ b/app/src/protyle/util/reload.ts@@ -110,7 +110,10 @@ export const reloadProtyle = (protyle: IProtyle, focus: boolean, updateReadonly?updateReadonly,isValid: reloadScroll?.isValid,beforeAVRender: reloadScroll?.beforeAVRender,- afterAVRender: reloadScroll?.afterAVRender,+ afterAVRender: async () => {+ await reloadScroll?.afterAVRender();+ callback?.();+ },onRenderError: () => {reloadScroll?.cancel();onError?.();@@ -122,7 +125,6 @@ export const reloadProtyle = (protyle: IProtyle, focus: boolean, updateReadonly?}protyle.databaseAttributePanel?.refresh();protyle.model?.refreshBottomBacklinkPanel();- callback?.();}});}diff --git a/app/src/protyle/util/renderContent.test.ts b/app/src/protyle/util/renderContent.test.tsindex 248f17d3ea..2c2c07b3e5 100644--- a/app/src/protyle/util/renderContent.test.ts+++ b/app/src/protyle/util/renderContent.test.ts@@ -52,8 +52,8 @@ const browserCases = async (renderSource: string, onGetSource: string) => {document.body.append(content);const protyle = {contentElement: content, wysiwyg: {element: container, prepareBlockVirtualization: noop},block: {rootID: "root", showAll: true}, scroll: {shouldKeepLoadedContent: () => true}} as unknown as IProtyle;- const load = (action: string[], html: string) => setHTML({action: [...action, "history"], content: html,- eof: true, isSyncing: false, expand: false}, protyle);+ const load = (action: string[], html: string, callbacks: Record<string, unknown> = {}) => setHTML({+ action: [...action, "history"], content: html, eof: true, isSyncing: false, expand: false, ...callbacks}, protyle);const settle = async () => { for (let i = 0; i < 20; i++) { await Promise.resolve(); } };const oldHTML = Array.from({length: 100}, (_v, index) =>`<div data-node-id="old-${index}" style="height: 20px"><span contenteditable="true">Old ${index}</span></div>`).join("");@@ -113,6 +113,32 @@ const browserCases = async (renderSource: string, onGetSource: string) => {check.equal(calls.some(call => call.root === b), false, "removed nodes do not render");check.equal(calls.some(call => call.root === c), true);+ // 完成与失效通知必须覆盖真实数据库渲染的等待区间。+ let completed = 0;+ let failures = 0;+ holdDatabase = true;+ load([], '<div data-node-id="pending-av"></div>', {+ afterAVRender: () => completed++, onAVRenderError: () => failures++,+ });+ await settle();+ check.equal(completed, 0);+ releaseDatabase();+ await settle();+ check.equal(completed, 1);+ check.equal(failures, 0);+ holdDatabase = true;+ valid = true;+ load([], '<div data-node-id="invalidated-av"></div>', {+ isValid: () => valid, afterAVRender: () => completed++, onAVRenderError: () => failures++,+ });+ valid = false;+ releaseDatabase();+ await settle();+ check.equal(completed, 1);+ check.equal(failures, 1, "an invalidated render settles its reload as a failure");+ load([], "", {isValid: () => false, onAVRenderError: () => failures++});+ check.equal(failures, 2, "a render invalidated before insertion also settles");+// 大文档追加的扫描规模由新块数决定;这里只比较遍历工作量,不代表整页耗时。container.innerHTML = Array.from({length: 20000}, () => '<div><span class="target"></span></div>').join("");const boundary = container.lastElementChild;diff --git a/app/src/util/reloadSync.test.ts b/app/src/util/reloadSync.test.tsnew file mode 100644index 0000000000..2d7de58862--- /dev/null+++ b/app/src/util/reloadSync.test.ts@@ -0,0 +1,101 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {test} from "node:test";+import {runInNewContext} from "node:vm";+import {ModuleKind, ScriptTarget, transpileModule} from "typescript";++const loadMobileReload = () => {+ let enabled = true;+ const conditions: {parent: boolean, mobile: boolean}[] = [];+ const source = readFileSync("src/util/reloadSync.ts", "utf8").split("\n").filter(line => {+ const condition = line.match(/\/\/\/ #if (.+)/);+ if (condition) {+ conditions.push({parent: enabled, mobile: condition[1].trim() === "MOBILE"});+ enabled &&= conditions.at(-1).mobile;+ return false;+ }+ if (/\/\/\/ #else/.test(line)) {+ enabled = conditions.at(-1).parent && !conditions.at(-1).mobile;+ return false;+ }+ if (/\/\/\/ #endif/.test(line)) {+ enabled = conditions.pop().parent;+ return false;+ }+ return enabled;+ }).join("\n");+ const exports = {} as Pick<typeof import("./reloadSync"), "reloadSync" | "waitForSyncReloads">;+ let finishBody: () => void;+ const body = new Promise<void>(resolve => finishBody = resolve);+ let receiveTitle: (response: unknown) => void;+ let failTitle: () => void;+ const titles: string[] = [];+ const protyle = {block: {rootID: "changed"}, notebookId: "box", title: {setTitle: (value: string) => titles.push(value)}};+ const mobile = {editor: {protyle}, docks: {}};+ const noop = () => {};+ runInNewContext(transpileModule(source, {+ compilerOptions: {module: ModuleKind.CommonJS, target: ScriptTarget.ES2021},+ }).outputText, {exports, console: {error: noop},+ window: {siyuan: {mobile}},+ document: {getElementById: () => ({classList: {contains: () => true}})},+ require: (name: string) => {+ if (name === "./fetch") return {fetchPost: (_url: string, _data: unknown, callback: typeof receiveTitle,+ _headers: unknown, fail: () => void) => {+ receiveTitle = callback;+ failTitle = fail;+ return Promise.resolve();+ }};+ if (name === "./pathName") return {isEncryptedBox: () => false, setNoteBook: noop};+ if (name.endsWith("backlinkPanels")) return {getMobileBacklinkPanels: (): unknown[] => [], removeMobileBacklinkContent: noop};+ if (name === "../protyle/util/reload") return {reloadProtyleAndWait: () => body};+ if (name === "../constants") return {Constants: {CUSTOM_SY_TITLE_EMPTY: "empty"}};+ return new Proxy({}, {get: () => noop});+ },+ });+ exports.reloadSync({} as any, {upsertRootIDs: ["changed"], removeRootIDs: []});+ return {...exports, mobile, protyle, titles, finishBody: () => finishBody(),+ receiveTitle: (code = 0) => receiveTitle({code, data: {name: "remote title", ial: {}}}),+ failTitle: () => failTitle()};+};++test("mobile sync waits for both the document and its title before restoring input", async () => {+ const reload = loadMobileReload();+ let completed = false;+ const pending = reload.waitForSyncReloads().then(() => completed = true);+ reload.finishBody();+ await Promise.resolve();+ await Promise.resolve();+ assert.equal(completed, false, "the title response is still pending");+ reload.receiveTitle();+ await pending;+ assert.deepEqual(reload.titles, ["remote title"]);+});++test("mobile title responses cannot rename an editor opened or navigated after the request", async () => {+ for (const navigation of ["editor", "root", "notebook"]) {+ const reload = loadMobileReload();+ if (navigation === "editor") {+ reload.mobile.editor = {protyle: {...reload.protyle, block: {rootID: "next"}}};+ } else if (navigation === "root") {+ reload.protyle.block.rootID = "next";+ } else {+ reload.protyle.notebookId = "next";+ }+ reload.receiveTitle();+ reload.finishBody();+ await reload.waitForSyncReloads();+ assert.deepEqual(reload.titles, [], navigation);+ }+});++test("mobile title request failures reject the pending sync refresh", async () => {+ for (const failure of ["business", "network"]) {+ const reload = loadMobileReload();+ const pending = assert.rejects(reload.waitForSyncReloads(), /Could not refresh synchronized document title/);+ reload.finishBody();+ if (failure === "business") reload.receiveTitle(-1);+ else reload.failTitle();+ await pending;+ assert.deepEqual(reload.titles, []);+ }+});diff --git a/app/src/util/reloadSync.ts b/app/src/util/reloadSync.tsindex e1b59730ad..874131fe62 100644--- a/app/src/util/reloadSync.ts+++ b/app/src/util/reloadSync.ts@@ -66,17 +66,31 @@ export const reloadSync = (setEmpty(app);}} else {- trackSyncReload(reloadProtyleAndWait(window.siyuan.mobile.editor.protyle, updateReadonly));+ const editor = window.siyuan.mobile.editor;+ const protyle = editor.protyle;+ const rootID = protyle.block.rootID;+ const notebookID = protyle.notebookId;const docInfoParam: BlockQueryRequestInput = {- id: window.siyuan.mobile.editor.protyle.block.rootID+ id: rootID};- if (isEncryptedBox(window.siyuan.mobile.editor.protyle.notebookId)) {- docInfoParam.notebook = window.siyuan.mobile.editor.protyle.notebookId;+ if (isEncryptedBox(notebookID)) {+ docInfoParam.notebook = notebookID;}- fetchPost("/api/block/getDocInfo", docInfoParam, (response) => {- setTitle(response.data.name);- window.siyuan.mobile.editor.protyle.title.setTitle(response.data.name, response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");+ const title = new Promise<void>((resolve, reject) => {+ void fetchPost("/api/block/getDocInfo", docInfoParam, (response) => {+ if (response.code !== 0 || !response.data) {+ reject(new Error("Could not refresh synchronized document title"));+ return;+ }+ if (window.siyuan.mobile.editor === editor && editor.protyle === protyle &&+ protyle.block.rootID === rootID && protyle.notebookId === notebookID) {+ setTitle(response.data.name);+ protyle.title?.setTitle(response.data.name, response.data.ial[Constants.CUSTOM_SY_TITLE_EMPTY] === "true");+ }+ resolve();+ }, undefined, () => reject(new Error("Could not refresh synchronized document title"))).catch(reject);});+ trackSyncReload(Promise.all([reloadProtyleAndWait(protyle, updateReadonly), title]).then(() => {}));// 同步刷新移动端大纲,避免大纲与重载后的编辑器数据不一致const outline = window.siyuan.mobile.docks.outline;if (outline) {diff --git a/kernel/model/repository.go b/kernel/model/repository.goindex f6bde8d305..d94bef3ade 100644--- a/kernel/model/repository.go+++ b/kernel/model/repository.go@@ -2823,7 +2823,11 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,}for boxID := range needUnindexBoxes {- (&Box{ID: boxID}).Unindex()+ if len(immediateIndex) != 0 && immediateIndex[0] {+ unindex(boxID)+ } else {+ (&Box{ID: boxID}).Unindex()+ }}for boxID := range needIndexBoxes {if box := Conf.GetBox(boxID); nil != box {@@ -2833,11 +2837,23 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,if _, err := EnsureBoxDoc(boxID); nil != err {logging.LogErrorf("ensure box document [%s] after sync failed: %s", boxID, err)}- box.Index()+ if len(immediateIndex) != 0 && immediateIndex[0] {+ removeBoxRefs(boxID)+ indexBox(boxID)+ } else {+ box.Index()+ }}}needReloadUI := 0 < len(needUnindexBoxes) || 0 < len(needIndexBoxes)+ if needReloadUI && len(immediateIndex) != 0 && immediateIndex[0] {+ // 应用保护内完成笔记本和引用索引,不将这些任务留到恢复编辑之后。+ sql.FlushQueue()+ IndexRefs()+ sql.FlushQueue()+ ResetVirtualBlockRefCache()+ }if needReloadUI {util.ReloadUI()}diff --git a/kernel/model/sync_checkout_fts5_test.go b/kernel/model/sync_checkout_fts5_test.goindex a68191dc6d..7938a94c49 100644--- a/kernel/model/sync_checkout_fts5_test.go+++ b/kernel/model/sync_checkout_fts5_test.go@@ -8,12 +8,15 @@ import ("os""os/exec""path/filepath"+ "slices""testing""time""github.com/siyuan-note/dejavu/entity"+ "github.com/siyuan-note/siyuan/kernel/conf""github.com/siyuan-note/siyuan/kernel/filesys""github.com/siyuan-note/siyuan/kernel/sql"+ "github.com/siyuan-note/siyuan/kernel/task""github.com/siyuan-note/siyuan/kernel/treenode""github.com/siyuan-note/siyuan/kernel/util")@@ -73,3 +76,88 @@ func TestSyncCheckoutCancellationRefreshesDocumentIndex(t *testing.T) {t.Fatal("completed task still active")}}++func TestSyncCheckoutCompletesIndexesBeforeResuming(t *testing.T) {+ const childEnv = "SIYUAN_TEST_CHECKOUT_COMPLETE_INDEX"+ if os.Getenv(childEnv) == "" {+ for _, scenario := range []string{"move", "notebook"} {+ t.Run(scenario, func(t *testing.T) {+ cmd := exec.Command(os.Args[0], "-test.run=^TestSyncCheckoutCompletesIndexesBeforeResuming$", "-test.v")+ cmd.Env = append(os.Environ(), childEnv+"="+scenario)+ if output, err := cmd.CombinedOutput(); err != nil {+ t.Fatalf("isolated index test failed: %v\n%s", err, output)+ }+ })+ }+ return+ }+ fixture, _, repo, _ := prepareAssetDownloadDocumentTest(t)+ boxConf := conf.NewBoxConf()+ boxConf.Closed = false+ if err := fixture.box.SaveConf(boxConf); err != nil {+ t.Fatal(err)+ }+ util.QueueDir = filepath.Join(util.TempDir, "queue")+ util.DBPath = filepath.Join(util.TempDir, util.DBName)+ util.HistoryDBPath = filepath.Join(util.TempDir, "history.db")+ util.AssetContentDBPath = filepath.Join(util.TempDir, "asset_content.db")+ if err := os.MkdirAll(util.TempDir, 0755); err != nil {+ t.Fatal(err)+ }+ sql.InitDatabase(true)+ sql.InitHistoryDatabase(true)+ sql.InitAssetContentDatabase(true)+ t.Cleanup(sql.CloseDatabase)+ const parentID, movedID = "20261021090000-parent1", "20261021090001-moved01"+ parent := treenode.NewTree(fixture.box.ID, "/"+parentID+".sy", "/Parent", "Parent")+ tree := treenode.NewTree(fixture.box.ID, "/"+movedID+".sy", "/Moved", "Moved")+ writeAssetDownloadDocumentTest(t, parent)+ writeAssetDownloadDocumentTest(t, tree)+ checkout := startSyncCheckout(repo)+ if checkout == nil {+ t.Fatal("missing checkout task")+ }+ t.Cleanup(func() { var err error; checkout.finish(repo, &err) })+ if err := checkout.prepareApply(); err != nil {+ t.Fatal(err)+ }+ wantPath := tree.Path+ if os.Getenv(childEnv) == "move" {+ oldPath := "/" + tree.Box + tree.Path+ upsertIndexes([]string{"/" + parent.Box + parent.Path, oldPath})+ sql.FlushQueue()+ wantPath = "/" + parentID + tree.Path+ newPath := "/" + tree.Box + wantPath+ if err := os.MkdirAll(filepath.Dir(filepath.Join(util.DataDir, newPath)), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.Rename(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); err != nil {+ t.Fatal(err)+ }+ checkout.upserts[newPath] = entity.NewFile(newPath, 0, time.Now().UnixMilli())+ checkout.removes[oldPath] = entity.NewFile(oldPath, 0, time.Now().UnixMilli())+ } else {+ path := "/" + fixture.box.ID + "/.siyuan/conf.json"+ checkout.upserts[path] = entity.NewFile(path, 0, time.Now().UnixMilli())+ }+ releaseWrites := checkout.releaseWrites+ checkout.releaseWrites = func() {+ block := sql.GetBlock(movedID)+ if block == nil || block.Path != wantPath {+ t.Errorf("editing resumed before the document was indexed: %+v", block)+ }+ if task.ContainIndexTask() {+ t.Error("notebook indexing remained queued when editing resumed")+ }+ releaseWrites()+ }+ checkout.completeApply()+ snapshot := GetSyncCheckout("", "").Task+ if snapshot.Blocking || snapshot.Phase != "background" {+ t.Fatalf("completed indexing retained the editing guard: %+v", snapshot)+ }+ if os.Getenv(childEnv) == "move" &&+ (!slices.Contains(snapshot.UpsertRootIDs, movedID) || slices.Contains(snapshot.RemoveRootIDs, movedID)) {+ t.Fatalf("a moved document was reported as removed: %+v", snapshot)+ }+}diff --git a/kernel/model/sync_checkout_state.go b/kernel/model/sync_checkout_state.goindex fef95926b0..0bc0f4a964 100644--- a/kernel/model/sync_checkout_state.go+++ b/kernel/model/sync_checkout_state.go@@ -48,22 +48,26 @@ func (task *syncCheckoutTask) snapshotLocked() apicontract.SyncCheckoutState {Blocking: task.phase != "background" && task.phase != "done", Canceling: task.canceling,Current: task.progress.Written, Total: task.progress.Size, Message: task.message,UpsertRootIDs: []string{}, RemoveRootIDs: []string{}}+ roots := map[string]bool{}if task.phase == "background" || task.phase == "done" {for id, upsert := range task.refreshedRoots {- if upsert {- state.UpsertRootIDs = append(state.UpsertRootIDs, id)- } else {- state.RemoveRootIDs = append(state.RemoveRootIDs, id)- }+ roots[id] = upsert}}- for path := range task.upserts {+ for path := range task.removes {if id := checkoutDocumentID(path); id != "" {- state.UpsertRootIDs = append(state.UpsertRootIDs, id)+ roots[id] = false}}- for path := range task.removes {+ for path := range task.upserts {if id := checkoutDocumentID(path); id != "" {+ roots[id] = true+ }+ }+ for id, upsert := range roots {+ if upsert {+ state.UpsertRootIDs = append(state.UpsertRootIDs, id)+ } else {state.RemoveRootIDs = append(state.RemoveRootIDs, id)}}@@ -201,16 +205,17 @@ func (task *syncCheckoutTask) completeApply() {if task.refreshedRoots == nil {task.refreshedRoots = map[string]bool{}}- for _, file := range changes.Upserts {+ // 文档移动会同时删除旧路径、写入新路径,最终仍存在的根文档按更新处理。+ for _, file := range changes.Removes {task.indexedPaths[file.Path] = trueif id := checkoutDocumentID(file.Path); id != "" {- task.refreshedRoots[id] = true+ task.refreshedRoots[id] = false}}- for _, file := range changes.Removes {+ for _, file := range changes.Upserts {task.indexedPaths[file.Path] = trueif id := checkoutDocumentID(file.Path); id != "" {- task.refreshedRoots[id] = false+ task.refreshedRoots[id] = true}}task.upserts, task.removes = map[string]*entity.File{}, map[string]*entity.File{}diff --git a/kernel/model/sync_checkout_state_test.go b/kernel/model/sync_checkout_state_test.goindex f974f5609e..1775f4bdba 100644--- a/kernel/model/sync_checkout_state_test.go+++ b/kernel/model/sync_checkout_state_test.go@@ -5,6 +5,7 @@ import ("errors""net/http""net/http/httptest"+ "slices""strings""testing""time"@@ -45,6 +46,22 @@ func prepareSyncCheckoutStateTest(t *testing.T) *syncCheckoutTask {return task}+func TestSyncCheckoutSnapshotKeepsMovedDocumentsAsUpserts(t *testing.T) {+ checkout := prepareSyncCheckoutStateTest(t)+ const rootID = "20261021090001-moved01"+ oldPath, newPath := "/box/"+rootID+".sy", "/box/parent/"+rootID+".sy"+ checkout.upserts[newPath] = &entity.File{Path: newPath}+ checkout.removes[oldPath] = &entity.File{Path: oldPath}+ checkout.refreshedRoots = map[string]bool{rootID: false}+ for _, phase := range []string{"applying", "background", "done"} {+ checkout.phase = phase+ state := GetSyncCheckout("", "").Task+ if !slices.Contains(state.UpsertRootIDs, rootID) || slices.Contains(state.RemoveRootIDs, rootID) {+ t.Fatalf("%s reported a moved document as removed: %+v", phase, state)+ }+ }+}+func connectSyncCheckoutClient(t *testing.T, id string) *websocket.Conn {t.Helper()push := melody.New()