vtcode-commons 0.147.2

Shared traits for paths, telemetry, and error reporting reused across VT Code component extractions
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Rollback-safe migration from the pre-XDG `~/.vtcode` layout.

use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::{Context, Result, anyhow, bail};
use serde::Serialize;

use super::{VtCodePaths, create_private_new_file, ensure_migration_dir, ensure_private_dir, open_no_follow};

/// Performs one idempotent migration using an immutable path-policy snapshot.
#[derive(Debug, Clone)]
pub struct LegacyMigrator {
    paths: VtCodePaths,
}

impl LegacyMigrator {
    /// Creates a migrator bound to one resolved global path policy.
    pub fn new(paths: VtCodePaths) -> Self {
        Self { paths }
    }

    /// Scans and copies eligible legacy content without modifying its source.
    pub fn run(&self) -> Result<MigrationReport> {
        let marker = self.paths.migration_marker_path();
        let mut report = MigrationReport::default();
        let marker_parent = marker.parent().ok_or_else(|| anyhow!("migration marker has no parent"))?;
        if let Err(error) = ensure_private_dir(marker_parent) {
            report.failures.push(MigrationFailure {
                path: marker_parent.to_path_buf(),
                error: error.to_string(),
            });
            persist_report_best_effort(&self.paths, &report);
            return Ok(report);
        }
        let marker_blocked = match fs::symlink_metadata(&marker) {
            Ok(metadata) if metadata.file_type().is_symlink() => {
                report.failures.push(MigrationFailure {
                    path: marker.clone(),
                    error: "migration marker is a symlink; refusing to follow it".to_string(),
                });
                true
            }
            Ok(metadata) if metadata.is_file() => match valid_migration_marker(&marker) {
                Ok(true) => {
                    return Ok(MigrationReport {
                        already_completed: true,
                        ..MigrationReport::default()
                    });
                }
                Ok(false) => {
                    report.failures.push(MigrationFailure {
                        path: marker.clone(),
                        error: "migration marker has invalid contents or permissions".to_string(),
                    });
                    true
                }
                Err(error) => {
                    report.failures.push(MigrationFailure {
                        path: marker.clone(),
                        error: format!("could not validate migration marker: {error}"),
                    });
                    true
                }
            },
            Ok(_) => {
                report.failures.push(MigrationFailure {
                    path: marker.clone(),
                    error: "migration marker is not a regular file".to_string(),
                });
                true
            }
            Err(error) if error.kind() == io::ErrorKind::NotFound => false,
            Err(error) => {
                report.failures.push(MigrationFailure {
                    path: marker.clone(),
                    error: format!("could not inspect migration marker: {error}"),
                });
                true
            }
        };

        let legacy_root = self.paths.legacy_home_dir();
        let legacy_root_is_safe = match fs::symlink_metadata(legacy_root) {
            Ok(metadata) if metadata.file_type().is_symlink() => {
                report.failures.push(MigrationFailure {
                    path: legacy_root.to_path_buf(),
                    error: "legacy root is a symlink; refusing to traverse it".to_string(),
                });
                false
            }
            Ok(metadata) if !metadata.is_dir() => {
                report.failures.push(MigrationFailure {
                    path: legacy_root.to_path_buf(),
                    error: "legacy root is not a directory".to_string(),
                });
                false
            }
            Ok(_) => true,
            Err(error) if error.kind() == io::ErrorKind::NotFound => true,
            Err(error) => {
                report.failures.push(MigrationFailure {
                    path: legacy_root.to_path_buf(),
                    error: format!("could not inspect legacy root: {error}"),
                });
                false
            }
        };

        if legacy_root_is_safe {
            let mappings = legacy_mappings(&self.paths);
            for mapping in &mappings {
                if mapping.source == mapping.destination {
                    continue;
                }
                if mapping.skip {
                    if fs::symlink_metadata(&mapping.source).is_ok() {
                        report.skipped.push(MigrationSkip {
                            path: mapping.source.clone(),
                            reason: MigrationSkipReason::Excluded,
                        });
                    }
                    continue;
                }
                if let Err(error) = copy_legacy_tree_with_exclusions(
                    &mapping.source,
                    &mapping.destination,
                    &mut report,
                    mapping.excluded_children,
                ) {
                    report.failures.push(MigrationFailure {
                        path: mapping.source.clone(),
                        error: error.to_string(),
                    });
                }
            }
            record_unmapped_entries(legacy_root, &mappings, &mut report);
        }

        if report.has_retryable_failures() || marker_blocked {
            persist_report_best_effort(&self.paths, &report);
            return Ok(report);
        }

        // Persist the complete scan report before publishing the completion
        // marker. If diagnostics cannot be recorded, retry the migration
        // rather than claiming a successful one-time migration.
        if let Err(error) = persist_report(&self.paths, &report) {
            report.failures.push(MigrationFailure {
                path: self.paths.migration_report_path(),
                error: error.to_string(),
            });
            persist_report_best_effort(&self.paths, &report);
            return Ok(report);
        }

        if let Err(error) = write_private_atomic(&marker, b"legacy migration completed\n") {
            if error
                .downcast_ref::<io::Error>()
                .is_some_and(|io_error| io_error.kind() == io::ErrorKind::AlreadyExists)
            {
                if valid_migration_marker(&marker).unwrap_or(false) {
                    report.already_completed = true;
                } else {
                    report.failures.push(MigrationFailure {
                        path: marker,
                        error: "migration marker appeared but is invalid".to_string(),
                    });
                }
                return Ok(report);
            }
            report
                .failures
                .push(MigrationFailure { path: marker, error: error.to_string() });
            persist_report_best_effort(&self.paths, &report);
            return Ok(report);
        }
        report.marker_written = true;
        if let Err(error) = persist_report(&self.paths, &report) {
            report.failures.push(MigrationFailure {
                path: self.paths.migration_report_path(),
                error: error.to_string(),
            });
        }
        Ok(report)
    }
}

