{"record":{"id":"82a399270f0dd0b3","repo":"siyuan-note/siyuan","slug":"encrypted-asset-history-is-missing-valid-notebook","errorCode":null,"errorMessage":"encrypted asset history is missing valid notebook context","messagePattern":"encrypted asset history is missing valid notebook context","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":578,"sourceCode":"\nfunc RollbackAssetsHistory(historyPath string) (err error) {\n\thistoryPath, err = validateHistoryPath(historyPath)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tfrom := historyPath\n\t// 从路径提取 boxID 判断是否加密笔记本的资源\n\trelPath := strings.TrimPrefix(filepath.ToSlash(historyPath), filepath.ToSlash(util.HistoryDir))\n\trelPath = strings.TrimPrefix(relPath, \"/\")\n\tpathParts := strings.SplitN(relPath, \"/\", 3)\n\tdata, readErr := filelock.ReadFile(from)\n\tif readErr != nil {\n\t\treturn readErr\n\t}\n\tencrypted := bytes.HasPrefix(data, encryptedAssetMagic)\n\tif encrypted && (len(pathParts) < 3 || !ast.IsNodeIDPattern(pathParts[1]) || !IsEncryptedBox(pathParts[1])) {\n\t\treturn errors.New(\"encrypted asset history is missing valid notebook context\")\n\t}\n\tto := filepath.Join(util.DataDir, \"assets\", filepath.Base(historyPath))\n\tif len(pathParts) >= 2 && IsEncryptedBox(pathParts[1]) {\n\t\tif !encrypted {\n\t\t\treturn fmt.Errorf(\"encrypted notebook asset history is plaintext [%s]\", pathParts[1])\n\t\t}\n\t\t// 加密笔记本的资源回滚到笔记本级 assets 目录\n\t\tto = filepath.Join(util.DataDir, pathParts[1], \"assets\", filepath.Base(historyPath))\n\t\tif err = os.MkdirAll(filepath.Dir(to), 0755); err != nil {\n\t\t\treturn\n\t\t}\n\t}\n\n\tif err = filelock.CopyNewtimes(from, to); err != nil {\n\t\tlogging.LogErrorf(\"copy file [%s] to [%s] failed: %s\", from, to, err)\n\t\treturn\n\t}\n\tIncSync()","sourceCodeStart":560,"sourceCodeEnd":596,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L560-L596","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the history path structure is <historyDir>/<op>-<timestamp>/<boxID>/assets/<file> with a real encrypted notebook ID as boxID","Confirm the target box is actually encrypted (IsEncryptedBox) before assuming encrypted-asset semantics","Re-capture asset history from the encrypted notebook with the current kernel version so assets are properly enveloped"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const parts = historyPath.split('/');\nconst ok = parts.length >= 3 && /^[0-9a-f]{20,}$/.test(parts[1]) && isEncryptedBox(parts[1]);\nif (!ok) throw new Error('invalid encrypted asset history path');","typeGuard":null,"tryCatchPattern":"try { await rollbackAssetsHistory(p); } catch (e) { if (String(e).includes('missing valid notebook context')) { logInvalidHistoryEntry(p); } else { throw e; } }","preventionTips":["Validate history path shape before invoking rollback APIs","Do not hand-edit or rename history directory segments","Regenerate history with the current kernel version before restoring very old snapshots"],"tags":["history","assets","encrypted-notebook","validation"],"backgroundTag":"invalid-argument-format","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}