{"record":{"id":"80d1f877321faa96","repo":"siyuan-note/siyuan","slug":"parse-document-history-failed","errorCode":null,"errorMessage":"parse document history failed","messagePattern":"parse document history failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":370,"sourceCode":"\t\t\treturn\n\t\t}\n\t\tvar decErr error\n\t\t// 历史路径格式：<historyDir>/<datePrefix>/<boxID>/<relativePath>\n\t\t// 用原始 boxID 解密（getRollbackBox 可能创建了新 box，密文仍属于原加密 box）\n\t\tfilePath := parts[2]\n\t\tsrcData, decErr = DecryptFile(origBoxID, filePath, dek, srcData)\n\t\tif decErr != nil {\n\t\t\tlogging.LogErrorf(\"decrypt history [%s] failed: %s\", srcPath, decErr)\n\t\t\terr = decErr\n\t\t\treturn\n\t\t}\n\t}\n\ttree, parseErr := loadTreeByData0(srcData)\n\tif parseErr != nil {\n\t\treturn fmt.Errorf(\"parse document history failed: %w\", parseErr)\n\t}\n\tif tree == nil {\n\t\treturn errors.New(\"parse document history failed\")\n\t}\n\tif encrypted && tree.Root.ID+\".sy\" != filepath.Base(historyPath) {\n\t\treturn errors.New(\"encrypted document history root ID does not match its filename\")\n\t}\n\tboundRecovery := &Transaction{trees: map[string]*parse.Tree{}}\n\tdefer func() { boundRecovery.finishAttributeViewMutation(err != nil) }()\n\tif nil != tree {\n\t\thistoryDir := filepath.Join(util.HistoryDir, parts[0])\n\n\t\tavNodes := tree.Root.ChildrenByType(ast.NodeAttributeView)\n\t\tfor _, avNode := range avNodes {\n\t\t\tsrcAvPath := filepath.Join(historyDir, \"storage\", \"av\", avNode.AttributeViewID+\".json\")\n\t\t\t// 加密笔记本的 AV 定义在笔记本级目录\n\t\t\tdestAvPath := filepath.Join(util.DataDir, \"storage\", \"av\", avNode.AttributeViewID+\".json\")\n\t\t\tif IsEncryptedBox(boxID) {\n\t\t\t\t// 历史目录里 AV 也可能在 boxID 子目录下\n\t\t\t\tboxSrcAvPath := filepath.Join(historyDir, boxID, \"storage\", \"av\", avNode.AttributeViewID+\".json\")\n\t\t\t\tif !gulu.File.IsExist(boxSrcAvPath) {","sourceCodeStart":352,"sourceCodeEnd":388,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L352-L388","documentation":"loadTreeByData0 returned no error but also a nil tree — an internal invariant violation where parsing neither succeeded nor reported why. The kernel converts this into a plain 'parse document history failed' so callers get a definitive failure instead of a nil-pointer panic downstream.","triggerScenarios":"RollbackDocHistory reaching the tree==nil branch after loadTreeByData0 returned (nil, nil) — typically when the parser silently declines the input (e.g. empty or degenerate data edge case).","commonSituations":"Empty or whitespace-only history file; a parser regression on unusual snapshot data; calling with a non-.sy payload that slips past ciphertext checks.","solutions":["Inspect the history file contents; replace empty/degenerate snapshots with a valid one","Retry with a different history snapshot of the same document","Check kernel version — update if the history format changed and the parser was fixed upstream","Report the snapshot to maintainers if it looks valid but consistently yields a nil tree (internal invariant bug)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const raw = await fs.promises.readFile(historyPath, 'utf8');\nif (!raw || !raw.trim()) throw new Error('empty history snapshot, pick another');","typeGuard":null,"tryCatchPattern":"try { await historyRollback({ historyPath }) } catch (e) { if (String(e.msg) === 'parse document history failed') { reportSnapshotToMaintainers(historyPath); } else { throw e } }","preventionTips":["Avoid zero-byte snapshots (never truncate files manually)","Update the kernel if a parser regression is suspected","Always keep a second valid rollback target"],"tags":["history","parse-error","internal-invariant","rollback"],"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"}