oxigdal-workflow 0.1.4

DAG-based workflow engine for complex geospatial processing pipelines
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
//! Workflow state management and persistence.

use crate::dag::WorkflowDag;
use crate::error::{Result, WorkflowError};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
use tokio::fs;

/// Workflow execution state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowState {
    /// Workflow ID.
    pub workflow_id: String,
    /// Workflow execution ID (unique per run).
    pub execution_id: String,
    /// Current workflow status.
    pub status: WorkflowStatus,
    /// Task states.
    pub task_states: HashMap<String, TaskState>,
    /// Workflow metadata.
    pub metadata: WorkflowMetadata,
    /// Execution context.
    pub context: ExecutionContext,
}

/// Workflow execution status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum WorkflowStatus {
    /// Workflow is pending execution.
    Pending,
    /// Workflow is currently running.
    Running,
    /// Workflow completed successfully.
    Completed,
    /// Workflow failed.
    Failed,
    /// Workflow was cancelled.
    Cancelled,
    /// Workflow is paused.
    Paused,
}

/// Individual task state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaskState {
    /// Task ID.
    pub task_id: String,
    /// Current task status.
    pub status: TaskStatus,
    /// Number of attempts.
    pub attempts: u32,
    /// Task start time.
    pub started_at: Option<DateTime<Utc>>,
    /// Task completion time.
    pub completed_at: Option<DateTime<Utc>>,
    /// Task duration in milliseconds.
    pub duration_ms: Option<u64>,
    /// Task output (if any).
    pub output: Option<serde_json::Value>,
    /// Task error (if any).
    pub error: Option<String>,
    /// Task logs.
    pub logs: Vec<String>,
}

/// Task execution status.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TaskStatus {
    /// Task is pending execution.
    Pending,
    /// Task is currently running.
    Running,
    /// Task completed successfully.
    Completed,
    /// Task failed.
    Failed,
    /// Task was skipped (due to conditionals).
    Skipped,
    /// Task was cancelled.
    Cancelled,
    /// Task is waiting for retry.
    WaitingRetry,
}

/// Workflow metadata.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowMetadata {
    /// Workflow name.
    pub name: String,
    /// Workflow version.
    pub version: String,
    /// Workflow creation time.
    pub created_at: DateTime<Utc>,
    /// Workflow start time.
    pub started_at: Option<DateTime<Utc>>,
    /// Workflow completion time.
    pub completed_at: Option<DateTime<Utc>>,
    /// Total duration in milliseconds.
    pub duration_ms: Option<u64>,
    /// Workflow creator/owner.
    pub owner: Option<String>,
    /// Custom tags.
    pub tags: HashMap<String, String>,
}

/// Execution context shared across tasks.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionContext {
    /// Shared variables.
    pub variables: HashMap<String, serde_json::Value>,
    /// Workflow parameters.
    pub parameters: HashMap<String, serde_json::Value>,
    /// Environment variables.
    pub env: HashMap<String, String>,
}

impl WorkflowState {
    /// Create a new workflow state.
    pub fn new(workflow_id: String, execution_id: String, name: String) -> Self {
        Self {
            workflow_id,
            execution_id,
            status: WorkflowStatus::Pending,
            task_states: HashMap::new(),
            metadata: WorkflowMetadata {
                name,
                version: "1.0.0".to_string(),
                created_at: Utc::now(),
                started_at: None,
                completed_at: None,
                duration_ms: None,
                owner: None,
                tags: HashMap::new(),
            },
            context: ExecutionContext {
                variables: HashMap::new(),
                parameters: HashMap::new(),
                env: HashMap::new(),
            },
        }
    }

    /// Initialize a task state.
    pub fn init_task(&mut self, task_id: String) {
        self.task_states.insert(
            task_id.clone(),
            TaskState {
                task_id,
                status: TaskStatus::Pending,
                attempts: 0,
                started_at: None,
                completed_at: None,
                duration_ms: None,
                output: None,
                error: None,
                logs: Vec::new(),
            },
        );
    }

