siyuan-note/siyuan · error
agent runtime turn changed
Error message
agent runtime turn changed
What it means
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.
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
Example fix
// before
saveRuntimeTurn(sessionID, staleTurn) // TurnID no longer active
// after
runtime := loadRuntime(sessionID)
if runtime.ActiveTurn == nil || runtime.ActiveTurn.TurnID != staleTurn.TurnID {
return // turn was superseded; drop the write
}
saveRuntimeTurn(sessionID, staleTurn) Defensive patterns
Strategy: retry
Validate before calling
runtime, _ := loadRuntime(sessionID) active := runtime != nil && runtime.ActiveTurn != nil && runtime.ActiveTurn.TurnID == turn.TurnID
Type guard
func isActiveTurn(runtime *agentRuntime, turn *agentRuntimeTurn) bool {
return runtime.ActiveTurn != nil && runtime.ActiveTurn.TurnID == turn.TurnID
} Try / catch
if err := saveRuntimeTurn(id, turn); err != nil && strings.Contains(err.Error(), "turn changed") {
// turn superseded — reload state and drop or rebase the write
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- agent session revision conflict
- agent runtime turn is not finalized
- attribute view order changed; retry the drag
- attribute view rows changed; retry the drag
- block position changed since conversion
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/fe12ffc68a10e958.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/agent/runtime.go:328
if !isValidSessionID(sessionID) {
return fmt.Errorf("invalid session id")
}
lock := sessionLock(sessionID)
lock.Lock()
defer lock.Unlock()
committed, err := isTurnCommittedLocked(sessionID, turn.TurnID)
if err != nil {
return err
}
if committed {
return nil
}
runtime, err := loadRuntimeLocked(sessionID)
if err != nil {
return err
}
if runtime.ActiveTurn != nil && runtime.ActiveTurn.TurnID != turn.TurnID {
return fmt.Errorf("agent runtime turn changed")
}
turn.UpdatedAt = time.Now().UnixMilli()
runtime.ActiveTurn = turn
return writeRuntimeLocked(sessionID, runtime)
}
func saveRuntimeCompaction(sessionID string, compaction *runtimeCompaction) error {
if sessionID == "" || compaction == nil {
return errContextCannotBeCompacted
}
if !isValidSessionID(sessionID) {
return fmt.Errorf("invalid session id")
}
lock := sessionLock(sessionID)
lock.Lock()
defer lock.Unlock()
runtime, err := loadRuntimeLocked(sessionID)
if err != nil {View on GitHub (pinned to 9f775e8a12)