Sagas

Sagas are multi-step transactions with automatic compensation (rollback) on failure. Use them when you need all-or-nothing execution across multiple agents.

How Sagas Work

Step 1 → Step 2 → Step 3 → Success
  ↓         ↓         ↓
Comp 1 ← Comp 2 ← Comp 3 ← Failure

Each step has a forward action and a compensation (undo) action. If any step fails, all previously completed steps are compensated in reverse order.

Creating a Saga

# Create the saga
naos saga create order-process

# Add steps (each step maps to an agent)
naos saga add-step order-process validator
naos saga add-step order-process processor
naos saga add-step order-process notifier

Running a Saga

naos saga run order-process

Output:

[saga] order-process starting
[step 1/3] validator    ✓ completed (1.2s)
[step 2/3] processor    ✓ completed (3.4s)
[step 3/3] notifier     ✓ completed (0.8s)
[saga] order-process completed successfully (5.4s)

Saga Failure & Compensation

If step 2 fails:

[saga] order-process starting
[step 1/3] validator    ✓ completed (1.2s)
[step 2/3] processor    ✗ failed (2.1s)
[compensate] validator  ✓ rolled back
[saga] order-process failed, all steps compensated

Viewing Saga History

naos saga history order-process
Run Status Steps Duration
#3 completed 3/3 5.4s
#2 compensated 1/3 3.3s
#1 completed 3/3 6.1s