ironflow-engine 2.23.0

Workflow orchestration engine for ironflow with FSM-based run lifecycle
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
//! Integration tests for automatic run retry.
//!
//! Covers the behaviour promised by `max_retries`: a run that fails on a
//! transient error is replayed after a backoff, a run that fails on something
//! replay cannot fix is not, and every attempt keeps its own steps.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use chrono::{TimeDelta, Utc};
use serde_json::json;
use tokio::time::sleep;

use ironflow_core::provider::AgentProvider;
use ironflow_core::providers::claude::ClaudeCodeProvider;
use ironflow_engine::config::{ApprovalConfig, ShellConfig};
use ironflow_engine::context::WorkflowContext;
use ironflow_engine::engine::Engine;
use ironflow_engine::error::EngineError;
use ironflow_engine::handler::{HandlerFuture, WorkflowHandler};
use ironflow_engine::notify::{Event, EventSubscriber, SubscriberFuture};
use ironflow_store::memory::InMemoryStore;
use ironflow_store::models::{NewRun, RunStatus, RunUpdate, StepKind, StepStatus, TriggerKind};
use ironflow_store::store::RunStore;

/// Records every event it receives, so tests can assert on what was published.
#[derive(Clone, Default)]
struct EventCollector {
    events: Arc<Mutex<Vec<Event>>>,
}

impl EventSubscriber for EventCollector {
    fn name(&self) -> &str {
        "event-collector"
    }

    fn handle<'a>(&'a self, event: &'a Event) -> SubscriberFuture<'a> {
        let events = self.events.clone();
        let event = event.clone();
        Box::pin(async move {
            events.lock().expect("collector lock").push(event);
        })
    }
}

fn engine_with(store: Arc<InMemoryStore>) -> Engine {
    let provider: Arc<dyn AgentProvider> = Arc::new(ClaudeCodeProvider::new());
    Engine::new(store, provider)
}

async fn enqueue(store: &InMemoryStore, workflow: &str, max_retries: u32) -> uuid::Uuid {
    store
        .create_run(NewRun {
            created_by: None,
            workflow_name: workflow.to_string(),
            trigger: TriggerKind::Manual,
            payload: json!({}),
            max_retries,
            handler_version: None,
            labels: HashMap::new(),
            scheduled_at: None,
            idempotency_key: None,
            max_cost_usd: None,
        })
        .await
        .expect("create run")
        .into_run()
        .id
}

/// Move a run armed for retry to the front of the queue, as if its backoff had
/// elapsed, then pick it up like a worker would.
async fn fast_forward_backoff(store: &InMemoryStore, run_id: uuid::Uuid) {
    store
        .update_run(
            run_id,
            RunUpdate {
                scheduled_at: Some(Utc::now() - TimeDelta::seconds(1)),
                ..RunUpdate::default()
            },
        )
        .await
        .expect("rewind scheduled_at");

    let picked = store
        .pick_next_pending(None)
        .await
        .expect("pick")
        .expect("a run waiting for its retry");
    assert_eq!(picked.id, run_id);
}

// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------

/// Fails on every attempt with a transient HTTP error.
struct AlwaysTransientlyFails;

impl WorkflowHandler for AlwaysTransientlyFails {
    fn name(&self) -> &str {
        "always-fails"
    }

    fn execute<'a>(&'a self, _ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            Err(EngineError::Operation(
                ironflow_core::error::OperationError::Http {
                    status: Some(503),
                    message: "upstream unavailable".to_string(),
                },
            ))
        })
    }
}

/// Fails with an error that replaying cannot fix.
struct FailsPermanently;

impl WorkflowHandler for FailsPermanently {
    fn name(&self) -> &str {
        "fails-permanently"
    }

    fn execute<'a>(&'a self, _ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            Err(EngineError::StepConfig(
                "payload is missing required field 'env'".to_string(),
            ))
        })
    }
}

/// Records a step, then fails on the first attempt only.
struct FailsOnceThenSucceeds {
    attempts: AtomicU32,
}

impl WorkflowHandler for FailsOnceThenSucceeds {
    fn name(&self) -> &str {
        "flaky"
    }

    fn execute<'a>(&'a self, ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            ctx.shell("greet", ShellConfig::new("echo hello")).await?;

