dot-agent-core 0.4.1

Core library for dot-agent profile management
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
use std::collections::HashMap;
use std::fs;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};

use chrono::{DateTime, Local, Utc};
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;

use crate::error::{DotAgentError, Result};
use crate::metadata::compute_hash;

const TARGET_SNAPSHOTS_DIR: &str = "target-snapshots";
const PROFILE_SNAPSHOTS_DIR: &str = "profile-snapshots";
const MANIFEST_FILE: &str = "manifest.toml";

/// Directories to exclude from target snapshots (Claude Code system directories)
const EXCLUDED_DIRS: &[&str] = &[
    "debug",
    "file-history",
    "paste-cache",
    "cache",
    "backups",
    "plans",
    "ide",
    "data",
    "config",
    "projects",
    "todos",
    ".claude",
    "_bk",
];

/// Files to exclude from target snapshots
const EXCLUDED_FILES: &[&str] = &[
    ".DS_Store",
    "history.jsonl",
    "__store.db",
    "config.json",
    ".dot-agent-meta.toml",
];

/// Trigger that caused the snapshot to be created
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum SnapshotTrigger {
    /// Automatically created before install operation
    PreInstall,
    /// Automatically created before uninstall operation
    PreUninstall,
    /// Automatically created before update operation
    PreUpdate,
    /// Manually created by user
    Manual,
}

impl SnapshotTrigger {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::PreInstall => "pre-install",
            Self::PreUninstall => "pre-uninstall",
            Self::PreUpdate => "pre-update",
            Self::Manual => "manual",
        }
    }
}

impl std::fmt::Display for SnapshotTrigger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

// ============================================================================
// Snapshot Target Trait
// ============================================================================

/// Trait for types that can be snapshot targets
pub trait SnapshotTarget {
    /// Get the storage directory for snapshots of this target
    fn storage_dir(&self, base_dir: &Path) -> PathBuf;

    /// Get the content path to snapshot
    fn content_path(&self) -> &Path;

    /// Get identifier for display/messages
    fn identifier(&self) -> String;

    /// Check if a file should be excluded from snapshot
    fn should_exclude(&self, path: &Path, base: &Path) -> bool;

    /// Error to return when source doesn't exist
    fn not_found_error(&self) -> DotAgentError;
}

/// Target directory snapshot (e.g., ~/.claude or project/.claude)
#[derive(Debug, Clone)]
pub struct TargetDir {
    path: PathBuf,
}

impl TargetDir {
    pub fn new(path: PathBuf) -> Self {
        Self { path }
    }
}

impl SnapshotTarget for TargetDir {
    fn storage_dir(&self, base_dir: &Path) -> PathBuf {
        let hash = compute_hash(self.path.to_string_lossy().as_bytes());
        let short_hash = &hash[..12];
        base_dir.join(TARGET_SNAPSHOTS_DIR).join(short_hash)
    }

    fn content_path(&self) -> &Path {
        &self.path
    }

    fn identifier(&self) -> String {
        self.path.to_string_lossy().to_string()
    }

    fn should_exclude(&self, path: &Path, base: &Path) -> bool {
        should_exclude_target(path, base)
    }

    fn not_found_error(&self) -> DotAgentError {
        DotAgentError::TargetNotFound {
            path: self.path.clone(),
        }
    }
}

/// Profile source directory snapshot
#[derive(Debug, Clone)]
pub struct ProfileDir {
    name: String,
    path: PathBuf,
}

impl ProfileDir {
    pub fn new(name: String, path: PathBuf) -> Self {
        Self { name, path }
    }
}

impl SnapshotTarget for ProfileDir {
    fn storage_dir(&self, base_dir: &Path) -> PathBuf {
        base_dir.join(PROFILE_SNAPSHOTS_DIR).join(&self.name)
    }

    fn content_path(&self) -> &Path {
        &self.path
    }

    fn identifier(&self) -> String {
        self.name.clone()
    }

    fn should_exclude(&self, path: &Path, _base: &Path) -> bool {
        should_exclude_profile(path)
    }

