oxify-model 0.1.0

Data models and types for OxiFY workflows, execution, and configuration
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
//! Checkpoint configuration and storage abstraction
//!
//! This module provides checkpoint frequency configuration and storage traits
//! for saving and restoring workflow execution state.

use crate::{ExecutionCheckpoint, ExecutionId, WorkflowId};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

/// Configuration for checkpoint frequency and behavior
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct CheckpointConfig {
    /// Enable automatic checkpointing
    pub enabled: bool,

    /// Checkpoint frequency strategy
    pub frequency: CheckpointFrequency,

    /// Maximum number of checkpoints to retain per execution
    pub max_checkpoints: usize,

    /// Automatically checkpoint after long-running nodes (threshold in ms)
    pub auto_checkpoint_threshold_ms: Option<u64>,

    /// Compress checkpoint data
    pub compress: bool,
}

impl Default for CheckpointConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            frequency: CheckpointFrequency::EveryNNodes(5),
            max_checkpoints: 10,
            auto_checkpoint_threshold_ms: Some(60000), // 1 minute
            compress: false,
        }
    }
}

/// Checkpoint frequency strategies
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum CheckpointFrequency {
    /// Checkpoint after every N nodes complete
    EveryNNodes(usize),

    /// Checkpoint at specific time intervals (in seconds)
    TimeInterval(u64),

    /// Checkpoint before specific node types (e.g., before expensive LLM calls)
    BeforeNodeTypes(Vec<String>),

    /// Manual checkpointing only (no automatic checkpoints)
    Manual,

    /// Checkpoint at every node completion (maximum safety, higher overhead)
    Always,
}

/// Storage abstraction for checkpoints
pub trait CheckpointStorage: Send + Sync {
    /// Save a checkpoint
    fn save_checkpoint(
        &self,
        execution_id: ExecutionId,
        checkpoint: &ExecutionCheckpoint,
    ) -> Result<CheckpointId, CheckpointError>;

    /// Load the latest checkpoint for an execution
    fn load_latest_checkpoint(
        &self,
        execution_id: ExecutionId,
    ) -> Result<Option<ExecutionCheckpoint>, CheckpointError>;

    /// Load a specific checkpoint by ID
    fn load_checkpoint(
        &self,
        checkpoint_id: CheckpointId,
    ) -> Result<Option<ExecutionCheckpoint>, CheckpointError>;

    /// List all checkpoints for an execution
    fn list_checkpoints(
        &self,
        execution_id: ExecutionId,
    ) -> Result<Vec<CheckpointMetadata>, CheckpointError>;

    /// Delete old checkpoints beyond retention limit
    fn prune_checkpoints(
        &self,
        execution_id: ExecutionId,
        keep_count: usize,
    ) -> Result<usize, CheckpointError>;

    /// Delete all checkpoints for an execution
    fn delete_checkpoints(&self, execution_id: ExecutionId) -> Result<usize, CheckpointError>;
}

/// Unique identifier for a checkpoint
pub type CheckpointId = uuid::Uuid;

/// Metadata about a stored checkpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct CheckpointMetadata {
    /// Unique checkpoint identifier
    #[cfg_attr(feature = "openapi", schema(value_type = String, format = "uuid"))]
    pub id: CheckpointId,

    /// Execution this checkpoint belongs to
    #[cfg_attr(feature = "openapi", schema(value_type = String, format = "uuid"))]
    pub execution_id: ExecutionId,

    /// Workflow being executed
    #[cfg_attr(feature = "openapi", schema(value_type = String, format = "uuid"))]
    pub workflow_id: WorkflowId,

    /// When the checkpoint was created
    pub created_at: DateTime<Utc>,

    /// Number of nodes completed at checkpoint time
    pub completed_node_count: usize,

    /// Size of checkpoint data in bytes
    pub size_bytes: usize,

    /// Whether the checkpoint is compressed
    pub compressed: bool,
}

/// Errors that can occur during checkpoint operations
#[derive(Debug, thiserror::Error)]
pub enum CheckpointError {
    #[error("Checkpoint not found: {0}")]
    NotFound(CheckpointId),

    #[error("Storage error: {0}")]
    StorageError(String),

    #[error("Serialization error: {0}")]
    SerializationError(String),

    #[error("Decompression error: {0}")]
    DecompressionError(String),

    #[error("Invalid checkpoint data: {0}")]
    InvalidData(String),
}

