{"record":{"id":"36f8b2d0b4ef2af7","repo":"siyuan-note/siyuan","slug":"encrypted-document-history-is-missing-valid-notebo","errorCode":null,"errorMessage":"encrypted document history is missing valid notebook context","messagePattern":"encrypted document history is missing valid notebook context","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history_diff.go","lineNumber":352,"sourceCode":"\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif !strings.HasSuffix(strings.ToLower(absPath), \".sy\") {\n\t\treturn nil, errors.New(\"history version is not a document\")\n\t}\n\trelPath, err := filepath.Rel(util.HistoryDir, absPath)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tparts := strings.SplitN(filepath.ToSlash(relPath), \"/\", 3)\n\tdata, err := filelock.ReadFile(absPath)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tciphertext := util.IsCiphertext(data)\n\tif ciphertext {\n\t\tif len(parts) < 3 || !ast.IsNodeIDPattern(parts[1]) || !IsEncryptedBox(parts[1]) {\n\t\t\treturn nil, errors.New(\"encrypted document history is missing valid notebook context\")\n\t\t}\n\t\tHoldBoxReadLock(parts[1])\n\t\tdefer ReleaseBoxReadLock(parts[1])\n\t\tdek, dekErr := GetDEKIfUnlocked(parts[1])\n\t\tif dekErr != nil {\n\t\t\treturn nil, errors.New(Conf.Language(314))\n\t\t}\n\t\tdata, err = DecryptFile(parts[1], parts[2], dek, data)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t} else if len(parts) >= 2 && IsEncryptedBox(parts[1]) {\n\t\treturn nil, fmt.Errorf(\"encrypted notebook document history is plaintext [%s]\", parts[1])\n\t}\n\n\trootID := strings.TrimSuffix(filepath.Base(absPath), filepath.Ext(absPath))\n\ttree, err := parseDocVersionTree(data, rootID)\n\thistoryRoot := filepath.Join(util.HistoryDir, parts[0])","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history_diff.go#L334-L370","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Restore the standard history layout history/<timestamp>/<boxID>/<docID>.sy so the notebook ID segment is present and well-formed.","Verify the notebook ID segment matches an existing encrypted notebook (IsEncryptedBox); if the notebook's encryption state changed, decrypt/re-encrypt consistently before diffing.","Do not move or rename files inside the history directory; regenerate history from snapshots if the layout is broken.","If the history was produced by an older format, restore it via the official history restore path rather than loading the raw file."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Go: pre-validate the history path layout timestamp/boxID/file.sy\nparts := strings.Split(filepath.ToSlash(rel), \"/\")\nif len(parts) < 3 || !ast.IsNodeIDPattern(parts[1]) || !model.IsEncryptedBox(parts[1]) {\n    return errors.New(\"history path lacks valid encrypted notebook context\")\n}","typeGuard":null,"tryCatchPattern":"catch (e) { if (e.message.includes(\"missing valid notebook context\")) { repairOrRegenerateHistory(); } else { throw e; } }","preventionTips":["Never reorganize files under history/; keep the timestamp/boxID/file layout","Keep notebook encryption state and its history directory consistent","Restore broken history via sync/backup snapshots, not manual edits"],"tags":["encryption","history","path-structure","notebook-context"],"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"}