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
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
# Legend

A strict, composable saga VM for sequential workflows with compensation.

Legend is a dependency-light orchestration runtime that runs **sequential workflows** with **compensation** (a.k.a. the *Saga* pattern). It focuses on **compile-time safety, composability, and resumability**.

## Features

- **Compile-time validation**: Empty programs and invalid state transitions are compile errors
- **Composable blocks**: Reusable step sequences that can be nested via the `block!` macro
- **Typestate execution**: `New``Paused`/`Completed`/`Failed` enforced by the type system
- **Strict compensation**: No silent failures - compensation must explicitly succeed or fail
- **Async-first**: Built with `async_trait` for native async/await support
- **Serializable state**: Pause, persist, and resume workflows across process restarts
- **Step timing**: Track execution duration for each step with `StepTiming`
- **Storage abstraction**: `Store` trait for persisting paused executions with `InMemoryStore` included
- **Tracing support**: Optional tracing events for each step (enable `tracing` feature)

## Quick Start: Travel Booking Saga

A complete flight + hotel booking system with automatic rollback on failure.

```rust
use legend::{Step, StepOutcome, CompensationOutcome, RetryPolicy, ExecutionResult, legend};

// =============================================================================
// Context & Errors
// =============================================================================

#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
struct TravelContext {
    // Bank API responses
    hold_id: Option<String>,
    charge_id: Option<String>,

    // Hotel API responses
    hotel_reservation_id: Option<String>,

    // Flight API responses
    flight_reservation_id: Option<String>,

    // Final confirmation
    confirmation_code: Option<String>,
}

#[derive(thiserror::Error, Clone, Debug, serde::Serialize, serde::Deserialize)]
enum TravelError {
    #[error("Bank API: Insufficient funds")]
    InsufficientFunds,
    #[error("Bank API: Hold expired")]
    HoldExpired,
    #[error("Hotel API: No rooms available")]
    HotelUnavailable,
    #[error("Flight API: No seats available")]
    FlightUnavailable,
    #[error("Service temporarily unavailable")]
    ServiceUnavailable,
}

// =============================================================================
// Step 1: Reserve Funds (Bank API)
// =============================================================================

struct ReserveFunds;

#[async_trait::async_trait]
impl Step<TravelContext, TravelError> for ReserveFunds {
    type Input = u64; // amount in cents

    async fn execute(ctx: &mut TravelContext, amount: &u64) -> Result<StepOutcome, TravelError> {
        // POST /api/bank/holds { amount, customer_id }
        // Returns: { hold_id: "hold_abc123", expires_at: "..." }
        let hold_id = format!("hold_{}", amount);
        ctx.hold_id = Some(hold_id);
        Ok(StepOutcome::Continue)
    }

    async fn compensate(ctx: &mut TravelContext, _: &u64) -> Result<CompensationOutcome, TravelError> {
        // DELETE /api/bank/holds/{hold_id}
        ctx.hold_id = None;
        Ok(CompensationOutcome::Completed)
    }

    fn retry_policy() -> RetryPolicy {
        RetryPolicy::retries(3) // Bank API can be flaky
    }
}

// =============================================================================
// Step 2: Book Hotel (Hotel API)
// =============================================================================

struct BookHotel;

#[async_trait::async_trait]
impl Step<TravelContext, TravelError> for BookHotel {
    type Input = (String, String, u32); // (hotel_id, room_type, nights)

    async fn execute(ctx: &mut TravelContext, input: &Self::Input) -> Result<StepOutcome, TravelError> {
        let (hotel_id, room_type, nights) = input;
        // POST /api/hotels/{hotel_id}/reservations
        // Body: { room_type, nights, check_in, check_out }
        // Returns: { reservation_id: "htl_xyz789" }
        let reservation_id = format!("htl_{}_{}", hotel_id, nights);
        ctx.hotel_reservation_id = Some(reservation_id);
        Ok(StepOutcome::Continue)
    }

    async fn compensate(ctx: &mut TravelContext, _: &Self::Input) -> Result<CompensationOutcome, TravelError> {
        // DELETE /api/hotels/reservations/{reservation_id}
        ctx.hotel_reservation_id = None;
        Ok(CompensationOutcome::Completed)
    }
}

// =============================================================================
// Step 3: Book Flight (Flight API)
// =============================================================================

struct BookFlight;

#[async_trait::async_trait]
impl Step<TravelContext, TravelError> for BookFlight {
    type Input = (String, String, String); // (flight_number, departure, seat_class)

    async fn execute(ctx: &mut TravelContext, input: &Self::Input) -> Result<StepOutcome, TravelError> {
        let (flight_number, departure, seat_class) = input;
        // POST /api/flights/{flight_number}/bookings
        // Body: { departure_date, seat_class, passenger_info }
        // Returns: { booking_id: "flt_abc456" }
        let booking_id = format!("flt_{}_{}", flight_number, departure);
        ctx.flight_reservation_id = Some(booking_id);
        Ok(StepOutcome::Continue)
    }

    async fn compensate(ctx: &mut TravelContext, _: &Self::Input) -> Result<CompensationOutcome, TravelError> {
        // DELETE /api/flights/bookings/{booking_id}
        ctx.flight_reservation_id = None;
        Ok(CompensationOutcome::Completed)
    }

    fn retry_policy() -> RetryPolicy {
        RetryPolicy::retries(2)
    }
}

// =============================================================================
// Step 4: Charge Payment (Bank API)
// =============================================================================

struct ChargePayment;

#[async_trait::async_trait]
impl Step<TravelContext, TravelError> for ChargePayment {
    type Input = ();

    async fn execute(ctx: &mut TravelContext, _: &()) -> Result<StepOutcome, TravelError> {
        // POST /api/bank/charges
        // Body: { hold_id: ctx.hold_id }
        // Converts the hold into an actual charge
        let Some(hold_id) = &ctx.hold_id else {
            return Err(TravelError::HoldExpired);
        };
        ctx.charge_id = Some(format!("chg_{}", hold_id));
        Ok(StepOutcome::Continue)
    }

    async fn compensate(ctx: &mut TravelContext, _: &()) -> Result<CompensationOutcome, TravelError> {
        // POST /api/bank/refunds { charge_id }
        ctx.charge_id = None;
        Ok(CompensationOutcome::Completed)
    }
}

// =============================================================================
// Step 5: Send Confirmation
// =============================================================================

struct SendConfirmation;

#[async_trait::async_trait]
impl Step<TravelContext, TravelError> for SendConfirmation {
    type Input = String; // customer email

    async fn execute(ctx: &mut TravelContext, _email: &String) -> Result<StepOutcome, TravelError> {
        // Generate confirmation code from all booking IDs
        let code = format!(
            "TRIP-{}-{}",
            ctx.hotel_reservation_id.as_ref().map(|s| &s[..7]).unwrap_or(""),
            ctx.flight_reservation_id.as_ref().map(|s| &s[..7]).unwrap_or("")
        );
        ctx.confirmation_code = Some(code);
        // POST /api/notifications/email { to, subject, body }
        Ok(StepOutcome::Continue)
    }

    async fn compensate(ctx: &mut TravelContext, _: &String) -> Result<CompensationOutcome, TravelError> {
        // POST /api/notifications/email { to, subject: "Cancellation Notice" }
        ctx.confirmation_code = None;
        Ok(CompensationOutcome::Completed)
    }
}

// =============================================================================
// Define the Travel Booking Saga
// =============================================================================

legend! {
    TravelBooking<TravelContext, TravelError> {
        reserve_funds: ReserveFunds,
        book_hotel: BookHotel,
        book_flight: BookFlight,
        charge_payment: ChargePayment,
        send_confirmation: SendConfirmation,
    }
}

// =============================================================================
// Run the Saga
// =============================================================================

let saga = TravelBooking::new(TravelBookingInputs {
    reserve_funds: 89900,                                           // $899.00
    book_hotel: ("marriott_nyc".into(), "king".into(), 3),         // 3 nights
    book_flight: ("UA123".into(), "2024-06-15".into(), "economy".into()),
    charge_payment: (),
    send_confirmation: "traveler@example.com".into(),
});

let execution = saga.build(TravelContext::default());

match execution.start().await {
    ExecutionResult::Completed(exec) => {
        println!("Trip booked! Confirmation: {:?}", exec.context().confirmation_code);
    }
    ExecutionResult::Paused(exec) => {
        // Serialize for later (e.g., waiting for external approval)
        let json = serde_json::to_string(&exec)?;
        save_to_db(&json).await?;
    }
    ExecutionResult::Failed(_, err) => {
        // All previous steps were automatically rolled back:
        // - Flight cancelled (if booked)
        // - Hotel cancelled (if booked)
        // - Bank hold released
        println!("Booking failed: {:?}", err);
    }
    ExecutionResult::CompensationFailed { original_error, .. } => {
        // Requires manual intervention - some rollback failed
        alert_ops_team(original_error).await;
    }
}
```

