{"record":{"id":"43a964ddc5837c2f","repo":"siyuan-note/siyuan","slug":"template-document-tree-plan-requires-reversible-pa","errorCode":null,"errorMessage":"template document tree plan requires reversible parent operations","messagePattern":"template document tree plan requires reversible parent operations","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":467,"sourceCode":"func 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\")\n\t}\n\tplan, ok := value.(*templateDocTreePlan)\n\tif !ok || plan.id != planID || time.Now().After(plan.expiresAt) {","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L449-L485","documentation":"A transaction carrying a template document tree plan must have both non-empty DoOperations and UndoOperations, because the plan's created documents are appended to these lists and the parent operations define reversibility. If either list is empty the kernel cannot fold the plan into a reversible transaction and rejects it.","triggerScenarios":"Committing a transaction whose only payload is a TemplateDocTreePlanID with no parent document operations (empty DoOperations or UndoOperations), e.g. the client sent the plan ID without the corresponding document-update operations.","commonSituations":"Frontend bug where the plan ID is attached to a bare transaction; operations were stripped by serialization; plugin constructed the transaction manually and forgot the undo operations.","solutions":["Attach the plan ID to the transaction that actually modifies the parent document (it must have both do and undo operations)","Verify the transaction payload still contains operations after serialization before committing","Regenerate the plan and re-render the template if the parent transaction was lost"],"exampleFix":"// before\n{\"transactions\":[{\"templateDocTreePlanID\":\"p1\",\"doOps\":[],\"undoOps\":[]}]}\n// after\n{\"transactions\":[{\"templateDocTreePlanID\":\"p1\",\"doOps\":[insertOps],\"undoOps\":[deleteOps]}]}","handlingStrategy":"validation","validationCode":"if (!tx.doOperations.length || !tx.undoOperations.length) throw new Error('plan transaction needs reversible parent operations')","typeGuard":"null","tryCatchPattern":"try { commit(tx) } catch (e) { if (String(e).includes('reversible parent operations')) { rebuildParentOps(tx); commit(tx) } else { throw e } }","preventionTips":["Attach the plan ID to the parent-document editing transaction, not to an empty one","Verify do/undo operations survive your serialization layer","Build transactions through the standard kernel API instead of hand-rolling"],"tags":["transaction","undo-redo","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"}