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

  1. Reload the runtime state and confirm the active TurnID before saving
  2. Discard the stale turn write — it is intentionally rejected, not a bug
  3. On the client, use the recovery/revision mechanism (BaseRevision, commitTurnID) instead of blind resaves
  4. 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

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


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)