### What Happens on Failure?

If the flight booking fails (Step 3), Legend automatically compensates in reverse order:

1. ~~Book Flight~~**Failed** (no seats available)
2. **Compensate Hotel** → DELETE /api/hotels/reservations/{id}
3. **Compensate Bank** → DELETE /api/bank/holds/{id}

The customer is never charged, and no orphaned reservations remain.

### Resume After Pause

```rust
// Resume a paused execution (e.g., after manager approval for large bookings)
async fn resume_booking(booking_id: &str) -> Result<(), Error> {
    let json = load_from_db(booking_id).await?;
    let execution: Execution<_, _, _, Paused> = serde_json::from_str(&json)?;

    match execution.resume().await {
        ExecutionResult::Completed(exec) => {
            println!("Trip booked! {}", exec.context().confirmation_code.as_ref().unwrap());
        }
        ExecutionResult::Failed(_, err) => {
            println!("Booking failed: {:?}", err);
        }
        _ => {}
    }
    Ok(())
}
```

## Execution Flow

### State Diagram

```mermaid
stateDiagram-v2
    [*] --> New: build()

    New --> Completed: start() success
    New --> Paused: start() pause
    New --> Failed: start() error

    Paused --> Completed: resume() success
    Paused --> Failed: resume() error

    Completed --> [*]
    Failed --> [*]
```

