ironflow-engine 2.20.1

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
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
//! Integration tests for artifact production and consumption.
//!
//! Black-box: real shell processes writing real files into a real temporary
//! directory, stored through the real `LocalBlobStore`.

use std::collections::HashMap;
use std::sync::Arc;

use serde_json::json;
use tempfile::TempDir;
use uuid::Uuid;

use ironflow_artifacts::blob_store::BlobStore;
use ironflow_artifacts::local::LocalBlobStore;
use ironflow_core::provider::AgentProvider;
use ironflow_core::providers::claude::ClaudeCodeProvider;
use ironflow_core::providers::record_replay::RecordReplayProvider;
use ironflow_engine::artifact::{ArtifactSink, DirectArtifactSink};
use ironflow_engine::config::ShellConfig;
use ironflow_engine::context::WorkflowContext;
use ironflow_engine::error::EngineError;
use ironflow_store::memory::InMemoryStore;
use ironflow_store::models::{NewRun, StepStatus, TriggerKind};
use ironflow_store::store::Store;

/// A run context wired to a filesystem-backed artifact store.
struct Fixture {
    ctx: WorkflowContext,
    store: Arc<dyn Store>,
    run_id: Uuid,
    /// Where steps run and write their files.
    work_dir: TempDir,
    /// Where artifact blobs are persisted.
    _blob_dir: TempDir,
}

impl Fixture {
    async fn new() -> Self {
        Self::build(true).await
    }

    /// Same fixture, without an artifact backend attached.
    async fn without_artifact_storage() -> Self {
        Self::build(false).await
    }

    async fn build(with_artifacts: bool) -> Self {
        let work_dir = TempDir::new().expect("work dir");
        let blob_dir = TempDir::new().expect("blob dir");

        let store: Arc<dyn Store> = Arc::new(InMemoryStore::new());
        let provider: Arc<dyn AgentProvider> = Arc::new(RecordReplayProvider::replay(
            ClaudeCodeProvider::new(),
            "/tmp/ironflow-fixtures",
        ));

        let run = store
            .create_run(NewRun {
                workflow_name: "artifacts".to_string(),
                trigger: TriggerKind::Manual,
                payload: json!({}),
                max_retries: 0,
                handler_version: None,
                labels: HashMap::new(),
                scheduled_at: None,
                created_by: None,
                idempotency_key: None,
                max_cost_usd: None,
            })
            .await
            .expect("create run")
            .into_run();

        let mut ctx = WorkflowContext::new(run.id, store.clone(), provider);
        if with_artifacts {
            let blob: Arc<dyn BlobStore> = Arc::new(LocalBlobStore::new(blob_dir.path()));
            let sink: Arc<dyn ArtifactSink> =
                Arc::new(DirectArtifactSink::new(blob, store.clone()));
            ctx.set_artifact_sink(sink);
        }

        Self {
            ctx,
            store,
            run_id: run.id,
            work_dir,
            _blob_dir: blob_dir,
        }
    }

    /// A shell config rooted in the fixture's working directory.
    fn shell(&self, command: &str) -> ShellConfig {
        ShellConfig::new(command).dir(self.work_dir.path().to_str().expect("utf-8 path"))
    }
}

#[tokio::test]
async fn a_declared_output_is_stored_with_its_size_type_and_hash() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'hello' > report.html")
                .output("report.html"),
        )
        .await
        .expect("step");

    let artifacts = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list");

    assert_eq!(artifacts.len(), 1);
    assert_eq!(artifacts[0].name, "report.html");
    assert_eq!(artifacts[0].content_type, "text/html");
    assert_eq!(artifacts[0].size_bytes, 5);
    assert_eq!(
        artifacts[0].sha256,
        "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
    );
}

#[tokio::test]
async fn a_later_step_reads_an_artifact_produced_earlier() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'payload' > report.txt")
                .output("report.txt"),
        )
        .await
        .expect("producer");

    // The consumer runs in a different directory: the only way it can see the
    // file is through the declared input.
    let consumer_dir = TempDir::new().expect("consumer dir");
    let consumer = ShellConfig::new("cat report.txt")
        .dir(consumer_dir.path().to_str().expect("utf-8 path"))
        .input("build", "report.txt");

    let output = fixture
        .ctx
        .shell("publish", consumer)
        .await
        .expect("consumer");

    assert_eq!(output.output["stdout"], "payload");
}

