@@ -141,17 +141,43 @@ pub async fn heartbeat_loop(
141141 . unwrap_or_else ( || registered_models. clone ( ) )
142142 } ;
143143
144- // 取注册模型和当前模型的交集
144+ // 取注册模型和当前模型的交集:
145+ // 服务端强制校验 accepted_models 必须是注册列表的子集
145146 let active_models: Vec < String > = registered_models
146147 . into_iter ( )
147148 . filter ( |m| current_models. contains ( m) )
148149 . collect ( ) ;
149150
150- // 仅在扫描时才检查模型变化,减少日志噪音
151- if ( do_scan || heartbeat_count == 1 )
152- && active_models. len ( ) != session. capabilities . models . len ( )
153- {
154- if active_models. is_empty ( ) {
151+ if do_scan || heartbeat_count == 1 {
152+ info ! (
153+ "Accepted models (schedulable): {:?}" ,
154+ active_models
155+ ) ;
156+ }
157+
158+ // 新模型(不在注册列表中)不会被 accepted_models 包含,
159+ // 受限于服务端设计:capabilities_json 在注册时写入后不再更新,
160+ // 且心跳强制校验 accepted_models 必须是注册列表的子集。
161+ // 新模型需联系管理员更新数据库中 nodes.capabilities_json 后重启。
162+
163+ // 检测模型删除:注册中有的模型在当前 Ollama 中不再存在
164+ // 注意:不能通过 active_models 长度判断,因为新模型也会被加入
165+ let removed: Vec < & String > = session
166+ . capabilities
167+ . models
168+ . iter ( )
169+ . map ( |m| & m. model )
170+ . filter ( |reg_m| {
171+ // cached_models 为 None(Ollama 扫描失败)时不做删除判定
172+ cached_models
173+ . as_ref ( )
174+ . map ( |c| !c. contains ( * reg_m) )
175+ . unwrap_or ( false )
176+ } )
177+ . collect ( ) ;
178+
179+ if !removed. is_empty ( ) {
180+ if removed. len ( ) == session. capabilities . models . len ( ) {
155181 error ! (
156182 "All registered models have been removed from Ollama. \
157183 Node will not receive any tasks. \
@@ -163,16 +189,11 @@ pub async fn heartbeat_loop(
163189 . map( |m| & m. model)
164190 . collect:: <Vec <_>>( )
165191 ) ;
166- } else {
192+ } else if do_scan || heartbeat_count == 1 {
167193 warn ! (
168194 "Some registered models no longer available in Ollama. \
169- Registered: {:?}, Active: {:?}",
170- session
171- . capabilities
172- . models
173- . iter( )
174- . map( |m| & m. model)
175- . collect:: <Vec <_>>( ) ,
195+ Removed: {:?}, Remaining: {:?}",
196+ removed,
176197 active_models
177198 ) ;
178199 }
0 commit comments