siyuan-note/siyuan · error

save session file failed

Error message

save session file failed: %w

What it means

SaveSessionState writes the marshaled session JSON through filelock.WriteFile, which takes the file lock and writes atomically. If the write fails, this wrapped error is returned, meaning the new revision was computed but NOT persisted — callers should not treat the returned revision as durable.

Solutions

  1. Check the wrapped cause: if it is a lock timeout, find/kill the process holding the file lock or remove the stale lock file
  2. Free disk space / raise quota on the data volume
  3. Verify the session file is writable by the kernel process user
  4. Retry the save after resolving the transient condition
Defensive patterns

Strategy: retry

Validate before calling

if df, err := diskFree(dataDir); err == nil && df < minRequiredBytes { return fmt.Errorf("insufficient disk space to save session") }

Try / catch

for attempt := 0; attempt < 3; attempt++ {
    rev, data, err = agent.SaveSessionState(meta, ...)
    if err == nil || !isTransientWriteError(err) {
        break
    }
    time.Sleep(backoff(attempt)) // transient lock/disk contention
}

Prevention

When it happens

Trigger: filelock.WriteFile fails due to the file being locked by another process for too long, a full disk, permission errors on the existing session file, or underlying I/O errors.

Common situations: Disk quota/full disk on the data volume; a stale lock from a crashed kernel instance; antivirus or backup software holding the session file; permissions changed on the session file.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


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

Appendix: source

Thrown at kernel/agent/session.go:419

			return currentRevision, nil, ErrSessionConflict
		}
	}

	newRevision := currentRevision + 1
	newData["revision"] = newRevision
	if commitTurnID != "" {
		newData["lastCommittedTurnID"] = commitTurnID
	}
	data, err = gulu.JSON.MarshalIndentJSON(newData, "", "\t")
	if err != nil {
		return currentRevision, nil, fmt.Errorf("encode session data failed: %w", err)
	}

	if err := os.MkdirAll(dir, 0755); err != nil {
		return currentRevision, nil, fmt.Errorf("create session dir failed: %w", err)
	}
	if err := filelock.WriteFile(path, data); err != nil {
		return currentRevision, nil, fmt.Errorf("save session file failed: %w", err)
	}
	if commitTurnID != "" {
		if err := markRuntimeCommittedLocked(meta.ID, commitTurnID); err != nil {
			logging.LogWarnf("commit agent runtime failed: %s", err)
		}
	}

	title, _ := newData["title"].(string)
	if title == "" {
		title = "AI Agent"
	}
	createdAt := meta.CreatedAt
	if value := numberToInt64(newData["createdAt"]); value > 0 {
		createdAt = value
	}
	updatedAt := meta.UpdatedAt
	if value := numberToInt64(newData["updatedAt"]); value > 0 {
		updatedAt = value

View on GitHub (pinned to 9f775e8a12)