Skip to content

Commit 3cc8850

Browse files
authored
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
1 parent 9f6809a commit 3cc8850

5 files changed

Lines changed: 91 additions & 2 deletions

File tree

crates/migrator/src/migrations.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@ pub(crate) mod m_2025_05_05 {
6363

6464
pub(crate) use settings::SETTINGS_PATTERNS;
6565
}
66+
67+
pub(crate) mod m_2025_05_08 {
68+
mod settings;
69+
70+
pub(crate) use settings::SETTINGS_PATTERNS;
71+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::ops::Range;
2+
use tree_sitter::{Query, QueryMatch};
3+
4+
use crate::{MigrationPatterns, patterns::SETTINGS_DUPLICATED_AGENT_PATTERN};
5+
6+
pub const SETTINGS_PATTERNS: MigrationPatterns =
7+
&[(SETTINGS_DUPLICATED_AGENT_PATTERN, comment_duplicated_agent)];
8+
9+
fn comment_duplicated_agent(
10+
contents: &str,
11+
mat: &QueryMatch,
12+
query: &Query,
13+
) -> Option<(Range<usize>, String)> {
14+
let pair_ix = query.capture_index_for_name("pair1")?;
15+
let mut range = mat.nodes_for_capture_index(pair_ix).next()?.byte_range();
16+
17+
// Include the comma into the commented region
18+
let rtext = &contents[range.end..];
19+
if let Some(comma_index) = rtext.find(',') {
20+
range.end += comma_index + 1;
21+
}
22+
23+
let value = contents[range.clone()].to_string();
24+
let commented_value = format!("/* Duplicated key auto-commented: {value} */");
25+
Some((range, commented_value))
26+
}

crates/migrator/src/migrator.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ pub fn migrate_settings(text: &str) -> Result<Option<String>> {
140140
migrations::m_2025_05_05::SETTINGS_PATTERNS,
141141
&SETTINGS_QUERY_2025_05_05,
142142
),
143+
(
144+
migrations::m_2025_05_08::SETTINGS_PATTERNS,
145+
&SETTINGS_QUERY_2025_05_08,
146+
),
143147
];
144148
run_migrations(text, migrations)
145149
}
@@ -230,6 +234,10 @@ define_query!(
230234
SETTINGS_QUERY_2025_05_05,
231235
migrations::m_2025_05_05::SETTINGS_PATTERNS
232236
);
237+
define_query!(
238+
SETTINGS_QUERY_2025_05_08,
239+
migrations::m_2025_05_08::SETTINGS_PATTERNS
240+
);
233241

234242
// custom query
235243
static EDIT_PREDICTION_SETTINGS_MIGRATION_QUERY: LazyLock<Query> = LazyLock::new(|| {
@@ -743,4 +751,38 @@ mod tests {
743751
),
744752
);
745753
}
754+
755+
#[test]
756+
fn test_comment_duplicated_agent() {
757+
assert_migrate_settings(
758+
r#"{
759+
"agent": {
760+
"name": "assistant-1",
761+
"model": "gpt-4", // weird formatting
762+
"utf8": "привіт"
763+
},
764+
"something": "else",
765+
"agent": {
766+
"name": "assistant-2",
767+
"model": "gemini-pro"
768+
}
769+
}
770+
"#,
771+
Some(
772+
r#"{
773+
/* Duplicated key auto-commented: "agent": {
774+
"name": "assistant-1",
775+
"model": "gpt-4", // weird formatting
776+
"utf8": "привіт"
777+
}, */
778+
"something": "else",
779+
"agent": {
780+
"name": "assistant-2",
781+
"model": "gemini-pro"
782+
}
783+
}
784+
"#,
785+
),
786+
);
787+
}
746788
}

crates/migrator/src/patterns.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ pub(crate) use keymap::{
88

99
pub(crate) use settings::{
1010
SETTINGS_ASSISTANT_PATTERN, SETTINGS_ASSISTANT_TOOLS_PATTERN,
11-
SETTINGS_EDIT_PREDICTIONS_ASSISTANT_PATTERN, SETTINGS_LANGUAGES_PATTERN,
12-
SETTINGS_NESTED_KEY_VALUE_PATTERN, SETTINGS_ROOT_KEY_VALUE_PATTERN,
11+
SETTINGS_DUPLICATED_AGENT_PATTERN, SETTINGS_EDIT_PREDICTIONS_ASSISTANT_PATTERN,
12+
SETTINGS_LANGUAGES_PATTERN, SETTINGS_NESTED_KEY_VALUE_PATTERN, SETTINGS_ROOT_KEY_VALUE_PATTERN,
1313
};

crates/migrator/src/patterns/settings.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,18 @@ pub const SETTINGS_EDIT_PREDICTIONS_ASSISTANT_PATTERN: &str = r#"(document
9393
(#eq? @edit_predictions "edit_predictions")
9494
(#eq? @enabled_in_assistant "enabled_in_assistant")
9595
)"#;
96+
97+
pub const SETTINGS_DUPLICATED_AGENT_PATTERN: &str = r#"(document
98+
(object
99+
(pair
100+
key: (string (string_content) @agent1)
101+
value: (_)
102+
) @pair1
103+
(pair
104+
key: (string (string_content) @agent2)
105+
value: (_)
106+
)
107+
)
108+
(#eq? @agent1 "agent")
109+
(#eq? @agent2 "agent")
110+
)"#;

0 commit comments

Comments
 (0)