vtcode_config/loader/manager/
persistence.rs1use std::fs;
4use std::path::Path;
5
6use anyhow::{Context, Result, bail};
7use vtcode_commons::VtCodePaths;
8
9use super::{ConfigLayerSource, ConfigManager, VTCodeConfig};
10
11fn ensure_private_parent_dir(path: &Path) -> Result<()> {
12 let Some(parent) = path.parent() else {
13 return Ok(());
14 };
15 let _ = VtCodePaths::ensure_user_dir(parent)
16 .with_context(|| format!("Failed to create directory: {}", parent.display()))?;
17 Ok(())
18}
19
20impl ConfigManager {
21 pub fn save_config_to_path(path: impl AsRef<Path>, config: &VTCodeConfig) -> Result<()> {
23 Self::save_config_to_path_internal(path, config, false)
24 }
25
26 pub fn save_repository_config_to_path(path: impl AsRef<Path>, config: &VTCodeConfig) -> Result<()> {
34 Self::save_config_to_path_internal(path, config, true)
35 }
36
37 fn save_config_to_path_internal(
38 path: impl AsRef<Path>,
39 config: &VTCodeConfig,
40 repository_controlled: bool,
41 ) -> Result<()> {
42 let path = path.as_ref();
43 ensure_private_parent_dir(path)?;
44 let config_to_persist = if repository_controlled {
45 Self::repository_safe_config(config)
46 } else {
47 config.clone()
48 };
49 let sparse_value =
50 Self::sparse_config_value(&config_to_persist).context("Failed to prepare sparse configuration")?;
51 let sparse_content =
52 toml::to_string_pretty(&sparse_value).context("Failed to serialize sparse configuration")?;
53
54 let existing_content = match fs::symlink_metadata(path) {
58 Ok(metadata) if metadata.file_type().is_symlink() => {
59 bail!("Refusing to read symlinked config file: {}", path.display())
60 }
61 Ok(metadata) if !metadata.is_file() => {
62 bail!("Config path is not a regular file: {}", path.display())
63 }
64 Ok(_) => Some(
65 String::from_utf8(VtCodePaths::read_file_no_follow(path)?)
66 .with_context(|| format!("Failed to read existing config: {}", path.display()))?,
67 ),
68 Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
69 Err(error) => {
70 return Err(error).with_context(|| format!("Failed to inspect config: {}", path.display()));
71 }
72 };
73
74 if let Some(original_content) = existing_content {
75 let mut doc = original_content
76 .parse::<toml_edit::DocumentMut>()
77 .with_context(|| format!("Failed to parse existing config: {}", path.display()))?;
78 Self::remove_deprecated_config_keys(&mut doc);
79 if repository_controlled {
80 Self::remove_repository_provider_settings(&mut doc);
81 Self::remove_path_and_prune_empty_parents(doc.as_table_mut(), &["auth", "copilot", "command"]);
82 }
83
84 let new_doc: toml_edit::DocumentMut = sparse_content
85 .parse()
86 .context("Failed to parse sparse serialized configuration")?;
87 let default_value =
88 toml::Value::try_from(VTCodeConfig::default()).context("Failed to serialize default configuration")?;
89 let default_doc: toml_edit::DocumentMut = toml::to_string_pretty(&default_value)
90 .context("Failed to serialize default configuration")?
91 .parse()
92 .context("Failed to parse default serialized configuration")?;
93
94 Self::merge_sparse_toml_documents(&mut doc, &new_doc, &default_doc);
96 Self::prune_cleared_optional_fields(&mut doc, &config_to_persist);
102
103 VtCodePaths::write_private_file_atomic(path, doc.to_string().as_bytes())
104 .with_context(|| format!("Failed to write config file: {}", path.display()))?;
105 } else {
106 VtCodePaths::write_private_file_atomic(path, sparse_content.as_bytes())
107 .with_context(|| format!("Failed to write config file: {}", path.display()))?;
108 }
109
110 Ok(())
111 }
112
113 fn repository_safe_config(config: &VTCodeConfig) -> VTCodeConfig {
114 let mut safe_config = config.clone();
115 safe_config.custom_providers.clear();
116 safe_config.auth.copilot.command = None;
117 for provider_override in safe_config.provider_overrides.values_mut() {
118 provider_override.base_url = None;
119 provider_override.api_key_env = None;
120 }
121 safe_config
122 }
123
124 fn prune_cleared_optional_fields(doc: &mut toml_edit::DocumentMut, config: &VTCodeConfig) {
132 let cleared: &[(&[&str], bool)] = &[
134 (&["provider", "openai", "service_tier"], config.provider.openai.service_tier.is_none()),
135 (&["provider", "anthropic", "speed"], config.provider.anthropic.speed.is_none()),
136 ];
137 for (path, is_cleared) in cleared {
138 if *is_cleared {
139 Self::remove_path_and_prune_empty_parents(doc.as_table_mut(), path);
140 }
141 }
142 }
143
144 fn remove_path_and_prune_empty_parents(table: &mut dyn toml_edit::TableLike, path: &[&str]) -> bool {
147 let Some((first, rest)) = path.split_first() else {
148 return table.is_empty();
149 };
150 if rest.is_empty() {
151 table.remove(first);
152 } else if let Some(child) = table.get_mut(first).and_then(toml_edit::Item::as_table_like_mut) {
153 if Self::remove_path_and_prune_empty_parents(child, rest) {
154 table.remove(first);
155 }
156 }
157 table.is_empty()
158 }
159
160 fn remove_deprecated_config_keys(doc: &mut toml_edit::DocumentMut) {
161 let table = doc.as_table_mut();
162 table.remove("project_doc_max_bytes");
163 table.remove("project_doc_fallback_filenames");
164 Self::remove_table_keys(table, "agent", &["autonomous_mode", "default_editing_mode"]);
165 Self::remove_table_keys(table, "permissions", &["allowed_tools", "disallowed_tools", "auto_permission"]);
166 Self::remove_path_and_prune_empty_parents(table, &["agent", "harness", "max_tool_calls_per_turn"]);
167 }
168
169 fn remove_table_keys(table: &mut toml_edit::Table, section: &str, keys: &[&str]) {
170 let Some(section) = table.get_mut(section).and_then(toml_edit::Item::as_table_mut) else {
171 return;
172 };
173
174 for key in keys {
175 section.remove(key);
176 }
177 }
178
179 pub fn sparse_config_value(config: &VTCodeConfig) -> Result<toml::Value> {
180 let mut value = toml::Value::try_from(config).context("Failed to serialize configuration")?;
181 let default_value =
182 toml::Value::try_from(VTCodeConfig::default()).context("Failed to serialize default configuration")?;
183 Self::prune_default_values(&mut value, &default_value);
184 Ok(value)
185 }
186
187 fn prune_default_values(value: &mut toml::Value, default_value: &toml::Value) -> bool {
188 match (value, default_value) {
189 (toml::Value::Table(table), toml::Value::Table(default_table)) => {
190 table.retain(|key, child| {
191 default_table
192 .get(key)
193 .is_none_or(|default_child| !Self::prune_default_values(child, default_child))
194 });
195 table.is_empty()
196 }
197 (value, default_value) => value == default_value,
198 }
199 }
200
201 fn merge_sparse_toml_documents(
203 original: &mut toml_edit::DocumentMut,
204 new: &toml_edit::DocumentMut,
205 default_doc: &toml_edit::DocumentMut,
206 ) {
207 Self::merge_sparse_tables(original.as_table_mut(), new.as_table(), default_doc.as_table());
208 }
209
210 fn merge_sparse_tables(original: &mut toml_edit::Table, new: &toml_edit::Table, default_table: &toml_edit::Table) {
211 let mut remove_keys = Vec::with_capacity(default_table.len());
212
213 for (key, default_value) in default_table.iter() {
214 if let Some(new_value) = new.get(key) {
215 if let Some(original_value) = original.get_mut(key) {
216 Self::merge_sparse_items(original_value, new_value, default_value);
217 } else {
218 original[key] = new_value.clone();
219 }
220 } else {
221 let Some(original_value) = original.get_mut(key) else {
222 continue;
223 };
224 if Self::remove_known_default_item(original_value, default_value) {
225 remove_keys.push(key.to_string());
226 }
227 }
228 }
229
230 for key in remove_keys {
231 original.remove(&key);
232 }
233
234 for (key, new_value) in new.iter() {
235 if default_table.contains_key(key) {
236 continue;
237 }
238 if let Some(original_value) = original.get_mut(key) {
239 *original_value = new_value.clone();
240 } else {
241 original[key] = new_value.clone();
242 }
243 }
244 }
245
246 fn merge_sparse_items(original: &mut toml_edit::Item, new: &toml_edit::Item, default_value: &toml_edit::Item) {
247 match (original, new, default_value) {
248 (
249 toml_edit::Item::Table(orig_table),
250 toml_edit::Item::Table(new_table),
251 toml_edit::Item::Table(default_table),
252 ) => Self::merge_sparse_tables(orig_table, new_table, default_table),
253 (orig, new, _) => {
254 *orig = new.clone();
255 }
256 }
257 }
258
259 fn remove_known_default_item(original: &mut toml_edit::Item, default_value: &toml_edit::Item) -> bool {
260 match (original, default_value) {
261 (toml_edit::Item::Table(orig_table), toml_edit::Item::Table(default_table)) => {
262 let mut remove_keys = Vec::new();
263 for (key, default_child) in default_table.iter() {
264 let Some(orig_child) = orig_table.get_mut(key) else {
265 continue;
266 };
267 if Self::remove_known_default_item(orig_child, default_child) {
268 remove_keys.push(key.to_string());
269 }
270 }
271 for key in remove_keys {
272 orig_table.remove(&key);
273 }
274 orig_table.is_empty()
275 }
276 _ => true,
277 }
278 }
279
280 pub fn save_config(&mut self, config: &VTCodeConfig) -> Result<()> {
282 let (target, repository_controlled) = if let Some(path) = &self.config_path {
283 if self.write_global_config_to_canonical || self.is_global_config_path(path) {
284 (self.preferred_user_config_path().unwrap_or_else(|| path.clone()), false)
285 } else {
286 (path.clone(), !self.explicit_config_is_trusted)
287 }
288 } else if let Some(workspace_root) = &self.workspace_root {
289 (workspace_root.join(&self.config_file_name), !self.explicit_config_is_trusted)
290 } else {
291 let cwd = std::env::current_dir().context("Failed to resolve current directory")?;
292 (cwd.join(&self.config_file_name), !self.explicit_config_is_trusted)
293 };
294
295 if repository_controlled {
296 Self::save_repository_config_to_path(target, config)?;
297 } else {
298 Self::save_config_to_path(target, config)?;
299 }
300
301 #[cfg(not(test))]
302 if let Some(workspace) = &self.workspace_root {
303 Self::invalidate_workspace_cache(workspace);
304 }
305
306 self.sync_from_config(config)
307 }
308
309 fn is_global_config_path(&self, path: &Path) -> bool {
310 self.layer_stack.layers().iter().any(|layer| {
311 layer.is_enabled()
312 && matches!(layer.source, ConfigLayerSource::System { .. } | ConfigLayerSource::User { .. })
313 && match &layer.source {
314 ConfigLayerSource::System { file } | ConfigLayerSource::User { file } => file == path,
315 _ => false,
316 }
317 })
318 }
319
320 fn sync_from_config(&mut self, config: &VTCodeConfig) -> Result<()> {
323 self.config = config.clone();
324 Ok(())
325 }
326}