{"record":{"id":"d7bb2a2f41b7e019","repo":"siyuan-note/siyuan","slug":"initialize-encrypted-notebook-document-failed-w","errorCode":null,"errorMessage":"initialize encrypted notebook document failed: %w","messagePattern":"initialize encrypted notebook document failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":2609,"sourceCode":"\tmarkRuntimeEncryptedBox(id)\n\tinvalidateEncryptedPublishAccessCache()\n\n\t// 复用刚派生的 DEK 直接开 db + 缓存，省去再次 Argon2id 解锁\n\tcachedDEKsLock.Lock()\n\tif err = sql.OpenEncryptedDB(id, dek); err != nil {\n\t\tcachedDEKsLock.Unlock()\n\t\treturn \"\", err\n\t}\n\tif err = treenode.OpenEncryptedBlockTreeDB(id, dek); err != nil {\n\t\tsql.CloseEncryptedDB(id)\n\t\tcachedDEKsLock.Unlock()\n\t\treturn \"\", err\n\t}\n\tcachedDEKs[id] = dek\n\tcachedDEKsLock.Unlock()\n\n\tif _, err = EnsureBoxDoc(id); err != nil {\n\t\treturn \"\", fmt.Errorf(\"initialize encrypted notebook document failed: %w\", err)\n\t}\n\n\t// 初始化自动锁定访问时间戳，与 UnlockBox 对称\n\tnewVal := &atomic.Int64{}\n\tnewVal.Store(time.Now().UnixNano())\n\tboxLastAccess.Store(id, newVal)\n\n\tsetEncryptedBoxState(id, EncryptedBoxStateUnlocked)\n\tIncSync()\n\treturn id, nil\n}\n\n// cleanupFailedEncryptedBox 清理创建失败的加密笔记本，清理目标必须是 DataDir 下的有效笔记本目录。\nfunc cleanupFailedEncryptedBox(boxID string) {\n\tif !ast.IsNodeIDPattern(boxID) {\n\t\tlogging.LogErrorf(\"refuse to cleanup failed encrypted box with invalid ID [%s]\", boxID)\n\t\treturn\n\t}","sourceCodeStart":2591,"sourceCodeEnd":2627,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/model/crypto.go#L2591-L2627","documentation":"Returned by CreateEncryptedBox when EnsureBoxDoc fails after the encrypted databases (SQL + blocktree) are already opened and the DEK is cached. EnsureBoxDoc initializes the notebook's root document (.sy tree); failure here means the notebook's content structure couldn't be created. The %w wraps the underlying error from EnsureBoxDoc.","triggerScenarios":"EnsureBoxDoc fails due to a filesys write error (can't create the root .sy file), a treenode indexing error, or a block-ID collision. The encrypted DBs are open but the notebook has no valid root document.","commonSituations":"Disk I/O failure when writing the initial document tree. Permission issue on the notebook data subdirectory. A rare ID collision or corrupted block-tree state on a freshly created notebook.","solutions":["Check the wrapped error from EnsureBoxDoc for the specific failure (disk I/O vs. treenode vs. block ID).","Verify write permissions and disk space on the notebook data directory.","The deferred cleanup will remove the half-created box; retry CreateEncryptedBox after fixing the underlying issue."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"id, err := model.CreateEncryptedBox(name, password)\nif err != nil {\n    if strings.Contains(err.Error(), \"initialize encrypted notebook document\") {\n        // EnsureBoxDoc failed — check disk/treenode state\n        logging.LogErrorf(\"EnsureBoxDoc failed: %+v\", err)\n    }\n    return err\n}","preventionTips":["Verify the notebook data directory is writable before creation.","Ensure the blocktree and SQL databases are healthy (run re-index if needed).","Check disk space before creating a new notebook."],"tags":["encryption","notebook-creation","document-init","disk-io","crypto"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}