a3s-flow 0.3.4

A3S workflow engine — JSON DAG execution for agentic workflows (Dify/n8n style)
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
# a3s-flow

**Workflow engine for agentic platforms** — Execute JSON-defined DAGs with concurrent wave scheduling, pluggable node types, and full lifecycle control.

```rust
let engine = FlowEngine::new(NodeRegistry::with_defaults());
let id = engine.start(&definition, variables).await?;
engine.pause(id).await?;
engine.resume(id).await?;
println!("{:?}", engine.state(id).await?);
```

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
[![Crates.io](https://img.shields.io/crates/v/a3s-flow.svg)](https://crates.io/crates/a3s-flow)

---

## Why a3s-flow?

- **JSON-native** — workflows are plain JSON objects (`{ nodes, edges }`); no YAML, no DSL, no visual editor required to run them
- **Correct by construction** — cycle detection and reference validation happen at parse time, before a single node executes
- **Concurrent by default** — nodes with no mutual dependency run in the same wave via Tokio's `JoinSet`, not one-by-one
- **Full lifecycle control** — pause at wave boundaries, resume, or cancel mid-execution via `FlowEngine`
- **Extend without forking** — implement the `Node` trait to add any node type: LLM prompt, HTTP call, condition branch, sub-flow, script
- **A3S ecosystem integration** — designed to sit beside `a3s-power` (LLM inference), `a3s-event` (pub/sub hooks), and `a3s-lane` (priority scheduling)

---

## Architecture

```
  External Caller
  ┌─────────────────────────────────────────────────────────────┐
  │                       FlowEngine                            │
  │                                                             │
  │  node_types() → Vec<String>   list registered node types    │
  │  start(def, vars) → Uuid      parse DAG, spawn task, return │
  │  pause(id)                    signal pause at wave boundary  │
  │  resume(id)                   unblock a paused execution     │
  │  terminate(id)                cancel via CancellationToken   │
  │  state(id) → ExecutionState   snapshot current state        │
  │                                                             │
  │  ┌───────────────────────────────────────────────────────┐  │
  │  │               ExecutionState (per Uuid)               │  │
  │  │                                                       │  │
  │  │            ┌──────────┐                               │  │
  │  │   start() ►│ Running  │◄─── resume()                  │  │
  │  │            └────┬─────┘                               │  │
  │  │    pause() ─────┤  terminate()                        │  │
  │  │                 │  ├──────────────► Terminated        │  │
  │  │                 ▼  │  node error ► Failed(msg)        │  │
  │  │            ┌────────┴─┐  all done ► Completed(result) │  │
  │  │            │  Paused  │                               │  │
  │  │            └──────────┘                               │  │
  │  └───────────────────────────────────────────────────────┘  │
  └────────────────────────────┬────────────────────────────────┘
                               │  spawns background Tokio task
                               │  (watch::Receiver + CancellationToken)
  ┌─────────────────────────────────────────────────────────────┐
  │                       FlowRunner                            │
  │                (one task per execution)                     │
  │                                                             │
  │  run_controlled(execution_id, vars, signal_rx, cancel)      │
  │                                                             │
  │  ┌──────────────────────────────────────────────────────┐  │
  │  │  Between waves: check signal_rx + CancellationToken  │  │
  │  │                                                      │  │
  │  │   signal = Run  ─────────────────────► execute wave  │  │
  │  │   signal = Pause ──► wait for Run/cancel             │  │
  │  │   cancel.is_cancelled() ────────────► Terminated     │  │
  │  └──────────────────────────────────────────────────────┘  │
  │                                                             │
  │  Wave 1 │ fetch                    no deps → run now        │
  │         │  └─ outputs["fetch"]                             │
  │  Wave 2 │ summarize                fetch done → run now     │
  │         │  └─ outputs["summarize"]                         │
  │  Wave 3 │ branch_a  branch_b       both ready → concurrent  │
  │         │  └─ outputs["branch_a"], outputs["branch_b"]     │
  │  Wave 4 │ notify                   fan-in join → run now    │
  │         │  └─ outputs["notify"]                            │
  │                                                             │
  │  ┌──────────────────────────────────────────────────────┐  │
  │  │  Within wave (JoinSet drain): tokio::select!         │  │
  │  │                                                      │  │
  │  │   cancel.cancelled() ───────────────► Terminated     │  │
  │  │   join_next() → node result ────────► store output   │  │
  │  │   join_next() → node error  ────────► fail-fast      │  │
  │  └──────────────────────────────────────────────────────┘  │
  └──────────────────────┬──────────────────────────────────────┘
          ┌──────────────┴──────────────┐
          │  parse definition (once)    │  resolve type per node
          ▼                             ▼
  ┌────────────────┐          ┌──────────────────────────────┐
  │    DagGraph    │          │        NodeRegistry           │
  │                │          │                              │
  │  1. Parse JSON │          │  "noop"                 (✓)  │
  │  2. Validate   │          │  "http-request"         (✓)  │
  │  3. Cycle det. │          │  "if-else"              (✓)  │
  │  4. Topo sort  │          │  "template-transform"   (✓)  │
  │                │          │  "variable-aggregator"  (✓)  │
  │                │          │  "code"                 (✓)  │
  │                │          │  "csv-parse"            (✓)  │
  │                │          │  "iteration"            (✓)  │
  │                │          │  "llm"                  (✓)  │
  │                │          │  "question-classifier"  (✓)  │
  │                │          │  "assign"               (✓)  │
  │                │          │  "parameter-extractor"  (✓)  │
  │                │          │  "loop"                 (✓)  │
  │                │          │  "list-operator"        (✓)  │
  │                │          │  <any>     → CustomNode      │
  └────────────────┘          └──────────────────────────────┘
                                         │  execute(ExecContext)
                              ┌──────────────────────────────┐
                              │         ExecContext          │
                              │                              │
                              │  data      — node config     │
                              │  inputs    — upstream output │
                              │  variables — global vars     │
                              └──────────────┬───────────────┘
                                             │  Result<Value>
                              ┌──────────────────────────────┐
                              │         FlowResult           │
                              │                              │
                              │  execution_id : Uuid         │
                              │  outputs : Map<id, Value>    │
                              └──────────────────────────────┘
```

### Module map

```
src/
├── lib.rs                  — public re-exports
├── error.rs                — FlowError enum, Result<T> alias
├── condition.rs            — Condition, CondOp, Case, LogicalOp
├── engine.rs               — FlowEngine: lifecycle API
├── execution.rs            — ExecutionState, ExecutionHandle (internal)
├── graph.rs                — DagGraph: parse, validate, topo sort
├── node.rs                 — Node trait, ExecContext
├── registry.rs             — NodeRegistry: type string → Arc<dyn Node>
├── runner.rs               — FlowRunner: wave-based execution
└── nodes/
    ├── mod.rs
    ├── noop.rs             — "noop"
    ├── http.rs             — "http-request"
    ├── cond.rs             — "if-else"
    ├── template_transform.rs — "template-transform"
    ├── variable_aggregator.rs — "variable-aggregator"
    ├── code.rs             — "code" (Rhai)
    ├── csv_parse.rs        — "csv-parse"
    ├── iteration.rs        — "iteration" (concurrent sub-flow loop)
    ├── llm.rs              — "llm"
    ├── question_classifier.rs — "question-classifier"
    ├── assign.rs           — "assign"
    ├── parameter_extractor.rs — "parameter-extractor"
    ├── loop_node.rs        — "loop"
    └── list_operator.rs    — "list-operator"
```

---

## Flow definition format

A flow is a JSON object with two arrays — **`nodes`** and **`edges`** — mirroring Dify's workflow format:

```json
{
  "nodes": [
    {
      "id":   "fetch_data",
      "type": "http-request",
      "data": { "url": "https://api.example.com/items", "method": "GET" }
    },
    {
      "id":   "check_ok",
      "type": "if-else",
      "data": { "cases": [{ "id": "ok", "conditions": [{ "from": "fetch_data", "path": "status", "op": "eq", "value": 200 }] }] }
    },
    {
      "id":   "notify",
      "type": "http-request",
      "data": {
        "url":    "https://hooks.example.com/done",
        "method": "POST",
        "run_if": { "from": "check_ok", "path": "branch", "op": "eq", "value": "ok" }
      }
    }
  ],
  "edges": [
    { "source": "fetch_data", "target": "check_ok" },
    { "source": "check_ok",   "target": "notify" }
  ]
}
```

**Node fields:**

| Field | Type | Required | Description |
|-------|------|:--------:|-------------|
| `id` | `string` || Unique node identifier within the flow |
| `type` | `string` || Node type — looked up in `NodeRegistry` |
| `data` | `object` | | Static node configuration (prompt, URL, script body, …) |

**Edge fields:**

| Field | Type | Required | Description |
|-------|------|:--------:|-------------|
| `source` | `string` || ID of the upstream node |
| `target` | `string` || ID of the downstream node |

**`run_if` guard** — place inside `data` to conditionally skip the node:

```json
"data": {
  "run_if": { "from": "upstream_id", "path": "branch", "op": "eq", "value": "ok" }
}
```

| Field | Type | Description |
|-------|------|-------------|
| `from` | `string` | Upstream node ID to read from |
| `path` | `string` | Dot-separated path into the output (e.g. `"body.count"`; `""` for root) |
| `op` | `string` | `eq` \| `ne` \| `gt` \| `lt` \| `gte` \| `lte` \| `contains` |
| `value` | any JSON | Right-hand side of the comparison |

Skip propagates automatically: if a node is skipped, any downstream node whose `run_if.from` points to it will also be skipped.

**Validation rules** (enforced at `DagGraph::from_json` time, before execution):

- At least one node must be present
- All node IDs must be unique
- Every ID listed in an edge's `source`/`target` must reference a node defined in `nodes`
- The graph must be acyclic

---

## Quick start

```toml
# Cargo.toml
[dependencies]
a3s-flow  = "0.1"
tokio     = { version = "1", features = ["full"] }
serde_json = "1"
```

### Via `FlowEngine` (recommended)

`FlowEngine` is the primary API. It owns the node registry and all running executions.

```rust
use a3s_flow::{ExecutionState, FlowEngine, NodeRegistry};
use serde_json::json;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> a3s_flow::Result<()> {
    let engine = FlowEngine::new(NodeRegistry::with_defaults());

    // Discover registered node types.
    println!("available nodes: {:?}", engine.node_types()); // ["noop"]

    let definition = json!({
        "nodes": [
            { "id": "start",   "type": "noop" },
            { "id": "process", "type": "noop" },
            { "id": "end",     "type": "noop" }
        ],
        "edges": [
            { "source": "start",   "target": "process" },
            { "source": "process", "target": "end" }
        ]
    });

    // Start: validates the DAG, spawns a background task, returns immediately.
    let id = engine.start(&definition, HashMap::new()).await?;

    // Pause at the next wave boundary.
    engine.pause(id).await?;

    // Resume.
    engine.resume(id).await?;

    // Query state at any time.
    match engine.state(id).await? {
        ExecutionState::Completed(result) => {
            println!("done — outputs: {:#?}", result.outputs);
        }
        ExecutionState::Running => println!("still running"),
        other => println!("state: {}", other.as_str()),
    }

    Ok(())
}
```

### Via `FlowRunner` (direct, no lifecycle control)

Use `FlowRunner` when you need a simple fire-and-forget execution with no
pause / resume / terminate support.

```rust
use a3s_flow::{DagGraph, FlowRunner, NodeRegistry};
use serde_json::json;
use std::collections::HashMap;

#[tokio::main]
async fn main() -> a3s_flow::Result<()> {
    let dag    = DagGraph::from_json(&json!({ "nodes": [{ "id": "a", "type": "noop" }], "edges": [] }))?;
    let runner = FlowRunner::new(dag, NodeRegistry::with_defaults());
    let result = runner.run(HashMap::new()).await?;
    println!("{:#?}", result.outputs);
    Ok(())
}
```

---

## Adding a custom node

Implement `Node` and register it before creating the engine or runner:

```rust
use a3s_flow::{ExecContext, FlowEngine, FlowError, Node, NodeRegistry};
use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;

struct HttpGetNode;

#[async_trait]
impl Node for HttpGetNode {
    fn node_type(&self) -> &str { "http_get" }

    async fn execute(&self, ctx: ExecContext) -> Result<Value, FlowError> {
        let url = ctx.data["url"]
            .as_str()
            .ok_or_else(|| FlowError::InvalidDefinition("missing data.url".into()))?;

        let body = reqwest::get(url)
            .await
            .map_err(|e| FlowError::Internal(e.to_string()))?
            .text()
            .await
            .map_err(|e| FlowError::Internal(e.to_string()))?;

        Ok(serde_json::json!({ "body": body }))
    }
}

let mut registry = NodeRegistry::with_defaults();
registry.register(Arc::new(HttpGetNode));

let engine = FlowEngine::new(registry);
```

### Global context and node context awareness

a3s-flow provides a **shared mutable context** (similar to Dify's global context) that persists across all nodes in a flow execution. This enables:

- **Cross-node state sharing** — Nodes can read and write shared state
- **Conversation context** — Store conversation IDs, user sessions, etc.
- **Workflow metadata** — Track execution history, timestamps, etc.

#### Accessing the shared context

Every node receives an `ExecContext` with a `context` field:

```rust
pub struct ExecContext {
    pub data: Value,                                    // Node configuration
    pub inputs: HashMap<String, Value>,                 // Upstream outputs
    pub variables: HashMap<String, Value>,              // Global variables
    pub context: Arc<RwLock<HashMap<String, Value>>>,   // Shared mutable context
    pub registry: Arc<NodeRegistry>,
    pub flow_store: Option<Arc<dyn FlowStore>>,
}
```

**Reading from context:**

```rust
let conversation_id = {
    let context = ctx.context.read().unwrap();
    context.get("conversation_id")
        .and_then(|v| v.as_str())
        .unwrap_or("default")
        .to_string()
};
```

**Writing to context:**

```rust
{
    let mut context = ctx.context.write().unwrap();
    context.insert("last_action".to_string(), json!({
        "node_id": "my_node",
        "timestamp": chrono::Utc::now().to_rfc3339()
    }));
}
```

#### Context vs Variables

| Feature | `variables` | `context` |
|---------|------------|-----------|
| **Scope** | Read-only per node | Shared mutable across all nodes |
| **Use case** | Flow inputs, env vars | Cross-node state, conversation context |
| **Modified by** | `"assign"` nodes only | Any node via `ctx.context.write()` |
| **Persistence** | Passed to sub-flows | Shared within single execution |

#### Example: Context-aware custom node

See [`examples/custom_mcp_node.rs`](examples/custom_mcp_node.rs) for a complete example of a custom node that:
- Reads `conversation_id` from shared context
- Writes `last_mcp_call` metadata to shared context
- Demonstrates context awareness across multiple nodes

### Core vs custom nodes

**Core nodes** (built-in, Dify-compatible):
- Registered by default in `NodeRegistry::with_defaults()`
- Cover common workflow patterns (HTTP, LLM, conditions, loops, etc.)
- Stable API, rarely change

**Custom nodes** (user-defined, service-specific):
- Implement the `Node` trait
- Registered dynamically via `registry.register(Arc::new(MyNode))`
- Examples: MCP integration, custom APIs, database connectors, etc.

**When to create a custom node:**
- Service-specific integrations (MCP, Slack, GitHub, etc.)
- Domain-specific logic (fraud detection, recommendation engines, etc.)
- Custom data transformations not covered by `"code"` node
- External system connectors (databases, message queues, etc.)

---

## Built-in nodes (Dify-compatible)

| Type string | Dify equivalent | Key config fields | Output |
|-------------|----------------|-------------------|--------|
| `"noop"` ||| Merged upstream inputs |
| `"start"` | Start | `inputs[]` (name, type, default) | Resolved input variables |
| `"end"` | End | `outputs` (name → JSON pointer) | Named output values |
| `"http-request"` | HTTP Request | `url`✱, `method`, `headers`, `body` | `{ status, ok, body }` |
| `"if-else"` | IF/ELSE | `cases[]` (id, conditions, logical_operator) | `{ branch: "case_id"\|"else" }` |
| `"template-transform"` | Template | `template` (Jinja2 string)✱ | `{ output: string }` |
| `"variable-aggregator"` | Variable Aggregator | `inputs` (ordered key list, optional) | `{ output: first_non_null }` |
| `"code"` | Code | `language` (`"rhai"`), `code`| Map or `{ output: value }` |
| `"iteration"` | Iteration | `input_selector`✱, `output_selector`✱, `flow`✱, `mode` (`"parallel"`/`"sequential"`) | `{ output: [value, ...] }` |
| `"llm"` | LLM | `model`✱, `user_prompt`✱, `system_prompt`, `api_base`, `api_key`, `temperature`, `max_tokens` | `{ text, model, finish_reason, usage }` |
| `"question-classifier"` | Question Classifier | `model`✱, `question`✱, `classes[]`✱ (id, name, description), `api_base`, `api_key` | `{ branch: "class_id" }` |
| `"assign"` | Variable Assigner | `assigns`✱ (name → Jinja2 template or literal value) | Assigned key-value map |
| `"parameter-extractor"` | Parameter Extractor | `model`✱, `query`✱, `parameters[]`✱ (name, type, description, required), `api_base`, `api_key` | Extracted JSON object |
| `"loop"` | Loop | `flow`✱, `output_selector`✱, `max_iterations` (default 10), `break_condition` | `{ output, iterations }` |
| `"list-operator"` | List Operator | `input_selector`✱, `filter`, `sort_by`, `sort_order`, `deduplicate_by`, `limit` | `{ output: [...] }` |

✱ = required field

**Note**: MCP (Model Context Protocol) nodes and other service-specific integrations should be implemented as custom nodes and registered dynamically. See [Adding a custom node](#adding-a-custom-node) for examples.

### `"if-else"` — conditional routing

```json
{
  "id": "route",
  "type": "if-else",
  "data": {
    "cases": [
      {
        "id": "is_ok",
        "logical_operator": "and",
        "conditions": [
          { "from": "fetch", "path": "status", "op": "eq", "value": 200 }
        ]
      },
      {
        "id": "is_error",
        "conditions": [
          { "from": "fetch", "path": "status", "op": "gte", "value": 500 }
        ]
      }
    ]
  }
}
```

Output: `{ "branch": "is_ok" }` | `{ "branch": "is_error" }` | `{ "branch": "else" }`

Downstream nodes use `run_if` inside `data` to select their path:
```json
{ "data": { "run_if": { "from": "route", "path": "branch", "op": "eq", "value": "is_ok" } } }
```

### `"template-transform"` — Jinja2 rendering

Upstream node outputs are available by node ID; global variables by their key:

```json
{ "template": "Hello {{ user.name }}! Status: {{ fetch.status }}" }
```

### `"code"` — Rhai scripting

`inputs` (upstream outputs by node ID) and `variables` are injected into scope:

```rhai
// Returns an object map → becomes output directly
#{
  ok:    inputs.fetch.status == 200,
  count: inputs.fetch.body.items.len()
}
```

### `"iteration"` — loop over an array

Runs an inline sub-flow for every element of an input array. Each iteration receives two extra flow variables: `variables.item` (the current element) and `variables.index` (0-based position). Iterations execute concurrently; results are returned in the original array order.

```json
{
  "id": "process_all",
  "type": "iteration",
  "data": {
    "input_selector":  "fetch.body.items",
    "output_selector": "summarize.output",
    "flow": {
      "nodes": [
        {
          "id": "summarize",
          "type": "code",
          "data": {
            "language": "rhai",
            "code": "#{ output: variables.item.name + \" processed\" }"
          }
        }
      ],
      "edges": []
    }
  }
}
```

Output: `{ "output": ["item0 processed", "item1 processed", ...] }`

---

## Reliability features (Phase 3)

### Per-node retry policy

Add a `retry` object to any node's `data` field to enable automatic retries on failure:

```json
{
  "id": "fetch",
  "type": "http-request",
  "data": {
    "url": "https://api.example.com/items",
    "retry": { "max_attempts": 3, "backoff_ms": 500 }
  }
}
```

| Field | Type | Required | Description |
|-------|------|:--------:|-------------|
| `max_attempts` | `u32` || Total attempts including the first (minimum: 1) |
| `backoff_ms` | `u64` | | Base delay between retries in ms. Each retry waits `base * 2^(n-1)` (capped at `base * 64`). Default: `0` (no delay) |

All errors (including transient network failures) count as an attempt. The last error is propagated as `FlowError::NodeFailed` if all attempts are exhausted.

### Per-node timeout

Add `timeout_ms` to any node's `data` field to limit its execution time:

```json
{
  "id": "fetch",
  "type": "http-request",
  "data": {
    "url": "https://api.example.com/items",
    "timeout_ms": 5000
  }
}
```

If the node does not complete within the specified duration, the attempt fails with `"timed out after Xms"`. Combine with `retry` to retry timed-out nodes.

### Partial execution resume

`FlowRunner::resume_from` continues a flow from a prior (partial or complete) `FlowResult`, skipping any nodes that already have recorded outputs:

```rust
// First run (possibly interrupted or partial).
let partial: FlowResult = runner.run(variables.clone()).await?;

// Resume: nodes listed in partial.completed_nodes are not re-executed.
let full: FlowResult = runner.resume_from(&partial, variables).await?;
```

`FlowResult` now exposes two additional fields to support this:

| Field | Type | Description |
|-------|------|-------------|
| `completed_nodes` | `HashSet<String>` | All nodes that finished (including skipped ones) |
| `skipped_nodes` | `HashSet<String>` | Nodes whose `run_if` guard was false |

Use `skipped_nodes` to distinguish a node that genuinely produced `null` from one that was conditionally skipped.

---

## Extension points

| Type / Trait | Purpose | Default |
|---|---|---|
| `Node` | Custom node execution logic | 7 built-in types |
| `NodeRegistry` | Maps type strings to `Arc<dyn Node>` | Ships with all built-ins |
| `Condition` / `Case` | Shared condition type for `run_if` + `"if-else"` ||
| `ExecContext` | Per-node runtime data (data + inputs + variables) ||
| `FlowEngine` | Lifecycle orchestrator — owns registry + execution map ||
| `ExecutionStore` | Persist execution history and replay | `MemoryExecutionStore` |
| `FlowStore` | Load and save named flow definitions | `MemoryFlowStore` |
| `EventEmitter` | Node and flow lifecycle events | `NoopEventEmitter` |
| `FlowEvent` | Cloneable event enum for broadcast streaming ||
| `StartNode` | Dify-compatible input declaration + defaults | built-in |
| `EndNode` | Output collection via JSON pointer paths | built-in |

---

## Persistence & observability (Phase 4)

### ExecutionStore — persist completed results

`FlowEngine` automatically saves every successfully completed `FlowResult` to an `ExecutionStore` when one is configured:

```rust
use a3s_flow::{FlowEngine, MemoryExecutionStore, NodeRegistry};
use std::sync::Arc;

let store = Arc::new(MemoryExecutionStore::new());
let engine = FlowEngine::new(NodeRegistry::with_defaults())
    .with_execution_store(Arc::clone(&store) as Arc<dyn a3s_flow::ExecutionStore>);

let id = engine.start(&definition, variables).await?;
// After the flow completes, the result is available in the store.
let result = store.load(id).await?.unwrap();
```

Implement `ExecutionStore` to persist to a database, S3, or any backend:

```rust
#[async_trait]
impl ExecutionStore for MyStore {
    async fn save(&self, result: &FlowResult) -> Result<()> { /* ... */ }
    async fn load(&self, id: Uuid) -> Result<Option<FlowResult>> { /* ... */ }
    async fn list(&self) -> Result<Vec<Uuid>> { /* ... */ }
    async fn delete(&self, id: Uuid) -> Result<()> { /* ... */ }
}
```

### FlowStore — named flow definition storage

`FlowStore` is a stand-alone utility for storing and retrieving named flow definitions:

```rust
use a3s_flow::{MemoryFlowStore, FlowStore, FlowEngine, NodeRegistry};
use std::sync::Arc;

let flow_store = MemoryFlowStore::new();
flow_store.save("daily-report", &definition).await?;

// Later: load by name and start.
let engine = FlowEngine::new(NodeRegistry::with_defaults());
if let Some(def) = flow_store.load("daily-report").await? {
    engine.start(&def, variables).await?;
}
```

### EventEmitter — lifecycle event hooks

Implement `EventEmitter` to receive flow and node lifecycle events. Register it on `FlowEngine` or `FlowRunner`:

```rust
use a3s_flow::{EventEmitter, FlowEngine, FlowResult, NodeRegistry};
use async_trait::async_trait;
use serde_json::Value;
use std::sync::Arc;
use uuid::Uuid;

struct MyEmitter;

#[async_trait]
impl EventEmitter for MyEmitter {
    async fn on_flow_started(&self, id: Uuid) { /* ... */ }
    async fn on_flow_completed(&self, id: Uuid, result: &FlowResult) { /* ... */ }
    async fn on_flow_failed(&self, id: Uuid, reason: &str) { /* ... */ }
    async fn on_flow_terminated(&self, id: Uuid) { /* ... */ }
    async fn on_node_started(&self, id: Uuid, node_id: &str, node_type: &str) { /* ... */ }
    async fn on_node_completed(&self, id: Uuid, node_id: &str, output: &Value) { /* ... */ }
    async fn on_node_skipped(&self, id: Uuid, node_id: &str) { /* ... */ }
    async fn on_node_failed(&self, id: Uuid, node_id: &str, reason: &str) { /* ... */ }
}

let engine = FlowEngine::new(NodeRegistry::with_defaults())
    .with_event_emitter(Arc::new(MyEmitter) as Arc<dyn EventEmitter>);
```

### OpenTelemetry-compatible tracing spans

Every node execution is wrapped in a `tracing::info_span!` with `node_id`, `node_type`, and `execution_id` as structured fields:

```
node.execute{node_id="fetch_data", node_type="http-request", execution_id="..."}
```

Attach a `tracing-opentelemetry` subscriber to export these spans to any OTel-compatible backend (Jaeger, OTLP, etc.). No additional configuration is needed in `a3s-flow`.

---

## Error handling

All errors are variants of `FlowError`:

| Variant | When |
|---|---|
| `InvalidDefinition(String)` | Bad JSON shape, empty flow, duplicate node ID, unknown node type |
| `CyclicGraph` | The DAG contains a cycle |
| `UnknownNode(String)` | An edge `source`/`target` references a non-existent node ID |
| `NodeFailed { node_id, execution_id, reason }` | A node's `execute` returned an error |
| `Terminated` | The execution was stopped by `terminate()` |
| `ExecutionNotFound(Uuid)` | No execution exists for the given ID |
| `InvalidTransition { action, from }` | State transition not allowed (e.g. pause a completed flow) |
| `Json(serde_json::Error)` | JSON deserialization failure |
| `Internal(String)` | Unexpected engine-level error |

---

## Roadmap

**Phase 1 — Core engine** ✅

- [x] JSON DAG parsing and validation
- [x] Cycle detection (petgraph topological sort)
- [x] Wave-based concurrent execution (Tokio `JoinSet`)
- [x] Pluggable `Node` trait and `NodeRegistry`
- [x] Built-in `noop` node
- [x] `ExecContext`: config + upstream inputs + global variables
- [x] `FlowResult` with per-node outputs and execution UUID
- [x] `FlowEngine`: start, pause, resume, terminate, state query
- [x] `ExecutionState` machine: Running → Paused / Completed / Failed / Terminated
- [x] Cancel-aware `JoinSet` drain via `tokio::select!` (fast termination mid-wave)

**Phase 2 — Built-in nodes (Dify-compatible)** ✅

- [x] `"http-request"` — HTTP request node (GET / POST / PUT / DELETE / PATCH)
- [x] `"if-else"` — multi-case conditional routing, output `{ "branch": "case_id" | "else" }`
- [x] `"template-transform"` — Jinja2 string rendering (minijinja)
- [x] `"variable-aggregator"` — first non-null fan-in after branch merge
- [x] `"code"` — sandboxed Rhai script execution
- [x] `run_if` — per-node guard condition with automatic skip propagation
- [x] `Case` + `LogicalOp` — multi-condition AND/OR within a branch
- [x] `"iteration"` — concurrent sub-flow loop over an array; `item` + `index` injected as variables; results collected in original order

**Phase 3 — Reliability** ✅

- [x] Per-node retry policy (max attempts, exponential backoff) — `data["retry"]`
- [x] Per-node timeout — `data["timeout_ms"]`
- [x] Partial execution resume — `FlowRunner::resume_from(&prior, vars)` skips already-completed nodes

**Phase 4 — Persistence & observability** ✅

- [x] `ExecutionStore` trait + `MemoryExecutionStore` — persist execution history; auto-saved by `FlowEngine`
- [x] `FlowStore` trait + `MemoryFlowStore` — load / save named flow definitions
- [x] `EventEmitter` trait + `NoopEventEmitter` — node and flow lifecycle events (integrates with `a3s-event`)
- [x] OpenTelemetry-compatible `info_span!("node.execute", node_id, node_type, execution_id)` per node

**Phase 5 — Streaming & sub-flow composition** ✅

- [x] `FlowEvent` enum — `Clone`-able snapshot of every lifecycle event (8 variants covering flow + node start/complete/skip/fail)
- [x] `FlowEngine::start_streaming` — returns `(Uuid, broadcast::Receiver<FlowEvent>)`; receiver is created before spawn so zero events are lost; composable with any existing `EventEmitter`
- [x] `"sub-flow"` built-in node — executes a named flow inline as a single step; inherits parent registry and variables; `data["variables"]` extends/overrides them; output is the sub-flow's per-node outputs map
- [x] `flow_store` propagation — `FlowRunner` and `ExecContext` now carry the engine's `FlowStore`, enabling `"sub-flow"` and future nodes to load named definitions at execution time

**Phase 6 — Error recovery & concurrency controls** ✅

- [x] `continue_on_error` per-node flag — a failed node produces `{"__error__": "reason"}` as output instead of halting the flow; downstream nodes run normally; `EventEmitter` still receives `on_node_completed` with the error output
- [x] `max_concurrency` on `FlowRunner` / `FlowEngine` — Tokio `Semaphore` limits the number of nodes executing simultaneously within a wave; unlimited by default; builder-pattern API (`with_max_concurrency(n)`)
- [x] `"start"` node — Dify-compatible entry point; declares typed flow inputs with optional defaults; validates type at execution time; passes resolved variables to downstream nodes
- [x] `"end"` node — Dify-compatible output collector; gathers values from upstream nodes using JSON pointer paths (`/node_id/field`); missing paths resolve to `null`

**Phase 7 — LLM nodes** ✅

- [x] `"llm"` node — OpenAI-compatible chat completion; system and user prompts rendered as Jinja2 templates; outputs `text`, `model`, `finish_reason`, and token usage; works with any `/v1/chat/completions` endpoint (OpenAI, Ollama, LM Studio, vLLM, Together AI, Anthropic proxy, etc.)
- [x] `"question-classifier"` node — LLM-powered routing; classifies input into one of N user-defined classes; outputs `{ "branch": "class_id" }` (same shape as `"if-else"`); fallback strategy: exact match → substring match → first class

**Phase 8 — State mutation & validation** ✅

- [x] `"assign"` node — writes key-value pairs into the live flow variable scope; string values rendered as Jinja2 templates, non-string values used as-is; runner automatically merges output into `ctx.variables` between waves so downstream nodes see updated values
- [x] Sequential iteration mode — `"iteration"` node gains `data["mode"]` field: `"parallel"` (default, existing behaviour) or `"sequential"` (items processed one-at-a-time in order; `prev_output` variable injected for each step)
- [x] `FlowEngine::validate` — synchronous pre-flight check returning `Vec<ValidationIssue>`; checks: DAG structural validity, all node types registered, `run_if.from` references existing nodes; zero-cost — does not start an execution

**Phase 9 — Dify parity: parameter-extractor, loop, list-operator** ✅

- [x] `"parameter-extractor"` node — LLM-powered structured extraction from natural language; `query` rendered as Jinja2 template; `parameters[]` declares names, types, descriptions, and required flag; LLM response parsed as JSON (markdown fences stripped automatically); required parameters that cannot be found surface as errors
- [x] `"loop"` node — while-loop over inline sub-flow; runs until `break_condition` (same `Condition` schema as `run_if`) is true or `max_iterations` is reached; injects `iteration_index` and `loop_output` (previous iteration result) as variables each round; outputs `{ output, iterations }`
- [x] `"list-operator"` node — pure in-process JSON array pipeline: filter (eq/ne/gt/lt/gte/lte/contains on a dot-path field) → sort (by dot-path field, asc/desc, numerics/strings/nulls) → deduplicate (by dot-path key or full equality) → limit (first N); all operations optional; zero network calls

**Phase 10 — Data transformation** ✅

- [x] `"csv-parse"` node — parse CSV text into JSON array; configurable delimiter and header handling; outputs array of objects (with header) or array of arrays (without header)

---

## Streaming execution (Phase 5)

### `start_streaming` — pull-based event subscription

`FlowEngine::start_streaming` is an alternative to `start` that also returns a live `broadcast::Receiver<FlowEvent>`. Because the receiver is created **before** the execution task is spawned, the first event (`FlowStarted`) is never missed.

```rust
use a3s_flow::{FlowEngine, FlowEvent, NodeRegistry};
use serde_json::json;
use std::collections::HashMap;

let engine = FlowEngine::new(NodeRegistry::with_defaults());
let def = json!({
    "nodes": [{ "id": "a", "type": "noop" }, { "id": "b", "type": "noop" }],
    "edges": [{ "source": "a", "target": "b" }]
});

let (id, mut rx) = engine.start_streaming(&def, HashMap::new()).await?;

while let Ok(event) = rx.recv().await {
    match event {
        FlowEvent::NodeCompleted { node_id, output, .. } => {
            println!("✓ {node_id}: {output}");
        }
        FlowEvent::FlowCompleted { result, .. } => {
            println!("flow done — {} nodes", result.completed_nodes.len());
            break;
        }
        FlowEvent::FlowFailed { reason, .. } => {
            eprintln!("flow failed: {reason}");
            break;
        }
        _ => {}
    }
}
```

Multiple subscribers are supported via [`broadcast::Receiver::resubscribe`]. If a custom `EventEmitter` is also attached (via `with_event_emitter`), both receive every event.

### `FlowEvent` variants

| Variant | When |
|---------|------|
| `FlowStarted { execution_id }` | Execution begins |
| `NodeStarted { execution_id, node_id, node_type }` | Before first attempt |
| `NodeCompleted { execution_id, node_id, output }` | Node succeeded |
| `NodeSkipped { execution_id, node_id }` | `run_if` guard was false |
| `NodeFailed { execution_id, node_id, reason }` | All retries exhausted |
| `FlowCompleted { execution_id, result }` | All nodes done |
| `FlowFailed { execution_id, reason }` | A node failed and halted the flow |
| `FlowTerminated { execution_id }` | `terminate()` was called |

---

## Sub-flow composition (Phase 5)

### `"sub-flow"` node — reuse named flows as steps

The `"sub-flow"` node loads a named flow definition from the engine's `FlowStore` and executes it synchronously as part of the parent wave. The parent's node registry is shared — all custom node types are available inside the sub-flow. Variables are inherited from the parent and can be overridden per invocation.

The node output is a JSON object whose keys are the sub-flow's node IDs and values are those nodes' outputs, identical in shape to `FlowResult::outputs`.

```rust
use a3s_flow::{FlowEngine, FlowStore, MemoryFlowStore, NodeRegistry};
use serde_json::json;
use std::{collections::HashMap, sync::Arc};

// Register the sub-flow definition.
let store = Arc::new(MemoryFlowStore::new());
let summarizer_def = json!({
    "nodes": [{ "id": "summarize", "type": "noop" }],
    "edges": []
});
store.save("summarizer", &summarizer_def).await?;

// Build the parent flow that calls the sub-flow.
let parent_def = json!({
    "nodes": [
        { "id": "fetch", "type": "noop" },
        {
            "id": "summarize",
            "type": "sub-flow",
            "data": {
                "name": "summarizer",
                "variables": { "max_tokens": 256 }
            }
        }
    ],
    "edges": [{ "source": "fetch", "target": "summarize" }]
});

let engine = FlowEngine::new(NodeRegistry::with_defaults())
    .with_flow_store(store as Arc<dyn FlowStore>);

let id = engine.start(&parent_def, HashMap::new()).await?;
```

**`data` fields for `"sub-flow"`:**

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `name` | string || Name of the flow definition in the `FlowStore` |
| `variables` | object || Extra variables merged on top of the parent's variables |

---

## Error recovery & concurrency controls (Phase 6)

### `continue_on_error` — absorb node failures

Set `data["continue_on_error"]: true` on any node to prevent its failure from halting the flow. Instead of propagating a `NodeFailed` error, the node outputs `{"__error__": "reason"}` and downstream nodes execute as normal.

```json
{
  "nodes": [
    {
      "id": "fetch",
      "type": "http-request",
      "data": {
        "url": "https://api.example.com/data",
        "continue_on_error": true
      }
    },
    {
      "id": "fallback",
      "type": "variable-aggregator",
      "data": { "inputs": ["fetch"] }
    }
  ],
  "edges": [{ "source": "fetch", "target": "fallback" }]
}
```

Downstream nodes receive `inputs["fetch"] = {"__error__": "..."}` and can branch on it via an `"if-else"` node or a `"code"` node.

### `max_concurrency` — rate-limit parallel execution

By default all nodes in a wave run simultaneously. Use `with_max_concurrency(n)` to cap this:

```rust
use a3s_flow::{FlowEngine, NodeRegistry};
use std::collections::HashMap;

let engine = FlowEngine::new(NodeRegistry::with_defaults())
    .with_max_concurrency(4);  // at most 4 nodes run at once

let id = engine.start(&definition, HashMap::new()).await?;
```

This is implemented with a Tokio `Semaphore` acquired *inside* each spawned task — all tasks are spawned immediately (preserving correct wave ordering) but at most `n` proceed concurrently.

### `"start"` node — declare and validate flow inputs

```json
{
  "id": "start",
  "type": "start",
  "data": {
    "inputs": [
      { "name": "query",      "type": "string" },
      { "name": "max_tokens", "type": "number", "default": 256 },
      { "name": "verbose",    "type": "bool",   "default": false }
    ]
  }
}
```

The `"start"` node resolves each declared input from the flow's `variables` map (applying defaults for absent ones) and validates that types match. Its output is `{ "query": "...", "max_tokens": 256, "verbose": false }`. Downstream nodes access these via `ctx.inputs["start"]["query"]`.

**`inputs[n]` fields:**

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `name` | string || Variable name |
| `type` | `"string"` \| `"number"` \| `"bool"` \| `"object"` \| `"array"` || Expected type (validated at runtime) |
| `default` | any || Fallback when the variable is absent; omitting it makes the input required |

### `"end"` node — collect flow outputs

```json
{
  "id": "end",
  "type": "end",
  "data": {
    "outputs": {
      "answer":       "/llm/text",
      "total_tokens": "/llm/usage/total_tokens",
      "raw":          "/transform"
    }
  }
}
```

Paths are JSON pointers resolved against the set of upstream node outputs. `/llm/text` resolves to `ctx.inputs["llm"]["text"]`. Missing paths resolve to `null`. Omitting `outputs` returns all upstream inputs as-is.

**Complete start → process → end flow:**

```rust
use a3s_flow::{FlowEngine, NodeRegistry};
use serde_json::json;
use std::collections::HashMap;

let def = json!({
    "nodes": [
        {
            "id": "start",
            "type": "start",
            "data": { "inputs": [{ "name": "query", "type": "string" }] }
        },
        { "id": "process", "type": "noop" },
        {
            "id": "end",
            "type": "end",
            "data": { "outputs": { "result": "/process" } }
        }
    ],
    "edges": [
        { "source": "start",   "target": "process" },
        { "source": "process", "target": "end" }
    ]
});

let engine = FlowEngine::new(NodeRegistry::with_defaults());
let mut vars = HashMap::new();
vars.insert("query".into(), json!("What is 2+2?"));
let id = engine.start(&def, vars).await?;
```

---

## LLM nodes (Phase 7)

### `"llm"` — OpenAI-compatible chat completion

Renders system and user prompts as Jinja2 templates, calls any `/v1/chat/completions` endpoint, and returns the assistant's reply with token-usage statistics.

**Config schema:**

| Field | Type | Required | Default | Description |
|-------|------|:--------:|---------|-------------|
| `model` | string ||| Model identifier |
| `user_prompt` | string ||| User turn — rendered as Jinja2 template |
| `system_prompt` | string || _(none)_ | System turn — rendered as Jinja2 template |
| `api_base` | string || `https://api.openai.com/v1` | Base URL (no trailing slash) |
| `api_key` | string || `""` | Bearer token; may be empty for local models |
| `temperature` | number || `0.7` | Sampling temperature `[0, 2]` |
| `max_tokens` | integer || _(none)_ | Max completion tokens |

**Template context:** Both prompts are Jinja2 templates. The rendering context contains all global flow `variables` (by key) and all upstream node outputs (by node ID). Upstream inputs shadow variables with the same key.

**Output schema:**

```json
{
  "text":          "The answer is 42.",
  "model":         "gpt-4o-mini",
  "finish_reason": "stop",
  "usage": {
    "prompt_tokens":     15,
    "completion_tokens":  8,
    "total_tokens":      23
  }
}
```

**Example — question answering with a `"start"` node:**

```json
{
  "nodes": [
    {
      "id": "start",
      "type": "start",
      "data": { "inputs": [{ "name": "query", "type": "string" }] }
    },
    {
      "id": "llm",
      "type": "llm",
      "data": {
        "model":         "gpt-4o-mini",
        "api_key":       "sk-...",
        "system_prompt": "You are a helpful assistant. Answer concisely.",
        "user_prompt":   "{{ query }}"
      }
    },
    {
      "id": "end",
      "type": "end",
      "data": { "outputs": { "answer": "/llm/text" } }
    }
  ],
  "edges": [
    { "source": "start", "target": "llm" },
    { "source": "llm",   "target": "end" }
  ]
}
```

**Example — local Ollama model:**

```json
{
  "id": "llm",
  "type": "llm",
  "data": {
    "model":    "llama3.2",
    "api_base": "http://localhost:11434/v1",
    "api_key":  "",
    "user_prompt": "Summarise this text: {{ fetch.body }}"
  }
}
```

---

### `"question-classifier"` — LLM-powered routing

Classifies an input question into one of several user-defined classes using an LLM, then outputs `{ "branch": "class_id" }` — the same shape as `"if-else"`, so `run_if` conditions work identically.

**Config schema:**

| Field | Type | Required | Description |
|-------|------|:--------:|-------------|
| `model` | string || Model identifier |
| `question` | string || Question to classify — rendered as Jinja2 template |
| `classes` | array || At least 2 classes; each requires `id` and `name` |
| `classes[].id` | string || Unique identifier returned as `branch` |
| `classes[].name` | string || Human-readable class name |
| `classes[].description` | string || Optional extra guidance for the LLM |
| `api_base`, `api_key`, `temperature`, `max_tokens` | || Same as `"llm"` node |

**Output schema:**

```json
{ "branch": "technical" }
```

If the LLM response does not match any declared class ID (case-insensitive), the node falls back to the first class.

**Example — three-way routing:**

```json
{
  "nodes": [
    {
      "id": "start",
      "type": "start",
      "data": { "inputs": [{ "name": "user_input", "type": "string" }] }
    },
    {
      "id": "classifier",
      "type": "question-classifier",
      "data": {
        "model":    "gpt-4o-mini",
        "api_key":  "sk-...",
        "question": "{{ user_input }}",
        "classes": [
          { "id": "technical", "name": "Technical question",
            "description": "Questions about code, APIs, or system behaviour" },
          { "id": "billing",   "name": "Billing question" },
          { "id": "general",   "name": "General question" }
        ]
      }
    },
    {
      "id": "tech-answer",
      "type": "llm",
      "data": {
        "model": "gpt-4o-mini", "api_key": "sk-...",
        "user_prompt": "Answer this technical question: {{ user_input }}"
      },
      "run_if": { "from": "classifier", "path": "branch", "op": "eq", "value": "technical" }
    },
    {
      "id": "billing-answer",
      "type": "llm",
      "data": {
        "model": "gpt-4o-mini", "api_key": "sk-...",
        "user_prompt": "Help with this billing question: {{ user_input }}"
      },
      "run_if": { "from": "classifier", "path": "branch", "op": "eq", "value": "billing" }
    },
    {
      "id": "general-answer",
      "type": "llm",
      "data": {
        "model": "gpt-4o-mini", "api_key": "sk-...",
        "user_prompt": "Answer this question: {{ user_input }}"
      },
      "run_if": { "from": "classifier", "path": "branch", "op": "eq", "value": "general" }
    }
  ],
  "edges": [
    { "source": "start",      "target": "classifier" },
    { "source": "classifier", "target": "tech-answer" },
    { "source": "classifier", "target": "billing-answer" },
    { "source": "classifier", "target": "general-answer" }
  ]
}
```

---

## Phase 8 — State mutation & validation

### `"assign"` node — write to the variable scope

The `"assign"` node is the only built-in node that mutates the flow's live variable map. Its output is merged into `ctx.variables` immediately after the wave completes, so every downstream node sees the new values without any special wiring.

**Config schema:**

| Field | Type | Required | Description |
|-------|------|:--------:|-------------|
| `assigns` | object || Map of variable names to Jinja2 templates (strings) or literal JSON values |

**Template context:** same as the `"llm"` node — all global variables + upstream node outputs (inputs shadow same-name variables).

**Output:** the resolved assignment map (identical to what is merged into `ctx.variables`).

**Example — initialise counters:**

```json
{
  "id": "init",
  "type": "assign",
  "data": {
    "assigns": {
      "attempt":  1,
      "user_msg": "{{ start.message }}",
      "tags":     ["default"]
    }
  }
}
```

**Example — update variable mid-flow:**

```json
{
  "nodes": [
    { "id": "start",  "type": "start",  "data": { "inputs": [{ "name": "name", "type": "string" }] } },
    { "id": "greet",  "type": "assign", "data": { "assigns": { "greeting": "Hello, {{ name }}!" } } },
    { "id": "render", "type": "template-transform", "data": { "template": "{{ greeting }}" } }
  ],
  "edges": [
    { "source": "start",  "target": "greet" },
    { "source": "greet",  "target": "render" }
  ]
}
```

**Behaviour notes:**
- If a wave contains multiple `"assign"` nodes, all their outputs are merged after the wave (order between concurrent assigns within one wave is not guaranteed — avoid conflicting keys in the same wave).
- If an `"assign"` node fails and has `continue_on_error: true`, the error output (`{ "__error__": "..." }`) is **not** merged into variables.
- Skipped `"assign"` nodes (via `run_if`) do not affect the variable scope.

---

### Sequential iteration mode

The `"iteration"` node now supports a `"mode"` field:

| Value | Behaviour |
|-------|-----------|
| `"parallel"` | *(default)* All items run concurrently via Tokio tasks |
| `"sequential"` | Items run one-at-a-time in order; each item receives the previous item's collected output as the `prev_output` variable |

**Additional variable injected in sequential mode:**

| Variable | Value |
|----------|-------|
| `prev_output` | The previous iteration's `output_selector` result (`null` for the first item) |

**Example — sequential summarisation pipeline:**

```json
{
  "id": "pipeline",
  "type": "iteration",
  "data": {
    "input_selector":  "fetch.body.chapters",
    "output_selector": "summarize.text",
    "mode":            "sequential",
    "flow": {
      "nodes": [
        {
          "id": "summarize",
          "type": "llm",
          "data": {
            "model":       "gpt-4o-mini",
            "api_key":     "sk-...",
            "user_prompt": "Previous summary: {{ prev_output }}\n\nSummarise this chapter: {{ item }}"
          }
        }
      ],
      "edges": []
    }
  }
}
```

---

### `FlowEngine::validate` — pre-flight validation

Validate a flow definition before executing it. Returns a `Vec<ValidationIssue>` — an empty list means the flow is structurally valid.

```rust
use a3s_flow::{FlowEngine, NodeRegistry, ValidationIssue};
use serde_json::json;

let engine = FlowEngine::new(NodeRegistry::with_defaults());

let issues = engine.validate(&json!({
    "nodes": [
        { "id": "a", "type": "noop" },
        { "id": "b", "type": "does-not-exist" }
    ],
    "edges": []
}));

for issue in &issues {
    println!("{issue}");  // "node 'b': unknown node type 'does-not-exist'"
}
assert_eq!(issues.len(), 1);
```

**Checks performed:**

| Check | Error location |
|-------|----------------|
| DAG parse failure (cycle, unknown edge ref, duplicate ID) | `node_id: None` |
| Unregistered node type | `node_id: Some("node_id")` |
| `run_if.from` references a node not in the graph | `node_id: Some("node_id")` |

`ValidationIssue` implements `Display` for human-readable messages:
- Flow-level: `"cyclic dependency detected"`
- Node-level: `"node 'b': unknown node type 'does-not-exist'"`

---

## License

MIT — see [LICENSE](./LICENSE).

---

<p align="center">
  Part of the <a href="https://github.com/A3S-Lab/a3s">A3S</a> ecosystem · Built by <a href="https://github.com/A3S-Lab">A3S Lab</a>
</p>