    /// Mark a task as running.
    pub fn start_task(&mut self, task_id: &str) -> Result<()> {
        let task_state = self
            .task_states
            .get_mut(task_id)
            .ok_or_else(|| WorkflowError::not_found(format!("Task '{}'", task_id)))?;

        task_state.status = TaskStatus::Running;
        task_state.started_at = Some(Utc::now());
        task_state.attempts += 1;

        Ok(())
    }

    /// Mark a task as completed.
    pub fn complete_task(
        &mut self,
        task_id: &str,
        output: Option<serde_json::Value>,
    ) -> Result<()> {
        let task_state = self
            .task_states
            .get_mut(task_id)
            .ok_or_else(|| WorkflowError::not_found(format!("Task '{}'", task_id)))?;

        task_state.status = TaskStatus::Completed;
        task_state.completed_at = Some(Utc::now());
        task_state.output = output;

        if let Some(started) = task_state.started_at {
            task_state.duration_ms = Some(
                (Utc::now() - started)
                    .num_milliseconds()
                    .try_into()
                    .unwrap_or(0),
            );
        }

        Ok(())
    }

    /// Mark a task as failed.
    pub fn fail_task(&mut self, task_id: &str, error: String) -> Result<()> {
        let task_state = self
            .task_states
            .get_mut(task_id)
            .ok_or_else(|| WorkflowError::not_found(format!("Task '{}'", task_id)))?;

        task_state.status = TaskStatus::Failed;
        task_state.completed_at = Some(Utc::now());
        task_state.error = Some(error);

        if let Some(started) = task_state.started_at {
            task_state.duration_ms = Some(
                (Utc::now() - started)
                    .num_milliseconds()
                    .try_into()
                    .unwrap_or(0),
            );
        }

        Ok(())
    }

    /// Mark a task as skipped.
    pub fn skip_task(&mut self, task_id: &str) -> Result<()> {
        let task_state = self
            .task_states
            .get_mut(task_id)
            .ok_or_else(|| WorkflowError::not_found(format!("Task '{}'", task_id)))?;

        task_state.status = TaskStatus::Skipped;
        task_state.completed_at = Some(Utc::now());

        Ok(())
    }

    /// Add a log entry for a task.
    pub fn add_task_log(&mut self, task_id: &str, log: String) -> Result<()> {
        let task_state = self
            .task_states
            .get_mut(task_id)
            .ok_or_else(|| WorkflowError::not_found(format!("Task '{}'", task_id)))?;

        task_state.logs.push(log);

        Ok(())
    }

    /// Start the workflow execution.
    pub fn start(&mut self) {
        self.status = WorkflowStatus::Running;
        self.metadata.started_at = Some(Utc::now());
    }

    /// Mark the workflow as completed.
    pub fn complete(&mut self) {
        self.status = WorkflowStatus::Completed;
        self.metadata.completed_at = Some(Utc::now());

        if let Some(started) = self.metadata.started_at {
            self.metadata.duration_ms = Some(
                (Utc::now() - started)
                    .num_milliseconds()
                    .try_into()
                    .unwrap_or(0),
            );
        }
    }

    /// Mark the workflow as failed.
    pub fn fail(&mut self) {
        self.status = WorkflowStatus::Failed;
        self.metadata.completed_at = Some(Utc::now());

        if let Some(started) = self.metadata.started_at {
            self.metadata.duration_ms = Some(
                (Utc::now() - started)
                    .num_milliseconds()
                    .try_into()
                    .unwrap_or(0),
            );
        }
    }

    /// Mark the workflow as cancelled.
    pub fn cancel(&mut self) {
        self.status = WorkflowStatus::Cancelled;
        self.metadata.completed_at = Some(Utc::now());

        if let Some(started) = self.metadata.started_at {
            self.metadata.duration_ms = Some(
                (Utc::now() - started)
                    .num_milliseconds()
                    .try_into()
                    .unwrap_or(0),
            );
        }
    }

    /// Get task state.
    pub fn get_task_state(&self, task_id: &str) -> Option<&TaskState> {
        self.task_states.get(task_id)
    }

    /// Set a context variable.
    pub fn set_variable(&mut self, key: String, value: serde_json::Value) {
        self.context.variables.insert(key, value);
    }

    /// Get a context variable.
    pub fn get_variable(&self, key: &str) -> Option<&serde_json::Value> {
        self.context.variables.get(key)
    }

