symbi-runtime 1.10.0

Agent Runtime System for the Symbi platform
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
//! Saga orchestrator for multi-step tool sequences
//!
//! Provides forward execution with backward compensation on failure.
//! Tool actions are classified as ReadOnly, Compensatable, or Final,
//! enabling automatic rollback when a sequence fails partway through.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;

/// Classification of a saga step's side effects.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum StepClassification {
    /// No side effects. Safe to skip during compensation.
    ReadOnly,
    /// Has side effects but can be reversed via a compensation action.
    Compensatable,
    /// Irreversible. Only permitted after all preceding steps succeed.
    Final,
}

/// A single step in a saga.
#[derive(Debug, Clone)]
pub struct SagaStep {
    /// Step name for logging and audit.
    pub name: String,
    /// Side-effect classification.
    pub classification: StepClassification,
    /// The forward action to execute.
    pub action: SagaAction,
    /// The compensation action (only for Compensatable steps).
    pub compensation: Option<SagaAction>,
}

/// An action in a saga (either forward or compensation).
#[derive(Debug, Clone)]
pub struct SagaAction {
    /// Tool name to invoke.
    pub tool_name: String,
    /// Arguments for the tool.
    pub arguments: serde_json::Value,
}

/// Result of a single saga step execution.
#[derive(Debug, Clone)]
pub struct StepResult {
    /// Step name.
    pub name: String,
    /// Whether the step succeeded.
    pub success: bool,
    /// Output from the step.
    pub output: String,
    /// Error message if failed.
    pub error: Option<String>,
}

/// Overall saga execution result.
#[derive(Debug, Clone)]
pub struct SagaResult {
    /// Whether the entire saga succeeded.
    pub success: bool,
    /// Results from each step (forward execution).
    pub step_results: Vec<StepResult>,
    /// Results from compensation steps (if saga failed).
    pub compensation_results: Vec<StepResult>,
    /// Summary of what happened.
    pub summary: String,
}

/// Unique key for idempotent execution of saga steps.
///
/// Enables safe retries of Final steps: if a step has already been executed
/// with a given key, re-execution is skipped.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IdempotencyKey(pub String);

impl IdempotencyKey {
    /// Generate a new unique idempotency key.
    pub fn generate() -> Self {
        Self(uuid::Uuid::new_v4().to_string())
    }
}

/// Persistence hook called before Final steps execute.
///
/// Records intent before execution and outcome after, enabling recovery
/// of Final steps that were started but not confirmed (e.g., process crash).
#[async_trait::async_trait]
pub trait SagaCheckpoint: Send + Sync {
    /// Record that we intend to execute this Final step.
    async fn record_intent(&self, step: &SagaStep, key: &IdempotencyKey) -> Result<(), SagaError>;

    /// Record the outcome of a Final step execution.
    async fn record_outcome(&self, key: &IdempotencyKey, success: bool) -> Result<(), SagaError>;

    /// Return all intents that were recorded but have no corresponding outcome.
    async fn pending_intents(&self) -> Result<Vec<(SagaStep, IdempotencyKey)>, SagaError>;
}

/// In-memory checkpoint implementation.
///
/// Suitable for testing and single-process use. Pairs with the
/// `MemoryJournalStorage` philosophy — no external dependencies required.
pub struct InMemoryCheckpoint {
    intents: Mutex<Vec<(SagaStep, IdempotencyKey)>>,
    outcomes: Mutex<HashMap<String, bool>>,
}

impl Default for InMemoryCheckpoint {
    fn default() -> Self {
        Self::new()
    }
}

impl InMemoryCheckpoint {
    pub fn new() -> Self {
        Self {
            intents: Mutex::new(Vec::new()),
            outcomes: Mutex::new(HashMap::new()),
        }
    }
}

#[async_trait::async_trait]
impl SagaCheckpoint for InMemoryCheckpoint {
    async fn record_intent(&self, step: &SagaStep, key: &IdempotencyKey) -> Result<(), SagaError> {
        self.intents.lock().await.push((step.clone(), key.clone()));
        Ok(())
    }

