{"record":{"id":"df4635a7846f0417","repo":"siyuan-note/siyuan","slug":"template-document-tree-plan-contains-an-invalid-pa","errorCode":null,"errorMessage":"template document tree plan contains an invalid parent undo operation","messagePattern":"template document tree plan contains an invalid parent undo operation","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":539,"sourceCode":"\t\t\ttemplateDocTreeRootID: plan.rootID,\n\t\t})\n\t}\n\treturn true, nil\n}\n\nfunc validateTemplateDocTreeParentOperations(transaction *Transaction) error {\n\tinverseActions := map[string]string{\n\t\t\"insert\":        \"delete\",\n\t\t\"delete\":        \"insert\",\n\t\t\"update\":        \"update\",\n\t\t\"foldHeading\":   \"unfoldHeading\",\n\t\t\"unfoldHeading\": \"foldHeading\",\n\t\t\"setAttrs\":      \"setAttrs\",\n\t}\n\tundoOperations := map[string]int{}\n\tfor _, operation := range transaction.UndoOperations {\n\t\tif nil == operation || \"\" == operation.ID {\n\t\t\treturn errors.New(\"template document tree plan contains an invalid parent undo operation\")\n\t\t}\n\t\tif _, supported := inverseActions[operation.Action]; !supported || \"\" != operation.RootID {\n\t\t\treturn errors.New(\"template document tree plan contains an unsupported parent undo operation\")\n\t\t}\n\t\tundoOperations[operation.Action+\"\\x00\"+operation.ID]++\n\t}\n\n\thasContentMutation := false\n\tfor _, operation := range transaction.DoOperations {\n\t\tif nil == operation || \"\" == operation.ID {\n\t\t\treturn errors.New(\"template document tree plan contains an invalid parent operation\")\n\t\t}\n\t\tinverseAction, supported := inverseActions[operation.Action]\n\t\tif !supported || \"\" != operation.RootID {\n\t\t\treturn errors.New(\"template document tree plan contains an unsupported parent operation\")\n\t\t}\n\t\tif \"insert\" == operation.Action || \"delete\" == operation.Action || \"update\" == operation.Action {\n\t\t\thasContentMutation = true","sourceCodeStart":521,"sourceCodeEnd":557,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L521-L557","documentation":"validateTemplateDocTreeParentOperations requires each undo operation in the parent transaction to be non-nil and carry a non-empty ID. An undo operation with a nil pointer or blank operation ID cannot be matched against the transaction's do-operations, so the plan attachment is rejected. This guards the reversibility bookkeeping that pairs each do operation with its inverse undo operation.","triggerScenarios":"Attaching a template doc-tree plan to a transaction whose UndoOperations slice contains a nil *Operation or an operation with an empty ID field, e.g. when the caller built undo entries manually or deserialized them from an incomplete payload.","commonSituations":"Programmatic/API construction of transactions with partially filled operations; a serialization round-trip that dropped the ID field; test code assembling synthetic transactions without IDs.","solutions":["Ensure every entry in UndoOperations is a non-nil *Operation with a populated ID","Populate IDs when building undo operations in the same loop that creates do operations","Validate the transaction payload before calling AttachTemplateDocTreePlans"],"exampleFix":"// before\nundo := &Operation{Action: \"delete\"}\n// after\nundo := &Operation{Action: \"delete\", ID: op.ID}","handlingStrategy":"validation","validationCode":"for _, op := range tx.UndoOperations {\n    if op == nil || op.ID == \"\" {\n        return errors.New(\"undo operation missing ID\")\n    }\n}","typeGuard":"func hasValidUndoOps(ops []*Operation) bool {\n    for _, op := range ops {\n        if op == nil || op.ID == \"\" {\n            return false\n        }\n    }\n    return true\n}","tryCatchPattern":null,"preventionTips":["Always set IDs when constructing undo operations","Build do/undo pairs in the same helper function","Validate transactions before attaching plans"],"tags":["go","transaction","validation"],"backgroundTag":"empty-required-field","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"}