{"record":{"id":"87b2323f4eb96261","repo":"siyuan-note/siyuan","slug":"template-document-tree-plan-parent-operations-are","errorCode":null,"errorMessage":"template document tree plan parent operations are not reversible","messagePattern":"template document tree plan parent operations are not reversible","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":561,"sourceCode":"\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\n\t\t}\n\t\tkey := inverseAction + \"\\x00\" + operation.ID\n\t\tif 1 > undoOperations[key] {\n\t\t\treturn errors.New(\"template document tree plan parent operations are not reversible\")\n\t\t}\n\t\tundoOperations[key]--\n\t}\n\tif !hasContentMutation {\n\t\treturn errors.New(\"template document tree plan requires a parent content operation\")\n\t}\n\tfor _, count := range undoOperations {\n\t\tif 0 != count {\n\t\t\treturn errors.New(\"template document tree plan parent operations are not reversible\")\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc transactionTargetsTemplateRoot(transaction *Transaction, rootID, boxID string) bool {\n\tmatched := false\n\tfor operationSetIndex, operations := range [][]*Operation{transaction.DoOperations, transaction.UndoOperations} {\n\t\tfor _, operation := range operations {","sourceCodeStart":543,"sourceCodeEnd":579,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L543-L579","documentation":"For every do-operation the validator looks up undoOperations[inverseAction\\x00ID]; if no matching undo operation exists (count below 1), the transaction is not reversible and the plan cannot be attached. The plan machinery appends restoreCreatedDoc/removeCreatedDoc pairs and assumes the surrounding parent transaction can itself be undone operation-by-operation.","triggerScenarios":"Attaching a plan to a transaction whose DoOperations include an operation whose matching inverse (per inverseActions) is absent from UndoOperations, e.g. a do 'insert' with no undo 'delete' carrying the same ID, or the undo entry uses a different action pairing than the map expects.","commonSituations":"Hand-built transactions where undo entries were generated with mismatched IDs; partially recorded undo stacks after an earlier failure; plugin code appending do operations without adding the inverse undo entries.","solutions":["For each do operation, add the inverse undo operation (insert->delete, delete->insert, update->update, foldHeading<->unfoldHeading, setAttrs->setAttrs) with the same operation ID","Keep do/undo construction in a single helper so IDs always match","Verify pairing by checking each do op's ID appears in an undo op before calling AttachTemplateDocTreePlans"],"exampleFix":"// before: insert without undo\nops.DoOperations = []*Operation{{Action: \"insert\", ID: id}}\n// after\nops.DoOperations = []*Operation{{Action: \"insert\", ID: id}}\nops.UndoOperations = []*Operation{{Action: \"delete\", ID: id}}","handlingStrategy":"validation","validationCode":"undoByID := map[string]string{}\nfor _, op := range tx.UndoOperations {\n    undoByID[op.Action+\"\\x00\"+op.ID] = op.Action\n}\nfor _, op := range tx.DoOperations {\n    inv := inverseOf(op.Action)\n    if _, ok := undoByID[inv+\"\\x00\"+op.ID]; !ok {\n        return fmt.Errorf(\"do op %s (%s) has no inverse undo\", op.ID, op.Action)\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create do/undo pairs together with shared IDs","Verify inverse pairing before attaching plans","Avoid incrementally building undo stacks across retries"],"tags":["go","transaction","reversibility"],"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"}