{"record":{"id":"1afa2c4ed9e926dc","repo":"siyuan-note/siyuan","slug":"template-document-tree-plan-contains-an-invalid-do","errorCode":null,"errorMessage":"template document tree plan contains an invalid document snapshot","messagePattern":"template document tree plan contains an invalid document snapshot","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":503,"sourceCode":"\tif !ok || plan.id != planID || time.Now().After(plan.expiresAt) {\n\t\treturn false, errors.New(\"template document tree plan is invalid or has expired\")\n\t}\n\tif !transactionTargetsTemplateRoot(target, plan.rootID, plan.boxID) {\n\t\treturn false, errors.New(\"template document tree plan does not match the edited document\")\n\t}\n\trootTree, loadErr := LoadTreeByBlockID(plan.rootID)\n\tif nil != loadErr || nil == rootTree || rootTree.Box != plan.boxID || rootTree.Path != plan.rootPath ||\n\t\trootTree.HPath != plan.rootHPath {\n\t\treturn false, errors.New(\"the document used to render the template has changed\")\n\t}\n\ttarget.templateDocTreeRootSnapshot = rootTree\n\tbox := Conf.Box(plan.boxID)\n\tif nil == box {\n\t\treturn false, ErrBoxNotFound\n\t}\n\tfor _, tree := range plan.trees {\n\t\tif nil == tree || nil == tree.Root || tree.ID != tree.Root.ID || tree.Box != plan.boxID || box.Exist(tree.Path) {\n\t\t\treturn false, errors.New(\"template document tree plan contains an invalid document snapshot\")\n\t\t}\n\t}\n\n\tfor _, tree := range plan.trees {\n\t\ttarget.DoOperations = append(target.DoOperations, &Operation{\n\t\t\tAction:                \"restoreCreatedDoc\",\n\t\t\tID:                    tree.ID,\n\t\t\tTree:                  tree,\n\t\t\ttemplateDocTreeRootID: plan.rootID,\n\t\t})\n\t}\n\tfor index := len(plan.trees) - 1; 0 <= index; index-- {\n\t\ttree := plan.trees[index]\n\t\ttarget.UndoOperations = append(target.UndoOperations, &Operation{\n\t\t\tAction:                \"removeCreatedDoc\",\n\t\t\tID:                    tree.ID,\n\t\t\tTree:                  tree,\n\t\t\ttemplateDocTreeRootID: plan.rootID,","sourceCodeStart":485,"sourceCodeEnd":521,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L485-L521","documentation":"AttachTemplateDocTreePlans validates every pre-rendered document tree snapshot stored in a template doc-tree plan before appending restoreCreatedDoc operations to the parent transaction. A tree fails validation if it is nil, has no Root, its ID does not match its Root.ID, its Box does not match the plan's box, or a file already exists at tree.Path in that notebook. This is an internal invariant check that the plan snapshot is a coherent, not-yet-created document.","triggerScenarios":"Calling AttachTemplateDocTreePlans with a plan whose plan.trees slice contains a nil *parse.Tree, a tree without a Root node, a tree whose ID differs from tree.Root.ID, a tree whose Box field differs from plan.boxID, or a tree whose Path already exists on disk in the target notebook (box.Exist(tree.Path) true).","commonSituations":"A stale plan is attached after the target path was created by another transaction or sync run; concurrent operations in the same notebook created the file between rendering and attachment; corrupted in-memory plan state from a bug or from reusing an expired/mutated plan object.","solutions":["Re-render the template doc-tree plan to obtain fresh snapshots, then re-attach it in one atomic step","Check box.Exist(tree.Path) for each planned document before attaching and remove or rename conflicting paths","Ensure the plan is not reused after expiry; call AttachTemplateDocTreePlans promptly after rendering","Do not mutate plan.trees between render and attach; treat the plan as read-only"],"exampleFix":"// before: attaching a stale plan after the doc was already created elsewhere\nattached, err := AttachTemplateDocTreePlans(target, planID)\n// after: verify no path conflicts before attaching\nbox := Conf.Box(plan.boxID)\nfor _, tree := range plan.trees {\n    if box.Exist(tree.Path) {\n        return fmt.Errorf(\"planned doc path already exists: %s\", tree.Path)\n    }\n}\nattached, err := AttachTemplateDocTreePlans(target, planID)","handlingStrategy":"validation","validationCode":"box := Conf.Box(plan.boxID)\nfor _, tree := range plan.trees {\n    if tree == nil || tree.Root == nil || tree.ID != tree.Root.ID || tree.Box != plan.boxID || box.Exist(tree.Path) {\n        return errors.New(\"invalid plan snapshot\")\n    }\n}","typeGuard":"func isValidTreeSnapshot(tree *parse.Tree, boxID string) bool {\n    return tree != nil && tree.Root != nil && tree.ID == tree.Root.ID && tree.Box == boxID\n}","tryCatchPattern":null,"preventionTips":["Attach plans immediately after rendering; do not hold stale plans across other operations","Never mutate plan.trees after rendering","Check for path conflicts in the notebook before attaching","Re-render the plan rather than reusing an expired one"],"tags":["go","transaction","template","validation"],"backgroundTag":"internal-invariant-violation","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}