@@ -246,7 +246,7 @@ private async Task<LocalAiRuntimeSnapshot> EnsureStartedCoreAsync(CancellationTo
246246 install . ModelPath ,
247247 cancellationToken )
248248 . ConfigureAwait ( false ) ;
249- if ( probe . IsHealthy )
249+ if ( probe . IsReadyForManagedModel ( install . ModelPath ) )
250250 {
251251 LocalAiInstallManifest verifiedManifest = install . Manifest with
252252 {
@@ -334,26 +334,148 @@ private async Task<LocalAiRuntimeSnapshot> RefreshCoreAsync(CancellationToken ca
334334 LocalAiResolvedInstall install = _install ! ;
335335 EndpointOwnershipObservation ownership = DiscoverOwnedEndpoint ( install , _managedProcess ) ;
336336 if ( ! ownership . IsComplete )
337- return Publish ( LocalAiRuntimeState . Conflict , LocalAiOwnership . None , "TCP listener ownership could not be determined." ) ;
337+ {
338+ LocalAiRuntimeSnapshot ? failure = await QuiesceOrStopAsync (
339+ install ,
340+ cancellationToken )
341+ . ConfigureAwait ( false ) ;
342+ return failure ?? Publish (
343+ LocalAiRuntimeState . Conflict ,
344+ LocalAiOwnership . None ,
345+ "TCP listener ownership could not be determined." ) ;
346+ }
338347 if ( ownership . ConflictDetail is not null )
339- return Publish ( LocalAiRuntimeState . Conflict , LocalAiOwnership . None , ownership . ConflictDetail ) ;
348+ {
349+ LocalAiRuntimeSnapshot ? failure = await QuiesceOrStopAsync (
350+ install ,
351+ cancellationToken )
352+ . ConfigureAwait ( false ) ;
353+ return failure ?? Publish (
354+ LocalAiRuntimeState . Conflict ,
355+ LocalAiOwnership . None ,
356+ ownership . ConflictDetail ) ;
357+ }
340358 if ( ownership . Endpoint is null )
341- return Publish ( LocalAiRuntimeState . Starting , LocalAiOwnership . CompanionManaged , "The local AI router has not opened its endpoint yet." , _managedProcess . ProcessId , _managedProcess . StartedAtUtc ) ;
359+ {
360+ LocalAiRuntimeSnapshot ? failure = await QuiesceOrStopAsync (
361+ install ,
362+ cancellationToken )
363+ . ConfigureAwait ( false ) ;
364+ return failure ?? Publish (
365+ LocalAiRuntimeState . Starting ,
366+ LocalAiOwnership . CompanionManaged ,
367+ "The local AI router has not opened its endpoint yet." ,
368+ _managedProcess . ProcessId ,
369+ _managedProcess . StartedAtUtc ) ;
370+ }
342371
343372 LlamaServerRouterProbeResult probe = await _client . ProbeManagedModelAsync (
344373 ownership . Endpoint ,
345374 install . Manifest . ModelAlias ,
346375 install . ModelPath ,
347376 cancellationToken )
348377 . ConfigureAwait ( false ) ;
349- return probe . IsHealthy
350- ? PublishHealthy ( probe )
351- : Publish (
352- LocalAiRuntimeState . Starting ,
353- LocalAiOwnership . CompanionManaged ,
354- "The local AI router is not healthy yet." ,
355- _managedProcess . ProcessId ,
356- _managedProcess . StartedAtUtc ) ;
378+ if ( probe . IsReadyForManagedModel ( install . ModelPath ) )
379+ {
380+ bool endpointChanged = install . Endpoint != ownership . Endpoint ;
381+ if ( _snapshot . State != LocalAiRuntimeState . Healthy || endpointChanged )
382+ {
383+ if ( endpointChanged && _snapshot . State == LocalAiRuntimeState . Healthy )
384+ {
385+ LocalAiRuntimeSnapshot ? failure = await QuiesceOrStopAsync (
386+ install ,
387+ cancellationToken )
388+ . ConfigureAwait ( false ) ;
389+ if ( failure is not null )
390+ return failure ;
391+ }
392+
393+ install = await BindVerifiedEndpointAsync (
394+ install ,
395+ ownership . Endpoint ,
396+ cancellationToken )
397+ . ConfigureAwait ( false ) ;
398+ LocalAiEndpointLifecycleResult published = await _options . EndpointLifecycle
399+ . PublishAsync ( install , cancellationToken )
400+ . ConfigureAwait ( false ) ;
401+ if ( ! published . Success )
402+ {
403+ return Publish (
404+ LocalAiRuntimeState . Failed ,
405+ LocalAiOwnership . CompanionManaged ,
406+ published . Detail ?? "The verified Local AI endpoint could not be republished." ,
407+ _managedProcess . ProcessId ,
408+ _managedProcess . StartedAtUtc ) ;
409+ }
410+ }
411+
412+ return PublishHealthy ( probe ) ;
413+ }
414+
415+ LocalAiRuntimeSnapshot ? quiesceFailure = await QuiesceOrStopAsync (
416+ install ,
417+ cancellationToken )
418+ . ConfigureAwait ( false ) ;
419+ if ( quiesceFailure is not null )
420+ return quiesceFailure ;
421+
422+ return Publish (
423+ LocalAiRuntimeState . Starting ,
424+ LocalAiOwnership . CompanionManaged ,
425+ probe . Detail ?? "The local AI router has not verified the managed model yet." ,
426+ _managedProcess . ProcessId ,
427+ _managedProcess . StartedAtUtc ) ;
428+ }
429+
430+ private async Task < LocalAiRuntimeSnapshot ? > QuiesceOrStopAsync (
431+ LocalAiResolvedInstall install ,
432+ CancellationToken cancellationToken )
433+ {
434+ LocalAiEndpointLifecycleResult ? quiesced = null ;
435+ try
436+ {
437+ quiesced = await _options . EndpointLifecycle
438+ . QuiesceAsync ( install , cancellationToken )
439+ . ConfigureAwait ( false ) ;
440+ }
441+ finally
442+ {
443+ if ( quiesced is null )
444+ {
445+ ++ _generation ;
446+ await DisposeManagedProcessAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
447+ Publish (
448+ LocalAiRuntimeState . Failed ,
449+ LocalAiOwnership . None ,
450+ "The Local AI gateway provider withdrawal did not complete." ) ;
451+ }
452+ }
453+ if ( quiesced . Success )
454+ return null ;
455+
456+ ++ _generation ;
457+ await DisposeManagedProcessAsync ( CancellationToken . None ) . ConfigureAwait ( false ) ;
458+ return Publish (
459+ LocalAiRuntimeState . Failed ,
460+ LocalAiOwnership . None ,
461+ quiesced . Detail ?? "The Local AI gateway provider could not be safely disabled." ) ;
462+ }
463+
464+ private async Task < LocalAiResolvedInstall > BindVerifiedEndpointAsync (
465+ LocalAiResolvedInstall install ,
466+ Uri endpoint ,
467+ CancellationToken cancellationToken )
468+ {
469+ if ( install . Endpoint == endpoint )
470+ return install ;
471+
472+ LocalAiInstallManifest verifiedManifest = install . Manifest with
473+ {
474+ Endpoint = endpoint . AbsoluteUri ,
475+ } ;
476+ await _manifestStore . SaveAsync ( verifiedManifest , cancellationToken ) . ConfigureAwait ( false ) ;
477+ _install = _manifestStore . ResolveAndValidate ( verifiedManifest ) ;
478+ return _install ;
357479 }
358480
359481 private async Task < bool > TryLoadInstallAsync ( CancellationToken cancellationToken )
0 commit comments