{"record":{"id":"bd4dcf633093b581","repo":"siyuan-note/siyuan","slug":"read-agent-runtime-failed-w","errorCode":null,"errorMessage":"read agent runtime failed: %w","messagePattern":"read agent runtime failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/session.go","lineNumber":388,"sourceCode":"\t\t\t\tif _, ok := newData[k]; !ok {\n\t\t\t\t\t// messages 是已废弃的旧会话字段，不再带入新格式；其他未知字段原样保留，\n\t\t\t\t\t// 避免前后端版本不一致时擦除较新版本写入的数据。\n\t\t\t\t\tif k != \"messages\" && k != \"expectedRevision\" && k != \"commitTurnID\" &&\n\t\t\t\t\t\tk != \"recoveryTurnID\" && k != \"recoveryState\" && k != \"recoveryRevision\" && k != \"agentRunning\" {\n\t\t\t\t\t\tnewData[k] = v\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t} else if err != nil && !os.IsNotExist(err) {\n\t\treturn 0, nil, fmt.Errorf(\"read session file failed: %w\", err)\n\t} else if meta.ExpectedRevision != nil && *meta.ExpectedRevision != 0 {\n\t\treturn 0, nil, ErrSessionConflict\n\t}\n\tif commitTurnID != \"\" {\n\t\truntime, err := loadRuntimeLocked(meta.ID)\n\t\tif err != nil {\n\t\t\treturn currentRevision, nil, fmt.Errorf(\"read agent runtime failed: %w\", err)\n\t\t}\n\t\tif runtime.ActiveTurn != nil {\n\t\t\tif runtime.ActiveTurn.TurnID != commitTurnID {\n\t\t\t\treturn currentRevision, nil, ErrSessionConflict\n\t\t\t}\n\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","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/session.go#L370-L406","documentation":"After reading the session file, SaveSessionState may need to load the persisted agent runtime (when commitTurnID is set) via loadRuntimeLocked. If loading the runtime fails, the error is wrapped as 'read agent runtime failed' and the commit is aborted before any state is written.","triggerScenarios":"Calling SaveSessionState with a non-empty commitTurnID whose runtime file cannot be read by loadRuntimeLocked: missing runtime file at an unexpected point, corrupt runtime JSON, or permission/IO error on the runtime file.","commonSituations":"The runtime file was deleted or corrupted by external tooling or a previous crash; the session was created by an older version writing a different runtime format; permissions on the agent runtime directory changed.","solutions":["Inspect the wrapped cause from loadRuntimeLocked (log it) and fix the underlying read error","Restore or delete the corrupt runtime file so it can be recreated on next commit","Verify the session was created with a compatible version of the runtime schema","Ensure the runtime directory is readable by the kernel process user"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"rev, data, err := agent.SaveSessionState(meta, commitTurnID, ...)\nif err != nil && strings.Contains(err.Error(), \"read agent runtime failed\") {\n    log.Printf(\"runtime load failed: %v; recovering by rebuilding runtime state\", err)\n}","preventionTips":["Never delete or hand-edit the agent runtime file while sessions are active","Keep kernel versions consistent so runtime schema matches","Include runtime files in workspace backups"],"tags":["io","runtime","persistence","session"],"backgroundTag":"file-read-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"}