vtcode-core 0.185.0

Core library for VT Code - a Rust-based terminal coding agent
Documentation
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use super::*;

use std::sync::Arc;

use serial_test::serial;
use vtcode_commons::reference::StaticWorkspacePaths;
use vtcode_config::defaults::WorkspacePathsDefaults;
use vtcode_config::defaults::provider::with_config_defaults_provider_for_test;
use vtcode_config::loader::set_explicit_config_path;

#[test]
#[serial]
fn read_returns_layers_and_origins() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let home_config = workspace.join("home").join("vtcode.toml");
    let workspace_config = workspace.join("vtcode.toml");
    fs::create_dir_all(home_config.parent().expect("home parent")).expect("home dir");

    fs::write(&home_config, "agent.provider = \"openai\"\n").expect("home config");
    fs::write(&workspace_config, "agent.provider = \"anthropic\"\nagent.default_model = \"claude-sonnet-4-6\"\n")
        .expect("workspace config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths)).with_home_paths(vec![home_config]);

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::read(ConfigReadRequest {
            workspace: workspace.to_path_buf(),
            runtime_overrides: Vec::new(),
        })
        .expect("read response");

        assert!(!response.layers.is_empty());
        assert!(!response.merged_version.is_empty());
        assert!(response.origins.contains_key("agent.provider"));
    });
}

#[test]
#[serial]
fn write_reports_override_when_higher_layer_wins() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let home_config = workspace.join("home").join("vtcode.toml");
    let workspace_config = workspace.join("vtcode.toml");
    fs::create_dir_all(home_config.parent().expect("home parent")).expect("home dir");

    fs::write(&home_config, "agent.provider = \"openai\"\n").expect("home config");
    fs::write(&workspace_config, "agent.provider = \"gemini\"\n").expect("workspace config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths)).with_home_paths(vec![home_config]);

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::write(ConfigWriteRequest {
            workspace: workspace.to_path_buf(),
            target: ConfigWriteTarget::User,
            path: "agent.provider".to_string(),
            value: TomlValue::String("anthropic".to_string()),
            strategy: ConfigWriteStrategy::Replace,
            expected_layer_version: None,
        })
        .expect("write response");

        assert_eq!(response.effective_value, Some(TomlValue::String("gemini".to_string())));
        assert!(response.overridden_metadata.is_some());
    });
}

#[test]
#[serial]
fn write_accepts_computer_use_enabled_without_a_target() {
    // The desktop master switch persists first and guides to the target
    // second; validation must accept the intermediate state (turns fail
    // closed until a target is set). Regression: the writer used to run
    // full-config validation that rejected enabled-without-target, so the
    // switch snapped back with no usable error.
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let home_config = workspace.join("home").join("vtcode.toml");
    let workspace_config = workspace.join("vtcode.toml");
    fs::create_dir_all(home_config.parent().expect("home parent")).expect("home dir");

    fs::write(&home_config, "agent.provider = \"openai\"\n").expect("home config");
    fs::write(&workspace_config, "agent.provider = \"openai\"\n").expect("workspace config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths)).with_home_paths(vec![home_config]);

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::write(ConfigWriteRequest {
            workspace: workspace.to_path_buf(),
            target: ConfigWriteTarget::Workspace,
            path: "computer_use.enabled".to_string(),
            value: TomlValue::Boolean(true),
            strategy: ConfigWriteStrategy::Replace,
            expected_layer_version: None,
        })
        .expect("enabling without a target must persist");
        assert_eq!(response.effective_value, Some(TomlValue::Boolean(true)));
    });
}

#[test]
#[serial]
fn repository_target_write_does_not_persist_provider_endpoint_or_credentials() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let home_config = workspace.join("home/vtcode.toml");
    let workspace_config = workspace.join("vtcode.toml");
    fs::create_dir_all(home_config.parent().expect("home parent")).expect("home dir");

    fs::write(
        &home_config,
        "[provider_overrides.openai]\nbase_url = \"https://trusted.example/v1\"\napi_key_env = \"TRUSTED_API_KEY\"\n",
    )
    .expect("home config");
    fs::write(&workspace_config, "agent.provider = \"openai\"\n").expect("workspace config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(vec![home_config])
        .with_system_config_paths(Vec::new());

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::write(ConfigWriteRequest {
            workspace: workspace.to_path_buf(),
            target: ConfigWriteTarget::Workspace,
            path: "provider_overrides.openai.base_url".to_string(),
            value: TomlValue::String("https://attacker.example/v1".to_string()),
            strategy: ConfigWriteStrategy::Replace,
            expected_layer_version: None,
        })
        .expect("repository write should complete");

        assert_eq!(response.effective_value, Some(TomlValue::String("https://trusted.example/v1".to_string())));
        let workspace_content = fs::read_to_string(&workspace_config).expect("workspace config");
        assert!(!workspace_content.contains("attacker.example"));
        assert!(!workspace_content.contains("base_url"));
        assert!(!workspace_content.contains("api_key_env"));
    });
}

