vtcode-core 0.104.1

Core library for VT Code - a Rust-based terminal coding agent
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
//! Backup and restore functionality for dotfiles.
//!
//! Creates versioned backups before any permitted modification,
//! preserving original permissions and ownership.

#[cfg(unix)]
use std::fs::Permissions;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use vtcode_commons::utils::calculate_sha256;

use crate::utils::file_utils::{ensure_dir_exists, read_json_file, write_json_file};

/// Metadata for a dotfile backup.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DotfileBackup {
    /// Original file path.
    pub original_path: String,
    /// Backup file path.
    pub backup_path: String,
    /// Timestamp of backup creation.
    pub created_at: DateTime<Utc>,
    /// SHA-256 hash of the original content.
    pub content_hash: String,
    /// Original file size in bytes.
    pub size_bytes: u64,
    /// Original file permissions (Unix mode).
    #[cfg(unix)]
    pub permissions: u32,
    /// Reason for the backup.
    pub reason: String,
    /// Session that triggered the backup.
    pub session_id: String,
}

impl DotfileBackup {
    /// Restore this backup to the original location.
    pub async fn restore(&self) -> Result<()> {
        let backup_path = Path::new(&self.backup_path);
        let original_path = Path::new(&self.original_path);

        if !backup_path.exists() {
            bail!("Backup file does not exist: {}", self.backup_path);
        }

        // Verify backup integrity
        let content = tokio::fs::read(backup_path)
            .await
            .with_context(|| format!("Failed to read backup: {}", self.backup_path))?;

        let hash = calculate_sha256(&content);
        if hash != self.content_hash {
            bail!(
                "Backup integrity check failed: hash mismatch for {}",
                self.backup_path
            );
        }

        // Restore content
        tokio::fs::write(original_path, &content)
            .await
            .with_context(|| format!("Failed to restore to: {}", self.original_path))?;

        // Restore permissions
        #[cfg(unix)]
        {
            let perms = Permissions::from_mode(self.permissions);
            tokio::fs::set_permissions(original_path, perms)
                .await
                .with_context(|| {
                    format!("Failed to restore permissions for: {}", self.original_path)
                })?;
        }

        tracing::info!(
            "Restored dotfile {} from backup {}",
            self.original_path,
            self.backup_path
        );

        Ok(())
    }
}

/// Manager for dotfile backups.
pub struct BackupManager {
    /// Base directory for backups.
    backup_dir: PathBuf,
    /// Maximum backups to retain per file.
    max_backups: usize,
}

impl BackupManager {
    /// Create a new backup manager.
    pub async fn new(backup_dir: impl AsRef<Path>, max_backups: usize) -> Result<Self> {
        let backup_dir = backup_dir.as_ref().to_path_buf();

        // Create backup directory if it doesn't exist
        ensure_dir_exists(&backup_dir)
            .await
            .with_context(|| format!("Failed to create backup directory: {:?}", backup_dir))?;

        Ok(Self {
            backup_dir,
            max_backups,
        })
    }

