{"record":{"id":"fe12ffc68a10e958","repo":"siyuan-note/siyuan","slug":"agent-runtime-turn-changed","errorCode":null,"errorMessage":"agent runtime turn changed","messagePattern":"agent runtime turn changed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/agent/runtime.go","lineNumber":328,"sourceCode":"\tif !isValidSessionID(sessionID) {\n\t\treturn fmt.Errorf(\"invalid session id\")\n\t}\n\tlock := sessionLock(sessionID)\n\tlock.Lock()\n\tdefer lock.Unlock()\n\tcommitted, err := isTurnCommittedLocked(sessionID, turn.TurnID)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif committed {\n\t\treturn nil\n\t}\n\truntime, err := loadRuntimeLocked(sessionID)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif runtime.ActiveTurn != nil && runtime.ActiveTurn.TurnID != turn.TurnID {\n\t\treturn fmt.Errorf(\"agent runtime turn changed\")\n\t}\n\tturn.UpdatedAt = time.Now().UnixMilli()\n\truntime.ActiveTurn = turn\n\treturn writeRuntimeLocked(sessionID, runtime)\n}\n\nfunc saveRuntimeCompaction(sessionID string, compaction *runtimeCompaction) error {\n\tif sessionID == \"\" || compaction == nil {\n\t\treturn errContextCannotBeCompacted\n\t}\n\tif !isValidSessionID(sessionID) {\n\t\treturn fmt.Errorf(\"invalid session id\")\n\t}\n\tlock := sessionLock(sessionID)\n\tlock.Lock()\n\tdefer lock.Unlock()\n\truntime, err := loadRuntimeLocked(sessionID)\n\tif err != nil {","sourceCodeStart":310,"sourceCodeEnd":346,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/agent/runtime.go#L310-L346","documentation":"saveRuntimeTurn refuses to overwrite a runtime state whose ActiveTurn.TurnID differs from the turn being saved. This protects the single-active-turn invariant: once the client has moved on to a different turn (new turn, regenerate, recovery), writes from the old turn must not clobber the new state.","triggerScenarios":"Calling saveRuntimeTurn with a turn whose TurnID does not match runtime.ActiveTurn.TurnID loaded from disk — e.g. saving a turn after the user already started a new turn or a regeneration, or two writers racing on the same session where one loaded a stale turn.","commonSituations":"Client-side retries after a network error deliver an old turn payload; a UI fires a late autosave for a superseded turn; concurrent requests mutate the same session.","solutions":["Reload the runtime state and confirm the active TurnID before saving","Discard the stale turn write — it is intentionally rejected, not a bug","On the client, use the recovery/revision mechanism (BaseRevision, commitTurnID) instead of blind resaves","Serialize turn lifecycle per session so only the current turn's owner writes"],"exampleFix":"// before\nsaveRuntimeTurn(sessionID, staleTurn) // TurnID no longer active\n// after\nruntime := loadRuntime(sessionID)\nif runtime.ActiveTurn == nil || runtime.ActiveTurn.TurnID != staleTurn.TurnID {\n    return // turn was superseded; drop the write\n}\nsaveRuntimeTurn(sessionID, staleTurn)","handlingStrategy":"retry","validationCode":"runtime, _ := loadRuntime(sessionID)\nactive := runtime != nil && runtime.ActiveTurn != nil && runtime.ActiveTurn.TurnID == turn.TurnID","typeGuard":"func isActiveTurn(runtime *agentRuntime, turn *agentRuntimeTurn) bool {\n    return runtime.ActiveTurn != nil && runtime.ActiveTurn.TurnID == turn.TurnID\n}","tryCatchPattern":"if err := saveRuntimeTurn(id, turn); err != nil && strings.Contains(err.Error(), \"turn changed\") {\n    // turn superseded — reload state and drop or rebase the write\n}","preventionTips":["Only the owner of the currently active turn should write runtime state","Treat this error as a signal to discard the stale turn, not to retry blindly","Rebase onto the new active turn if the content is still needed"],"tags":["go","agent","concurrency","stale-write"],"backgroundTag":"invalid-state-transition","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"}