llm-orchestrator-state 0.1.1

State persistence and recovery for LLM workflow orchestrator
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
// Copyright (c) 2025 LLM DevOps
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Comprehensive unit and integration tests for state persistence.

#[cfg(test)]
mod unit_tests {
    use crate::models::*;
    use serde_json::json;

    #[test]
    fn test_workflow_status_display() {
        assert_eq!(WorkflowStatus::Pending.to_string(), "pending");
        assert_eq!(WorkflowStatus::Running.to_string(), "running");
        assert_eq!(WorkflowStatus::Paused.to_string(), "paused");
        assert_eq!(WorkflowStatus::Completed.to_string(), "completed");
        assert_eq!(WorkflowStatus::Failed.to_string(), "failed");
    }

    #[test]
    fn test_workflow_status_from_str() {
        use std::str::FromStr;

        assert_eq!(WorkflowStatus::from_str("pending").unwrap(), WorkflowStatus::Pending);
        assert_eq!(WorkflowStatus::from_str("RUNNING").unwrap(), WorkflowStatus::Running);
        assert_eq!(WorkflowStatus::from_str("Completed").unwrap(), WorkflowStatus::Completed);
        assert!(WorkflowStatus::from_str("invalid").is_err());
    }

    #[test]
    fn test_step_status_display() {
        assert_eq!(StepStatus::Pending.to_string(), "pending");
        assert_eq!(StepStatus::Running.to_string(), "running");
        assert_eq!(StepStatus::Completed.to_string(), "completed");
        assert_eq!(StepStatus::Failed.to_string(), "failed");
        assert_eq!(StepStatus::Skipped.to_string(), "skipped");
    }

    #[test]
    fn test_step_status_from_str() {
        use std::str::FromStr;

        assert_eq!(StepStatus::from_str("pending").unwrap(), StepStatus::Pending);
        assert_eq!(StepStatus::from_str("RUNNING").unwrap(), StepStatus::Running);
        assert_eq!(StepStatus::from_str("Skipped").unwrap(), StepStatus::Skipped);
        assert!(StepStatus::from_str("invalid").is_err());
    }

    #[test]
    fn test_workflow_state_creation() {
        let state = WorkflowState::new(
            "test-wf-001",
            "Test Workflow",
            Some("user-123".to_string()),
            json!({"key": "value"}),
        );

        assert_eq!(state.workflow_id, "test-wf-001");
        assert_eq!(state.workflow_name, "Test Workflow");
        assert_eq!(state.status, WorkflowStatus::Pending);
        assert_eq!(state.user_id, Some("user-123".to_string()));
        assert!(state.is_active());
        assert!(state.error.is_none());
        assert!(state.completed_at.is_none());
    }

    #[test]
    fn test_workflow_state_lifecycle() {
        let mut state = WorkflowState::new(
            "test-wf",
            "Test",
            None,
            json!({}),
        );

        // Initial state
        assert_eq!(state.status, WorkflowStatus::Pending);
        assert!(state.is_active());

        // Mark as running
        state.mark_running();
        assert_eq!(state.status, WorkflowStatus::Running);
        assert!(state.is_active());
        assert!(state.completed_at.is_none());

        // Mark as completed
        state.mark_completed();
        assert_eq!(state.status, WorkflowStatus::Completed);
        assert!(!state.is_active());
        assert!(state.completed_at.is_some());
        assert!(state.error.is_none());
    }

    #[test]
    fn test_workflow_state_failure() {
        let mut state = WorkflowState::new(
            "test-wf",
            "Test",
            None,
            json!({}),
        );

        state.mark_running();
        state.mark_failed("Something went wrong");

        assert_eq!(state.status, WorkflowStatus::Failed);
        assert!(!state.is_active());
        assert!(state.completed_at.is_some());
        assert_eq!(state.error, Some("Something went wrong".to_string()));
    }

    #[test]
    fn test_step_state_creation() {
        let step = StepState::new("step-1");

        assert_eq!(step.step_id, "step-1");
        assert_eq!(step.status, StepStatus::Pending);
        assert!(step.started_at.is_none());
        assert!(step.completed_at.is_none());
        assert_eq!(step.outputs, serde_json::Value::Null);
        assert!(step.error.is_none());
        assert_eq!(step.retry_count, 0);
    }

    #[test]
    fn test_step_state_lifecycle() {
        let mut step = StepState::new("step-1");

        // Mark as running
        step.mark_running();
        assert_eq!(step.status, StepStatus::Running);
        assert!(step.started_at.is_some());
        assert!(step.completed_at.is_none());

        // Mark as completed
        let outputs = json!({"result": "success", "count": 42});
        step.mark_completed(outputs.clone());
        assert_eq!(step.status, StepStatus::Completed);
        assert!(step.completed_at.is_some());
        assert_eq!(step.outputs, outputs);
        assert!(step.error.is_none());
    }