    fn not_found_error(&self) -> DotAgentError {
        DotAgentError::ProfileNotFound {
            name: self.name.clone(),
        }
    }
}

// ============================================================================
// Exclusion Filters
// ============================================================================

fn should_exclude_target(path: &Path, base: &Path) -> bool {
    if let Some(name) = path.file_name() {
        let name_str = name.to_string_lossy();
        if EXCLUDED_FILES.iter().any(|&f| name_str == f) {
            return true;
        }
        if name_str.starts_with(".dot-agent") {
            return true;
        }
    }

    if let Ok(relative) = path.strip_prefix(base) {
        if let Some(first_component) = relative.components().next() {
            let first = first_component.as_os_str().to_string_lossy();
            if EXCLUDED_DIRS.iter().any(|&d| first == d) {
                return true;
            }
        }
    }

    false
}

fn should_exclude_profile(path: &Path) -> bool {
    if let Some(name) = path.file_name() {
        let name_str = name.to_string_lossy();
        if name_str.starts_with('.') || name_str == ".DS_Store" {
            return true;
        }
    }
    false
}

// ============================================================================
// Snapshot Data Structures
// ============================================================================

/// Snapshot metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Snapshot {
    pub id: String,
    pub timestamp: DateTime<Utc>,
    pub trigger: SnapshotTrigger,
    pub message: Option<String>,
    pub profiles_affected: Vec<String>,
    pub file_count: usize,
}

impl Snapshot {
    /// Format timestamp for display in local timezone
    pub fn display_time(&self) -> String {
        self.timestamp
            .with_timezone(&Local)
            .format("%Y-%m-%d %H:%M:%S")
            .to_string()
    }
}

/// Manifest storing list of snapshots for a target
#[derive(Debug, Serialize, Deserialize)]
struct SnapshotManifest {
    target_path: String,
    created_at: DateTime<Utc>,
    snapshots: Vec<Snapshot>,
}

impl Default for SnapshotManifest {
    fn default() -> Self {
        Self {
            target_path: String::new(),
            created_at: Utc::now(),
            snapshots: Vec::new(),
        }
    }
}

/// Differences between a snapshot and current state
#[derive(Debug, Default)]
pub struct SnapshotDiff {
    pub unchanged: Vec<String>,
    pub modified: Vec<String>,
    pub added: Vec<String>,
    pub deleted: Vec<String>,
}

impl SnapshotDiff {
    pub fn has_changes(&self) -> bool {
        !self.modified.is_empty() || !self.added.is_empty() || !self.deleted.is_empty()
    }
}

// ============================================================================
// Generic Snapshot Manager
// ============================================================================

/// Generic snapshot manager that works with any SnapshotTarget
pub struct GenericSnapshotManager<T: SnapshotTarget> {
    base_dir: PathBuf,
    _marker: PhantomData<T>,
}

impl<T: SnapshotTarget> GenericSnapshotManager<T> {
    pub fn new(base_dir: PathBuf) -> Self {
        Self {
            base_dir,
            _marker: PhantomData,
        }
    }

    fn load_manifest(&self, target: &T) -> Result<SnapshotManifest> {
        let manifest_path = target.storage_dir(&self.base_dir).join(MANIFEST_FILE);
        if !manifest_path.exists() {
            return Ok(SnapshotManifest::default());
        }
        let content = fs::read_to_string(&manifest_path)?;
        toml::from_str(&content).map_err(DotAgentError::TomlDe)
    }

    fn save_manifest(&self, target: &T, manifest: &SnapshotManifest) -> Result<()> {
        let storage_dir = target.storage_dir(&self.base_dir);
        fs::create_dir_all(&storage_dir)?;
        let manifest_path = storage_dir.join(MANIFEST_FILE);
        let content = toml::to_string_pretty(manifest)?;
        fs::write(manifest_path, content)?;
        Ok(())
    }

