applyRunModelOverride applies modelRef as the per-agent model override on the session backing rs. It mirrors the in-memory mutations that SetSessionAgentModel performs, but without acquiring sm.mux (the caller already holds it) and without an explicit store write — the caller's pending UpdateSession
(ctx context.Context, rs *activeRuntimes, modelRef string)
| 1066 | // invoke undo to roll the runtime override back; the in-memory session |
| 1067 | // fields are owned by the caller and rolled back inline. |
| 1068 | func (sm *SessionManager) applyRunModelOverride(ctx context.Context, rs *activeRuntimes, modelRef string) (prevOverride string, hadPrev bool, undo func(context.Context, string, bool), err error) { |
| 1069 | noop := func(context.Context, string, bool) {} |
| 1070 | if modelRef == "" { |
| 1071 | return "", false, noop, nil |
| 1072 | } |
| 1073 | if !rs.runtime.SupportsModelSwitching() { |
| 1074 | return "", false, noop, ErrModelSwitchingNotSupported |
| 1075 | } |
| 1076 | |
| 1077 | agentName := rs.runtime.CurrentAgentName(ctx) |
| 1078 | sess := rs.session |
| 1079 | |
| 1080 | if sess != nil && sess.AgentModelOverrides != nil { |
| 1081 | prevOverride, hadPrev = sess.AgentModelOverrides[agentName] |
| 1082 | } |
| 1083 | |
| 1084 | if err := rs.runtime.SetAgentModel(ctx, agentName, modelRef); err != nil { |
| 1085 | return "", false, noop, err |
| 1086 | } |
| 1087 | |
| 1088 | var appendedCustom bool |
| 1089 | if sess != nil { |
| 1090 | if sess.AgentModelOverrides == nil { |
| 1091 | sess.AgentModelOverrides = make(map[string]string) |
| 1092 | } |
| 1093 | sess.AgentModelOverrides[agentName] = modelRef |
| 1094 | if strings.Contains(modelRef, "/") && !slices.Contains(sess.CustomModelsUsed, modelRef) { |
| 1095 | sess.CustomModelsUsed = append(sess.CustomModelsUsed, modelRef) |
| 1096 | appendedCustom = true |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | undo = func(ctx context.Context, prev string, had bool) { |
| 1101 | rollback := prev |
| 1102 | if !had { |
| 1103 | rollback = "" |
| 1104 | } |
| 1105 | if rbErr := rs.runtime.SetAgentModel(ctx, agentName, rollback); rbErr != nil { |
| 1106 | slog.ErrorContext(ctx, "Failed to roll back runtime model override", "agent", agentName, "error", rbErr) |
| 1107 | } |
| 1108 | if sess == nil { |
| 1109 | return |
| 1110 | } |
| 1111 | if had { |
| 1112 | sess.AgentModelOverrides[agentName] = prev |
| 1113 | } else { |
| 1114 | delete(sess.AgentModelOverrides, agentName) |
| 1115 | } |
| 1116 | if appendedCustom { |
| 1117 | sess.CustomModelsUsed = sess.CustomModelsUsed[:len(sess.CustomModelsUsed)-1] |
| 1118 | } |
| 1119 | } |
| 1120 | return prevOverride, hadPrev, undo, nil |
| 1121 | } |
| 1122 | |
| 1123 | // applyStoredOverrides applies the persisted per-agent model overrides on |
| 1124 | // the freshly created runtime. Failures are logged at WARN and otherwise |
no test coverage detected