{"record":{"id":"8ce4544c17263c50","repo":"siyuan-note/siyuan","slug":"history-path-is-missing-notebook-context-s","errorCode":null,"errorMessage":"history path is missing notebook context [%s]","messagePattern":"history path is missing notebook context \\[(.+?)\\]","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":291,"sourceCode":"\t}\n\treturn\n}\n\nfunc RollbackDocHistory(historyPath string) (err error) {\n\thistoryPath, err = validateHistoryPath(historyPath)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tFlushTxQueue()\n\n\trelPath := strings.TrimPrefix(historyPath, util.HistoryDir)\n\trelPath = strings.TrimPrefix(relPath, string(os.PathSeparator))\n\trelPath = filepath.ToSlash(relPath)\n\tparts := strings.SplitN(relPath, \"/\", 3)\n\tif len(parts) < 3 {\n\t\tlogging.LogWarnf(\"invalid history path [%s]\", historyPath)\n\t\treturn fmt.Errorf(\"history path is missing notebook context [%s]\", historyPath)\n\t}\n\tboxID := parts[1]\n\torigBoxID := boxID // 保留原始 boxID 用于解密（getRollbackBox 可能返回不同的 box）\n\tencrypted := IsEncryptedBox(origBoxID)\n\tif encrypted {\n\t\t// 整个回滚持有操作租约，内部文件读写可独立获取读锁，锁定等待租约结束后再申请写锁。\n\t\tif err = AcquireEncryptedBoxOperation(origBoxID); err != nil {\n\t\t\treturn\n\t\t}\n\t\tdefer ReleaseEncryptedBoxOperation(origBoxID)\n\t}\n\n\t// 加密笔记本的历史回滚要求原笔记本已挂载：\n\t// WriteTree 根据 tree.Box 判断是否加密落盘。若原笔记本未挂载导致\n\t// getRollbackBox fallback 到普通 Rollback 笔记本，解密后的 .sy 将被 WriteTree\n\t// 以明文落盘，违反\"WriteTree auto-encrypts on write-back\"的设计承诺。\n\tif IsEncryptedBox(origBoxID) && nil == Conf.Box(origBoxID) {\n\t\tlogging.LogErrorf(\"rollback encrypted doc history requires notebook [%s] to be mounted\", origBoxID)","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L273-L309","documentation":"RollbackDocHistory parses the history path as <historyDir>/<datePrefix>/<boxID>/<relativePath> and needs at least three slash-separated segments to identify the owning notebook. The supplied path has fewer segments, so no notebook context can be derived. This guards against rolling back arbitrary or malformed paths that were not produced by the history subsystem.","triggerScenarios":"Calling model.RollbackDocHistory (via historyRollback API or tests) with a path directly under data/history/ (e.g. data/history/foo.sy) or otherwise lacking the <datePrefix>/<boxID>/... structure.","commonSituations":"Client code concatenates the wrong base directory; passing an asset or doc path instead of a history path; a truncated or hand-constructed path after a failed history listing.","solutions":["Pass the full history path as returned by the history index (containing date prefix, notebook ID, and relative doc path)","Obtain the path from the history query API rather than constructing it manually","Ensure util.HistoryDir prefix and OS path separators are intact (no manual rewriting to a different base)","If migrating data, rebuild the directory as <historyDir>/<date>/<boxID>/<doc>.sy before rolling back"],"exampleFix":"// before\nRollbackDocHistory(filepath.Join(util.HistoryDir, \"20260918091234-abc123.sy\"))\n// after\nRollbackDocHistory(filepath.Join(util.HistoryDir, \"20260918\", boxID, \"20260918091234-abc123.sy\"))","handlingStrategy":"validation","validationCode":"const rel = historyPath.replace(/^.*data\\/history\\//, '').split(/[\\\\/]/);\nif (rel.length < 3) throw new Error('history path must be <date>/<boxID>/<doc>.sy');","typeGuard":"function isValidHistoryPath(p) { const rel = p.split(/[\\\\/]/); const i = rel.indexOf('history'); return i >= 0 && rel.length - i - 1 >= 3; }","tryCatchPattern":"try { await historyRollback({ historyPath }) } catch (e) { if (String(e.msg).includes('missing notebook context')) { /* fix path construction */ } else { throw e } }","preventionTips":["Construct history paths only from values returned by history APIs","Keep the data/history/<date>/<boxID>/<doc>.sy layout intact","Validate path segment count before calling the rollback API"],"tags":["history","path-format","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"}