siyuan-note/siyuan · error

marshal AI editor actions failed

Error message

marshal AI editor actions failed: %w

What it means

saveAIEditorActions serializes aiEditorActionsData with gulu.JSON.MarshalIndentJSON before writing. This error wraps a marshal failure, which is rare for this plain data struct but can occur if the data contains unsupported values (e.g. invalid types injected via reflection or channels/funcs in custom fields). Thrown by SaveAIEditorAction, RemoveAIEditorAction, and migrateLegacyAIEditorActions.

Solutions

  1. Check the wrapped error (%w) in the kernel log to identify the offending field/type.
  2. Remove or make serializable any new AIEditorAction field that is not JSON-safe (use string/number/bool/slices/maps only).
  3. Restart the kernel to reset in-memory state, then retry SaveAIEditorAction.
  4. Report/upstream as an internal bug if it reproduces with stock builds — stock data should always marshal.

Example fix

// before: non-serializable field in AIEditorAction
type AIEditorAction struct { ID, Name, Action string; Callback func() }
// after: only JSON-safe fields persisted
type AIEditorAction struct { ID, Name, Action string }
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity: attempt to round-trip the payload client-side before saving
JSON.parse(JSON.stringify({ id, name, action }));

Try / catch

try {
  await fetchSyncPost('/api/ai/editor/saveAIEditorAction', { id, name, action });
} catch (e) {
  if (/marshal AI editor actions failed/.test(e.message)) {
    // internal bug — capture kernel version and the offending field; restart kernel
  }
}

Prevention

When it happens

Trigger: Calling SaveAIEditorAction/RemoveAIEditorAction when the in-memory aiEditorActionsData cannot be JSON-marshalled — practically only if fields hold values unsupported by encoding/json (func, chan, cycle), which indicates an internal bug or corrupted in-memory state.

Common situations: A kernel/plugin modification introduced a non-serializable field into AIEditorAction; extremely deep nesting causing stack overflow; corrupted in-memory data after a failed migration.

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/a093bcd19ea15e1f. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/ai_editor.go:197

		}
		ids[action.ID] = struct{}{}
	}
	return ret, nil
}

func saveAIEditorActions(data *aiEditorActionsData) (err error) {
	data.Version = aiEditorActionsVersion
	if data.Actions == nil {
		data.Actions = []*AIEditorAction{}
	}

	dirPath := filepath.Dir(aiEditorActionsPath())
	if err = os.MkdirAll(dirPath, 0755); err != nil {
		return fmt.Errorf("create AI editor actions directory failed: %w", err)
	}
	bytes, err := gulu.JSON.MarshalIndentJSON(data, "", "  ")
	if err != nil {
		return fmt.Errorf("marshal AI editor actions failed: %w", err)
	}
	if err = filelock.WriteFile(aiEditorActionsPath(), bytes); err != nil {
		return fmt.Errorf("write AI editor actions failed: %w", err)
	}
	return nil
}

func migrateLegacyAIEditorActions(data *aiEditorActionsData, persist bool) (err error) {
	localStorage := GetLocalStorage()
	legacyRaw, ok := localStorage[legacyAIEditorActionsStorageKey]
	if !ok {
		return nil
	}

	var legacyActions []*legacyAIEditorAction
	if legacyJSON, isString := legacyRaw.(string); isString {
		if err = gulu.JSON.UnmarshalJSON([]byte(legacyJSON), &legacyActions); err != nil {
			return fmt.Errorf("unmarshal legacy AI editor actions failed: %w", err)

View on GitHub (pinned to 9f775e8a12)