/// In-memory checkpoint storage implementation (for testing/development)
#[derive(Debug, Default)]
pub struct InMemoryCheckpointStorage {
    checkpoints: std::sync::RwLock<HashMap<CheckpointId, (ExecutionId, ExecutionCheckpoint)>>,
    metadata: std::sync::RwLock<HashMap<CheckpointId, CheckpointMetadata>>,
}

impl InMemoryCheckpointStorage {
    pub fn new() -> Self {
        Self::default()
    }
}

impl CheckpointStorage for InMemoryCheckpointStorage {
    fn save_checkpoint(
        &self,
        execution_id: ExecutionId,
        checkpoint: &ExecutionCheckpoint,
    ) -> Result<CheckpointId, CheckpointError> {
        let checkpoint_id = uuid::Uuid::new_v4();

        // Serialize to estimate size
        let data = serde_json::to_vec(checkpoint)
            .map_err(|e| CheckpointError::SerializationError(e.to_string()))?;

        let metadata = CheckpointMetadata {
            id: checkpoint_id,
            execution_id,
            workflow_id: uuid::Uuid::new_v4(), // Would be passed from context
            created_at: checkpoint.timestamp,
            completed_node_count: checkpoint.completed_nodes.len(),
            size_bytes: data.len(),
            compressed: false,
        };

        self.checkpoints
            .write()
            .unwrap()
            .insert(checkpoint_id, (execution_id, checkpoint.clone()));
        self.metadata
            .write()
            .unwrap()
            .insert(checkpoint_id, metadata);

        Ok(checkpoint_id)
    }

    fn load_latest_checkpoint(
        &self,
        execution_id: ExecutionId,
    ) -> Result<Option<ExecutionCheckpoint>, CheckpointError> {
        let checkpoints = self.checkpoints.read().unwrap();

        // Find latest checkpoint for this execution
        let latest = checkpoints
            .iter()
            .filter(|(_, (exec_id, _))| *exec_id == execution_id)
            .map(|(id, (_, checkpoint))| (*id, checkpoint))
            .max_by_key(|(_, checkpoint)| checkpoint.timestamp);

        Ok(latest.map(|(_, checkpoint)| checkpoint.clone()))
    }

    fn load_checkpoint(
        &self,
        checkpoint_id: CheckpointId,
    ) -> Result<Option<ExecutionCheckpoint>, CheckpointError> {
        let checkpoints = self.checkpoints.read().unwrap();
        Ok(checkpoints
            .get(&checkpoint_id)
            .map(|(_, checkpoint)| checkpoint.clone()))
    }

    fn list_checkpoints(
        &self,
        execution_id: ExecutionId,
    ) -> Result<Vec<CheckpointMetadata>, CheckpointError> {
        let metadata = self.metadata.read().unwrap();
        let mut list: Vec<_> = metadata
            .values()
            .filter(|m| m.execution_id == execution_id)
            .cloned()
            .collect();

        // Sort by creation time (newest first)
        list.sort_by(|a, b| b.created_at.cmp(&a.created_at));

        Ok(list)
    }

    fn prune_checkpoints(
        &self,
        execution_id: ExecutionId,
        keep_count: usize,
    ) -> Result<usize, CheckpointError> {
        let list = self.list_checkpoints(execution_id)?;

        if list.len() <= keep_count {
            return Ok(0);
        }

        let to_delete = &list[keep_count..];
        let mut checkpoints = self.checkpoints.write().unwrap();
        let mut metadata = self.metadata.write().unwrap();

        for meta in to_delete {
            checkpoints.remove(&meta.id);
            metadata.remove(&meta.id);
        }

        Ok(to_delete.len())
    }

