Skip to content

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

:bug: Prevent Agent block failures from reloading the UI https://github.com/siyuan-note/siyuan/issues/18777

Daniel33835c8eaed4
diff --git a/kernel/mcp/tools/block.go b/kernel/mcp/tools/block.go
index 8f4593bdc8..fc21882516 100644
--- a/kernel/mcp/tools/block.go
+++ b/kernel/mcp/tools/block.go
@@ -189,6 +189,9 @@ func blockInsert(args map[string]any) (CallToolResult, error) {
if v, ok := args["nextID"].(string); ok {
nextID = v
}
+ if parentID == "" && previousID == "" && nextID == "" {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "parentID, previousID, or nextID is required"}}, IsError: true}, nil
+ }
boxID, release, scopeErr := beginBlockToolScope(args, true, parentID, previousID, nextID)
if scopeErr != nil {
return blockToolError(scopeErr.Error())
@@ -210,7 +213,7 @@ func blockInsert(args map[string]any) (CallToolResult, error) {
}
}
- transactions := []*model.Transaction{{
+ transaction := &model.Transaction{
DoOperations: []*model.Operation{{
Action: "insert",
Data: data,
@@ -218,10 +221,11 @@ func blockInsert(args map[string]any) (CallToolResult, error) {
PreviousID: previousID,
NextID: nextID,
}},
- }}
+ }
- model.PerformTransactions(&transactions)
- model.FlushTxQueue()
+ if err := model.PerformTxSync(transaction); err != nil {
+ return blockToolError("insert block failed: " + err.Error())
+ }
reloadID := nextID
if reloadID == "" {
@@ -266,16 +270,17 @@ func blockAppend(args map[string]any) (CallToolResult, error) {
}
}
- transactions := []*model.Transaction{{
+ transaction := &model.Transaction{
DoOperations: []*model.Operation{{
Action: "appendInsert",
Data: data,
ParentID: parentID,
}},
- }}
+ }
- model.PerformTransactions(&transactions)
- model.FlushTxQueue()
+ if err := model.PerformTxSync(transaction); err != nil {
+ return blockToolError("append block failed: " + err.Error())
+ }
if bt := treenode.GetBlockTreeInExactBox(parentID, boxID); bt != nil {
util.PushReloadProtyle(bt.RootID)
@@ -310,16 +315,17 @@ func blockPrepend(args map[string]any) (CallToolResult, error) {
}
}
- transactions := []*model.Transaction{{
+ transaction := &model.Transaction{
DoOperations: []*model.Operation{{
Action: "prependInsert",
Data: data,
ParentID: parentID,
}},
- }}
+ }
- model.PerformTransactions(&transactions)
- model.FlushTxQueue()
+ if err := model.PerformTxSync(transaction); err != nil {
+ return blockToolError("prepend block failed: " + err.Error())
+ }
if bt := treenode.GetBlockTreeInExactBox(parentID, boxID); bt != nil {
util.PushReloadProtyle(bt.RootID)
@@ -373,15 +379,16 @@ func blockDelete(args map[string]any) (CallToolResult, error) {
bt := treenode.GetBlockTreeInExactBox(id, boxID)
- transactions := []*model.Transaction{{
+ transaction := &model.Transaction{
DoOperations: []*model.Operation{{
Action: "delete",
ID: id,
}},
- }}
+ }
- model.PerformTransactions(&transactions)
- model.FlushTxQueue()
+ if err := model.PerformTxSync(transaction); err != nil {
+ return blockToolError("delete block failed: " + err.Error())
+ }
if bt != nil {
util.PushReloadProtyle(bt.RootID)
@@ -434,17 +441,18 @@ func blockMove(args map[string]any) (CallToolResult, error) {
}
}
- transactions := []*model.Transaction{{
+ transaction := &model.Transaction{
DoOperations: []*model.Operation{{
Action: "move",
ID: id,
ParentID: parentID,
PreviousID: previousID,
}},
- }}
+ }
- model.PerformTransactions(&transactions)
- model.FlushTxQueue()
+ if err := model.PerformTxSync(transaction); err != nil {
+ return blockToolError("move block failed: " + err.Error())
+ }
if bt := treenode.GetBlockTreeInExactBox(id, boxID); bt != nil {
util.PushReloadProtyle(bt.RootID)
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 202c882193..9230fbf203 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -138,7 +138,7 @@ func flushTx(tx *Transaction) {
util.PushTxErr(pushMsg, txErr.code, nil)
return
case TxErrCodeReloadUI:
- logging.LogErrorf("transaction structure validation failed: %s", txErr.msg)
+ logging.LogErrorf("transaction failed and requires UI reload: %s", txErr.msg)
util.ReloadUI()
return
case TxErrCodeDataIsSyncing:
@@ -822,8 +822,7 @@ func (tx *Transaction) doPrependInsert(operation *Operation) (ret *TxErr) {
block := treenode.GetBlockTree(operation.ParentID)
if nil == block {
logging.LogWarnf("not found block [%s]", operation.ParentID)
- util.ReloadUI() // 比如分屏后编辑器状态不一致,这里强制重新载入界面
- return
+ return &TxErr{code: TxErrCodeReloadUI, msg: "parent block not found", id: operation.ParentID}
}
tree, err := tx.loadTree(block.ID)
if err != nil {
@@ -926,8 +925,7 @@ func (tx *Transaction) doAppendInsert(operation *Operation) (ret *TxErr) {
block := treenode.GetBlockTree(operation.ParentID)
if nil == block {
logging.LogWarnf("not found block [%s]", operation.ParentID)
- util.ReloadUI() // 比如分屏后编辑器状态不一致,这里强制重新载入界面
- return
+ return &TxErr{code: TxErrCodeReloadUI, msg: "parent block not found", id: operation.ParentID}
}
tree, err := tx.loadTree(block.ID)
if err != nil {
@@ -1462,8 +1460,7 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
}
if nil == bt {
logging.LogWarnf("not found block tree [%s, %s, %s]", operation.ParentID, operation.PreviousID, operation.NextID)
- util.ReloadUI() // 比如分屏后编辑器状态不一致,这里强制重新载入界面
- return
+ return &TxErr{code: TxErrCodeReloadUI, msg: "insertion target block not found"}
}
var err error
diff --git a/kernel/model/transaction_structure_test.go b/kernel/model/transaction_structure_test.go
index 8550204df2..38977cda41 100644
--- a/kernel/model/transaction_structure_test.go
+++ b/kernel/model/transaction_structure_test.go
@@ -96,6 +96,18 @@ func TestInsertRejectsParagraphDirectlyUnderList(t *testing.T) {
}
}
+func TestInsertMissingTargetReturnsTransactionError(t *testing.T) {
+ setupStructureTransactionTest(t)
+ dom := util.NewLute().Md2BlockDOM("inserted", false)
+ tx := &Transaction{DoOperations: []*Operation{{
+ Action: "insert",
+ ParentID: "20260816000000-missing",
+ Data: dom,
+ }}}
+
+ requireStructureTransactionError(t, PerformTxSync(tx))
+}
+
func TestMoveRejectsParagraphDirectlyUnderList(t *testing.T) {
tests := []struct {
name string