paladin-ai-core 0.5.1

Pure domain types for the Paladin framework — zero infrastructure dependencies
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
/*
Node Version Service

The Node Version Service manages versioning for Node entities. It automatically creates
new versions when nodes are created or modified (if versioning is enabled), stores
historical versions, and provides retrieval methods for version history.

This service implements a revision system similar to Drupal's node revisions, where
each change creates a new version while preserving the complete history.
*/

use crate::base::entity::node::Node;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use thiserror::Error;
use uuid::Uuid;

#[derive(Debug, Clone, Error)]
pub enum VersioningError {
    #[error("Version not found: {0}")]
    VersionNotFound(String),
    #[error("Node not found: {0}")]
    NodeNotFound(Uuid),
    #[error("Invalid version number: {0}")]
    InvalidVersionNumber(u32),
    #[error("Versioning disabled for node: {0}")]
    VersioningDisabled(Uuid),
    #[error("Storage error: {0}")]
    StorageError(String),
    #[error("Serialization error: {0}")]
    SerializationError(String),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NodeVersion<T> {
    pub version_id: Uuid,
    pub node_id: Uuid,
    pub version_number: u32,
    pub node_data: T,
    pub metadata: NodeVersionMetadata,
    pub created_at: DateTime<Utc>,
    pub created_by: Option<String>,
    pub change_summary: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NodeVersionMetadata {
    pub is_published: bool,
    pub is_current: bool,
    pub tags: Vec<String>,
    pub change_type: ChangeType,
    pub content_hash: String,
    pub size_bytes: Option<u64>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ChangeType {
    Created,
    Updated,
    Published,
    Unpublished,
    Restored,
    Archived,
}

#[derive(Debug, Clone)]
pub struct VersionHistory<T> {
    pub node_id: Uuid,
    pub current_version: u32,
    pub versions: Vec<NodeVersion<T>>,
    pub total_versions: u32,
}

/// Node Version Repository trait
/// Defines the contract for persisting node versions
pub trait NodeVersionRepository<T> {
    fn save_version(&self, version: &NodeVersion<T>) -> Result<(), VersioningError>;
    fn get_version(
        &self,
        node_id: Uuid,
        version_number: u32,
    ) -> Result<Option<NodeVersion<T>>, VersioningError>;
    fn get_current_version(&self, node_id: Uuid)
    -> Result<Option<NodeVersion<T>>, VersioningError>;
    fn get_version_history(&self, node_id: Uuid) -> Result<VersionHistory<T>, VersioningError>;
    fn delete_version(&self, node_id: Uuid, version_number: u32) -> Result<(), VersioningError>;
    fn purge_old_versions(&self, node_id: Uuid, keep_count: u32) -> Result<u32, VersioningError>;
}

/// Node Version Service
/// Core service for managing node versioning
pub struct NodeVersionService<T> {
    repository: Arc<dyn NodeVersionRepository<T> + Send + Sync>,
    config: VersioningConfig,
}

#[derive(Debug, Clone)]
pub struct VersioningConfig {
    pub max_versions_per_node: Option<u32>,
    pub auto_purge_enabled: bool,
    pub compress_old_versions: bool,
    pub hash_algorithm: HashAlgorithm,
}

#[derive(Debug, Clone)]
pub enum HashAlgorithm {
    Sha256,
    Blake3,
    Xxhash,
}

impl Default for VersioningConfig {
    fn default() -> Self {
        Self {
            max_versions_per_node: Some(50),
            auto_purge_enabled: true,
            compress_old_versions: false,
            hash_algorithm: HashAlgorithm::Sha256,
        }
    }
}

impl<T> NodeVersionService<T>
where
    T: Clone + serde::Serialize + for<'de> serde::Deserialize<'de>,
{
    pub fn new(
        repository: Arc<dyn NodeVersionRepository<T> + Send + Sync>,
        config: Option<VersioningConfig>,
    ) -> Self {
        Self {
            repository,
            config: config.unwrap_or_default(),
        }
    }

    /// Create a new version when a node is created or modified
    pub fn create_version(
        &self,
        node: &Node<T>,
        change_type: ChangeType,
        created_by: Option<String>,
        change_summary: Option<String>,
    ) -> Result<NodeVersion<T>, VersioningError> {
        if !node.version {
            return Err(VersioningError::VersioningDisabled(node.uuid));
        }

        // Get current version number
        let current_version_number = match self.repository.get_current_version(node.uuid)? {
            Some(current) => current.version_number + 1,
            None => 1,
        };

        // Generate content hash
        let content_hash = self.generate_content_hash(&node.node)?;

        // Calculate serialized size
        let size_bytes = self.calculate_size(&node.node)?;

        let metadata = NodeVersionMetadata {
            is_published: matches!(change_type, ChangeType::Created | ChangeType::Published),
            is_current: true,
            tags: Vec::new(),
            change_type,
            content_hash,
            size_bytes: Some(size_bytes),
        };

        let version = NodeVersion {
            version_id: Uuid::new_v4(),
            node_id: node.uuid,
            version_number: current_version_number,
            node_data: node.node.clone(),
            metadata,
            created_at: Utc::now(),
            created_by,
            change_summary,
        };

        // Mark previous version as not current
        if let Some(mut current) = self.repository.get_current_version(node.uuid)? {
            current.metadata.is_current = false;
            self.repository.save_version(&current)?;
        }

        // Save new version
        self.repository.save_version(&version)?;

        // Auto-purge if enabled
        if self.config.auto_purge_enabled
            && let Some(max_versions) = self.config.max_versions_per_node
            && current_version_number > max_versions
        {
            let _ = self.repository.purge_old_versions(node.uuid, max_versions);
        }

        Ok(version)
    }

    /// Get a specific version of a node
    pub fn get_version(
        &self,
        node_id: Uuid,
        version_number: u32,
    ) -> Result<Option<NodeVersion<T>>, VersioningError> {
        self.repository.get_version(node_id, version_number)
    }

    /// Get the current (latest) version of a node
    pub fn get_current_version(
        &self,
        node_id: Uuid,
    ) -> Result<Option<NodeVersion<T>>, VersioningError> {
        self.repository.get_current_version(node_id)
    }

    /// Get complete version history for a node
    pub fn get_version_history(&self, node_id: Uuid) -> Result<VersionHistory<T>, VersioningError> {
        self.repository.get_version_history(node_id)
    }

    /// Restore a specific version as the current version
    pub fn restore_version(
        &self,
        node_id: Uuid,
        version_number: u32,
        restored_by: Option<String>,
    ) -> Result<NodeVersion<T>, VersioningError> {
        let version_to_restore = self
            .repository
            .get_version(node_id, version_number)?
            .ok_or_else(|| {
                VersioningError::VersionNotFound(format!("{}:{}", node_id, version_number))
            })?;

        // Create a new version based on the restored data
        let node = Node {
            uuid: node_id,
            created: Utc::now(), // This would normally come from the original node
            modified: Utc::now(),
            node: version_to_restore.node_data,
            name: None, // This would normally come from the original node
            version: true,
        };

        self.create_version(
            &node,
            ChangeType::Restored,
            restored_by,
            Some(format!("Restored from version {}", version_number)),
        )
    }

    /// Compare two versions and return differences
    pub fn compare_versions(
        &self,
        node_id: Uuid,
        version1: u32,
        version2: u32,
    ) -> Result<VersionComparison<T>, VersioningError> {
        let v1 = self
            .repository
            .get_version(node_id, version1)?
            .ok_or_else(|| VersioningError::VersionNotFound(format!("{}:{}", node_id, version1)))?;

        let v2 = self
            .repository
            .get_version(node_id, version2)?
            .ok_or_else(|| VersioningError::VersionNotFound(format!("{}:{}", node_id, version2)))?;

        let hash_changed = v1.metadata.content_hash != v2.metadata.content_hash;
        let size_changed = v1.metadata.size_bytes != v2.metadata.size_bytes;
        let time_diff = v2.created_at.signed_duration_since(v1.created_at);

        Ok(VersionComparison {
            node_id,
            version1: v1,
            version2: v2,
            hash_changed,
            size_changed,
            time_diff,
        })
    }

    /// Purge old versions manually
    pub fn purge_old_versions(
        &self,
        node_id: Uuid,
        keep_count: u32,
    ) -> Result<u32, VersioningError> {
        self.repository.purge_old_versions(node_id, keep_count)
    }

    fn generate_content_hash(&self, content: &T) -> Result<String, VersioningError> {
        let serialized = serde_json::to_vec(content)
            .map_err(|e| VersioningError::SerializationError(e.to_string()))?;

        match self.config.hash_algorithm {
            HashAlgorithm::Sha256 => {
                use sha2::{Digest, Sha256};
                let hash = Sha256::digest(&serialized);
                Ok(format!("{:x}", hash))
            }
            HashAlgorithm::Blake3 => {
                let hash = blake3::hash(&serialized);
                Ok(hash.to_hex().to_string())
            }
            HashAlgorithm::Xxhash => {
                use std::collections::hash_map::DefaultHasher;
                use std::hash::{Hash, Hasher};
                let mut hasher = DefaultHasher::new();
                serialized.hash(&mut hasher);
                Ok(format!("{:x}", hasher.finish()))
            }
        }
    }

    fn calculate_size(&self, content: &T) -> Result<u64, VersioningError> {
        let serialized = serde_json::to_vec(content)
            .map_err(|e| VersioningError::SerializationError(e.to_string()))?;
        Ok(serialized.len() as u64)
    }
}

#[derive(Debug, Clone)]
pub struct VersionComparison<T> {
    pub node_id: Uuid,
    pub version1: NodeVersion<T>,
    pub version2: NodeVersion<T>,
    pub hash_changed: bool,
    pub size_changed: bool,
    pub time_diff: chrono::Duration,
}

/// Type alias for node version storage
type NodeVersionMap<T> = Arc<RwLock<HashMap<(Uuid, u32), NodeVersion<T>>>>;

/// In-memory implementation for testing and development
pub struct InMemoryNodeVersionRepository<T> {
    versions: NodeVersionMap<T>,
    current_versions: Arc<RwLock<HashMap<Uuid, u32>>>,
}

impl<T> Default for InMemoryNodeVersionRepository<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> InMemoryNodeVersionRepository<T> {
    pub fn new() -> Self {
        Self {
            versions: Arc::new(RwLock::new(HashMap::new())),
            current_versions: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl<T> NodeVersionRepository<T> for InMemoryNodeVersionRepository<T>
where
    T: Clone,
{
    fn save_version(&self, version: &NodeVersion<T>) -> Result<(), VersioningError> {
        let mut versions = self
            .versions
            .write()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;
        let mut current_versions = self
            .current_versions
            .write()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;

        versions.insert((version.node_id, version.version_number), version.clone());

        if version.metadata.is_current {
            current_versions.insert(version.node_id, version.version_number);
        }

        Ok(())
    }

    fn get_version(
        &self,
        node_id: Uuid,
        version_number: u32,
    ) -> Result<Option<NodeVersion<T>>, VersioningError> {
        let versions = self
            .versions
            .read()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;
        Ok(versions.get(&(node_id, version_number)).cloned())
    }

    fn get_current_version(
        &self,
        node_id: Uuid,
    ) -> Result<Option<NodeVersion<T>>, VersioningError> {
        let current_versions = self
            .current_versions
            .read()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;
        let versions = self
            .versions
            .read()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;

        if let Some(&current_version_number) = current_versions.get(&node_id) {
            Ok(versions.get(&(node_id, current_version_number)).cloned())
        } else {
            Ok(None)
        }
    }

    fn get_version_history(&self, node_id: Uuid) -> Result<VersionHistory<T>, VersioningError> {
        let versions = self
            .versions
            .read()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;
        let current_versions = self
            .current_versions
            .read()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;

        let mut node_versions: Vec<NodeVersion<T>> = versions
            .iter()
            .filter(|((id, _), _)| *id == node_id)
            .map(|(_, version)| version.clone())
            .collect();

        node_versions.sort_by_key(|a| a.version_number);

        let current_version = current_versions.get(&node_id).copied().unwrap_or(0);
        let total_versions = node_versions.len() as u32;

        Ok(VersionHistory {
            node_id,
            current_version,
            versions: node_versions,
            total_versions,
        })
    }

    fn delete_version(&self, node_id: Uuid, version_number: u32) -> Result<(), VersioningError> {
        let mut versions = self
            .versions
            .write()
            .map_err(|e| VersioningError::StorageError(e.to_string()))?;

        versions.remove(&(node_id, version_number));
        Ok(())
    }

    fn purge_old_versions(&self, node_id: Uuid, keep_count: u32) -> Result<u32, VersioningError> {
        let history = self.get_version_history(node_id)?;

        if history.total_versions <= keep_count {
            return Ok(0);
        }

        let versions_to_delete = history.total_versions - keep_count;
        let mut deleted_count = 0;

        // Delete oldest versions, keeping the most recent ones
        for version in history.versions.iter().take(versions_to_delete as usize) {
            self.delete_version(node_id, version.version_number)?;
            deleted_count += 1;
        }

        Ok(deleted_count)
    }
}

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

    #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
    struct TestData {
        value: String,
        number: i32,
    }

    #[test]
    fn test_node_version_creation() {
        let repository = Arc::new(InMemoryNodeVersionRepository::new());
        let service = NodeVersionService::new(repository, None);

        let test_data = TestData {
            value: "test".to_string(),
            number: 42,
        };

        let node = Node::new(test_data, Some("test_node".to_string()));

        let version = service
            .create_version(
                &node,
                ChangeType::Created,
                Some("test_user".to_string()),
                Some("Initial creation".to_string()),
            )
            .unwrap();

        assert_eq!(version.version_number, 1);
        assert_eq!(version.node_id, node.uuid);
        assert!(version.metadata.is_current);
        assert_eq!(version.metadata.change_type, ChangeType::Created);
    }

    #[test]
    fn test_version_history() {
        let repository = Arc::new(InMemoryNodeVersionRepository::new());
        let service = NodeVersionService::new(repository, None);

        let test_data = TestData {
            value: "test".to_string(),
            number: 42,
        };

        let node = Node::new(test_data.clone(), Some("test_node".to_string()));

        // Create initial version
        service
            .create_version(&node, ChangeType::Created, None, None)
            .unwrap();

        // Create updated version
        let updated_data = TestData {
            value: "updated".to_string(),
            number: 43,
        };
        let updated_node = Node {
            node: updated_data,
            ..node
        };

        service
            .create_version(&updated_node, ChangeType::Updated, None, None)
            .unwrap();

        let history = service.get_version_history(node.uuid).unwrap();
        assert_eq!(history.total_versions, 2);
        assert_eq!(history.current_version, 2);
    }

    #[test]
    fn test_version_restore() {
        let repository = Arc::new(InMemoryNodeVersionRepository::new());
        let service = NodeVersionService::new(repository, None);

        let test_data = TestData {
            value: "original".to_string(),
            number: 1,
        };

        let node = Node::new(test_data.clone(), Some("test_node".to_string()));

        // Create initial version
        service
            .create_version(&node, ChangeType::Created, None, None)
            .unwrap();

        // Create updated version
        let updated_data = TestData {
            value: "updated".to_string(),
            number: 2,
        };
        let updated_node = Node {
            node: updated_data,
            ..node
        };

        service
            .create_version(&updated_node, ChangeType::Updated, None, None)
            .unwrap();

        // Restore original version
        let restored = service
            .restore_version(node.uuid, 1, Some("test_user".to_string()))
            .unwrap();

        assert_eq!(restored.version_number, 3);
        assert_eq!(restored.metadata.change_type, ChangeType::Restored);
    }
}