#[test]
#[serial]
fn user_writes_target_the_canonical_path_not_the_legacy_fallback() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let legacy_path = workspace.join("legacy").join("vtcode.toml");
    let canonical_path = workspace.join("xdg").join("vtcode.toml");
    fs::create_dir_all(legacy_path.parent().expect("legacy parent")).expect("legacy dir");
    fs::write(&legacy_path, "agent.provider = \"openai\"\n").expect("legacy config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(vec![legacy_path.clone(), canonical_path.clone()]);

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        ConfigService::write(ConfigWriteRequest {
            workspace: workspace.to_path_buf(),
            target: ConfigWriteTarget::User,
            path: "agent.provider".to_string(),
            value: TomlValue::String("anthropic".to_string()),
            strategy: ConfigWriteStrategy::Replace,
            expected_layer_version: None,
        })
        .expect("write response");

        assert!(canonical_path.exists(), "the canonical user config should be created");
        assert_eq!(fs::read_to_string(&legacy_path).expect("legacy config"), "agent.provider = \"openai\"\n");
    });
}

#[test]
#[serial]
fn write_rejects_stale_expected_version() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let workspace_config = workspace.join("vtcode.toml");
    fs::write(&workspace_config, "agent.provider = \"openai\"\n\n[workspace]\nuse_root_config = true\n")
        .expect("workspace config");

    let response = ConfigService::write(ConfigWriteRequest {
        workspace: workspace.to_path_buf(),
        target: ConfigWriteTarget::Workspace,
        path: "agent.provider".to_string(),
        value: TomlValue::String("anthropic".to_string()),
        strategy: ConfigWriteStrategy::Replace,
        expected_layer_version: Some("stale-version".to_string()),
    });

    assert!(response.is_err());
    let error = format!("{:#}", response.expect_err("expected stale version error"));
    assert!(error.contains("Layer version mismatch"));
}

#[test]
#[serial]
fn reset_workspace_layer_preserves_lower_precedence_user_values() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let user_config = workspace.join("home").join("vtcode.toml");
    let workspace_config = workspace.join("vtcode.toml");
    fs::create_dir_all(user_config.parent().expect("user parent")).expect("user dir");
    fs::write(&user_config, "agent.provider = \"openai\"\n").expect("user config");
    fs::write(&workspace_config, "agent.provider = \"anthropic\"\n").expect("workspace config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths)).with_home_paths(vec![user_config]);

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::reset(ConfigResetRequest {
            workspace: workspace.to_path_buf(),
            target: ConfigWriteTarget::Workspace,
            expected_layer_version: None,
            path: None,
        })
        .expect("workspace reset");

        assert_eq!(response.target, ConfigWriteTarget::Workspace);
        assert!(response.had_file);
        assert!(fs::read_to_string(&workspace_config).expect("reset file").trim().is_empty());

        let effective: VTCodeConfig = serde_json::from_value(response.effective_config).expect("effective config");
        assert_eq!(effective.agent.provider, "openai");
    });
}

#[test]
#[serial]
fn reset_canonical_user_layer_preserves_legacy_user_layer() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path();
    let legacy_config = workspace.join("legacy").join("vtcode.toml");
    let canonical_config = workspace.join("canonical").join("vtcode.toml");
    fs::create_dir_all(legacy_config.parent().expect("legacy parent")).expect("legacy dir");
    fs::create_dir_all(canonical_config.parent().expect("canonical parent")).expect("canonical dir");
    fs::write(&legacy_config, "agent.provider = \"openai\"\n").expect("legacy config");
    fs::write(&canonical_config, "agent.provider = \"anthropic\"\n").expect("canonical config");

    let static_paths = StaticWorkspacePaths::new(workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(vec![legacy_config.clone(), canonical_config.clone()]);

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::reset(ConfigResetRequest {
            workspace: workspace.to_path_buf(),
            target: ConfigWriteTarget::User,
            expected_layer_version: None,
            path: None,
        })
        .expect("user reset");

        assert_eq!(response.path, canonical_config);
        assert!(canonical_config.exists());
        assert_eq!(fs::read_to_string(&legacy_config).expect("legacy config"), "agent.provider = \"openai\"\n");
        let effective: VTCodeConfig = serde_json::from_value(response.effective_config).expect("effective config");
        assert_eq!(effective.agent.provider, "openai");
    });
}

#[test]
#[serial]
fn reset_project_layer_preserves_workspace_values() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path().join("workspace");
    let config_dir = workspace.join(".vtcode");
    let project_dir = config_dir.join("projects").join("demo").join("config");
    fs::create_dir_all(&project_dir).expect("project config dir");
    fs::write(workspace.join(".vtcode-project"), "demo\n").expect("project marker");
    fs::write(workspace.join("vtcode.toml"), "agent.provider = \"openai\"\n").expect("workspace config");
    let project_config = project_dir.join("vtcode.toml");
    fs::write(&project_config, "agent.default_model = \"project-model\"\n").expect("project config");

    let static_paths = StaticWorkspacePaths::new(&workspace, &config_dir);
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(Vec::new())
        .with_system_config_paths(Vec::new());

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::reset(ConfigResetRequest {
            workspace: workspace.clone(),
            target: ConfigWriteTarget::Project,
            expected_layer_version: None,
            path: None,
        })
        .expect("project reset");

        assert_eq!(response.target, ConfigWriteTarget::Project);
        assert_eq!(response.path, project_config);
        assert!(
            fs::read_to_string(&project_config)
                .expect("reset project file")
                .trim()
                .is_empty()
        );
        let effective: VTCodeConfig = serde_json::from_value(response.effective_config).expect("effective config");
        assert_eq!(effective.agent.provider, "openai");
    });
}

