{"record":{"id":"891cd89ed830f41a","repo":"siyuan-note/siyuan","slug":"invalid-created-doc-compensation-snapshot","errorCode":null,"errorMessage":"invalid created doc compensation snapshot","messagePattern":"invalid created doc compensation snapshot","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/transaction.go","lineNumber":2983,"sourceCode":"\t\t}\n\t\tif restoreErr := restoreCreatedDocTreeSnapshot(tree); nil != restoreErr {\n\t\t\terr = errors.Join(err, restoreErr)\n\t\t\tcontinue\n\t\t}\n\t\tif box := Conf.Box(tree.Box); nil != box {\n\t\t\tbox.setSortByConf(path.Dir(tree.Path), tree.ID)\n\t\t\tPushCreate(box, tree.Path, nil)\n\t\t}\n\t}\n\tif nil != tx.templateDocTreeRootSnapshot {\n\t\terr = errors.Join(err, restoreCreatedDocTreeSnapshot(tx.templateDocTreeRootSnapshot))\n\t}\n\treturn\n}\n\nfunc restoreCreatedDocTreeSnapshot(tree *parse.Tree) error {\n\tif nil == tree || nil == tree.Root {\n\t\treturn errors.New(\"invalid created doc compensation snapshot\")\n\t}\n\tif err := writeTreeUpsertQueue(tree); nil != err {\n\t\treturn err\n\t}\n\ttreenode.UpsertBlockTree(tree)\n\treturn nil\n}\n\nfunc (tx *Transaction) loadTreeByBlockTree(bt *treenode.BlockTree) (ret *parse.Tree, err error) {\n\tif nil == bt {\n\t\treturn nil, ErrBlockNotFound\n\t}\n\n\tret = tx.trees[bt.RootID]\n\tif nil != ret {\n\t\treturn\n\t}\n","sourceCodeStart":2965,"sourceCodeEnd":3001,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/transaction.go#L2965-L3001","documentation":"restoreCreatedDocTreeSnapshot is the compensation writer for template-created documents; it requires a fully formed tree with a non-nil Root. A nil tree or a tree missing its root cannot be written via writeTreeUpsertQueue, so the guard rejects it as an invalid compensation snapshot.","triggerScenarios":"Transaction compensation calling restoreCreatedDocTreeSnapshot(nil) or with a partially constructed *parse.Tree whose Root was never set — e.g. a tree built from a failed load or a placeholder entry.","commonSituations":"A prior rollback step stored a nil/partial tree in attemptedTemplateCreatedDocs; corrupted .sy load produced a tree without a root; programming error building the compensation list.","solutions":["Ensure the tree passed in was loaded from disk via LoadTreeByBlockID/LoadTree rather than constructed ad hoc","Check the code that populates attemptedTemplateCreatedDocs for nil/partial entries","Re-load the document tree and retry the compensation","Skip logging-and-skip rather than writing malformed trees if the doc is unrecoverable"],"exampleFix":"// before\nrestoreCreatedDocTreeSnapshot(partialTree) // Root == nil\n// after\nif tree != nil && tree.Root != nil {\n    restoreCreatedDocTreeSnapshot(tree)\n}","handlingStrategy":"validation","validationCode":"if tree == nil || tree.Root == nil { return errors.New(\"cannot restore: incomplete tree\") }\nrestoreCreatedDocTreeSnapshot(tree)","typeGuard":"func restorableTree(t *parse.Tree) bool { return t != nil && t.Root != nil }","tryCatchPattern":"if err := restoreCreatedDocTreeSnapshot(tree); err != nil && strings.Contains(err.Error(), \"invalid created doc compensation snapshot\") { reloadTree(); retry() }","preventionTips":["Only pass trees loaded by LoadTree/LoadTreeByBlockID into compensation code","Validate tree.Root before storing trees in compensation lists","Handle failed loads without inserting placeholder trees","Add tests for compensation with nil and rootless trees"],"tags":["transaction","rollback","invariant"],"backgroundTag":"internal-invariant-violation","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"}