#[tokio::test]
async fn an_input_can_be_written_to_another_path() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'x' > report.txt")
                .output("report.txt"),
        )
        .await
        .expect("producer");

    let consumer_dir = TempDir::new().expect("consumer dir");
    let consumer = ShellConfig::new("cat nested/renamed.txt")
        .dir(consumer_dir.path().to_str().expect("utf-8 path"))
        .input_at("build", "report.txt", "nested/renamed.txt");

    let output = fixture
        .ctx
        .shell("publish", consumer)
        .await
        .expect("consumer");

    assert_eq!(output.output["stdout"], "x");
}

#[tokio::test]
async fn a_glob_stores_every_match() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture.shell("touch a.log b.log notes.txt").output("*.log"),
        )
        .await
        .expect("step");

    let names: Vec<String> = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list")
        .into_iter()
        .map(|artifact| artifact.name)
        .collect();

    assert_eq!(names.len(), 2);
    assert!(names.contains(&"a.log".to_string()));
    assert!(names.contains(&"b.log".to_string()));
}

#[tokio::test]
async fn an_output_that_matches_nothing_fails_a_successful_step() {
    let mut fixture = Fixture::new().await;

    let err = fixture
        .ctx
        .shell("build", fixture.shell("true").output("target/report.html"))
        .await
        .expect_err("declared output never appeared");

    assert!(matches!(
        err,
        EngineError::MissingArtifact { ref step, ref pattern }
            if step == "build" && pattern == "target/report.html"
    ));

    let steps = fixture
        .store
        .list_steps(fixture.run_id)
        .await
        .expect("steps");
    assert_eq!(steps[0].status.state, StepStatus::Failed);
}

#[tokio::test]
async fn a_failed_step_still_yields_the_files_it_did_produce() {
    let mut fixture = Fixture::new().await;

    let err = fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'partial' > build.log; exit 1")
                .output("build.log"),
        )
        .await
        .expect_err("command failed");

    // The command failure is what surfaces, not an artifact error.
    assert!(matches!(err, EngineError::Operation(_)));

    let artifacts = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list");
    assert_eq!(artifacts.len(), 1);
    assert_eq!(artifacts[0].name, "build.log");
    assert_eq!(artifacts[0].size_bytes, 7);
}

#[tokio::test]
async fn a_failed_step_tolerates_an_output_that_matched_nothing() {
    let mut fixture = Fixture::new().await;

    let err = fixture
        .ctx
        .shell("build", fixture.shell("exit 1").output("report.html"))
        .await
        .expect_err("command failed");

    assert!(matches!(err, EngineError::Operation(_)));
    assert!(
        fixture
            .store
            .list_artifacts_for_run(fixture.run_id)
            .await
            .expect("list")
            .is_empty()
    );
}

#[tokio::test]
async fn an_unresolvable_input_fails_the_step_before_the_command_runs() {
    let mut fixture = Fixture::new().await;

    let err = fixture
        .ctx
        .shell(
            "publish",
            fixture
                .shell("printf 'ran' > marker.txt")
                .input("build", "report.txt"),
        )
        .await
        .expect_err("input does not exist");

    assert!(matches!(
        err,
        EngineError::ArtifactNotFound { ref step, ref name }
            if step == "build" && name == "report.txt"
    ));
    assert!(
        !fixture.work_dir.path().join("marker.txt").exists(),
        "the command must not have run"
    );
}

#[tokio::test]
async fn an_input_produced_by_a_later_step_is_not_visible() {
    let mut fixture = Fixture::new().await;

    // Position 0 declares an input produced at position 1: nothing can satisfy
    // it, because resolution only looks backwards.
    let err = fixture
        .ctx
        .shell(
            "publish",
            fixture.shell("true").input("build", "report.txt"),
        )
        .await
        .expect_err("nothing produced yet");
    assert!(matches!(err, EngineError::ArtifactNotFound { .. }));

    fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'x' > report.txt")
                .output("report.txt"),
        )
        .await
        .expect("producer");
}

#[tokio::test]
async fn the_same_file_name_from_two_steps_is_kept_separately() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture.shell("printf 'one' > out.txt").output("out.txt"),
        )
        .await
        .expect("first");
    fixture
        .ctx
        .shell(
            "rebuild",
            fixture.shell("printf 'two' > out.txt").output("out.txt"),
        )
        .await
        .expect("second");

    let artifacts = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list");

    assert_eq!(artifacts.len(), 2);
    assert_ne!(artifacts[0].step_id, artifacts[1].step_id);
}

#[tokio::test]
async fn an_explicit_content_type_wins_over_the_guess() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf '{}' > data.txt")
                .output_typed("data.txt", "application/json"),
        )
        .await
        .expect("step");

    let artifacts = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list");

    assert_eq!(artifacts[0].content_type, "application/json");
}