#[test]
#[serial]
fn reset_workspace_target_uses_explicit_session_config_path() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path().join("workspace");
    fs::create_dir_all(&workspace).expect("workspace dir");
    let explicit_path = temp.path().join("night.toml");
    fs::write(&explicit_path, "agent.provider = \"openai\"\n").expect("explicit config");

    let static_paths = StaticWorkspacePaths::new(&workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(Vec::new())
        .with_system_config_paths(Vec::new());

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        struct ExplicitConfigGuard(Option<PathBuf>);

        impl Drop for ExplicitConfigGuard {
            fn drop(&mut self) {
                set_explicit_config_path(self.0.take());
            }
        }

        let _override = ExplicitConfigGuard(explicit_config_path());
        set_explicit_config_path(Some(explicit_path.clone()));
        let response = ConfigService::reset(ConfigResetRequest {
            workspace,
            target: ConfigWriteTarget::Workspace,
            expected_layer_version: None,
            path: None,
        })
        .expect("explicit config reset");

        assert_eq!(response.target, ConfigWriteTarget::Workspace);
        assert_eq!(response.path, explicit_path);
        assert!(
            fs::read_to_string(response.path)
                .expect("reset explicit file")
                .trim()
                .is_empty()
        );
    });
}

#[test]
#[serial]
fn reset_creates_missing_explicit_session_config_path() {
    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path().join("workspace");
    fs::create_dir_all(&workspace).expect("workspace dir");
    let explicit_path = temp.path().join("new-session.toml");

    let static_paths = StaticWorkspacePaths::new(&workspace, workspace.join(".vtcode"));
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(Vec::new())
        .with_system_config_paths(Vec::new());

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        struct ExplicitConfigGuard(Option<PathBuf>);

        impl Drop for ExplicitConfigGuard {
            fn drop(&mut self) {
                set_explicit_config_path(self.0.take());
            }
        }

        let _override = ExplicitConfigGuard(explicit_config_path());
        set_explicit_config_path(Some(explicit_path.clone()));
        let response = ConfigService::reset(ConfigResetRequest {
            workspace,
            target: ConfigWriteTarget::Workspace,
            expected_layer_version: None,
            path: None,
        })
        .expect("missing explicit config reset");

        assert!(!response.had_file);
        assert_eq!(fs::read_to_string(&explicit_path).expect("created config"), "");
    });
}

#[cfg(unix)]
#[test]
#[serial]
fn reset_rejects_symlinked_workspace_config() {
    use std::os::unix::fs::symlink;

    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path().join("workspace");
    let outside = temp.path().join("outside.toml");
    fs::create_dir_all(&workspace).expect("workspace");
    fs::write(&outside, "agent.provider = \"openai\"\n").expect("outside config");
    symlink(&outside, workspace.join("vtcode.toml")).expect("config symlink");

    let response = ConfigService::reset(ConfigResetRequest {
        workspace,
        target: ConfigWriteTarget::Workspace,
        expected_layer_version: None,
        path: None,
    });

    let error = format!("{:#}", response.expect_err("symlink reset must fail"));
    assert!(error.contains("symlink"));
    assert!(outside.exists(), "the symlink target must remain untouched");
}

#[cfg(unix)]
#[test]
#[serial]
fn reset_rejects_dangling_workspace_symlink_instead_of_using_fallback() {
    use std::os::unix::fs::symlink;

    let temp = tempfile::tempdir().expect("tempdir");
    let workspace = temp.path().join("workspace");
    let config_dir = workspace.join(".vtcode");
    let fallback = config_dir.join("vtcode.toml");
    let dangling_target = temp.path().join("missing.toml");
    fs::create_dir_all(&config_dir).expect("workspace config dir");
    fs::write(&fallback, "agent.provider = \"openai\"\n").expect("fallback config");
    symlink(&dangling_target, workspace.join("vtcode.toml")).expect("dangling config symlink");

    let static_paths = StaticWorkspacePaths::new(&workspace, &config_dir);
    let provider = WorkspacePathsDefaults::new(Arc::new(static_paths))
        .with_home_paths(Vec::new())
        .with_system_config_paths(Vec::new());

    with_config_defaults_provider_for_test(Arc::new(provider), || {
        let response = ConfigService::reset(ConfigResetRequest {
            workspace: workspace.clone(),
            target: ConfigWriteTarget::Workspace,
            expected_layer_version: None,
            path: None,
        });

        let error = format!("{:#}", response.expect_err("dangling symlink reset must fail"));
        assert!(error.contains("symlink"));
        assert_eq!(fs::read_to_string(&fallback).expect("fallback config"), "agent.provider = \"openai\"\n");
    });
}