Skip to content

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

:bug: Preserve inbox originals when document creation fails https://github.com/siyuan-note/siyuan/issues/19455

Daniel8564e03c10a3
diff --git a/kernel/mcp/tools/inbox.go b/kernel/mcp/tools/inbox.go
index bc86d2a896..5d76dc9cf8 100644
--- a/kernel/mcp/tools/inbox.go
+++ b/kernel/mcp/tools/inbox.go
@@ -39,7 +39,7 @@ var InboxTool = &Tool{
"page": {Type: "integer", Description: "Page number for list (1-based, default 1)"},
"id": {Type: "string", Description: "Shorthand ID (for get)"},
"ids": {Type: "string", Description: "Comma-separated shorthand IDs (for convert), e.g. \"1700000000000,1700000000001\""},
- "notebook": {Type: "string", Description: "Target notebook ID (for convert)"},
+ "notebook": {Type: "string", Description: "Target non-encrypted notebook ID (for convert)"},
"path": {Type: "string", Description: "Target hPath in the notebook for the new documents (default \"/\", the notebook root). Parent path must already exist."},
"remove_after": {Type: "boolean", Description: "Whether to delete the cloud shorthand after a successful conversion (default true)"},
},
@@ -148,6 +148,9 @@ func inboxConvert(args map[string]any) (CallToolResult, error) {
if notebook == "" {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "notebook is required"}}, IsError: true}, nil
}
+ if model.IsEncryptedBox(notebook) {
+ return CallToolResult{Content: []ContentItem{{Type: "text", Text: "inbox conversion requires a non-encrypted notebook"}}, IsError: true}, nil
+ }
ids := parseShorthandIDs(args["ids"])
if len(ids) == 0 {
@@ -205,7 +208,7 @@ func inboxConvert(args map[string]any) (CallToolResult, error) {
docID := ast.NewNodeID()
docPath := strings.TrimRight(parentPath, "/") + "/" + docID + ".sy"
- tree, err := model.CreateDocByMd(notebook, docPath, title, md, nil, nil)
+ tree, err := model.CreateDocByMdSync(notebook, docPath, title, md, nil, nil)
if err != nil {
sb.WriteString(fmt.Sprintf("- [%s] %s -> FAILED: %s\n", id, title, err.Error()))
failed++
diff --git a/kernel/mcp/tools/inbox_test.go b/kernel/mcp/tools/inbox_test.go
new file mode 100644
index 0000000000..6447ed5d6a
--- /dev/null
+++ b/kernel/mcp/tools/inbox_test.go
@@ -0,0 +1,29 @@
+package tools
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/88250/lute/ast"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func TestInboxConvertRejectsEncryptedNotebookBeforeCloudAccess(t *testing.T) {
+ originalDataDir := util.DataDir
+ util.DataDir = t.TempDir()
+ t.Cleanup(func() { util.DataDir = originalDataDir })
+ boxID := ast.NewNodeID()
+ confDir := filepath.Join(util.DataDir, boxID, ".siyuan")
+ if err := os.MkdirAll(confDir, 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(confDir, "conf.json"), []byte(`{"encrypted":true}`), 0644); err != nil {
+ t.Fatal(err)
+ }
+ result, err := inboxConvert(map[string]any{"notebook": boxID, "ids": "1700000000000"})
+ if err != nil || !result.IsError || len(result.Content) != 1 || !strings.Contains(result.Content[0].Text, "non-encrypted notebook") {
+ t.Fatalf("expected encrypted target rejection before cloud access, got %+v, %v", result, err)
+ }
+}
diff --git a/kernel/model/file.go b/kernel/model/file.go
index 96ea3e4cf0..392841aff4 100644
--- a/kernel/model/file.go
+++ b/kernel/model/file.go
@@ -1280,6 +1280,15 @@ func createTreeTx(tree *parse.Tree) {
var createDocLock = sync.Mutex{}
func CreateDocByMd(boxID, p, title, md string, sorts []string, arg map[string]any) (tree *parse.Tree, err error) {
+ return createDocByMd(boxID, p, title, md, sorts, arg, false)
+}
+
+// CreateDocByMdSync 同步创建 Markdown 文档并返回事务落盘错误,供成功后需要清理源数据的调用方使用。
+func CreateDocByMdSync(boxID, p, title, md string, sorts []string, arg map[string]any) (tree *parse.Tree, err error) {
+ return createDocByMd(boxID, p, title, md, sorts, arg, true)
+}
+
+func createDocByMd(boxID, p, title, md string, sorts []string, arg map[string]any, syncWrite bool) (tree *parse.Tree, err error) {
createDocLock.Lock()
defer createDocLock.Unlock()
@@ -1304,7 +1313,7 @@ func CreateDocByMd(boxID, p, title, md string, sorts []string, arg map[string]an
luteEngine := util.NewLute()
luteEngine.SetHTMLTag2TextMark(true)
dom := luteEngine.Md2BlockDOM(md, false)
- tree, err = createDoc(box.ID, p, title, dom, false)
+ tree, err = createDoc0(box.ID, p, title, dom, false, syncWrite)
if err != nil {
return
}
diff --git a/kernel/model/file_test.go b/kernel/model/file_test.go
index b16bab0dfc..9878f270ec 100644
--- a/kernel/model/file_test.go
+++ b/kernel/model/file_test.go
@@ -826,6 +826,21 @@ func TestPerformCreateDocTransactionSyncReturnsWriteError(t *testing.T) {
}
}
+func TestCreateDocByMdSyncReturnsWriteError(t *testing.T) {
+ fixture := setupFileOperationTest(t)
+ docID := "20260718000009-abcdefg\x00"
+ tree, err := CreateDocByMdSync(fixture.box.ID, "/"+docID+".sy", "Inbox", "Keep the cloud original", nil, nil)
+ if tree == nil {
+ t.Fatal("expected document validation to succeed before the filesystem write fails")
+ }
+ if err == nil {
+ t.Fatal("expected Markdown document creation to return the write error")
+ }
+ if bt := treenode.GetBlockTree(docID); bt != nil {
+ t.Fatal("failed document remains in the block tree")
+ }
+}
+
func TestGetHPathsByPathsUsesDocumentRoot(t *testing.T) {
fixture := setupFileOperationTest(t)
tree, err := LoadTreeByBlockID(fixture.sourceID)