From 3cc8850a587d1dcd65f411ebb641950b734c3e46 Mon Sep 17 00:00:00 2001 From: Oleksiy Syvokon Date: Thu, 8 May 2025 15:38:19 +0300 Subject: [PATCH] settings: Migration for fixing duplicated `agent` keys (#30237) As a byproduct, this fixes bug where it's impossible to change Agent profile Closes #30000 Release Notes: - N/A --- crates/migrator/src/migrations.rs | 6 +++ .../src/migrations/m_2025_05_08/settings.rs | 26 ++++++++++++ crates/migrator/src/migrator.rs | 42 +++++++++++++++++++ crates/migrator/src/patterns.rs | 4 +- crates/migrator/src/patterns/settings.rs | 15 +++++++ 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 crates/migrator/src/migrations/m_2025_05_08/settings.rs diff --git a/crates/migrator/src/migrations.rs b/crates/migrator/src/migrations.rs index 875cf62e69..16d260d9e2 100644 --- a/crates/migrator/src/migrations.rs +++ b/crates/migrator/src/migrations.rs @@ -63,3 +63,9 @@ pub(crate) mod m_2025_05_05 { pub(crate) use settings::SETTINGS_PATTERNS; } + +pub(crate) mod m_2025_05_08 { + mod settings; + + pub(crate) use settings::SETTINGS_PATTERNS; +} diff --git a/crates/migrator/src/migrations/m_2025_05_08/settings.rs b/crates/migrator/src/migrations/m_2025_05_08/settings.rs new file mode 100644 index 0000000000..6157ad0d43 --- /dev/null +++ b/crates/migrator/src/migrations/m_2025_05_08/settings.rs @@ -0,0 +1,26 @@ +use std::ops::Range; +use tree_sitter::{Query, QueryMatch}; + +use crate::{MigrationPatterns, patterns::SETTINGS_DUPLICATED_AGENT_PATTERN}; + +pub const SETTINGS_PATTERNS: MigrationPatterns = + &[(SETTINGS_DUPLICATED_AGENT_PATTERN, comment_duplicated_agent)]; + +fn comment_duplicated_agent( + contents: &str, + mat: &QueryMatch, + query: &Query, +) -> Option<(Range, String)> { + let pair_ix = query.capture_index_for_name("pair1")?; + let mut range = mat.nodes_for_capture_index(pair_ix).next()?.byte_range(); + + // Include the comma into the commented region + let rtext = &contents[range.end..]; + if let Some(comma_index) = rtext.find(',') { + range.end += comma_index + 1; + } + + let value = contents[range.clone()].to_string(); + let commented_value = format!("/* Duplicated key auto-commented: {value} */"); + Some((range, commented_value)) +} diff --git a/crates/migrator/src/migrator.rs b/crates/migrator/src/migrator.rs index 37af91cd85..7433ca624d 100644 --- a/crates/migrator/src/migrator.rs +++ b/crates/migrator/src/migrator.rs @@ -140,6 +140,10 @@ pub fn migrate_settings(text: &str) -> Result> { migrations::m_2025_05_05::SETTINGS_PATTERNS, &SETTINGS_QUERY_2025_05_05, ), + ( + migrations::m_2025_05_08::SETTINGS_PATTERNS, + &SETTINGS_QUERY_2025_05_08, + ), ]; run_migrations(text, migrations) } @@ -230,6 +234,10 @@ define_query!( SETTINGS_QUERY_2025_05_05, migrations::m_2025_05_05::SETTINGS_PATTERNS ); +define_query!( + SETTINGS_QUERY_2025_05_08, + migrations::m_2025_05_08::SETTINGS_PATTERNS +); // custom query static EDIT_PREDICTION_SETTINGS_MIGRATION_QUERY: LazyLock = LazyLock::new(|| { @@ -743,4 +751,38 @@ mod tests { ), ); } + + #[test] + fn test_comment_duplicated_agent() { + assert_migrate_settings( + r#"{ + "agent": { + "name": "assistant-1", + "model": "gpt-4", // weird formatting + "utf8": "привіт" + }, + "something": "else", + "agent": { + "name": "assistant-2", + "model": "gemini-pro" + } + } + "#, + Some( + r#"{ + /* Duplicated key auto-commented: "agent": { + "name": "assistant-1", + "model": "gpt-4", // weird formatting + "utf8": "привіт" + }, */ + "something": "else", + "agent": { + "name": "assistant-2", + "model": "gemini-pro" + } + } + "#, + ), + ); + } } diff --git a/crates/migrator/src/patterns.rs b/crates/migrator/src/patterns.rs index 0cefb90fcf..3848baf23b 100644 --- a/crates/migrator/src/patterns.rs +++ b/crates/migrator/src/patterns.rs @@ -8,6 +8,6 @@ pub(crate) use keymap::{ pub(crate) use settings::{ SETTINGS_ASSISTANT_PATTERN, SETTINGS_ASSISTANT_TOOLS_PATTERN, - SETTINGS_EDIT_PREDICTIONS_ASSISTANT_PATTERN, SETTINGS_LANGUAGES_PATTERN, - SETTINGS_NESTED_KEY_VALUE_PATTERN, SETTINGS_ROOT_KEY_VALUE_PATTERN, + SETTINGS_DUPLICATED_AGENT_PATTERN, SETTINGS_EDIT_PREDICTIONS_ASSISTANT_PATTERN, + SETTINGS_LANGUAGES_PATTERN, SETTINGS_NESTED_KEY_VALUE_PATTERN, SETTINGS_ROOT_KEY_VALUE_PATTERN, }; diff --git a/crates/migrator/src/patterns/settings.rs b/crates/migrator/src/patterns/settings.rs index bad228fbff..72fd02b153 100644 --- a/crates/migrator/src/patterns/settings.rs +++ b/crates/migrator/src/patterns/settings.rs @@ -93,3 +93,18 @@ pub const SETTINGS_EDIT_PREDICTIONS_ASSISTANT_PATTERN: &str = r#"(document (#eq? @edit_predictions "edit_predictions") (#eq? @enabled_in_assistant "enabled_in_assistant") )"#; + +pub const SETTINGS_DUPLICATED_AGENT_PATTERN: &str = r#"(document + (object + (pair + key: (string (string_content) @agent1) + value: (_) + ) @pair1 + (pair + key: (string (string_content) @agent2) + value: (_) + ) + ) + (#eq? @agent1 "agent") + (#eq? @agent2 "agent") +)"#;