Personal AI, On Personal Devices
Python
fix: require desktop inference consent (#896)
Elliot Sluskyd27fd801354d
diff --git a/frontend/src-tauri/src/lib.rs b/frontend/src-tauri/src/lib.rsindex 527512c2..97ea7234 100644--- a/frontend/src-tauri/src/lib.rs+++ b/frontend/src-tauri/src/lib.rs@@ -366,6 +366,18 @@ impl ChildHandle {}}+/// Spawn a subprocess whose lifetime cannot escape the async operation that+/// created it. Tokio's default is `kill_on_drop(false)`, which means aborting+/// the boot task in the small window between `spawn()` and storing the child in+/// `BackendManager` would detach it. The same default also lets one-shot boot+/// commands (`git clone`, `uv sync`, extension verification) continue after a+/// source reset. Every subprocess on the boot path must opt in here (or call+/// `kill_on_drop(true)` before `output()`).+fn spawn_owned_child(cmd: &mut tokio::process::Command) -> std::io::Result<tokio::process::Child> {+ cmd.kill_on_drop(true);+ cmd.spawn()+}+/// Rolling buffer holding the most recent ~16 KB of jarvis stderr.////// Populated by a background drainer task spawned at boot so the pipe@@ -379,6 +391,7 @@ struct BackendManager {ollama: Option<ChildHandle>,jarvis: Option<ChildHandle>,jarvis_stderr_tail: StderrTail,+ boot_task: Option<tauri::async_runtime::JoinHandle<()>>,}impl Default for BackendManager {@@ -387,12 +400,20 @@ impl Default for BackendManager {ollama: None,jarvis: None,jarvis_stderr_tail: Arc::new(Mutex::new(Vec::new())),+ boot_task: None,}}}impl BackendManager {async fn stop_all(&mut self) {+ if let Some(task) = self.boot_task.take() {+ task.abort();+ // `abort()` only requests cancellation. Await it before touching+ // children or returning so the boot future has dropped any local,+ // not-yet-registered child and cannot write config after reset.+ let _ = task.await;+ }if let Some(ref mut h) = self.jarvis {h.kill().await;}@@ -406,6 +427,19 @@ impl BackendManager {type SharedBackend = Arc<Mutex<BackendManager>>;+/// Spawn exactly one tracked boot task. Keeping its handle lets recovery+/// abort an in-flight endpoint check/model download before killing children.+async fn start_managed_boot(backend: SharedBackend, status: SharedStatus) {+ let mut manager = backend.lock().await;+ if let Some(task) = manager.boot_task.take() {+ task.abort();+ let _ = task.await;+ }+ let task_backend = backend.clone();+ let task = tauri::async_runtime::spawn(boot_backend(task_backend, status));+ manager.boot_task = Some(task);+}+// ---------------------------------------------------------------------------// Setup status (reported to frontend)// ---------------------------------------------------------------------------@@ -418,20 +452,41 @@ struct SetupStatus {server_ready: bool,model_ready: bool,error: Option<String>,- /// "ollama" | "custom" — lets the setup UI relabel the progress steps.+ /// "unconfigured" | "ollama" | "custom" — lets the setup UI choose+ /// between the consent screen and source-aware progress labels.source: String,+ /// A fresh desktop install must collect an explicit source choice before+ /// any inference process is allowed to start.+ requires_source: bool,}impl Default for SetupStatus {fn default() -> Self {Self {- phase: "starting".into(),- detail: "Initializing...".into(),+ phase: "awaiting_source".into(),+ detail: "Choose where OpenJarvis should run models.".into(),ollama_ready: false,server_ready: false,model_ready: false,error: None,- source: "ollama".into(),+ source: "unconfigured".into(),+ requires_source: true,+ }+ }+}++impl SetupStatus {+ fn starting(cfg: &InferenceConfig) -> Self {+ Self {+ phase: "starting".into(),+ detail: "Initializing...".into(),+ source: match cfg.kind {+ SourceKind::Ollama => "ollama",+ SourceKind::Custom => "custom",+ }+ .into(),+ requires_source: false,+ ..Self::default()}}}@@ -647,9 +702,18 @@ fn matching_installed_model(models: &[String], requested: &str) -> Option<Stringfn model_name_looks_embedding_only(model: &str) -> bool {let name = model.to_ascii_lowercase();- ["embed", "embedding", "rerank", "minilm", "bge-", "bge_", "e5-", "e5_"]- .iter()- .any(|marker| name.contains(marker))+ [+ "embed",+ "embedding",+ "rerank",+ "minilm",+ "bge-",+ "bge_",+ "e5-",+ "e5_",+ ]+ .iter()+ .any(|marker| name.contains(marker))}fn preferred_installed_model(models: &[String]) -> Option<String> {@@ -718,18 +782,19 @@ async fn pull_model(model: &str) -> Result<(), String> {fn uv_sync_stderr_tail(stderr: &str, max_chars: usize) -> String {let total = stderr.chars().count();let skip = total.saturating_sub(max_chars);- stderr.chars().skip(skip).collect::<String>().trim().to_string()+ stderr+ .chars()+ .skip(skip)+ .collect::<String>()+ .trim()+ .to_string()}/// Error message shown when `uv sync` runs but exits non-zero (#331).////// `exit_code` is `None` when the process was terminated by a signal with/// no exit code (rendered as "unknown" rather than a misleading -1).-fn format_uv_sync_failure(- root: &std::path::Path,- exit_code: Option<i32>,- stderr: &str,-) -> String {+fn format_uv_sync_failure(root: &std::path::Path, exit_code: Option<i32>, stderr: &str) -> String {let code = exit_code.map(|c| c.to_string()).unwrap_or_else(|| "unknown".to_string());@@ -870,6 +935,7 @@ async fn verify_openjarvis_rust_extension(.current_dir(root);prepare_subprocess_for_appimage(&mut cmd);add_cargo_bin_to_path(&mut cmd);+ cmd.kill_on_drop(true);match cmd.output().await {Ok(out) if out.status.success() => Ok(()),@@ -917,11 +983,18 @@ fn check_jarvis_port_available() -> Result<(), String> {// ---------------------------------------------------------------------------async fn boot_backend(backend: SharedBackend, status: SharedStatus) {- // Decide the inference source (default Ollama) before launching anything.- let cfg = read_inference_config();+ // Never infer consent from a missing or malformed config. A fresh desktop+ // install must persist an explicit source choice before this function can+ // launch Ollama, contact a custom endpoint, or download a model.+ let Some(mut cfg) = read_configured_inference_config() else {+ *status.lock().await = SetupStatus::default();+ return;+ };+ let mut pending_rollback = PendingInferenceRollback::new(&cfg);let plan = boot_plan(&cfg, total_ram_gb());{let mut s = status.lock().await;+ *s = SetupStatus::starting(&cfg);s.source = match cfg.kind {SourceKind::Ollama => "ollama",SourceKind::Custom => "custom",@@ -953,7 +1026,7 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {.stderr(std::process::Stdio::null());// Avoid LD_LIBRARY_PATH leak when running inside an AppImage (#455).prepare_subprocess_for_appimage(&mut sidecar_cmd);- match sidecar_cmd.spawn() {+ match spawn_owned_child(&mut sidecar_cmd) {Ok(child) => Some(child),Err(_) => None,}@@ -989,7 +1062,9 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {}let installed_models = ollama_model_names().await;- let resolved_model = if let Some(installed) = startup_installed_model(&model, &installed_models) {+ let resolved_model = if let Some(installed) =+ startup_installed_model(&model, &installed_models)+ {installed} else {{@@ -1003,7 +1078,8 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {// If a local model appeared while pulling, use it instead of// making startup depend on another network pull.- if let Some(installed) = preferred_installed_model(&ollama_model_names().await) {+ if let Some(installed) = preferred_installed_model(&ollama_model_names().await)+ {installed} else if ollama_has_model(FALLBACK_MODEL).await {FALLBACK_MODEL.to_string()@@ -1043,7 +1119,9 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {if should_persist_resolved_model(&cfg) {let mut persisted = cfg.clone();persisted.model = Some(resolved_model);- let _ = write_inference_config(&persisted);+ if write_inference_config(&persisted).is_ok() {+ cfg = persisted;+ }}{@@ -1067,8 +1145,12 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {let mut s = status.lock().await;s.error = Some(format!("Could not reach your custom inference server at {}. \- Start the server (e.g. LM Studio) and check the URL in Settings, then relaunch.",- if host.is_empty() { "(no URL set)" } else { host.as_str() }+ Start the server (e.g. LM Studio), or choose Change inference source below.",+ if host.is_empty() {+ "(no URL set)"+ } else {+ host.as_str()+ }));return;}@@ -1165,7 +1247,8 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {s.detail = "Downloading OpenJarvis (first launch)...".into();}- let clone_result = tokio::process::Command::new(&git_bin)+ let mut clone_cmd = tokio::process::Command::new(&git_bin);+ clone_cmd.args(["clone","--depth",@@ -1174,8 +1257,9 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {&clone_target,]).stdout(std::process::Stdio::null())- .stderr(std::process::Stdio::piped())- .spawn();+ .stderr(std::process::Stdio::piped());+ prepare_subprocess_for_appimage(&mut clone_cmd);+ let clone_result = spawn_owned_child(&mut clone_cmd);match clone_result {Ok(child) => match child.wait_with_output().await {@@ -1266,16 +1350,42 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {// away between probes.// (No early return — we want to spawn our own.)} else {+ // A newly staged key may only enter a child process that+ // this desktop instance launched. A listener that merely+ // answers /health could be a port squatter; never POST or+ // otherwise disclose the pending secret to it.+ match pending_rollback.staged_credential_present() {+ Ok(true) => {+ let mut s = status.lock().await;+ s.error = Some(format!(+ "An API server is already running on port {}, but OpenJarvis cannot securely apply your new inference API key to a server it did not start. Stop that server, then try setup again.",+ JARVIS_PORT,+ ));+ return;+ }+ Err(err) => {+ let mut s = status.lock().await;+ s.error = Some(format!(+ "Could not verify the staged inference API key: {}",+ err+ ));+ return;+ }+ Ok(false) => {}+ }// Attach to the existing healthy server. Mark every// pre-spawn step done so the setup UI doesn't show a// half-progress bar (model_ready / ollama_ready stay// false otherwise because we skipped those steps).+ if let Err(err) = pending_rollback.confirm(&mut cfg) {+ let mut s = status.lock().await;+ s.error = Some(format!("Could not confirm inference setup: {}", err));+ return;+ }let mut s = status.lock().await;s.phase = "ready".into();- s.detail = format!(- "Connected to existing API server on port {}.",- JARVIS_PORT,- );+ s.detail =+ format!("Connected to existing API server on port {}.", JARVIS_PORT,);s.server_ready = true;s.model_ready = true;s.ollama_ready = true;@@ -1351,12 +1461,16 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {sync_cmd.args(["sync",- "--extra", "desktop",- "--extra", "inference-cloud",- "--extra", "inference-google",+ "--extra",+ "desktop",+ "--extra",+ "inference-cloud",+ "--extra",+ "inference-google",// openjarvis_rust lives in a uv dependency group (not the published// `desktop` extra) so pip installs from PyPI don't require it (#584).- "--group", "desktop-native",+ "--group",+ "desktop-native",]).stdout(std::process::Stdio::null()).stderr(std::process::Stdio::piped())@@ -1364,6 +1478,7 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {// Avoid LD_LIBRARY_PATH leak when running inside an AppImage (#455).prepare_subprocess_for_appimage(&mut sync_cmd);add_cargo_bin_to_path(&mut sync_cmd);+ sync_cmd.kill_on_drop(true);let sync_output = sync_cmd.output().await;match sync_output {Ok(out) if !out.status.success() => {@@ -1427,10 +1542,10 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {prepare_subprocess_for_appimage(&mut cmd);// Inject cloud API keys from secure desktop storage.- for (key, value) in read_cloud_keys() {+ for (key, value) in cloud_keys_for_inference(&cfg) {cmd.env(&key, &value);}- let jarvis_child = cmd.spawn();+ let jarvis_child = spawn_owned_child(&mut cmd);match jarvis_child {Ok(mut child) => {@@ -1532,6 +1647,12 @@ async fn boot_backend(backend: SharedBackend, status: SharedStatus) {}}+ if let Err(err) = pending_rollback.confirm(&mut cfg) {+ let mut s = status.lock().await;+ s.error = Some(format!("Could not confirm inference setup: {}", err));+ return;+ }+{let mut s = status.lock().await;s.server_ready = true;@@ -1572,9 +1693,13 @@ async fn start_backend(backend: tauri::State<'_, SharedBackend>,status: tauri::State<'_, SharedStatus>,) -> Result<(), String> {+ if read_configured_inference_config().is_none() {+ *status.lock().await = SetupStatus::default();+ return Err("Choose an inference source before starting OpenJarvis.".into());+ }let b = backend.inner().clone();let s = status.inner().clone();- tauri::async_runtime::spawn(boot_backend(b, s));+ start_managed_boot(b, s).await;Ok(())}@@ -1584,6 +1709,19 @@ async fn stop_backend(backend: tauri::State<'_, SharedBackend>) -> Result<(), StOk(())}+/// Abort startup, stop every child, discard an unworkable source, and return+/// the UI to the inert chooser. This is the recovery path for setup failures.+#[tauri::command]+async fn reset_inference_source(+ backend: tauri::State<'_, SharedBackend>,+ status: tauri::State<'_, SharedStatus>,+) -> Result<(), String> {+ backend.lock().await.stop_all().await;+ discard_pending_inference_setup()?;+ *status.lock().await = SetupStatus::default();+ Ok(())+}+#[tauri::command]async fn check_health(api_url: String) -> Result<serde_json::Value, String> {let url = format!(@@ -1943,6 +2081,10 @@ async fn submit_savings(// ---------------------------------------------------------------------------const SECURE_KEY_SERVICE: &str = "OpenJarvis Cloud Keys";+// A first-run custom credential is kept in its own secure-storage slot until+// the managed backend has passed every readiness gate. In particular, do not+// overwrite an existing <ENGINE>_API_KEY while setup can still be cancelled.+const PENDING_INFERENCE_API_KEY: &str = "OPENJARVIS_PENDING_INFERENCE_API_KEY";const MANAGED_CLOUD_KEY_NAMES: &[&str] = &["OPENAI_API_KEY","ANTHROPIC_API_KEY",@@ -2003,7 +2145,9 @@ fn managed_cloud_key_names() -> Vec<String> {let cfg = read_inference_config();if matches!(&cfg.kind, SourceKind::Custom) {- let engine = cfg.engine.unwrap_or_else(|| CUSTOM_FALLBACK_ENGINE.to_string());+ let engine = cfg+ .engine+ .unwrap_or_else(|| CUSTOM_FALLBACK_ENGINE.to_string());let key_name = engine_api_key_name(&engine);if validate_cloud_key_name(&key_name).is_ok() {names.push(key_name);@@ -2017,19 +2161,30 @@ fn managed_cloud_key_names() -> Vec<String> {fn secure_store_get(key_name: &str) -> Result<Option<String>, String> {validate_cloud_key_name(key_name)?;- let entry = keyring::Entry::new(SECURE_KEY_SERVICE, key_name)- .map_err(|err| format!("Failed to open secure key storage for {}: {}", key_name, err))?;+ let entry = keyring::Entry::new(SECURE_KEY_SERVICE, key_name).map_err(|err| {+ format!(+ "Failed to open secure key storage for {}: {}",+ key_name, err+ )+ })?;match entry.get_password() {Ok(value) => Ok(Some(value)),Err(keyring::Error::NoEntry) => Ok(None),- Err(err) => Err(format!("Failed to read {} from secure key storage: {}", key_name, err)),+ Err(err) => Err(format!(+ "Failed to read {} from secure key storage: {}",+ key_name, err+ )),}}fn secure_store_set(key_name: &str, key_value: &str) -> Result<(), String> {validate_cloud_key_name(key_name)?;- let entry = keyring::Entry::new(SECURE_KEY_SERVICE, key_name)- .map_err(|err| format!("Failed to open secure key storage for {}: {}", key_name, err))?;+ let entry = keyring::Entry::new(SECURE_KEY_SERVICE, key_name).map_err(|err| {+ format!(+ "Failed to open secure key storage for {}: {}",+ key_name, err+ )+ })?;if key_value.is_empty() {return match entry.delete_credential() {Ok(()) => Ok(()),@@ -2045,6 +2200,23 @@ fn secure_store_set(key_name: &str, key_value: &str) -> Result<(), String> {.map_err(|err| format!("Failed to save {} in secure key storage: {}", key_name, err))}+trait InferenceCredentialStore: Send + Sync {+ fn get(&self, key_name: &str) -> Result<Option<String>, String>;+ fn set(&self, key_name: &str, key_value: &str) -> Result<(), String>;+}++struct SystemInferenceCredentialStore;++impl InferenceCredentialStore for SystemInferenceCredentialStore {+ fn get(&self, key_name: &str) -> Result<Option<String>, String> {+ secure_store_get(key_name)+ }++ fn set(&self, key_name: &str, key_value: &str) -> Result<(), String> {+ secure_store_set(key_name, key_value)+ }+}+fn read_legacy_cloud_keys() -> Vec<(String, String)> {let path = legacy_cloud_keys_path();let mut keys = Vec::new();@@ -2101,29 +2273,91 @@ fn read_cloud_keys() -> Vec<(String, String)> {.collect()}-async fn reload_cloud_keys(keys: Vec<(String, String)>) {- let reload_url = format!("http://127.0.0.1:{}/v1/cloud/reload", JARVIS_PORT);+/// Overlay a staged first-run credential onto the child-process environment.+/// The durable provider key remains untouched until setup is confirmed.+fn cloud_keys_for_inference(cfg: &InferenceConfig) -> Vec<(String, String)> {+ let mut keys = read_cloud_keys();+ let Some(credential) = PendingInferenceCredential::for_config(cfg) else {+ return keys;+ };+ let Ok(Some(staged)) = secure_store_get(PENDING_INFERENCE_API_KEY) else {+ return keys;+ };+ if staged.is_empty() {+ return keys;+ }+ keys.retain(|(name, _)| name != &credential.target_key);+ keys.push((credential.target_key, staged));+ keys+}++async fn reload_cloud_keys_at(reload_url: &str, keys: Vec<(String, String)>) {let key_map: serde_json::Map<String, serde_json::Value> = keys.into_iter().map(|(key, value)| (key, serde_json::Value::String(value))).collect();let _ = reqwest::Client::new()- .post(&reload_url)+ .post(reload_url).json(&serde_json::json!({ "keys": key_map })).timeout(std::time::Duration::from_secs(10)).send().await;}+/// Hot-reload only a live server process launched and owned by this desktop+/// process. A healthy-looking loopback listener is not proof of ownership and+/// must never receive raw credentials.+async fn reload_cloud_keys_for_owned_backend(+ backend: &SharedBackend,+ status: &SharedStatus,+ keys: Vec<(String, String)>,+) {+ let reload_url = format!("http://127.0.0.1:{}/v1/cloud/reload", JARVIS_PORT);+ reload_cloud_keys_for_owned_backend_at(backend, status, keys, &reload_url).await;+}++async fn reload_cloud_keys_for_owned_backend_at(+ backend: &SharedBackend,+ status: &SharedStatus,+ keys: Vec<(String, String)>,+ reload_url: &str,+) {+ if !status.lock().await.server_ready {+ return;+ }++ // Keep the manager lock for the request so stop/reset cannot kill our+ // child and let an unrelated process claim the port between the ownership+ // check and the credential POST.+ let mut manager = backend.lock().await;+ let Some(handle) = manager.jarvis.as_mut() else {+ return;+ };+ if !matches!(handle.child.try_wait(), Ok(None)) {+ return;+ }+ reload_cloud_keys_at(reload_url, keys).await;+}+/// Save a single cloud API key to secure desktop storage.#[tauri::command]-async fn save_cloud_key(key_name: String, key_value: String) -> Result<(), String> {+async fn save_cloud_key(+ key_name: String,+ key_value: String,+ backend: tauri::State<'_, SharedBackend>,+ status: tauri::State<'_, SharedStatus>,+) -> Result<(), String> {let key_value = key_value.trim().to_string();secure_store_set(&key_name, &key_value)?;- // Tell the running server to hot-reload its cloud engine so the user- // doesn't need to restart the app after entering an API key.- reload_cloud_keys(vec![(key_name, key_value)]).await;+ // Tell only our own ready child to hot-reload its cloud engine. Posting to+ // a merely responsive port would disclose the raw key to a port squatter.+ reload_cloud_keys_for_owned_backend(+ backend.inner(),+ status.inner(),+ vec![(key_name, key_value)],+ )+ .await;Ok(())}@@ -2158,6 +2392,7 @@ async fn set_inference_source(host: Option<String>,engine: Option<String>,api_key: Option<String>,+ pending: Option<bool>,) -> Result<(), String> {let kind = match kind.as_str() {"ollama" => SourceKind::Ollama,@@ -2166,10 +2401,14 @@ async fn set_inference_source(};let cfg = InferenceConfig {kind,+ confirmed: !pending.unwrap_or(false),model: model.filter(|m| !m.is_empty()),host: host.map(|h| normalize_host(&h)).filter(|h| !h.is_empty()),engine: engine.filter(|e| !e.is_empty()),};+ let api_key = api_key+ .map(|key| key.trim().to_string())+ .filter(|key| !key.is_empty());if let SourceKind::Custom = cfg.kind {if cfg.host.is_none() {return Err("A server URL is required for a custom endpoint.".into());@@ -2177,21 +2416,14 @@ async fn set_inference_source(if cfg.model.as_deref().unwrap_or("").is_empty() {return Err("A model name is required for a custom endpoint.".into());}- if let Some(key) = api_key.filter(|k| !k.is_empty()) {- let engine = cfg- .engine- .clone()- .unwrap_or_else(|| CUSTOM_FALLBACK_ENGINE.to_string());- let key_name = engine_api_key_name(&engine);- // Save the key before persisting the config: if the key can't be- // written, surface it and DON'T record a custom source whose- // credential is missing (which would fail confusingly at runtime).- save_cloud_key(key_name, key)- .await- .map_err(|e| format!("Could not store the API key: {}", e))?;- }}- write_inference_config(&cfg)++ let store = SystemInferenceCredentialStore;+ if cfg.confirmed {+ persist_confirmed_inference_config(&cfg, api_key.as_deref(), &store)+ } else {+ stage_pending_inference_config(&cfg, api_key.as_deref(), &store)+ }}/// Pull a model via Ollama (called from frontend download button).@@ -2244,6 +2476,10 @@ impl Default for SourceKind {struct InferenceConfig {#[serde(default)]kind: SourceKind,+ /// First-run choices stay pending until the complete backend reaches+ /// ready. Legacy configs predate this field and remain trusted.+ #[serde(default = "legacy_config_is_confirmed")]+ confirmed: bool,#[serde(default, skip_serializing_if = "Option::is_none")]model: Option<String>,/// Bare base URL (no trailing `/v1`), custom only.@@ -2254,6 +2490,10 @@ struct InferenceConfig {engine: Option<String>,}+fn legacy_config_is_confirmed() -> bool {+ true+}+/// Path to the inference-source config (~/.openjarvis/inference.json).fn inference_config_path() -> std::path::PathBuf {std::path::PathBuf::from(home_dir())@@ -2261,28 +2501,313 @@ fn inference_config_path() -> std::path::PathBuf {.join("inference.json")}-/// Parse config text. Any error (missing/garbage) yields the Ollama default —-/// a broken file must never strand the user with no working inference source.-fn parse_inference_config(text: &str) -> InferenceConfig {- serde_json::from_str::<InferenceConfig>(text).unwrap_or_default()+fn remove_inference_config_at(path: &std::path::Path) -> Result<(), String> {+ match std::fs::remove_file(path) {+ Ok(()) => Ok(()),+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),+ Err(err) => Err(format!("Failed to clear inference config: {}", err)),+ }+}++#[derive(Clone, Debug, PartialEq, Eq)]+struct PendingInferenceCredential {+ target_key: String,+}++impl PendingInferenceCredential {+ fn for_config(cfg: &InferenceConfig) -> Option<Self> {+ if cfg.confirmed || !matches!(cfg.kind, SourceKind::Custom) {+ return None;+ }+ let engine = cfg.engine.as_deref().unwrap_or(CUSTOM_FALLBACK_ENGINE);+ Some(Self {+ target_key: engine_api_key_name(engine),+ })+ }+}++fn restore_credential(+ store: &dyn InferenceCredentialStore,+ key_name: &str,+ previous: Option<&str>,+) -> Result<(), String> {+ store.set(key_name, previous.unwrap_or(""))+}++/// Persist a returning-user source and its optional credential as one logical+/// transaction. If the config write fails, restore the exact prior key.+fn persist_confirmed_inference_config(+ cfg: &InferenceConfig,+ api_key: Option<&str>,+ store: &dyn InferenceCredentialStore,+) -> Result<(), String> {+ persist_confirmed_inference_config_at(cfg, api_key, store, inference_config_path().as_path())+}++fn persist_confirmed_inference_config_at(+ cfg: &InferenceConfig,+ api_key: Option<&str>,+ store: &dyn InferenceCredentialStore,+ path: &std::path::Path,+) -> Result<(), String> {+ let Some(api_key) = api_key else {+ return write_inference_config_at(cfg, path);+ };+ let Some(target_key) = (matches!(cfg.kind, SourceKind::Custom))+ .then(|| engine_api_key_name(cfg.engine.as_deref().unwrap_or(CUSTOM_FALLBACK_ENGINE)))+ else {+ return write_inference_config_at(cfg, path);+ };++ let previous = store+ .get(&target_key)+ .map_err(|err| format!("Could not read the existing API key: {}", err))?;+ store+ .set(&target_key, api_key)+ .map_err(|err| format!("Could not store the API key: {}", err))?;+ if let Err(config_err) = write_inference_config_at(cfg, path) {+ return match restore_credential(store, &target_key, previous.as_deref()) {+ Ok(()) => Err(config_err),+ Err(restore_err) => Err(format!(+ "{}; additionally could not restore the previous API key: {}",+ config_err, restore_err+ )),+ };+ }+ Ok(())+}++/// Stage a first-run key in a dedicated secure slot. The real provider key is+/// not touched until the managed server is healthy and setup is confirmed.+fn stage_pending_inference_config(+ cfg: &InferenceConfig,+ api_key: Option<&str>,+ store: &dyn InferenceCredentialStore,+) -> Result<(), String> {+ stage_pending_inference_config_at(cfg, api_key, store, inference_config_path().as_path())+}++fn stage_pending_inference_config_at(+ cfg: &InferenceConfig,+ api_key: Option<&str>,+ store: &dyn InferenceCredentialStore,+ path: &std::path::Path,+) -> Result<(), String> {+ // A new choice supersedes any interrupted prior attempt. Fail closed if+ // secure storage cannot clear it: otherwise a stale secret could be bound+ // to the wrong engine when boot begins.+ store+ .set(PENDING_INFERENCE_API_KEY, "")+ .map_err(|err| format!("Could not clear a pending API key: {}", err))?;+ if matches!(cfg.kind, SourceKind::Custom) {+ if let Some(api_key) = api_key {+ store+ .set(PENDING_INFERENCE_API_KEY, api_key)+ .map_err(|err| format!("Could not stage the API key: {}", err))?;+ }+ }++ if let Err(config_err) = write_inference_config_at(cfg, path) {+ return match store.set(PENDING_INFERENCE_API_KEY, "") {+ Ok(()) => Err(config_err),+ Err(cleanup_err) => Err(format!(+ "{}; additionally could not discard the staged API key: {}",+ config_err, cleanup_err+ )),+ };+ }+ Ok(())+}++fn discard_pending_inference_setup() -> Result<(), String> {+ discard_pending_inference_setup_at(+ inference_config_path().as_path(),+ &SystemInferenceCredentialStore,+ )+}++fn discard_pending_inference_setup_at(+ path: &std::path::Path,+ store: &dyn InferenceCredentialStore,+) -> Result<(), String> {+ let key_result = store.set(PENDING_INFERENCE_API_KEY, "");+ let config_result = remove_inference_config_at(path);+ match (key_result, config_result) {+ (Ok(()), Ok(())) => Ok(()),+ (Err(key_err), Ok(())) => Err(format!("Failed to discard pending API key: {}", key_err)),+ (Ok(()), Err(config_err)) => Err(config_err),+ (Err(key_err), Err(config_err)) => Err(format!(+ "{}; additionally failed to discard pending API key: {}",+ config_err, key_err+ )),+ }+}++/// Rolls a first-run choice back whenever boot exits before confirmation.+/// Confirmed returning-user configs are never removed by a transient outage.+struct PendingInferenceRollback {+ path: Option<std::path::PathBuf>,+ credential: Option<PendingInferenceCredential>,+ credential_store: Option<Arc<dyn InferenceCredentialStore>>,+}++impl PendingInferenceRollback {+ fn new(cfg: &InferenceConfig) -> Self {+ Self {+ path: (!cfg.confirmed).then_some(inference_config_path()),+ credential: PendingInferenceCredential::for_config(cfg),+ credential_store: (!cfg.confirmed).then(|| {+ Arc::new(SystemInferenceCredentialStore) as Arc<dyn InferenceCredentialStore>+ }),+ }+ }++ #[cfg(test)]+ fn at_path(cfg: &InferenceConfig, path: std::path::PathBuf) -> Self {+ Self {+ path: (!cfg.confirmed).then_some(path),+ credential: None,+ credential_store: None,+ }+ }++ #[cfg(test)]+ fn at_path_with_store(+ cfg: &InferenceConfig,+ path: std::path::PathBuf,+ credential_store: Arc<dyn InferenceCredentialStore>,+ ) -> Self {+ Self {+ path: (!cfg.confirmed).then_some(path),+ credential: PendingInferenceCredential::for_config(cfg),+ credential_store: (!cfg.confirmed).then_some(credential_store),+ }+ }++ fn disarm(&mut self) {+ self.path = None;+ self.credential = None;+ self.credential_store = None;+ }++ fn staged_credential_present(&self) -> Result<bool, String> {+ let (Some(_), Some(store)) = (&self.credential, &self.credential_store) else {+ return Ok(false);+ };+ Ok(matches!(+ store.get(PENDING_INFERENCE_API_KEY)?,+ Some(value) if !value.is_empty()+ ))+ }++ /// Commit a staged first-run choice only after every readiness gate has+ /// succeeded. Keeping this operation on the rollback guard makes all+ /// successful exits use the same write-then-disarm ordering: if the write+ /// fails (or the future is cancelled), `Drop` removes the staged file.+ fn confirm(&mut self, cfg: &mut InferenceConfig) -> Result<(), String> {+ if cfg.confirmed {+ self.disarm();+ return Ok(());+ }++ let path = self+ .path+ .as_ref()+ .ok_or_else(|| "Pending inference setup lost its rollback path.".to_string())?;+ let mut confirmed = cfg.clone();+ confirmed.confirmed = true;++ let mut promoted: Option<(String, Option<String>)> = None;+ if let (Some(credential), Some(store)) = (&self.credential, &self.credential_store) {+ if let Some(staged) = store.get(PENDING_INFERENCE_API_KEY)? {+ if !staged.is_empty() {+ let previous = store.get(&credential.target_key)?;+ store.set(&credential.target_key, &staged)?;+ if let Err(cleanup_err) = store.set(PENDING_INFERENCE_API_KEY, "") {+ let restore_result = restore_credential(+ store.as_ref(),+ &credential.target_key,+ previous.as_deref(),+ );+ return match restore_result {+ Ok(()) => Err(cleanup_err),+ Err(restore_err) => Err(format!(+ "{}; additionally could not restore the previous API key: {}",+ cleanup_err, restore_err+ )),+ };+ }+ promoted = Some((credential.target_key.clone(), previous));+ }+ }+ }++ if let Err(config_err) = write_inference_config_at(&confirmed, path) {+ if let (Some((target_key, previous)), Some(store)) = (promoted, &self.credential_store)+ {+ return match restore_credential(store.as_ref(), &target_key, previous.as_deref()) {+ Ok(()) => Err(config_err),+ Err(restore_err) => Err(format!(+ "{}; additionally could not restore the previous API key: {}",+ config_err, restore_err+ )),+ };+ }+ return Err(config_err);+ }+ *cfg = confirmed;+ self.disarm();+ Ok(())+ }+}++impl Drop for PendingInferenceRollback {+ fn drop(&mut self) {+ if let Some(path) = self.path.take() {+ let _ = std::fs::remove_file(path);+ }+ if self.credential.take().is_some() {+ if let Some(store) = self.credential_store.take() {+ let _ = store.set(PENDING_INFERENCE_API_KEY, "");+ }+ }+ }+}++/// Parse only an explicitly persisted source choice. Missing/invalid config is+/// intentionally `None`: callers on the boot path must wait for setup rather+/// than silently falling back to Ollama.+fn parse_configured_inference_config(text: &str) -> Option<InferenceConfig> {+ let value = serde_json::from_str::<serde_json::Value>(text).ok()?;+ if !value.as_object()?.contains_key("kind") {+ return None;+ }+ serde_json::from_value::<InferenceConfig>(value).ok()+}++fn read_configured_inference_config() -> Option<InferenceConfig> {+ std::fs::read_to_string(inference_config_path())+ .ok()+ .and_then(|text| parse_configured_inference_config(&text))}/// Read the on-disk inference config, or the Ollama default if absent.fn read_inference_config() -> InferenceConfig {- match std::fs::read_to_string(inference_config_path()) {- Ok(text) => parse_inference_config(&text),- Err(_) => InferenceConfig::default(),- }+ read_configured_inference_config().unwrap_or_default()}/// Write the inference config to disk (pretty JSON).fn write_inference_config(cfg: &InferenceConfig) -> Result<(), String> {let path = inference_config_path();+ write_inference_config_at(cfg, &path)+}++fn write_inference_config_at(cfg: &InferenceConfig, path: &std::path::Path) -> Result<(), String> {if let Some(parent) = path.parent() {let _ = std::fs::create_dir_all(parent);}let json = serde_json::to_string_pretty(cfg).map_err(|e| e.to_string())?;- std::fs::write(&path, json + "\n").map_err(|e| format!("Failed to save inference config: {}", e))+ std::fs::write(path, json + "\n").map_err(|e| format!("Failed to save inference config: {}", e))}/// Upsert `[engine.<engine>] host = "<host>"` into an existing config.toml@@ -2440,7 +2965,7 @@ mod native_overlay {// Also inject CSS to nuke any remaining backgroundlet js = nsstring("document.documentElement.style.background='transparent';\- document.body.style.background='transparent';"+ document.body.style.background='transparent';",);let nil: *mut Object = std::ptr::null_mut();let _: () = msg_send![wv, evaluateJavaScript: js completionHandler: nil];@@ -2471,7 +2996,9 @@ mod native_overlay {let sup = Class::get("NSObject").unwrap();let mut decl = ClassDecl::new("JarvisOverlayNavDelegate", sup).unwrap();extern "C" fn did_finish(_: &Object, _: Sel, wv: *mut Object, _nav: *mut Object) {- unsafe { force_transparent(wv); }+ unsafe {+ force_transparent(wv);+ }}decl.add_method(sel!(webView:didFinishNavigation:),@@ -2728,7 +3255,31 @@ async fn hide_overlay() -> Result<(), String> {#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {let backend: SharedBackend = Arc::new(Mutex::new(BackendManager::default()));- let status: SharedStatus = Arc::new(Mutex::new(SetupStatus::default()));+ let configured_at_launch = match read_configured_inference_config() {+ Some(cfg) if cfg.confirmed => {+ // A confirmed source never consumes a staging slot. Clear any+ // remnant from a prior interrupted config write.+ let _ = secure_store_set(PENDING_INFERENCE_API_KEY, "");+ Some(cfg)+ }+ Some(_) => {+ // A prior first-run boot was interrupted or failed before it+ // reached ready. Never replay that unvalidated choice on launch.+ let _ = discard_pending_inference_setup();+ None+ }+ None => {+ // Clean up a stage left behind if the process exited between the+ // secure-store write and the pending config write.+ let _ = secure_store_set(PENDING_INFERENCE_API_KEY, "");+ None+ }+ };+ let initial_status = configured_at_launch+ .as_ref()+ .map(SetupStatus::starting)+ .unwrap_or_default();+ let status: SharedStatus = Arc::new(Mutex::new(initial_status));let boot_backend_ref = backend.clone();let boot_status_ref = status.clone();@@ -2813,8 +3364,12 @@ pub fn run() {}}- // Auto-start backend services on launch- tauri::async_runtime::spawn(boot_backend(boot_backend_ref, boot_status_ref));+ // Returning users keep automatic startup after they have persisted+ // a source. Fresh installs stay inert until the setup UI records+ // explicit consent and invokes `start_backend`.+ if configured_at_launch.is_some() {+ tauri::async_runtime::spawn(start_managed_boot(boot_backend_ref, boot_status_ref));+ }Ok(())})@@ -2823,6 +3378,7 @@ pub fn run() {get_api_base,start_backend,stop_backend,+ reset_inference_source,check_health,fetch_energy,fetch_telemetry,@@ -2868,14 +3424,67 @@ pub fn run() {#[cfg(test)]mod tests {use super::{- boot_plan, default_local_model, format_extension_import_failure,- format_missing_rust_toolchain, format_port_unavailable, format_uv_sync_failure,- format_uv_sync_spawn_error, matching_installed_model, model_names_match, normalize_host,- parse_inference_config, parse_ollama_model_names, preferred_installed_model,- should_persist_resolved_model, startup_installed_model, upsert_engine_host,- uv_sync_stderr_tail, InferenceConfig, SourceKind, DESKTOP_UV_SYNC_COMMAND,+ boot_plan, default_local_model, discard_pending_inference_setup_at,+ format_extension_import_failure, format_missing_rust_toolchain, format_port_unavailable,+ format_uv_sync_failure, format_uv_sync_spawn_error, matching_installed_model,+ model_names_match, normalize_host, parse_configured_inference_config,+ parse_ollama_model_names, persist_confirmed_inference_config_at, preferred_installed_model,+ reload_cloud_keys_for_owned_backend_at, should_persist_resolved_model, spawn_owned_child,+ stage_pending_inference_config_at, startup_installed_model, upsert_engine_host,+ uv_sync_stderr_tail, BackendManager, InferenceConfig, InferenceCredentialStore,+ PendingInferenceRollback, SetupStatus, SourceKind, DESKTOP_UV_SYNC_COMMAND,+ PENDING_INFERENCE_API_KEY,};+ use std::collections::HashMap;use std::path::Path;+ use std::sync::Mutex as StdMutex;++ #[derive(Default)]+ struct MemoryCredentialStore {+ values: StdMutex<HashMap<String, String>>,+ }++ impl MemoryCredentialStore {+ fn put(&self, key_name: &str, key_value: &str) {+ self.values+ .lock()+ .unwrap()+ .insert(key_name.to_string(), key_value.to_string());+ }++ fn value(&self, key_name: &str) -> Option<String> {+ self.values.lock().unwrap().get(key_name).cloned()+ }+ }++ impl InferenceCredentialStore for MemoryCredentialStore {+ fn get(&self, key_name: &str) -> Result<Option<String>, String> {+ Ok(self.value(key_name))+ }++ fn set(&self, key_name: &str, key_value: &str) -> Result<(), String> {+ let mut values = self.values.lock().unwrap();+ if key_value.is_empty() {+ values.remove(key_name);+ } else {+ values.insert(key_name.to_string(), key_value.to_string());+ }+ Ok(())+ }+ }++ fn unique_test_root(label: &str) -> std::path::PathBuf {+ let nonce = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos();+ std::env::temp_dir().join(format!(+ "openjarvis-{}-{}-{}",+ label,+ std::process::id(),+ nonce+ ))+ }#[test]fn tail_returns_whole_string_when_shorter_than_limit() {@@ -2986,10 +3595,10 @@ mod tests {#[test]fn default_local_model_picks_second_largest_that_fits() {// QWEN35_MODELS min_ram ladder: 4,6,8,12,24,32,96 GB- assert_eq!(default_local_model(4.0), "qwen3.5:0.8b"); // only one fits- assert_eq!(default_local_model(8.0), "qwen3.5:2b"); // fits 0.8/2/4 → 2nd-largest- assert_eq!(default_local_model(16.0), "qwen3.5:4b"); // fits ..9b → 2nd-largest- assert_eq!(default_local_model(32.0), "qwen3.5:27b"); // fits 0.8/2/4/9/27/35b → 2nd-largest is 27b+ assert_eq!(default_local_model(4.0), "qwen3.5:0.8b"); // only one fits+ assert_eq!(default_local_model(8.0), "qwen3.5:2b"); // fits 0.8/2/4 → 2nd-largest+ assert_eq!(default_local_model(16.0), "qwen3.5:4b"); // fits ..9b → 2nd-largest+ assert_eq!(default_local_model(32.0), "qwen3.5:27b"); // fits 0.8/2/4/9/27/35b → 2nd-largest is 27bassert_eq!(default_local_model(128.0), "qwen3.5:35b"); // fits all → 2nd-largest}@@ -3071,7 +3680,10 @@ mod tests {#[test]fn resolved_model_is_only_persisted_when_no_model_was_configured() {- let default_cfg = InferenceConfig { kind: SourceKind::Ollama, ..Default::default() };+ let default_cfg = InferenceConfig {+ kind: SourceKind::Ollama,+ ..Default::default()+ };assert!(should_persist_resolved_model(&default_cfg));let empty_cfg = InferenceConfig {@@ -3090,16 +3702,61 @@ mod tests {}#[test]- fn parse_defaults_to_ollama_when_file_missing_or_garbage() {- assert!(matches!(parse_inference_config("").kind, SourceKind::Ollama));- assert!(matches!(parse_inference_config("not json").kind, SourceKind::Ollama));+ fn startup_requires_an_explicit_valid_source_config() {+ assert!(parse_configured_inference_config("").is_none());+ assert!(parse_configured_inference_config("not json").is_none());+ assert!(parse_configured_inference_config("{}").is_none());++ let ollama = parse_configured_inference_config(r#"{"kind":"ollama"}"#).unwrap();+ assert!(matches!(ollama.kind, SourceKind::Ollama));+ assert!(ollama.confirmed, "legacy explicit configs remain trusted");++ let custom = parse_configured_inference_config(+ r#"{"kind":"custom","model":"m","host":"http://localhost:1234"}"#,+ )+ .unwrap();+ assert!(matches!(custom.kind, SourceKind::Custom));+ assert!(custom.confirmed);++ let pending = parse_configured_inference_config(+ r#"{"kind":"custom","confirmed":false,"model":"m","host":"http://localhost:1234"}"#,+ )+ .unwrap();+ assert!(!pending.confirmed);+ }++ #[test]+ fn initial_setup_status_is_inert_until_a_source_is_chosen() {+ let status = SetupStatus::default();+ assert!(status.requires_source);+ assert_eq!(status.phase, "awaiting_source");+ assert_eq!(status.source, "unconfigured");+ assert!(!status.ollama_ready);+ assert!(!status.model_ready);+ assert!(!status.server_ready);+ }++ #[test]+ fn persisted_source_status_can_enter_startup() {+ let cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: true,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };+ let status = SetupStatus::starting(&cfg);+ assert!(!status.requires_source);+ assert_eq!(status.phase, "starting");+ assert_eq!(status.source, "custom");}#[test]fn parse_reads_custom_endpoint() {- let cfg = parse_inference_config(+ let cfg = parse_configured_inference_config(r#"{"kind":"custom","model":"qwen2.5-7b","host":"http://localhost:1234","engine":"lmstudio"}"#,- );+ )+ .unwrap();assert!(matches!(cfg.kind, SourceKind::Custom));assert_eq!(cfg.model.as_deref(), Some("qwen2.5-7b"));assert_eq!(cfg.host.as_deref(), Some("http://localhost:1234"));@@ -3108,21 +3765,39 @@ mod tests {#[test]fn normalize_host_strips_trailing_slash_and_v1() {- assert_eq!(normalize_host("http://localhost:1234/v1"), "http://localhost:1234");- assert_eq!(normalize_host("http://localhost:1234/v1/"), "http://localhost:1234");- assert_eq!(normalize_host("http://localhost:1234/"), "http://localhost:1234");+ assert_eq!(+ normalize_host("http://localhost:1234/v1"),+ "http://localhost:1234"+ );+ assert_eq!(+ normalize_host("http://localhost:1234/v1/"),+ "http://localhost:1234"+ );+ assert_eq!(+ normalize_host("http://localhost:1234/"),+ "http://localhost:1234"+ );assert_eq!(normalize_host("http://host:8000"), "http://host:8000");}#[test]fn boot_plan_ollama_launches_and_pulls_one_model() {- let cfg = InferenceConfig { kind: SourceKind::Ollama, ..Default::default() };+ let cfg = InferenceConfig {+ kind: SourceKind::Ollama,+ ..Default::default()+ };let plan = boot_plan(&cfg, 16.0);assert!(plan.launch_ollama);assert_eq!(plan.model_to_pull.as_deref(), Some("qwen3.5:4b"));assert!(plan.engine_host.is_none());- assert!(plan.serve_args.windows(2).any(|w| w == ["--engine", "ollama"]));- assert!(plan.serve_args.windows(2).any(|w| w == ["--model", "qwen3.5:4b"]));+ assert!(plan+ .serve_args+ .windows(2)+ .any(|w| w == ["--engine", "ollama"]));+ assert!(plan+ .serve_args+ .windows(2)+ .any(|w| w == ["--model", "qwen3.5:4b"]));}#[test]@@ -3140,6 +3815,7 @@ mod tests {fn boot_plan_custom_skips_ollama_and_sets_engine_host() {let cfg = InferenceConfig {kind: SourceKind::Custom,+ confirmed: true,model: Some("qwen2.5-7b".into()),host: Some("http://localhost:1234".into()),engine: Some("lmstudio".into()),@@ -3151,21 +3827,31 @@ mod tests {plan.engine_host,Some(("lmstudio".to_string(), "http://localhost:1234".to_string())));- assert!(plan.serve_args.windows(2).any(|w| w == ["--engine", "lmstudio"]));- assert!(plan.serve_args.windows(2).any(|w| w == ["--model", "qwen2.5-7b"]));+ assert!(plan+ .serve_args+ .windows(2)+ .any(|w| w == ["--engine", "lmstudio"]));+ assert!(plan+ .serve_args+ .windows(2)+ .any(|w| w == ["--model", "qwen2.5-7b"]));}#[test]fn boot_plan_custom_defaults_engine_to_lmstudio() {let cfg = InferenceConfig {kind: SourceKind::Custom,+ confirmed: true,model: Some("m".into()),host: Some("http://h:1".into()),engine: None,};let plan = boot_plan(&cfg, 16.0);assert_eq!(plan.engine_host.as_ref().unwrap().0, "lmstudio");- assert!(plan.serve_args.windows(2).any(|w| w == ["--engine", "lmstudio"]));+ assert!(plan+ .serve_args+ .windows(2)+ .any(|w| w == ["--engine", "lmstudio"]));}#[test]@@ -3173,6 +3859,7 @@ mod tests {// No configured host → don't set engine_host (no override to write).let cfg = InferenceConfig {kind: SourceKind::Custom,+ confirmed: true,model: Some("m".into()),host: None,engine: Some("lmstudio".into()),@@ -3181,10 +3868,379 @@ mod tests {assert!(plan.engine_host.is_none());}+ #[test]+ fn pending_config_rolls_back_unless_boot_confirms_it() {+ let root = std::env::temp_dir().join(format!(+ "openjarvis-pending-inference-{}",+ std::process::id()+ ));+ std::fs::create_dir_all(&root).unwrap();+ let pending_path = root.join("pending.json");+ std::fs::write(&pending_path, "pending").unwrap();+ let pending = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: false,+ model: Some("m".into()),+ host: Some("http://localhost:1".into()),+ engine: Some("lmstudio".into()),+ };++ {+ let _rollback = PendingInferenceRollback::at_path(&pending, pending_path.clone());+ }+ assert!(!pending_path.exists());++ let confirmed_path = root.join("confirmed.json");+ std::fs::write(&confirmed_path, "confirmed").unwrap();+ let mut confirmed = pending;+ confirmed.confirmed = true;+ {+ let _rollback = PendingInferenceRollback::at_path(&confirmed, confirmed_path.clone());+ }+ assert!(confirmed_path.exists());++ let committed_path = root.join("committed.json");+ std::fs::write(&committed_path, "pending").unwrap();+ let mut staged = InferenceConfig {+ kind: SourceKind::Ollama,+ confirmed: false,+ model: Some("qwen3.5:4b".into()),+ host: None,+ engine: None,+ };+ {+ let mut rollback = PendingInferenceRollback::at_path(&staged, committed_path.clone());+ rollback.confirm(&mut staged).unwrap();+ }+ assert!(staged.confirmed);+ assert!(committed_path.exists());+ let committed: InferenceConfig =+ serde_json::from_str(&std::fs::read_to_string(&committed_path).unwrap()).unwrap();+ assert!(committed.confirmed);+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn pending_custom_key_is_staged_without_overwriting_existing_key() {+ let root = unique_test_root("stage-inference-key");+ std::fs::create_dir_all(&root).unwrap();+ let path = root.join("inference.json");+ let store = MemoryCredentialStore::default();+ store.put("LMSTUDIO_API_KEY", "existing-key");+ let cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: false,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };++ stage_pending_inference_config_at(&cfg, Some("new-key"), &store, &path).unwrap();++ assert_eq!(+ store.value("LMSTUDIO_API_KEY").as_deref(),+ Some("existing-key")+ );+ assert_eq!(+ store.value(PENDING_INFERENCE_API_KEY).as_deref(),+ Some("new-key")+ );+ let staged: InferenceConfig =+ serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();+ assert!(!staged.confirmed);+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn cancelled_pending_setup_discards_stage_and_preserves_existing_key() {+ let root = unique_test_root("cancel-inference-key");+ std::fs::create_dir_all(&root).unwrap();+ let path = root.join("inference.json");+ let store = std::sync::Arc::new(MemoryCredentialStore::default());+ store.put("LMSTUDIO_API_KEY", "existing-key");+ let cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: false,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };+ stage_pending_inference_config_at(&cfg, Some("new-key"), store.as_ref(), &path).unwrap();++ {+ let _rollback =+ PendingInferenceRollback::at_path_with_store(&cfg, path.clone(), store.clone());+ }++ assert!(!path.exists());+ assert_eq!(store.value(PENDING_INFERENCE_API_KEY), None);+ assert_eq!(+ store.value("LMSTUDIO_API_KEY").as_deref(),+ Some("existing-key")+ );+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn successful_pending_setup_promotes_stage_only_at_confirmation() {+ let root = unique_test_root("confirm-inference-key");+ std::fs::create_dir_all(&root).unwrap();+ let path = root.join("inference.json");+ let store = std::sync::Arc::new(MemoryCredentialStore::default());+ store.put("LMSTUDIO_API_KEY", "existing-key");+ let mut cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: false,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };+ stage_pending_inference_config_at(&cfg, Some("new-key"), store.as_ref(), &path).unwrap();++ {+ let mut rollback =+ PendingInferenceRollback::at_path_with_store(&cfg, path.clone(), store.clone());+ rollback.confirm(&mut cfg).unwrap();+ }++ assert!(cfg.confirmed);+ assert_eq!(store.value(PENDING_INFERENCE_API_KEY), None);+ assert_eq!(store.value("LMSTUDIO_API_KEY").as_deref(), Some("new-key"));+ let confirmed: InferenceConfig =+ serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();+ assert!(confirmed.confirmed);+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn failed_confirmation_restores_existing_key_and_leaves_no_stage() {+ let root = unique_test_root("failed-confirm-inference-key");+ let path = root.join("config-is-a-directory");+ std::fs::create_dir_all(&path).unwrap();+ let store = std::sync::Arc::new(MemoryCredentialStore::default());+ store.put("LMSTUDIO_API_KEY", "existing-key");+ store.put(PENDING_INFERENCE_API_KEY, "new-key");+ let mut cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: false,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };++ let result = {+ let mut rollback =+ PendingInferenceRollback::at_path_with_store(&cfg, path.clone(), store.clone());+ rollback.confirm(&mut cfg)+ };++ assert!(result.is_err());+ assert!(!cfg.confirmed);+ assert_eq!(store.value(PENDING_INFERENCE_API_KEY), None);+ assert_eq!(+ store.value("LMSTUDIO_API_KEY").as_deref(),+ Some("existing-key")+ );+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn failed_pending_config_write_discards_new_stage() {+ let root = unique_test_root("failed-stage-inference-key");+ let path = root.join("config-is-a-directory");+ std::fs::create_dir_all(&path).unwrap();+ let store = MemoryCredentialStore::default();+ store.put("LMSTUDIO_API_KEY", "existing-key");+ let cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: false,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };++ let result = stage_pending_inference_config_at(&cfg, Some("new-key"), &store, &path);++ assert!(result.is_err());+ assert_eq!(store.value(PENDING_INFERENCE_API_KEY), None);+ assert_eq!(+ store.value("LMSTUDIO_API_KEY").as_deref(),+ Some("existing-key")+ );+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn failed_confirmed_config_write_restores_existing_key() {+ let root = unique_test_root("failed-confirmed-inference-key");+ let path = root.join("config-is-a-directory");+ std::fs::create_dir_all(&path).unwrap();+ let store = MemoryCredentialStore::default();+ store.put("LMSTUDIO_API_KEY", "existing-key");+ let cfg = InferenceConfig {+ kind: SourceKind::Custom,+ confirmed: true,+ model: Some("m".into()),+ host: Some("http://localhost:1234".into()),+ engine: Some("lmstudio".into()),+ };++ let result = persist_confirmed_inference_config_at(&cfg, Some("new-key"), &store, &path);++ assert!(result.is_err());+ assert_eq!(+ store.value("LMSTUDIO_API_KEY").as_deref(),+ Some("existing-key")+ );+ std::fs::remove_dir_all(root).unwrap();+ }++ #[test]+ fn reset_discards_pending_config_and_stage_without_touching_existing_key() {+ let root = unique_test_root("reset-inference-key");+ std::fs::create_dir_all(&root).unwrap();+ let path = root.join("inference.json");+ std::fs::write(&path, "pending").unwrap();+ let store = MemoryCredentialStore::default();+ store.put("LMSTUDIO_API_KEY", "existing-key");+ store.put(PENDING_INFERENCE_API_KEY, "new-key");++ discard_pending_inference_setup_at(&path, &store).unwrap();++ assert!(!path.exists());+ assert_eq!(store.value(PENDING_INFERENCE_API_KEY), None);+ assert_eq!(+ store.value("LMSTUDIO_API_KEY").as_deref(),+ Some("existing-key")+ );+ std::fs::remove_dir_all(root).unwrap();+ }++ #[tokio::test]+ async fn ready_port_squatter_never_receives_cloud_key_without_owned_child() {+ let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();+ listener.set_nonblocking(true).unwrap();+ let reload_url = format!("http://{}/v1/cloud/reload", listener.local_addr().unwrap());+ let backend = std::sync::Arc::new(tokio::sync::Mutex::new(BackendManager::default()));+ let mut setup_status = SetupStatus::default();+ setup_status.server_ready = true;+ let status = std::sync::Arc::new(tokio::sync::Mutex::new(setup_status));++ reload_cloud_keys_for_owned_backend_at(+ &backend,+ &status,+ vec![("OPENAI_API_KEY".into(), "must-not-leak".into())],+ &reload_url,+ )+ .await;++ let error = listener.accept().unwrap_err();+ assert_eq!(error.kind(), std::io::ErrorKind::WouldBlock);+ }++ #[tokio::test]+ async fn stopping_backend_aborts_the_tracked_boot_task() {+ use std::sync::atomic::{AtomicBool, Ordering};+ use std::sync::Arc;++ struct DropSignal(Arc<AtomicBool>);+ impl Drop for DropSignal {+ fn drop(&mut self) {+ self.0.store(true, Ordering::SeqCst);+ }+ }++ let dropped = Arc::new(AtomicBool::new(false));+ let signal = DropSignal(dropped.clone());+ let task = tokio::spawn(async move {+ let _signal = signal;+ std::future::pending::<()>().await;+ });+ tokio::task::yield_now().await;++ let mut manager = BackendManager::default();+ manager.boot_task = Some(tauri::async_runtime::JoinHandle::Tokio(task));+ manager.stop_all().await;++ assert!(manager.boot_task.is_none());+ assert!(+ dropped.load(Ordering::SeqCst),+ "stop_all must not return before the aborted boot future is dropped"+ );+ }++ #[test]+ fn owned_child_process_helper() {+ let Some(started) = std::env::var_os("OPENJARVIS_OWNED_CHILD_STARTED") else {+ return;+ };+ let completed = std::env::var_os("OPENJARVIS_OWNED_CHILD_COMPLETED").unwrap();+ std::fs::write(started, "started").unwrap();+ std::thread::sleep(std::time::Duration::from_millis(500));+ std::fs::write(completed, "completed").unwrap();+ }++ #[tokio::test]+ async fn aborting_boot_kills_child_before_manager_registration() {+ let unique = std::time::SystemTime::now()+ .duration_since(std::time::UNIX_EPOCH)+ .unwrap()+ .as_nanos();+ let root = std::env::temp_dir().join(format!(+ "openjarvis-owned-child-{}-{unique}",+ std::process::id()+ ));+ std::fs::create_dir_all(&root).unwrap();+ let started = root.join("started");+ let completed = root.join("completed");++ let (spawned_tx, spawned_rx) = tokio::sync::oneshot::channel();+ let started_for_child = started.clone();+ let completed_for_child = completed.clone();+ let task = tokio::spawn(async move {+ let mut cmd = tokio::process::Command::new(std::env::current_exe().unwrap());+ cmd.args([+ "--exact",+ "tests::owned_child_process_helper",+ "--nocapture",+ ])+ .env("OPENJARVIS_OWNED_CHILD_STARTED", started_for_child)+ .env("OPENJARVIS_OWNED_CHILD_COMPLETED", completed_for_child)+ .stdout(std::process::Stdio::null())+ .stderr(std::process::Stdio::null());+ let child = spawn_owned_child(&mut cmd).unwrap();+ spawned_tx.send(()).unwrap();+ let _unregistered_child = child;+ std::future::pending::<()>().await;+ });+ spawned_rx.await.unwrap();++ for _ in 0..100 {+ if started.exists() {+ break;+ }+ tokio::time::sleep(std::time::Duration::from_millis(10)).await;+ }+ assert!(started.exists(), "helper process did not start");++ task.abort();+ let _ = task.await;+ tokio::time::sleep(std::time::Duration::from_millis(700)).await;++ assert!(+ !completed.exists(),+ "an aborted boot task left its not-yet-registered child running"+ );+ std::fs::remove_dir_all(root).unwrap();+ }+#[test]fn boot_plan_ollama_uses_fallback_model_on_low_ram() {// Below the smallest model's min_ram → default_local_model → FALLBACK_MODEL.- let cfg = InferenceConfig { kind: SourceKind::Ollama, ..Default::default() };+ let cfg = InferenceConfig {+ kind: SourceKind::Ollama,+ ..Default::default()+ };let plan = boot_plan(&cfg, 1.0);assert_eq!(plan.model_to_pull.as_deref(), Some(super::FALLBACK_MODEL));}@@ -3204,8 +4260,14 @@ mod tests {let existing = "[intelligence]\ndefault_model = \"keep-me\"\n";let out = upsert_engine_host(existing, "vllm", "http://host:8000").unwrap();let doc: toml_edit::DocumentMut = out.parse().unwrap();- assert_eq!(doc["intelligence"]["default_model"].as_str(), Some("keep-me"));- assert_eq!(doc["engine"]["vllm"]["host"].as_str(), Some("http://host:8000"));+ assert_eq!(+ doc["intelligence"]["default_model"].as_str(),+ Some("keep-me")+ );+ assert_eq!(+ doc["engine"]["vllm"]["host"].as_str(),+ Some("http://host:8000")+ );}#[test]@@ -3213,7 +4275,10 @@ mod tests {let existing = "[engine.lmstudio]\nhost = \"http://old:1\"\n";let out = upsert_engine_host(existing, "lmstudio", "http://new:2").unwrap();let doc: toml_edit::DocumentMut = out.parse().unwrap();- assert_eq!(doc["engine"]["lmstudio"]["host"].as_str(), Some("http://new:2"));+ assert_eq!(+ doc["engine"]["lmstudio"]["host"].as_str(),+ Some("http://new:2")+ );}// -----------------------------------------------------------------diff --git a/frontend/src/components/InferenceSourceSetup.test.tsx b/frontend/src/components/InferenceSourceSetup.test.tsxnew file mode 100644index 00000000..81b4e9e2--- /dev/null+++ b/frontend/src/components/InferenceSourceSetup.test.tsx@@ -0,0 +1,133 @@+import { renderToStaticMarkup } from 'react-dom/server';+import { describe, expect, it, vi } from 'vitest';+import {+ CustomEndpointSetup,+ InferenceRecoveryButton,+ InferenceSourceChooser,+ OllamaConsent,+ inferenceSetupReducer,+ persistAndStartInferenceSource,+ type InferenceSourceSubmission,+} from './InferenceSourceSetup';++describe('first-run inference source navigation', () => {+ it('requires a second, explicit confirmation before local setup', () => {+ expect(inferenceSetupReducer('choose', 'choose_ollama')).toBe('ollama');+ expect(inferenceSetupReducer('ollama', 'back')).toBe('choose');+ });++ it('lets custom endpoint setup cancel safely back to the chooser', () => {+ expect(inferenceSetupReducer('choose', 'choose_custom')).toBe('custom');+ expect(inferenceSetupReducer('custom', 'back')).toBe('choose');+ });++ it('renders both sources without claiming either has started', () => {+ const html = renderToStaticMarkup(<InferenceSourceChooser onChoose={vi.fn()} />);++ expect(html).toContain('Local Ollama');+ expect(html).toContain('OpenAI-compatible server');+ expect(html).toContain('Nothing is installed, started, or downloaded until you confirm');+ expect(html).not.toContain('Starting local setup');+ });++ it('discloses the model download and offers a no-op back path', () => {+ const html = renderToStaticMarkup(+ <OllamaConsent onBack={vi.fn()} onConfirm={vi.fn()} />,+ );++ expect(html).toContain('may download one model');+ expect(html).toContain('several gigabytes');+ expect(html).toContain('Going back leaves Ollama stopped');+ expect(html).toContain('Use Ollama and continue');+ expect(html).toContain('Back');+ });++ it('requires endpoint coordinates and keeps an explicit cancel control', () => {+ const html = renderToStaticMarkup(+ <CustomEndpointSetup onCancel={vi.fn()} onConfirm={vi.fn()} />,+ );++ expect(html).toContain('Server URL');+ expect(html).toContain('Model ID');+ expect(html).toMatch(/aria-label="Server URL"[^>]*required/);+ expect(html).toMatch(/aria-label="Model ID"[^>]*required/);+ expect(html).toContain('Ollama will not start or download models');+ expect(html).toContain('Cancel');+ });++ it('renders a source-change recovery action with a guarded busy state', () => {+ const ready = renderToStaticMarkup(+ <InferenceRecoveryButton recovering={false} onChange={vi.fn()} />,+ );+ const stopping = renderToStaticMarkup(+ <InferenceRecoveryButton recovering onChange={vi.fn()} />,+ );++ expect(ready).toContain('Change inference source');+ expect(ready).not.toMatch(/ disabled(?:=""|>)/);+ expect(stopping).toContain('Stopping setup...');+ expect(stopping).toMatch(/ disabled(?:=""|>)/);+ });+});++describe('first-run inference source persistence', () => {+ it('persists the exact choice before requesting backend startup', async () => {+ const calls: string[] = [];+ const persist = vi.fn(async (source: InferenceSourceSubmission) => {+ calls.push(`persist:${source.kind}`);+ });+ const start = vi.fn(async () => {+ calls.push('start');+ });++ await persistAndStartInferenceSource(+ { kind: 'custom', host: 'http://localhost:1234', model: 'local-model', engine: 'lmstudio' },+ persist,+ start,+ );++ expect(calls).toEqual(['persist:custom', 'start']);+ expect(persist).toHaveBeenCalledWith({+ kind: 'custom',+ host: 'http://localhost:1234',+ model: 'local-model',+ engine: 'lmstudio',+ });+ });++ it('does not start anything when persistence fails', async () => {+ const persist = vi.fn(async () => {+ throw new Error('secure storage unavailable');+ });+ const start = vi.fn(async () => {});++ await expect(+ persistAndStartInferenceSource({ kind: 'ollama' }, persist, start),+ ).rejects.toThrow('secure storage unavailable');+ expect(start).not.toHaveBeenCalled();+ });++ it('rolls a staged choice back when startup rejects', async () => {+ const calls: string[] = [];+ const persist = vi.fn(async () => {+ calls.push('persist');+ });+ const start = vi.fn(async () => {+ calls.push('start');+ throw new Error('endpoint unavailable');+ });+ const rollback = vi.fn(async () => {+ calls.push('rollback');+ });++ await expect(+ persistAndStartInferenceSource(+ { kind: 'custom', host: 'http://localhost:1234', model: 'missing' },+ persist,+ start,+ rollback,+ ),+ ).rejects.toThrow('endpoint unavailable');+ expect(calls).toEqual(['persist', 'start', 'rollback']);+ });+});diff --git a/frontend/src/components/InferenceSourceSetup.tsx b/frontend/src/components/InferenceSourceSetup.tsxnew file mode 100644index 00000000..b77e8840--- /dev/null+++ b/frontend/src/components/InferenceSourceSetup.tsx@@ -0,0 +1,385 @@+import { useReducer, useState, type FormEvent } from 'react';+import { ArrowLeft, Cpu, Download, Loader2, Server, ShieldCheck } from 'lucide-react';+import {+ resetInferenceSource,+ stageInferenceSource,+ startBackend,+ type InferenceSource,+} from '../lib/api';++export type InferenceSetupStep = 'choose' | 'ollama' | 'custom';+export type InferenceSetupAction = 'choose_ollama' | 'choose_custom' | 'back';+export type InferenceSourceSubmission = InferenceSource & { apiKey?: string };++/** Pure navigation state: merely inspecting a source can never start setup. */+export function inferenceSetupReducer(+ state: InferenceSetupStep,+ action: InferenceSetupAction,+): InferenceSetupStep {+ switch (action) {+ case 'choose_ollama':+ return 'ollama';+ case 'choose_custom':+ return 'custom';+ case 'back':+ return 'choose';+ default:+ return state;+ }+}++/**+ * Persist first, start second. Dependency injection keeps the consent boundary+ * directly testable without a Tauri runtime.+ */+export async function persistAndStartInferenceSource(+ source: InferenceSourceSubmission,+ persist: (value: InferenceSourceSubmission) => Promise<void> = stageInferenceSource,+ start: () => Promise<void> = startBackend,+ rollback: () => Promise<void> = resetInferenceSource,+): Promise<void> {+ let persisted = false;+ try {+ await persist(source);+ persisted = true;+ await start();+ } catch (error) {+ if (persisted) {+ await rollback().catch(() => {});+ }+ throw error;+ }+}++function SetupFrame({ children }: { children: React.ReactNode }) {+ return (+ <div className="fixed inset-0 flex items-center justify-center" style={{ background: 'var(--color-bg)' }}>+ <div className="w-full max-w-lg px-6">+ <div className="text-center mb-8">+ <div+ className="w-16 h-16 rounded-2xl flex items-center justify-center mx-auto mb-4"+ style={{ background: 'var(--color-accent-subtle)', color: 'var(--color-accent)' }}+ >+ <Cpu size={32} />+ </div>+ <h1 className="text-2xl font-bold mb-1" style={{ color: 'var(--color-text)' }}>+ Choose your inference source+ </h1>+ <p className="text-sm" style={{ color: 'var(--color-text-secondary)' }}>+ Nothing is installed, started, or downloaded until you confirm a choice.+ </p>+ </div>+ <div+ className="p-6 rounded-2xl"+ style={{ background: 'var(--color-bg-secondary)', border: '1px solid var(--color-border)' }}+ >+ {children}+ </div>+ </div>+ </div>+ );+}++export function InferenceSourceChooser({+ onChoose,+}: {+ onChoose: (source: 'ollama' | 'custom') => void;+}) {+ return (+ <SetupFrame>+ <div className="flex flex-col gap-3">+ <button+ type="button"+ onClick={() => onChoose('ollama')}+ className="flex items-start gap-4 p-4 rounded-xl text-left transition-all cursor-pointer"+ style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)' }}+ >+ <Cpu size={22} className="shrink-0 mt-0.5" style={{ color: 'var(--color-accent)' }} />+ <span>+ <span className="block text-sm font-semibold" style={{ color: 'var(--color-text)' }}>+ Local Ollama+ </span>+ <span className="block text-xs mt-1" style={{ color: 'var(--color-text-secondary)' }}>+ Run models on this computer. Requires a separate confirmation before Ollama starts or a model downloads.+ </span>+ </span>+ </button>+ <button+ type="button"+ onClick={() => onChoose('custom')}+ className="flex items-start gap-4 p-4 rounded-xl text-left transition-all cursor-pointer"+ style={{ background: 'var(--color-surface)', border: '1px solid var(--color-border)' }}+ >+ <Server size={22} className="shrink-0 mt-0.5" style={{ color: 'var(--color-accent)' }} />+ <span>+ <span className="block text-sm font-semibold" style={{ color: 'var(--color-text)' }}>+ OpenAI-compatible server+ </span>+ <span className="block text-xs mt-1" style={{ color: 'var(--color-text-secondary)' }}>+ Connect to LM Studio, vLLM, SGLang, llama.cpp, MLX, or another compatible endpoint. Ollama stays off.+ </span>+ </span>+ </button>+ </div>+ </SetupFrame>+ );+}++function BackButton({ onClick, label = 'Back', disabled = false }: { onClick: () => void; label?: string; disabled?: boolean }) {+ return (+ <button+ type="button"+ onClick={onClick}+ disabled={disabled}+ className="flex items-center gap-1.5 text-sm mb-5 cursor-pointer disabled:cursor-not-allowed disabled:opacity-50"+ style={{ color: 'var(--color-text-secondary)' }}+ >+ <ArrowLeft size={15} />+ {label}+ </button>+ );+}++export function InferenceRecoveryButton({+ recovering,+ onChange,+}: {+ recovering: boolean;+ onChange: () => void;+}) {+ return (+ <button+ type="button"+ onClick={onChange}+ disabled={recovering}+ className="w-full mt-4 py-2.5 px-4 rounded-xl text-sm font-medium cursor-pointer disabled:cursor-not-allowed disabled:opacity-60"+ style={{+ color: 'var(--color-text-secondary)',+ border: '1px solid var(--color-border)',+ background: 'var(--color-surface)',+ }}+ >+ {recovering ? 'Stopping setup...' : 'Change inference source'}+ </button>+ );+}++export function OllamaConsent({+ onBack,+ onConfirm,+ busy = false,+ error = '',+}: {+ onBack: () => void;+ onConfirm: () => void;+ busy?: boolean;+ error?: string;+}) {+ return (+ <SetupFrame>+ <BackButton onClick={onBack} disabled={busy} />+ <div className="flex items-start gap-3 mb-5">+ <Download size={22} className="shrink-0 mt-0.5" style={{ color: 'var(--color-accent)' }} />+ <div>+ <h2 className="text-base font-semibold mb-1" style={{ color: 'var(--color-text)' }}>+ Allow local model setup?+ </h2>+ <p className="text-sm leading-relaxed" style={{ color: 'var(--color-text-secondary)' }}>+ OpenJarvis will start Ollama and may download one model selected for this computer. Model downloads can use several gigabytes of disk space and network data.+ </p>+ </div>+ </div>+ <div+ className="flex items-start gap-2.5 px-3.5 py-3 rounded-lg text-xs mb-5"+ style={{ background: 'var(--color-accent-subtle)', color: 'var(--color-text-secondary)' }}+ >+ <ShieldCheck size={16} className="shrink-0" style={{ color: 'var(--color-accent)' }} />+ Going back leaves Ollama stopped and does not save this choice.+ </div>+ {error && <SetupError message={error} />}+ <button+ type="button"+ onClick={onConfirm}+ disabled={busy}+ className="w-full py-3 px-4 rounded-xl font-semibold text-sm flex items-center justify-center gap-2 cursor-pointer disabled:cursor-not-allowed disabled:opacity-70"+ style={{ background: 'var(--color-accent)', color: 'white' }}+ >+ {busy && <Loader2 size={16} className="animate-spin" />}+ {busy ? 'Starting local setup...' : 'Use Ollama and continue'}+ </button>+ </SetupFrame>+ );+}++function SetupError({ message }: { message: string }) {+ return (+ <div+ role="alert"+ className="px-3.5 py-3 rounded-lg text-sm mb-4"+ style={{+ background: 'color-mix(in srgb, var(--color-error) 10%, transparent)',+ border: '1px solid color-mix(in srgb, var(--color-error) 20%, transparent)',+ color: 'var(--color-error)',+ }}+ >+ {message}+ </div>+ );+}++export function CustomEndpointSetup({+ onCancel,+ onConfirm,+ busy = false,+ error = '',+}: {+ onCancel: () => void;+ onConfirm: (source: InferenceSourceSubmission) => void;+ busy?: boolean;+ error?: string;+}) {+ const [host, setHost] = useState('http://localhost:1234/v1');+ const [model, setModel] = useState('');+ const [engine, setEngine] = useState('lmstudio');+ const [apiKey, setApiKey] = useState('');++ const submit = (event: FormEvent) => {+ event.preventDefault();+ onConfirm({+ kind: 'custom',+ host: host.trim(),+ model: model.trim(),+ engine,+ apiKey: apiKey.trim() || undefined,+ });+ };++ const fieldStyle = {+ background: 'var(--color-surface)',+ color: 'var(--color-text)',+ border: '1px solid var(--color-border)',+ };++ return (+ <SetupFrame>+ <BackButton onClick={onCancel} label="Cancel" disabled={busy} />+ <form onSubmit={submit} className="flex flex-col gap-4">+ <div>+ <h2 className="text-base font-semibold mb-1" style={{ color: 'var(--color-text)' }}>+ Connect your server+ </h2>+ <p className="text-xs" style={{ color: 'var(--color-text-secondary)' }}>+ OpenJarvis will connect only after you submit this form. Ollama will not start or download models.+ </p>+ </div>+ <label className="text-xs font-medium" style={{ color: 'var(--color-text-secondary)' }}>+ Server URL+ <input+ aria-label="Server URL"+ type="url"+ required+ value={host}+ onChange={(event) => setHost(event.target.value)}+ className="block w-full mt-1.5 px-3 py-2 rounded-lg text-sm outline-none"+ style={fieldStyle}+ />+ </label>+ <label className="text-xs font-medium" style={{ color: 'var(--color-text-secondary)' }}>+ Model ID+ <input+ aria-label="Model ID"+ type="text"+ required+ value={model}+ onChange={(event) => setModel(event.target.value)}+ placeholder="qwen2.5-7b-instruct"+ className="block w-full mt-1.5 px-3 py-2 rounded-lg text-sm outline-none"+ style={fieldStyle}+ />+ </label>+ <label className="text-xs font-medium" style={{ color: 'var(--color-text-secondary)' }}>+ Server type+ <select+ aria-label="Server type"+ value={engine}+ onChange={(event) => setEngine(event.target.value)}+ className="block w-full mt-1.5 px-3 py-2 rounded-lg text-sm outline-none"+ style={fieldStyle}+ >+ <option value="lmstudio">LM Studio</option>+ <option value="vllm">vLLM</option>+ <option value="sglang">SGLang</option>+ <option value="llamacpp">llama.cpp</option>+ <option value="mlx">MLX</option>+ </select>+ </label>+ <label className="text-xs font-medium" style={{ color: 'var(--color-text-secondary)' }}>+ API key (optional)+ <input+ aria-label="API key"+ type="password"+ value={apiKey}+ onChange={(event) => setApiKey(event.target.value)}+ autoComplete="off"+ className="block w-full mt-1.5 px-3 py-2 rounded-lg text-sm outline-none"+ style={fieldStyle}+ />+ </label>+ {error && <SetupError message={error} />}+ <button+ type="submit"+ disabled={busy || !host.trim() || !model.trim()}+ className="w-full py-3 px-4 rounded-xl font-semibold text-sm flex items-center justify-center gap-2 cursor-pointer disabled:cursor-not-allowed disabled:opacity-50"+ style={{ background: 'var(--color-accent)', color: 'white' }}+ >+ {busy && <Loader2 size={16} className="animate-spin" />}+ {busy ? 'Connecting...' : 'Save and connect'}+ </button>+ </form>+ </SetupFrame>+ );+}++export function InferenceSourceSetup({ onStarted }: { onStarted: () => void }) {+ const [step, dispatch] = useReducer(inferenceSetupReducer, 'choose');+ const [busy, setBusy] = useState(false);+ const [error, setError] = useState('');++ const begin = async (source: InferenceSourceSubmission) => {+ setBusy(true);+ setError('');+ try {+ await persistAndStartInferenceSource(source);+ onStarted();+ } catch (err) {+ setError(err instanceof Error ? err.message : String(err));+ setBusy(false);+ }+ };++ if (step === 'ollama') {+ return (+ <OllamaConsent+ busy={busy}+ error={error}+ onBack={() => dispatch('back')}+ onConfirm={() => void begin({ kind: 'ollama' })}+ />+ );+ }+ if (step === 'custom') {+ return (+ <CustomEndpointSetup+ busy={busy}+ error={error}+ onCancel={() => dispatch('back')}+ onConfirm={(source) => void begin(source)}+ />+ );+ }+ return (+ <InferenceSourceChooser+ onChoose={(source) => dispatch(source === 'ollama' ? 'choose_ollama' : 'choose_custom')}+ />+ );+}diff --git a/frontend/src/components/SetupScreen.tsx b/frontend/src/components/SetupScreen.tsxindex eeffd1b1..1ea468c1 100644--- a/frontend/src/components/SetupScreen.tsx+++ b/frontend/src/components/SetupScreen.tsx@@ -4,10 +4,12 @@ import {getSetupStatus,fetchModels,fetchRecommendedModel,+ resetInferenceSource,type SetupStatus,} from '../lib/api';import { useAppStore } from '../lib/store';import { isEmbedOnlyModel } from '../lib/model-capabilities';+import { InferenceRecoveryButton, InferenceSourceSetup } from './InferenceSourceSetup';const STEPS = [{ key: 'ollama_ready', label: 'Inference Engine', icon: Cpu, detail: 'Starting Ollama...' },@@ -77,10 +79,15 @@ function StepRow({export function SetupScreen({ onReady }: { onReady: () => void }) {const [status, setStatus] = useState<SetupStatus | null>(null);+ const [statusChecked, setStatusChecked] = useState(false);+ const [setupInitiated, setSetupInitiated] = useState(false);+ const [recovering, setRecovering] = useState(false);+ const [recoveryError, setRecoveryError] = useState('');const handedOffRef = useRef(false);const poll = useCallback(async () => {const s = await getSetupStatus();if (s) setStatus(s);+ setStatusChecked(true);if (s?.phase === 'ready' && !handedOffRef.current) {handedOffRef.current = true;// Pre-select a model BEFORE handing off so the chat is usable on@@ -110,12 +117,39 @@ export function SetupScreen({ onReady }: { onReady: () => void }) {}}, [onReady]);+ const changeInferenceSource = useCallback(async () => {+ setRecovering(true);+ setRecoveryError('');+ try {+ await resetInferenceSource();+ handedOffRef.current = false;+ setSetupInitiated(false);+ setStatus(null);+ await poll();+ } catch (error) {+ setRecoveryError(error instanceof Error ? error.message : String(error));+ } finally {+ setRecovering(false);+ }+ }, [poll]);+useEffect(() => {poll();const interval = setInterval(poll, 800);return () => clearInterval(interval);}, [poll]);+ if (statusChecked && status?.requires_source && !setupInitiated) {+ return (+ <InferenceSourceSetup+ onStarted={() => {+ setSetupInitiated(true);+ void poll();+ }}+ />+ );+ }+const activeStep: StepKey | null =status && !status.ollama_ready? 'ollama_ready'@@ -143,7 +177,11 @@ export function SetupScreen({ onReady }: { onReady: () => void }) {OpenJarvis</h1><p className="text-sm" style={{ color: 'var(--color-text-secondary)' }}>- Setting up your local AI...+ {!statusChecked+ ? 'Checking your saved setup...'+ : status?.source === 'custom'+ ? 'Connecting to your AI server...'+ : 'Setting up your local AI...'}</p></div>@@ -187,6 +225,23 @@ export function SetupScreen({ onReady }: { onReady: () => void }) {</div>)}+ {recoveryError && (+ <div+ role="alert"+ className="mt-3 px-4 py-3 rounded-xl text-sm"+ style={{ color: 'var(--color-error)' }}+ >+ {recoveryError}+ </div>+ )}++ {statusChecked && status && !status.requires_source && status.phase !== 'ready' && (+ <InferenceRecoveryButton+ recovering={recovering}+ onChange={() => void changeInferenceSource()}+ />+ )}+{/* Progress bar */}{!status?.error && (<divdiff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.tsindex fa6768fa..0cf9548e 100644--- a/frontend/src/lib/api.ts+++ b/frontend/src/lib/api.ts@@ -137,7 +137,8 @@ export interface SetupStatus {server_ready: boolean;model_ready: boolean;error: string | null;- source?: 'ollama' | 'custom'; // drives source-aware setup labels+ source: 'unconfigured' | 'ollama' | 'custom';+ requires_source: boolean;}export async function getSetupStatus(): Promise<SetupStatus | null> {@@ -150,6 +151,28 @@ export async function getSetupStatus(): Promise<SetupStatus | null> {}}+/** Start desktop services after an inference source has been persisted. */+export async function startBackend(): Promise<void> {+ if (!isTauri()) throw new Error('The desktop backend is available in the desktop app only.');+ try {+ const { invoke } = await import('@tauri-apps/api/core');+ await invoke<void>('start_backend');+ } catch (e: any) {+ throw new Error(e?.message ?? e ?? 'Failed to start the desktop backend');+ }+}++/** Stop an in-flight setup and return the desktop to its inert source chooser. */+export async function resetInferenceSource(): Promise<void> {+ if (!isTauri()) throw new Error('Inference setup recovery is available in the desktop app only.');+ try {+ const { invoke } = await import('@tauri-apps/api/core');+ await invoke<void>('reset_inference_source');+ } catch (e: any) {+ throw new Error(e?.message ?? e ?? 'Failed to reset inference setup');+ }+}+// ---------------------------------------------------------------------------// API functions// ---------------------------------------------------------------------------@@ -1128,6 +1151,7 @@ export async function getInferenceSource(): Promise<InferenceSource> {export async function setInferenceSource(src: InferenceSource & { apiKey?: string },+ options: { pending?: boolean } = {},): Promise<void> {if (!isTauri()) throw new Error('Inference source is configurable in the desktop app only.');try {@@ -1138,6 +1162,7 @@ export async function setInferenceSource(host: src.host ?? null,engine: src.engine ?? null,apiKey: src.apiKey ?? null,+ pending: options.pending ?? null,});} catch (e: any) {// Surface the backend's actionable error strings (e.g. "A server URL is@@ -1145,3 +1170,8 @@ export async function setInferenceSource(throw new Error(e?.message ?? e ?? 'Failed to save inference source');}}++/** Stage a first-run choice; Rust confirms it only after backend readiness. */+export const stageInferenceSource = (+ src: InferenceSource & { apiKey?: string },+): Promise<void> => setInferenceSource(src, { pending: true });