The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
merge: preserve CI compatibility fixes
Steven Enamakelf8badd86ea7f
diff --cc src/openhuman/inference/embeddings/rpc_part_01.rsindex 959cc6cd59,0000000000..79ece69161mode 100644,000000..100644--- a/src/openhuman/inference/embeddings/rpc_part_01.rs+++ b/src/openhuman/inference/embeddings/rpc_part_01.rs@@@ -1,639 -1,0 +1,663 @@@+use std::collections::HashMap;++use crate::openhuman::config::Config;+use crate::openhuman::security::credentials::AuthService;+use crate::rpc::RpcOutcome;++use super::catalog;+use super::factory::{create_embedding_provider_with_config, model_supports_dimensions};++const LOG_PREFIX: &str = "[embeddings::rpc]";++/// Slug naming the embedder ingestion will actually use, resolved host-side+/// from the `Config` fields the resolution ladder reads.+///+/// Mirrors `tinymemory_core::tree::score::embed::effective_embedder_slug` so+/// `get_settings` no longer calls `tinymemory_core::` directly (#5560).+///+/// `MemoryScoring::embedder_slug()` is not used here for two reasons:+/// (1) `get_settings` is a synchronous config-reading RPC handler and cannot+/// await an async bus call; (2) this function answers "what slug will ingestion+/// use?" — a config-derived prediction that must work even when the module is+/// not loaded. The bus call would give the same answer when the module is+/// running, but would fail gracefully when it is not, offering no benefit over+/// reading the config directly. Keep both implementations in sync whenever the+/// engine's resolution ladder changes.+///+/// Resolution order (matches the engine factory's ladder):+/// 1. Explicit Ollama override — `memory_tree.embedding_endpoint` ++/// `memory_tree.embedding_model` both `Some` and non-empty → `"ollama"`.+/// 2. Deliberate opt-out — `embeddings_provider` trimmed equals `"none"` → `"none"`.+/// 3. Local Ollama via unified workload setting — `workload_local_model("embeddings")`+/// is `Some` → `"ollama"`.+/// 4. User OpenAI-compatible endpoint — `memory.embedding_provider` is+/// `"openai"`, `"custom"`, or starts with `"custom:"` → `"custom"`.+/// 5. Managed cloud session — `auth-profiles.json` exists next to the config+/// file → `"cloud"`.+/// 6. Nothing usable → `"unconfigured"`.+fn effective_embedder_slug_from_config(config: &Config) -> &'static str {+ // 1. Explicit Ollama override.+ if let (Some(ep), Some(model)) = (+ config.memory_tree.embedding_endpoint.as_deref(),+ config.memory_tree.embedding_model.as_deref(),+ ) {+ if !ep.trim().is_empty() && !model.trim().is_empty() {+ return "ollama";+ }+ }+ // 2. Deliberate opt-out.+ if config+ .embeddings_provider+ .as_deref()+ .map(str::trim)+ .is_some_and(|s| s == "none")+ {+ return "none";+ }+ // 3. Local Ollama via unified workload setting.+ if config.workload_local_model("embeddings").is_some() {+ return "ollama";+ }+ // 4. User OpenAI-compatible endpoint.+ let picker = config.memory.embedding_provider.trim();+ if picker == "openai" || picker == "custom" || picker.starts_with("custom:") {+ return "custom";+ }+ // 5. Managed cloud session.+ let session_exists = config+ .config_path+ .parent()+ .map(|dir| dir.join("auth-profiles.json").exists())+ .unwrap_or(false);+ if session_exists {+ return "cloud";+ }+ "unconfigured"+}+- /// Dimension to run a Custom (OpenAI-compatible) verification probe at.- ///- /// The user-entered `dimensions` field is a guess: for any model outside the- /// `text-embedding-3-*` family we never send the OpenAI `dimensions` request- /// param (see [`model_supports_dimensions`]), so the endpoint returns its own- /// native vector length. Forcing the probe to enforce the guessed length makes- /// every reachable, valid embedding endpoint fail verification whenever the- /// guess (default 1024) differs from the native size — the root cause of- /// issue #4056.- ///- /// So we probe a `text-embedding-3-*` model at the configured size (the server- /// honours the param and returns exactly that), but probe every other model at- /// `0`, which disables both the request param and the post-response length- /// guard in `OpenAiEmbedding::embed` — the probe then only has to prove the- /// endpoint can embed, and we learn the real dimension from the returned- /// vector (see [`final_probe_dims`]).- fn probe_dims_for(model: &str, configured: usize) -> usize {- if model_supports_dimensions(model) {- configured++/// Send one OpenAI-compatible embedding request without requesting or++/// validating a vector width. This is intentionally separate from the live++/// provider: a setup probe must discover a custom endpoint's native width,++/// while a live provider must enforce the width persisted after that probe.++async fn probe_custom_embeddings(++ endpoint: &str,++ api_key: &str,++ model: &str,++) -> Result<Vec<Vec<f32>>, String> {++ let base = endpoint.trim_end_matches('/');++ let url = if base.ends_with("/embeddings") {++ base.to_string()++ } else if base.ends_with("/v1") {++ format!("{base}/embeddings")+ } else {- 0++ format!("{base}/v1/embeddings")++ };++ let mut request = reqwest::Client::new()++ .post(&url)++ .json(&serde_json::json!({ "model": model, "input": ["connection test"] }));++ if !api_key.trim().is_empty() {++ request = request.bearer_auth(api_key.trim());+ }++ let response = request++ .send()++ .await++ .map_err(|e| format!("custom embeddings request to {url} failed: {e}"))?;++ let status = response.status();++ let body = response++ .text()++ .await++ .map_err(|e| format!("custom embeddings response read failed: {e}"))?;++ if !status.is_success() {++ return Err(format!("custom embeddings returned HTTP {status}: {body}"));++ }++ let data = serde_json::from_str::<serde_json::Value>(&body)++ .map_err(|e| format!("custom embeddings response was not JSON: {e}"))?++ .get("data")++ .and_then(serde_json::Value::as_array)++ .cloned()++ .ok_or_else(|| "custom embeddings response missing data array".to_string())?;++ data.into_iter()++ .map(|item| {++ item.get("embedding")++ .and_then(serde_json::Value::as_array)++ .ok_or_else(|| "custom embeddings response missing embedding array".to_string())?++ .iter()++ .map(|value| value.as_f64().map(|value| value as f32).ok_or_else(|| "custom embeddings response contains a non-numeric vector".to_string()))++ .collect()++ })++ .collect()+}++/// Dimension to persist after a successful Custom verification probe.+///+/// For a `text-embedding-3-*` model the endpoint honoured the requested size,+/// so keep the user's `configured` value (Matryoshka). For every other model we+/// probed dimension-agnostically, so adopt the endpoint's actual returned+/// length (`actual`) — the user can't be expected to know it, and storing the+/// real size is what lets the live embed path's length guard pass afterwards.+/// Falls back to `configured` if the probe somehow reported a zero-length+/// vector (defensive — `classify_embed_probe` already rejects empty vectors).+fn final_probe_dims(model: &str, configured: usize, actual: usize) -> usize {+ if model_supports_dimensions(model) || actual == 0 {+ configured+ } else {+ actual+ }+}++/// Returns the current embedding settings plus the provider catalog.+pub async fn get_settings(config: &Config) -> Result<RpcOutcome<serde_json::Value>, String> {+ let provider = &config.memory.embedding_provider;+ let model = &config.memory.embedding_model;+ let dimensions = config.memory.embedding_dimensions;+ let rate_limit = config.memory.embedding_rate_limit_per_min;++ let auth = AuthService::from_config(config);+ let providers: Vec<serde_json::Value> = catalog::all_providers()+ .iter()+ .map(|entry| {+ let has_key = if entry.requires_api_key {+ let cred_provider = format!("embeddings:{}", entry.slug);+ auth.get_provider_bearer_token(&cred_provider, None)+ .ok()+ .flatten()+ .is_some()+ } else {+ false+ };+ serde_json::json!({+ "slug": entry.slug,+ "label": entry.label,+ "description": entry.description,+ "requires_api_key": entry.requires_api_key,+ "requires_endpoint": entry.requires_endpoint,+ "has_api_key": has_key,+ "models": entry.models,+ })+ })+ .collect();++ let vector_search_enabled = {+ let slug = if provider.starts_with("custom:") {+ "custom"+ } else {+ provider.as_str()+ };+ slug != "none"+ };++ // The embedder ingestion will *actually* use. `provider` above is the+ // per-section setting the picker writes; it is NOT authoritative for how+ // embeddings are funded, because the Local AI "Memory embeddings" toggle and+ // the `memory_tree.embedding_endpoint` override both route to local Ollama+ // without rewriting it. Additive field — callers that only need the picker+ // value are unaffected; callers asking "does this bill the managed budget?"+ // must read this one (#5402).+ let effective_provider = effective_embedder_slug_from_config(config);++ let payload = serde_json::json!({+ "provider": provider,+ "effective_provider": effective_provider,+ "model": model,+ "dimensions": dimensions,+ "rate_limit_per_min": rate_limit,+ "providers": providers,+ "vector_search_enabled": vector_search_enabled,+ });++ tracing::debug!(+ provider = provider.as_str(),+ effective_provider,+ model = model.as_str(),+ dimensions,+ vector_search_enabled,+ "{LOG_PREFIX} get_settings"+ );++ Ok(RpcOutcome::new(+ payload,+ vec!["embeddings settings loaded".into()],+ ))+}++/// Updates embedding provider/model/dimensions. If the embedding signature+/// changes, requires `confirm_wipe = true` and wipes memory.+pub async fn update_settings(+ provider: Option<String>,+ model: Option<String>,+ dimensions: Option<usize>,+ custom_endpoint: Option<String>,+ rate_limit_per_min: Option<u32>,+ confirm_wipe: bool,+) -> Result<RpcOutcome<serde_json::Value>, String> {+ use crate::openhuman::config::ops as config_rpc;+ use crate::openhuman::inference::embeddings::format_embedding_signature;++ let mut config = config_rpc::load_config_with_timeout().await?;++ let old_sig = format_embedding_signature(+ &config.memory.embedding_provider,+ &config.memory.embedding_model,+ config.memory.embedding_dimensions,+ );++ let new_provider = provider+ .clone()+ .unwrap_or_else(|| config.memory.embedding_provider.clone());+ let new_model = model+ .clone()+ .unwrap_or_else(|| config.memory.embedding_model.clone());+ // `new_dims`/`new_sig`/`dims_changed` are recomputed after the Custom+ // verification probe auto-detects the endpoint's real vector length+ // (issue #4056), so they must be mutable.+ let mut new_dims = dimensions.unwrap_or(config.memory.embedding_dimensions);+ let mut new_sig = format_embedding_signature(&new_provider, &new_model, new_dims);++ let old_dims = config.memory.embedding_dimensions;+ let mut dims_changed = new_dims != old_dims;+ let mut sig_changed = new_sig != old_sig;++ // Setup-time verification gate (TAURI-RUST-5JR / 4P4): a Custom+ // (OpenAI-compatible) embeddings endpoint — e.g. LM Studio — must prove it+ // can actually embed *before* we accept it. We run one live test embed and+ // only persist the config if it succeeds; any failure (no `/embeddings`+ // route, no model loaded, timeout, 5xx, empty/zero-dim vector) rejects the+ // save so a config that can't embed is never stored (and we never wipe+ // memory for one). Verifying at setup is the fix — we deliberately do NOT+ // try to classify-and-suppress the resulting embed flood in code; any+ // residual flood (e.g. the user unloads the model *after* a good save) is+ // handled on the Sentry side.+ //+ // Only custom endpoints are probed: named catalog providers are+ // embedding-capable by construction, and probing `managed`/`cloud`+ // pre-login would false-fail. Resolve the provider string exactly as it+ // will be stored so the probe targets the real endpoint.+ let effective_provider = match &custom_endpoint {+ Some(ep) if new_provider == "custom" || new_provider.starts_with("custom:") => {+ format!("custom:{ep}")+ }+ _ => new_provider.clone(),+ };+ if effective_provider.starts_with("custom:") {+ // Probe dimension-agnostically for non-`text-embedding-3-*` models so the+ // user's guessed `dimensions` can't fail an otherwise-valid endpoint; the+ // real length is detected from the returned vector below (issue #4056).- let probe_dims = probe_dims_for(&new_model, new_dims);- match build_embedder(&config, &effective_provider, &new_model, probe_dims) {- Ok(embedder) => {++ let endpoint = effective_provider++ .strip_prefix("custom:")++ .expect("custom provider was checked above");++ let api_key = resolve_api_key(&config, "custom");++ let probe = tokio::time::timeout(++ std::time::Duration::from_secs(10),++ probe_custom_embeddings(endpoint, &api_key, &new_model),++ )++ .await;++ {+ // Time-box the probe so a black-hole host can't hang the RPC.+ tracing::debug!(+ provider = effective_provider.as_str(),- probe_dims,+ "{LOG_PREFIX} update_settings verifying embeddings endpoint with a test embed"+ );- let probe = tokio::time::timeout(- std::time::Duration::from_secs(10),- embedder.embed(&["connection test"]),- )- .await;+ // Normalize the timeout/result into one shape, then apply the+ // pure verification policy (`classify_embed_probe`, unit-tested).+ let outcome = match probe {+ Ok(Ok(vectors)) => EmbedProbe::Returned(vectors),+ Ok(Err(e)) => EmbedProbe::Failed(e.to_string()),+ Err(_elapsed) => EmbedProbe::TimedOut,+ };+ // Peek the actual vector length before the policy consumes the+ // outcome — on a pass this is the endpoint's real dimension.+ let probe_actual_dims = match &outcome {+ EmbedProbe::Returned(vectors) => vectors.first().map(|v| v.len()).unwrap_or(0),+ _ => 0,+ };+ if let Some(reject) = classify_embed_probe(outcome) {+ // Log the classified error code (never the raw detail — it can+ // carry endpoint response bodies) so support can distinguish+ // auth vs wrong-model vs unreachable failures (issue #5017).+ let reject_code = reject+ .value+ .get("error")+ .and_then(|v| v.as_str())+ .unwrap_or("EMBEDDINGS_VERIFICATION_FAILED");+ tracing::warn!(+ provider = effective_provider.as_str(),+ reject_code,+ "{LOG_PREFIX} update_settings rejected — embeddings endpoint failed verification"+ );+ // Right-feedback (issue #3761): the probe failed. If the+ // endpoint lists its served models and the requested id+ // isn't among them, the cause is almost certainly a name+ // mismatch (e.g. the user entered `bge-m3` but LM Studio+ // serves `text-embedding-bge-m3`). Replace the generic+ // failure with an actionable message naming the available+ // models and the suggested match. Best-effort and only on+ // the failure path, so a passing config is never blocked by+ // an endpoint that doesn't expose `/models`. Derive the+ // endpoint from the payload OR the already-stored+ // `custom:<url>` provider, so a model-only update to an+ // existing custom endpoint still gets the guidance.+ let listed_endpoint = custom_endpoint+ .as_deref()+ .or_else(|| effective_provider.strip_prefix("custom:"));+ if let Some(ep) = listed_endpoint {+ let api_key = resolve_api_key(&config, "custom");+ tracing::debug!(+ provider = effective_provider.as_str(),+ requested = new_model.as_str(),+ "{LOG_PREFIX} update_settings: probing endpoint /models for served-id guidance"+ );+ match fetch_served_model_ids(ep, &api_key).await {+ Ok(served) => match check_requested_model_served(&new_model, &served) {+ Some(better) => {+ tracing::warn!(+ provider = effective_provider.as_str(),+ requested = new_model.as_str(),+ served = served.len(),+ "{LOG_PREFIX} update_settings: model not in served list — returning name-mismatch guidance"+ );+ return Ok(better);+ }+ None => {+ tracing::debug!(+ provider = effective_provider.as_str(),+ served = served.len(),+ "{LOG_PREFIX} update_settings: requested model is served (or list empty) — keeping generic verification error"+ );+ }+ },+ Err(e) => {+ tracing::debug!(+ provider = effective_provider.as_str(),+ error = %e,+ "{LOG_PREFIX} update_settings: /models lookup failed — keeping generic verification error"+ );+ }+ }+ }+ return Ok(reject);+ }+ // Passed. Adopt the endpoint's real vector length for every model+ // we probed dimension-agnostically — the user can't be expected to+ // know it, and storing the actual size is what keeps the live embed+ // path's length guard from rejecting future embeds (issue #4056).+ // `text-embedding-3-*` keeps the requested size (server honoured it).+ let detected_dims = final_probe_dims(&new_model, new_dims, probe_actual_dims);+ if detected_dims != new_dims {+ tracing::info!(+ provider = effective_provider.as_str(),+ model = new_model.as_str(),+ requested = new_dims,+ detected = detected_dims,+ "{LOG_PREFIX} update_settings auto-detected custom embedding dimension from probe"+ );+ new_dims = detected_dims;+ new_sig = format_embedding_signature(&new_provider, &new_model, new_dims);+ dims_changed = new_dims != old_dims;+ sig_changed = new_sig != old_sig;+ }+ tracing::debug!(+ provider = effective_provider.as_str(),+ new_dims,+ "{LOG_PREFIX} update_settings test embed passed — accepting config"+ );+ }- Err(e) => {- // Construction failure (unknown slug / bad config) — surface it- // rather than persisting a config that can never embed.- return Err(format!("invalid embedding provider configuration: {e}"));- }- }+ }++ // Only require a wipe when dimensions actually change — switching+ // provider/model at the same dimensionality keeps vectors comparable.+ if dims_changed && !confirm_wipe {+ let payload = serde_json::json!({+ "error": "EMBEDDINGS_DIMENSION_CHANGE_REQUIRES_WIPE",+ "old_dimensions": old_dims,+ "new_dimensions": new_dims,+ "old_signature": old_sig,+ "new_signature": new_sig,+ "message": "Changing embedding dimensions invalidates all stored vectors. \+ Pass confirm_wipe=true to wipe memory and apply.",+ });+ return Ok(RpcOutcome::new(+ payload,+ vec!["embedding dimension change requires wipe confirmation".into()],+ ));+ }++ if dims_changed {+ tracing::warn!(+ old_dims,+ new_dims,+ "{LOG_PREFIX} embedding dimensions changing — wiping memory"+ );+ crate::openhuman::memory::read_rpc::wipe_all_rpc(&config)+ .await+ .map_err(|e| format!("memory wipe failed: {e}"))?;+ }++ // Apply provider+ if let Some(p) = &provider {+ config.memory.embedding_provider = p.clone();+ // Also update the workload routing to keep them in sync+ config.embeddings_provider = Some(match p.as_str() {+ "managed" | "cloud" => "openhuman".to_string(),+ "ollama" => format!("ollama:{new_model}"),+ other => other.to_string(),+ });+ }+ if let Some(m) = &model {+ config.memory.embedding_model = m.clone();+ }+ // Persist `new_dims`, not the raw `dimensions` arg: the Custom verification+ // probe may have auto-detected the endpoint's real length (issue #4056), and+ // `new_dims` already defaults to the stored value when neither a new arg nor+ // detection changed it — so this is a no-op for the unchanged case.+ config.memory.embedding_dimensions = new_dims;+ if let Some(rl) = rate_limit_per_min {+ config.memory.embedding_rate_limit_per_min = rl;+ }+ // Store custom endpoint in a convention field if provided+ if let Some(ep) = &custom_endpoint {+ if new_provider == "custom" || new_provider.starts_with("custom:") {+ config.memory.embedding_provider = format!("custom:{ep}");+ }+ }++ config.save().await.map_err(|e| e.to_string())?;++ if sig_changed {+ crate::openhuman::memory::ops::maintenance::reembed_best_effort(+ &config,+ "embedding settings",+ )+ .await;+ }++ // #5324: this is the exact screen the "embedding budget reached" alert+ // deep-links to, so a provider/endpoint save here is the user completing+ // the remediation. Un-park the jobs that failed under the old+ // (budget-exhausted / misconfigured) provider so memory resumes growing+ // without the user also having to find "Retry failed" in Memory Tree+ // settings.+ //+ // Gated on an actual provider/endpoint/signature touch — NOT unconditional:+ // a save that only nudges `rate_limit_per_min` does not remediate the+ // embedder, so it must leave terminally-failed jobs parked. `provider`+ // covers re-selecting the *same* provider after fixing the account behind+ // it (a legitimate remediation even when the signature is unchanged).+ let is_embedding_remediation = sig_changed || provider.is_some() || custom_endpoint.is_some();+ // #5324: the settings save has already succeeded. A failed un-park must not+ // fail the RPC, but it must be surfaced (not reported as `0`) so a queue+ // that stayed parked isn't presented as remediated.+ let requeue_result = if is_embedding_remediation {+ crate::openhuman::memory::ops::maintenance::retry_failed(&config).await+ } else {+ Ok(0)+ };+ let requeued_count = *requeue_result.as_ref().unwrap_or(&0);+ let requeue_error = requeue_result.as_ref().err().cloned();+ let requeued_note = match &requeue_error {+ None => requeued_count.to_string(),+ Some(e) => format!("error ({e})"),+ };++ tracing::info!(+ provider = config.memory.embedding_provider.as_str(),+ model = config.memory.embedding_model.as_str(),+ dimensions = config.memory.embedding_dimensions,+ sig_changed,+ requeued = requeued_count,+ requeue_error = requeue_error.as_deref().unwrap_or(""),+ "{LOG_PREFIX} update_settings applied"+ );++ let payload = serde_json::json!({+ "provider": config.memory.embedding_provider,+ "model": config.memory.embedding_model,+ "dimensions": config.memory.embedding_dimensions,+ "signature_changed": sig_changed,+ "new_signature": new_sig,+ "requeued_failed_jobs": requeued_count,+ "requeue_error": requeue_error,+ });++ Ok(RpcOutcome::new(+ payload,+ vec![format!(+ "embeddings settings updated (sig_changed={sig_changed} requeued_failed={requeued_note})"+ )],+ ))+}++/// Stores an API key for a specific embedding provider.+pub async fn set_api_key(+ config: &Config,+ provider_slug: &str,+ api_key: &str,+) -> Result<RpcOutcome<serde_json::Value>, String> {+ if provider_slug.is_empty() {+ return Err("provider slug is required".into());+ }+ if api_key.trim().is_empty() {+ return Err("api_key cannot be empty".into());+ }++ let cred_provider = format!("embeddings:{provider_slug}");+ let auth = AuthService::from_config(config);+ auth.store_provider_token(&cred_provider, "default", api_key, HashMap::new(), true)+ .map_err(|e| format!("failed to store embedding API key: {e}"))?;++ // #5324: supplying a BYO key does NOT change the embedding signature, so+ // `ensure_reembed_backfill` has nothing to enqueue — but it is precisely+ // the action that unblocks jobs parked on `budget_exhausted` /+ // `auth_missing`. Requeue them here or they stay dead until the user+ // separately discovers the "Retry failed" button. A store failure is+ // surfaced (not reported as `0`) so the key-stored response can't imply the+ // parked queue was recovered when it wasn't.+ let requeue_result = crate::openhuman::memory::ops::maintenance::retry_failed(config).await;+ let requeued_count = *requeue_result.as_ref().unwrap_or(&0);+ let requeue_error = requeue_result.as_ref().err().cloned();+ let requeued_note = match &requeue_error {+ None => requeued_count.to_string(),+ Some(e) => format!("error ({e})"),+ };++ tracing::info!(+ provider = provider_slug,+ requeued = requeued_count,+ requeue_error = requeue_error.as_deref().unwrap_or(""),+ "{LOG_PREFIX} set_api_key stored"+ );++ Ok(RpcOutcome::new(+ serde_json::json!({ "stored": true, "provider": provider_slug, "requeued_failed_jobs": requeued_count, "requeue_error": requeue_error }),+ vec![format!(+ "embedding API key stored for {provider_slug} (requeued_failed={requeued_note})"+ )],+ ))+}++/// Removes the API key for a specific embedding provider.+pub async fn clear_api_key(+ config: &Config,+ provider_slug: &str,+) -> Result<RpcOutcome<serde_json::Value>, String> {+ if provider_slug.is_empty() {+ return Err("provider slug is required".into());+ }++ let cred_provider = format!("embeddings:{provider_slug}");+ let auth = AuthService::from_config(config);+ let removed = auth+ .remove_profile(&cred_provider, "default")+ .map_err(|e| format!("failed to clear embedding API key: {e}"))?;++ tracing::info!(+ provider = provider_slug,+ removed,+ "{LOG_PREFIX} clear_api_key"+ );++ Ok(RpcOutcome::new(+ serde_json::json!({ "cleared": removed, "provider": provider_slug }),+ vec![format!("embedding API key cleared for {provider_slug}")],+ ))+}++/// Generates embeddings for the given input texts using the currently+/// configured provider.+pub async fn embed(+ config: &Config,+ inputs: &[String],+) -> Result<RpcOutcome<serde_json::Value>, String> {+ let provider_name = &config.memory.embedding_provider;+ let model = &config.memory.embedding_model;+ let dims = config.memory.embedding_dimensions;++ let api_key = resolve_api_key(config, provider_name);++ let custom_endpoint = if provider_name.starts_with("custom:") {+ provider_name+ .strip_prefix("custom:")+ .map(|s: &str| s.to_string())+ } else {+ None+ };++ let provider_slug = if provider_name.starts_with("custom:") {+ "custom"+ } else {+ provider_name.as_str()+ };++ let embedder = create_embedding_provider_with_config(+ config,+ provider_slug,+ model,+ dims,+ &api_key,+ custom_endpoint.as_deref(),+ )+ .map_err(|e| e.to_string())?;++ let refs: Vec<&str> = inputs.iter().map(|s| s.as_str()).collect();+ let vectors = embedder.embed(&refs).await.map_err(|e| e.to_string())?;++ let actual_dims = vectors.first().map(|v| v.len()).unwrap_or(0);++ tracing::debug!(+ provider = provider_slug,+ model,+ input_count = inputs.len(),+ vector_count = vectors.len(),+ dims = actual_dims,+ "{LOG_PREFIX} embed completed"+ );++ let payload = serde_json::json!({+ "vectors": vectors,+ "dimensions": actual_dims,+ "count": vectors.len(),+ "provider": provider_slug,+ "model": model,+ });++ Ok(RpcOutcome::new(payload, vec!["embedding completed".into()]))+}diff --cc src/openhuman/inference/embeddings/rpc_part_02.rsindex ed965a7abc,0000000000..500d74219amode 100644,000000..100644--- a/src/openhuman/inference/embeddings/rpc_part_02.rs+++ b/src/openhuman/inference/embeddings/rpc_part_02.rs@@@ -1,474 -1,0 +1,470 @@@++/// Tests connectivity to the configured (or specified) embedding provider.+pub async fn test_connection(+ config: &Config,+ provider_slug: Option<&str>,+ model: Option<&str>,+ dims: Option<usize>,+) -> Result<RpcOutcome<serde_json::Value>, String> {+ let slug = provider_slug.unwrap_or(&config.memory.embedding_provider);+ let model = model.unwrap_or(&config.memory.embedding_model);+ let dims = dims.unwrap_or(config.memory.embedding_dimensions);++ let api_key = resolve_api_key(config, slug);++ let custom_endpoint = if slug.starts_with("custom:") {+ slug.strip_prefix("custom:").map(|s| s.to_string())+ } else {+ None+ };++ let provider_tag = if slug.starts_with("custom:") {+ "custom"+ } else {+ slug+ };+- // Probe a Custom endpoint dimension-agnostically (issue #4056): the user's- // `dims` is a guess, so enforcing it here would make a valid endpoint fail- // the Test-connection button whenever the guess differs from the native- // size. Catalog providers keep their fixed `dims`. We still report the- // requested vs actual dimensions in the payload below.- let probe_dims = if provider_tag == "custom" {- probe_dims_for(model, dims)- } else {- dims- };-- let embedder = create_embedding_provider_with_config(- config,- provider_tag,- model,- probe_dims,- &api_key,- custom_endpoint.as_deref(),- )- .map_err(|e| e.to_string())?;-+ tracing::debug!(+ provider = provider_tag,+ model,+ dims,- probe_dims,+ "{LOG_PREFIX} test_connection starting"+ );+- match embedder.embed(&["connection test"]).await {++ let result = if let Some(endpoint) = custom_endpoint.as_deref() {++ probe_custom_embeddings(endpoint, &api_key, model).await++ } else {++ let embedder = create_embedding_provider_with_config(++ config,++ provider_tag,++ model,++ dims,++ &api_key,++ None,++ )++ .map_err(|e| e.to_string())?;++ embedder++ .embed(&["connection test"])++ .await++ .map_err(|e| e.to_string())++ };++++ match result {+ Ok(vectors) => {+ let actual_dims = vectors.first().map(|v| v.len()).unwrap_or(0);+ let payload = serde_json::json!({+ "success": true,+ "provider": provider_tag,+ "model": model,+ "requested_dimensions": dims,+ "actual_dimensions": actual_dims,+ });+ Ok(RpcOutcome::new(+ payload,+ vec!["connection test passed".into()],+ ))+ }+ Err(e) => {+ let payload = serde_json::json!({+ "success": false,+ "provider": provider_tag,+ "model": model,+ "error": e.to_string(),+ });+ Ok(RpcOutcome::new(+ payload,+ vec![format!("connection test failed: {e}")],+ ))+ }+ }+}++/// Build an embedding provider from the live config — the same construction+/// [`embed`] uses, exposed so other domains (e.g. `codegraph`) can obtain a+/// provider for `signature()` + direct embedding without a JSON-RPC round-trip.+pub fn provider_from_config(config: &Config) -> anyhow::Result<Box<dyn super::EmbeddingProvider>> {+ build_embedder(+ config,+ &config.memory.embedding_provider,+ &config.memory.embedding_model,+ config.memory.embedding_dimensions,+ )+}++/// Construct an embedding provider for an explicit `(provider_name, model,+/// dims)` triple, resolving the stored API key + inline `custom:<url>` endpoint+/// the same way [`embed`] / [`test_connection`] do. Single construction seam so+/// the save-time probe in [`update_settings`] and the live embed path can't+/// drift on slug-normalization / credential-lookup rules.+fn build_embedder(+ config: &Config,+ provider_name: &str,+ model: &str,+ dims: usize,+) -> anyhow::Result<Box<dyn super::EmbeddingProvider>> {+ let api_key = resolve_api_key(config, provider_name);+ let custom_endpoint = provider_name.strip_prefix("custom:").map(|s| s.to_string());+ let provider_slug = if provider_name.starts_with("custom:") {+ "custom"+ } else {+ provider_name+ };+ create_embedding_provider_with_config(+ config,+ provider_slug,+ model,+ dims,+ &api_key,+ custom_endpoint.as_deref(),+ )+}++/// Normalized result of the setup-time test embed in [`update_settings`].+/// Collapses the `Result<Result<_, _>, Elapsed>` timeout shape into one enum so+/// the verification policy can be expressed (and unit-tested) as a pure+/// function over it.+enum EmbedProbe {+ /// The endpoint returned vectors (may still be empty/zero-dim — checked).+ Returned(Vec<Vec<f32>>),+ /// The embed call returned an error; the string is the provider detail.+ Failed(String),+ /// The probe didn't complete within the time box.+ TimedOut,+}++/// Setup-time embeddings verification policy. Returns `None` when the endpoint+/// is verified (accept + persist the config) or `Some(reject)` — the+/// "not saved" RPC payload — otherwise.+///+/// The endpoint must prove it can embed before we accept it: only a non-empty+/// vector passes; every failure mode (no model loaded, no `/embeddings` route,+/// 5xx/auth/network, timeout, empty vector) rejects the save. We do NOT try to+/// classify-and-suppress the resulting embed flood in code — residual floods+/// (e.g. the user unloads the model after a good save) are handled Sentry-side.+/// The known shapes only get a friendlier remediation message.+fn classify_embed_probe(outcome: EmbedProbe) -> Option<RpcOutcome<serde_json::Value>> {+ let reject = |error: &str, message: &str, summary: &str, detail: Option<&str>| {+ let mut body = serde_json::json!({ "error": error, "message": message });+ if let Some(d) = detail {+ // The probe detail is the raw endpoint response body. It can carry the+ // API key (OpenAI's 401 echoes `Incorrect API key provided: sk-…`), and+ // the frontend appends `detail` to the surfaced message — so redact any+ // key/bearer material before it ever leaves the core, for both the UI+ // and logs (#5116). The clean classified `message` is the primary text;+ // the sanitized detail only adds a self-diagnosis hint.+ body["detail"] = serde_json::Value::String(redact_secrets(d));+ }+ Some(RpcOutcome::new(body, vec![summary.to_string()]))+ };++ match outcome {+ // Pass only when the endpoint returns a usable vector.+ EmbedProbe::Returned(vectors)+ if vectors.first().map(|v| !v.is_empty()).unwrap_or(false) =>+ {+ None+ }+ // Reachable but produced no usable vector — not a valid embedder.+ EmbedProbe::Returned(_) => reject(+ "EMBEDDINGS_VERIFICATION_FAILED",+ "The embeddings endpoint responded but returned no vector. Choose an \+ embeddings-capable provider or endpoint, then save again.",+ "test embed returned no vectors — not saved",+ None,+ ),+ EmbedProbe::Failed(detail) => {+ let lower = detail.to_ascii_lowercase();+ // The endpoint IS reachable and correctly shaped (POST /v1/embeddings+ // with the user's model + key — verified conformant by the mock-endpoint+ // regression test). The failures below are all *distinct causes*; issue+ // #5017 was that they collapsed into one generic "test embed failed"+ // message, so a user whose endpoint works for chat couldn't tell that+ // (e.g.) their chosen model isn't an embeddings model, their key was+ // rejected, or the host was unreachable. Order matters: check the+ // specific shapes before the generic fallback.+ if lower.contains("no models loaded") {+ // Reachable but no model loaded (e.g. LM Studio idle).+ reject(+ "EMBEDDINGS_NO_MODEL_LOADED",+ "Your local embeddings server (e.g. LM Studio) is running but has no \+ model loaded. Load an embedding model — in LM Studio use the developer \+ page or the `lms load` command — then save again.",+ "embeddings server has no model loaded — not saved",+ Some(&detail),+ )+ } else if crate::core::observability::is_embedding_endpoint_absent(&lower) {+ // Endpoint exposes no embeddings API (404/405).+ reject(+ "EMBEDDINGS_ENDPOINT_NO_API",+ "This endpoint has no embeddings API. Choose an embeddings-capable \+ provider (Managed, Voyage, OpenAI, Cohere, Ollama) or a different \+ custom endpoint.",+ "embeddings endpoint has no embeddings API — not saved",+ Some(&detail),+ )+ } else if is_embedding_dimension_mismatch(&lower) {+ // Endpoint embedded fine but returned a different vector length than+ // the (Matryoshka) size we requested — a `text-embedding-3-*` model+ // name pointed at a host that ignores the `dimensions` param.+ reject(+ "EMBEDDINGS_DIMENSION_MISMATCH",+ "The endpoint returned a vector with a different length than the \+ dimensions you entered. Set dimensions to match the model's native \+ output, then save again.",+ "embeddings endpoint returned mismatched dimensions — not saved",+ Some(&detail),+ )+ } else if is_embedding_model_incompatible(&lower) {+ // Reachable, authenticated embeddings API that rejected the model —+ // the user pasted a chat/reasoning model (e.g. `gpt-5-mini`) into the+ // embeddings model field. This is the #5017 reporter's exact case:+ // the same model works for chat but is not an embeddings model.+ reject(+ "EMBEDDINGS_MODEL_INCOMPATIBLE",+ "That model isn't an embeddings model on this endpoint. A chat model \+ (the one that works in Chat settings) can't produce embeddings — \+ enter an embeddings model id (e.g. text-embedding-3-small, bge-m3), \+ then save again.",+ "embeddings model is not an embeddings model — not saved",+ Some(&detail),+ )+ } else if embed_error_mentions_status(&lower, 401)+ || embed_error_mentions_status(&lower, 403)+ {+ // Auth failure — key missing/wrong/lacking embeddings scope. The+ // embeddings key is stored separately from the chat BYOK key, so+ // "works for chat" does not imply the embeddings key is set.+ reject(+ "EMBEDDINGS_AUTH_FAILED",+ "The endpoint rejected the API key (401/403). Enter a valid key for \+ this endpoint — note the embeddings key is stored separately from the \+ Chat provider key — then save again.",+ "embeddings endpoint rejected the API key — not saved",+ Some(&detail),+ )+ } else if is_embedding_endpoint_unreachable(&lower) {+ // Transport-level failure — DNS, refused connection, TLS. The base+ // URL is wrong or the host is down.+ reject(+ "EMBEDDINGS_ENDPOINT_UNREACHABLE",+ "Couldn't reach the embeddings endpoint (network/DNS/connection \+ error). Check the base URL and that the host is reachable, then save \+ again.",+ "embeddings endpoint unreachable — not saved",+ Some(&detail),+ )+ } else {+ // Any other failure (5xx, unclassified) — didn't pass verification.+ reject(+ "EMBEDDINGS_VERIFICATION_FAILED",+ "Couldn't verify the embeddings endpoint — the test embed failed. Make \+ sure the endpoint is reachable and serving an embedding model, then \+ save again.",+ "embeddings endpoint failed verification — not saved",+ Some(&detail),+ )+ }+ }+ EmbedProbe::TimedOut => reject(+ "EMBEDDINGS_ENDPOINT_UNREACHABLE",+ "Couldn't verify the embeddings endpoint — the test embed timed out. Make sure \+ the endpoint is running and reachable, then save again.",+ "embeddings endpoint timed out during verification — not saved",+ None,+ ),+ }+}++/// Whether a lowercased embed-error detail names the given HTTP status, tolerant+/// of the wire shapes the embeddings stack emits:+/// `openai embeddings returned HTTP 401 Unauthorized: …` (tinyagents adapter)+/// `Embedding API error (401 Unauthorized): …` (parenthesized host shape)+/// `Embedding API error 401 Unauthorized: …` (bare-status host shape)+/// The bare-status `Embedding API error {code}` form is the one the observability+/// classifier in `src/core/observability.rs` covers; without it, setup-time+/// verification for those hosts fell through to the generic failure code (#5017).+fn embed_error_mentions_status(lower: &str, code: u16) -> bool {+ let code = code.to_string();+ lower.contains(&format!("http {code}"))+ || lower.contains(&format!("({code}"))+ || lower.contains(&format!("embedding api error {code}"))+}++/// A reachable, authenticated embeddings API that **rejected the model id** — the+/// user pointed the embeddings model field at a chat/reasoning model.+///+/// Two tiers of phrasing:+///+/// - **Strong, status-independent phrasings** unambiguously name a model that+/// can't embed. OpenAI returns *HTTP 403* "You are not allowed to generate+/// embeddings from this model" when a chat model (e.g. `gpt-4o-mini`) is used+/// as the embeddings model — a MODEL problem, not an auth problem. Because+/// `classify_embed_probe` checks this **before** the 401/403 auth branch, that+/// 403 must be caught here or it falls through and misreports "enter a valid+/// key" (issue #5116). None of these phrases appear in a genuine auth rejection+/// (`Incorrect API key provided …`), so matching them ahead of auth is safe.+/// - **Weak phrasings** (a stray "does not exist" / odd model-name format) are+/// only unambiguous alongside a 400/422 bad-request, so a genuine 5xx or an+/// oversized-input 400 still falls through to the generic failure (issue #5017).+fn is_embedding_model_incompatible(lower: &str) -> bool {+ let strong_model_rejection = lower.contains("not allowed to generate embeddings")+ || lower.contains("does not support embeddings")+ || lower.contains("not an embedding model")+ || lower.contains("is not an embedding")+ || lower.contains("not supported for embeddings")+ || (lower.contains("unsupported") && lower.contains("embedding"));+ if strong_model_rejection {+ return true;+ }+ let bad_request =+ embed_error_mentions_status(lower, 400) || embed_error_mentions_status(lower, 422);+ bad_request+ && (lower.contains("does not exist") || lower.contains("unexpected model name format"))+}++/// Strip API-key / bearer-token material from any text before it reaches the UI+/// or logs. Matches OpenAI-style keys (`sk-…`, including the modern `sk-proj-…`+/// form with embedded hyphens/underscores) and `Bearer <token>` headers, and+/// replaces each **whole** match — the replacements deliberately contain no `sk-`+/// substring, so not even a key *prefix* can surface (#5116).+fn redact_secrets(input: &str) -> String {+ use once_cell::sync::Lazy;+ use regex::Regex;+ static SK_KEY_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(?i)\bsk-[A-Za-z0-9_-]+").unwrap());+ static BEARER_RE: Lazy<Regex> =+ Lazy::new(|| Regex::new(r"(?i)\bBearer\s+[A-Za-z0-9._~+/=-]+").unwrap());+ let redacted = SK_KEY_RE.replace_all(input, "[redacted-key]");+ BEARER_RE+ .replace_all(&redacted, "Bearer [redacted]")+ .into_owned()+}++/// The post-response length guard fired: the endpoint embedded but returned a+/// vector whose length differs from the requested (Matryoshka) `dimensions`.+/// Canonical shape from the tinyagents adapter:+/// `openai embed dimension mismatch: expected 1024, got 3072`.+fn is_embedding_dimension_mismatch(lower: &str) -> bool {+ lower.contains("dimension mismatch")+}++/// A transport-level failure (DNS, refused connection, TLS, connect timeout) —+/// the endpoint was never reached, so the base URL is wrong or the host is down.+/// The tinyagents adapter wraps these as+/// `openai embeddings request to <url> failed: <reqwest error>`.+fn is_embedding_endpoint_unreachable(lower: &str) -> bool {+ lower.contains("request to") && lower.contains("failed")+ || lower.contains("connection refused")+ || lower.contains("error sending request")+ || lower.contains("error trying to connect")+ || lower.contains("dns error")+ || lower.contains("failed to lookup address")+ || lower.contains("tcp connect error")+}++/// GET `{endpoint}/models` (OpenAI-compatible) and return the served model ids.+/// Time-boxed and best-effort — any failure returns `Err` and the caller falls+/// back to the live test-embed probe (issue #3761).+async fn fetch_served_model_ids(endpoint: &str, api_key: &str) -> Result<Vec<String>, String> {+ #[derive(serde::Deserialize)]+ struct ModelEntry {+ id: String,+ }+ #[derive(serde::Deserialize)]+ struct ModelsResponse {+ #[serde(default)]+ data: Vec<ModelEntry>,+ }++ let url = format!("{}/models", endpoint.trim_end_matches('/'));+ let client = reqwest::Client::new();+ let mut req = client.get(&url).timeout(std::time::Duration::from_secs(5));+ if !api_key.trim().is_empty() {+ req = req.bearer_auth(api_key.trim());+ }+ let resp = req+ .send()+ .await+ .map_err(|e| format!("models request failed: {e}"))?;+ if !resp.status().is_success() {+ return Err(format!("models request returned status {}", resp.status()));+ }+ let parsed: ModelsResponse = resp+ .json()+ .await+ .map_err(|e| format!("models parse failed: {e}"))?;+ Ok(parsed.data.into_iter().map(|m| m.id).collect())+}++/// Normalize an embedding model id for tolerant *suggestion* matching:+/// lowercase, drop a leading `text-embedding-`, drop a trailing `:tag`. Used+/// only to suggest the right served name — never to silently rewrite the id.+fn normalize_embed_model_id(name: &str) -> String {+ let lower = name.trim().to_ascii_lowercase();+ let stripped = lower.strip_prefix("text-embedding-").unwrap_or(&lower);+ stripped.split(':').next().unwrap_or(stripped).to_string()+}++/// Decide whether the requested model is acceptable given the endpoint's served+/// list. Returns `Some(reject)` only when the endpoint reports a non-empty list+/// that does NOT contain the requested id — i.e. we have positive evidence the+/// model isn't loaded. An empty/unknown list returns `None` (defer to the live+/// test-embed probe) so we never block on a server that doesn't expose+/// `/models` (issue #3761).+fn check_requested_model_served(+ requested: &str,+ served: &[String],+) -> Option<RpcOutcome<serde_json::Value>> {+ if served.is_empty() || served.iter().any(|m| m == requested) {+ return None;+ }+ Some(reject_model_not_served(requested, served))+}++/// Build the "model not served" rejection: names what the endpoint actually+/// serves and, when a normalized match exists, suggests the exact name to pick+/// (e.g. `bge-m3` → `text-embedding-bge-m3`). Reuses the+/// `EMBEDDINGS_NO_MODEL_LOADED` error code so the existing Embeddings setup+/// dialog surfaces `message` and keeps the config unsaved (issue #3761).+fn reject_model_not_served(requested: &str, served: &[String]) -> RpcOutcome<serde_json::Value> {+ let want = normalize_embed_model_id(requested);+ let suggestion = served+ .iter()+ .find(|m| normalize_embed_model_id(m) == want)+ .cloned();+ let served_list = served.join(", ");+ let message = match suggestion.as_deref() {+ Some(s) => format!(+ "`{requested}` isn't loaded on this embeddings server — but the same model appears to be served as `{s}`. Select `{s}` (the exact name your server reports), then save again. Available models: {served_list}."+ ),+ None => format!(+ "`{requested}` isn't loaded on this embeddings server. Select one of the loaded models (the exact name your server reports), then save again. Available models: {served_list}."+ ),+ };+ let mut body = serde_json::json!({+ "error": "EMBEDDINGS_NO_MODEL_LOADED",+ "message": message,+ "requested_model": requested,+ "available_models": served,+ });+ if let Some(s) = suggestion {+ body["suggested_model"] = serde_json::Value::String(s);+ }+ RpcOutcome::new(+ body,+ vec!["embedding model not served by endpoint — not saved".to_string()],+ )+}++pub(crate) fn resolve_api_key(config: &Config, provider_name: &str) -> String {+ let slug = if provider_name.starts_with("custom:") {+ "custom"+ } else {+ provider_name+ };+ let cred_provider = format!("embeddings:{slug}");+ let auth = AuthService::from_config(config);+ auth.get_provider_bearer_token(&cred_provider, None)+ .ok()+ .flatten()+ .unwrap_or_default()+}diff --cc src/openhuman/inference/embeddings/rpc_tests.rsindex 959486fdc6,0000000000..b1e5ccd948mode 100644,000000..100644--- a/src/openhuman/inference/embeddings/rpc_tests.rs+++ b/src/openhuman/inference/embeddings/rpc_tests.rs@@@ -1,604 -1,0 +1,588 @@@+use super::*;+// Production code now routes managed construction through+// `create_embedding_provider_with_config`; this low-level custom-endpoint+// regression test still drives the credentialed factory directly.+use super::super::create_embedding_provider_with_credentials;+use tempfile::TempDir;++/// The seam the memory factory depends on (TAURI-RUST-52S fix): the three+/// `create_memory_with_local_ai` call sites resolve the user's stored BYO+/// embedding credential via `resolve_api_key` and thread it into the+/// provider. If this lookup silently returns "" for a configured key —+/// wrong cred slug, encryption mismatch, profile-store regression — the+/// memory pipeline reverts to sending an empty bearer and Cohere 401s on+/// every embed. Lock the round-trip: store under `embeddings:<slug>`, read+/// it back; an unrelated provider must stay empty (no cross-bleed).+#[test]+fn resolve_api_key_returns_stored_embeddings_credential() {+ let tmp = TempDir::new().unwrap();+ let mut config = Config::default();+ config.config_path = tmp.path().join("config.toml");++ // Nothing stored yet → empty (the empty-key guard's "" input).+ assert_eq!(resolve_api_key(&config, "cohere"), "");++ // Store a Cohere embeddings key exactly as `set_api_key` does.+ AuthService::from_config(&config)+ .store_provider_token(+ "embeddings:cohere",+ "default",+ "sk-cohere-test",+ HashMap::new(),+ true,+ )+ .unwrap();++ // Resolve returns it; a provider with no stored key stays empty.+ assert_eq!(resolve_api_key(&config, "cohere"), "sk-cohere-test");+ assert_eq!(resolve_api_key(&config, "voyage"), "");+}++/// `get_settings` must report the embedder ingestion will **actually** use+/// alongside the picker's own setting (#5402). The two disagree whenever+/// the user enabled local embeddings through Local AI Settings: that path+/// never rewrites `memory.embedding_provider`, so `provider` still reads+/// `"cloud"` while nothing bills the managed budget. A consumer that gated+/// a "your memory has stopped growing" banner on `provider` would fire it+/// at a user whose memory is growing fine.+#[tokio::test]+async fn get_settings_reports_effective_provider_separately_from_the_setting() {+ let tmp = TempDir::new().unwrap();+ let mut config = Config::default();+ config.config_path = tmp.path().join("config.toml");+ config.workspace_dir = tmp.path().to_path_buf();+ config.memory.embedding_provider = "cloud".to_string();+ // A managed session exists, so the ladder would resolve to cloud …+ std::fs::write(tmp.path().join("auth-profiles.json"), "{}").unwrap();+ // … except a local Ollama route wins. As of tinymemory v1.0.1 the+ // effective-embedder ladder no longer treats the `embeddings_provider`+ // string alone as authoritative for local routing — local Ollama is+ // resolved from an explicit `memory_tree.embedding_endpoint` override or+ // the unified `workload_local_model` setting. Drive the explicit+ // endpoint rung here: it resolves deterministically without an installed+ // embedding host, and still exercises the point of the test — that+ // `provider` (the picker) stays `cloud` while `effective_provider`+ // reports the local route that bills nothing (#5402).+ config.embeddings_provider = Some("ollama:all-minilm:latest".into());+ config.memory_tree.embedding_endpoint = Some("http://localhost:11434".into());+ config.memory_tree.embedding_model = Some("all-minilm".into());++ let out = get_settings(&config)+ .await+ .expect("get_settings must succeed");+ assert_eq!(+ out.value["provider"], "cloud",+ "the picker setting is unchanged"+ );+ assert_eq!(+ out.value["effective_provider"], "ollama",+ "the effective embedder is local, so nothing bills the managed budget"+ );+}++/// `custom:<url>` providers must look up under the `embeddings:custom`+/// slug (the inline URL is not part of the credential key), mirroring the+/// slug normalization in `embed`/`set_api_key`.+#[test]+fn resolve_api_key_normalizes_custom_prefix_to_custom_slug() {+ let tmp = TempDir::new().unwrap();+ let mut config = Config::default();+ config.config_path = tmp.path().join("config.toml");++ AuthService::from_config(&config)+ .store_provider_token(+ "embeddings:custom",+ "default",+ "sk-custom-test",+ HashMap::new(),+ true,+ )+ .unwrap();++ assert_eq!(+ resolve_api_key(&config, "custom:http://localhost:1234"),+ "sk-custom-test"+ );+}+- /// Issue #4056: a Custom endpoint is probed dimension-agnostically for any- /// model that doesn't honour the OpenAI `dimensions` request param, so the- /// user's guessed size can't fail an otherwise-valid endpoint. Only the- /// `text-embedding-3-*` family (which honours the param) is probed at the- /// requested size.- #[test]- fn probe_dims_for_zeroes_non_matryoshka_models() {- // text-embedding-3-* honours the param → probe at the requested size.- assert_eq!(probe_dims_for("text-embedding-3-large", 1024), 1024);- assert_eq!(probe_dims_for("text-embedding-3-small", 512), 512);- // Everything else → 0 (no param sent, no length guard).- assert_eq!(probe_dims_for("bge-m3", 1024), 0);- assert_eq!(probe_dims_for("nomic-embed-text", 768), 0);- assert_eq!(probe_dims_for("gpt-5-mini", 1024), 0);- }-+/// Issue #4056: after a successful probe we adopt the endpoint's real+/// returned length for auto-detected models, but keep the requested size for+/// `text-embedding-3-*` (the server returned exactly that). A zero actual+/// (defensive — empty vectors are already rejected upstream) falls back to+/// the configured value.+#[test]+fn final_probe_dims_adopts_actual_for_auto_detected_models() {+ // Auto-detected model → adopt the real length, ignoring the guess.+ assert_eq!(final_probe_dims("bge-m3", 1024, 1024), 1024);+ assert_eq!(final_probe_dims("bge-m3", 1024, 768), 768);+ assert_eq!(final_probe_dims("nomic-embed-text", 1024, 768), 768);+ // text-embedding-3-* → keep the requested size (param was honoured).+ assert_eq!(final_probe_dims("text-embedding-3-large", 1024, 3072), 1024);+ // Defensive: zero actual falls back to the configured value.+ assert_eq!(final_probe_dims("bge-m3", 1024, 0), 1024);+}++#[test]+fn normalize_embed_model_id_strips_prefix_and_tag() {+ assert_eq!(normalize_embed_model_id("text-embedding-bge-m3"), "bge-m3");+ assert_eq!(normalize_embed_model_id("bge-m3"), "bge-m3");+ assert_eq!(normalize_embed_model_id("bge-m3:latest"), "bge-m3");+ assert_eq!(normalize_embed_model_id("TEXT-EMBEDDING-BGE-M3"), "bge-m3");+ // Exact-after-strip: must not collapse a different model onto bge-m3.+ assert_ne!(normalize_embed_model_id("bge-m3-distill"), "bge-m3");+}++#[test]+fn reject_model_not_served_suggests_normalized_match() {+ // User entered `bge-m3`; LM Studio serves `text-embedding-bge-m3` —+ // the feedback names the exact served id to select (issue #3761).+ let served = vec!["text-embedding-bge-m3".to_string(), "qwen-chat".to_string()];+ let out = reject_model_not_served("bge-m3", &served);+ assert_eq!(out.value["error"], "EMBEDDINGS_NO_MODEL_LOADED");+ assert_eq!(out.value["suggested_model"], "text-embedding-bge-m3");+ let msg = out.value["message"].as_str().unwrap();+ assert!(msg.contains("text-embedding-bge-m3"));+}++#[test]+fn reject_model_not_served_without_match_lists_available() {+ let served = vec!["qwen-chat".to_string(), "llama-3".to_string()];+ let out = reject_model_not_served("bge-m3", &served);+ assert_eq!(out.value["error"], "EMBEDDINGS_NO_MODEL_LOADED");+ assert!(out.value.get("suggested_model").is_none());+ let msg = out.value["message"].as_str().unwrap();+ assert!(msg.contains("qwen-chat") && msg.contains("llama-3"));+}++#[test]+fn check_requested_model_served_decisions() {+ // Served exactly → accept (None).+ assert!(check_requested_model_served(+ "text-embedding-bge-m3",+ &["text-embedding-bge-m3".to_string()],+ )+ .is_none());+ // Empty/unknown list → defer to probe (None), never block.+ assert!(check_requested_model_served("bge-m3", &[]).is_none());+ // Non-empty list without the model → reject with feedback.+ let reject = check_requested_model_served("bge-m3", &["text-embedding-bge-m3".to_string()]);+ assert_eq!(reject.unwrap().value["error"], "EMBEDDINGS_NO_MODEL_LOADED");+}++#[tokio::test]+async fn fetch_served_model_ids_parses_openai_models_list() {+ use axum::{routing::get, Json, Router};+ let app = Router::new().route(+ "/v1/models",+ get(|| async {+ Json(serde_json::json!({+ "object": "list",+ "data": [+ { "id": "text-embedding-bge-m3", "object": "model" },+ { "id": "qwen-chat", "object": "model" }+ ]+ }))+ }),+ );+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let base = format!("http://127.0.0.1:{}", listener.local_addr().unwrap().port());+ tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });++ let ids = fetch_served_model_ids(&format!("{base}/v1"), "")+ .await+ .expect("models list");+ assert_eq!(ids, vec!["text-embedding-bge-m3", "qwen-chat"]);+}++/// Helper: pull the `error` code out of a reject payload.+fn reject_code(outcome: EmbedProbe) -> Option<String> {+ classify_embed_probe(outcome).map(|rpc| {+ rpc.value+ .get("error")+ .and_then(|v| v.as_str())+ .unwrap_or("")+ .to_string()+ })+}++/// A usable vector is the ONLY thing that passes the setup-time gate — the+/// config is then accepted and persisted.+#[test]+fn classify_embed_probe_accepts_only_usable_vector() {+ assert!(+ classify_embed_probe(EmbedProbe::Returned(vec![vec![0.1, 0.2, 0.3]])).is_none(),+ "a non-empty vector must verify the endpoint"+ );+}++/// Reachable but empty/zero-dim response is a failed verification, not a+/// valid embedder — never persist it.+#[test]+fn classify_embed_probe_rejects_empty_vectors() {+ assert_eq!(+ reject_code(EmbedProbe::Returned(vec![])).as_deref(),+ Some("EMBEDDINGS_VERIFICATION_FAILED")+ );+ assert_eq!(+ reject_code(EmbedProbe::Returned(vec![vec![]])).as_deref(),+ Some("EMBEDDINGS_VERIFICATION_FAILED")+ );+}++/// LM Studio idle ("No models loaded") must reject the save with the+/// one-step remediation code so the doomed config is never persisted — the+/// fix is verifying at setup, not suppressing the later flood.+#[test]+fn classify_embed_probe_rejects_no_model_loaded() {+ let body = r#"Embedding API error (400 Bad Request): {"error":"No models loaded. Please load a model in the developer page or use the 'lms load' command."}"#;+ let rpc = classify_embed_probe(EmbedProbe::Failed(body.to_string())).unwrap();+ assert_eq!(+ rpc.value.get("error").and_then(|v| v.as_str()),+ Some("EMBEDDINGS_NO_MODEL_LOADED")+ );+ // The raw provider detail is preserved for the UI.+ assert_eq!(rpc.value.get("detail").and_then(|v| v.as_str()), Some(body));+}++/// A 404/405 (no `/embeddings` route) keeps its dedicated code.+#[test]+fn classify_embed_probe_rejects_endpoint_absent() {+ for detail in [+ "Embedding API error (404 Not Found): no route",+ "openai embeddings returned HTTP 404 Not Found: no route",+ ] {+ assert_eq!(+ reject_code(EmbedProbe::Failed(detail.into())).as_deref(),+ Some("EMBEDDINGS_ENDPOINT_NO_API")+ );+ }+}++/// An unclassified 5xx still rejects with the generic code — it's a real+/// server fault, not one of the actionable user-config shapes.+#[test]+fn classify_embed_probe_rejects_unclassified_5xx_generically() {+ assert_eq!(+ reject_code(EmbedProbe::Failed(+ "openai embeddings returned HTTP 500 Internal Server Error: boom".into()+ ))+ .as_deref(),+ Some("EMBEDDINGS_VERIFICATION_FAILED")+ );+}++/// Issue #5017 — the #5017 reporter's exact case: a chat/reasoning model id+/// (`gpt-5-mini`) that works for chat is pasted into the embeddings model+/// field. The endpoint IS an embeddings API but rejects the model with a 400;+/// before the fix this collapsed into the generic "test embed failed", so the+/// user couldn't tell the model wasn't an embeddings model. Now it maps to a+/// dedicated, actionable code — across both wire shapes.+#[test]+fn classify_embed_probe_distinguishes_incompatible_model() {+ for detail in [+ r#"openai embeddings returned HTTP 400 Bad Request: {"error":{"message":"gpt-5-mini does not support embeddings"}}"#,+ r#"Embedding API error (400 Bad Request): {"error":{"message":"Model gpt-5-mini does not exist"}}"#,+ r#"openai embeddings returned HTTP 400 Bad Request: {"error":"this is not an embedding model"}"#,+ ] {+ assert_eq!(+ reject_code(EmbedProbe::Failed(detail.into())).as_deref(),+ Some("EMBEDDINGS_MODEL_INCOMPATIBLE"),+ "detail should classify as incompatible model: {detail}"+ );+ }+}++/// Issue #5017 — a rejected/absent API key (401/403) is its own actionable+/// cause: the embeddings key is stored separately from the Chat BYOK key, so+/// "works for chat" does not imply the embeddings key is set. Both wire+/// shapes map to the dedicated auth code, not the generic failure.+#[test]+fn classify_embed_probe_distinguishes_auth_failure() {+ for detail in [+ "openai embeddings returned HTTP 401 Unauthorized: {\"error\":\"invalid api key\"}",+ "Embedding API error (403 Forbidden): no access",+ // Bare-status host shape (no parentheses) — the form the observability+ // classifier covers; must map to auth, not the generic failure (#5017).+ "Embedding API error 401 Unauthorized: {\"error\":\"invalid token\"}",+ ] {+ assert_eq!(+ reject_code(EmbedProbe::Failed(detail.into())).as_deref(),+ Some("EMBEDDINGS_AUTH_FAILED"),+ "detail should classify as auth failure: {detail}"+ );+ }+}++/// Issue #5116 — a **chat** model used as an embeddings model. OpenAI answers+/// *HTTP 403* "You are not allowed to generate embeddings from this model".+/// Before the fix this fell through to the 401/403 auth branch and told the+/// user to "enter a valid key" even though the key was fine — the model is the+/// problem. It must classify as MODEL_INCOMPATIBLE, ahead of the auth branch.+#[test]+fn classify_embed_probe_403_not_an_embeddings_model_is_model_incompatible_not_auth() {+ for detail in [+ r#"openai embeddings returned HTTP 403 Forbidden: {"error":{"message":"You are not allowed to generate embeddings from this model","type":"invalid_request_error","param":null,"code":null}}"#,+ r#"Embedding API error (403 Forbidden): {"error":{"message":"This is not an embedding model"}}"#,+ r#"openai embeddings returned HTTP 403 Forbidden: {"error":{"message":"unsupported model for embedding"}}"#,+ ] {+ assert_eq!(+ reject_code(EmbedProbe::Failed(detail.into())).as_deref(),+ Some("EMBEDDINGS_MODEL_INCOMPATIBLE"),+ "403 model-rejection must be model-incompatible, not auth: {detail}"+ );+ }+}++/// Issue #5116 (security) — a genuine bad key (401 "Incorrect API key+/// provided: sk-…") must STILL classify as auth, but the surfaced payload must+/// never carry the key: neither the message nor the redacted detail may+/// contain an `sk-` substring.+#[test]+fn classify_embed_probe_401_bad_key_is_auth_and_redacts_key() {+ let detail = r#"openai embeddings returned HTTP 401 Unauthorized: {"error":{"message":"Incorrect API key provided: sk-proj-ABC123def456GHI789jkl012MNO. You can find your API key at https://platform.openai.com/account/api-keys.","type":"invalid_request_error","param":null,"code":"invalid_api_key"}}"#;+ let rpc = classify_embed_probe(EmbedProbe::Failed(detail.into()))+ .expect("bad key must reject the save");+ assert_eq!(+ rpc.value.get("error").and_then(|v| v.as_str()),+ Some("EMBEDDINGS_AUTH_FAILED"),+ "a genuine 401 bad key must stay classified as auth"+ );+ // Nothing in the surfaced payload may leak the key.+ let surfaced = serde_json::to_string(&rpc.value).unwrap();+ assert!(+ !surfaced.contains("sk-"),+ "surfaced payload must not contain any sk- key material: {surfaced}"+ );+ assert!(+ rpc.value+ .get("detail")+ .and_then(|v| v.as_str())+ .map(|d| d.contains("[redacted-key]"))+ .unwrap_or(false),+ "the detail should keep a redaction marker for support diagnosis"+ );+}++/// The redaction helper strips whole OpenAI-style keys (incl. the modern+/// `sk-proj-…` form) and bearer tokens, leaving no `sk-` prefix behind.+#[test]+fn redact_secrets_removes_key_and_bearer_material() {+ let redacted =+ redact_secrets("key sk-proj-ABC123_def-456 and Authorization: Bearer tok-xyz.789");+ assert!(+ !redacted.contains("sk-"),+ "no sk- prefix survives: {redacted}"+ );+ assert!(!redacted.contains("tok-xyz.789"), "bearer token stripped");+ assert!(redacted.contains("[redacted-key]"));+ assert!(redacted.contains("Bearer [redacted]"));+ // Non-secret text is preserved.+ assert!(redacted.contains("Authorization:"));+}++/// Issue #5017 — a transport-level failure (DNS / refused connection) is a+/// reachability problem, distinct from a server that answered. Timeouts fall+/// in the same bucket.+#[test]+fn classify_embed_probe_distinguishes_unreachable() {+ for detail in [+ "openai embeddings request to http://127.0.0.1:9/v1/embeddings failed: connection refused",+ "error trying to connect: dns error: failed to lookup address information",+ ] {+ assert_eq!(+ reject_code(EmbedProbe::Failed(detail.into())).as_deref(),+ Some("EMBEDDINGS_ENDPOINT_UNREACHABLE"),+ "detail should classify as unreachable: {detail}"+ );+ }+ // A timeout is a reachability problem too.+ assert_eq!(+ reject_code(EmbedProbe::TimedOut).as_deref(),+ Some("EMBEDDINGS_ENDPOINT_UNREACHABLE")+ );+}++/// Issue #5017 — a length guard trip (endpoint ignored the `dimensions`+/// param and returned its native size) is a dimension problem, not a generic+/// failure, so the user knows to fix the dimensions field.+#[test]+fn classify_embed_probe_distinguishes_dimension_mismatch() {+ assert_eq!(+ reject_code(EmbedProbe::Failed(+ "openai embed dimension mismatch: expected 1024, got 3072".into()+ ))+ .as_deref(),+ Some("EMBEDDINGS_DIMENSION_MISMATCH")+ );+}++/// Issue #5017 regression — the request the app sends is correct: a conformant+/// OpenAI-compatible `POST /v1/embeddings` host (right path, the user's model,+/// Bearer key, `{"input":[…],"model":…}` body) verifies successfully. Builds+/// the provider exactly as the save-time probe does+/// (`create_embedding_provider_with_credentials("custom", …, custom_endpoint)`)+/// and drives it against a mock that echoes the OpenAI embeddings wire shape,+/// asserting the captured request AND that the probe classifies it as a pass.+#[tokio::test]+async fn conformant_custom_endpoint_verifies_and_sends_expected_request() {+ use std::sync::{Arc, Mutex};++ use axum::{+ extract::State,+ routing::{get, post},+ Json, Router,+ };++ #[derive(Clone, Default)]+ struct Captured {+ auth: Arc<Mutex<Option<String>>>,+ body: Arc<Mutex<Option<serde_json::Value>>>,+ }++ let captured = Captured::default();+ let app = Router::new()+ .route(+ "/v1/embeddings",+ post(+ |State(cap): State<Captured>,+ headers: axum::http::HeaderMap,+ Json(body): Json<serde_json::Value>| async move {+ *cap.auth.lock().unwrap() = headers+ .get("authorization")+ .and_then(|v| v.to_str().ok())+ .map(str::to_owned);+ *cap.body.lock().unwrap() = Some(body);+ Json(serde_json::json!({+ "object": "list",+ "data": [{+ "object": "embedding",+ "index": 0,+ "embedding": [0.1_f32, 0.2, 0.3, 0.4]+ }],+ "model": "my-embed",+ }))+ },+ ),+ )+ .route(+ "/v1/models",+ get(|| async { Json(serde_json::json!({"data": []})) }),+ )+ .with_state(captured.clone());++ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let base = format!(+ "http://127.0.0.1:{}/v1",+ listener.local_addr().unwrap().port()+ );+ tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });++ // Same construction as the save-time probe for a non-`text-embedding-3-*`+ // model: probe dimension-agnostically (dims = 0).+ let embedder = create_embedding_provider_with_credentials(+ "custom",+ "gpt-5-mini",+ 0,+ "sk-secret-key",+ Some(&base),+ )+ .expect("provider builds");++ let vectors = embedder+ .embed(&["connection test"])+ .await+ .expect("conformant endpoint must verify");+ let probe_dims = vectors.first().map(|v| v.len()).unwrap_or(0);+ assert_eq!(+ probe_dims, 4,+ "auto-detected the endpoint's real vector length"+ );++ // The probe policy accepts the returned vector.+ assert!(+ classify_embed_probe(EmbedProbe::Returned(vectors)).is_none(),+ "a usable vector from a conformant endpoint must pass verification"+ );++ // The exact request: user's model forwarded + Bearer key present.+ assert_eq!(+ captured.auth.lock().unwrap().as_deref(),+ Some("Bearer sk-secret-key"),+ "API key must be sent on the test-embed request"+ );+ let body = captured+ .body+ .lock()+ .unwrap()+ .clone()+ .expect("body captured");+ assert_eq!(+ body["model"], "gpt-5-mini",+ "user-supplied model must be forwarded"+ );+ assert_eq!(+ body["input"],+ serde_json::json!(["connection test"]),+ "input is the OpenAI array-of-strings shape"+ );+}++/// #5356: the managed paths build the cloud embedder through the+/// config-aware factory, so the bearer resolver reads the config-scoped+/// credential store. With no stored `app-session` token the resolver+/// short-circuits to the backend-session error *before* any network call+/// (privacy defaults to `Standard`, so egress is allowed and the failure is+/// the missing session, not a local-only block). Also covers the rerouted+/// `test_connection` construction line.+#[tokio::test]+async fn test_connection_managed_without_session_reports_no_backend_session() {+ let tmp = TempDir::new().unwrap();+ let mut config = Config::default();+ config.config_path = tmp.path().join("config.toml");+ let out = test_connection(&config, Some("managed"), Some("voyage-3-large"), Some(1024))+ .await+ .expect("rpc returns Ok carrying a success flag");+ assert_eq!(+ out.value["success"],+ serde_json::json!(false),+ "managed test with no session must not pass"+ );+ let err = out.value["error"].as_str().unwrap_or_default();+ assert!(+ err.contains("No backend session"),+ "managed test with no session must report the backend-session error, got: {err}"+ );+}++/// Live `embed` (RPC) for managed also routes through the config-aware+/// factory; with no session it surfaces the same backend-session error.+#[tokio::test]+async fn embed_managed_without_session_errors_with_no_backend_session() {+ let tmp = TempDir::new().unwrap();+ let mut config = Config::default();+ config.config_path = tmp.path().join("config.toml");+ config.memory.embedding_provider = "managed".to_string();+ config.memory.embedding_model = "voyage-3-large".to_string();+ let err = embed(&config, &["hello".to_string()])+ .await+ .expect_err("managed embed with no session must error");+ assert!(+ err.contains("No backend session"),+ "expected backend-session error, got: {err}"+ );+}++/// `provider_from_config` (reused by other domains for direct embedding)+/// routes managed construction through the config-aware factory too — pure+/// construction, so it builds the cloud provider without a network call.+#[test]+fn provider_from_config_managed_builds_cloud() {+ let tmp = TempDir::new().unwrap();+ let mut config = Config::default();+ config.config_path = tmp.path().join("config.toml");+ config.memory.embedding_provider = "managed".to_string();+ config.memory.embedding_model = "voyage-3-large".to_string();+ config.memory.embedding_dimensions = 1024;+ let provider = provider_from_config(&config).expect("managed provider must build");+ assert_eq!(provider.name(), "cloud");+}