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 duplicate parent document hierarchies https://github.com/siyuan-note/siyuan/issues/18694
Danielf8a7cc439cf7
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.goindex 22756dcf65..e3e855a8d3 100644--- a/kernel/model/file_test.go+++ b/kernel/model/file_test.go@@ -265,6 +265,127 @@ func TestCreateDocsByHPathUsesBoxDocAsLogicalRoot(t *testing.T) {}}+func TestCreateDocsByHPathUsesExactParentDocument(t *testing.T) {+ tests := []struct {+ name string+ title string+ }{+ {name: "non-breaking space", title: "Parent\u00a0Document"},+ {name: "zero-width joiner", title: "Parent \U0001F468\u200d\U0001F4BB"},+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ fixture := setupFileOperationTest(t)+ parentID := "20260718000005-abcdefg"+ parentPath := "/" + parentID + ".sy"+ parentTree := treenode.NewTree(fixture.box.ID, parentPath, "/"+test.title, test.title)+ if _, err := filesys.WriteTree(parentTree); err != nil {+ t.Fatalf("write parent document failed: %v", err)+ }+ treenode.UpsertBlockTree(parentTree)+ t.Cleanup(func() {+ cache.RemoveTreeData(parentTree.ID)+ cache.RemoveDocIAL(parentTree.Path)+ })++ childID := "20260718000006-abcdefg"+ createdID, err := createDocsByHPath(+ fixture.box.ID,+ parentTree.HPath+"/Child",+ "",+ parentID,+ childID,+ false,+ )+ if err != nil {+ t.Fatalf("create child document failed: %v", err)+ }+ if childID != createdID {+ t.Fatalf("unexpected child document ID: got %s, want %s", createdID, childID)+ }++ childTree := treenode.GetBlockTree(childID)+ if nil == childTree {+ t.Fatalf("created child document block tree [%s] not found", childID)+ }+ wantPath := strings.TrimSuffix(parentPath, ".sy") + "/" + childID + ".sy"+ if wantPath != childTree.Path {+ t.Fatalf("child document created under unexpected parent: got %s, want %s", childTree.Path, wantPath)+ }+ })+ }+}++func TestCreateDocsByHPathUsesParentIDForDuplicateHPath(t *testing.T) {+ fixture := setupFileOperationTest(t)+ title := "Parent\u00a0Document"+ var parents []*parse.Tree+ for _, parentID := range []string{"20260718000005-abcdefg", "20260718000006-abcdefg"} {+ parentPath := "/" + parentID + ".sy"+ parentTree := treenode.NewTree(fixture.box.ID, parentPath, "/"+title, title)+ if _, err := filesys.WriteTree(parentTree); err != nil {+ t.Fatalf("write parent document [%s] failed: %v", parentID, err)+ }+ treenode.UpsertBlockTree(parentTree)+ parents = append(parents, parentTree)+ }+ t.Cleanup(func() {+ for _, parentTree := range parents {+ cache.RemoveTreeData(parentTree.ID)+ cache.RemoveDocIAL(parentTree.Path)+ }+ })++ selectedParent := parents[1]+ childID := "20260718000007-abcdefg"+ _, err := createDocsByHPath(+ fixture.box.ID,+ selectedParent.HPath+"/Child",+ "",+ selectedParent.ID,+ childID,+ false,+ )+ if err != nil {+ t.Fatalf("create child document failed: %v", err)+ }++ childTree := treenode.GetBlockTree(childID)+ if nil == childTree {+ t.Fatalf("created child document block tree [%s] not found", childID)+ }+ wantPath := strings.TrimSuffix(selectedParent.Path, ".sy") + "/" + childID + ".sy"+ if wantPath != childTree.Path {+ t.Fatalf("child document created under unexpected duplicate parent: got %s, want %s", childTree.Path, wantPath)+ }+}++func TestCreateDocsByHPathKeepsHPathFallbackForDifferentParent(t *testing.T) {+ fixture := setupFileOperationTest(t)+ childID := "20260718000005-abcdefg"+ _, err := createDocsByHPath(+ fixture.box.ID,+ "/Target/Child",+ "",+ fixture.sourceID,+ childID,+ false,+ )+ if err != nil {+ t.Fatalf("create document by configured hpath failed: %v", err)+ }++ childTree := treenode.GetBlockTree(childID)+ if nil == childTree {+ t.Fatalf("created child document block tree [%s] not found", childID)+ }+ wantPath := strings.TrimSuffix(fixture.targetPath, ".sy") + "/" + childID + ".sy"+ if wantPath != childTree.Path {+ t.Fatalf("configured hpath fallback used unexpected parent: got %s, want %s", childTree.Path, wantPath)+ }+}+func TestGetBoxesByPathsStrictRejectsInvalidPaths(t *testing.T) {fixture := setupFileOperationTest(t)tests := []struct {diff --git a/kernel/model/path.go b/kernel/model/path.goindex ba57dc1ce7..f410be95e2 100644--- a/kernel/model/path.go+++ b/kernel/model/path.go@@ -46,11 +46,10 @@ func createDocsByHPath(boxID, hPath, content, parentID, id string, titleEmpty boparentID = ""}if "" != parentID {- // The save path is incorrect when creating a sub-doc by ref in a doc with the same name https://github.com/siyuan-note/siyuan/issues/8138- // 在指定了父文档 ID 的情况下优先查找父文档+ // 存在同名文档时通过父文档 ID 精确定位 https://github.com/siyuan-note/siyuan/issues/8138parentHPath, name := path.Split(hPath)parentHPath = strings.TrimSuffix(parentHPath, "/")- preferredParent := treenode.GetBlockTreeByHPathPreferredParentID(boxID, parentHPath, parentID)+ preferredParent := treenode.GetBlockTreeRootByIDAndHPath(boxID, parentID, parentHPath)if nil != preferredParent && preferredParent.RootID == parentID {// 如果父文档存在且 ID 一致,则直接在父文档下创建p := strings.TrimSuffix(preferredParent.Path, ".sy") + "/" + id + ".sy"diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.goindex a7081a190f..e886a77337 100644--- a/kernel/treenode/blocktree.go+++ b/kernel/treenode/blocktree.go@@ -307,40 +307,12 @@ func GetBlockTreeRootsByHPath(boxID, hPath string) (ret []*BlockTree) {return}-func GetBlockTreeByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) {- hPath = gulu.Str.RemoveInvisible(hPath)- var roots []*BlockTree- sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND parent_id = ? LIMIT 1"- rows, err := queryForBox(boxID, sqlStmt, boxID, hPath, preferredParentID)- if err != nil {- logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)- return- }- defer rows.Close()- for rows.Next() {- var block BlockTree- if err = rows.Scan(&block.ID, &block.RootID, &block.ParentID, &block.BoxID, &block.Path, &block.HPath, &block.Updated, &block.Type); err != nil {- logging.LogErrorf("query scan field failed: %s", err)- return- }- if "" == preferredParentID {- ret = &block- return- }- roots = append(roots, &block)- }-- if 1 > len(roots) {- return- }-- for _, root := range roots {- if root.ID == preferredParentID {- ret = root- return- }+// GetBlockTreeRootByIDAndHPath 按笔记本、根块 ID 和原始 HPath 精确查找文档。+func GetBlockTreeRootByIDAndHPath(boxID, id, hPath string) (ret *BlockTree) {+ ret = GetBlockTreeInExactBox(id, boxID)+ if nil == ret || ret.ID != ret.RootID || "d" != ret.Type || hPath != ret.HPath {+ ret = nil}- ret = roots[0]return}