@@ -3,6 +3,7 @@ use std::path::PathBuf;
33use sha2:: { Digest , Sha256 } ;
44use tauri:: { AppHandle , Manager , State } ;
55
6+ use super :: mesh_readiness:: wait_for_mesh_inference;
67use crate :: { app_state:: AppState , mesh_llm, relay} ;
78
89#[ derive( Clone , Debug , serde:: Deserialize , serde:: Serialize ) ]
@@ -530,171 +531,6 @@ pub async fn mesh_start_node(
530531 Ok ( status)
531532}
532533
533- /// Mesh can bind its HTTP ingress and advertise a model shortly before the
534- /// router has installed a usable target. Probe the exact chat path agents use
535- /// so startup cannot race that gap (`single target None unavailable`).
536- /// Which startup stage a mesh client is stuck at when it never becomes
537- /// inference-ready. The two live-observed failure modes are physically
538- /// distinct and want different user copy:
539- ///
540- /// * `CatalogNeverSynced` — the local client node came up and connected to
541- /// the host at the control level (ping/RTT fine), but the served model
542- /// never appeared in the local `/v1/models` catalog. That catalog is
543- /// populated by the peer gossip exchange; when the gossip bi-stream can't
544- /// establish across the network (observed as iroh
545- /// `MultipathNotNegotiated` / unreachable direct path), the catalog stays
546- /// empty forever and every request is rejected "model not available".
547- /// Root cause is the network path between this machine and the host.
548- /// * `RoutingNeverCompleted` — the model *did* sync into the catalog, but
549- /// inference requests never completed (routing/transport to the host
550- /// failing per-request). The host is discoverable and advertised but not
551- /// actually serving us.
552- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
553- enum MeshReadinessFailure {
554- CatalogNeverSynced ,
555- RoutingNeverCompleted ,
556- }
557-
558- /// Pure classifier: given whether the served model was ever observed in the
559- /// local `/v1/models` catalog during the wait, decide which stage failed.
560- /// Split out so the diagnosis is unit-testable without a live mesh.
561- /// Whether the catalog has synced enough to count, for the model actually being
562- /// requested (a wire name — see `relay_mesh_wire_model`).
563- ///
564- /// The virtual `mesh` model delegates the choice to the router, so any
565- /// advertised model proves the catalog synced. It has to work that way: MeshLLM
566- /// only advertises `mesh` itself once two non-virtual models are reachable
567- /// (`should_advertise_virtual_mesh`), so requiring it by name would leave a
568- /// single-worker mesh looking permanently unsynced and misreport a slow model
569- /// load as a network path problem.
570- fn mesh_catalog_shows_model ( advertised : & [ String ] , wire_model : & str ) -> bool {
571- if advertised. is_empty ( ) {
572- return false ;
573- }
574- if wire_model == crate :: managed_agents:: RELAY_MESH_VIRTUAL_MODEL_ID {
575- return true ;
576- }
577- let wanted = wire_model. trim ( ) . replace ( "@main" , "" ) ;
578- advertised
579- . iter ( )
580- . any ( |id| id. replace ( "@main" , "" ) == wanted)
581- }
582-
583- fn classify_mesh_readiness_failure ( model_ever_visible : bool ) -> MeshReadinessFailure {
584- if model_ever_visible {
585- MeshReadinessFailure :: RoutingNeverCompleted
586- } else {
587- MeshReadinessFailure :: CatalogNeverSynced
588- }
589- }
590-
591- /// Actionable, non-technical copy for a readiness failure. `last_detail` is the
592- /// last raw transport/HTTP error, appended for support triage.
593- fn mesh_readiness_failure_message (
594- failure : MeshReadinessFailure ,
595- model_id : & str ,
596- last_detail : & str ,
597- ) -> String {
598- match failure {
599- MeshReadinessFailure :: CatalogNeverSynced => format ! (
600- "Buzz shared compute connected to the serving member but could not sync \
601- the model list for \" {model_id}\" — this is a network path problem \
602- between this machine and the host (the compute node is reachable for \
603- pings but the model-sync stream did not establish). Try again, or have \
604- the host and this machine on a more direct network. (last: {last_detail})"
605- ) ,
606- MeshReadinessFailure :: RoutingNeverCompleted => format ! (
607- "Buzz shared compute found \" {model_id}\" on a serving member but inference \
608- requests did not complete — the host is discoverable but not currently \
609- reachable for requests. Try again shortly. (last: {last_detail})"
610- ) ,
611- }
612- }
613-
614- /// Poll the local mesh OpenAI ingress until a real inference for `model_id`
615- /// succeeds, or a deadline elapses. On failure, returns a stage-specific,
616- /// actionable message (see [`MeshReadinessFailure`]) rather than a raw
617- /// `HTTP 429`, so the UI can tell "still warming up" apart from "can't reach
618- /// the host".
619- async fn wait_for_mesh_inference ( model_id : & str ) -> CmdResult < ( ) > {
620- // Probe the name that will actually be requested. Callers pass a stored
621- // value, which for shared-compute `auto` is not a model the mesh
622- // advertises: probing it would validate a route no agent uses, and could
623- // fail readiness while the real route works. Named models pass through
624- // unchanged, so this is safe for the serve-side callers too.
625- let requested_model = model_id;
626- let model_id = crate :: managed_agents:: relay_mesh_wire_model ( model_id) ;
627- let client = reqwest:: Client :: builder ( )
628- . timeout ( std:: time:: Duration :: from_secs ( 30 ) )
629- . build ( )
630- . map_err ( |error| format ! ( "failed to build mesh readiness client: {error}" ) ) ?;
631- let deadline = tokio:: time:: Instant :: now ( ) + std:: time:: Duration :: from_secs ( 120 ) ;
632- let models_url = format ! ( "{}/models" , crate :: managed_agents:: RELAY_MESH_API_BASE_URL ) ;
633- let chat_url = format ! (
634- "{}/chat/completions" ,
635- crate :: managed_agents:: RELAY_MESH_API_BASE_URL
636- ) ;
637- let mut last_error = "mesh inference is not ready" . to_string ( ) ;
638- // Track whether the served model ever reached the local catalog — the
639- // signal that splits "catalog never synced" from "routing never completed".
640- let mut model_ever_visible = false ;
641-
642- while tokio:: time:: Instant :: now ( ) < deadline {
643- // Refresh catalog visibility. The virtual `mesh` model delegates the
644- // choice to the router, so any advertised model counts as the catalog
645- // having synced — and it must, because MeshLLM only advertises `mesh`
646- // itself once two non-virtual models are reachable
647- // (`should_advertise_virtual_mesh`). Requiring it by name would leave a
648- // single-worker mesh looking permanently unsynced.
649- if let Ok ( response) = client
650- . get ( & models_url)
651- . bearer_auth ( crate :: managed_agents:: RELAY_MESH_API_KEY_PLACEHOLDER )
652- . send ( )
653- . await
654- {
655- if let Ok ( body) = response. json :: < serde_json:: Value > ( ) . await {
656- if let Some ( data) = body. get ( "data" ) . and_then ( |d| d. as_array ( ) ) {
657- let advertised: Vec < String > = data
658- . iter ( )
659- . filter_map ( |m| m. get ( "id" ) . and_then ( |id| id. as_str ( ) ) )
660- . map ( str:: to_owned)
661- . collect ( ) ;
662- model_ever_visible |= mesh_catalog_shows_model ( & advertised, model_id) ;
663- }
664- }
665- }
666-
667- match client
668- . post ( & chat_url)
669- . bearer_auth ( crate :: managed_agents:: RELAY_MESH_API_KEY_PLACEHOLDER )
670- . json ( & serde_json:: json!( {
671- "model" : model_id,
672- "messages" : [ { "role" : "user" , "content" : "Reply OK" } ] ,
673- "max_tokens" : 1 ,
674- "stream" : false
675- } ) )
676- . send ( )
677- . await
678- {
679- Ok ( response) if response. status ( ) . is_success ( ) => return Ok ( ( ) ) ,
680- Ok ( response) => {
681- let status = response. status ( ) ;
682- let body = response. text ( ) . await . unwrap_or_default ( ) ;
683- last_error = format ! ( "HTTP {status}: {body}" ) ;
684- }
685- Err ( error) => last_error = error. to_string ( ) ,
686- }
687- tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) . await ;
688- }
689-
690- let failure = classify_mesh_readiness_failure ( model_ever_visible) ;
691- Err ( mesh_readiness_failure_message (
692- failure,
693- requested_model,
694- & last_error,
695- ) )
696- }
697-
698534pub ( crate ) async fn ensure_client_node_for_model (
699535 state : & AppState ,
700536 model_id : impl AsRef < str > ,
0 commit comments