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: Publish batch attributes after document persistence https://github.com/siyuan-note/siyuan/issues/20924
Daniel498f9b6b5b5d
diff --git a/kernel/model/blockial.go b/kernel/model/blockial.goindex 0af2ca21b8..b878b80e91 100644--- a/kernel/model/blockial.go+++ b/kernel/model/blockial.go@@ -143,20 +143,24 @@ func BatchSetBlockAttrs(blockAttrs []map[string]any) (err error) {if handled, collectionErr := performCollectionAttributeOperations(blockAttrs, true); handled || collectionErr != nil {return collectionErr}+ return batchSetBlockAttrs(blockAttrs, indexWriteTreeUpsertQueue)+}+// Validate the entire batch before publishing changes, then publish only persisted documents.+func batchSetBlockAttrs(blockAttrs []map[string]any, writeTree func(*parse.Tree) error) (err error) {var blockIDs []stringfor _, blockAttr := range blockAttrs {blockIDs = append(blockIDs, blockAttr["id"].(string))}trees := filesys.LoadTrees(blockIDs)- var nodes []*ast.Node- type docSortModeChange struct {+ type attributeChange struct {oldAttrs map[string]stringnode *ast.Node- tree *parse.Tree}- var docSortModeChanges []docSortModeChange+ changes := map[string][]attributeChange{}+ var orderedTrees []*parse.Tree+ seenTrees := map[string]bool{}boxIcons := map[string]string{}for _, blockAttr := range blockAttrs {id := blockAttr["id"].(string)@@ -178,44 +182,49 @@ func BatchSetBlockAttrs(blockAttrs []map[string]any) (err error) {attrs["icon"] = boxIcons[tree.Box]}}- oldAttrs, e := setNodeAttrs0(node, attrs, tree.Box)+ oldAttrs, e := setNodeAttrsData(node, attrs, tree.Box)if nil != e {return e}- cache.PutBlockIALInBox(node.ID, tree.Box, parse.IAL2Map(node.KramdownIAL))- pushBlockAttrs(oldAttrs, node)- docSortModeChanges = append(docSortModeChanges, docSortModeChange{oldAttrs: oldAttrs, node: node, tree: tree})- nodes = append(nodes, node)- }-- for _, tree := range trees {- if err = indexWriteTreeUpsertQueue(tree); err != nil {- return+ if !seenTrees[tree.ID] {+ seenTrees[tree.ID] = true+ orderedTrees = append(orderedTrees, tree)}+ changes[tree.ID] = append(changes[tree.ID], attributeChange{oldAttrs: oldAttrs, node: node})}- for boxID, icon := range boxIcons {- box := &Box{ID: boxID}- boxConf := box.GetConf()- oldIcon := boxConf.Icon- boxConf.Icon = icon- if err = box.SaveConf(boxConf); err != nil {- return++ for i, tree := range orderedTrees {+ if err = writeTree(tree); err != nil {+ return fmt.Errorf("batch attribute write failed after saving %d of %d documents: %w", i, len(orderedTrees), err)}- if oldIcon != icon {- pushNotebookIconChanged(boxID, icon)+ IncSync()+ for _, change := range changes[tree.ID] {+ newAttrs := parse.IAL2Map(change.node.KramdownIAL)+ cache.PutBlockIALInBox(change.node.ID, tree.Box, newAttrs)+ pushBlockAttrs(change.oldAttrs, change.node)+ if html.EscapeAttrVal(change.oldAttrs["tags"]) != change.node.IALAttr("tags") {+ ReloadTag()+ }+ if change.oldAttrs[DocSortModeAttr] != newAttrs[DocSortModeAttr] && ast.NodeDocument == change.node.Type {+ cache.RemoveDocIALInBox(tree.Path, tree.Box)+ cache.PutDocIALInBox(tree.Path, tree.Box, newAttrs)+ }+ pushDocSortModeChanged(change.oldAttrs, change.node, tree)}- }- for _, change := range docSortModeChanges {- newAttrs := parse.IAL2Map(change.node.KramdownIAL)- if change.oldAttrs[DocSortModeAttr] != newAttrs[DocSortModeAttr] && ast.NodeDocument == change.node.Type {- cache.RemoveDocIALInBox(change.tree.Path, change.tree.Box)- cache.PutDocIALInBox(change.tree.Path, change.tree.Box, newAttrs)+ if icon, ok := boxIcons[tree.Box]; ok && IsBoxDoc(tree.Box, tree.ID) {+ box := &Box{ID: tree.Box}+ boxConf := box.GetConf()+ oldIcon := boxConf.Icon+ boxConf.Icon = icon+ if err = box.SaveConf(boxConf); err != nil {+ return fmt.Errorf("batch attribute write failed after saving %d of %d documents: %w", i+1, len(orderedTrees), err)+ }+ if oldIcon != icon {+ pushNotebookIconChanged(tree.Box, icon)+ }}- pushDocSortModeChanged(change.oldAttrs, change.node, change.tree)}-- IncSync()// 不做锚文本刷新return}@@ -377,6 +386,14 @@ func setNodeAttrsWithTx(tx *Transaction, node *ast.Node, tree *parse.Tree, nameV}func setNodeAttrs0(node *ast.Node, nameValues map[string]string, boxID string) (oldAttrs map[string]string, err error) {+ oldAttrs, err = setNodeAttrsData(node, nameValues, boxID)+ if err == nil && html.EscapeAttrVal(oldAttrs["tags"]) != node.IALAttr("tags") {+ ReloadTag()+ }+ return+}++func setNodeAttrsData(node *ast.Node, nameValues map[string]string, boxID string) (oldAttrs map[string]string, err error) {if err = treenode.ValidateTabsAttrs(node, nameValues); nil != err {return}@@ -470,9 +487,6 @@ func setNodeAttrs0(node *ast.Node, nameValues map[string]string, boxID string) (node.KramdownIAL = parse.Map2IAL(newAttrsUnEsc)- if html.EscapeAttrVal(oldAttrs["tags"]) != newAttrsUnEsc["tags"] {- ReloadTag()- }return}diff --git a/kernel/model/blockial_batch_test.go b/kernel/model/blockial_batch_test.gonew file mode 100644index 0000000000..c1b8a159f7--- /dev/null+++ b/kernel/model/blockial_batch_test.go@@ -0,0 +1,74 @@+package model++import (+ "errors"+ "strings"+ "testing"++ "github.com/88250/lute/parse"+ "github.com/siyuan-note/siyuan/kernel/cache"+ "github.com/siyuan-note/siyuan/kernel/filesys"+ "github.com/siyuan-note/siyuan/kernel/treenode"+)++func TestBatchBlockAttrsValidationDoesNotPublishChanges(t *testing.T) {+ fixture := setupFileOperationTest(t)+ cache.PutBlockIALInBox(fixture.childID, fixture.box.ID, map[string]string{"custom-test": "old"})+ err := batchSetBlockAttrs([]map[string]any{+ {"id": fixture.childID, "attrs": map[string]string{"custom-test": "new"}},+ {"id": fixture.targetID, "attrs": map[string]string{"data-task": "x"}},+ }, func(*parse.Tree) error {+ t.Fatal("validation failure reached document persistence")+ return nil+ })+ if err == nil || !strings.Contains(err.Error(), "data-task") {+ t.Fatalf("expected validation error: %v", err)+ }+ if got := cache.GetBlockIALInBox(fixture.childID, fixture.box.ID)["custom-test"]; got != "old" {+ t.Fatalf("failed validation changed cached attributes: %q", got)+ }+ tree, err := LoadTreeByBlockID(fixture.childID)+ if err != nil || treenode.GetNodeInTree(tree, fixture.childID).IALAttr("custom-test") != "" {+ t.Fatalf("failed validation changed persisted attributes: %v", err)+ }+}++func TestBatchBlockAttrsPublishesOnlyPersistedDocuments(t *testing.T) {+ fixture := setupFileOperationTest(t)+ for _, id := range []string{fixture.childID, fixture.sourceID, fixture.targetID} {+ cache.PutBlockIALInBox(id, fixture.box.ID, map[string]string{"custom-test": "old"})+ }+ injected := errors.New("injected second document failure")+ writes := 0+ err := batchSetBlockAttrs([]map[string]any{+ {"id": fixture.childID, "attrs": map[string]string{"custom-test": "new"}},+ {"id": fixture.sourceID, "attrs": map[string]string{"custom-test": "new"}},+ {"id": fixture.targetID, "attrs": map[string]string{"custom-test": "new"}},+ }, func(tree *parse.Tree) error {+ writes+++ if writes == 2 {+ return injected+ }+ if got := cache.GetBlockIALInBox(fixture.childID, fixture.box.ID)["custom-test"]; got != "old" {+ t.Fatalf("cache changed before persistence: %q", got)+ }+ _, err := filesys.WriteTree(tree)+ return err+ })+ if !errors.Is(err, injected) || writes != 2 || !strings.Contains(err.Error(), "1 of 2 documents") {+ t.Fatalf("partial failure was not reported accurately: writes=%d err=%v", writes, err)+ }+ for _, id := range []string{fixture.childID, fixture.sourceID, fixture.targetID} {+ wantCache, wantDisk := "new", "new"+ if id == fixture.targetID {+ wantCache, wantDisk = "old", ""+ }+ if got := cache.GetBlockIALInBox(id, fixture.box.ID)["custom-test"]; got != wantCache {+ t.Fatalf("cache does not match committed document %s: %q", id, got)+ }+ tree, loadErr := LoadTreeByBlockID(id)+ if loadErr != nil || treenode.GetNodeInTree(tree, id).IALAttr("custom-test") != wantDisk {+ t.Fatalf("unexpected persisted document %s: %v", id, loadErr)+ }+ }+}