    fn delete_checkpoints(&self, execution_id: ExecutionId) -> Result<usize, CheckpointError> {
        let list = self.list_checkpoints(execution_id)?;
        let mut checkpoints = self.checkpoints.write().unwrap();
        let mut metadata = self.metadata.write().unwrap();

        for meta in &list {
            checkpoints.remove(&meta.id);
            metadata.remove(&meta.id);
        }

        Ok(list.len())
    }
}

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

    #[test]
    fn test_checkpoint_config_default() {
        let config = CheckpointConfig::default();
        assert!(config.enabled);
        assert_eq!(config.frequency, CheckpointFrequency::EveryNNodes(5));
        assert_eq!(config.max_checkpoints, 10);
    }

    #[test]
    fn test_checkpoint_frequency_variants() {
        let freq1 = CheckpointFrequency::EveryNNodes(10);
        let freq2 = CheckpointFrequency::TimeInterval(60);
        let freq3 = CheckpointFrequency::Manual;
        let freq4 = CheckpointFrequency::Always;

        assert_eq!(freq1, CheckpointFrequency::EveryNNodes(10));
        assert_ne!(freq2, freq3);
        assert_ne!(freq3, freq4);
    }

    #[test]
    fn test_in_memory_storage_save_load() {
        let storage = InMemoryCheckpointStorage::new();
        let execution_id = uuid::Uuid::new_v4();

        let checkpoint = ExecutionCheckpoint {
            timestamp: Utc::now(),
            completed_nodes: vec![uuid::Uuid::new_v4()],
            variables: HashMap::new(),
            state: ExecutionState::Running,
        };

        // Save checkpoint
        let checkpoint_id = storage.save_checkpoint(execution_id, &checkpoint).unwrap();

        // Load checkpoint by ID
        let loaded = storage.load_checkpoint(checkpoint_id).unwrap();
        assert!(loaded.is_some());
        assert_eq!(loaded.unwrap().completed_nodes, checkpoint.completed_nodes);

        // Load latest checkpoint
        let latest = storage.load_latest_checkpoint(execution_id).unwrap();
        assert!(latest.is_some());
    }

    #[test]
    fn test_list_checkpoints() {
        let storage = InMemoryCheckpointStorage::new();
        let execution_id = uuid::Uuid::new_v4();

        // Create multiple checkpoints
        for i in 0..3 {
            let checkpoint = ExecutionCheckpoint {
                timestamp: Utc::now(),
                completed_nodes: vec![uuid::Uuid::new_v4(); i + 1],
                variables: HashMap::new(),
                state: ExecutionState::Running,
            };
            storage.save_checkpoint(execution_id, &checkpoint).unwrap();
        }

        let list = storage.list_checkpoints(execution_id).unwrap();
        assert_eq!(list.len(), 3);
    }

    #[test]
    fn test_prune_checkpoints() {
        let storage = InMemoryCheckpointStorage::new();
        let execution_id = uuid::Uuid::new_v4();

        // Create 5 checkpoints
        for _ in 0..5 {
            let checkpoint = ExecutionCheckpoint {
                timestamp: Utc::now(),
                completed_nodes: vec![uuid::Uuid::new_v4()],
                variables: HashMap::new(),
                state: ExecutionState::Running,
            };
            storage.save_checkpoint(execution_id, &checkpoint).unwrap();
        }

        // Prune to keep only 2
        let deleted = storage.prune_checkpoints(execution_id, 2).unwrap();
        assert_eq!(deleted, 3);

        let remaining = storage.list_checkpoints(execution_id).unwrap();
        assert_eq!(remaining.len(), 2);
    }

    #[test]
    fn test_delete_all_checkpoints() {
        let storage = InMemoryCheckpointStorage::new();
        let execution_id = uuid::Uuid::new_v4();

        // Create checkpoints
        for _ in 0..3 {
            let checkpoint = ExecutionCheckpoint {
                timestamp: Utc::now(),
                completed_nodes: vec![uuid::Uuid::new_v4()],
                variables: HashMap::new(),
                state: ExecutionState::Running,
            };
            storage.save_checkpoint(execution_id, &checkpoint).unwrap();
        }

        // Delete all
        let deleted = storage.delete_checkpoints(execution_id).unwrap();
        assert_eq!(deleted, 3);

        let remaining = storage.list_checkpoints(execution_id).unwrap();
        assert_eq!(remaining.len(), 0);
    }

    #[test]
    fn test_multiple_executions() {
        let storage = InMemoryCheckpointStorage::new();
        let exec1 = uuid::Uuid::new_v4();
        let exec2 = uuid::Uuid::new_v4();

        // Create checkpoints for two different executions
        for exec_id in [exec1, exec2] {
            for _ in 0..2 {
                let checkpoint = ExecutionCheckpoint {
                    timestamp: Utc::now(),
                    completed_nodes: vec![uuid::Uuid::new_v4()],
                    variables: HashMap::new(),
                    state: ExecutionState::Running,
                };
                storage.save_checkpoint(exec_id, &checkpoint).unwrap();
            }
        }

        // Each execution should have 2 checkpoints
        assert_eq!(storage.list_checkpoints(exec1).unwrap().len(), 2);
        assert_eq!(storage.list_checkpoints(exec2).unwrap().len(), 2);

        // Delete one execution's checkpoints
        storage.delete_checkpoints(exec1).unwrap();

        // exec1 should have no checkpoints, exec2 should still have 2
        assert_eq!(storage.list_checkpoints(exec1).unwrap().len(), 0);
        assert_eq!(storage.list_checkpoints(exec2).unwrap().len(), 2);
    }
}