Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:white_check_mark: Define collection origins and backfill coverage https://github.com/siyuan-note/siyuan/issues/20926

Daniel7f94d926c248
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
index 9b1ca54ea7..d96f341348 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
@@ -5448,7 +5448,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "Collection is evaluated after block creation, content or attribute edits, and block moves. Moving an entire document in the document tree, imports, sync, and history restoration do not backfill matches; use the preview to collect existing content."
+ "Data": "Collection is evaluated after user creation, copying, content or attribute edits, content template application, global find and replace, and block or document moves into a watched scope. Document tree copies and moves include descendant documents. Existing rows keep their field values. Markdown and .sy imports, sync, history restoration, undo, and redo do not trigger new collection; use the preview to collect existing content explicitly."
}
]
},
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
index 3970f5bdd3..0d09fbf7fd 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
@@ -5210,7 +5210,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "收集在块新增、正文或属性修改、块移动后判断。文档树中移动整篇文档、导入、同步和历史恢复不会自动补收,可通过预览收集存量内容。自动动作不会串联触发其他规则。来源与目标数据库须处于同一加密边界,加密笔记本需先解锁再预览或编辑规则。"
+ "Data": "用户新增、复制、修改正文或属性、应用内容模板、全局查找替换,以及将块或文档移入监听范围后,都会判断收集条件。文档树复制和移动包含子文档。已有条目保留字段值。Markdown 和 .sy 导入、同步、历史恢复、撤销及重做不会触发新的收集,可通过预览手动收集存量内容。自动动作不会串联触发其他规则。来源与目标数据库须处于同一加密边界,加密笔记本需先解锁再预览或编辑规则。"
}
]
},
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
index c709345280..b100b50064 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
@@ -5210,7 +5210,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "收集會在區塊新增、內文或屬性修改、區塊移動後判斷。在文件樹中移動整篇文件、匯入、同步及歷史復原不會自動補收,可透過預覽收集既有內容。自動動作不會連鎖觸發其他規則。來源與目標資料庫須處於同一加密邊界,加密筆記本需先解鎖再預覽或編輯規則。"
+ "Data": "使用者新增、複製、修改內文或屬性、套用內容範本、全域尋找取代,以及將區塊或文件移入監聽範圍後,都會判斷收集條件。文件樹複製和移動包含子文件。既有條目保留欄位值。Markdown 和 .sy 匯入、同步、歷史復原、復原及重做不會觸發新的收集,可透過預覽手動收集既有內容。自動動作不會連鎖觸發其他規則。來源與目標資料庫須處於同一加密邊界,加密筆記本需先解鎖再預覽或編輯規則。"
}
]
},
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
index a90f669656..719b8e7ed6 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
@@ -5398,7 +5398,7 @@
"Children": [
{
"Type": "NodeText",
- "Data": "収集条件はブロックの作成、本文や属性の編集、ブロックの移動後に評価します。文書ツリーでの文書全体の移動、インポート、同期、履歴の復元では既存内容を自動収集しないため、プレビューから収集してください。自動アクションは他のルールを連鎖的に実行しません。元のブロックと対象データベースは同じ暗号化境界内にある必要があります。暗号化ノートブックでは、先にロックを解除してからプレビューやルールの編集を行ってください。"
+ "Data": "ユーザーによる作成、コピー、本文や属性の編集、内容テンプレートの適用、全体検索置換、および監視範囲へのブロックや文書の移動後に収集条件を評価します。文書ツリーのコピーと移動には子文書も含まれます。既存の項目のフィールド値は保持します。Markdown と .sy のインポート、同期、履歴の復元、元に戻す操作、やり直しでは新たな収集を実行しません。既存内容はプレビューから手動で収集してください。自動アクションは他のルールを連鎖的に実行しません。元のブロックと対象データベースは同じ暗号化境界内にある必要があります。暗号化ノートブックでは、先にロックを解除してからプレビューやルールの編集を行ってください。"
}
]
},
diff --git a/kernel/model/attribute_view_collection_origin_fts5_test.go b/kernel/model/attribute_view_collection_origin_fts5_test.go
new file mode 100644
index 0000000000..bd43f1f8fe
--- /dev/null
+++ b/kernel/model/attribute_view_collection_origin_fts5_test.go
@@ -0,0 +1,139 @@
+//go:build fts5
+
+package model
+
+import (
+ "archive/zip"
+ "bytes"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestDocumentCollectionImportsRequireExplicitBackfill(t *testing.T) {
+ for _, sy := range []bool{false, true} {
+ t.Run(map[bool]string{false: "markdown", true: "sy"}[sy], func(t *testing.T) {
+ fixture, view, keyID := setupBlockCollectionTest(t)
+ rule := view.Automations.Rules[0]
+ rule.BlockSource.DocumentIDs = nil
+ rule.BlockSource.NotebookIDs = []string{fixture.box.ID}
+ rule.BlockSource.Types = []string{"p"}
+ if err := av.SaveAttributeView(view); err != nil {
+ t.Fatal(err)
+ }
+ if sy {
+ id := ast.NewNodeID()
+ tree := treenode.NewTree(fixture.box.ID, "/"+id+".sy", "/Imported", "Imported")
+ tree.Root.FirstChild.AppendChild(&ast.Node{Type: ast.NodeTextMark, TextMarkType: "tag", TextMarkTextContent: "todo"})
+ var archive bytes.Buffer
+ writer := zip.NewWriter(&archive)
+ entry, err := writer.Create("Imported/" + id + ".sy")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err = entry.Write(treeToSYJSON(tree)); err != nil {
+ t.Fatal(err)
+ }
+ if err = writer.Close(); err != nil {
+ t.Fatal(err)
+ }
+ filename := filepath.Join(t.TempDir(), "Imported.sy.zip")
+ if err = os.WriteFile(filename, archive.Bytes(), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if err = ImportSY(filename, fixture.box.ID, "/"); err != nil {
+ t.Fatal(err)
+ }
+ } else {
+ filename := filepath.Join(t.TempDir(), "Imported.md")
+ if err := os.WriteFile(filename, []byte("Imported #todo#"), 0600); err != nil {
+ t.Fatal(err)
+ }
+ if err := ImportFromLocalPath(fixture.box.ID, filename, "/"); err != nil {
+ t.Fatal(err)
+ }
+ }
+ preview, err := PreviewAttributeViewCollection(view.ID, rule.ID, 1)
+ if err != nil || len(preview.Blocks) != 1 || preview.Blocks[0].Collected {
+ t.Fatalf("imported matching block missing from preview: %+v, %v", preview, err)
+ }
+ if len(readAttributeViewItemsTest(t, view.ID).GetBlockKeyValues().Values) != 0 {
+ t.Fatal("import or preview executed collection")
+ }
+ id := preview.Blocks[0].ID
+ op := &Operation{Action: "collectAttrViewBlocks", AvID: view.ID, Data: map[string]any{"ruleID": rule.ID, "blockIDs": []string{id}}}
+ if err = PerformTxSync(&Transaction{fromAPI: true, DoOperations: []*Operation{op}}); err != nil {
+ t.Fatal(err)
+ }
+ stored := readAttributeViewItemsTest(t, view.ID)
+ row := stored.GetBlockValueByBoundID(id)
+ if row == nil || stored.GetValue(keyID, row.BlockID).Text.Content != "New" {
+ t.Fatal("explicit collection did not initialize the imported block")
+ }
+ })
+ }
+}
+
+func TestDocumentCollectionRecoveryAndReplayDoNotTrigger(t *testing.T) {
+ for _, origin := range []string{"repository-conflict", "history", "replay"} {
+ t.Run(origin, func(t *testing.T) {
+ fixture, view, _ := setupBlockCollectionTest(t)
+ rule := view.Automations.Rules[0]
+ rule.BlockSource.DocumentIDs = nil
+ rule.BlockSource.NotebookIDs = []string{fixture.box.ID}
+ if err := av.SaveAttributeView(view); err != nil {
+ t.Fatal(err)
+ }
+ id := fixture.sourceID
+ switch origin {
+ case "repository-conflict":
+ id = ast.NewNodeID()
+ tree := treenode.NewTree(fixture.box.ID, "/"+id+".sy", "/Recovered", "Recovered")
+ tree.Root.SetIALAttr("tags", "todo")
+ createTreeTx(tree)
+ FlushTxQueue()
+ case "history":
+ tree, err := LoadTreeByBlockID(id)
+ if err != nil {
+ t.Fatal(err)
+ }
+ tree.Root.SetIALAttr("tags", "todo")
+ oldWorkspace := util.WorkspaceDir
+ util.WorkspaceDir = filepath.Dir(util.HistoryDir)
+ t.Cleanup(func() { util.WorkspaceDir = oldWorkspace })
+ filename := filepath.Join(util.HistoryDir, "2026-10-26-100000-update", tree.Box, id+".sy")
+ if err = os.MkdirAll(filepath.Dir(filename), 0700); err != nil {
+ t.Fatal(err)
+ }
+ if err = os.WriteFile(filename, treeToSYJSON(tree), 0600); err != nil {
+ t.Fatal(err)
+ }
+ relative, err := filepath.Rel(util.WorkspaceDir, filename)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err = RollbackDocHistory(relative); err != nil {
+ t.Fatal(err)
+ }
+ case "replay":
+ tx := blockCollectionTags(t, id, "todo")
+ tx.isReplay = true
+ if err := PerformTxSync(tx); err != nil {
+ t.Fatal(err)
+ }
+ }
+ tree, err := LoadTreeByBlockID(id)
+ if err != nil || !collectionMatches(rule.BlockSource, tree)[id] {
+ t.Fatalf("recovered content does not match the active rule: %v", err)
+ }
+ if len(readAttributeViewItemsTest(t, view.ID).GetBlockKeyValues().Values) != 0 {
+ t.Fatal("recovery or replay executed a new collection")
+ }
+ })
+ }
+}
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index bd47371cf9..2fbf2abdab 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -2796,7 +2796,7 @@ type Transaction struct {
attributeViewItemCreation bool
invalidatedAvHistory map[string]bool
- fromAPI bool // 是否来自 /api/transactions HTTP 入口(用于撤销日志捕获判别)
+ fromAPI bool // 用户主动修改(HTTP、CLI、MCP 等);允许自动化,撤销日志仍需具备 UndoOperations
isReplay bool // 是否为 undo/redo 重放构造的事务(重放不再进入撤销日志)
listItemFoldCandidates []listItemFoldCandidate