### Compensation Flowchart

```mermaid
flowchart TD
    Start([Start]) --> RF[1. Reserve Funds<br/>Bank API]
    RF -->|Continue| BH[2. Book Hotel<br/>Hotel API]
    BH -->|Continue| BF[3. Book Flight<br/>Flight API]
    BF -->|Continue| CP[4. Charge Payment<br/>Bank API]
    CP -->|Continue| SC[5. Send Confirmation]
    SC -->|Continue| Success([Completed])

    RF -->|Fail| Fail([Failed])

    BH -->|Fail| C1[Compensate Bank Hold]
    C1 --> Fail

    BF -->|Fail| C2[Compensate Hotel]
    C2 --> C1

    CP -->|Fail| C3[Compensate Flight]
    C3 --> C2

    SC -->|Fail| C4[Refund Payment]
    C4 --> C3

    style Success fill:#10b981
    style Fail fill:#ef4444
```

## Core Concepts

### Step

A unit of work implementing the `Step<Ctx, Err>` trait:

```rust
#[async_trait::async_trait]
impl Step<MyContext, MyError> for MyStep {
    type Input = MyInput;

    async fn execute(ctx: &mut MyContext, input: &MyInput) -> Result<StepOutcome, MyError> {
        // Perform the forward action
        Ok(StepOutcome::Continue)
    }

    async fn compensate(ctx: &mut MyContext, input: &MyInput) -> Result<CompensationOutcome, MyError> {
        // Undo the action during rollback
        Ok(CompensationOutcome::Completed)
    }

    fn retry_policy() -> RetryPolicy {
        RetryPolicy::NoRetry // or RetryPolicy::retries(3)
    }
}
```

### StepOutcome

Controls execution flow from `execute` (returned via `Result<StepOutcome, E>`):

- `Continue` - Proceed to the next step
- `Pause` - Suspend execution (can be serialized and resumed later)

Errors are returned via `Err(e)`, which triggers retry (if policy allows) or compensation.

### CompensationOutcome

Controls compensation flow (returned via `Result<CompensationOutcome, E>`):

- `Completed` - Compensation succeeded
- `Pause` - Compensation needs to pause

Errors are returned via `Err(e)`, representing critical failures requiring manual intervention.

### RetryPolicy

Configures automatic retries for transient failures:

```rust
// No retries (default)
RetryPolicy::NoRetry

// Retry up to 3 times
RetryPolicy::retries(3)

// Retry with backoff hint (100ms)
RetryPolicy::retries_with_backoff(3, 100)
```

### ExecutionResult

The outcome of `start()` or `resume()`:

```rust
match execution.start().await {
    ExecutionResult::Completed(exec) => {
        // Success - access final context
        let ctx = exec.into_context();
    }
    ExecutionResult::Paused(exec) => {
        // Suspended - serialize for later
        let json = serde_json::to_string(&exec)?;
    }
    ExecutionResult::Failed(exec, error) => {
        // Failed after compensation
        println!("Error: {:?}", error);
    }
    ExecutionResult::CompensationFailed { original_error, compensation_error, .. } => {
        // Compensation also failed - critical state
    }
}
```

### Composable Blocks

Define reusable step sequences with the `block!` macro:

```rust
use legend::block;

block! {
    PaymentBlock<PaymentContext, PaymentError> {
        reserve: ReserveFunds,
        charge: ChargePayment,
    }
}

// Blocks implement Step, so they can be nested in programs
legend! {
    OrderSaga<OrderContext, OrderError> {
        validate: ValidateOrder,
        payment: PaymentBlock,  // Nested block!
        ship: ShipOrder,
    }
}
```

