{"record":{"id":"9b858ec5f3489dda","repo":"siyuan-note/siyuan","slug":"encode-session-data-failed-w","errorCode":null,"errorMessage":"encode session data failed: %w","messagePattern":"encode session data failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/session.go","lineNumber":412,"sourceCode":"\t\t\tif !isRuntimeTurnTerminal(runtime.ActiveTurn) {\n\t\t\t\treturn currentRevision, nil, ErrRuntimeNotFinalized\n\t\t\t}\n\t\t\tif err := applyRuntimeTurnToSessionLocked(newData, runtime.ActiveTurn); err != nil {\n\t\t\t\treturn currentRevision, nil, err\n\t\t\t}\n\t\t} else if currentCommittedTurnID != commitTurnID {\n\t\t\treturn currentRevision, nil, ErrSessionConflict\n\t\t}\n\t}\n\n\tnewRevision := currentRevision + 1\n\tnewData[\"revision\"] = newRevision\n\tif commitTurnID != \"\" {\n\t\tnewData[\"lastCommittedTurnID\"] = commitTurnID\n\t}\n\tdata, err = gulu.JSON.MarshalIndentJSON(newData, \"\", \"\\t\")\n\tif err != nil {\n\t\treturn currentRevision, nil, fmt.Errorf(\"encode session data failed: %w\", err)\n\t}\n\n\tif err := os.MkdirAll(dir, 0755); err != nil {\n\t\treturn currentRevision, nil, fmt.Errorf(\"create session dir failed: %w\", err)\n\t}\n\tif err := filelock.WriteFile(path, data); err != nil {\n\t\treturn currentRevision, nil, fmt.Errorf(\"save session file failed: %w\", err)\n\t}\n\tif commitTurnID != \"\" {\n\t\tif err := markRuntimeCommittedLocked(meta.ID, commitTurnID); err != nil {\n\t\t\tlogging.LogWarnf(\"commit agent runtime failed: %s\", err)\n\t\t}\n\t}\n\n\ttitle, _ := newData[\"title\"].(string)\n\tif title == \"\" {\n\t\ttitle = \"AI Agent\"\n\t}","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/session.go#L394-L430","documentation":"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).","triggerScenarios":"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).","commonSituations":"Storing custom runtime objects or Go-specific types in session fields; injecting values read from an untrusted source with unsupported types; cyclic data structures.","solutions":["Ensure all values in the session data map are JSON-marshalable (string keys, basic types, slices, maps, tagged structs)","Remove or convert unsupported values (func/channel/cyclic refs) before saving","Log newData keys and value types when this error occurs to find the offending field"],"exampleFix":"// before: storing a non-serializable value\nnewData[\"callback\"] = someFunc\n// after: store a serializable identifier instead\nnewData[\"callbackID\"] = someFuncID","handlingStrategy":"validation","validationCode":"for k, v := range newData {\n    if err := json.Valid(mustMarshal(v)); err != nil {\n        return fmt.Errorf(\"session field %q is not JSON-serializable\", k)\n    }\n}","typeGuard":"func isJSONSafe(v any) bool {\n    switch v.(type) {\n    case func(), chan struct{}:\n        return false\n    }\n    return true\n}","tryCatchPattern":"if err != nil && strings.HasPrefix(err.Error(), \"encode session data failed\") {\n    return fmt.Errorf(\"session data contained a non-serializable value: %w\", err)\n}","preventionTips":["Store only JSON-compatible types in session data maps","Avoid putting functions, channels, or cyclic structures into session state","Add a marshal smoke-test for session data in CI"],"tags":["json","serialization","session"],"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"}