Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(app): do not log MCP migration errors on a first start (#9469) (#9568)

Thinh Lebec073437aeb
diff --git a/src-tauri/src/core/setup.rs b/src-tauri/src/core/setup.rs
index d8a713d1b..edc5d3fa8 100644
--- a/src-tauri/src/core/setup.rs
+++ b/src-tauri/src/core/setup.rs
@@ -24,15 +24,43 @@ use super::{mcp::helpers::run_mcp_commands, state::AppState};
/// migration silently.
pub const CURRENT_MCP_SCHEMA_VERSION: i64 = 5;
+/// The schema version a profile's MCP migration starts from.
+///
+/// Every step in `migrate_mcp_servers` upgrades an entry of an existing
+/// `mcp_config.json`. With no file - a first start, or a deleted config - none
+/// of them has anything to act on and each would only log an error for the
+/// missing file, burying a real first-run failure. `setup_mcp` writes the
+/// current default config right after, so such a profile starts at the current
+/// schema instead.
+///
+/// `pub` for the upgrade-compat fixture test.
+pub fn mcp_migration_start(stored: i64, config_exists: bool) -> i64 {
+ if config_exists {
+ stored
+ } else {
+ CURRENT_MCP_SCHEMA_VERSION
+ }
+}
+
// Migrate MCP servers configuration
pub fn migrate_mcp_servers(
app_handle: tauri::AppHandle,
store: Arc<Store<Wry>>,
) -> Result<(), String> {
- let mcp_version = store
- .get("mcp_version")
- .and_then(|v| v.as_i64())
- .unwrap_or(0);
+ // `exists()` reads a stat error as "missing", which would pin the current
+ // schema for a profile that does have a config. An inconclusive check counts
+ // as present so the steps run and a failed read retries at the next start.
+ let config_exists = get_jan_data_folder_path(app_handle.clone())
+ .join("mcp_config.json")
+ .try_exists()
+ .unwrap_or(true);
+ let mcp_version = mcp_migration_start(
+ store
+ .get("mcp_version")
+ .and_then(|v| v.as_i64())
+ .unwrap_or(0),
+ config_exists,
+ );
if mcp_version < 1 {
log::info!("Migrating MCP schema version 1");
let result = add_server_config(
diff --git a/src-tauri/tests/upgrade_compat.rs b/src-tauri/tests/upgrade_compat.rs
index 93f5ecd75..6c827f254 100644
--- a/src-tauri/tests/upgrade_compat.rs
+++ b/src-tauri/tests/upgrade_compat.rs
@@ -24,8 +24,8 @@
use app_lib::core::mcp::models::McpSettings;
use app_lib::core::setup::{
- is_default_exa_server, is_hosted_default_exa_server, remove_default_exa_from_config,
- schema_after_exa_cutover, CURRENT_MCP_SCHEMA_VERSION,
+ is_default_exa_server, is_hosted_default_exa_server, mcp_migration_start,
+ remove_default_exa_from_config, schema_after_exa_cutover, CURRENT_MCP_SCHEMA_VERSION,
};
use app_lib::core::threads::helpers::read_messages_from_file;
use app_lib::core::threads::utils::{get_messages_path, get_thread_dir, get_thread_metadata_path};
@@ -435,3 +435,23 @@ fn a_failed_exa_cutover_is_retried_at_the_next_startup() {
CURRENT_MCP_SCHEMA_VERSION
);
}
+
+/// A first start has no `mcp_config.json` and an empty store. Every migration
+/// step edits an entry of an existing config, so each would fail on the missing
+/// file and log an error (#9469); such a profile has nothing to migrate and
+/// starts at the current schema, which `setup_mcp` then writes the default for.
+#[test]
+fn a_profile_without_an_mcp_config_has_nothing_to_migrate() {
+ assert_eq!(mcp_migration_start(0, false), CURRENT_MCP_SCHEMA_VERSION);
+ // A deleted config on an old store is the same: there is nothing to edit.
+ assert_eq!(mcp_migration_start(3, false), CURRENT_MCP_SCHEMA_VERSION);
+
+ // An existing config keeps the gates it has not passed yet, so a real
+ // upgrade still walks them.
+ assert_eq!(mcp_migration_start(0, true), 0);
+ assert_eq!(mcp_migration_start(3, true), 3);
+ assert_eq!(
+ mcp_migration_start(CURRENT_MCP_SCHEMA_VERSION, true),
+ CURRENT_MCP_SCHEMA_VERSION
+ );
+}