    /// Save a snapshot of the target
    pub fn save(
        &self,
        target: &T,
        trigger: SnapshotTrigger,
        message: Option<&str>,
        profiles_affected: &[String],
    ) -> Result<Snapshot> {
        let content_path = target.content_path();
        if !content_path.exists() {
            return Err(target.not_found_error());
        }

        let timestamp = Utc::now();
        let id = timestamp.format("%Y%m%d_%H%M%S").to_string();

        let snapshot_dir = target.storage_dir(&self.base_dir).join(&id);
        fs::create_dir_all(&snapshot_dir)?;

        // Copy files from content to snapshot
        let mut file_count = 0;
        for entry in WalkDir::new(content_path)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().is_file())
        {
            let src = entry.path();

            if target.should_exclude(src, content_path) {
                continue;
            }

            if let Ok(relative) = src.strip_prefix(content_path) {
                let dst = snapshot_dir.join(relative);
                if let Some(parent) = dst.parent() {
                    fs::create_dir_all(parent)?;
                }
                fs::copy(src, dst)?;
                file_count += 1;
            }
        }

        let snapshot = Snapshot {
            id,
            timestamp,
            trigger,
            message: message.map(String::from),
            profiles_affected: profiles_affected.to_vec(),
            file_count,
        };

        // Update manifest
        let mut manifest = self.load_manifest(target)?;
        if manifest.target_path.is_empty() {
            manifest.target_path = target.identifier();
        }
        manifest.snapshots.push(snapshot.clone());
        self.save_manifest(target, &manifest)?;

        Ok(snapshot)
    }

    /// List all snapshots for a target
    pub fn list(&self, target: &T) -> Result<Vec<Snapshot>> {
        let manifest = self.load_manifest(target)?;
        Ok(manifest.snapshots)
    }

    /// Get a specific snapshot
    pub fn get(&self, target: &T, id: &str) -> Result<Snapshot> {
        let manifest = self.load_manifest(target)?;
        manifest
            .snapshots
            .into_iter()
            .find(|s| s.id == id)
            .ok_or_else(|| DotAgentError::SnapshotNotFound { id: id.to_string() })
    }

    /// Restore a snapshot
    pub fn restore(&self, target: &T, id: &str) -> Result<(usize, usize)> {
        let _ = self.get(target, id)?;

        let snapshot_dir = target.storage_dir(&self.base_dir).join(id);
        if !snapshot_dir.exists() {
            return Err(DotAgentError::SnapshotNotFound { id: id.to_string() });
        }

        let content_path = target.content_path();

        // Clear content directory
        let mut removed = 0;
        if content_path.exists() {
            for entry in WalkDir::new(content_path)
                .into_iter()
                .filter_map(|e| e.ok())
                .filter(|e| e.path().is_file())
            {
                let path = entry.path();
                if target.should_exclude(path, content_path) {
                    continue;
                }
                fs::remove_file(path)?;
                removed += 1;
            }
            clean_empty_dirs(content_path)?;
        }

        // Copy files from snapshot to content
        fs::create_dir_all(content_path)?;
        let mut restored = 0;
        for entry in WalkDir::new(&snapshot_dir)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.path().is_file())
        {
            let src = entry.path();
            if let Ok(relative) = src.strip_prefix(&snapshot_dir) {
                let dst = content_path.join(relative);
                if let Some(parent) = dst.parent() {
                    fs::create_dir_all(parent)?;
                }
                fs::copy(src, dst)?;
                restored += 1;
            }
        }

        Ok((removed, restored))
    }

    /// Compare snapshot with current state
    pub fn diff(&self, target: &T, id: &str) -> Result<SnapshotDiff> {
        let _ = self.get(target, id)?;

        let snapshot_dir = target.storage_dir(&self.base_dir).join(id);
        if !snapshot_dir.exists() {
            return Err(DotAgentError::SnapshotNotFound { id: id.to_string() });
        }

        let content_path = target.content_path();
        let mut diff = SnapshotDiff::default();

        let snapshot_files = collect_files(&snapshot_dir)?;
        let current_files = if content_path.exists() {
            collect_files_filtered(content_path, |p| target.should_exclude(p, content_path))?
        } else {
            HashMap::new()
        };

        for (path, snap_hash) in &snapshot_files {
            match current_files.get(path) {
                Some(curr_hash) if curr_hash == snap_hash => {
                    diff.unchanged.push(path.clone());
                }
                Some(_) => {
                    diff.modified.push(path.clone());
                }
                None => {
                    diff.deleted.push(path.clone());
                }
            }
        }

        for path in current_files.keys() {
            if !snapshot_files.contains_key(path) {
                diff.added.push(path.clone());
            }
        }

        diff.unchanged.sort();
        diff.modified.sort();
        diff.added.sort();
        diff.deleted.sort();

        Ok(diff)
    }

    /// Delete a snapshot
    pub fn delete(&self, target: &T, id: &str) -> Result<()> {
        let _ = self.get(target, id)?;

        let snapshot_dir = target.storage_dir(&self.base_dir).join(id);
        if snapshot_dir.exists() {
            fs::remove_dir_all(&snapshot_dir)?;
        }

        let mut manifest = self.load_manifest(target)?;
        manifest.snapshots.retain(|s| s.id != id);
        self.save_manifest(target, &manifest)?;

        Ok(())
    }

    /// Prune old snapshots, keeping only the most recent `keep` snapshots
    pub fn prune(&self, target: &T, keep: usize) -> Result<Vec<String>> {
        let mut manifest = self.load_manifest(target)?;

        if manifest.snapshots.len() <= keep {
            return Ok(vec![]);
        }

        manifest
            .snapshots
            .sort_by(|a, b| a.timestamp.cmp(&b.timestamp));

        let to_remove = manifest.snapshots.len() - keep;
        let removed: Vec<Snapshot> = manifest.snapshots.drain(..to_remove).collect();

        let mut deleted_ids = Vec::new();
        for snap in &removed {
            let snapshot_dir = target.storage_dir(&self.base_dir).join(&snap.id);
            if snapshot_dir.exists() {
                fs::remove_dir_all(&snapshot_dir)?;
            }
            deleted_ids.push(snap.id.clone());
        }

        self.save_manifest(target, &manifest)?;

        Ok(deleted_ids)
    }

    /// Get the latest snapshot
    pub fn latest(&self, target: &T) -> Result<Option<Snapshot>> {
        let manifest = self.load_manifest(target)?;
        Ok(manifest
            .snapshots
            .into_iter()
            .max_by(|a, b| a.timestamp.cmp(&b.timestamp)))
    }
}