    /// Check if the workflow is terminal (completed, failed, or cancelled).
    pub fn is_terminal(&self) -> bool {
        matches!(
            self.status,
            WorkflowStatus::Completed | WorkflowStatus::Failed | WorkflowStatus::Cancelled
        )
    }
}

/// Workflow checkpoint containing both state and DAG for recovery.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowCheckpoint {
    /// Checkpoint version for compatibility.
    pub version: u32,
    /// Timestamp when checkpoint was created.
    pub created_at: DateTime<Utc>,
    /// Checkpoint sequence number (increments with each save).
    pub sequence: u64,
    /// The workflow state.
    pub state: WorkflowState,
    /// The workflow DAG definition.
    pub dag: WorkflowDag,
}

impl WorkflowCheckpoint {
    /// Current checkpoint format version.
    pub const CURRENT_VERSION: u32 = 1;

    /// Create a new checkpoint from state and DAG.
    pub fn new(state: WorkflowState, dag: WorkflowDag, sequence: u64) -> Self {
        Self {
            version: Self::CURRENT_VERSION,
            created_at: Utc::now(),
            sequence,
            state,
            dag,
        }
    }

    /// Get tasks that need to be executed (pending or failed but retriable).
    pub fn get_pending_tasks(&self) -> Vec<String> {
        self.state
            .task_states
            .iter()
            .filter(|(_, ts)| matches!(ts.status, TaskStatus::Pending | TaskStatus::WaitingRetry))
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Get tasks that were running when checkpoint was saved (need retry).
    pub fn get_interrupted_tasks(&self) -> Vec<String> {
        self.state
            .task_states
            .iter()
            .filter(|(_, ts)| ts.status == TaskStatus::Running)
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Get tasks that completed successfully.
    pub fn get_completed_tasks(&self) -> Vec<String> {
        self.state
            .task_states
            .iter()
            .filter(|(_, ts)| ts.status == TaskStatus::Completed)
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Get tasks that failed (not retriable).
    pub fn get_failed_tasks(&self) -> Vec<String> {
        self.state
            .task_states
            .iter()
            .filter(|(_, ts)| ts.status == TaskStatus::Failed)
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Get tasks that were skipped.
    pub fn get_skipped_tasks(&self) -> Vec<String> {
        self.state
            .task_states
            .iter()
            .filter(|(_, ts)| ts.status == TaskStatus::Skipped)
            .map(|(id, _)| id.clone())
            .collect()
    }

    /// Check if all dependencies for a task are satisfied.
    pub fn are_dependencies_satisfied(&self, task_id: &str) -> bool {
        let dependencies = self.dag.get_dependencies(task_id);
        dependencies.iter().all(|dep_id| {
            self.state
                .task_states
                .get(dep_id)
                .map(|ts| ts.status == TaskStatus::Completed)
                .unwrap_or(false)
        })
    }

    /// Get tasks ready to execute (pending with satisfied dependencies).
    pub fn get_ready_tasks(&self) -> Vec<String> {
        self.get_pending_tasks()
            .into_iter()
            .filter(|task_id| self.are_dependencies_satisfied(task_id))
            .collect()
    }

    /// Prepare state for resumption by resetting interrupted tasks.
    pub fn prepare_for_resume(&mut self) -> Result<()> {
        // Reset interrupted (running) tasks to pending for retry
        let interrupted = self.get_interrupted_tasks();
        for task_id in interrupted {
            if let Some(task_state) = self.state.task_states.get_mut(&task_id) {
                task_state.status = TaskStatus::Pending;
                // Keep attempt count for proper retry tracking
            }
        }

        // Reset workflow status to running
        if self.state.status == WorkflowStatus::Paused {
            self.state.status = WorkflowStatus::Running;
        }

        Ok(())
    }
}

/// State persistence manager.
pub struct StatePersistence {
    /// Directory for state storage.
    state_dir: String,
}

impl StatePersistence {
    /// Create a new state persistence manager.
    pub fn new(state_dir: String) -> Self {
        Self { state_dir }
    }

    /// Save workflow state to disk.
    pub async fn save(&self, state: &WorkflowState) -> Result<()> {
        let dir_path = Path::new(&self.state_dir);
        fs::create_dir_all(dir_path).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to create state dir: {}", e))
        })?;

        let file_path = dir_path.join(format!("{}.json", state.execution_id));
        let json = serde_json::to_string_pretty(state)?;

        fs::write(&file_path, json)
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to write state: {}", e)))?;

        Ok(())
    }

    /// Load workflow state from disk.
    pub async fn load(&self, execution_id: &str) -> Result<WorkflowState> {
        let file_path = Path::new(&self.state_dir).join(format!("{}.json", execution_id));

        let json = fs::read_to_string(&file_path)
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read state: {}", e)))?;

        let state = serde_json::from_str(&json)?;
        Ok(state)
    }

    /// Delete workflow state from disk.
    pub async fn delete(&self, execution_id: &str) -> Result<()> {
        let file_path = Path::new(&self.state_dir).join(format!("{}.json", execution_id));

        fs::remove_file(&file_path)
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to delete state: {}", e)))?;

        Ok(())
    }

    /// List all workflow states.
    pub async fn list(&self) -> Result<Vec<String>> {
        let dir_path = Path::new(&self.state_dir);

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

        let mut entries = fs::read_dir(dir_path)
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read state dir: {}", e)))?;

        let mut execution_ids = Vec::new();

        while let Some(entry) = entries
            .next_entry()
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read entry: {}", e)))?
        {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some("json") {
                if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                    execution_ids.push(stem.to_string());
                }
            }
        }

        Ok(execution_ids)
    }

    /// Save a workflow checkpoint (state + DAG) to disk.
    pub async fn save_checkpoint(&self, checkpoint: &WorkflowCheckpoint) -> Result<()> {
        let dir_path = Path::new(&self.state_dir).join("checkpoints");
        fs::create_dir_all(&dir_path).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to create checkpoint dir: {}", e))
        })?;

