dot-agent-core 0.5.0

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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

use super::delta::{compute_delta, load_delta, save_delta_files, Delta};
use crate::error::Result;

/// Checkpoint metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Checkpoint {
    /// Unique checkpoint ID
    pub id: String,
    /// Associated operation ID
    pub operation_id: String,
    /// When the checkpoint was created
    pub created_at: DateTime<Utc>,
    /// Whether this is a full snapshot
    pub is_full: bool,
    /// Base checkpoint ID for incremental (None for full)
    pub base_checkpoint: Option<String>,
    /// File hashes at this checkpoint (path -> sha256)
    pub file_hashes: HashMap<String, String>,
    /// Total files count
    pub file_count: usize,
    /// Total size in bytes
    pub total_size: u64,
}

impl Checkpoint {
    pub fn new(id: String, operation_id: String, is_full: bool) -> Self {
        Self {
            id,
            operation_id,
            created_at: Utc::now(),
            is_full,
            base_checkpoint: None,
            file_hashes: HashMap::new(),
            file_count: 0,
            total_size: 0,
        }
    }

    pub fn with_base(mut self, base_id: String) -> Self {
        self.base_checkpoint = Some(base_id);
        self
    }

    pub fn with_hashes(mut self, hashes: HashMap<String, String>) -> Self {
        self.file_count = hashes.len();
        self.file_hashes = hashes;
        self
    }

    pub fn with_size(mut self, size: u64) -> Self {
        self.total_size = size;
        self
    }
}

/// Manages checkpoints storage
pub struct CheckpointManager {
    /// Base directory for checkpoints
    base_dir: PathBuf,
    /// Manifest of all checkpoints
    manifest: CheckpointManifest,
    /// Number of operations between full snapshots
    full_snapshot_interval: usize,
}

