{"record":{"id":"cae76dfdae294da5","repo":"siyuan-note/siyuan","slug":"unmarshal-legacy-ai-editor-actions-failed-w","errorCode":null,"errorMessage":"unmarshal legacy AI editor actions failed: %w","messagePattern":"unmarshal legacy AI editor actions failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/ai_editor.go","lineNumber":215,"sourceCode":"\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)\n\t\t}\n\t} else {\n\t\tlegacyJSON, marshalErr := gulu.JSON.MarshalJSON(legacyRaw)\n\t\tif marshalErr != nil {\n\t\t\treturn fmt.Errorf(\"marshal legacy AI editor actions failed: %w\", marshalErr)\n\t\t}\n\t\tif err = gulu.JSON.UnmarshalJSON(legacyJSON, &legacyActions); err != nil {\n\t\t\treturn fmt.Errorf(\"unmarshal legacy AI editor actions failed: %w\", err)\n\t\t}\n\t}\n\n\ttype actionKey struct {\n\t\tname   string\n\t\taction string\n\t}\n\texisting := make(map[actionKey]struct{}, len(data.Actions))\n\tfor _, action := range data.Actions {\n\t\texisting[actionKey{name: action.Name, action: action.Action}] = struct{}{}","sourceCodeStart":197,"sourceCodeEnd":233,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/ai_editor.go#L197-L233","documentation":"migrateLegacyAIEditorActions reads the legacy actions stored in localStorage and, when the stored value is a JSON string, unmarshals it into []*legacyAIEditorAction. This error wraps that unmarshal failure, meaning the legacy string payload is malformed or does not match the legacy schema. Thrown (via loadAIEditorActions) by GetAIEditorActions, SaveAIEditorAction, and RemoveAIEditorAction.","triggerScenarios":"Migrating on first access when the localStorage entry for AI editor actions is a string that fails to parse into the legacy action array — corrupted localStorage value, hand-edited sync/conf data, or a legacy format written by a much older frontend build.","commonSituations":"Upgrading an old workspace whose localStorage JSON was corrupted; third-party scripts rewriting the localStorage entry; partial data from an interrupted sync of localStorage-backed storage.","solutions":["Inspect the wrapped error (%w) in the kernel log; then locate the legacy localStorage entry in the workspace storage JSON and fix or validate its JSON string.","Clear/repair the legacy localStorage entry so migration skips it (empty values are ignored) and re-create actions in the new format via the UI.","Restore the storage file from backup if the legacy data is important.","Reproduce offline: gulu.JSON.UnmarshalJSON([]byte(legacyString), &[]*legacyAIEditorAction{}) with the stored string to see the exact schema error."],"exampleFix":"// before: malformed legacy string in localStorage entry\n\"[{\\\"id\\\":\\\"20240101\\\",}]\"\n// after: valid legacy actions JSON\n\"[{\\\"id\\\":\\\"20240101120000-a1b2c3d4e5f6\\\",\\\"name\\\":\\\"Summarize\\\",\\\"action\\\":\\\"summarize\\\"}]\"","handlingStrategy":"fallback","validationCode":"// pre-validate the legacy string before migration runs\ntry { JSON.parse(legacyString); } catch (e) { /* repair or clear the legacy entry */ }","typeGuard":null,"tryCatchPattern":"try {\n  const actions = await fetchSyncPost('/api/ai/editor/getAIEditorActions', {});\n} catch (e) {\n  if (/unmarshal legacy AI editor actions failed/.test(e.message)) {\n    // clear the corrupt legacy localStorage entry; recreate actions in new format\n  }\n}","preventionTips":["Do not let third-party scripts rewrite localStorage-backed storage","Back up storage files before major upgrades","Validate legacy JSON after any manual merge","Prefer recreating actions in the new format over repairing legacy data"],"tags":["json","migration","legacy","localstorage"],"backgroundTag":"json-unmarshal-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"}