/// Outcome of a legacy migration attempt.
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize)]
pub struct MigrationReport {
    pub migrated: Vec<MigrationEntry>,
    pub skipped: Vec<MigrationSkip>,
    pub failures: Vec<MigrationFailure>,
    pub marker_written: bool,
    pub already_completed: bool,
}

impl MigrationReport {
    /// Returns whether a later startup should retry the migration.
    pub fn has_retryable_failures(&self) -> bool {
        !self.failures.is_empty()
    }
}

/// A source file copied by migration.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MigrationEntry {
    pub source: PathBuf,
    pub destination: PathBuf,
}

/// A legacy item that was deliberately left untouched.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MigrationSkip {
    pub path: PathBuf,
    pub reason: MigrationSkipReason,
}

/// An individual migration failure; remaining entries are still scanned.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct MigrationFailure {
    pub path: PathBuf,
    pub error: String,
}

/// Reason a legacy item was not copied.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MigrationSkipReason {
    DestinationExists,
    Symlink,
    SpecialFile,
    Excluded,
    Unmapped,
}

struct LegacyMapping {
    source: PathBuf,
    destination: PathBuf,
    skip: bool,
    excluded_children: &'static [&'static str],
}

fn legacy_mappings(paths: &VtCodePaths) -> Vec<LegacyMapping> {
    let legacy = paths.legacy_home_dir();
    let mut mappings = Vec::with_capacity(44);
    let mut add = |name: &str, destination: PathBuf| {
        mappings.push(LegacyMapping {
            source: legacy.join(name),
            destination,
            skip: false,
            excluded_children: &[],
        });
    };
    for name in [
        "vtcode.toml",
        "update.toml",
        "config.toml",
        "AGENTS.md",
        "AGENTS.override.md",
        "CLAUDE.md",
        "commands",
        "agents",
        "rules",
        "prompts",
        "tool-policy.json",
        "mcp.json",
        "mcp.toml",
        "mcp-config.json",
        "mcp-config.toml",
        "mcp",
        "auth",
        "output-styles",
        "output_styles",
    ] {
        add(name, paths.config_dir().join(name));
    }
    add("auth.json", paths.auth_file());
    for name in [
        "plugins",
        "skills",
        "installed-skills",
        "assets",
        "durable-assets",
        "tools",
    ] {
        add(name, paths.data_dir().join(name));
    }
    add("bin", paths.executable_dir().to_path_buf());
    for name in [
        "projects",
        "sessions",
        "history",
        "memory",
        "agent-memory",
        "audit",
        "logs",
        "scheduler",
        "pods",
        "checkpoints",
        "backups",
    ] {
        add(name, paths.state_dir().join(name));
    }
    for name in [
        "model-cache",
        "prompt-cache",
        "approval-data",
        "ast-grep",
        "ast-grep.lock",
        "web-fetch",
        "large-output",
    ] {
        add(name, paths.cache_dir().join(name));
    }
    add("cache", paths.cache_dir().to_path_buf());
    add(".cache", paths.cache_dir().to_path_buf());
    mappings.push(LegacyMapping {
        source: legacy.join("state"),
        destination: paths.state_dir().to_path_buf(),
        skip: false,
        // Migration metadata is owned by this protocol. Copying a legacy
        // marker could make an incomplete scan look completed.
        excluded_children: &["migration"],
    });
    mappings.push(LegacyMapping {
        source: legacy.join("tmp"),
        destination: paths.runtime_dir().join("tmp"),
        skip: true,
        excluded_children: &[],
    });
    mappings
}