    #[test]
    fn test_step_state_failure() {
        let mut step = StepState::new("step-1");

        step.mark_running();
        step.mark_failed("Network timeout");

        assert_eq!(step.status, StepStatus::Failed);
        assert!(step.completed_at.is_some());
        assert_eq!(step.error, Some("Network timeout".to_string()));
    }

    #[test]
    fn test_step_state_retry_count() {
        let mut step = StepState::new("step-1");

        assert_eq!(step.retry_count, 0);

        step.increment_retry();
        assert_eq!(step.retry_count, 1);

        step.increment_retry();
        step.increment_retry();
        assert_eq!(step.retry_count, 3);
    }

    #[test]
    fn test_checkpoint_creation() {
        use uuid::Uuid;

        let workflow_id = Uuid::new_v4();
        let snapshot = json!({"state": "data", "progress": 50});

        let checkpoint = Checkpoint::new(workflow_id, "step-5", snapshot.clone());

        assert_eq!(checkpoint.workflow_state_id, workflow_id);
        assert_eq!(checkpoint.step_id, "step-5");
        assert_eq!(checkpoint.snapshot, snapshot);
        // Timestamp should be recent
        assert!((chrono::Utc::now() - checkpoint.timestamp).num_seconds() < 5);
    }

    #[test]
    fn test_workflow_state_serialization() {
        let state = WorkflowState::new(
            "wf-001",
            "Workflow 1",
            Some("user-1".to_string()),
            json!({"test": true}),
        );

        // Serialize
        let json_str = serde_json::to_string(&state).unwrap();
        assert!(json_str.contains("wf-001"));
        assert!(json_str.contains("Workflow 1"));

        // Deserialize
        let deserialized: WorkflowState = serde_json::from_str(&json_str).unwrap();
        assert_eq!(deserialized.workflow_id, state.workflow_id);
        assert_eq!(deserialized.workflow_name, state.workflow_name);
        assert_eq!(deserialized.status, state.status);
    }

    #[test]
    fn test_workflow_state_with_steps() {
        let mut state = WorkflowState::new(
            "wf-001",
            "Workflow with steps",
            None,
            json!({}),
        );

        // Add step states
        let mut step1 = StepState::new("step-1");
        step1.mark_running();
        step1.mark_completed(json!({"result": "done"}));

        let mut step2 = StepState::new("step-2");
        step2.mark_running();

        state.steps.insert("step-1".to_string(), step1);
        state.steps.insert("step-2".to_string(), step2);

        // Verify
        assert_eq!(state.steps.len(), 2);
        assert_eq!(state.steps.get("step-1").unwrap().status, StepStatus::Completed);
        assert_eq!(state.steps.get("step-2").unwrap().status, StepStatus::Running);
    }

    #[test]
    fn test_workflow_state_is_active() {
        let mut state = WorkflowState::new("wf", "test", None, json!({}));

        // Pending is active
        assert!(state.is_active());

        // Running is active
        state.mark_running();
        assert!(state.is_active());

        // Paused is active
        state.status = WorkflowStatus::Paused;
        assert!(state.is_active());

        // Completed is not active
        state.mark_completed();
        assert!(!state.is_active());

        // Failed is not active
        state.status = WorkflowStatus::Failed;
        assert!(!state.is_active());
    }
}

#[cfg(test)]
mod sqlite_integration_tests {
    use crate::{StateStore, SqliteStateStore, WorkflowState, Checkpoint};
    use serde_json::json;
    

    #[tokio::test]
    async fn test_sqlite_store_creation() {
        let store = SqliteStateStore::new(":memory:")
            .await
            .expect("Failed to create store");

        store.health_check().await.expect("Health check failed");
    }

    #[tokio::test]
    async fn test_save_and_load_workflow_state() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        let mut state = WorkflowState::new(
            "test-wf-123",
            "Test Workflow",
            Some("user-456".to_string()),
            json!({"inputs": {"query": "test"}}),
        );
        state.mark_running();

        // Save
        store.save_workflow_state(&state).await.unwrap();

        // Load by ID
        let loaded = store.load_workflow_state(&state.id).await.unwrap();
        assert_eq!(loaded.id, state.id);
        assert_eq!(loaded.workflow_id, state.workflow_id);
        assert_eq!(loaded.status, state.status);