            if self.attempts.fetch_add(1, Ordering::SeqCst) == 0 {
                return Err(EngineError::Operation(
                    ironflow_core::error::OperationError::Http {
                        status: Some(500),
                        message: "flaky upstream".to_string(),
                    },
                ));
            }
            Ok(())
        })
    }
}

/// Gated on a human approval, then fails on the first attempt only.
struct ApprovalThenFailsOnce {
    attempts: AtomicU32,
}

impl WorkflowHandler for ApprovalThenFailsOnce {
    fn name(&self) -> &str {
        "gated"
    }

    fn execute<'a>(&'a self, ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            ctx.approval("deploy-gate", ApprovalConfig::new("Approve deployment?"))
                .await?;

            if self.attempts.fetch_add(1, Ordering::SeqCst) == 0 {
                return Err(EngineError::Operation(
                    ironflow_core::error::OperationError::Http {
                        status: Some(502),
                        message: "bad gateway".to_string(),
                    },
                ));
            }
            Ok(())
        })
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[tokio::test]
async fn transient_failure_is_replayed_until_max_retries() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(AlwaysTransientlyFails).expect("register");

    let run_id = enqueue(&store, "always-fails", 2).await;

    // Attempt 1.
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());
    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Retrying);
    assert_eq!(run.retry_count, 1);
    assert!(run.scheduled_at.is_some(), "backoff must be armed");

    // Attempt 2.
    fast_forward_backoff(&store, run_id).await;
    assert!(engine.execute_handler_run(run_id).await.is_err());
    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Retrying);
    assert_eq!(run.retry_count, 2);

    // Attempt 3: no attempt left.
    fast_forward_backoff(&store, run_id).await;
    assert!(engine.execute_handler_run(run_id).await.is_err());
    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Failed);
    assert_eq!(run.retry_count, 2);
    assert!(run.completed_at.is_some());
}

#[tokio::test]
async fn backoff_is_in_the_future() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(AlwaysTransientlyFails).expect("register");

    let run_id = enqueue(&store, "always-fails", 1).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    let before = Utc::now();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    let run = store.get_run(run_id).await.unwrap().unwrap();
    let scheduled_at = run.scheduled_at.expect("backoff armed");
    assert!(
        scheduled_at > before + TimeDelta::seconds(20),
        "first retry must wait ~30s, got {scheduled_at}"
    );
    assert!(scheduled_at < before + TimeDelta::seconds(40));

    // The run must not be picked up before its backoff elapses.
    assert!(store.pick_next_pending(None).await.unwrap().is_none());
}

#[tokio::test]
async fn non_retryable_failure_consumes_no_attempt() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(FailsPermanently).expect("register");

    let run_id = enqueue(&store, "fails-permanently", 3).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Failed);
    assert_eq!(run.retry_count, 0);
    assert!(run.scheduled_at.is_none());
}

#[tokio::test]
async fn max_retries_zero_fails_immediately() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(AlwaysTransientlyFails).expect("register");

    let run_id = enqueue(&store, "always-fails", 0).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Failed);
    assert_eq!(run.retry_count, 0);
}

#[tokio::test]
async fn each_attempt_keeps_its_own_steps() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine
        .register(FailsOnceThenSucceeds {
            attempts: AtomicU32::new(0),
        })
        .expect("register");

    let run_id = enqueue(&store, "flaky", 1).await;

    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    fast_forward_backoff(&store, run_id).await;
    engine
        .execute_handler_run(run_id)
        .await
        .expect("second attempt succeeds");

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Completed);
    assert_eq!(run.retry_count, 1);

    let steps = store.list_steps(run_id).await.unwrap();
    let greets: Vec<_> = steps.iter().filter(|s| s.name == "greet").collect();
    assert_eq!(greets.len(), 2, "both attempts must be inspectable");

    let mut attempts: Vec<u32> = greets.iter().map(|s| s.attempt).collect();
    attempts.sort_unstable();
    assert_eq!(attempts, vec![1, 2]);
}