        let file_path = dir_path.join(format!(
            "{}_checkpoint_{}.json",
            checkpoint.state.execution_id, checkpoint.sequence
        ));
        let json = serde_json::to_string_pretty(checkpoint)?;

        fs::write(&file_path, json).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to write checkpoint: {}", e))
        })?;

        // Also save a "latest" symlink/copy for easy access
        let latest_path = dir_path.join(format!("{}_latest.json", checkpoint.state.execution_id));
        let json_latest = serde_json::to_string_pretty(checkpoint)?;
        fs::write(&latest_path, json_latest).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to write latest checkpoint: {}", e))
        })?;

        Ok(())
    }

    /// Load the latest checkpoint for an execution.
    pub async fn load_checkpoint(&self, execution_id: &str) -> Result<WorkflowCheckpoint> {
        let latest_path = Path::new(&self.state_dir)
            .join("checkpoints")
            .join(format!("{}_latest.json", execution_id));

        let json = fs::read_to_string(&latest_path)
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read checkpoint: {}", e)))?;

        let checkpoint: WorkflowCheckpoint = serde_json::from_str(&json)?;

        // Validate checkpoint version
        if checkpoint.version > WorkflowCheckpoint::CURRENT_VERSION {
            return Err(WorkflowError::persistence(format!(
                "Checkpoint version {} is newer than supported version {}",
                checkpoint.version,
                WorkflowCheckpoint::CURRENT_VERSION
            )));
        }

        Ok(checkpoint)
    }

    /// Load a specific checkpoint by sequence number.
    pub async fn load_checkpoint_by_sequence(
        &self,
        execution_id: &str,
        sequence: u64,
    ) -> Result<WorkflowCheckpoint> {
        let file_path = Path::new(&self.state_dir)
            .join("checkpoints")
            .join(format!("{}_checkpoint_{}.json", execution_id, sequence));

        let json = fs::read_to_string(&file_path)
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read checkpoint: {}", e)))?;

        let checkpoint: WorkflowCheckpoint = serde_json::from_str(&json)?;
        Ok(checkpoint)
    }

    /// Delete a checkpoint.
    pub async fn delete_checkpoint(&self, execution_id: &str, sequence: u64) -> Result<()> {
        let file_path = Path::new(&self.state_dir)
            .join("checkpoints")
            .join(format!("{}_checkpoint_{}.json", execution_id, sequence));

        fs::remove_file(&file_path).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to delete checkpoint: {}", e))
        })?;

        Ok(())
    }

    /// Delete all checkpoints for an execution.
    pub async fn delete_all_checkpoints(&self, execution_id: &str) -> Result<()> {
        let checkpoints_dir = Path::new(&self.state_dir).join("checkpoints");

        if !checkpoints_dir.exists() {
            return Ok(());
        }

        let mut entries = fs::read_dir(&checkpoints_dir).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to read checkpoints dir: {}", e))
        })?;

        let prefix = format!("{}_", execution_id);

        while let Some(entry) = entries
            .next_entry()
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read entry: {}", e)))?
        {
            let path = entry.path();
            if let Some(name) = path.file_name().and_then(|s| s.to_str()) {
                if name.starts_with(&prefix) {
                    fs::remove_file(&path).await.map_err(|e| {
                        WorkflowError::persistence(format!("Failed to delete checkpoint: {}", e))
                    })?;
                }
            }
        }

        Ok(())
    }

    /// List all checkpoints for an execution (sorted by sequence).
    pub async fn list_checkpoints(&self, execution_id: &str) -> Result<Vec<u64>> {
        let checkpoints_dir = Path::new(&self.state_dir).join("checkpoints");

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

        let mut entries = fs::read_dir(&checkpoints_dir).await.map_err(|e| {
            WorkflowError::persistence(format!("Failed to read checkpoints dir: {}", e))
        })?;

        let mut sequences = Vec::new();
        let prefix = format!("{}_checkpoint_", execution_id);

        while let Some(entry) = entries
            .next_entry()
            .await
            .map_err(|e| WorkflowError::persistence(format!("Failed to read entry: {}", e)))?
        {
            let path = entry.path();
            if let Some(name) = path.file_stem().and_then(|s| s.to_str()) {
                if name.starts_with(&prefix) {
                    if let Some(seq_str) = name.strip_prefix(&prefix) {
                        if let Ok(seq) = seq_str.parse::<u64>() {
                            sequences.push(seq);
                        }
                    }
                }
            }
        }

        sequences.sort();
        Ok(sequences)
    }

    /// Check if a checkpoint exists for an execution.
    pub async fn checkpoint_exists(&self, execution_id: &str) -> bool {
        let latest_path = Path::new(&self.state_dir)
            .join("checkpoints")
            .join(format!("{}_latest.json", execution_id));
        latest_path.exists()
    }
}

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

    #[test]
    fn test_workflow_state_lifecycle() {
        let mut state = WorkflowState::new(
            "wf1".to_string(),
            "exec1".to_string(),
            "Test Workflow".to_string(),
        );

        assert_eq!(state.status, WorkflowStatus::Pending);

        state.start();
        assert_eq!(state.status, WorkflowStatus::Running);
        assert!(state.metadata.started_at.is_some());

        state.complete();
        assert_eq!(state.status, WorkflowStatus::Completed);
        assert!(state.metadata.completed_at.is_some());
        assert!(state.metadata.duration_ms.is_some());
    }

    #[test]
    fn test_task_state_lifecycle() {
        let mut state = WorkflowState::new(
            "wf1".to_string(),
            "exec1".to_string(),
            "Test Workflow".to_string(),
        );

        state.init_task("task1".to_string());
        assert_eq!(
            state.get_task_state("task1").map(|t| t.status),
            Some(TaskStatus::Pending)
        );

        state.start_task("task1").ok();
        assert_eq!(
            state.get_task_state("task1").map(|t| t.status),
            Some(TaskStatus::Running)
        );
        assert_eq!(state.get_task_state("task1").map(|t| t.attempts), Some(1));

        state
            .complete_task("task1", Some(serde_json::json!({"result": "success"})))
            .ok();
        assert_eq!(
            state.get_task_state("task1").map(|t| t.status),
            Some(TaskStatus::Completed)
        );
    }

    #[test]
    fn test_context_variables() {
        let mut state = WorkflowState::new(
            "wf1".to_string(),
            "exec1".to_string(),
            "Test Workflow".to_string(),
        );

        state.set_variable("key1".to_string(), serde_json::json!("value1"));
        assert_eq!(
            state.get_variable("key1"),
            Some(&serde_json::json!("value1"))
        );
    }
}