    /// Create a backup of a dotfile before modification.
    pub async fn create_backup(
        &self,
        file_path: &Path,
        reason: impl Into<String>,
        session_id: impl Into<String>,
    ) -> Result<DotfileBackup> {
        if !file_path.exists() {
            bail!("Cannot backup non-existent file: {:?}", file_path);
        }

        // Read original content
        let content = tokio::fs::read(file_path)
            .await
            .with_context(|| format!("Failed to read file for backup: {:?}", file_path))?;

        // Get file metadata
        let metadata = tokio::fs::metadata(file_path)
            .await
            .with_context(|| format!("Failed to get metadata: {:?}", file_path))?;

        // Compute content hash
        let content_hash = calculate_sha256(&content);

        // Generate backup path
        let timestamp = Utc::now();
        let safe_name = self.safe_filename(file_path);
        let backup_filename = format!(
            "{}.{}.backup",
            safe_name,
            timestamp.format("%Y%m%d_%H%M%S_%3f")
        );
        let backup_path = self.backup_dir.join(&backup_filename);

        // Write backup
        tokio::fs::write(&backup_path, &content)
            .await
            .with_context(|| format!("Failed to write backup: {:?}", backup_path))?;

        // Preserve permissions on backup
        #[cfg(unix)]
        {
            let perms = metadata.permissions();
            tokio::fs::set_permissions(&backup_path, perms.clone())
                .await
                .with_context(|| format!("Failed to set backup permissions: {:?}", backup_path))?;
        }

        #[cfg(unix)]
        let permissions = metadata.permissions().mode();

        let backup = DotfileBackup {
            original_path: file_path.to_string_lossy().into_owned(),
            backup_path: backup_path.to_string_lossy().into_owned(),
            created_at: timestamp,
            content_hash,
            size_bytes: metadata.len(),
            #[cfg(unix)]
            permissions,
            reason: reason.into(),
            session_id: session_id.into(),
        };

        // Save backup metadata
        self.save_backup_metadata(&backup).await?;

        // Cleanup old backups
        self.cleanup_old_backups(file_path).await?;

        tracing::info!("Created backup for {:?} at {:?}", file_path, backup_path);

        Ok(backup)
    }

    /// Convert a file path to a safe filename for backup.
    fn safe_filename(&self, path: &Path) -> String {
        path.to_string_lossy()
            .replace(['/', '\\', ':', '.'], "_")
            .trim_start_matches('_')
            .to_string()
    }

    /// Save backup metadata to a JSON index.
    async fn save_backup_metadata(&self, backup: &DotfileBackup) -> Result<()> {
        let index_path = self.backup_dir.join("backups.json");

        let mut backups = self.load_backup_index().await.unwrap_or_default();
        backups.push(backup.clone());

        write_json_file(&index_path, &backups)
            .await
            .with_context(|| format!("Failed to write backup index: {:?}", index_path))?;

        Ok(())
    }

    /// Load the backup index.
    async fn load_backup_index(&self) -> Result<Vec<DotfileBackup>> {
        let index_path = self.backup_dir.join("backups.json");

        if !index_path.exists() {
            return Ok(Vec::new());
        }

        let backups: Vec<DotfileBackup> = read_json_file(&index_path)
            .await
            .with_context(|| format!("Failed to parse backup index: {:?}", index_path))?;

        Ok(backups)
    }

    /// Cleanup old backups, keeping only the most recent N.
    async fn cleanup_old_backups(&self, file_path: &Path) -> Result<()> {
        let backups = self.load_backup_index().await?;
        let file_path_str = file_path.to_string_lossy();

        // Get backups for this file, sorted by date (newest first)
        let mut file_backups: Vec<_> = backups
            .iter()
            .filter(|b| b.original_path == file_path_str)
            .collect();

        file_backups.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        // Delete old backups beyond max_backups
        for backup in file_backups.iter().skip(self.max_backups) {
            let backup_path = Path::new(&backup.backup_path);
            if backup_path.exists() {
                if let Err(e) = tokio::fs::remove_file(backup_path).await {
                    tracing::warn!("Failed to remove old backup {:?}: {}", backup_path, e);
                } else {
                    tracing::debug!("Removed old backup: {:?}", backup_path);
                }
            }
        }

        // Update index (remove deleted backups)
        let remaining: Vec<_> = backups
            .into_iter()
            .filter(|b| {
                if b.original_path == file_path_str {
                    Path::new(&b.backup_path).exists()
                } else {
                    true
                }
            })
            .collect();

        let index_path = self.backup_dir.join("backups.json");
        write_json_file(&index_path, &remaining)
            .await
            .with_context(|| "Failed to update backup index")?;

        Ok(())
    }

