fix: persist model name to .env so dotted names survive restart (#426)

* fix: persist model name to .env so dotted names survive restart (#400)

The setup wizard saved selected_model to the DB but not to .env.
Since Config::from_env_with_toml() runs before the DB connects, the
model name was lost on restart -- backends fell back to hardcoded
defaults, truncating names like "llama3.2" to "llama3".

- Add LlmBackend::model_env_var() as single source of truth for the
  backend-to-env-var mapping
- Write the model env var in write_bootstrap_env() using the new method
- Add selected_model fallback to all 6 backends (was missing from
  OpenAI, Anthropic, Ollama, and Tinfoil)

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* refactor: extract resolve_model() helper to reduce duplication

Address review feedback: the env → settings → default model resolution
pattern was repeated across all 6 backends.  Centralise it in a single
LlmConfig::resolve_model() helper.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
This commit is contained in:
Zaki Manian
2026-03-01 08:32:59 +00:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 914f3cd075
commit f62937d482
2 changed files with 125 additions and 11 deletions
+14
View File
@@ -2040,6 +2040,20 @@ impl SetupWizard {
env_vars.push(("OLLAMA_BASE_URL", url.clone()));
}
// Model name: same chicken-and-egg — Config::from_env() resolves the
// model before the DB is connected, so we must persist it to .env.
// Write the backend-specific env var so the correct resolution path
// picks it up.
if let Some(ref model) = self.settings.selected_model {
let backend: crate::config::LlmBackend = self
.settings
.llm_backend
.as_deref()
.and_then(|s| s.parse().ok())
.unwrap_or_default();
env_vars.push((backend.model_env_var(), model.clone()));
}
// Preserve NEARAI_API_KEY if present (set by API key auth flow)
if let Ok(api_key) = std::env::var("NEARAI_API_KEY")
&& !api_key.is_empty()