siyuan-note/siyuan · error

parse document history failed

Error message

parse document history failed

What it means

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.

Solutions

  1. Inspect the history file contents; replace empty/degenerate snapshots with a valid one
  2. Retry with a different history snapshot of the same document
  3. Check kernel version — update if the history format changed and the parser was fixed upstream
  4. Report the snapshot to maintainers if it looks valid but consistently yields a nil tree (internal invariant bug)
Defensive patterns

Strategy: try-catch

Validate before calling

const raw = await fs.promises.readFile(historyPath, 'utf8');
if (!raw || !raw.trim()) throw new Error('empty history snapshot, pick another');

Try / catch

try { await historyRollback({ historyPath }) } catch (e) { if (String(e.msg) === 'parse document history failed') { reportSnapshotToMaintainers(historyPath); } else { throw e } }

Prevention

When it happens

Trigger: 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).

Common situations: Empty or whitespace-only history file; a parser regression on unusual snapshot data; calling with a non-.sy payload that slips past ciphertext checks.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/80d1f877321faa96. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/history.go:370

			return
		}
		var decErr error
		// 历史路径格式:<historyDir>/<datePrefix>/<boxID>/<relativePath>
		// 用原始 boxID 解密(getRollbackBox 可能创建了新 box,密文仍属于原加密 box)
		filePath := parts[2]
		srcData, decErr = DecryptFile(origBoxID, filePath, dek, srcData)
		if decErr != nil {
			logging.LogErrorf("decrypt history [%s] failed: %s", srcPath, decErr)
			err = decErr
			return
		}
	}
	tree, parseErr := loadTreeByData0(srcData)
	if parseErr != nil {
		return fmt.Errorf("parse document history failed: %w", parseErr)
	}
	if tree == nil {
		return errors.New("parse document history failed")
	}
	if encrypted && tree.Root.ID+".sy" != filepath.Base(historyPath) {
		return errors.New("encrypted document history root ID does not match its filename")
	}
	boundRecovery := &Transaction{trees: map[string]*parse.Tree{}}
	defer func() { boundRecovery.finishAttributeViewMutation(err != nil) }()
	if nil != tree {
		historyDir := filepath.Join(util.HistoryDir, parts[0])

		avNodes := tree.Root.ChildrenByType(ast.NodeAttributeView)
		for _, avNode := range avNodes {
			srcAvPath := filepath.Join(historyDir, "storage", "av", avNode.AttributeViewID+".json")
			// 加密笔记本的 AV 定义在笔记本级目录
			destAvPath := filepath.Join(util.DataDir, "storage", "av", avNode.AttributeViewID+".json")
			if IsEncryptedBox(boxID) {
				// 历史目录里 AV 也可能在 boxID 子目录下
				boxSrcAvPath := filepath.Join(historyDir, boxID, "storage", "av", avNode.AttributeViewID+".json")
				if !gulu.File.IsExist(boxSrcAvPath) {

View on GitHub (pinned to 9f775e8a12)