{"record":{"id":"152dafca90255f38","repo":"siyuan-note/siyuan","slug":"template-document-tree-plans-cannot-be-attached-to","errorCode":null,"errorMessage":"template document tree plans cannot be attached to replay transactions","messagePattern":"template document tree plans cannot be attached to replay transactions","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":464,"sourceCode":"}\n\n// AttachTemplateDocTreePlans 将一次性计划转换为内核事务操作，父文档内容与全部子文档共用一条撤销记录。\nfunc AttachTemplateDocTreePlans(transactions []*Transaction) (attached bool, err error) {\n\tvar target *Transaction\n\tfor _, transaction := range transactions {\n\t\tif nil == transaction || \"\" == transaction.TemplateDocTreePlanID {\n\t\t\tcontinue\n\t\t}\n\t\tif nil != target || 1 != len(transactions) {\n\t\t\treturn false, errors.New(\"a document tree plan must be applied in a single transaction\")\n\t\t}\n\t\ttarget = transaction\n\t}\n\tif nil == target {\n\t\treturn false, nil\n\t}\n\tif target.isReplay {\n\t\treturn false, errors.New(\"template document tree plans cannot be attached to replay transactions\")\n\t}\n\tif 0 == len(target.DoOperations) || 0 == len(target.UndoOperations) {\n\t\treturn false, errors.New(\"template document tree plan requires reversible parent operations\")\n\t}\n\tif err = validateTemplateDocTreeParentOperations(target); nil != err {\n\t\treturn false, err\n\t}\n\tfor _, operation := range append(append([]*Operation{}, target.DoOperations...), target.UndoOperations...) {\n\t\tif nil != operation && (\"restoreCreatedDoc\" == operation.Action || \"removeCreatedDoc\" == operation.Action) {\n\t\t\treturn false, errors.New(\"template document tree transaction contains a reserved operation\")\n\t\t}\n\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\")","sourceCodeStart":446,"sourceCodeEnd":482,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L446-L482","documentation":"Template document tree plans cannot be attached to transactions that are being replayed (isReplay). Replay transactions are kernel-internal redrives of earlier commits; attaching a plan there would create documents twice and corrupt undo history, so the kernel refuses.","triggerScenarios":"A transaction carrying TemplateDocTreePlanID is passed through the replay/rollback machinery instead of a normal commit — e.g. the plan ID survived into a transaction reconstructed during undo/redo replay.","commonSituations":"Undo/redo of a commit whose payload still contains the plan ID because AttachTemplateDocTreePlans was never allowed to consume it; custom client code re-submitting an old transaction payload.","solutions":["Resubmit the transaction as a normal (non-replay) commit so the plan attaches and is consumed","Clear TemplateDocTreePlanID before feeding a transaction into the replay path","Regenerate the plan if it expired and commit once through the standard flow"],"exampleFix":"// before\ntx.isReplay = true; tx.TemplateDocTreePlanID = planID\n// after\ntx.isReplay = false; tx.TemplateDocTreePlanID = planID; CommitTx(tx)","handlingStrategy":"validation","validationCode":"if (tx.isReplay && tx.templateDocTreePlanID) { delete tx.templateDocTreePlanID }","typeGuard":"null","tryCatchPattern":"try { commit(tx) } catch (e) { if (String(e).includes('replay transactions')) { tx.templateDocTreePlanID = ''; commit(tx) } else { throw e } }","preventionTips":["Clear TemplateDocTreePlanID before any undo/redo replay of captured transactions","Only attach plans at initial render-and-commit time","Treat replay transactions as kernel-internal and never enrich them"],"tags":["transaction","replay","template"],"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"}