Skip to content

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

:recycle: Remove unreachable document creation fallbacks https://github.com/siyuan-note/siyuan/issues/20892

Danielc5af1fd02ac8
diff --git a/kernel/agent/agent.go b/kernel/agent/agent.go
index b54c62a280..c7f5a108d7 100644
--- a/kernel/agent/agent.go
+++ b/kernel/agent/agent.go
@@ -54,7 +54,7 @@ const systemPrompt = `You are a SiYuan AI assistant. You help users manage their
- Heading hierarchy: headings (h1-h6) are leaf blocks. Blocks that appear "under" a heading in the UI are its *following siblings* in the AST, not its children.
- Native structured blocks: use native capabilities and preserve existing structure, block IDs, and metadata unless the user explicitly requests a conversion. Follow the relevant tool descriptions for block-specific syntax and operations.
- Notebook: a top-level container holding documents. Use notebook.list to enumerate; pass notebook ID when creating documents.
-- hPath (human-readable path): display text only. A literal title can contain "/", so identical hPaths can refer to different document hierarchies. Never split an hPath or use it to recover identity. Document create/list/move and inbox.convert use notebook + parentID; parentID="" explicitly selects the notebook root. Legacy path=hPath is only available in compatibility-mode notebooks and returns legacyPathUnsupported in structured-title notebooks. Obtain document IDs from document.list/search_docs/get; a rename changes display text but not ID.
+- hPath (human-readable path): display text only. A literal title can contain "/", so identical hPaths can refer to different document hierarchies. Never split an hPath or use it to recover identity. Document create/list/move and inbox.convert use notebook + parentID; parentID="" explicitly selects the notebook root. Legacy path=hPath resolves only unambiguous ancestor chains with non-empty titles and no literal "/"; unsupported or ambiguous candidates and unavailable indexes return explicit errors. Obtain document IDs from document.list/search_docs/get; a rename changes display text but not ID.
- Document vs block move: document.move relocates an entire document (and children) — needs id, notebook (from document.get field "Box"), and target parentID. block.move repositions a single content block under a new parent block.
- Dailynote (daily note/diary/journal): a special document on the notebook's daily-note save path. For diary/daily note/journal requests, use dailynote.create (not document.create) to open today's note, then dailynote.append/prepend to add content.
diff --git a/kernel/mcp/tools/document.go b/kernel/mcp/tools/document.go
index 06e62121c0..71943ec9de 100644
--- a/kernel/mcp/tools/document.go
+++ b/kernel/mcp/tools/document.go
@@ -29,7 +29,7 @@ import (
var DocumentTool = &Tool{
Name: "document",
- Description: "Document operations. Actions: get(id), create(notebook, parentID, title, markdown?), list(notebook, parentID=notebook root by default), delete(id), rename(id, title), move(id, notebook, parentID), duplicate(id), search_docs(keyword), info(id). Use parentID=\"\" for the notebook root. Titles are independent literal text. The legacy path=hPath parameter works only in compatibility-mode notebooks; never derive identity or hierarchy from a display hPath.",
+ Description: "Document operations. Actions: get(id), create(notebook, parentID, title, markdown?), list(notebook, parentID=notebook root by default), delete(id), rename(id, title), move(id, notebook, parentID), duplicate(id), search_docs(keyword), info(id). Use parentID=\"\" for the notebook root. Titles are independent literal text. The legacy path=hPath parameter checks each candidate ancestor chain; ambiguous paths, empty or slash-containing ancestor titles and unavailable indexes return explicit errors. Never derive identity or hierarchy from a display hPath.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
@@ -37,7 +37,7 @@ var DocumentTool = &Tool{
"id": {Type: "string", Description: "Document block ID"},
"title": {Type: "string", Description: "Document title (for create, rename)"},
"parentID": {Type: "string", Description: "Target parent document ID (for create, list, move); an empty string explicitly selects the notebook root. Do not combine with path. Obtain IDs from list, search_docs or get."},
- "path": {Type: "string", Description: "Legacy hPath for compatibility-mode notebooks only (for create, list, move). Structured-title notebooks reject this parameter with legacyPathUnsupported; use parentID instead."},
+ "path": {Type: "string", Description: "Legacy hPath (for create, list, move). Requires a unique ancestor chain with non-empty titles and no literal slash. Conflicts return pathAmbiguous or legacyPathUnsupported; unavailable metadata returns indexUnavailable. Prefer parentID."},
"markdown": {Type: "string", Description: "Initial markdown content (for create).\n\n" + markdownContentSyntax},
"keyword": {Type: "string", Description: "Search keyword (for search_docs)"},
"notebook": {Type: "string", Description: "Notebook ID (required for create, list, move)"},
diff --git a/kernel/mcp/tools/inbox.go b/kernel/mcp/tools/inbox.go
index 066049f42c..935fb53901 100644
--- a/kernel/mcp/tools/inbox.go
+++ b/kernel/mcp/tools/inbox.go
@@ -30,7 +30,7 @@ import (
// 收集箱数据存放在思源云端,需要订阅会员;底层复用 model 层的云端 shorthand 读写函数。
var InboxTool = &Tool{
Name: "inbox",
- Description: "Inbox management (cloud-clipped web pages, messages, and audio/video/file attachments; requires subscription). Actions: list(page=1), get(id), convert(ids, notebook, parentID=notebook root by default, remove_after=true) — converts one or more shorthands into local documents, deleting the cloud originals on success. Use parentID=\"\" for root. Legacy path is available only in compatibility-mode notebooks.",
+ Description: "Inbox management (cloud-clipped web pages, messages, and audio/video/file attachments; requires subscription). Actions: list(page=1), get(id), convert(ids, notebook, parentID=notebook root by default, remove_after=true) — converts one or more shorthands into local documents, deleting the cloud originals on success. Use parentID=\"\" for root. Legacy path requires a unique ancestor chain with non-empty titles and no literal slash; conflicts or unavailable indexes return explicit errors.",
InputSchema: ToolSchema{
Type: "object",
Properties: map[string]Property{
@@ -40,7 +40,7 @@ var InboxTool = &Tool{
"ids": {Type: "string", Description: "Comma-separated shorthand IDs (for convert), e.g. \"1700000000000,1700000000001\""},
"notebook": {Type: "string", Description: "Target non-encrypted notebook ID (for convert)"},
"parentID": DocumentTool.InputSchema.Properties["parentID"],
- "path": {Type: "string", Description: "Legacy hPath in compatibility-mode notebooks only. Its parent must already exist. Do not combine with parentID; structured-title notebooks require parentID instead."},
+ "path": {Type: "string", Description: "Legacy hPath whose target parent must already exist. Requires a unique ancestor chain with non-empty titles and no literal slash. Conflicts return pathAmbiguous or legacyPathUnsupported; unavailable metadata returns indexUnavailable. Do not combine with parentID."},
"remove_after": {Type: "boolean", Description: "Whether to delete the cloud shorthand after a successful conversion (default true)"},
},
Required: []string{"action"},
diff --git a/kernel/model/document_plan.go b/kernel/model/document_plan.go
index 40cdf12cee..114de78221 100644
--- a/kernel/model/document_plan.go
+++ b/kernel/model/document_plan.go
@@ -41,7 +41,6 @@ type renderedDocumentTarget struct {
parents []string
title string
missing string
- legacy bool
}
func documentParentLocation(boxID string, id *string) (*string, error) {
@@ -57,7 +56,7 @@ func documentParentLocation(boxID string, id *string) (*string, error) {
// renderDocumentTarget 只对旧模板拆分渲染结果;版本 2 的每一段在同一时间快照独立求值。
func renderDocumentTarget(target *EffectiveDocumentTarget, request DocumentPlanRequest) (ret renderedDocumentTarget, err error) {
- ret.missing, ret.legacy = "create", false
+ ret.missing = "create"
if request.Now.IsZero() {
request.Now = time.Now()
}
@@ -143,17 +142,10 @@ func renderDocumentTarget(target *EffectiveDocumentTarget, request DocumentPlanR
return
}
default:
- ret.parents = append(ret.parents, normalizeDocTitle(segment))
+ ret.parents = append(ret.parents, normalizeDocumentTitle(segment))
}
}
}
- // 兼容旧 parentID 优先规则:相同展示位置仍使用调用方的实际父身份。
- if ret.legacy && request.ContextID != nil && target.Notebook == request.Notebook && ret.parentID == nil {
- if current, readErr := getDocumentPath(target.Notebook, *request.ContextID); readErr == nil &&
- current.DisplayPath == "/"+strings.Join(ret.parents, "/") {
- ret.parentID, ret.parents = request.ContextID, nil
- }
- }
if ret.parentID != nil && IsBoxDoc(target.Notebook, *ret.parentID) {
ret.parentID = nil
}
@@ -161,7 +153,7 @@ func renderDocumentTarget(target *EffectiveDocumentTarget, request DocumentPlanR
}
// PlanDocumentCreation 预先验证整组标题并分配全部 ID,不创建源文件或目录。
-// 旧模板保留兼容模式的同名选择,新对象逐级拒绝歧义;执行仍按单篇提交并保留已完成结果。
+// 各类模板均逐级拒绝歧义;执行仍按单篇提交并保留已完成结果。
func PlanDocumentCreation(request DocumentPlanRequest) (*DocumentCreatePlan, error) {
if !ast.IsNodeIDPattern(request.ID) || request.ContextID != nil && !ast.IsNodeIDPattern(*request.ContextID) ||
request.Placement != "" && request.Placement != "configured" && request.Placement != "child" {
@@ -254,17 +246,11 @@ func PlanDocumentCreation(request DocumentPlanRequest) (*DocumentCreatePlan, err
return nil, resolveErr
}
if resolved.Status == "ambiguous" {
- if !rendered.legacy {
- return nil, documentAddressingError("planChanged", fmt.Errorf("ambiguous document ancestor [%s]", title))
- }
- resolved.Resolved = legacyDocumentCandidate(target.Notebook, resolved.Candidates)
- resolved.Status = "resolved"
+ return nil, documentAddressingError("planChanged", fmt.Errorf("ambiguous document ancestor [%s]", title))
}
if resolved.Status == "resolved" {
id := resolved.Resolved.ID
- if !rendered.legacy {
- conditions = append(conditions, treenode.DocumentNameCondition{ParentID: parentID, Title: title, ExpectedID: &id})
- }
+ conditions = append(conditions, treenode.DocumentNameCondition{ParentID: parentID, Title: title, ExpectedID: &id})
parentID, revision = &id, resolved.Resolved.Revision
continue
}
@@ -290,10 +276,6 @@ func PlanDocumentCreation(request DocumentPlanRequest) (*DocumentCreatePlan, err
return plan, nil
}
if resolved.Status == "ambiguous" {
- if rendered.legacy {
- plan.ExistingID = legacyDocumentCandidate(target.Notebook, resolved.Candidates).ID
- return plan, nil
- }
return nil, documentAddressingError("planChanged", errors.New("ambiguous document reuse target"))
}
}
@@ -337,17 +319,6 @@ func documentCreationPlanPaths(plan *DocumentCreatePlan) (map[string]string, err
return paths, nil
}
-func legacyDocumentCandidate(boxID string, candidates []*treenode.DocumentPath) *treenode.DocumentPath {
- if previous := treenode.GetBlockTreeRootByHPath(boxID, candidates[0].DisplayPath); previous != nil {
- for _, candidate := range candidates {
- if candidate.ID == previous.ID {
- return candidate
- }
- }
- }
- return candidates[0]
-}
-
// executeDocumentCreationPlan 保留已完成文档,下一步使用实际提交后的父版本,不重放正文副作用。
func executeDocumentCreationPlan(plan *DocumentCreatePlan, creationLocked bool) (*treenode.DocumentPath, error) {
if plan.ExistingID != "" {
diff --git a/kernel/model/document_plan_test.go b/kernel/model/document_plan_test.go
index 2967470578..cf1f7cd8b3 100644
--- a/kernel/model/document_plan_test.go
+++ b/kernel/model/document_plan_test.go
@@ -4,6 +4,7 @@ package model
import (
"context"
+ "errors"
"os"
"os/exec"
"path"
@@ -189,4 +190,33 @@ func TestDocumentCreationPlans(t *testing.T) {
}
}
})
+ t.Run("all template formats reject ambiguous ancestors and reuse targets", func(t *testing.T) {
+ for i := 0; i < 2; i++ {
+ newHPathTestDoc(t, box.ID, "/", "Ambiguous parent", 1)
+ newHPathTestDoc(t, box.ID, "/", "Ambiguous reuse", 1)
+ }
+ for _, structured := range []bool{false, true} {
+ for _, reuse := range []bool{false, true} {
+ target := &EffectiveDocumentTarget{Notebook: box.ID, Path: "/Ambiguous parent/Title"}
+ parents, title := []string{"Ambiguous parent"}, "Title"
+ if reuse {
+ target.Path, parents, title = "/Ambiguous reuse", []string{}, "Ambiguous reuse"
+ }
+ if structured {
+ target.Structured = &conf.DocumentTarget{Version: 2, Notebook: box.ID,
+ Anchor: conf.DocumentTargetAnchor{Kind: "notebookRoot"}, Parents: parents, Title: title, OnMissingParent: "create"}
+ }
+ id := ast.NewNodeID()
+ plan, err := PlanDocumentCreation(DocumentPlanRequest{Notebook: box.ID, Purpose: "document", ID: id,
+ Reuse: reuse, Target: target})
+ var failure *DocumentAddressingError
+ if plan != nil || !errors.As(err, &failure) || failure.Reason != "planChanged" {
+ t.Fatalf("structured=%t reuse=%t selected an ambiguous document: %+v, %v", structured, reuse, plan, err)
+ }
+ if treenode.GetBlockTree(id) != nil {
+ t.Fatal("rejected plan created a document")
+ }
+ }
+ }
+ })
}