{"record":{"id":"61937e1d1667283a","repo":"siyuan-note/siyuan","slug":"encrypted-notebook-is-not-unlocked","errorCode":null,"errorMessage":"encrypted notebook is not unlocked","messagePattern":"encrypted notebook is not unlocked","errorType":"validation","errorClass":"ErrEncryptedBoxNotUnlocked","httpStatus":null,"severity":"error","filePath":"kernel/model/crypto_lifecycle.go","lineNumber":203,"sourceCode":"\tlifecycle.acceptOperations = false\n\tfor lifecycle.activeOperations > 0 {\n\t\tlifecycle.condition.Wait()\n\t}\n\tlifecycle.lock.Unlock()\n}\n\ntype encryptedBoxOperationScope struct {\n\tlock     sync.Mutex\n\tboxIDs   []string\n\tboxIDSet map[string]struct{}\n\tclosed   bool\n}\n\ntype encryptedBoxOperationScopeKey struct{}\n\nvar (\n\t// ErrEncryptedBoxNotUnlocked 表示加密笔记本当前未解锁。\n\tErrEncryptedBoxNotUnlocked = errors.New(\"encrypted notebook is not unlocked\")\n\t// ErrEncryptedBoxOperationScopeClosed 表示响应级操作作用域已经关闭。\n\tErrEncryptedBoxOperationScopeClosed = errors.New(\"encrypted notebook operation scope is closed\")\n)\n\n// WithEncryptedBoxOperationScope 创建覆盖整个外层响应过程的租约作用域。\nfunc WithEncryptedBoxOperationScope(ctx context.Context) (context.Context, func()) {\n\tscope := &encryptedBoxOperationScope{boxIDSet: map[string]struct{}{}}\n\tscopedContext := context.WithValue(ctx, encryptedBoxOperationScopeKey{}, scope)\n\treturn scopedContext, scope.release\n}\n\n// AcquireEncryptedBoxOperations 按固定顺序取得多个笔记本的响应级租约。\nfunc AcquireEncryptedBoxOperations(ctx context.Context, boxIDs []string) (release func(), err error) {\n\tunique := map[string]struct{}{}\n\tfor _, boxID := range boxIDs {\n\t\tif boxID != \"\" && IsEncryptedBox(boxID) {\n\t\t\tunique[boxID] = struct{}{}\n\t\t}","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto_lifecycle.go#L185-L221","documentation":"ErrEncryptedBoxNotUnlocked is the sentinel error returned by AcquireEncryptedBoxOperation(s) when a box's lifecycle state is not EncryptedBoxStateUnlocked or the lifecycle is not accepting operations. It lets callers (e.g. the MCP server) detect the specific locked-notebook condition via errors.Is and translate it into a user-facing 'encrypted notebook is locked, please unlock it first' tool error instead of a generic failure.","triggerScenarios":"Any operation that goes through AcquireEncryptedBoxOperations (e.g. MCP tool calls with box leases at kernel/mcp/server.go:284) while the target encrypted box is in a state other than Unlocked (locked at startup, locked after timeout/idle, or lifecycle.acceptOperations is false because a lock/rekey/close transition is in progress).","commonSituations":"MCP client or plugin performs a write/read operation on an encrypted notebook after a kernel restart (boxes start locked); the box auto-locked after inactivity; an unlock/rekey operation is mid-flight so operations are temporarily rejected; the user never entered the notebook password this session.","solutions":["Unlock the encrypted notebook (enter its password in the UI or via the unlock API), then retry the MCP tool call / operation.","In API/tool code, match with errors.Is(err, model.ErrEncryptedBoxNotUnlocked) and surface an 'unlock first' message instead of retrying blindly (the MCP server already does this).","If the box should be unlocked but still errors, check whether a lifecycle transition (lock/rekey/close) is in progress and wait/retry once it completes.","Ensure the operation targets the correct box ID — operating on a different, still-locked encrypted box produces this error."],"exampleFix":"// before\nops, err := model.AcquireEncryptedBoxOperations(ctx, resolvers)\nif err != nil { return toolErrorResult(err.Error()) }\n// after\nif errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {\n    return toolErrorResult(\"encrypted notebook is locked, please unlock it first\")\n}","handlingStrategy":"try-catch","validationCode":"// Go: check the sentinel with errors.Is before generic handling\nif errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {\n    return unlockPrompt(boxID)\n}","typeGuard":null,"tryCatchPattern":"ops, err := model.AcquireEncryptedBoxOperations(ctx, resolvers)\nif err != nil {\n    if errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {\n        return toolErrorResult(\"encrypted notebook is locked, please unlock it first\")\n    }\n    return err\n}\ndefer ops.Release()","preventionTips":["Always compare with errors.Is against the sentinel, never string matching","Unlock encrypted notebooks at session start when running automated/MCP operations","Handle the transient case where lifecycle transitions (rekey/close) temporarily reject operations","Target the correct box ID in lease resolvers"],"tags":["encryption","locked","lifecycle","mcp"],"backgroundTag":"authentication-required","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"}