    /// Get all backups for a specific file.
    pub async fn get_backups_for_file(&self, file_path: &Path) -> Result<Vec<DotfileBackup>> {
        let backups = self.load_backup_index().await?;
        let file_path_str = file_path.to_string_lossy();

        let mut file_backups: Vec<_> = backups
            .into_iter()
            .filter(|b| b.original_path == file_path_str)
            .collect();

        file_backups.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        Ok(file_backups)
    }

    /// Get the most recent backup for a file.
    pub async fn get_latest_backup(&self, file_path: &Path) -> Result<Option<DotfileBackup>> {
        let backups = self.get_backups_for_file(file_path).await?;
        Ok(backups.into_iter().next())
    }

    /// List all backups.
    pub async fn list_all_backups(&self) -> Result<Vec<DotfileBackup>> {
        self.load_backup_index().await
    }

    /// Restore the most recent backup for a file.
    pub async fn restore_latest(&self, file_path: &Path) -> Result<()> {
        let backup = self
            .get_latest_backup(file_path)
            .await?
            .ok_or_else(|| anyhow::anyhow!("No backup found for: {:?}", file_path))?;

        backup.restore().await
    }

    /// Verify integrity of all backups.
    pub async fn verify_all_backups(&self) -> Result<Vec<(DotfileBackup, bool)>> {
        let backups = self.load_backup_index().await?;
        let mut results = Vec::new();

        for backup in backups {
            let backup_path = Path::new(&backup.backup_path);
            let valid = if backup_path.exists() {
                match tokio::fs::read(backup_path).await {
                    Ok(content) => {
                        let hash = calculate_sha256(&content);
                        hash == backup.content_hash
                    }
                    Err(_) => false,
                }
            } else {
                false
            };
            results.push((backup, valid));
        }

        Ok(results)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_backup_creation() {
        let dir = tempdir().unwrap();
        let backup_dir = dir.path().join("backups");
        let test_file = dir.path().join(".testrc");

        // Create test file
        tokio::fs::write(&test_file, "test content").await.unwrap();

        let manager = BackupManager::new(&backup_dir, 5).await.unwrap();
        let backup = manager
            .create_backup(&test_file, "test backup", "test-session")
            .await
            .unwrap();

        assert_eq!(backup.original_path, test_file.to_string_lossy());
        assert!(Path::new(&backup.backup_path).exists());
    }

    #[tokio::test]
    async fn test_backup_restore() {
        let dir = tempdir().unwrap();
        let backup_dir = dir.path().join("backups");
        let test_file = dir.path().join(".testrc");

        // Create test file with original content
        let original_content = "original content";
        tokio::fs::write(&test_file, original_content)
            .await
            .unwrap();

        let manager = BackupManager::new(&backup_dir, 5).await.unwrap();
        let backup = manager
            .create_backup(&test_file, "before modification", "test-session")
            .await
            .unwrap();

        // Modify the file
        tokio::fs::write(&test_file, "modified content")
            .await
            .unwrap();

        // Restore from backup
        backup.restore().await.unwrap();

        // Verify content is restored
        let restored = tokio::fs::read_to_string(&test_file).await.unwrap();
        assert_eq!(restored, original_content);
    }

    #[tokio::test]
    async fn test_backup_cleanup() {
        let dir = tempdir().unwrap();
        let backup_dir = dir.path().join("backups");
        let test_file = dir.path().join(".testrc");

        tokio::fs::write(&test_file, "test").await.unwrap();

        let manager = BackupManager::new(&backup_dir, 2).await.unwrap();

        // Create 5 backups (should keep only 2)
        for i in 0..5 {
            tokio::fs::write(&test_file, format!("content {}", i))
                .await
                .unwrap();
            manager
                .create_backup(&test_file, format!("backup {}", i), "test-session")
                .await
                .unwrap();
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }

        let backups = manager.get_backups_for_file(&test_file).await.unwrap();
        assert_eq!(backups.len(), 2);
    }
}