Skip to content

Commit d3cdb0e

Browse files
committed
refactor(mesh): give startup readiness its own module
The review fix pushed `commands/mesh_llm.rs` from 994 to 1023 lines, over the 1000-line ceiling the desktop ratchet enforces — a file already over the limit may not grow, and this one was three lines under it. Readiness is a separable responsibility: probe the chat path agents actually use, and classify a timeout into copy that names the stage. It moves whole, with its tests, into `commands/mesh_readiness.rs`. No behaviour change. mesh_llm.rs 1023 -> 858 mesh_readiness.rs 251 (with its 7 tests) The tests move with the code they cover rather than staying behind in `mesh_llm_tests.rs`, so the module owns its own contract. Signed-off-by: Michael Neale <[email protected]>
1 parent c3bcdea commit d3cdb0e

4 files changed

Lines changed: 254 additions & 241 deletions

File tree

‎desktop/src-tauri/src/commands/mesh_llm.rs‎

Lines changed: 1 addition & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::path::PathBuf;
33
use sha2::{Digest, Sha256};
44
use tauri::{AppHandle, Manager, State};
55

6+
use super::mesh_readiness::wait_for_mesh_inference;
67
use 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-
698534
pub(crate) async fn ensure_client_node_for_model(
699535
state: &AppState,
700536
model_id: impl AsRef<str>,

‎desktop/src-tauri/src/commands/mesh_llm_tests.rs‎

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -178,82 +178,6 @@ fn role_switch_checkpoint_starts_exactly_once_after_restart() {
178178
assert_eq!(consumed.relay_url, config.relay_url);
179179
}
180180

181-
/// The regression this guards: MeshLLM only advertises the virtual `mesh` model
182-
/// once two non-virtual models are reachable, so a single-worker mesh never
183-
/// lists it by name. Keying visibility on the literal name would report a lone
184-
/// host that is still loading weights as a network path problem.
185-
#[test]
186-
fn virtual_mesh_counts_any_advertised_model_as_a_synced_catalog() {
187-
let one_worker = vec!["unsloth/gemma-4-E4B-it-GGUF:Q4_K_M".to_string()];
188-
assert!(mesh_catalog_shows_model(
189-
&one_worker,
190-
crate::managed_agents::RELAY_MESH_VIRTUAL_MODEL_ID
191-
));
192-
}
193-
194-
#[test]
195-
fn an_empty_catalog_is_never_synced_even_for_the_virtual_model() {
196-
assert!(!mesh_catalog_shows_model(
197-
&[],
198-
crate::managed_agents::RELAY_MESH_VIRTUAL_MODEL_ID
199-
));
200-
}
201-
202-
#[test]
203-
fn a_named_model_must_actually_be_advertised() {
204-
let advertised = vec!["unsloth/gemma-4-E4B-it-GGUF:Q4_K_M".to_string()];
205-
assert!(mesh_catalog_shows_model(
206-
&advertised,
207-
"unsloth/gemma-4-E4B-it-GGUF:Q4_K_M"
208-
));
209-
assert!(!mesh_catalog_shows_model(&advertised, "some/other-model"));
210-
}
211-
212-
#[test]
213-
fn a_named_model_ignores_the_main_revision_suffix() {
214-
let advertised = vec!["unsloth/gemma-4-E4B-it-GGUF:Q4_K_M@main".to_string()];
215-
assert!(mesh_catalog_shows_model(
216-
&advertised,
217-
"unsloth/gemma-4-E4B-it-GGUF:Q4_K_M"
218-
));
219-
}
220-
221-
#[test]
222-
fn readiness_failure_is_catalog_sync_when_model_never_visible() {
223-
assert_eq!(
224-
classify_mesh_readiness_failure(false),
225-
MeshReadinessFailure::CatalogNeverSynced
226-
);
227-
}
228-
229-
#[test]
230-
fn readiness_failure_is_routing_when_model_was_visible() {
231-
assert_eq!(
232-
classify_mesh_readiness_failure(true),
233-
MeshReadinessFailure::RoutingNeverCompleted
234-
);
235-
}
236-
237-
#[test]
238-
fn readiness_messages_are_distinct_and_actionable() {
239-
let catalog = mesh_readiness_failure_message(
240-
MeshReadinessFailure::CatalogNeverSynced,
241-
"auto",
242-
"HTTP 429",
243-
);
244-
let routing = mesh_readiness_failure_message(
245-
MeshReadinessFailure::RoutingNeverCompleted,
246-
"auto",
247-
"HTTP 503",
248-
);
249-
// Distinct diagnoses, each names the model and carries the raw detail.
250-
assert_ne!(catalog, routing);
251-
assert!(catalog.contains("network path"));
252-
assert!(catalog.contains("HTTP 429"));
253-
assert!(routing.contains("did not complete"));
254-
assert!(routing.contains("HTTP 503"));
255-
}
256-
257181
#[test]
258182
fn mesh_status_cursor_uses_relay_composite_tiebreak() {
259183
let event = nostr::EventBuilder::new(nostr::Kind::TextNote, "status")

0 commit comments

Comments
 (0)