siyuan-note/siyuan · error

invalid Agent session ID

Error message

invalid Agent session ID

What it means

SaveAgentTodos validates the Agent session ID against ast.IsNodeIDPattern (the SiYuan node-ID format) before using it to build a filesystem path. A session ID that is not a valid node ID is rejected so that arbitrary or hostile strings cannot traverse directories or create unexpected paths under storage/ai/agent/sessions/.

Solutions

  1. Obtain the session ID as a valid SiYuan node ID (ast.IsNodeIDPattern must accept it)
  2. Replace custom UUID-based IDs with the node-ID format (timestamp-random suffix)
  3. Sanitize/normalize the ID before calling SaveAgentTodos
  4. Check the caller (todoWriteHandler) for where the ID originates and fix the producer

Example fix

// before
SaveAgentTodos(uuid.NewString(), todos) // rejected
// after
id := treenode.CreateNodeID()
SaveAgentTodos(id, todos)
Defensive patterns

Strategy: validation

Validate before calling

if !ast.IsNodeIDPattern(sessionID) { return errors.New("bad session id") }
SaveAgentTodos(sessionID, todos)

Type guard

func validSessionID(id string) bool { return ast.IsNodeIDPattern(id) }

Try / catch

if err := SaveAgentTodos(sid, todos); err != nil && strings.Contains(err.Error(), "invalid Agent session ID") { /* regenerate ID */ }

Prevention

When it happens

Trigger: Calling SaveAgentTodos with an empty string, a path-like string (e.g. "../../x"), a UUID, or any identifier not matching the node-ID pattern (format YYYYMMDDhhmmss-xxxxxxxx, 14 digits, dash, 7+ alphanumeric chars).

Common situations: Generating your own session IDs with uuid.NewString() instead of using SiYuan block IDs; passing a filename or session label from another system; older clients producing IDs in a legacy format.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/todo.go:46

)

type AgentTodoItem struct {
	Content string `json:"content"`
	Status  string `json:"status"` // pending, in_progress, completed, cancelled
}

type AgentTodoList struct {
	SessionID string          `json:"sessionID"`
	Todos     []AgentTodoItem `json:"todos"`
}

func agentTodosPath(sessionID string) string {
	return filepath.Join(util.DataDir, "storage", "ai", "agent", "sessions", sessionID, "todos.json")
}

func SaveAgentTodos(sessionID string, todos []AgentTodoItem) error {
	if !ast.IsNodeIDPattern(sessionID) {
		return errors.New("invalid Agent session ID")
	}
	dir := filepath.Join(util.DataDir, "storage", "ai", "agent", "sessions", sessionID)
	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	data := AgentTodoList{
		SessionID: sessionID,
		Todos:     todos,
	}

	b, err := json.Marshal(data)
	if err != nil {
		return err
	}

	return filelock.WriteFile(agentTodosPath(sessionID), b)
}

View on GitHub (pinned to 9f775e8a12)