/// Manifest tracking all checkpoints
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CheckpointManifest {
    pub version: u32,
    pub checkpoints: Vec<CheckpointEntry>,
    pub latest_full: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointEntry {
    pub id: String,
    pub operation_id: String,
    pub created_at: DateTime<Utc>,
    pub is_full: bool,
}

impl CheckpointManager {
    pub fn new(base_dir: PathBuf) -> Result<Self> {
        fs::create_dir_all(&base_dir)?;

        let manifest_path = base_dir.join("manifest.toml");
        let manifest = if manifest_path.exists() {
            let content = fs::read_to_string(&manifest_path)?;
            toml::from_str(&content)
                .map_err(|e| crate::error::DotAgentError::Internal(e.to_string()))?
        } else {
            CheckpointManifest {
                version: 1,
                checkpoints: Vec::new(),
                latest_full: None,
            }
        };

        Ok(Self {
            base_dir,
            manifest,
            full_snapshot_interval: 10,
        })
    }

    /// Set the interval for full snapshots
    pub fn with_full_snapshot_interval(mut self, interval: usize) -> Self {
        self.full_snapshot_interval = interval;
        self
    }

    /// Generate a new checkpoint ID
    fn generate_checkpoint_id() -> String {
        let now = Utc::now();
        format!("cp-{}", now.format("%Y%m%d_%H%M%S_%3f"))
    }

    /// Determine if we should create a full snapshot
    fn should_create_full(&self) -> bool {
        if self.manifest.latest_full.is_none() {
            return true;
        }

        let since_last_full = self
            .manifest
            .checkpoints
            .iter()
            .rev()
            .take_while(|c| !c.is_full)
            .count();

        since_last_full >= self.full_snapshot_interval
    }

    /// Create a checkpoint for the given source directory
    pub fn create_checkpoint(
        &mut self,
        operation_id: &str,
        source_dir: &Path,
    ) -> Result<Checkpoint> {
        let checkpoint_id = Self::generate_checkpoint_id();
        let is_full = self.should_create_full();

        let checkpoint_dir = self.base_dir.join(&checkpoint_id);
        fs::create_dir_all(&checkpoint_dir)?;

        // Get base file hashes for incremental
        let base_hashes = if is_full {
            HashMap::new()
        } else if let Some(ref latest_full_id) = self.manifest.latest_full {
            self.load_checkpoint(latest_full_id)?
                .map(|c| c.file_hashes)
                .unwrap_or_default()
        } else {
            HashMap::new()
        };

        // Compute delta
        let delta = compute_delta(&base_hashes, source_dir)?;

        // Save delta files
        save_delta_files(&delta, source_dir, &checkpoint_dir)?;

        // Collect current file hashes
        let mut current_hashes = base_hashes.clone();
        let mut total_size = 0u64;

        for entry in &delta.entries {
            match entry.delta_type {
                super::delta::DeltaType::Added | super::delta::DeltaType::Modified => {
                    if let Some(ref hash) = entry.hash {
                        current_hashes.insert(entry.path.clone(), hash.clone());
                    }
                    if let Some(size) = entry.size {
                        total_size = total_size.saturating_add(size);
                    }
                }
                super::delta::DeltaType::Deleted => {
                    current_hashes.remove(&entry.path);
                }
            }
        }

        // Create checkpoint
        let checkpoint = Checkpoint::new(checkpoint_id.clone(), operation_id.to_string(), is_full)
            .with_hashes(current_hashes)
            .with_size(total_size);

        if !is_full {
            let checkpoint = checkpoint.with_base(
                self.manifest
                    .latest_full
                    .clone()
                    .unwrap_or_else(|| checkpoint_id.clone()),
            );
            self.save_checkpoint_meta(&checkpoint, &checkpoint_dir)?;

            // Update manifest
            self.manifest.checkpoints.push(CheckpointEntry {
                id: checkpoint_id,
                operation_id: operation_id.to_string(),
                created_at: checkpoint.created_at,
                is_full,
            });
            self.save_manifest()?;

            return Ok(checkpoint);
        }

        // Save checkpoint metadata
        self.save_checkpoint_meta(&checkpoint, &checkpoint_dir)?;

        // Update manifest
        self.manifest.checkpoints.push(CheckpointEntry {
            id: checkpoint_id.clone(),
            operation_id: operation_id.to_string(),
            created_at: checkpoint.created_at,
            is_full,
        });

        if is_full {
            self.manifest.latest_full = Some(checkpoint_id);
        }

        self.save_manifest()?;

        Ok(checkpoint)
    }

    /// Save checkpoint metadata to disk
    fn save_checkpoint_meta(&self, checkpoint: &Checkpoint, checkpoint_dir: &Path) -> Result<()> {
        let meta_path = checkpoint_dir.join("meta.toml");
        let content = toml::to_string_pretty(checkpoint)
            .map_err(|e| crate::error::DotAgentError::Internal(e.to_string()))?;
        fs::write(meta_path, content)?;
        Ok(())
    }

    /// Save manifest to disk
    fn save_manifest(&self) -> Result<()> {
        let manifest_path = self.base_dir.join("manifest.toml");
        let content = toml::to_string_pretty(&self.manifest)
            .map_err(|e| crate::error::DotAgentError::Internal(e.to_string()))?;
        fs::write(manifest_path, content)?;
        Ok(())
    }

    /// Load a checkpoint by ID
    pub fn load_checkpoint(&self, checkpoint_id: &str) -> Result<Option<Checkpoint>> {
        let checkpoint_dir = self.base_dir.join(checkpoint_id);
        let meta_path = checkpoint_dir.join("meta.toml");

        if !meta_path.exists() {
            return Ok(None);
        }

        let content = fs::read_to_string(meta_path)?;
        let checkpoint: Checkpoint = toml::from_str(&content)
            .map_err(|e| crate::error::DotAgentError::Internal(e.to_string()))?;

        Ok(Some(checkpoint))
    }

    /// Restore a checkpoint to the target directory
    pub fn restore_checkpoint(&self, checkpoint_id: &str, target_dir: &Path) -> Result<()> {
        // Find the chain of checkpoints to apply
        let chain = self.build_checkpoint_chain(checkpoint_id)?;

        // Clear target directory (but keep .dot-agent-meta.toml)
        self.clear_target_preserving_meta(target_dir)?;

        // Apply checkpoints in order (oldest first)
        for cp_id in chain {
            let checkpoint_dir = self.base_dir.join(&cp_id);
            let delta = load_delta(&checkpoint_dir)?;
            self.apply_delta(&delta, &checkpoint_dir, target_dir)?;
        }

        Ok(())
    }

    /// Build the chain of checkpoints from base to target
    fn build_checkpoint_chain(&self, checkpoint_id: &str) -> Result<Vec<String>> {
        let mut chain = Vec::new();
        let mut current_id = checkpoint_id.to_string();

        loop {
            chain.push(current_id.clone());

            let checkpoint = self
                .load_checkpoint(&current_id)?
                .ok_or_else(|| crate::error::DotAgentError::NotFound(current_id.clone()))?;

            if checkpoint.is_full {
                break;
            }

            match checkpoint.base_checkpoint {
                Some(base_id) => current_id = base_id,
                None => break,
            }
        }

        chain.reverse();
        Ok(chain)
    }

    /// Clear target directory while preserving metadata files
    fn clear_target_preserving_meta(&self, target_dir: &Path) -> Result<()> {
        if !target_dir.exists() {
            return Ok(());
        }

        for entry in fs::read_dir(target_dir)? {
            let entry = entry?;
            let path = entry.path();
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

            // Preserve dot-agent metadata
            if name == ".dot-agent-meta.toml" || name == ".dot-agent-history" {
                continue;
            }

            if path.is_dir() {
                fs::remove_dir_all(&path)?;
            } else {
                fs::remove_file(&path)?;
            }
        }

        Ok(())
    }

    /// Apply a delta to the target directory
    fn apply_delta(&self, delta: &Delta, checkpoint_dir: &Path, target_dir: &Path) -> Result<()> {
        let delta_dir = checkpoint_dir.join("delta");

        for entry in &delta.entries {
            let target_path = target_dir.join(&entry.path);

            match entry.delta_type {
                super::delta::DeltaType::Added | super::delta::DeltaType::Modified => {
                    let source_path = delta_dir.join(&entry.path);
                    if source_path.exists() {
                        if let Some(parent) = target_path.parent() {
                            fs::create_dir_all(parent)?;
                        }
                        fs::copy(&source_path, &target_path)?;
                    }
                }
                super::delta::DeltaType::Deleted => {
                    if target_path.exists() {
                        fs::remove_file(&target_path)?;
                    }
                }
            }
        }

        Ok(())
    }

    /// List all checkpoints
    pub fn list_checkpoints(&self) -> &[CheckpointEntry] {
        &self.manifest.checkpoints
    }

    /// Prune old checkpoints, keeping at least `keep_count` recent ones
    pub fn prune(&mut self, keep_count: usize) -> Result<usize> {
        if self.manifest.checkpoints.len() <= keep_count {
            return Ok(0);
        }

        let to_remove = self.manifest.checkpoints.len() - keep_count;
        let mut removed = 0;

        // Remove oldest checkpoints (but never remove latest_full)
        let mut new_checkpoints = Vec::new();
        for (i, entry) in self.manifest.checkpoints.iter().enumerate() {
            if i < to_remove && Some(&entry.id) != self.manifest.latest_full.as_ref() {
                let checkpoint_dir = self.base_dir.join(&entry.id);
                if checkpoint_dir.exists() {
                    fs::remove_dir_all(&checkpoint_dir)?;
                    removed += 1;
                }
            } else {
                new_checkpoints.push(entry.clone());
            }
        }

        self.manifest.checkpoints = new_checkpoints;
        self.save_manifest()?;

        Ok(removed)
    }
}

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

    #[test]
    fn checkpoint_creation() {
        let cp = Checkpoint::new("cp-001".into(), "op-001".into(), true);
        assert!(cp.is_full);
        assert!(cp.base_checkpoint.is_none());
    }

    #[test]
    fn checkpoint_manager_init() -> Result<()> {
        let temp = TempDir::new()?;
        let manager = CheckpointManager::new(temp.path().to_path_buf())?;

        assert!(manager.manifest.checkpoints.is_empty());
        assert!(manager.manifest.latest_full.is_none());

        Ok(())
    }

    #[test]
    fn create_full_checkpoint() -> Result<()> {
        let temp_base = TempDir::new()?;
        let temp_source = TempDir::new()?;

        // Create source files
        fs::write(temp_source.path().join("file1.txt"), "content1")?;
        fs::write(temp_source.path().join("file2.txt"), "content2")?;

        let mut manager = CheckpointManager::new(temp_base.path().to_path_buf())?;
        let checkpoint = manager.create_checkpoint("op-001", temp_source.path())?;

        assert!(checkpoint.is_full);
        assert_eq!(checkpoint.file_count, 2);

        Ok(())
    }

    #[test]
    fn restore_checkpoint() -> Result<()> {
        let temp_base = TempDir::new()?;
        let temp_source = TempDir::new()?;
        let temp_target = TempDir::new()?;

        // Create source files
        fs::write(temp_source.path().join("file1.txt"), "content1")?;
        fs::write(temp_source.path().join("file2.txt"), "content2")?;

        let mut manager = CheckpointManager::new(temp_base.path().to_path_buf())?;
        let checkpoint = manager.create_checkpoint("op-001", temp_source.path())?;

        // Restore to target
        manager.restore_checkpoint(&checkpoint.id, temp_target.path())?;

        assert!(temp_target.path().join("file1.txt").exists());
        assert!(temp_target.path().join("file2.txt").exists());

        Ok(())
    }
}