    async fn record_outcome(&self, key: &IdempotencyKey, success: bool) -> Result<(), SagaError> {
        self.outcomes.lock().await.insert(key.0.clone(), success);
        Ok(())
    }

    async fn pending_intents(&self) -> Result<Vec<(SagaStep, IdempotencyKey)>, SagaError> {
        let intents = self.intents.lock().await;
        let outcomes = self.outcomes.lock().await;
        Ok(intents
            .iter()
            .filter(|(_, key)| !outcomes.contains_key(&key.0))
            .cloned()
            .collect())
    }
}

/// Orchestrates saga execution with compensation.
pub struct SagaOrchestrator {
    steps: Vec<SagaStep>,
}

impl SagaOrchestrator {
    /// Create a new saga with the given steps.
    ///
    /// Validates that Final steps only appear after all Compensatable steps.
    pub fn new(steps: Vec<SagaStep>) -> Result<Self, SagaError> {
        // Validate: no Compensatable or ReadOnly steps after a Final step
        let mut seen_final = false;
        for step in &steps {
            if seen_final && step.classification != StepClassification::Final {
                return Err(SagaError::InvalidStepOrder {
                    step: step.name.clone(),
                    reason: "Non-final steps cannot appear after a Final step".into(),
                });
            }
            if step.classification == StepClassification::Final {
                seen_final = true;
            }
        }

        // Validate: Compensatable steps must have compensation actions
        for step in &steps {
            if step.classification == StepClassification::Compensatable
                && step.compensation.is_none()
            {
                return Err(SagaError::MissingCompensation {
                    step: step.name.clone(),
                });
            }
        }

        Ok(Self { steps })
    }

    /// Execute the saga using the provided executor function.
    ///
    /// The executor takes (tool_name, arguments) and returns (success, output).
    pub async fn execute<F, Fut>(&self, executor: F) -> SagaResult
    where
        F: Fn(String, serde_json::Value) -> Fut,
        Fut: std::future::Future<Output = Result<String, String>>,
    {
        self.execute_inner(&executor, None).await
    }

    /// Execute the saga with a checkpoint for Final step durability.
    ///
    /// Before each Final step, records intent via the checkpoint. After
    /// execution, records the outcome. On crash, `recover()` can identify
    /// Final steps that were intended but not confirmed.
    pub async fn execute_with_checkpoint<F, Fut>(
        &self,
        executor: F,
        checkpoint: Arc<dyn SagaCheckpoint>,
    ) -> SagaResult
    where
        F: Fn(String, serde_json::Value) -> Fut,
        Fut: std::future::Future<Output = Result<String, String>>,
    {
        self.execute_inner(&executor, Some(checkpoint)).await
    }