// ============================================================================
// Type Aliases for Backward Compatibility
// ============================================================================

/// Snapshot manager for target directories (e.g., installed .claude folders)
pub type SnapshotManager = GenericSnapshotManager<TargetDir>;

/// Snapshot manager for profile source directories
pub type ProfileSnapshotManager = GenericSnapshotManager<ProfileDir>;

// ============================================================================
// Convenience Methods for SnapshotManager
// ============================================================================

impl SnapshotManager {
    /// Save a snapshot of a target directory
    pub fn save_target(
        &self,
        target: &Path,
        trigger: SnapshotTrigger,
        message: Option<&str>,
        profiles_affected: &[String],
    ) -> Result<Snapshot> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.save(&target_dir, trigger, message, profiles_affected)
    }

    /// List snapshots for a target directory
    pub fn list_target(&self, target: &Path) -> Result<Vec<Snapshot>> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.list(&target_dir)
    }

    /// Get a specific snapshot for a target directory
    pub fn get_target(&self, target: &Path, id: &str) -> Result<Snapshot> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.get(&target_dir, id)
    }

    /// Restore a snapshot to a target directory
    pub fn restore_target(&self, target: &Path, id: &str) -> Result<(usize, usize)> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.restore(&target_dir, id)
    }

    /// Compare snapshot with current target state
    pub fn diff_target(&self, target: &Path, id: &str) -> Result<SnapshotDiff> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.diff(&target_dir, id)
    }

    /// Delete a snapshot for a target directory
    pub fn delete_target(&self, target: &Path, id: &str) -> Result<()> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.delete(&target_dir, id)
    }

    /// Prune old snapshots for a target directory
    pub fn prune_target(&self, target: &Path, keep: usize) -> Result<Vec<String>> {
        let target_dir = TargetDir::new(target.to_path_buf());
        self.prune(&target_dir, keep)
    }
}

