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: Prevent reference hints from hanging on stale indexes https://github.com/siyuan-note/siyuan/issues/20666
Daniel51691e21fbfa
diff --git a/app/src/protyle/hint/dailyNoteRef.test.ts b/app/src/protyle/hint/dailyNoteRef.test.tsindex af8c4fba28..e2505a58cd 100644--- a/app/src/protyle/hint/dailyNoteRef.test.ts+++ b/app/src/protyle/hint/dailyNoteRef.test.ts@@ -10,7 +10,9 @@ const source = createSourceFile("extend.ts", readFileSync(join(__dirname, "extenconst declaration = source.statements.find(node => isVariableStatement(node) &&node.declarationList.declarations.some(item => item.name.getText(source) === "hintRef"));assert.ok(declaration);-const code = transpileModule(declaration.getText(source).replace("export ", "") + "\nhintRef;", {+const searchRefSource = readFileSync(join(__dirname, "searchRef.ts"), "utf8")+ .replace(/^import .*;\r?\n/gm, "").replace("export ", "");+const code = transpileModule(searchRefSource + "\n" + declaration.getText(source).replace("export ", "") + "\nhintRef;", {compilerOptions: {target: ScriptTarget.ES2020},}).outputText;@@ -71,7 +73,7 @@ test("closing a reference resolves exact titles before daily notes or configuredassert.equal(original, closeContext);commits.push(value);},- genHTML: () => { rendered = true; },+ genHTML: (data: IHintData[]) => { rendered = data.length > 0; },}};hintRef("Title", protyle, "hint");await setImmediate();diff --git a/app/src/protyle/hint/extend.ts b/app/src/protyle/hint/extend.tsindex b727d3402b..8ff30ddf5b 100644--- a/app/src/protyle/hint/extend.ts+++ b/app/src/protyle/hint/extend.ts@@ -7,6 +7,7 @@ import {getIconByType} from "../../editor/getIcon";import {isDisabledFeature, updateHotkeyTip} from "../util/compatibility";import {blockRender} from "../render/blockRender";import {Constants} from "../../constants";+import {searchRef} from "./searchRef";import {processRender} from "../util/processCode";import {highlightRender} from "../render/highlightRender";import {focusBlock, getEditorRange} from "../util/selection";@@ -726,42 +727,49 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IHrefParam.exactDocTitle = true;}const dailyNotes = source === "hint" ? getDailyNoteHints(key, protyle).catch((): IHintData[] => []) : Promise.resolve([] as IHintData[]);- fetchPost("/api/search/searchRefBlock", refParam, (response) => {- Promise.all([createTarget.promise, dailyNotes]).then(([hideConfiguredCreate, dateHints]) => {+ searchRef(refParam).then((response) => {+ if (!response) {+ if (createTarget.isCurrent()) {+ protyle.hint.genHTML([], protyle, true, source);+ }+ return;+ }+ return Promise.all([createTarget.promise, dailyNotes]).then(([hideConfiguredCreate, dateHints]) => {if (!createTarget.isCurrent()) {return;}- if (response.code !== 0 || (closeContext && !protyle.hint.isRefCloseContextCurrent(protyle, closeContext))) {+ if (closeContext && !protyle.hint.isRefCloseContextCurrent(protyle, closeContext)) {+ protyle.hint.genHTML([], protyle, true, source);return;}const dataList: IHintData[] = [];let createItemCount = 0;dataList.push(...dateHints);createItemCount += dateHints.length;- if (response.data.newDoc) {- const newFileName = Lute.UnEscapeHTMLStr(response.data.k);+ if (response.newDoc) {+ const newFileName = Lute.UnEscapeHTMLStr(response.k);if (!hideConfiguredCreate) {dataList.push({value: `((newFile "${newFileName}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`,html: `<div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>-<span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.data.k}</mark></span></div>`,+<span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.k}</mark></span></div>`,});createItemCount++;}dataList.push({value: `((newSubDoc "${newFileName}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`,html: `<div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>-<span class="b3-list-item__text">${window.siyuan.languages.newSubDoc} <mark>${response.data.k}</mark></span></div>`,+<span class="b3-list-item__text">${window.siyuan.languages.newSubDoc} <mark>${response.k}</mark></span></div>`,});createItemCount++;dataList.push({value: `((newFileAtPath "${newFileName}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`,html: `<div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFolder"></use></svg>-<span class="b3-list-item__text">${window.siyuan.languages.newFileAtPath} <mark>${response.data.k}</mark></span></div>`,+<span class="b3-list-item__text">${window.siyuan.languages.newFileAtPath} <mark>${response.k}</mark></span></div>`,});createItemCount++;}- response.data.blocks.forEach((item: IBlock) => {+ response.blocks.forEach((item: IBlock) => {const name = item.name ? stripSearchMark(escapeSearchHighlight(item.name)) : item.refText.replaceAll(Constants.ZWSP, "");let value = `<span data-type="block-ref" data-id="${item.id}" data-subtype="d">${name}</span>`;if (source === "search") {@@ -780,7 +788,7 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH});if (closeContext) {// 依次选择标题完全一致的文档、日记、新建文档- const commitItem = response.data.blocks.length > 0 ? dataList[createItemCount] :+ const commitItem = response.blocks.length > 0 ? dataList[createItemCount] :(createItemCount > 0 ? dataList[0] : undefined);if (commitItem) {protyle.hint.fillRefClose(commitItem.value, protyle, closeContext);@@ -801,6 +809,10 @@ export const hintRef = (key: string, protyle: IProtyle, source: THintSource): IH}protyle.hint.genHTML(dataList, protyle, true, source);});+ }).catch(() => {+ if (createTarget.isCurrent()) {+ protyle.hint.genHTML([], protyle, true, source);+ }});return [];};diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.tsindex d5ffcb9b5a..c84b2bb242 100644--- a/app/src/protyle/hint/index.ts+++ b/app/src/protyle/hint/index.ts@@ -19,6 +19,7 @@ import {} from "../util/selection";import {focusByRange, getSelectionOffset} from "../util/selectionOffsets";import {genHintItemHTML, hintEmbed, hintRef, hintSlash, hintTag} from "./extend";+import {searchRef} from "./searchRef";import {getSlashEntryKey, TSlashMenuItem} from "./slashMenu";import {getFrequentSlashItems, recordSlashExecution} from "./frequentSlashStorage";import {escapeAttr} from "../../util/escape";@@ -891,27 +892,33 @@ export class Hint {if (isEncryptedBox(protyle.notebookId)) {searchParam.notebook = protyle.notebookId;}- fetchPost("/api/search/searchRefBlock", searchParam, (response) => {- createTarget.promise.then((hideConfiguredCreate) => {+ searchRef(searchParam).then((response) => {+ if (!response) {+ if (createTarget.isCurrent()) {+ this.genHTML([], protyle, true, source);+ }+ return;+ }+ return createTarget.promise.then((hideConfiguredCreate) => {if (!createTarget.isCurrent()) {return;}let searchHTML = "";- if (response.data.newDoc && !hideConfiguredCreate) {- const blockRefText = `((newFile "${oldValue}"${Constants.ZWSP}'${response.data.k}${Lute.Caret}'))`;- searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${response.data.blocks.length === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>-<span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.data.k}</mark></span></div></button>`;+ if (response.newDoc && !hideConfiguredCreate) {+ const blockRefText = `((newFile "${oldValue}"${Constants.ZWSP}'${response.k}${Lute.Caret}'))`;+ searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${response.blocks.length === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(blockRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>+<span class="b3-list-item__text">${window.siyuan.languages.newFile} <mark>${response.k}</mark></span></div></button>`;}- if (response.data.newDoc) {- const newFileName = Lute.UnEscapeHTMLStr(response.data.k);+ if (response.newDoc) {+ const newFileName = Lute.UnEscapeHTMLStr(response.k);const subDocRefText = `((newSubDoc "${oldValue}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`;- searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${hideConfiguredCreate && response.data.blocks.length === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(subDocRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>-<span class="b3-list-item__text">${window.siyuan.languages.newSubDoc} <mark>${response.data.k}</mark></span></div></button>`;+ searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two${hideConfiguredCreate && response.blocks.length === 0 ? " b3-list-item--focus" : ""}" data-value="${encodeURIComponent(subDocRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFile"></use></svg>+<span class="b3-list-item__text">${window.siyuan.languages.newSubDoc} <mark>${response.k}</mark></span></div></button>`;const pathRefText = `((newFileAtPath "${oldValue}"${Constants.ZWSP}'${newFileName}${Lute.Caret}'))`;searchHTML += `<button style="width: calc(100% - 16px)" class="b3-list-item b3-list-item--two" data-value="${encodeURIComponent(pathRefText)}"><div class="b3-list-item__first"><svg class="b3-list-item__graphic"><use xlink:href="#iconFolder"></use></svg>-<span class="b3-list-item__text">${window.siyuan.languages.newFileAtPath} <mark>${response.data.k}</mark></span></div></button>`;+<span class="b3-list-item__text">${window.siyuan.languages.newFileAtPath} <mark>${response.k}</mark></span></div></button>`;}- response.data.blocks.forEach((item: IBlock, index: number) => {+ response.blocks.forEach((item: IBlock, index: number) => {let blockRefHTML;if (source === "av") {// av 搜索时需要获取值 https://github.com/siyuan-note/siyuan/issues/12020@@ -933,6 +940,10 @@ ${genHintItemHTML(item)}this.element.lastElementChild.innerHTML = searchHTML;setPosition(this.element, parseInt(this.element.style.left), parseInt(this.element.style.right));});+ }).catch(() => {+ if (createTarget.isCurrent()) {+ this.genHTML([], protyle, true, source);+ }});}diff --git a/app/src/protyle/hint/searchRef.test.ts b/app/src/protyle/hint/searchRef.test.tsnew file mode 100644index 0000000000..4e4384f8cc--- /dev/null+++ b/app/src/protyle/hint/searchRef.test.ts@@ -0,0 +1,124 @@+import * as assert from "node:assert/strict";+import {readFileSync} from "node:fs";+import {join} from "node:path";+import {test} from "node:test";+import {setImmediate} from "node:timers/promises";+import {runInNewContext} from "node:vm";+import {createSourceFile, isClassDeclaration, isVariableStatement, ScriptTarget, transpileModule} from "typescript";++const extend = createSourceFile("extend.ts", readFileSync(join(__dirname, "extend.ts"), "utf8"), ScriptTarget.Latest);+const functions = ["genHintItemHTML", "hintRef"].map(name => extend.statements.find(statement =>+ isVariableStatement(statement) && statement.declarationList.declarations.some(item => item.name.getText(extend) === name))+ .getText(extend).replace("export ", ""));+const index = createSourceFile("index.ts", readFileSync(join(__dirname, "index.ts"), "utf8"), ScriptTarget.Latest);+const hintClass = index.statements.find(statement => isClassDeclaration(statement) && statement.name.text === "Hint");+assert.ok(hintClass && isClassDeclaration(hintClass));+const searchMethod = hintClass.members.find(member => member.name?.getText(index) === "genSearchHTML").getText(index);+const searchSource = readFileSync(join(__dirname, "searchRef.ts"), "utf8")+ .replace(/^import .*;\r?\n/gm, "").replace("export ", "");+const code = transpileModule(`${searchSource}\n${functions.join("\n")}\nclass Hint { ${searchMethod} }\n({hintRef, Hint});`, {+ compilerOptions: {target: ScriptTarget.ES2020},+}).outputText;++const fixture = (pendingCreate = false) => {+ const requests: Array<{callback: (response: unknown) => void, resolve: () => void, reject: (error: Error) => void}> = [];+ const element = {lastElementChild: {innerHTML: ""}, style: {left: "0", right: "0"}};+ const block = {getAttribute: () => "block"};+ let generation = 0;+ let hidden = false;+ const rendered: IHintData[][] = [];+ const api = runInNewContext(code, {+ window: {siyuan: {config: {editor: {}}, languages: {emptyContent: "Empty", ref: "Reference"}}},+ Constants: {ZWSP: "\u200b"},+ getEditorRange: () => ({startContainer: block}), hasClosestBlock: () => block,+ isEncryptedBox: () => false, getDailyNoteHints: async (): Promise<IHintData[]> => [],+ getIconByType: () => "iconFile", unicode2Emoji: () => "icon",+ stripSearchMark: (value: string) => value, escapeSearchHighlight: (value: string) => value,+ setPosition() {},+ fetchPost: (_url: string, _params: unknown, callback: (response: unknown) => void) =>+ new Promise<void>((resolve, reject) => requests.push({callback, resolve, reject})),+ });+ const hint = new api.Hint();+ Object.assign(hint, {+ element, splitChar: "((",+ prepareCreateTarget: () => {+ const current = ++generation;+ return {promise: pendingCreate ? new Promise(() => {}) : Promise.resolve(false),+ isCurrent: () => current === generation && !hidden};+ },+ genLoading() { hidden = false; element.lastElementChild.innerHTML = "loading"; },+ genHTML(data: IHintData[]) {+ rendered.push(data);+ hidden = data.length === 0;+ element.lastElementChild.innerHTML = hidden ? "hidden" : "results";+ },+ });+ const protyle = {notebookId: "box", element: {clientWidth: 320}, block: {rootID: "root"},+ toolbar: {range: {startContainer: block}}, wysiwyg: {element: {}}, hint};+ return {requests, element, rendered,+ start(source = "hint", searchInput = false) {+ if (searchInput) {+ hint.genSearchHTML(protyle, {value: ""}, block, "Original anchor", source);+ } else {+ api.hintRef("", protyle, source);+ }+ },+ async respond(response: unknown, request = 0, callback = true) {+ if (callback) {+ requests[request].callback(response);+ }+ requests[request].resolve();+ await setImmediate();+ },+ };+};++for (const source of ["hint", "search", "av"]) {+ test(`${source} reference loading ends on empty, invalid, rejected and discarded responses`, async () => {+ for (const searchInput of source === "hint" ? [false] : [false, true]) {+ for (const response of ["", undefined, {code: -1, data: null}, {code: 0, data: {reqId: 1}},+ {code: 0, data: {blocks: "invalid"}}, new Error("network")]) {+ const f = fixture(true);+ f.start(source, searchInput);+ if (response instanceof Error) {+ f.requests[0].reject(response);+ await setImmediate();+ } else {+ await f.respond(response, 0, response !== undefined);+ }+ assert.equal(f.element.lastElementChild.innerHTML, "hidden");+ }+ }+ });++ test(`${source} reference search accepts empty candidates and ignores obsolete failures`, async () => {+ for (const searchInput of source === "hint" ? [false] : [false, true]) {+ const emptyCandidates: Array<null[] | null> = [[], null, [null]];+ for (const blocks of emptyCandidates) {+ const f = fixture();+ f.start(source, searchInput);+ await f.respond({code: 0, data: {k: "", newDoc: false, blocks}});+ assert.equal(f.element.lastElementChild.innerHTML.includes("loading"), false);+ assert.notEqual(f.element.lastElementChild.innerHTML, "hidden");+ }+ const f = fixture();+ f.start(source, searchInput);+ f.start(source, searchInput);+ await f.respond({code: 0, data: {k: "", newDoc: false, blocks: []}}, 1);+ const current = f.element.lastElementChild.innerHTML;+ await f.respond("", 0);+ assert.equal(f.element.lastElementChild.innerHTML, current);+ }+ });+}++test("a failing reference request can be followed by a successful query without resetting the editor", async () => {+ const f = fixture();+ f.start();+ await f.respond("");+ f.start();+ await f.respond({code: 0, data: {k: "", newDoc: false, blocks: [{id: "target", type: "NodeDocument",+ refText: "Target", name: "", ial: {}, content: "Target", hPath: "/"}]}}, 1);+ assert.equal(f.element.lastElementChild.innerHTML, "results");+ assert.match(f.rendered.at(-1)[0].value, /data-id="target" data-subtype="d"/);+});diff --git a/app/src/protyle/hint/searchRef.ts b/app/src/protyle/hint/searchRef.tsnew file mode 100644index 0000000000..5bf15574c3--- /dev/null+++ b/app/src/protyle/hint/searchRef.ts@@ -0,0 +1,23 @@+import {fetchPost} from "../../util/fetch";+import type {SearchBlock, SearchRefBlockRequestInput, SearchRefResult} from "../../types/api";++type RefSearchResult = Omit<SearchRefResult, "blocks"> & {blocks: SearchBlock[]};++// 请求结束后统一返回可渲染的候选,空响应、错误及被丢弃的请求均结束本次加载。+export const searchRef = async (params: SearchRefBlockRequestInput): Promise<RefSearchResult | undefined> => {+ let result: RefSearchResult | undefined;+ await fetchPost("/api/search/searchRefBlock", params, response => {+ if (!response || typeof response !== "object" || response.code !== 0 ||+ !response.data || typeof response.data !== "object" || !("blocks" in response.data)) {+ return;+ }+ const data = response.data;+ if (typeof data.k !== "string" || typeof data.newDoc !== "boolean" ||+ (data.blocks !== null && !Array.isArray(data.blocks))) {+ return;+ }+ result = {k: data.k, newDoc: data.newDoc, reqId: data.reqId,+ blocks: (data.blocks || []).filter((block): block is SearchBlock => block !== null)};+ });+ return result;+};diff --git a/app/tests/avRichTextEditor.test.js b/app/tests/avRichTextEditor.test.jsindex 1ddabb8ab3..37b12ded40 100644--- a/app/tests/avRichTextEditor.test.js+++ b/app/tests/avRichTextEditor.test.js@@ -255,7 +255,9 @@ const runElectron = async () => {{compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText]));const hintSource = ts.createSourceFile("extend.ts", readFileSync(path.join(__dirname,"../src/protyle/hint/extend.ts"), "utf8"), ts.ScriptTarget.Latest, true);- sources.hintRef = ts.transpileModule(hintSource.statements.filter(ts.isVariableStatement).find(statement =>+ const searchRefSource = readFileSync(path.join(__dirname, "../src/protyle/hint/searchRef.ts"), "utf8")+ .replace(/^import .*;\r?\n/gm, "");+ sources.hintRef = ts.transpileModule(searchRefSource + "\n" + hintSource.statements.filter(ts.isVariableStatement).find(statement =>statement.declarationList.declarations.some(declaration => declaration.name.getText(hintSource) === "hintRef")).getText(hintSource), {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText;await win.loadURL("data:text/html,<html><body></body></html>");diff --git a/app/tests/newRefDocAtPath.test.js b/app/tests/newRefDocAtPath.test.jsindex cdb64a00f5..64e16bcc12 100644--- a/app/tests/newRefDocAtPath.test.js+++ b/app/tests/newRefDocAtPath.test.js@@ -58,6 +58,7 @@ const fixture = () => {registerBuiltinSlashHint: value => value,path: path,};+ Object.assign(deps, load("protyle/hint/searchRef", deps, window, Lute));const paths = load("util/pathName", {...deps, path}, window, Lute);Object.assign(deps, load("util/newFileSelection", deps, window, Lute), {...paths,diff --git a/kernel/model/search.go b/kernel/model/search.goindex d0e194fb0b..53aa4a4ce1 100644--- a/kernel/model/search.go+++ b/kernel/model/search.go@@ -458,22 +458,26 @@ func SearchRefBlockInBox(id, rootID, keyword string, beforeLen int, isSquareBracif 32 < len(refs) {refs = refs[:32]}- var btsID []string+ var defBlockIDs []stringfor _, ref := range refs {- btsID = append(btsID, ref.DefBlockRootID)+ defBlockIDs = append(defBlockIDs, ref.DefBlockID)}- btsID = gulu.Str.RemoveDuplicatedElem(btsID)- bts := treenode.GetBlockTreesInBox(btsID, boxID)+ bts := treenode.GetBlockTreesInBox(defBlockIDs, boxID)for _, ref := range refs {- tree := cachedTrees[ref.DefBlockRootID]+ // 按目标块的当前索引定位文档,兼容引用记录中尚未更新的所属文档信息。+ bt := bts[ref.DefBlockID]+ if nil == bt {+ continue+ }+ tree := cachedTrees[bt.RootID]if nil == tree {- tree, _ = loadTreeByBlockTree(bts[ref.DefBlockRootID])+ tree, _ = loadTreeByBlockTree(bt)}if nil == tree {continue}- cachedTrees[ref.RootID] = tree+ cachedTrees[bt.RootID] = treenode := treenode.GetNodeInTree(tree, ref.DefBlockID)if nil == node {diff --git a/kernel/model/search_ref_fts5_test.go b/kernel/model/search_ref_fts5_test.gonew file mode 100644index 0000000000..405130b630--- /dev/null+++ b/kernel/model/search_ref_fts5_test.go@@ -0,0 +1,138 @@+//go:build fts5 && (sqlcipher || libsqlcipher)++package model++import (+ "bytes"+ "encoding/json"+ "os"+ "path/filepath"+ "slices"+ "testing"++ "github.com/88250/lute/ast"+ "github.com/88250/lute/parse"+ "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/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestSearchRefRecentStaleDocumentIndex(t *testing.T) {+ for _, encrypted := range []bool{false, true} {+ name := "plain"+ if encrypted {+ name = "encrypted"+ }+ t.Run(name, func(t *testing.T) {+ fixture := setupStructureTransactionTest(t)+ setupFoldTransactionDatabase(t, fixture)+ Conf.Search.Store(conf.NewSearch())+ var trees []*parse.Tree+ for _, id := range []string{fixture.sourceID, fixture.targetID} {+ tree, err := LoadTreeByBlockID(id)+ if err != nil {+ t.Fatal(err)+ }+ trees = append(trees, tree)+ }+ boxID := ""+ exec := sql.Exec+ if encrypted {+ boxID = fixture.box.ID+ dek := bytes.Repeat([]byte{0x66}, 32)+ markRuntimeEncryptedBox(boxID)+ setDEKForTest(boxID, dek)+ boxConf := conf.NewBoxConf()+ boxConf.Encrypted = true+ boxConf.BoxCrypt = &conf.BoxEncryption{Spec: boxEncryptionSpec}+ if err := encryptBoxMetadata(boxID, boxConf, dek); err != nil {+ t.Fatal(err)+ }+ data, err := json.Marshal(boxConf)+ if err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json"), data, 0600); err != nil {+ t.Fatal(err)+ }+ mountedEncryptedBoxes.Store(boxID, true)+ t.Cleanup(func() {+ sql.CloseEncryptedDB(boxID)+ treenode.CloseEncryptedBlockTreeDB(boxID)+ mountedEncryptedBoxes.Delete(boxID)+ forgetRuntimeEncryptedBox(boxID)+ encryptedBoxLifecycles.Delete(boxID)+ cachedDEKsLock.Lock()+ delete(cachedDEKs, boxID)+ cachedDEKsLock.Unlock()+ })+ if err = treenode.OpenEncryptedBlockTreeDB(boxID, dek); err != nil {+ t.Fatal(err)+ }+ if err = sql.OpenEncryptedDB(boxID, dek); err != nil {+ t.Fatal(err)+ }+ exec = func(stmt string, args ...any) error {+ _, err := sql.GetEncryptedDB(boxID).Exec(stmt, args...)+ return err+ }+ }+ firstID := trees[0].Root.FirstChild.ID+ otherID := trees[1].Root.FirstChild.ID+ const secondID = "20261018150000-second1"+ second := treenode.NewParagraph(secondID)+ second.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte("Second candidate")})+ trees[0].Root.AppendChild(second)+ for _, item := range []struct {+ tree *parse.Tree+ id string+ }{{trees[1], firstID}, {trees[1], secondID}, {trees[0], otherID}} {+ p := treenode.NewParagraph("")+ p.AppendChild(&ast.Node{Type: ast.NodeTextMark, TextMarkType: "block-ref", TextMarkBlockRefID: item.id,+ TextMarkBlockRefSubtype: "s", TextMarkTextContent: "Candidate"})+ item.tree.Root.AppendChild(p)+ }+ for _, tree := range trees {+ if _, err := filesys.WriteTree(tree); err != nil {+ t.Fatal(err)+ }+ treenode.UpsertBlockTree(tree)+ sql.IndexTreeQueue(tree)+ }+ sql.FlushQueue()+ for _, tree := range trees {+ sql.UpdateRefsTreeQueue(tree)+ }+ sql.FlushQueue()+ if err := os.MkdirAll(filepath.Join(util.DataDir, "storage"), 0755); err != nil {+ t.Fatal(err)+ }+ setRefUsed(map[string]int64{firstID: 3, otherID: 2, secondID: 1})+ check := func(want []string) {+ t.Helper()+ blocks, newDoc := SearchRefBlockInBox("", "", "", 48, false, false, boxID)+ var got []string+ for _, block := range blocks {+ got = append(got, block.ID)+ }+ if blocks == nil || newDoc || !slices.Equal(got, want) {+ t.Fatalf("recent candidates: got %v newDoc=%v, want %v", got, newDoc, want)+ }+ }+ for _, staleRoot := range []string{"", "20261018150001-missing", fixture.targetID} {+ if err := exec("UPDATE refs SET def_block_root_id = ? WHERE def_block_id IN (?, ?)", staleRoot, firstID, secondID); err != nil {+ t.Fatal(err)+ }+ check([]string{firstID, otherID, secondID})+ }+ treenode.RemoveBlockTree(fixture.box.ID, firstID)+ check([]string{otherID, secondID})+ treenode.RemoveBlockTreesByRootID(fixture.box.ID, fixture.sourceID)+ check([]string{otherID})+ treenode.RemoveBlockTree(fixture.box.ID, otherID)+ check(nil)+ })+ }+}