    async fn execute_inner<F, Fut>(
        &self,
        executor: &F,
        checkpoint: Option<Arc<dyn SagaCheckpoint>>,
    ) -> SagaResult
    where
        F: Fn(String, serde_json::Value) -> Fut,
        Fut: std::future::Future<Output = Result<String, String>>,
    {
        let mut step_results = Vec::new();
        let mut completed_compensatable: Vec<&SagaStep> = Vec::new();

        for step in &self.steps {
            // For Final steps with a checkpoint, record intent before execution
            let idempotency_key = if step.classification == StepClassification::Final {
                if let Some(cp) = &checkpoint {
                    let key = IdempotencyKey::generate();
                    if let Err(e) = cp.record_intent(step, &key).await {
                        return SagaResult {
                            success: false,
                            step_results,
                            compensation_results: self
                                .compensate(&completed_compensatable, executor)
                                .await,
                            summary: format!(
                                "Failed to record intent for Final step '{}': {}",
                                step.name, e
                            ),
                        };
                    }
                    Some(key)
                } else {
                    None
                }
            } else {
                None
            };

            let result =
                executor(step.action.tool_name.clone(), step.action.arguments.clone()).await;

            match result {
                Ok(output) => {
                    // Record successful outcome for Final steps
                    if let Some(key) = &idempotency_key {
                        if let Some(cp) = &checkpoint {
                            let _ = cp.record_outcome(key, true).await;
                        }
                    }

                    step_results.push(StepResult {
                        name: step.name.clone(),
                        success: true,
                        output,
                        error: None,
                    });

                    if step.classification == StepClassification::Compensatable {
                        completed_compensatable.push(step);
                    }
                }
                Err(error) => {
                    // Record failed outcome for Final steps
                    if let Some(key) = &idempotency_key {
                        if let Some(cp) = &checkpoint {
                            let _ = cp.record_outcome(key, false).await;
                        }
                    }

                    step_results.push(StepResult {
                        name: step.name.clone(),
                        success: false,
                        output: String::new(),
                        error: Some(error.clone()),
                    });

                    // Compensate in reverse order
                    let compensation_results =
                        self.compensate(&completed_compensatable, executor).await;

                    return SagaResult {
                        success: false,
                        step_results,
                        compensation_results,
                        summary: format!("Saga failed at step '{}': {}", step.name, error),
                    };
                }
            }
        }

        SagaResult {
            success: true,
            step_results,
            compensation_results: Vec::new(),
            summary: "Saga completed successfully".into(),
        }
    }

    /// Recover pending Final steps from a checkpoint.
    ///
    /// Returns the list of Final steps that were intended but never got an
    /// outcome recorded (e.g., due to process crash). The caller can then
    /// decide to re-execute or report them.
    pub async fn recover<F, Fut>(
        checkpoint: &dyn SagaCheckpoint,
        executor: F,
    ) -> Result<Vec<StepResult>, SagaError>
    where
        F: Fn(String, serde_json::Value) -> Fut,
        Fut: std::future::Future<Output = Result<String, String>>,
    {
        let pending = checkpoint.pending_intents().await?;
        let mut results = Vec::new();

        for (step, key) in &pending {
            let result =
                executor(step.action.tool_name.clone(), step.action.arguments.clone()).await;

            let success = result.is_ok();
            let _ = checkpoint.record_outcome(key, success).await;

            results.push(StepResult {
                name: format!("recover:{}", step.name),
                success,
                output: result.as_ref().cloned().unwrap_or_default(),
                error: result.err(),
            });
        }

        Ok(results)
    }

    async fn compensate<F, Fut>(&self, completed: &[&SagaStep], executor: &F) -> Vec<StepResult>
    where
        F: Fn(String, serde_json::Value) -> Fut,
        Fut: std::future::Future<Output = Result<String, String>>,
    {
        let mut results = Vec::new();

        // Compensate in reverse order
        for step in completed.iter().rev() {
            if let Some(compensation) = &step.compensation {
                let result = executor(
                    compensation.tool_name.clone(),
                    compensation.arguments.clone(),
                )
                .await;

                results.push(StepResult {
                    name: format!("compensate:{}", step.name),
                    success: result.is_ok(),
                    output: result.as_ref().cloned().unwrap_or_default(),
                    error: result.err(),
                });
            }
        }

        results
    }

    /// Get the step count.
    pub fn step_count(&self) -> usize {
        self.steps.len()
    }

    /// Get steps by classification.
    pub fn steps_by_classification(&self) -> HashMap<String, usize> {
        let mut counts = HashMap::new();
        for step in &self.steps {
            let key = format!("{:?}", step.classification);
            *counts.entry(key).or_insert(0) += 1;
        }
        counts
    }
}

/// Errors from the saga orchestrator.
#[derive(Debug, thiserror::Error)]
pub enum SagaError {
    #[error("Invalid step order for '{step}': {reason}")]
    InvalidStepOrder { step: String, reason: String },

    #[error("Compensatable step '{step}' is missing a compensation action")]
    MissingCompensation { step: String },

