siyuan-note/siyuan · error

encrypted document history is missing valid notebook context

Error message

encrypted document history is missing valid notebook context

What it means

When a history .sy file is detected as ciphertext, the loader derives the notebook ID from the path (history/<timestamp>/<boxID>/<file>) and requires it to be a valid node-ID-style notebook ID that is also a registered encrypted box. If the path is too shallow, the segment is not a notebook ID, or the box is not an encrypted notebook, decryption cannot proceed safely and this error is thrown.

Solutions

  1. Restore the standard history layout history/<timestamp>/<boxID>/<docID>.sy so the notebook ID segment is present and well-formed.
  2. Verify the notebook ID segment matches an existing encrypted notebook (IsEncryptedBox); if the notebook's encryption state changed, decrypt/re-encrypt consistently before diffing.
  3. Do not move or rename files inside the history directory; regenerate history from snapshots if the layout is broken.
  4. If the history was produced by an older format, restore it via the official history restore path rather than loading the raw file.
Defensive patterns

Strategy: validation

Validate before calling

// Go: pre-validate the history path layout timestamp/boxID/file.sy
parts := strings.Split(filepath.ToSlash(rel), "/")
if len(parts) < 3 || !ast.IsNodeIDPattern(parts[1]) || !model.IsEncryptedBox(parts[1]) {
    return errors.New("history path lacks valid encrypted notebook context")
}

Try / catch

catch (e) { if (e.message.includes("missing valid notebook context")) { repairOrRegenerateHistory(); } else { throw e; } }

Prevention

When it happens

Trigger: Requesting loadDocVersion on an encrypted history .sy whose path has fewer than 3 segments, whose second segment is not a 14-digit+ID notebook pattern (ast.IsNodeIDPattern fails), or whose notebook segment is not registered as an encrypted box (IsEncryptedBox false).

Common situations: History files copied/moved so the expected timestamp/boxID/file layout is broken; a notebook was converted to non-encrypted after history was created (or vice versa); hand-crafted or plugin-supplied history paths with wrong nesting; corrupted history directory structure.

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/36f8b2d0b4ef2af7. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/history_diff.go:352

	if err != nil {
		return nil, err
	}
	if !strings.HasSuffix(strings.ToLower(absPath), ".sy") {
		return nil, errors.New("history version is not a document")
	}
	relPath, err := filepath.Rel(util.HistoryDir, absPath)
	if err != nil {
		return nil, err
	}
	parts := strings.SplitN(filepath.ToSlash(relPath), "/", 3)
	data, err := filelock.ReadFile(absPath)
	if err != nil {
		return nil, err
	}
	ciphertext := util.IsCiphertext(data)
	if ciphertext {
		if len(parts) < 3 || !ast.IsNodeIDPattern(parts[1]) || !IsEncryptedBox(parts[1]) {
			return nil, errors.New("encrypted document history is missing valid notebook context")
		}
		HoldBoxReadLock(parts[1])
		defer ReleaseBoxReadLock(parts[1])
		dek, dekErr := GetDEKIfUnlocked(parts[1])
		if dekErr != nil {
			return nil, errors.New(Conf.Language(314))
		}
		data, err = DecryptFile(parts[1], parts[2], dek, data)
		if err != nil {
			return nil, err
		}
	} else if len(parts) >= 2 && IsEncryptedBox(parts[1]) {
		return nil, fmt.Errorf("encrypted notebook document history is plaintext [%s]", parts[1])
	}

	rootID := strings.TrimSuffix(filepath.Base(absPath), filepath.Ext(absPath))
	tree, err := parseDocVersionTree(data, rootID)
	historyRoot := filepath.Join(util.HistoryDir, parts[0])

View on GitHub (pinned to 9f775e8a12)