        // Load by workflow_id
        let loaded_by_wf_id = store.load_workflow_state_by_workflow_id("test-wf-123").await.unwrap();
        assert_eq!(loaded_by_wf_id.id, state.id);
    }

    #[tokio::test]
    async fn test_update_workflow_state() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        let mut state = WorkflowState::new(
            "wf-update",
            "Update Test",
            None,
            json!({}),
        );

        // Save initial state
        store.save_workflow_state(&state).await.unwrap();

        // Update state
        state.mark_running();
        store.save_workflow_state(&state).await.unwrap();

        // Load and verify
        let loaded = store.load_workflow_state(&state.id).await.unwrap();
        assert_eq!(loaded.status, crate::WorkflowStatus::Running);

        // Update again
        state.mark_completed();
        store.save_workflow_state(&state).await.unwrap();

        let loaded = store.load_workflow_state(&state.id).await.unwrap();
        assert_eq!(loaded.status, crate::WorkflowStatus::Completed);
        assert!(loaded.completed_at.is_some());
    }

    #[tokio::test]
    async fn test_list_active_workflows() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        // Create multiple workflows
        let mut wf1 = WorkflowState::new("wf-1", "WF 1", None, json!({}));
        wf1.mark_running();
        store.save_workflow_state(&wf1).await.unwrap();

        let mut wf2 = WorkflowState::new("wf-2", "WF 2", None, json!({}));
        wf2.mark_running();
        store.save_workflow_state(&wf2).await.unwrap();

        let mut wf3 = WorkflowState::new("wf-3", "WF 3", None, json!({}));
        wf3.mark_completed();
        store.save_workflow_state(&wf3).await.unwrap();

        // List active (should get wf1 and wf2, not wf3)
        let active = store.list_active_workflows().await.unwrap();
        assert_eq!(active.len(), 2);

        let active_ids: Vec<_> = active.iter().map(|w| w.workflow_id.as_str()).collect();
        assert!(active_ids.contains(&"wf-1"));
        assert!(active_ids.contains(&"wf-2"));
        assert!(!active_ids.contains(&"wf-3"));
    }

    #[tokio::test]
    async fn test_checkpoint_operations() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        let state = WorkflowState::new("wf-cp", "Checkpoint Test", None, json!({}));
        store.save_workflow_state(&state).await.unwrap();

        // Create checkpoint
        let snapshot = serde_json::to_value(&state).unwrap();
        let checkpoint = Checkpoint::new(state.id, "step-1", snapshot);
        store.create_checkpoint(&checkpoint).await.unwrap();

        // Get latest checkpoint
        let latest = store.get_latest_checkpoint(&state.id).await.unwrap();
        assert!(latest.is_some());
        let latest = latest.unwrap();
        assert_eq!(latest.step_id, "step-1");

        // Restore from checkpoint
        let restored = store.restore_from_checkpoint(&checkpoint.id).await.unwrap();
        assert_eq!(restored.id, state.id);
    }

    #[tokio::test]
    async fn test_checkpoint_cleanup() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        let state = WorkflowState::new("wf-cleanup", "Cleanup Test", None, json!({}));
        store.save_workflow_state(&state).await.unwrap();

        // Create 15 checkpoints
        for i in 1..=15 {
            let snapshot = json!({"checkpoint": i});
            let cp = Checkpoint::new(state.id, format!("step-{}", i), snapshot);
            store.create_checkpoint(&cp).await.unwrap();
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await; // Ensure different timestamps
        }

        // Should keep only last 10 (due to auto-cleanup in create_checkpoint)
        // Verify by trying to get latest - should exist
        let latest = store.get_latest_checkpoint(&state.id).await.unwrap();
        assert!(latest.is_some());
    }

    #[tokio::test]
    async fn test_delete_old_states() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        // Create old completed workflow
        let mut old_wf = WorkflowState::new("old-wf", "Old WF", None, json!({}));
        old_wf.mark_completed();
        old_wf.completed_at = Some(chrono::Utc::now() - chrono::Duration::days(30));
        old_wf.updated_at = chrono::Utc::now() - chrono::Duration::days(30); // Set updated_at to match
        store.save_workflow_state(&old_wf).await.unwrap();

        // Create recent workflow
        let mut new_wf = WorkflowState::new("new-wf", "New WF", None, json!({}));
        new_wf.mark_running();
        store.save_workflow_state(&new_wf).await.unwrap();

        // Delete states older than 7 days
        let cutoff = chrono::Utc::now() - chrono::Duration::days(7);
        let deleted = store.delete_old_states(cutoff).await.unwrap();
        assert_eq!(deleted, 1);

        // Verify new workflow still exists
        let result = store.load_workflow_state(&new_wf.id).await;
        assert!(result.is_ok());

        // Verify old workflow is gone
        let result = store.load_workflow_state(&old_wf.id).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_workflow_with_step_states() {
        let store = SqliteStateStore::new(":memory:").await.unwrap();

        let mut state = WorkflowState::new("wf-steps", "WF with Steps", None, json!({}));

        // Add step states
        let mut step1 = crate::StepState::new("step-1");
        step1.mark_running();
        step1.mark_completed(json!({"result": "success"}));

        let mut step2 = crate::StepState::new("step-2");
        step2.mark_running();

        state.steps.insert("step-1".to_string(), step1);
        state.steps.insert("step-2".to_string(), step2);

        // Save
        store.save_workflow_state(&state).await.unwrap();

        // Load and verify
        let loaded = store.load_workflow_state(&state.id).await.unwrap();
        assert_eq!(loaded.steps.len(), 2);
        assert_eq!(loaded.steps.get("step-1").unwrap().status, crate::StepStatus::Completed);
        assert_eq!(loaded.steps.get("step-2").unwrap().status, crate::StepStatus::Running);
    }
}