legend 0.1.0

Strict, composable saga VM for sequential workflows with compensation
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
//! Execution state types with typestate pattern.
//!
//! This module provides compile-time enforced state transitions using Rust's type system.
//! Invalid transitions (like resuming a completed execution) are caught at compile time.

use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

use crate::hlist::{BlockResult, InstructionList};
use crate::store::now_millis;

// ============================================================================
// Typestate Markers
// ============================================================================

/// Marker: Execution has not started yet.
#[derive(Debug, Clone, Copy, Default)]
pub struct New;

/// Marker: Execution is paused and can be resumed.
#[derive(Debug, Clone, Copy)]
pub struct Paused;

/// Marker: Execution completed successfully.
#[derive(Debug, Clone, Copy)]
pub struct Completed;

/// Marker: Execution failed (after compensation).
#[derive(Debug, Clone, Copy)]
pub struct Failed;

// ============================================================================
// Execution Phase (Runtime)
// ============================================================================

/// The current phase of execution (runtime tracking).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum ExecutionPhase {
    /// Executing instructions in forward order.
    #[default]
    Forward,
    /// Compensating instructions in reverse order.
    Compensating,
}

// ============================================================================
// Step Timing
// ============================================================================

/// Outcome of a step execution or compensation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum StepStatus {
    /// Step continued to the next step.
    Continue,
    /// Step paused execution.
    Pause,
    /// Step failed with an error.
    Failed,
    /// Step was compensated successfully.
    Compensated,
    /// Step compensation failed critically.
    CompensationFailed,
}

/// Timing information for a single step execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepTiming {
    /// Index of the step.
    pub step_index: usize,
    /// Whether this is an execution or compensation.
    pub is_compensation: bool,
    /// When the step started (Unix timestamp ms).
    pub started_at: u64,
    /// When the step completed (Unix timestamp ms), if completed.
    pub completed_at: Option<u64>,
    /// Outcome of the step, if completed.
    pub outcome: Option<StepStatus>,
}

impl StepTiming {
    /// Create a new step timing record.
    pub fn new(step_index: usize, is_compensation: bool) -> Self {
        Self {
            step_index,
            is_compensation,
            started_at: now_millis(),
            completed_at: None,
            outcome: None,
        }
    }

    /// Mark the step as completed with the given outcome.
    pub fn complete(&mut self, outcome: StepStatus) {
        self.completed_at = Some(now_millis());
        self.outcome = Some(outcome);
    }

    /// Get the duration in milliseconds, if completed.
    pub fn duration_ms(&self) -> Option<u64> {
        self.completed_at
            .map(|end| end.saturating_sub(self.started_at))
    }
}

// ============================================================================
// Execution State (Internal)
// ============================================================================

/// Internal execution state - not directly accessible to users.
///
/// Tracks position, retry count, phase, and timing during execution.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ExecutionState {
    /// Current instruction index.
    index: usize,
    /// Number of instructions that have been executed (for compensation bounds).
    executed: usize,
    /// Current retry attempt for the current instruction.
    retry_count: u8,
    /// Current execution phase.
    phase: ExecutionPhase,
    /// When the execution started (Unix timestamp ms).
    started_at: Option<u64>,
    /// Timing records for each step.
    step_timings: Vec<StepTiming>,
}

impl ExecutionState {
    /// Create a new execution state.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get the current instruction index.
    pub fn current_index(&self) -> usize {
        self.index
    }

    /// Get the number of executed instructions.
    pub fn executed_count(&self) -> usize {
        self.executed
    }

    /// Get the current retry count.
    pub fn retry_count(&self) -> u8 {
        self.retry_count
    }

    /// Get the current phase.
    pub fn phase(&self) -> ExecutionPhase {
        self.phase
    }

    /// Advance to the next instruction.
    pub fn advance(&mut self) {
        self.index += 1;
        self.retry_count = 0;
    }

    /// Mark that an instruction was executed (for compensation tracking).
    pub fn mark_executed(&mut self) {
        self.executed = self.executed.max(self.index);
    }

    /// Increment the retry counter.
    pub fn increment_retry(&mut self) {
        self.retry_count += 1;
    }

    /// Begin compensation phase.
    pub fn begin_compensation(&mut self) {
        self.phase = ExecutionPhase::Compensating;
    }

    /// Check if in compensation phase.
    pub fn is_compensating(&self) -> bool {
        self.phase == ExecutionPhase::Compensating
    }

    /// Mark the execution as started.
    pub fn mark_started(&mut self) {
        if self.started_at.is_none() {
            self.started_at = Some(now_millis());
        }
    }

    /// Get when the execution started.
    pub fn started_at(&self) -> Option<u64> {
        self.started_at
    }

    /// Record the start of a step execution.
    pub fn record_step_start(&mut self, step_index: usize, is_compensation: bool) {
        self.step_timings
            .push(StepTiming::new(step_index, is_compensation));
    }

    /// Record the end of a step execution.
    pub fn record_step_end(&mut self, outcome: StepStatus) {
        if let Some(timing) = self.step_timings.last_mut() {
            timing.complete(outcome);
        }
    }

    /// Get all step timing records.
    pub fn step_timings(&self) -> &[StepTiming] {
        &self.step_timings
    }

    /// Get the timing for a specific step by index.
    pub fn timing_for_step(&self, step_index: usize, is_compensation: bool) -> Option<&StepTiming> {
        self.step_timings
            .iter()
            .find(|t| t.step_index == step_index && t.is_compensation == is_compensation)
    }
}

