siyuan-note/siyuan · error

read session file failed

Error message

read session file failed: %w

What it means

SaveSessionState persists an agent session's state to disk. Before writing, it reads the existing session file to merge data and compute the new revision. If that read fails with a real I/O error (anything other than os.IsNotExist), the save is aborted and this wrapped error is returned, preserving the underlying cause via %w.

Solutions

  1. Check the wrapped cause with errors.Unwrap or %v of the error and fix the underlying I/O/permission problem
  2. Verify the session file permissions allow the current process user to read it
  3. If the file is corrupt, back it up and delete it so the next save starts fresh (IsNotExist path)
  4. Check filesystem health / free disk space (dmesg, fsck) if I/O errors repeat

Example fix

// before: blindly saving
currentRevision, data, err := agent.SaveSessionState(meta, ...)
// after: inspect the wrapped cause and recover
if err != nil && errors.Is(err, fs.ErrPermission) {
    os.Chmod(sessionFilePath, 0600) // restore read access, then retry
    currentRevision, data, err = agent.SaveSessionState(meta, ...)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := os.Stat(sessionPath); err != nil && !os.IsNotExist(err) { return fmt.Errorf("session file unreadable: %w", err) }

Try / catch

rev, data, err := agent.SaveSessionState(meta, ...)
if err != nil {
    if cause := errors.Unwrap(err); cause != nil {
        log.Printf("session save failed: %v (cause: %v)", err, cause)
    }
    // inspect with os.IsPermission(err)/fs.ErrPermission and recover or abort
}

Prevention

When it happens

Trigger: Calling SaveSessionState (directly or via SaveSession) when the session file at sessionsDir/<id> exists but cannot be read: corrupted file, wrong permissions, or an I/O error from the filesystem. A plain missing file does NOT trigger this (IsNotExist is tolerated and treated as a fresh session).

Common situations: File permissions changed after creating the session (e.g. session dir owned by another user), disk errors, a partially-written/corrupt session JSON file left by a crashed process, or a read-only filesystem after the file was created.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at kernel/agent/session.go:381

				}
				return currentRevision, existingData, nil
			}
			if meta.ExpectedRevision != nil && *meta.ExpectedRevision != currentRevision {
				return currentRevision, nil, ErrSessionConflict
			}
			for k, v := range existingData {
				if _, ok := newData[k]; !ok {
					// messages 是已废弃的旧会话字段,不再带入新格式;其他未知字段原样保留,
					// 避免前后端版本不一致时擦除较新版本写入的数据。
					if k != "messages" && k != "expectedRevision" && k != "commitTurnID" &&
						k != "recoveryTurnID" && k != "recoveryState" && k != "recoveryRevision" && k != "agentRunning" {
						newData[k] = v
					}
				}
			}
		}
	} else if err != nil && !os.IsNotExist(err) {
		return 0, nil, fmt.Errorf("read session file failed: %w", err)
	} else if meta.ExpectedRevision != nil && *meta.ExpectedRevision != 0 {
		return 0, nil, ErrSessionConflict
	}
	if commitTurnID != "" {
		runtime, err := loadRuntimeLocked(meta.ID)
		if err != nil {
			return currentRevision, nil, fmt.Errorf("read agent runtime failed: %w", err)
		}
		if runtime.ActiveTurn != nil {
			if runtime.ActiveTurn.TurnID != commitTurnID {
				return currentRevision, nil, ErrSessionConflict
			}
			if !isRuntimeTurnTerminal(runtime.ActiveTurn) {
				return currentRevision, nil, ErrRuntimeNotFinalized
			}
			if err := applyRuntimeTurnToSessionLocked(newData, runtime.ActiveTurn); err != nil {
				return currentRevision, nil, err
			}

View on GitHub (pinned to 9f775e8a12)