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: Fix ref cache cross-box pollution by filtering QueryRefsByDefID results by box, pass explicit boxID to virtualBlockRefKeywords instead of relying on root.Box which is always empty, validate symlinks resolve within data/assets for global resolver, fix ref cache to filter by boxID when building from global DB https://github.com/siyuan-note/siyuan/issues/18034
Daniel5991d2a878e4
diff --git a/kernel/model/assets.go b/kernel/model/assets.goindex fe6af49be2..e55ecfb6b5 100644--- a/kernel/model/assets.go+++ b/kernel/model/assets.go@@ -755,11 +755,13 @@ func getAssetAbsPath(relativePath string, includeEncrypted bool) (absPath stringif !gulu.File.IsSubPath(util.WorkspaceDir, p) {return "", fmt.Errorf("[%s] is not sub path of workspace", p)}- // 解析符号链接,防止软链接跳出工作区+ // 解析符号链接,验证真实路径仍在 data/assets/ 下if realP, evalErr := filepath.EvalSymlinks(p); evalErr == nil && realP != p {- if !gulu.File.IsSubPath(util.WorkspaceDir, realP) {- return "", fmt.Errorf("symlink [%s] resolves outside workspace: [%s]", p, realP)+ assetsRoot := util.GetDataAssetsAbsPath()+ if !gulu.File.IsSubPath(assetsRoot, realP) {+ return "", fmt.Errorf("symlink [%s] resolves outside data/assets: [%s]", p, realP)}+ return realP, nil}return p, nil}diff --git a/kernel/model/crypto.go b/kernel/model/crypto.goindex b004871d86..dd42b46db5 100644--- a/kernel/model/crypto.go+++ b/kernel/model/crypto.go@@ -756,6 +756,8 @@ func LockBox(boxID string) {}// 清理动态引用锚文本缓存treenode.RemoveDynamicRefTexts(boxID)+ // 清理虚拟引用关键字缓存+ ResetVirtualBlockRefCache()}// LockAllBoxes 清除所有已缓存的 DEK 并删除所有加密 db 文件。退出登录或全局锁定时调用。diff --git a/kernel/model/file.go b/kernel/model/file.goindex 65f88bf217..edb3986041 100644--- a/kernel/model/file.go+++ b/kernel/model/file.go@@ -699,7 +699,7 @@ func GetDocInBox(startID, endID, id string, index int, query string, queryTypes,}refCount := sql.QueryRootChildrenRefCountInBox(rootID, boxID)- virtualBlockRefKeywords := getBlockVirtualRefKeywords(tree.Root)+ virtualBlockRefKeywords := getBlockVirtualRefKeywords(tree.Root, tree.Box)subTree := &parse.Tree{ID: rootID, Root: &ast.Node{Type: ast.NodeDocument}, Marks: tree.Marks}diff --git a/kernel/model/transaction.go b/kernel/model/transaction.goindex f84298a320..91c98f7e3b 100644--- a/kernel/model/transaction.go+++ b/kernel/model/transaction.go@@ -2100,7 +2100,7 @@ func (tx *Transaction) writeTree(tree *parse.Tree) {func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes []*ast.Node) {changedNodesMap := map[string]*ast.Node{}- ret = sql.GetRefsCacheByDefID(updateNode.ID)+ ret = sql.GetRefsCacheByDefIDInBox(updateNode.ID, updateNode.Box)if nil != updateNode.Parent && ast.NodeDocument != updateNode.Parent.Type &&updateNode.Parent.IsContainerBlock() && updateNode == treenode.FirstLeafBlock(updateNode.Parent) {// 如果是容器块下第一个叶子块,则需要向上查找引用@@ -2109,7 +2109,7 @@ func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes [break}- parentRefs := sql.GetRefsCacheByDefID(parent.ID)+ parentRefs := sql.GetRefsCacheByDefIDInBox(parent.ID, updateNode.Box)if 0 < len(parentRefs) {ret = append(ret, parentRefs...)if _, ok := changedNodesMap[parent.ID]; !ok {@@ -2125,7 +2125,7 @@ func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes [return ast.WalkContinue}- childRefs := sql.GetRefsCacheByDefID(n.ID)+ childRefs := sql.GetRefsCacheByDefIDInBox(n.ID, updateNode.Box)if 0 < len(childRefs) {ret = append(ret, childRefs...)changedNodesMap[n.ID] = n@@ -2137,7 +2137,7 @@ func getRefsCacheByDefNode(updateNode *ast.Node) (ret []*sql.Ref, changedNodes [// 如果是折叠标题,则需要向下查找引用children := treenode.HeadingChildren(updateNode)for _, child := range children {- childRefs := sql.GetRefsCacheByDefID(child.ID)+ childRefs := sql.GetRefsCacheByDefIDInBox(child.ID, updateNode.Box)if 0 < len(childRefs) {ret = append(ret, childRefs...)changedNodesMap[child.ID] = childdiff --git a/kernel/model/virutalref.go b/kernel/model/virutalref.goindex 99780fe52f..9fd16d485b 100644--- a/kernel/model/virutalref.go+++ b/kernel/model/virutalref.go@@ -45,8 +45,9 @@ var virtualBlockRefCache, _ = ristretto.NewCache(&ristretto.Config{BufferItems: 64,})-func getBlockVirtualRefKeywords(root *ast.Node) (ret []string) {- val, ok := virtualBlockRefCache.Get(root.ID)+func getBlockVirtualRefKeywords(root *ast.Node, boxID string) (ret []string) {+ key := boxID + "\x00" + root.ID+ val, ok := virtualBlockRefCache.Get(key)if !ok {buf := bytes.Buffer{}ast.Walk(root, func(n *ast.Node, entering bool) ast.WalkStatus {@@ -60,14 +61,14 @@ func getBlockVirtualRefKeywords(root *ast.Node) (ret []string) {return ast.WalkContinue})content := buf.String()- ret = putBlockVirtualRefKeywords(content, root)+ ret = putBlockVirtualRefKeywords(content, root, boxID)return}ret = val.([]string)return}-func putBlockVirtualRefKeywords(blockContent string, root *ast.Node) (ret []string) {+func putBlockVirtualRefKeywords(blockContent string, root *ast.Node, boxID string) (ret []string) {keywords := getVirtualRefKeywords(root)if 1 > len(keywords) {return@@ -98,7 +99,8 @@ func putBlockVirtualRefKeywords(blockContent string, root *ast.Node) (ret []stri}ret = gulu.Str.RemoveDuplicatedElem(ret)- virtualBlockRefCache.SetWithTTL(root.ID, ret, 1, 10*time.Minute)+ key := boxID + "\x00" + root.ID+ virtualBlockRefCache.SetWithTTL(key, ret, 1, 10*time.Minute)return}diff --git a/kernel/sql/cache.go b/kernel/sql/cache.goindex 47ed624aa7..b0d5bc1630 100644--- a/kernel/sql/cache.go+++ b/kernel/sql/cache.go@@ -47,6 +47,7 @@ var blockCache, _ = ristretto.NewCache(&ristretto.Config{func ClearCache() {blockCache.Clear()+ clearRefCache()}func putBlockCache(block *Block) {@@ -81,20 +82,33 @@ func removeBlockCache(id string) {removeRefCacheByDefID(id)}-var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute) // [defBlockID]map[refBlockID]*Ref+var defIDRefsCache = gcache.New(30*time.Minute, 5*time.Minute)++func refCacheKey(defBlockID, boxID string) string {+ return boxID + "\x00" + defBlockID+}func GetRefsCacheByDefID(defID string) (ret []*Ref) {- for defBlockID, refs := range defIDRefsCache.Items() {- if defBlockID == defID {+ return GetRefsCacheByDefIDInBox(defID, "")+}++func GetRefsCacheByDefIDInBox(defID, boxID string) (ret []*Ref) {+ key := refCacheKey(defID, boxID)+ for k, refs := range defIDRefsCache.Items() {+ if k == key {for _, ref := range refs.Object.(map[string]*Ref) {ret = append(ret, ref)}}}if 1 > len(ret) {- ret = QueryRefsByDefID(defID, false)- for _, ref := range ret {- putRefCache(ref)+ allRefs := QueryRefsByDefID(defID, false)+ for _, ref := range allRefs {+ // 按 box 过滤:boxID 非空时只选同 box 的 Ref,boxID 为空时全部保留+ if boxID == "" || ref.Box == boxID {+ ret = append(ret, ref)+ putRefCache(boxID, ref)+ }}}return@@ -102,18 +116,27 @@ func GetRefsCacheByDefID(defID string) (ret []*Ref) {func CacheRef(tree *parse.Tree, refNode *ast.Node) {ref := buildRef(tree, refNode)- putRefCache(ref)+ putRefCache(tree.Box, ref)}-func putRefCache(ref *Ref) {- defBlockRefs, ok := defIDRefsCache.Get(ref.DefBlockID)+func putRefCache(boxID string, ref *Ref) {+ key := refCacheKey(ref.DefBlockID, boxID)+ defBlockRefs, ok := defIDRefsCache.Get(key)if !ok {defBlockRefs = map[string]*Ref{}}defBlockRefs.(map[string]*Ref)[ref.BlockID] = ref- defIDRefsCache.SetDefault(ref.DefBlockID, defBlockRefs)+ defIDRefsCache.SetDefault(key, defBlockRefs)}func removeRefCacheByDefID(defID string) {- defIDRefsCache.Delete(defID)+ defIDRefsCache.Delete(refCacheKey(defID, ""))+}++func removeRefCacheByDefIDInBox(defID, boxID string) {+ defIDRefsCache.Delete(refCacheKey(defID, boxID))+}++func clearRefCache() {+ defIDRefsCache.Flush()}diff --git a/kernel/sql/database.go b/kernel/sql/database.goindex 8b8f105f18..d7ce0f50ce 100644--- a/kernel/sql/database.go+++ b/kernel/sql/database.go@@ -566,7 +566,7 @@ func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {defBlockID, text, _ := treenode.GetBlockRef(refNode)var defBlockParentID, defBlockRootID, defBlockPath string- defBlock := treenode.GetBlockTree(defBlockID)+ defBlock := treenode.GetBlockTreeInBox(defBlockID, tree.Box)if nil != defBlock {defBlockParentID = defBlock.ParentIDdefBlockRootID = defBlock.RootID@@ -592,7 +592,7 @@ func buildRef(tree *parse.Tree, refNode *ast.Node) *Ref {func buildEmbedRef(tree *parse.Tree, embedNode *ast.Node) *Ref {defBlockID := getEmbedRef(embedNode)var defBlockParentID, defBlockRootID, defBlockPath string- defBlock := treenode.GetBlockTree(defBlockID)+ defBlock := treenode.GetBlockTreeInBox(defBlockID, tree.Box)if nil != defBlock {defBlockParentID = defBlock.ParentIDdefBlockRootID = defBlock.RootIDdiff --git a/kernel/sql/upsert.go b/kernel/sql/upsert.goindex c172257aa0..ee37dfeebb 100644--- a/kernel/sql/upsert.go+++ b/kernel/sql/upsert.go@@ -370,7 +370,7 @@ func insertRefs0(tx *sql.Tx, bulk []*Ref) (err error) {valueArgs = append(valueArgs, ref.Markdown)valueArgs = append(valueArgs, ref.Type)- putRefCache(ref)+ putRefCache(ref.Box, ref)}stmt := fmt.Sprintf("INSERT INTO refs (id, def_block_id, def_block_parent_id, def_block_root_id, def_block_path, block_id, root_id, box, path, content, markdown, type) VALUES %s", strings.Join(valueStrings, ","))err = prepareExecInsertTx(tx, stmt, valueArgs)