// ============================================================================
// Execution Result
// ============================================================================

/// Result of running or resuming an execution.
///
/// This enum represents the possible outcomes after calling `start()` or `resume()`.
pub enum ExecutionResult<Ctx, Err, Steps>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    /// Execution paused and can be resumed.
    Paused(Execution<Ctx, Err, Steps, Paused>),
    /// Execution completed successfully.
    Completed(Execution<Ctx, Err, Steps, Completed>),
    /// Execution failed (after compensation).
    Failed(Execution<Ctx, Err, Steps, Failed>, Err),
    /// Execution failed and compensation also failed.
    CompensationFailed {
        /// The execution in failed state.
        execution: Execution<Ctx, Err, Steps, Failed>,
        /// The original error that triggered compensation.
        original_error: Err,
        /// The error that occurred during compensation.
        compensation_error: Err,
        /// Index of the step where compensation failed.
        failed_at: usize,
    },
}

impl<Ctx, Err, Steps> ExecutionResult<Ctx, Err, Steps>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    /// Returns `true` if the execution is paused.
    pub fn is_paused(&self) -> bool {
        matches!(self, Self::Paused(_))
    }

    /// Returns `true` if the execution completed successfully.
    pub fn is_completed(&self) -> bool {
        matches!(self, Self::Completed(_))
    }

    /// Returns `true` if the execution failed (including compensation failures).
    pub fn is_failed(&self) -> bool {
        matches!(self, Self::Failed(_, _) | Self::CompensationFailed { .. })
    }
}

// ============================================================================
// Execution (Typestate)
// ============================================================================

/// A saga execution with compile-time state tracking.
///
/// The `State` type parameter enforces valid operations:
/// - `Execution<..., New>`: Can call `start()`
/// - `Execution<..., Paused>`: Can call `resume()` or serialize
/// - `Execution<..., Completed>`: Can access final context
/// - `Execution<..., Failed>`: Can access error and context
#[derive(Serialize, Deserialize)]
#[serde(
    bound = "Steps: Serialize + serde::de::DeserializeOwned, Ctx: Serialize + serde::de::DeserializeOwned, Err: Serialize + serde::de::DeserializeOwned"
)]
pub struct Execution<Ctx, Err, Steps, State = New>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    steps: Steps,
    ctx: Ctx,
    state: ExecutionState,
    #[serde(skip)]
    _marker: PhantomData<(Err, State)>,
}

impl<Ctx, Err, Steps> Execution<Ctx, Err, Steps, New>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    /// Create a new execution in the `New` state.
    pub fn new(steps: Steps, ctx: Ctx) -> Self {
        Self {
            steps,
            ctx,
            state: ExecutionState::new(),
            _marker: PhantomData,
        }
    }

    /// Start execution.
    ///
    /// Runs the saga from the beginning until completion, pause, or failure.
    pub async fn start(self) -> ExecutionResult<Ctx, Err, Steps> {
        self.run_internal().await
    }
}

impl<Ctx, Err, Steps> Execution<Ctx, Err, Steps, Paused>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    /// Resume a paused execution.
    ///
    /// Continues from where the execution was paused.
    pub async fn resume(self) -> ExecutionResult<Ctx, Err, Steps> {
        self.run_internal().await
    }

    /// Get a reference to the context.
    pub fn context(&self) -> &Ctx {
        &self.ctx
    }
}

impl<Ctx, Err, Steps> Execution<Ctx, Err, Steps, Completed>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    /// Get a reference to the final context.
    pub fn context(&self) -> &Ctx {
        &self.ctx
    }

    /// Consume the execution and return the final context.
    pub fn into_context(self) -> Ctx {
        self.ctx
    }
}

impl<Ctx, Err, Steps> Execution<Ctx, Err, Steps, Failed>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    /// Get a reference to the context (for inspection).
    pub fn context(&self) -> &Ctx {
        &self.ctx
    }

    /// Consume the execution and return the context.
    pub fn into_context(self) -> Ctx {
        self.ctx
    }
}

// Internal implementation for both start and resume
impl<Ctx, Err, Steps, State> Execution<Ctx, Err, Steps, State>
where
    Steps: InstructionList<Ctx, Err>,
    Ctx: Send + Sync,
    Err: Send + Sync + Clone,
{
    async fn run_internal(mut self) -> ExecutionResult<Ctx, Err, Steps> {
        self.state.mark_started();
        match self.steps.execute_all(&mut self.ctx, &mut self.state).await {
            BlockResult::Completed => ExecutionResult::Completed(Execution {
                steps: self.steps,
                ctx: self.ctx,
                state: self.state,
                _marker: PhantomData,
            }),
            BlockResult::Paused => ExecutionResult::Paused(Execution {
                steps: self.steps,
                ctx: self.ctx,
                state: self.state,
                _marker: PhantomData,
            }),
            BlockResult::Failed(e) => ExecutionResult::Failed(
                Execution {
                    steps: self.steps,
                    ctx: self.ctx,
                    state: self.state,
                    _marker: PhantomData,
                },
                e,
            ),
            BlockResult::CompensationFailed {
                original_error,
                compensation_error,
                failed_at,
            } => ExecutionResult::CompensationFailed {
                execution: Execution {
                    steps: self.steps,
                    ctx: self.ctx,
                    state: self.state,
                    _marker: PhantomData,
                },
                original_error,
                compensation_error,
                failed_at,
            },
        }
    }
}