## Storage

Legend provides a `Store` trait for persisting paused executions, enabling workflows to survive process restarts:

```rust
use legend::{Store, InMemoryStore, ExecutionId, Execution, Paused};

// Create a store (in-memory for development, implement Store for production)
let store = InMemoryStore::new();

// When execution pauses, save it
if let ExecutionResult::Paused(exec) = execution.start().await {
    let id = ExecutionId::new();
    let data = serde_json::to_vec(&exec)?;
    store.save(id, data).await?;
    println!("Saved paused execution: {}", id);
}

// Later: retrieve and resume
let record = store.get(id).await?;
let exec: Execution<_, _, _, Paused> = serde_json::from_slice(&record.data)?;
let result = exec.resume().await;

// Clean up after completion
store.delete(id).await?;
```

### Store Trait

Implement the `Store` trait for custom backends (Redis, PostgreSQL, sled, etc.):

```rust
use legend::{Store, StoreError, PausedRecord, ExecutionId};

#[async_trait::async_trait]
impl Store for MyStore {
    /// Save a paused execution.
    async fn save(&self, id: ExecutionId, data: Vec<u8>) -> Result<(), StoreError>;

    /// Get a paused execution by ID.
    async fn get(&self, id: ExecutionId) -> Result<PausedRecord, StoreError>;

    /// Delete a paused execution (after resuming).
    async fn delete(&self, id: ExecutionId) -> Result<(), StoreError>;

    /// Check if an execution exists.
    async fn exists(&self, id: ExecutionId) -> Result<bool, StoreError>;
}
```

The `InMemoryStore` implementation uses `parking_lot::RwLock` for thread-safe access and is suitable for testing and single-process deployments.

## Step Timing

Legend tracks timing for each step execution:

```rust
use legend::{StepTiming, StepOutcome};

// After execution completes, access timing data
let timings = execution.state().step_timings();

for timing in timings {
    println!(
        "Step {} ({}): {:?} in {:?}ms",
        timing.step_index,
        if timing.is_compensation { "compensate" } else { "execute" },
        timing.outcome,
        timing.duration_ms()
    );
}
```

`StepOutcome` values:
- `Continue` - Step proceeded to next
- `Pause` - Step paused execution
- `Failed` - Step failed
- `Compensated` - Step was compensated
- `CompensationFailed` - Compensation failed

## Tracing

Enable the `tracing` feature for step-level tracing events:

```toml
[dependencies]
legend = { version = "0.1", features = ["tracing"] }
```

Events emitted:
- `step.start` - Step execution begins
- `step.end` - Step execution ends (with outcome)
- `step.retry` - Step is being retried
- `compensate.start` - Compensation begins
- `compensate.end` - Compensation ends (with outcome)

## Type Safety

Legend uses Rust's type system to prevent errors at compile time:

### Empty Programs

```rust
// This won't compile - HSingle requires at least one step
legend! {
    Empty<Ctx, Err> {
        // No steps - compile error!
    }
}
```

### Invalid State Transitions

```rust
let exec = saga.build(ctx);

// Won't compile - New state has no `resume` method
exec.resume().await;

if let ExecutionResult::Completed(done) = exec.start().await {
    // Won't compile - Completed state has no `start` or `resume`
    done.start().await;
}
```

## Testing

Steps are async functions, making them easy to unit test:

```rust
#[tokio::test]
async fn test_reserve_funds() {
    let mut ctx = PaymentContext::default();
    let result = ReserveFunds::execute(&mut ctx, &1000).await;
    assert!(result.is_continue());
    assert!(ctx.reservation_id.is_some());
}
```

See `src/tests/` for comprehensive examples:
- `basic.rs` - Success path tests
- `compensation.rs` - Rollback tests
- `retry.rs` - Retry policy tests
- `pause.rs` - Pause/resume tests

## Why Sagas?

Distributed operations often cross multiple resources (databases, payment systems, external APIs). Two-phase commit is heavy and often unavailable. Sagas keep each step **locally atomic** and rely on **compensation** to restore invariants on failure.

Use Legend for:
- Payment flows
- Account provisioning
- Document pipelines
- Any multi-step process with meaningful rollback semantics

## Design Goals

- **Compile-time safety**: Invalid programs don't compile
- **Strict compensation**: No silent failures
- **Composability**: Reusable blocks that nest via `block!` macro
- **Testability**: Each step is an async function over your context type
- **Resumability**: Serialize execution state, pause, persist, and resume later

## Notes on Reliability

- Prefer **idempotent** steps; retrying becomes safe
- Compensation must be **explicit** - return `Completed` or `Critical`
- Record enough data in the context for audit trails and troubleshooting

## License

MIT