siyuan-note/siyuan · error

encrypted document history is missing notebook context

Error message

encrypted document history is missing notebook context

What it means

GetDocHistoryContent reads a document snapshot from history. For ciphertext snapshots, the history file path must encode the owning notebook ID as a path segment so the correct DEK can be located. When the relative path has fewer than 3 segments or its second segment is not a node ID, the snapshot lacks notebook context and decryption is impossible, so the reader fails with "encrypted document history is missing notebook context" while preserving the original data.

Solutions

  1. Regenerate history for the document (re-save the doc so a new snapshot with the current path layout is written) and read the new entry
  2. Verify the history file lives at <HistoryDir>/<...>/<boxID>/<docID>.sy with a valid notebook-ID segment; move/repair misplaced history files
  3. If the snapshot predates the current encrypted-history format, restore it into a notebook via the documented recovery migration instead of reading it directly

Example fix

// before
content, err := model.GetDocHistoryContent("history/2024/old-snapshot.sy")
// after
// path must include the notebook ID segment for ciphertext snapshots
content, err := model.GetDocHistoryContent("history/2024/20240101120000/<boxID>/<docID>.sy")
Defensive patterns

Strategy: validation

Validate before calling

rel := strings.TrimPrefix(filepath.ToSlash(historyPath), filepath.ToSlash(util.HistoryDir))
parts := strings.SplitN(strings.TrimPrefix(rel, "/"), "/", 3)
if len(parts) < 3 || !ast.IsNodeIDPattern(parts[1]) {
    return errors.New("history path must embed a valid notebook ID segment")
}

Type guard

func hasNotebookContext(historyPath string) bool {
    parts := strings.SplitN(filepath.ToSlash(historyPath), "/", 3)
    return len(parts) == 3 && ast.IsNodeIDPattern(parts[1])
}

Try / catch

content, err := model.GetDocHistoryContent(historyPath)
if err != nil && strings.Contains(err.Error(), "missing notebook context") {
    // snapshot predates current layout: regenerate or migrate instead of retrying
}

Prevention

When it happens

Trigger: Requesting content of an encrypted document's history snapshot where the historyPath under util.HistoryDir does not follow the expected <something>/<boxID>/<file> layout — e.g. history entries created by an older SiYuan version before notebook context was added to encrypted history paths, or a hand-crafted/malformed path passed to the API.

Common situations: Upgrading from a version whose encrypted history layout lacked the notebook-ID segment; restoring history files from a partial backup into the wrong directory depth; plugins constructing history paths manually.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/history.go:183

	historyPath, err = validateHistoryPath(historyPath)
	if err != nil {
		return
	}

	data, err := filelock.ReadFile(historyPath)
	if err != nil {
		logging.LogErrorf("read file [%s] failed: %s", historyPath, err)
		return
	}

	// 加密笔记本的历史是密文,按路径里的 boxID 解密后解析
	relPath := strings.TrimPrefix(filepath.ToSlash(historyPath), filepath.ToSlash(util.HistoryDir))
	relPath = strings.TrimPrefix(relPath, "/")
	pathParts := strings.SplitN(relPath, "/", 3)
	ciphertext := util.IsCiphertext(data)
	if ciphertext {
		if len(pathParts) < 3 || !ast.IsNodeIDPattern(pathParts[1]) {
			err = errors.New("encrypted document history is missing notebook context")
			return
		}
		histBoxID := pathParts[1]
		if !IsEncryptedBox(histBoxID) {
			err = fmt.Errorf("encrypted document history has no matching notebook [%s]", histBoxID)
			return
		}
		HoldBoxReadLock(histBoxID)
		defer ReleaseBoxReadLock(histBoxID)
		dek, dekErr := GetDEKIfUnlocked(histBoxID)
		if dekErr != nil {
			err = errors.New(Conf.Language(314))
			return
		}
		data, err = DecryptFile(histBoxID, pathParts[2], dek, data)
		if err != nil {
			logging.LogErrorf("decrypt history [%s] failed: %s", historyPath, err)
			return

View on GitHub (pinned to 9f775e8a12)