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: Roll back failed embedding index updates https://github.com/siyuan-note/siyuan/issues/20939
Daniel3904cf563bb8
diff --git a/kernel/sql/index_recovery_fts5_test.go b/kernel/sql/index_recovery_fts5_test.goindex 7456b23438..24fd090c7e 100644--- a/kernel/sql/index_recovery_fts5_test.go+++ b/kernel/sql/index_recovery_fts5_test.go@@ -279,6 +279,57 @@ func TestIndexRecoveryLifecycle(t *testing.T) {}})+ for _, action := range []string{"delete", "delete_id", "delete_ids", "rename", "rename_doc", "move", "delete_box"} {+ t.Run(action+" auxiliary failure rolls back and retries", func(t *testing.T) {+ ClearQueue()+ tree := newTree()+ IndexTreeQueue(tree)+ FlushQueue()+ if _, err := db.Exec("INSERT INTO block_embeddings (id, root_id, box, path) VALUES (?, ?, ?, ?)", tree.ID, tree.ID, box, tree.Path); err != nil {+ t.Fatal(err)+ }+ verb := "DELETE"+ if action == "rename" || action == "rename_doc" || action == "move" {+ verb = "UPDATE"+ }+ if _, err := db.Exec("CREATE TRIGGER reject_embedding BEFORE " + verb + " ON block_embeddings BEGIN SELECT RAISE(ABORT, 'reject embedding'); END"); err != nil {+ t.Fatal(err)+ }+ latest := *tree+ latest.HPath = "/Updated"+ if action == "move" {+ latest.Path = "/20261026000000-parent1/" + tree.ID + ".sy"+ }+ treenode.IndexBlockTree(&latest)+ op := &dbQueueOperation{action: action, indexTree: &latest, box: box,+ removeTreeBox: box, removeTreePath: tree.Path, removeTreeID: tree.ID, removeTreeIDs: []string{tree.ID}}+ appendOperation(op)+ FlushQueue()+ if len(operationQueue) != 1 || len(loadIndexQueue()) != 1 {+ t.Fatal("auxiliary SQL failure was acknowledged as success")+ }+ assertRows(tree)+ var hpath string+ if err := db.QueryRow("SELECT hpath FROM blocks WHERE id = ?", tree.ID).Scan(&hpath); err != nil || hpath != tree.HPath {+ t.Fatalf("failed auxiliary SQL committed primary changes: %q, %v", hpath, err)+ }+ if _, err := db.Exec("DROP TRIGGER reject_embedding"); err != nil {+ t.Fatal(err)+ }+ FlushQueue()+ if len(operationQueue) != 0 || len(loadIndexQueue()) != 0 {+ t.Fatal("successful auxiliary retry left pending work")+ }+ var rows int+ if err := db.QueryRow("SELECT COUNT(*) FROM block_embeddings WHERE root_id = ?", tree.ID).Scan(&rows); err != nil {+ t.Fatal(err)+ }+ if verb == "DELETE" && rows != 0 || verb == "UPDATE" && rows != 1 {+ t.Fatalf("incorrect auxiliary state after retry: %d rows", rows)+ }+ })+ }+t.Run("embed content is recalculated and searchable after restart", func(t *testing.T) {ClearQueue()tree := newTree()diff --git a/kernel/sql/queue.go b/kernel/sql/queue.goindex 3891d31a4b..c4d828a4c6 100644--- a/kernel/sql/queue.go+++ b/kernel/sql/queue.go@@ -418,18 +418,20 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]any) (err errorerr = batchDeleteByPathPrefix(tx, op.removeTreeBox, op.removeTreePath)if nil == err {condition, args := documentPathPrefixCondition("path", op.removeTreeBox, op.removeTreePath)- tx.Exec("DELETE FROM block_embeddings WHERE "+condition, args...)+ _, err = tx.Exec("DELETE FROM block_embeddings WHERE "+condition, args...)}case "delete_id":err = deleteByRootID(tx, op.removeTreeID, context)if nil == err {- tx.Exec("DELETE FROM block_embeddings WHERE root_id = ?", op.removeTreeID)+ _, err = tx.Exec("DELETE FROM block_embeddings WHERE root_id = ?", op.removeTreeID)}case "delete_ids":err = batchDeleteByRootIDs(tx, op.removeTreeIDs, context)if nil == err {for _, rootID := range op.removeTreeIDs {- tx.Exec("DELETE FROM block_embeddings WHERE root_id = ?", rootID)+ if _, err = tx.Exec("DELETE FROM block_embeddings WHERE root_id = ?", rootID); err != nil {+ return+ }}}case "rename", "rename_doc":@@ -445,12 +447,12 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]any) (err errorerr = updateRootContent(tx, treenode.ReadDocumentTitle(op.indexTree.Root).Text, op.indexTree.Root.IALAttr("updated"), treenode.IALStr(op.indexTree.Root), op.indexTree.ID)if nil == err {- tx.Exec("UPDATE block_embeddings SET box = ?, path = ? WHERE root_id = ?", op.indexTree.Box, op.indexTree.Path, op.indexTree.ID)+ _, err = tx.Exec("UPDATE block_embeddings SET box = ?, path = ? WHERE root_id = ?", op.indexTree.Box, op.indexTree.Path, op.indexTree.ID)}case "move":err = batchUpdatePath(tx, op.indexTree, context)if nil == err {- tx.Exec("UPDATE block_embeddings SET box = ?, path = ? WHERE root_id = ?", op.indexTree.Box, op.indexTree.Path, op.indexTree.ID)+ _, err = tx.Exec("UPDATE block_embeddings SET box = ?, path = ? WHERE root_id = ?", op.indexTree.Box, op.indexTree.Path, op.indexTree.ID)}case "delete_box":// 清理 box 的内容索引。事务由 beginTxForBox(op.boxID()) 按所属库路由:@@ -459,7 +461,7 @@ func execOp(op *dbQueueOperation, tx *sql.Tx, context map[string]any) (err error// 会用纯 INSERT 在无主键的 blocks 表上叠加重复行,导致搜索结果翻倍。err = deleteByBoxTx(tx, op.box)if nil == err {- tx.Exec("DELETE FROM block_embeddings WHERE box = ?", op.box)+ _, err = tx.Exec("DELETE FROM block_embeddings WHERE box = ?", op.box)}case "delete_box_refs":err = deleteRefsByBoxTx(tx, op.box)