forked from openai/codex
-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathvalidation.rs
More file actions
274 lines (232 loc) · 8.61 KB
/
Copy pathvalidation.rs
File metadata and controls
274 lines (232 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use super::ConfigToml;
use std::io::ErrorKind;
use toml::Value as TomlValue;
pub(crate) fn apply_toml_override(root: &mut TomlValue, path: &str, value: TomlValue) {
use toml::value::Table;
let segments: Vec<&str> = path.split('.').collect();
let mut current = root;
for (idx, segment) in segments.iter().enumerate() {
let is_last = idx == segments.len() - 1;
if is_last {
match current {
TomlValue::Table(table) => {
table.insert(segment.to_string(), value);
}
_ => {
let mut table = Table::new();
table.insert(segment.to_string(), value);
*current = TomlValue::Table(table);
}
}
return;
}
// Traverse or create intermediate object.
match current {
TomlValue::Table(table) => {
current = table
.entry(segment.to_string())
.or_insert_with(|| TomlValue::Table(Table::new()));
}
_ => {
*current = TomlValue::Table(Table::new());
if let TomlValue::Table(tbl) = current {
current = tbl
.entry(segment.to_string())
.or_insert_with(|| TomlValue::Table(Table::new()));
}
}
}
}
}
fn warn_on_suspicious_cli_overrides(cli_paths: &[String]) {
if cli_paths.is_empty() {
return;
}
for cli_path in cli_paths {
if cli_path == "auto_drive.use_chat_model"
|| (cli_path.starts_with("auto_drive.") && cli_path.ends_with(".use_chat_model"))
{
eprintln!(
"Warning: unknown config override `{cli_path}` (ignored). Did you mean `auto_drive_use_chat_model`?"
);
}
if cli_path == "auto_review_enabled" {
eprintln!(
"Warning: unknown config override `{cli_path}` (ignored). Did you mean `tui.auto_review_enabled`?"
);
}
}
}
fn warn_on_unknown_cli_overrides(cli_paths: &[String], ignored_paths: &[String]) {
if cli_paths.is_empty() || ignored_paths.is_empty() {
return;
}
for cli_path in cli_paths {
let mut ignored = false;
for ignored_path in ignored_paths {
if cli_path == ignored_path
|| cli_path.starts_with(&format!("{ignored_path}."))
|| ignored_path.starts_with(&format!("{cli_path}."))
{
ignored = true;
break;
}
}
if !ignored {
continue;
}
// Avoid duplicate warnings for known common confusions.
if cli_path == "auto_review_enabled"
|| cli_path == "auto_drive.use_chat_model"
|| (cli_path.starts_with("auto_drive.") && cli_path.ends_with(".use_chat_model"))
{
continue;
}
eprintln!("Warning: unknown config override `{cli_path}` (ignored). See `code exec --help` for valid keys.");
}
}
pub(crate) fn deserialize_config_toml_with_cli_warnings(
root_value: &TomlValue,
cli_paths: &[String],
) -> std::io::Result<ConfigToml> {
// Note: We intentionally deserialize via `serde_json::Value` so that we can
// reliably detect unknown fields via `serde_ignored`. Some deserializers
// (including TOML implementations) may filter unknown struct fields before
// they reach Serde's ignored-field machinery.
let deserializer = serde_json::to_value(root_value).map_err(|e| {
tracing::error!("Failed to convert overridden config for deserialization: {e}");
std::io::Error::new(ErrorKind::InvalidData, e)
})?;
let mut ignored_paths: Vec<String> = Vec::new();
let cfg: ConfigToml = serde_ignored::deserialize(deserializer, |path| {
ignored_paths.push(path.to_string());
})
.map_err(|e| std::io::Error::new(ErrorKind::InvalidData, e))?;
warn_on_suspicious_cli_overrides(cli_paths);
warn_on_unknown_cli_overrides(cli_paths, &ignored_paths);
Ok(cfg)
}
pub(crate) fn upgrade_legacy_model_slugs(cfg: &mut ConfigToml) {
fn maybe_upgrade(field: &mut Option<String>) {
if let Some(old) = field.clone() {
if let Some(new) = upgrade_legacy_model_slug(&old) {
tracing::info!(
target: "code.config",
old,
new,
"upgrading legacy model slug to newer default",
);
*field = Some(new);
}
}
}
fn maybe_upgrade_name(field: &mut String) {
if let Some(new) = upgrade_legacy_model_slug(field) {
tracing::info!(
target: "code.config",
old = field.as_str(),
new,
"upgrading legacy agent slug to newer default",
);
*field = new;
}
}
maybe_upgrade(&mut cfg.model);
maybe_upgrade(&mut cfg.review_model);
for profile in cfg.profiles.values_mut() {
maybe_upgrade(&mut profile.model);
maybe_upgrade(&mut profile.review_model);
}
for agent in &mut cfg.agents {
maybe_upgrade_name(&mut agent.name);
}
if let Some(subagents) = cfg.subagents.as_mut() {
for command in &mut subagents.commands {
for agent in &mut command.agents {
maybe_upgrade_name(agent);
}
}
}
}
fn upgrade_legacy_model_slug(slug: &str) -> Option<String> {
match slug {
"gpt-5.2.4" => return Some("gpt-5.4".to_string()),
"test-gpt-5.2.4" => return Some("test-gpt-5.4".to_string()),
_ => {}
}
fn is_current_or_newer_gpt5_slug(slug: &str) -> bool {
for prefix in ["gpt-5.", "test-gpt-5."] {
let Some(rest) = slug.strip_prefix(prefix) else {
continue;
};
let minor: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect();
if minor.is_empty() {
continue;
}
if minor.parse::<u32>().is_ok_and(|value| value >= 2) {
return true;
}
}
false
}
if is_current_or_newer_gpt5_slug(slug) {
return None;
}
match slug {
"gpt-5.1-codex" => return Some("gpt-5.1-codex-max".to_string()),
"gpt-4.1" => return Some("gpt-4.1-2024-04-09".to_string()),
"gpt-4.1-mini" => return Some("gpt-4.1-mini-2024-04-09".to_string()),
"gpt-4.1-nano" => return Some("gpt-4.1-nano-2024-04-09".to_string()),
_ => {}
}
if let Some(rest) = slug.strip_prefix("test-gpt-5-codex") {
return Some(format!("test-gpt-5.1-codex{rest}"));
}
if let Some(rest) = slug.strip_prefix("gpt-5-codex") {
return Some(format!("gpt-5.1-codex{rest}"));
}
// Upgrade Anthropic Opus 4.1/4.5 to 4.6
if slug.eq_ignore_ascii_case("claude-opus-4.1") || slug.eq_ignore_ascii_case("claude-opus-4.5")
{
return Some("claude-opus-4.6".to_string());
}
// Upgrade Anthropic Sonnet 4.5 to 4.6.
if slug.eq_ignore_ascii_case("claude-sonnet-4.5") {
return Some("claude-sonnet-4.6".to_string());
}
// Upgrade Gemini Pro slugs to the latest preview track.
if slug.eq_ignore_ascii_case("gemini-2.5-pro")
|| slug.eq_ignore_ascii_case("gemini-3-pro")
|| slug.eq_ignore_ascii_case("gemini-3-pro-preview")
{
return Some("gemini-3.1-pro-preview".to_string());
}
// Upgrade Gemini Flash slugs to the latest preview track.
if slug.eq_ignore_ascii_case("gemini-2.5-flash")
|| slug.eq_ignore_ascii_case("gemini-3-flash")
{
return Some("gemini-3-flash-preview".to_string());
}
// Upgrade the older Qwen coder slug to the current plus line.
if slug.eq_ignore_ascii_case("qwen-3-coder") {
return Some("qwen3-coder-plus".to_string());
}
// Keep codex variants on their existing line; upgrades are surfaced via the
// migration prompt instead of silently rewriting explicit config.
if slug.starts_with("gpt-5.1-codex") || slug.starts_with("test-gpt-5.1-codex") {
return None;
}
if let Some(rest) = slug.strip_prefix("test-gpt-5.1") {
return Some(format!("test-gpt-5.2{rest}"));
}
if let Some(rest) = slug.strip_prefix("gpt-5.1") {
return Some(format!("gpt-5.2{rest}"));
}
if let Some(rest) = slug.strip_prefix("test-gpt-5") {
return Some(format!("test-gpt-5.2{rest}"));
}
if let Some(rest) = slug.strip_prefix("gpt-5") {
return Some(format!("gpt-5.2{rest}"));
}
None
}