#[tokio::test]
async fn retry_does_not_replay_previous_attempt_steps() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine
        .register(FailsOnceThenSucceeds {
            attempts: AtomicU32::new(0),
        })
        .expect("register");

    let run_id = enqueue(&store, "flaky", 1).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    fast_forward_backoff(&store, run_id).await;
    engine.execute_handler_run(run_id).await.expect("retry");

    // The second attempt really executed its step rather than replaying the
    // first attempt's record: it has its own completed step.
    let steps = store.list_steps(run_id).await.unwrap();
    let second = steps
        .iter()
        .find(|s| s.name == "greet" && s.attempt == 2)
        .expect("attempt 2 recorded its own step");
    assert_eq!(second.status.state, StepStatus::Completed);
}

#[tokio::test]
async fn approval_granted_in_a_previous_attempt_is_not_asked_again() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine
        .register(ApprovalThenFailsOnce {
            attempts: AtomicU32::new(0),
        })
        .expect("register");

    let run_id = enqueue(&store, "gated", 1).await;

    // Attempt 1 suspends on the approval gate.
    store.pick_next_pending(None).await.unwrap().unwrap();
    engine.execute_handler_run(run_id).await.expect("suspends");
    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::AwaitingApproval);

    // A human approves, and the resumed attempt fails past the gate.
    store
        .update_run_status(run_id, RunStatus::Running)
        .await
        .unwrap();
    assert!(engine.resume_run(run_id).await.is_err());

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Retrying);

    // Attempt 2 must run to completion without suspending again.
    fast_forward_backoff(&store, run_id).await;
    engine
        .execute_handler_run(run_id)
        .await
        .expect("retry runs past the approval gate");

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(run.status.state, RunStatus::Completed);

    let steps = store.list_steps(run_id).await.unwrap();
    let carried = steps
        .iter()
        .find(|s| s.kind == StepKind::Approval && s.attempt == 2)
        .expect("attempt 2 records the carried-over approval");
    assert_eq!(carried.status.state, StepStatus::Completed);
    assert_eq!(carried.output, Some(json!({"approved_in_attempt": 1})));
}

#[tokio::test]
async fn scheduling_a_retry_does_not_publish_run_failed() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(AlwaysTransientlyFails).expect("register");

    let collector = EventCollector::default();
    engine.subscribe(collector.clone(), Event::ALL);

    let run_id = enqueue(&store, "always-fails", 1).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    // Subscribers run in spawned tasks; let them drain.
    sleep(Duration::from_millis(50)).await;

    let events = collector.events.lock().expect("collector lock").clone();
    assert!(
        !events.iter().any(|e| matches!(e, Event::RunFailed { .. })),
        "a scheduled retry is not a run failure"
    );
    assert!(
        events.iter().any(|e| matches!(
            e,
            Event::RunStatusChanged {
                to: RunStatus::Retrying,
                ..
            }
        )),
        "the move to Retrying must be published"
    );
}

#[tokio::test]
async fn duration_accumulates_across_attempts() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(AlwaysTransientlyFails).expect("register");

    let run_id = enqueue(&store, "always-fails", 1).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    // Pretend the first attempt took a while.
    store
        .update_run(
            run_id,
            RunUpdate {
                duration_ms: Some(5_000),
                ..RunUpdate::default()
            },
        )
        .await
        .unwrap();

    fast_forward_backoff(&store, run_id).await;
    assert!(engine.execute_handler_run(run_id).await.is_err());

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert!(
        run.duration_ms >= 5_000,
        "the second attempt must add to the first attempt's duration, got {}",
        run.duration_ms
    );
}

#[tokio::test]
async fn cost_accumulates_across_attempts() {
    let store = Arc::new(InMemoryStore::new());
    let mut engine = engine_with(store.clone());
    engine.register(AlwaysTransientlyFails).expect("register");

    let run_id = enqueue(&store, "always-fails", 1).await;
    store.pick_next_pending(None).await.unwrap().unwrap();
    assert!(engine.execute_handler_run(run_id).await.is_err());

    // Pretend the first attempt spent something.
    store
        .update_run(
            run_id,
            RunUpdate {
                cost_usd: Some(rust_decimal::Decimal::new(150, 2)),
                ..RunUpdate::default()
            },
        )
        .await
        .unwrap();

    fast_forward_backoff(&store, run_id).await;
    assert!(engine.execute_handler_run(run_id).await.is_err());

    let run = store.get_run(run_id).await.unwrap().unwrap();
    assert_eq!(
        run.cost_usd,
        rust_decimal::Decimal::new(150, 2),
        "the second attempt must not wipe the first attempt's cost"
    );
}