fn record_unmapped_entries(legacy_root: &Path, mappings: &[LegacyMapping], report: &mut MigrationReport) {
    let entries = match fs::read_dir(legacy_root) {
        Ok(entries) => entries,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return,
        Err(error) => {
            report.failures.push(MigrationFailure {
                path: legacy_root.to_path_buf(),
                error: format!("could not list legacy root: {error}"),
            });
            return;
        }
    };
    for entry in entries {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error) => {
                report.failures.push(MigrationFailure {
                    path: legacy_root.to_path_buf(),
                    error: format!("could not inspect legacy entry: {error}"),
                });
                continue;
            }
        };
        if !mappings.iter().any(|mapping| mapping.source == entry.path()) {
            report.skipped.push(MigrationSkip {
                path: entry.path(),
                reason: MigrationSkipReason::Unmapped,
            });
        }
    }
}

fn copy_legacy_tree(source: &Path, destination: &Path, report: &mut MigrationReport) -> Result<()> {
    copy_legacy_tree_with_exclusions(source, destination, report, &[])
}

fn copy_legacy_tree_with_exclusions(
    source: &Path,
    destination: &Path,
    report: &mut MigrationReport,
    excluded_children: &[&str],
) -> Result<()> {
    let metadata = match fs::symlink_metadata(source) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(error).with_context(|| format!("could not inspect legacy path {}", source.display())),
    };
    if metadata.file_type().is_symlink() {
        report.skipped.push(MigrationSkip {
            path: source.to_path_buf(),
            reason: MigrationSkipReason::Symlink,
        });
        return Ok(());
    }
    if metadata.is_file() {
        return copy_regular_file(source, destination, report);
    }
    if !metadata.is_dir() {
        report.skipped.push(MigrationSkip {
            path: source.to_path_buf(),
            reason: MigrationSkipReason::SpecialFile,
        });
        return Ok(());
    }
    ensure_migration_dir(destination)?;
    let entries =
        fs::read_dir(source).with_context(|| format!("could not read legacy directory {}", source.display()))?;
    for entry in entries {
        let entry = match entry {
            Ok(entry) => entry,
            Err(error) => {
                report.failures.push(MigrationFailure {
                    path: source.to_path_buf(),
                    error: format!("could not inspect legacy entry: {error}"),
                });
                continue;
            }
        };
        let child_source = entry.path();
        let child_destination = destination.join(entry.file_name());
        if excluded_children.iter().any(|name| entry.file_name() == OsStr::new(name)) {
            report.skipped.push(MigrationSkip {
                path: child_source,
                reason: MigrationSkipReason::Excluded,
            });
            continue;
        }
        if let Err(error) = copy_legacy_tree(&child_source, &child_destination, report) {
            report
                .failures
                .push(MigrationFailure { path: child_source, error: error.to_string() });
        }
    }
    Ok(())
}

