{"record":{"id":"3d1a460d4d7caa8c","repo":"siyuan-note/siyuan","slug":"the-document-used-to-render-the-template-has-chang","errorCode":null,"errorMessage":"the document used to render the template has changed","messagePattern":"the document used to render the template has changed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":494,"sourceCode":"\t}\n\n\tplanID := target.TemplateDocTreePlanID\n\ttarget.TemplateDocTreePlanID = \"\"\n\tvalue, loaded := templateDocTreePlans.LoadAndDelete(planID)\n\tif !loaded {\n\t\treturn false, errors.New(\"template document tree plan is missing or has expired\")\n\t}\n\tplan, ok := value.(*templateDocTreePlan)\n\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,","sourceCodeStart":476,"sourceCodeEnd":512,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L476-L512","documentation":"The plan also snapshots the root document's path and human path at render time. When attaching, the kernel reloads the tree by root ID and compares Box, Path, and HPath with the snapshot. If the document was renamed, moved between notebooks, deleted, or changed path since rendering, the plan no longer matches disk state and the commit is rejected to avoid corrupting a moved document.","triggerScenarios":"User renames or moves the root document (or its ancestors) between rendering the template and committing the transaction; the document is deleted; sync changes the tree concurrently.","commonSituations":"Long delay between render and commit during which the doc tree is edited; another device syncs a rename; a plugin programmatically moves the document mid-flow.","solutions":["Re-render the template after any rename/move of the root document and commit with the fresh plan","Avoid renaming or moving the source document while a template plan is pending","If the document was deleted, recreate it or abort the template application"],"exampleFix":"// before\nrenderTemplate(doc); renameDoc(doc, \"new-name\"); commit(planID) // path changed\n// after\nrenderTemplate(doc); commit(planID); renameDoc(doc, \"new-name\")","handlingStrategy":"retry","validationCode":"const cur = await loadTreeByBlockId(plan.rootId); if (!cur || cur.path !== plan.rootPath || cur.hPath !== plan.rootHPath) { planId = await renderTemplateDocTree(); }","typeGuard":"null","tryCatchPattern":"try { commit(txWith(planId)) } catch (e) { if (String(e).includes('render the template has changed')) { planId = await renderTemplateDocTree(); commit(txWith(planId)) } else { throw e } }","preventionTips":["Do not rename or move the source document while a template plan is pending","Pause user edits of the doc tree between render and commit","Re-render and re-commit whenever the root document's path changed"],"tags":["transaction","document-changed","template","stale-state"],"backgroundTag":"invalid-state-transition","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"}