siyuan-note/siyuan · error

parse document history failed

Error message

parse document history failed: %w

What it means

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.

Solutions

  1. Read the wrapped cause (%w) to see the concrete JSON/parse failure and fix accordingly
  2. Verify the file is complete (not zero-byte or truncated); re-sync or restore that history file from backup
  3. If decryption produced garbage, confirm you are decrypting with the original box's DEK, not a fallback box's
  4. Pick a different history snapshot to roll back to; abandon the corrupted one
  5. Ensure kernel and history format versions are compatible (no downgrade across format changes)
Defensive patterns

Strategy: try-catch

Validate before calling

try { JSON.parse(await fs.promises.readFile(historyPath, 'utf8')); } catch { /* skip corrupted snapshot */ }

Try / catch

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

Prevention

When it happens

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

Common situations: 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.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/history.go:367

		dek, dekErr := GetDEKIfUnlocked(origBoxID)
		if dekErr != nil {
			err = errors.New(Conf.Language(314))
			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) {

View on GitHub (pinned to 9f775e8a12)