YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: merge pi models.json instead of rebuilding from scratch
Louis Beaumontba1ae2796759
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rsindex 125f80000..282eb3110 100644--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs@@ -668,11 +668,11 @@ fn default_max_tokens() -> i32 {4096}-/// Build the models.json content for pi-coding-agent.+/// Build the providers to add/update in models.json for pi-coding-agent.///-/// Pure function (no I/O) — builds a clean JSON value from scratch with only the-/// providers we manage. This prevents stale/schema-invalid providers left over from-/// older versions from poisoning pi-coding-agent's Ajv validation.+/// Returns a map of provider entries to merge into the existing models.json.+/// We merge instead of rebuilding from scratch to avoid a race condition where+/// concurrent pipes overwrite each other's providers.fn build_models_json(user_token: Option<&str>,provider_config: Option<&PiProviderConfig>,@@ -769,9 +769,30 @@ fn ensure_pi_config(std::fs::create_dir_all(&config_dir).map_err(|e| format!("Failed to create pi config dir: {}", e))?;- let models_config = build_models_json(user_token, provider_config);+ let new_providers = build_models_json(user_token, provider_config);+ // Merge into existing models.json to avoid race conditions with concurrent pipeslet models_path = config_dir.join("models.json");+ let mut models_config: serde_json::Value = if models_path.exists() {+ let content = std::fs::read_to_string(&models_path).unwrap_or_default();+ serde_json::from_str(&content).unwrap_or_else(|_| json!({"providers": {}}))+ } else {+ json!({"providers": {}})+ };+ if !models_config.get("providers").and_then(|p| p.as_object()).is_some() {+ models_config = json!({"providers": {}});+ }++ // Merge new providers into existing ones (add/update, don't remove others)+ if let (Some(existing), Some(new)) = (+ models_config.get_mut("providers").and_then(|p| p.as_object_mut()),+ new_providers.get("providers").and_then(|p| p.as_object()),+ ) {+ for (k, v) in new {+ existing.insert(k.clone(), v.clone());+ }+ }+let models_str = serde_json::to_string_pretty(&models_config).map_err(|e| format!("Failed to serialize models config: {}", e))?;std::fs::write(&models_path, models_str)diff --git a/crates/screenpipe-core/src/agents/pi.rs b/crates/screenpipe-core/src/agents/pi.rsindex 151d6ca2d..c39533773 100644--- a/crates/screenpipe-core/src/agents/pi.rs+++ b/crates/screenpipe-core/src/agents/pi.rs@@ -273,12 +273,27 @@ impl PiExecutor {let config_dir = get_pi_config_dir()?;std::fs::create_dir_all(&config_dir)?;- // -- models.json: build clean config with only managed providers --- // Writing from scratch prevents stale/schema-invalid providers left over- // from older versions from poisoning pi-coding-agent's Ajv validation,- // which would reject the entire file and hide all custom providers.+ // -- models.json: merge our provider into existing config --+ // We read the existing file and merge to avoid a race condition where+ // concurrent pipes (scheduled at the same time) overwrite each other's+ // providers. Each pipe adds/updates only its own provider entry.let models_path = config_dir.join("models.json");- let mut models_config = json!({"providers": {}});+ let mut models_config: serde_json::Value = if models_path.exists() {+ let content = std::fs::read_to_string(&models_path).unwrap_or_default();+ match serde_json::from_str(&content) {+ Ok(v) => v,+ Err(_) => {+ warn!("pi config: models.json is corrupt, rebuilding from scratch");+ json!({"providers": {}})+ }+ }+ } else {+ json!({"providers": {}})+ };+ // Ensure providers key exists and is an object+ if !models_config.get("providers").and_then(|p| p.as_object()).is_some() {+ models_config = json!({"providers": {}});+ }// Only add screenpipe cloud provider if it's the intended provider// (or no provider specified). If the user explicitly chose ollama/openai/custom,@@ -1791,12 +1806,7 @@ mod tests {let providers = config.get("providers").unwrap().as_object().unwrap();- // When a non-screenpipe provider is explicitly set, screenpipe provider- // is intentionally NOT added (to avoid silent credit drain via fallback).- assert!(- !providers.contains_key("screenpipe"),- "screenpipe provider should NOT be added when ollama is explicitly chosen"- );+ // Ollama provider must be presentassert!(providers.contains_key("ollama"), "missing ollama provider");let ollama = &providers["ollama"];