{"record":{"id":"a093bcd19ea15e1f","repo":"siyuan-note/siyuan","slug":"marshal-ai-editor-actions-failed-w","errorCode":null,"errorMessage":"marshal AI editor actions failed: %w","messagePattern":"marshal AI editor actions failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/ai_editor.go","lineNumber":197,"sourceCode":"\t\t}\n\t\tids[action.ID] = struct{}{}\n\t}\n\treturn ret, nil\n}\n\nfunc saveAIEditorActions(data *aiEditorActionsData) (err error) {\n\tdata.Version = aiEditorActionsVersion\n\tif data.Actions == nil {\n\t\tdata.Actions = []*AIEditorAction{}\n\t}\n\n\tdirPath := filepath.Dir(aiEditorActionsPath())\n\tif err = os.MkdirAll(dirPath, 0755); err != nil {\n\t\treturn fmt.Errorf(\"create AI editor actions directory failed: %w\", err)\n\t}\n\tbytes, err := gulu.JSON.MarshalIndentJSON(data, \"\", \"  \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshal AI editor actions failed: %w\", err)\n\t}\n\tif err = filelock.WriteFile(aiEditorActionsPath(), bytes); err != nil {\n\t\treturn fmt.Errorf(\"write AI editor actions failed: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc migrateLegacyAIEditorActions(data *aiEditorActionsData, persist bool) (err error) {\n\tlocalStorage := GetLocalStorage()\n\tlegacyRaw, ok := localStorage[legacyAIEditorActionsStorageKey]\n\tif !ok {\n\t\treturn nil\n\t}\n\n\tvar legacyActions []*legacyAIEditorAction\n\tif legacyJSON, isString := legacyRaw.(string); isString {\n\t\tif err = gulu.JSON.UnmarshalJSON([]byte(legacyJSON), &legacyActions); err != nil {\n\t\t\treturn fmt.Errorf(\"unmarshal legacy AI editor actions failed: %w\", err)","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/ai_editor.go#L179-L215","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the wrapped error (%w) in the kernel log to identify the offending field/type.","Remove or make serializable any new AIEditorAction field that is not JSON-safe (use string/number/bool/slices/maps only).","Restart the kernel to reset in-memory state, then retry SaveAIEditorAction.","Report/upstream as an internal bug if it reproduces with stock builds — stock data should always marshal."],"exampleFix":"// before: non-serializable field in AIEditorAction\ntype AIEditorAction struct { ID, Name, Action string; Callback func() }\n// after: only JSON-safe fields persisted\ntype AIEditorAction struct { ID, Name, Action string }","handlingStrategy":"try-catch","validationCode":"// sanity: attempt to round-trip the payload client-side before saving\nJSON.parse(JSON.stringify({ id, name, action }));","typeGuard":null,"tryCatchPattern":"try {\n  await fetchSyncPost('/api/ai/editor/saveAIEditorAction', { id, name, action });\n} catch (e) {\n  if (/marshal AI editor actions failed/.test(e.message)) {\n    // internal bug — capture kernel version and the offending field; restart kernel\n  }\n}","preventionTips":["Keep AIEditorAction fields JSON-serializable (no funcs/chans/cycles)","Round-trip payloads through JSON.parse(JSON.stringify(...)) in tests","Restart kernel if in-memory state is suspect","Treat reproducible cases as upstream bugs"],"tags":["json","serialization","persistence","internal"],"backgroundTag":"json-marshal-failed","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}