#[tokio::test]
async fn a_file_name_outside_the_whitelist_is_refused() {
    let mut fixture = Fixture::new().await;

    let err = fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'x' > 'rapport été.html'")
                .output("rapport été.html"),
        )
        .await
        .expect_err("name is not in the whitelist");

    assert!(matches!(err, EngineError::Artifact(_)));
}

#[tokio::test]
async fn a_step_without_artifacts_runs_without_a_backend() {
    let mut fixture = Fixture::without_artifact_storage().await;

    let output = fixture
        .ctx
        .shell("greet", fixture.shell("echo hello"))
        .await
        .expect("step");

    assert_eq!(output.output["stdout"], "hello");
}

#[tokio::test]
async fn declaring_an_output_without_a_backend_fails_explicitly() {
    let mut fixture = Fixture::without_artifact_storage().await;

    let err = fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("printf 'x' > report.txt")
                .output("report.txt"),
        )
        .await
        .expect_err("no artifact storage");

    assert!(matches!(err, EngineError::ArtifactsUnavailable(_)));
}

#[tokio::test]
async fn put_and_get_artifact_roundtrip_for_custom_operations() {
    let mut fixture = Fixture::new().await;

    // A shell step gives us a persisted step to hang the artifact on.
    fixture
        .ctx
        .shell("generate", fixture.shell("true"))
        .await
        .expect("step");

    let step_id = fixture
        .store
        .list_steps(fixture.run_id)
        .await
        .expect("steps")[0]
        .id;

    let artifact = fixture
        .ctx
        .put_artifact(step_id, "summary.json", None, br#"{"ok":true}"#.to_vec())
        .await
        .expect("put");

    assert_eq!(artifact.content_type, "application/json");

    let bytes = fixture
        .ctx
        .get_artifact("generate", "summary.json")
        .await
        .expect("get");

    assert_eq!(bytes, br#"{"ok":true}"#);
}

#[tokio::test]
async fn get_artifact_on_an_unknown_name_reports_not_found() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell("generate", fixture.shell("true"))
        .await
        .expect("step");

    let err = fixture
        .ctx
        .get_artifact("generate", "missing.json")
        .await
        .expect_err("unknown artifact");

    assert!(matches!(err, EngineError::ArtifactNotFound { .. }));
}

#[tokio::test]
async fn put_artifact_refuses_a_duplicate_name_on_the_same_step() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell("generate", fixture.shell("true"))
        .await
        .expect("step");
    let step_id = fixture
        .store
        .list_steps(fixture.run_id)
        .await
        .expect("steps")[0]
        .id;

    fixture
        .ctx
        .put_artifact(step_id, "a.json", None, b"{}".to_vec())
        .await
        .expect("first");

    let err = fixture
        .ctx
        .put_artifact(step_id, "a.json", None, b"{}".to_vec())
        .await
        .expect_err("duplicate");

    assert!(matches!(err, EngineError::Store(_)));
}

#[tokio::test]
async fn put_artifact_without_a_backend_fails_explicitly() {
    let mut fixture = Fixture::without_artifact_storage().await;

    fixture
        .ctx
        .shell("generate", fixture.shell("true"))
        .await
        .expect("step");
    let step_id = fixture
        .store
        .list_steps(fixture.run_id)
        .await
        .expect("steps")[0]
        .id;

    let err = fixture
        .ctx
        .put_artifact(step_id, "a.json", None, b"{}".to_vec())
        .await
        .expect_err("no artifact storage");

    assert!(matches!(err, EngineError::ArtifactsUnavailable(_)));
}

#[tokio::test]
async fn an_empty_file_is_a_valid_artifact() {
    let mut fixture = Fixture::new().await;

    fixture
        .ctx
        .shell(
            "build",
            fixture.shell("touch empty.txt").output("empty.txt"),
        )
        .await
        .expect("step");

    let artifacts = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list");

    assert_eq!(artifacts[0].size_bytes, 0);
    assert_eq!(
        artifacts[0].sha256,
        "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
    );
}

#[tokio::test]
async fn a_directory_matching_a_pattern_is_ignored() {
    let mut fixture = Fixture::new().await;

    // `out.d` is a directory and `out.f` a file; only the file is an artifact.
    fixture
        .ctx
        .shell(
            "build",
            fixture
                .shell("mkdir -p out.d && printf 'x' > out.f")
                .output("out.*"),
        )
        .await
        .expect("step");

    let artifacts = fixture
        .store
        .list_artifacts_for_run(fixture.run_id)
        .await
        .expect("list");

    assert_eq!(artifacts.len(), 1);
    assert_eq!(artifacts[0].name, "out.f");
}