siyuan-note/siyuan · error

encrypted asset history is missing valid notebook context

Error message

encrypted asset history is missing valid notebook context

What it means

RollbackAssetsHistory validates that an encrypted history asset actually belongs to an encrypted notebook. The file is read and inspected for the encrypted-asset magic prefix; if the path implies an encrypted notebook (a node-ID-like second path segment that resolves to an encrypted box) but the history path is malformed, or if the magic prefix is absent where required, this error is thrown because the notebook context for the encrypted asset cannot be authenticated.

Solutions

  1. Verify the history path structure is <historyDir>/<op>-<timestamp>/<boxID>/assets/<file> with a real encrypted notebook ID as boxID
  2. Confirm the target box is actually encrypted (IsEncryptedBox) before assuming encrypted-asset semantics
  3. Re-capture asset history from the encrypted notebook with the current kernel version so assets are properly enveloped
Defensive patterns

Strategy: validation

Validate before calling

const parts = historyPath.split('/');
const ok = parts.length >= 3 && /^[0-9a-f]{20,}$/.test(parts[1]) && isEncryptedBox(parts[1]);
if (!ok) throw new Error('invalid encrypted asset history path');

Try / catch

try { await rollbackAssetsHistory(p); } catch (e) { if (String(e).includes('missing valid notebook context')) { logInvalidHistoryEntry(p); } else { throw e; } }

Prevention

When it happens

Trigger: Calling RollbackAssetsHistory with a history asset path whose second path segment is a valid node ID of an encrypted box but which has fewer than 3 path parts, or an invalid/missing notebook ID segment, or where the asset content does not carry the encrypted-asset magic bytes.

Common situations: Restoring assets from hand-copied or partially synced history directories of encrypted notebooks; history produced by an older kernel version before encrypted-asset envelope wrapping; path segments edited or renamed by tooling outside SiYuan.

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/82a399270f0dd0b3. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/history.go:578

func RollbackAssetsHistory(historyPath string) (err error) {
	historyPath, err = validateHistoryPath(historyPath)
	if err != nil {
		return
	}

	from := historyPath
	// 从路径提取 boxID 判断是否加密笔记本的资源
	relPath := strings.TrimPrefix(filepath.ToSlash(historyPath), filepath.ToSlash(util.HistoryDir))
	relPath = strings.TrimPrefix(relPath, "/")
	pathParts := strings.SplitN(relPath, "/", 3)
	data, readErr := filelock.ReadFile(from)
	if readErr != nil {
		return readErr
	}
	encrypted := bytes.HasPrefix(data, encryptedAssetMagic)
	if encrypted && (len(pathParts) < 3 || !ast.IsNodeIDPattern(pathParts[1]) || !IsEncryptedBox(pathParts[1])) {
		return errors.New("encrypted asset history is missing valid notebook context")
	}
	to := filepath.Join(util.DataDir, "assets", filepath.Base(historyPath))
	if len(pathParts) >= 2 && IsEncryptedBox(pathParts[1]) {
		if !encrypted {
			return fmt.Errorf("encrypted notebook asset history is plaintext [%s]", pathParts[1])
		}
		// 加密笔记本的资源回滚到笔记本级 assets 目录
		to = filepath.Join(util.DataDir, pathParts[1], "assets", filepath.Base(historyPath))
		if err = os.MkdirAll(filepath.Dir(to), 0755); err != nil {
			return
		}
	}

	if err = filelock.CopyNewtimes(from, to); err != nil {
		logging.LogErrorf("copy file [%s] to [%s] failed: %s", from, to, err)
		return
	}
	IncSync()

View on GitHub (pinned to 9f775e8a12)