{"record":{"id":"78369f838790e26f","repo":"siyuan-note/siyuan","slug":"parse-document-history-failed-w","errorCode":null,"errorMessage":"parse document history failed: %w","messagePattern":"parse document history failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":367,"sourceCode":"\t\tdek, dekErr := GetDEKIfUnlocked(origBoxID)\n\t\tif dekErr != nil {\n\t\t\terr = errors.New(Conf.Language(314))\n\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) {","sourceCodeStart":349,"sourceCodeEnd":385,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L349-L385","documentation":"After any required decryption, loadTreeByData0 could not parse the history snapshot into a document tree. The wrap preserves the underlying parse error while attributing it to history parsing. This means the .sy bytes (plaintext or decrypted) are not valid JSON matching the tree schema, or are corrupted/truncated.","triggerScenarios":"RollbackDocHistory on a history file whose data (after optional DecryptFile) fails dataparser/lute parsing — corrupted snapshot, truncated write, wrong file content, or decrypting with a wrong-but-accepted key producing garbage.","commonSituations":"Disk corruption or interrupted sync left a partial .sy; the file was overwritten by another process; decrypting a snapshot with mismatched key material; very old history format no longer parseable.","solutions":["Read the wrapped cause (%w) to see the concrete JSON/parse failure and fix accordingly","Verify the file is complete (not zero-byte or truncated); re-sync or restore that history file from backup","If decryption produced garbage, confirm you are decrypting with the original box's DEK, not a fallback box's","Pick a different history snapshot to roll back to; abandon the corrupted one","Ensure kernel and history format versions are compatible (no downgrade across format changes)"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"try { JSON.parse(await fs.promises.readFile(historyPath, 'utf8')); } catch { /* skip corrupted snapshot */ }","typeGuard":null,"tryCatchPattern":"try { await historyRollback({ historyPath }) } catch (e) { if (String(e.msg).includes('parse document history failed')) { pickAlternativeSnapshot(); } else { throw e } }","preventionTips":["Keep history files in backups and verify sync completes without truncation","Do not edit data/history files externally","Keep kernel versions consistent so history formats stay parseable"],"tags":["history","parse-error","corruption","rollback"],"backgroundTag":"json-parse-error","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"}