{"record":{"id":"d892ea0fecc1a2a7","repo":"siyuan-note/siyuan","slug":"encrypted-document-history-is-missing-notebook-con","errorCode":null,"errorMessage":"encrypted document history is missing notebook context","messagePattern":"encrypted document history is missing notebook context","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":183,"sourceCode":"\thistoryPath, err = validateHistoryPath(historyPath)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tdata, err := filelock.ReadFile(historyPath)\n\tif err != nil {\n\t\tlogging.LogErrorf(\"read file [%s] failed: %s\", historyPath, err)\n\t\treturn\n\t}\n\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\tciphertext := util.IsCiphertext(data)\n\tif ciphertext {\n\t\tif len(pathParts) < 3 || !ast.IsNodeIDPattern(pathParts[1]) {\n\t\t\terr = errors.New(\"encrypted document history is missing notebook context\")\n\t\t\treturn\n\t\t}\n\t\thistBoxID := pathParts[1]\n\t\tif !IsEncryptedBox(histBoxID) {\n\t\t\terr = fmt.Errorf(\"encrypted document history has no matching notebook [%s]\", histBoxID)\n\t\t\treturn\n\t\t}\n\t\tHoldBoxReadLock(histBoxID)\n\t\tdefer ReleaseBoxReadLock(histBoxID)\n\t\tdek, dekErr := GetDEKIfUnlocked(histBoxID)\n\t\tif dekErr != nil {\n\t\t\terr = errors.New(Conf.Language(314))\n\t\t\treturn\n\t\t}\n\t\tdata, err = DecryptFile(histBoxID, pathParts[2], dek, data)\n\t\tif err != nil {\n\t\t\tlogging.LogErrorf(\"decrypt history [%s] failed: %s\", historyPath, err)\n\t\t\treturn","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L165-L201","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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","Verify the history file lives at <HistoryDir>/<...>/<boxID>/<docID>.sy with a valid notebook-ID segment; move/repair misplaced history files","If the snapshot predates the current encrypted-history format, restore it into a notebook via the documented recovery migration instead of reading it directly"],"exampleFix":"// before\ncontent, err := model.GetDocHistoryContent(\"history/2024/old-snapshot.sy\")\n// after\n// path must include the notebook ID segment for ciphertext snapshots\ncontent, err := model.GetDocHistoryContent(\"history/2024/20240101120000/<boxID>/<docID>.sy\")","handlingStrategy":"validation","validationCode":"rel := strings.TrimPrefix(filepath.ToSlash(historyPath), filepath.ToSlash(util.HistoryDir))\nparts := strings.SplitN(strings.TrimPrefix(rel, \"/\"), \"/\", 3)\nif len(parts) < 3 || !ast.IsNodeIDPattern(parts[1]) {\n    return errors.New(\"history path must embed a valid notebook ID segment\")\n}","typeGuard":"func hasNotebookContext(historyPath string) bool {\n    parts := strings.SplitN(filepath.ToSlash(historyPath), \"/\", 3)\n    return len(parts) == 3 && ast.IsNodeIDPattern(parts[1])\n}","tryCatchPattern":"content, err := model.GetDocHistoryContent(historyPath)\nif err != nil && strings.Contains(err.Error(), \"missing notebook context\") {\n    // snapshot predates current layout: regenerate or migrate instead of retrying\n}","preventionTips":["Never hand-construct history paths; take them from the history listing API","After upgrading from an older format, let the migration regenerate history entries","Validate history path depth/segments before calling read APIs on ciphertext snapshots"],"tags":["history","encrypted-notebook","path-format"],"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"}