    #[error("Checkpoint operation failed: {0}")]
    CheckpointFailed(String),
}

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

    fn read_step(name: &str) -> SagaStep {
        SagaStep {
            name: name.into(),
            classification: StepClassification::ReadOnly,
            action: SagaAction {
                tool_name: "read".into(),
                arguments: serde_json::json!({"path": name}),
            },
            compensation: None,
        }
    }

    fn write_step(name: &str) -> SagaStep {
        SagaStep {
            name: name.into(),
            classification: StepClassification::Compensatable,
            action: SagaAction {
                tool_name: "write".into(),
                arguments: serde_json::json!({"path": name, "data": "content"}),
            },
            compensation: Some(SagaAction {
                tool_name: "delete".into(),
                arguments: serde_json::json!({"path": name}),
            }),
        }
    }

    fn final_step(name: &str) -> SagaStep {
        SagaStep {
            name: name.into(),
            classification: StepClassification::Final,
            action: SagaAction {
                tool_name: "publish".into(),
                arguments: serde_json::json!({"target": name}),
            },
            compensation: None,
        }
    }

    #[test]
    fn test_valid_saga_creation() {
        let saga = SagaOrchestrator::new(vec![
            read_step("check"),
            write_step("create"),
            write_step("update"),
            final_step("publish"),
        ]);
        assert!(saga.is_ok());
        assert_eq!(saga.unwrap().step_count(), 4);
    }

    #[test]
    fn test_invalid_order_non_final_after_final() {
        let saga = SagaOrchestrator::new(vec![
            write_step("create"),
            final_step("publish"),
            read_step("check"), // Invalid: ReadOnly after Final
        ]);
        assert!(saga.is_err());
    }

    #[test]
    fn test_missing_compensation() {
        let step = SagaStep {
            name: "bad".into(),
            classification: StepClassification::Compensatable,
            action: SagaAction {
                tool_name: "write".into(),
                arguments: serde_json::json!({}),
            },
            compensation: None, // Missing!
        };
        let saga = SagaOrchestrator::new(vec![step]);
        assert!(saga.is_err());
    }

    #[tokio::test]
    async fn test_saga_all_succeed() {
        let saga = SagaOrchestrator::new(vec![
            read_step("check"),
            write_step("create"),
            write_step("update"),
        ])
        .unwrap();

        let result = saga
            .execute(|_tool, _args| async { Ok("success".to_string()) })
            .await;

        assert!(result.success);
        assert_eq!(result.step_results.len(), 3);
        assert!(result.compensation_results.is_empty());
    }

    #[tokio::test]
    async fn test_saga_fail_at_step_3_compensates_2_and_1() {
        let saga = SagaOrchestrator::new(vec![
            read_step("check"),
            write_step("create"),
            write_step("update"),
            write_step("finalize"),
            final_step("publish"),
        ])
        .unwrap();

        let call_count = std::sync::Arc::new(std::sync::atomic::AtomicU32::new(0));
        let cc = call_count.clone();

        let result = saga
            .execute(move |_tool, _args| {
                let count = cc.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                async move {
                    if count == 3 {
                        // Fail at step 4 (finalize)
                        Err("Connection refused".to_string())
                    } else {
                        Ok("ok".to_string())
                    }
                }
            })
            .await;

        assert!(!result.success);
        assert_eq!(result.step_results.len(), 4); // 3 succeeded + 1 failed
        assert!(!result.step_results[3].success);

        // Compensation should run for create and update (in reverse order)
        assert_eq!(result.compensation_results.len(), 2);
        assert_eq!(result.compensation_results[0].name, "compensate:update");
        assert_eq!(result.compensation_results[1].name, "compensate:create");
    }

    #[tokio::test]
    async fn test_saga_fail_at_readonly_no_compensation() {
        let saga = SagaOrchestrator::new(vec![read_step("check")]).unwrap();

        let result = saga
            .execute(|_tool, _args| async { Err("fail".to_string()) })
            .await;

        assert!(!result.success);
        assert!(result.compensation_results.is_empty());
    }

    #[test]
    fn test_steps_by_classification() {
        let saga = SagaOrchestrator::new(vec![
            read_step("r1"),
            write_step("w1"),
            write_step("w2"),
            final_step("f1"),
        ])
        .unwrap();

        let counts = saga.steps_by_classification();
        assert_eq!(counts.get("ReadOnly"), Some(&1));
        assert_eq!(counts.get("Compensatable"), Some(&2));
        assert_eq!(counts.get("Final"), Some(&1));
    }

    #[tokio::test]
    async fn test_final_step_with_checkpoint_records_intent_and_outcome() {
        let checkpoint = Arc::new(InMemoryCheckpoint::new());
        let saga =
            SagaOrchestrator::new(vec![write_step("prepare"), final_step("publish")]).unwrap();

        let result = saga
            .execute_with_checkpoint(
                |_tool, _args| async { Ok("done".to_string()) },
                checkpoint.clone(),
            )
            .await;

        assert!(result.success);

        // Intent was recorded for the Final step
        let intents = checkpoint.intents.lock().await;
        assert_eq!(intents.len(), 1);
        assert_eq!(intents[0].0.name, "publish");

        // Outcome was recorded
        let outcomes = checkpoint.outcomes.lock().await;
        assert_eq!(outcomes.len(), 1);
        assert!(outcomes.values().next().unwrap());

        // No pending intents (outcome recorded)
        drop(intents);
        drop(outcomes);
        let pending = checkpoint.pending_intents().await.unwrap();
        assert!(pending.is_empty());
    }

    #[tokio::test]
    async fn test_simulated_crash_leaves_pending_intent() {
        let checkpoint = Arc::new(InMemoryCheckpoint::new());

        // Manually record an intent with no outcome (simulates crash)
        let step = final_step("deploy");
        let key = IdempotencyKey::generate();
        checkpoint.record_intent(&step, &key).await.unwrap();

        // No outcome recorded → pending_intents should return it
        let pending = checkpoint.pending_intents().await.unwrap();
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].0.name, "deploy");
        assert_eq!(pending[0].1, key);
    }

    #[tokio::test]
    async fn test_recovery_re_executes_pending_final_step() {
        let checkpoint = Arc::new(InMemoryCheckpoint::new());

        // Simulate a crash: intent recorded, no outcome
        let step = final_step("deploy");
        let key = IdempotencyKey::generate();
        checkpoint.record_intent(&step, &key).await.unwrap();

        // Recover
        let results = SagaOrchestrator::recover(checkpoint.as_ref(), |_tool, _args| async {
            Ok("recovered".to_string())
        })
        .await
        .unwrap();

        assert_eq!(results.len(), 1);
        assert!(results[0].success);
        assert_eq!(results[0].name, "recover:deploy");
        assert_eq!(results[0].output, "recovered");

        // After recovery, no more pending intents
        let pending = checkpoint.pending_intents().await.unwrap();
        assert!(pending.is_empty());
    }

    #[tokio::test]
    async fn test_idempotency_key_prevents_double_execution() {
        let checkpoint = Arc::new(InMemoryCheckpoint::new());

        // Record intent + outcome (completed step)
        let step = final_step("deploy");
        let key = IdempotencyKey::generate();
        checkpoint.record_intent(&step, &key).await.unwrap();
        checkpoint.record_outcome(&key, true).await.unwrap();

        // Recovery should find nothing pending
        let results = SagaOrchestrator::recover(checkpoint.as_ref(), |_tool, _args| async {
            Ok("should not run".to_string())
        })
        .await
        .unwrap();

        assert!(results.is_empty());
    }

    #[tokio::test]
    async fn test_execute_without_checkpoint_backward_compat() {
        let saga =
            SagaOrchestrator::new(vec![write_step("create"), final_step("publish")]).unwrap();

        // Original execute() still works without any checkpoint
        let result = saga
            .execute(|_tool, _args| async { Ok("ok".to_string()) })
            .await;

        assert!(result.success);
        assert_eq!(result.step_results.len(), 2);
    }
}