fn copy_regular_file(source: &Path, destination: &Path, report: &mut MigrationReport) -> Result<()> {
    if let Ok(metadata) = fs::symlink_metadata(destination) {
        report.skipped.push(MigrationSkip {
            path: source.to_path_buf(),
            reason: if metadata.file_type().is_symlink() {
                MigrationSkipReason::Symlink
            } else {
                MigrationSkipReason::DestinationExists
            },
        });
        return Ok(());
    }
    let parent = destination
        .parent()
        .ok_or_else(|| anyhow!("migration destination {} has no parent", destination.display()))?;
    ensure_migration_dir(parent)?;
    let (temporary, mut output) = unique_private_file(parent, destination.file_name().unwrap_or(OsStr::new("file")))?;
    let mut input =
        open_no_follow(source).with_context(|| format!("could not safely open legacy file {}", source.display()))?;
    let result: Result<()> = (|| {
        let _bytes_copied = io::copy(&mut input, &mut output)?;
        output.sync_all()?;
        drop(output);
        match fs::hard_link(&temporary, destination) {
            Ok(()) => {
                fs::remove_file(&temporary)?;
                report.migrated.push(MigrationEntry {
                    source: source.to_path_buf(),
                    destination: destination.to_path_buf(),
                });
                Ok(())
            }
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {
                report.skipped.push(MigrationSkip {
                    path: source.to_path_buf(),
                    reason: MigrationSkipReason::DestinationExists,
                });
                Ok(())
            }
            Err(error) => Err(error).with_context(|| format!("could not atomically publish {}", destination.display())),
        }
    })();
    if result.is_err() {
        remove_temporary_file(&temporary);
    }
    result
}

fn persist_report(paths: &VtCodePaths, report: &MigrationReport) -> Result<()> {
    let serialized = serde_json::to_vec_pretty(report).context("could not serialize legacy migration report")?;
    VtCodePaths::write_private_file_atomic(paths.migration_report_path(), &serialized)
        .with_context(|| format!("could not write migration report {}", paths.migration_report_path().display()))
}

fn persist_report_best_effort(paths: &VtCodePaths, report: &MigrationReport) {
    if let Err(error) = persist_report(paths, report) {
        tracing::debug!(error = %error, "could not persist legacy migration report");
    }
}

fn write_private_atomic(destination: &Path, contents: &[u8]) -> Result<()> {
    let parent = destination
        .parent()
        .ok_or_else(|| anyhow!("private file {} has no parent", destination.display()))?;
    ensure_private_dir(parent)?;
    let (temporary, mut file) = unique_private_file(parent, destination.file_name().unwrap_or(OsStr::new("file")))?;
    let result: io::Result<()> = (|| {
        file.write_all(contents)?;
        file.sync_all()?;
        drop(file);
        fs::hard_link(&temporary, destination)?;
        fs::remove_file(&temporary)?;
        Ok(())
    })();
    if result.is_err() {
        remove_temporary_file(&temporary);
    }
    result.with_context(|| format!("could not publish {}", destination.display()))
}

fn valid_migration_marker(path: &Path) -> io::Result<bool> {
    let metadata = fs::symlink_metadata(path)?;
    if !metadata.is_file() || metadata.file_type().is_symlink() {
        return Ok(false);
    }
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if metadata.permissions().mode() & 0o077 != 0 {
            return Ok(false);
        }
    }
    let mut file = open_no_follow(path)?;
    let mut contents = Vec::new();
    let _bytes_read = file.read_to_end(&mut contents)?;
    Ok(contents == b"legacy migration completed\n")
}

fn unique_private_file(parent: &Path, stem: &OsStr) -> Result<(PathBuf, File)> {
    let timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_nanos())
        .unwrap_or_default();
    let stem = stem.to_string_lossy();
    for attempt in 0..32u8 {
        let temporary = parent.join(format!(".{stem}.{}.{}.{}.migration", std::process::id(), timestamp, attempt));
        match create_private_new_file(&temporary) {
            Ok(file) => return Ok((temporary, file)),
            Err(error) if error.kind() == io::ErrorKind::AlreadyExists => continue,
            Err(error) => return Err(error).with_context(|| format!("could not create {}", temporary.display())),
        }
    }
    bail!("could not allocate a unique migration temporary file in {}", parent.display())
}

fn remove_temporary_file(path: &Path) {
    if let Err(error) = fs::remove_file(path)
        && error.kind() != io::ErrorKind::NotFound
    {
        tracing::debug!(path = %path.display(), %error, "failed to remove temporary migration file");
    }
}