// ============================================================================
// Convenience Methods for ProfileSnapshotManager
// ============================================================================

impl ProfileSnapshotManager {
    /// Save a snapshot of a profile
    pub fn save_profile(
        &self,
        profile_name: &str,
        profile_path: &Path,
        message: Option<&str>,
    ) -> Result<Snapshot> {
        let profile_dir = ProfileDir::new(profile_name.to_string(), profile_path.to_path_buf());
        self.save(
            &profile_dir,
            SnapshotTrigger::Manual,
            message,
            &[profile_name.to_string()],
        )
    }

    /// List snapshots for a profile
    pub fn list_profile(&self, profile_name: &str) -> Result<Vec<Snapshot>> {
        // Use empty path for listing - we only need the name for storage_dir
        let profile_dir = ProfileDir::new(profile_name.to_string(), PathBuf::new());
        self.list(&profile_dir)
    }

    /// Get a specific snapshot for a profile
    pub fn get_profile(&self, profile_name: &str, id: &str) -> Result<Snapshot> {
        let profile_dir = ProfileDir::new(profile_name.to_string(), PathBuf::new());
        self.get(&profile_dir, id)
    }

    /// Restore a snapshot to a profile directory
    pub fn restore_profile(
        &self,
        profile_name: &str,
        profile_path: &Path,
        id: &str,
    ) -> Result<(usize, usize)> {
        let profile_dir = ProfileDir::new(profile_name.to_string(), profile_path.to_path_buf());
        self.restore(&profile_dir, id)
    }

    /// Compare snapshot with current profile state
    pub fn diff_profile(
        &self,
        profile_name: &str,
        profile_path: &Path,
        id: &str,
    ) -> Result<SnapshotDiff> {
        let profile_dir = ProfileDir::new(profile_name.to_string(), profile_path.to_path_buf());
        self.diff(&profile_dir, id)
    }

    /// Delete a snapshot for a profile
    pub fn delete_profile(&self, profile_name: &str, id: &str) -> Result<()> {
        let profile_dir = ProfileDir::new(profile_name.to_string(), PathBuf::new());
        self.delete(&profile_dir, id)
    }

    /// Prune old snapshots for a profile
    pub fn prune_profile(&self, profile_name: &str, keep: usize) -> Result<Vec<String>> {
        let profile_dir = ProfileDir::new(profile_name.to_string(), PathBuf::new());
        self.prune(&profile_dir, keep)
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

fn collect_files(dir: &Path) -> Result<HashMap<String, String>> {
    let mut files = HashMap::new();
    for entry in WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_file())
    {
        let path = entry.path();
        if let Ok(relative) = path.strip_prefix(dir) {
            let content = fs::read(path)?;
            let hash = compute_hash(&content);
            files.insert(relative.to_string_lossy().to_string(), hash);
        }
    }
    Ok(files)
}

fn collect_files_filtered<F>(dir: &Path, should_exclude: F) -> Result<HashMap<String, String>>
where
    F: Fn(&Path) -> bool,
{
    let mut files = HashMap::new();
    for entry in WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_file())
    {
        let path = entry.path();
        if should_exclude(path) {
            continue;
        }
        if let Ok(relative) = path.strip_prefix(dir) {
            let content = fs::read(path)?;
            let hash = compute_hash(&content);
            files.insert(relative.to_string_lossy().to_string(), hash);
        }
    }
    Ok(files)
}

fn clean_empty_dirs(dir: &Path) -> Result<()> {
    for entry in WalkDir::new(dir)
        .contents_first(true)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().is_dir())
    {
        let path = entry.path();
        if path != dir && fs::read_dir(path)?.next().is_none() {
            fs::remove_dir(path)?;
        }
    }
    Ok(())
}