siyuan-note/siyuan · error · ErrEncryptedBoxNotUnlocked

encrypted notebook is not unlocked

Error message

encrypted notebook is not unlocked

What it means

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.

Solutions

  1. Unlock the encrypted notebook (enter its password in the UI or via the unlock API), then retry the MCP tool call / operation.
  2. 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).
  3. 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.
  4. Ensure the operation targets the correct box ID — operating on a different, still-locked encrypted box produces this error.

Example fix

// before
ops, err := model.AcquireEncryptedBoxOperations(ctx, resolvers)
if err != nil { return toolErrorResult(err.Error()) }
// after
if errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {
    return toolErrorResult("encrypted notebook is locked, please unlock it first")
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: check the sentinel with errors.Is before generic handling
if errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {
    return unlockPrompt(boxID)
}

Try / catch

ops, err := model.AcquireEncryptedBoxOperations(ctx, resolvers)
if err != nil {
    if errors.Is(err, model.ErrEncryptedBoxNotUnlocked) {
        return toolErrorResult("encrypted notebook is locked, please unlock it first")
    }
    return err
}
defer ops.Release()

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/61937e1d1667283a. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto_lifecycle.go:203

	lifecycle.acceptOperations = false
	for lifecycle.activeOperations > 0 {
		lifecycle.condition.Wait()
	}
	lifecycle.lock.Unlock()
}

type encryptedBoxOperationScope struct {
	lock     sync.Mutex
	boxIDs   []string
	boxIDSet map[string]struct{}
	closed   bool
}

type encryptedBoxOperationScopeKey struct{}

var (
	// ErrEncryptedBoxNotUnlocked 表示加密笔记本当前未解锁。
	ErrEncryptedBoxNotUnlocked = errors.New("encrypted notebook is not unlocked")
	// ErrEncryptedBoxOperationScopeClosed 表示响应级操作作用域已经关闭。
	ErrEncryptedBoxOperationScopeClosed = errors.New("encrypted notebook operation scope is closed")
)

// WithEncryptedBoxOperationScope 创建覆盖整个外层响应过程的租约作用域。
func WithEncryptedBoxOperationScope(ctx context.Context) (context.Context, func()) {
	scope := &encryptedBoxOperationScope{boxIDSet: map[string]struct{}{}}
	scopedContext := context.WithValue(ctx, encryptedBoxOperationScopeKey{}, scope)
	return scopedContext, scope.release
}

// AcquireEncryptedBoxOperations 按固定顺序取得多个笔记本的响应级租约。
func AcquireEncryptedBoxOperations(ctx context.Context, boxIDs []string) (release func(), err error) {
	unique := map[string]struct{}{}
	for _, boxID := range boxIDs {
		if boxID != "" && IsEncryptedBox(boxID) {
			unique[boxID] = struct{}{}
		}

View on GitHub (pinned to 9f775e8a12)