{"record":{"id":"72d4b1545cb20b33","repo":"siyuan-note/siyuan","slug":"parse-encrypted-notebook-history-conf-s-failed","errorCode":null,"errorMessage":"parse encrypted notebook history conf [%s] failed: %w","messagePattern":"parse encrypted notebook history conf \\[(.+?)\\] failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":954,"sourceCode":"\tif _, err := os.Stat(backupPath); err == nil {\n\t\treturn true, nil\n\t} else if !os.IsNotExist(err) {\n\t\treturn false, fmt.Errorf(\"stat encrypted notebook history backup [%s] failed: %w\", boxDir, err)\n\t}\n\tconfPath := filepath.Join(siyuanDir, \"conf.json\")\n\tif _, err := os.Stat(confPath); err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn false, nil\n\t\t}\n\t\treturn false, fmt.Errorf(\"stat encrypted notebook history conf [%s] failed: %w\", boxDir, err)\n\t}\n\tdata, err := filelock.ReadFile(confPath)\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"read encrypted notebook history conf [%s] failed: %w\", boxDir, err)\n\t}\n\tvar boxConf conf.BoxConf\n\tif err = gulu.JSON.UnmarshalJSON(data, &boxConf); err != nil {\n\t\treturn false, fmt.Errorf(\"parse encrypted notebook history conf [%s] failed: %w\", boxDir, err)\n\t}\n\treturn boxConf.Encrypted, nil\n}\n\n// cachedDEKs 缓存已解锁加密笔记本的 DEK，按 boxID 索引。\n// KEK 不全局缓存（\"严格每笔记本单独解锁\"语义）：UnlockBox 临时派生 KEK 解出 DEK 后即丢弃 KEK，\n// 仅保留 per-box DEK 供后续读写加解密。\nvar (\n\tcachedDEKs     = map[string][]byte{}\n\tcachedDEKsLock sync.RWMutex\n)\n\n// boxLastAccess 记录每个加密笔记本最近一次真实用户交互或显式保活时间（unix 纳秒），供自动锁定 cron 使用。\n// key: boxID, value: *atomic.Int64。UnlockBox 成功时初始化，Unmount 时清理。\nvar boxLastAccess sync.Map\n\n// EnableEncryptedNotebookWithSync 在启用前先完成同步；同步恢复了既有配置时只校验原主密码。\nfunc EnableEncryptedNotebookWithSync(password string) error {","sourceCodeStart":936,"sourceCodeEnd":972,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L936-L972","documentation":"The .siyuan/conf.json of a history box is JSON-decoded into conf.BoxConf to read its Encrypted flag. This error is thrown when the file was read successfully but its bytes are not valid JSON or do not decode into conf.BoxConf — meaning the history box's configuration is corrupt, so its encrypted status is unknown and recovery classification stops for that box.","triggerScenarios":"filelock.ReadFile succeeds on <boxDir>/.siyuan/conf.json but gulu.JSON.UnmarshalJSON fails during a scan from encryptedNotebookHistoryBoxDirs, hasEncryptedNotebookDeleteHistory, or IsEncryptedHistoryPath — due to truncation, hand-editing, or an incompatible legacy conf format.","commonSituations":"A crash during a historic conf.json write left truncated JSON; the box came from a very old SiYuan version whose conf shape differs; the file was manually edited; a backup restored a partially written conf.json.","solutions":["Restore the box's .siyuan/conf.json from a backup or sync snapshot taken before the corruption","Validate/repair the JSON manually only after copying the original aside (keep recovery material intact)","If the conf is from an old format, upgrade/migrate it with current tooling rather than deleting the box","Never delete the history box on this error; classification failure must not remove recovery material"],"exampleFix":"// before: deleting a box whose conf cannot be parsed\nif _, err := isEncryptedHistoryBoxDir(dir); err != nil {\n    os.RemoveAll(dir)\n}\n// after: quarantine instead of delete\nif _, err := isEncryptedHistoryBoxDir(dir); err != nil {\n    os.Rename(dir, dir+\".quarantine\")\n    log.Printf(\"quarantined unparseable history box: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"data, err := os.ReadFile(filepath.Join(boxDir, \".siyuan\", \"conf.json\"))\nif err == nil {\n    var probe conf.BoxConf\n    if jsonErr := json.Unmarshal(data, &probe); jsonErr != nil {\n        // corrupt conf — restore from backup before scanning\n    }\n}","typeGuard":"func isParsableBoxConf(data []byte) bool {\n    var c conf.BoxConf\n    return len(data) > 0 && json.Unmarshal(data, &c) == nil\n}","tryCatchPattern":"enc, err := isEncryptedHistoryBoxDir(boxDir)\nif err != nil {\n    if strings.Contains(err.Error(), \"parse encrypted notebook history conf\") {\n        // restore conf.json from snapshot; keep the box regardless\n    }\n    return err\n}","preventionTips":["Never hand-edit conf.json inside history boxes","Shut the kernel down cleanly so conf writes complete","Restore only whole boxes from backups, not partial files","Quarantine boxes with unparseable conf instead of deleting them"],"tags":["json","unmarshal","history","conf","corruption"],"backgroundTag":"json-unmarshal-failed","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"}