siyuan-note/siyuan · error

encode session data failed

Error message

encode session data failed: %w

What it means

SaveSessionState serializes the merged session data map to indented JSON with gulu.JSON.MarshalIndentJSON before writing to disk. If marshaling fails, this wrapped error is returned and nothing is written. The session data must be JSON-encodable (maps keyed by string, JSON-compatible value types).

Solutions

  1. Ensure all values in the session data map are JSON-marshalable (string keys, basic types, slices, maps, tagged structs)
  2. Remove or convert unsupported values (func/channel/cyclic refs) before saving
  3. Log newData keys and value types when this error occurs to find the offending field

Example fix

// before: storing a non-serializable value
newData["callback"] = someFunc
// after: store a serializable identifier instead
newData["callbackID"] = someFuncID
Defensive patterns

Strategy: validation

Validate before calling

for k, v := range newData {
    if err := json.Valid(mustMarshal(v)); err != nil {
        return fmt.Errorf("session field %q is not JSON-serializable", k)
    }
}

Type guard

func isJSONSafe(v any) bool {
    switch v.(type) {
    case func(), chan struct{}:
        return false
    }
    return true
}

Try / catch

if err != nil && strings.HasPrefix(err.Error(), "encode session data failed") {
    return fmt.Errorf("session data contained a non-serializable value: %w", err)
}

Prevention

When it happens

Trigger: Placing a non-JSON-serializable value into the session data map passed to SaveSessionState (e.g. a func, channel, complex struct with cycles, or non-string map keys introduced via newData merging).

Common situations: Storing custom runtime objects or Go-specific types in session fields; injecting values read from an untrusted source with unsupported types; cyclic data structures.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


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

Appendix: source

Thrown at kernel/agent/session.go:412

			if !isRuntimeTurnTerminal(runtime.ActiveTurn) {
				return currentRevision, nil, ErrRuntimeNotFinalized
			}
			if err := applyRuntimeTurnToSessionLocked(newData, runtime.ActiveTurn); err != nil {
				return currentRevision, nil, err
			}
		} else if currentCommittedTurnID != commitTurnID {
			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"
	}

View on GitHub (pinned to 9f775e8a12)