distri-workflow 0.3.9

Workflow engine for Distri — define, execute, and track multi-step workflows
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
1369
1370
1371
1372
//! Core types for the workflow engine.
//!
//! Two layers:
//!
//! - **Definition** (`WorkflowDefinition`, `WorkflowStep`): the static
//!   template — what this workflow IS. Stored alongside the agent
//!   config; never mutated by execution.
//! - **Run** (`WorkflowRun`, `WorkflowStepRun`): runtime state for one
//!   execution — status, current step pointer, shared context, per-step
//!   result/error/timestamps. Built from a definition via
//!   `WorkflowRun::new(definition)` and then mutated by the engine.
//!
//! Status uses the canonical `distri_types::TaskStatus` everywhere.
//! Workflow-specific concepts that don't have a 1:1 TaskStatus value
//! map as follows:
//!
//! | concept | TaskStatus | extra signal |
//! |---|---|---|
//! | step waiting for input / workflow paused | InputRequired | — |
//! | step skipped (skip_if / entry-point) | Canceled | note appended |
//! | step blocked (missing requirement) | Failed | error explains |
//!
//! Phase 2b will replace `WorkflowStateStore` with the cloud
//! `TaskStore` + a `workflow_step_executions` sidecar so runs flow
//! through the canonical task tree.

use chrono::{DateTime, Utc};
use distri_types::TaskStatus;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ============================================================================
// Workflow Definition (template)
// ============================================================================

/// A workflow is a DAG of steps. The definition is the *template* — no
/// runtime state lives here. Use `WorkflowRun::new(definition)` to start
/// an execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowDefinition {
    pub id: String,
    pub steps: Vec<WorkflowStep>,
    /// JSON Schema describing required inputs for this workflow.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input_schema: Option<serde_json::Value>,
    /// How workflow state is checkpointed between steps.
    #[serde(default)]
    pub checkpoint: CheckpointStrategy,
    /// Named entry points for multi-entry workflows.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub entry_points: Vec<EntryPoint>,
}

/// Built-in channel commands a workflow may not shadow. Mirrors
/// `distri-gateway` `ChannelCommand::parse`.
pub const BUILTIN_CHANNEL_COMMANDS: &[&str] = &[
    "/start",
    "/stop",
    "/disconnect",
    "/reset",
    "/new",
    "/newsession",
    "/newthread",
    "/status",
    "/debug",
    "/verbose",
    "/help",
    "/switch",
    "/workspace",
    "/context",
    "/ctx",
];

impl WorkflowDefinition {
    pub fn new(steps: Vec<WorkflowStep>) -> Self {
        Self {
            id: uuid::Uuid::new_v4().to_string(),
            steps,
            input_schema: None,
            checkpoint: CheckpointStrategy::default(),
            entry_points: vec![],
        }
    }

    pub fn with_id(mut self, id: &str) -> Self {
        self.id = id.to_string();
        self
    }

    pub fn with_checkpoint(mut self, strategy: CheckpointStrategy) -> Self {
        self.checkpoint = strategy;
        self
    }

    pub fn with_entry_points(mut self, entry_points: Vec<EntryPoint>) -> Self {
        self.entry_points = entry_points;
        self
    }

    /// Get an entry point by ID.
    pub fn entry_point(&self, id: &str) -> Option<&EntryPoint> {
        self.entry_points.iter().find(|ep| ep.id == id)
    }

    /// Find all step IDs reachable from the given step (inclusive) by following
    /// depends_on forward. Used by entry-point logic to mark unreachable steps
    /// as skipped at run start.
    pub fn reachable_from(&self, start_step_id: &str) -> std::collections::HashSet<String> {
        use std::collections::{HashSet, VecDeque};

        let mut reachable = HashSet::new();
        let mut queue = VecDeque::new();
        queue.push_back(start_step_id.to_string());

        while let Some(current) = queue.pop_front() {
            if !reachable.insert(current.clone()) {
                continue;
            }
            for step in &self.steps {
                if step.depends_on.contains(&current) && !reachable.contains(&step.id) {
                    queue.push_back(step.id.clone());
                }
            }
        }

        reachable
    }

    /// Validate the channel-command surface declared by entry-point
    /// triggers. Returns a precise error string on the first problem.
    pub fn validate_channel_surface(&self) -> Result<(), String> {
        use distri_types::channel_commands::ChannelTrigger;
        use std::collections::HashSet;

        let step_ids: HashSet<&str> = self.steps.iter().map(|s| s.id.as_str()).collect();
        let mut slash_names: HashSet<String> = HashSet::new();
        let mut callback_ids: HashSet<String> = HashSet::new();
        let mut message_count = 0usize;

        for ep in &self.entry_points {
            if !step_ids.contains(ep.starts_at.as_str()) {
                return Err(format!(
                    "entry point '{}' starts_at unknown step '{}'",
                    ep.id, ep.starts_at
                ));
            }
            let Some(trigger) = &ep.trigger else { continue };
            match trigger {
                ChannelTrigger::Slash { name, aliases, .. } => {
                    for n in std::iter::once(name).chain(aliases.iter()) {
                        let lower = n.to_lowercase();
                        if BUILTIN_CHANNEL_COMMANDS.contains(&lower.as_str()) {
                            return Err(format!("slash command '{n}' shadows a built-in command"));
                        }
                        if !slash_names.insert(lower.clone()) {
                            return Err(format!(
                                "entry point '{}': slash command '{}' is already declared",
                                ep.id, n
                            ));
                        }
                    }
                }
                ChannelTrigger::Callback { id, .. } => {
                    // Note: cross-validating a Reply step's ReplyButtonSpec::Callback.callback_data
                    // against declared callback ids is intentionally NOT done in v1 (out of plan scope).
                    if !callback_ids.insert(id.clone()) {
                        return Err(format!(
                            "entry point '{}': callback id '{}' is already declared",
                            ep.id, id
                        ));
                    }
                }
                ChannelTrigger::Message {} => message_count += 1,
            }
        }
        if message_count > 1 {
            return Err(format!(
                "workflow declares {message_count} message catch-all entry \
                 points; at most one is allowed"
            ));
        }
        for step in &self.steps {
            if let StepKind::Reply {
                buttons_from,
                button_template,
                ..
            } = &step.kind
            {
                if button_template.is_some() != buttons_from.is_some() {
                    return Err(format!(
                        "reply step '{}': button_template and buttons_from \
                         must be set together",
                        step.id
                    ));
                }
            }
        }
        Ok(())
    }

    /// Detect circular dependencies in the workflow DAG.
    /// Returns `Err` with the cycle description if found.
    pub fn detect_cycles(&self) -> Result<(), String> {
        use std::collections::{HashMap, HashSet};

        let step_ids: HashSet<&str> = self.steps.iter().map(|s| s.id.as_str()).collect();
        let mut adj: HashMap<&str, Vec<&str>> = HashMap::new();
        for step in &self.steps {
            adj.insert(
                step.id.as_str(),
                step.depends_on.iter().map(|s| s.as_str()).collect(),
            );
        }

        let mut visited = HashSet::new();
        let mut in_stack = HashSet::new();

        fn dfs<'a>(
            node: &'a str,
            adj: &HashMap<&'a str, Vec<&'a str>>,
            visited: &mut HashSet<&'a str>,
            in_stack: &mut HashSet<&'a str>,
            path: &mut Vec<&'a str>,
        ) -> Result<(), String> {
            visited.insert(node);
            in_stack.insert(node);
            path.push(node);

            if let Some(deps) = adj.get(node) {
                for &dep in deps {
                    if !visited.contains(dep) {
                        dfs(dep, adj, visited, in_stack, path)?;
                    } else if in_stack.contains(dep) {
                        let cycle_start = path.iter().position(|&n| n == dep).unwrap();
                        let cycle: Vec<&str> = path[cycle_start..].to_vec();
                        return Err(format!(
                            "Circular dependency detected: {}{}",
                            cycle.join(""),
                            dep
                        ));
                    }
                }
            }

            in_stack.remove(node);
            path.pop();
            Ok(())
        }

        let mut path = Vec::new();
        for step in &self.steps {
            if !visited.contains(step.id.as_str()) {
                dfs(
                    step.id.as_str(),
                    &adj,
                    &mut visited,
                    &mut in_stack,
                    &mut path,
                )?;
            }
        }

        // Also check for references to non-existent steps
        for step in &self.steps {
            for dep in &step.depends_on {
                if !step_ids.contains(dep.as_str()) {
                    return Err(format!(
                        "Step '{}' depends on '{}' which does not exist",
                        step.id, dep
                    ));
                }
            }
        }

        Ok(())
    }
}

// ============================================================================
// Workflow Run (execution state)
// ============================================================================

fn default_empty_object() -> serde_json::Value {
    serde_json::json!({})
}

fn default_now() -> DateTime<Utc> {
    Utc::now()
}

/// One execution of a `WorkflowDefinition`. Owns the definition plus all
/// runtime state — status, shared context, per-step status / result /
/// error / timestamps. The engine mutates a `WorkflowRun`.
///
/// `step_runs` is parallel to `definition.steps` (same length, same
/// order).
///
/// **Wire shape**: `definition` is flattened, so a `WorkflowRun`
/// serializes as one flat JSON object with the definition fields
/// (`id`, `steps`, `input_schema`, …) alongside the runtime fields
/// (`status`, `current_step`, `context`, `step_runs`, …). This
/// keeps the persisted run row identical to the legacy monolithic
/// shape.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowRun {
    #[serde(flatten)]
    pub definition: WorkflowDefinition,
    #[serde(default)]
    pub status: WorkflowStatus,
    #[serde(default)]
    pub current_step: usize,
    #[serde(default = "default_empty_object")]
    pub context: serde_json::Value,
    #[serde(default)]
    pub notes: Vec<WorkflowNote>,
    #[serde(default)]
    pub step_runs: Vec<WorkflowStepRun>,
    #[serde(default = "default_now")]
    pub created_at: DateTime<Utc>,
    #[serde(default = "default_now")]
    pub updated_at: DateTime<Utc>,
}

/// Per-step runtime state. Parallel to `WorkflowDefinition.steps[i]`.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct WorkflowStepRun {
    pub step_id: String,
    #[serde(default)]
    pub status: StepStatus,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub started_at: Option<DateTime<Utc>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completed_at: Option<DateTime<Utc>>,
}

impl WorkflowRun {
    /// Build a fresh run from a definition. All step_runs start `Pending`,
    /// context is `{}`, status is `Pending`.
    pub fn new(definition: WorkflowDefinition) -> Self {
        let step_runs = definition
            .steps
            .iter()
            .map(|s| WorkflowStepRun {
                step_id: s.id.clone(),
                ..Default::default()
            })
            .collect();
        Self {
            definition,
            status: WorkflowStatus::Pending,
            current_step: 0,
            context: serde_json::json!({}),
            notes: vec![],
            step_runs,
            created_at: Utc::now(),
            updated_at: Utc::now(),
        }
    }

    /// Convenience for callers that want to skip the explicit
    /// definition struct (mostly tests).
    pub fn from_steps(steps: Vec<WorkflowStep>) -> Self {
        Self::new(WorkflowDefinition::new(steps))
    }

    pub fn with_context(mut self, context: serde_json::Value) -> Self {
        self.context = context;
        self
    }

    pub fn with_id(mut self, id: &str) -> Self {
        self.definition.id = id.to_string();
        self
    }

    pub fn with_checkpoint(mut self, strategy: CheckpointStrategy) -> Self {
        self.definition.checkpoint = strategy;
        self
    }

    pub fn with_entry_points(mut self, entry_points: Vec<EntryPoint>) -> Self {
        self.definition.entry_points = entry_points;
        self
    }

    pub fn id(&self) -> &str {
        &self.definition.id
    }

    pub fn steps(&self) -> &[WorkflowStep] {
        &self.definition.steps
    }

    pub fn step(&self, idx: usize) -> &WorkflowStep {
        &self.definition.steps[idx]
    }

    pub fn step_run(&self, idx: usize) -> &WorkflowStepRun {
        &self.step_runs[idx]
    }

    pub fn step_run_mut(&mut self, idx: usize) -> &mut WorkflowStepRun {
        &mut self.step_runs[idx]
    }

    pub fn step_run_by_id(&self, step_id: &str) -> Option<&WorkflowStepRun> {
        self.step_runs.iter().find(|s| s.step_id == step_id)
    }

    pub fn step_run_by_id_mut(&mut self, step_id: &str) -> Option<&mut WorkflowStepRun> {
        self.step_runs.iter_mut().find(|s| s.step_id == step_id)
    }

    /// Apply an entry point: mark steps not reachable from `starts_at` as
    /// Skipped, pre-populate their results from `preset_results`, and
    /// merge those into context so downstream `{steps.X}` references work.
    pub fn apply_entry_point(mut self, entry_point_id: &str) -> Result<Self, String> {
        let ep = self
            .definition
            .entry_points
            .iter()
            .find(|ep| ep.id == entry_point_id)
            .ok_or_else(|| format!("Entry point '{}' not found", entry_point_id))?
            .clone();

        if !self.definition.steps.iter().any(|s| s.id == ep.starts_at) {
            return Err(format!(
                "Entry point '{}' references step '{}' which does not exist",
                entry_point_id, ep.starts_at
            ));
        }

        let reachable = self.definition.reachable_from(&ep.starts_at);

        for (i, step) in self.definition.steps.iter().enumerate() {
            if !reachable.contains(&step.id) {
                self.step_runs[i].status = StepStatus::Skipped;
                if let Some(result) = ep.preset_results.get(&step.id) {
                    self.step_runs[i].result = Some(result.clone());
                }
            }
        }

        if let Some(ctx) = self.context.as_object_mut() {
            let steps = ctx
                .entry("steps")
                .or_insert(serde_json::json!({}))
                .as_object_mut()
                .expect("steps must be an object");
            for (step_id, result) in &ep.preset_results {
                steps.insert(step_id.clone(), result.clone());
            }
        }

        Ok(self)
    }

    /// Initialize the run with validated input. Input is validated
    /// against `definition.input_schema` if present, then merged into
    /// `context`. Status flips to Running.
    pub fn with_input(mut self, input: serde_json::Value) -> Result<Self, String> {
        if let Some(ref schema_value) = self.definition.input_schema {
            let validator = jsonschema::validator_for(schema_value)
                .map_err(|e| format!("Invalid input_schema: {e}"))?;

            if !validator.is_valid(&input) {
                let errors: Vec<String> = validator
                    .iter_errors(&input)
                    .map(|e| format!("{}", e))
                    .collect();
                return Err(format!("Input validation failed: {}", errors.join("; ")));
            }
        }

        if let (Some(ctx), Some(inp)) = (self.context.as_object_mut(), input.as_object()) {
            for (k, v) in inp {
                ctx.insert(k.clone(), v.clone());
            }
            ctx.insert("input".to_string(), input.clone());
        }

        self.status = WorkflowStatus::Running;
        self.updated_at = Utc::now();
        Ok(self)
    }

    /// First pending step, if any.
    pub fn next_pending_step(&self) -> Option<(usize, &WorkflowStep)> {
        self.step_runs
            .iter()
            .enumerate()
            .find(|(_, s)| s.status == StepStatus::Pending)
            .map(|(i, _)| (i, &self.definition.steps[i]))
    }

    /// All steps that can run now: pending + all dependencies completed.
    /// Pure query — does not mutate.
    pub fn runnable_steps(&self) -> Vec<(usize, &WorkflowStep)> {
        let mut runnable = vec![];
        for (i, step) in self.definition.steps.iter().enumerate() {
            if self.step_runs[i].status != StepStatus::Pending {
                continue;
            }
            let deps_met = step.depends_on.iter().all(|dep_id| {
                self.definition
                    .steps
                    .iter()
                    .zip(self.step_runs.iter())
                    .any(|(s, sr)| {
                        &s.id == dep_id
                            && matches!(sr.status, StepStatus::Done | StepStatus::Skipped)
                    })
            });
            if deps_met {
                runnable.push((i, step));
            }
        }
        runnable
    }

    pub fn is_complete(&self) -> bool {
        self.step_runs.iter().all(|s| {
            matches!(
                s.status,
                StepStatus::Done | StepStatus::Skipped | StepStatus::Blocked
            )
        })
    }

    pub fn is_waiting_for_input(&self) -> bool {
        self.step_runs
            .iter()
            .any(|s| s.status == StepStatus::WaitingForInput)
    }

    pub fn waiting_step(&self) -> Option<(usize, &WorkflowStep)> {
        self.step_runs
            .iter()
            .enumerate()
            .find(|(_, s)| s.status == StepStatus::WaitingForInput)
            .map(|(i, _)| (i, &self.definition.steps[i]))
    }

    /// Resume a paused run by providing input for the waiting step.
    pub fn resume_step(
        &mut self,
        step_id: &str,
        result: serde_json::Value,
    ) -> Result<usize, String> {
        let idx = self
            .step_runs
            .iter()
            .position(|s| s.step_id == step_id && s.status == StepStatus::WaitingForInput)
            .ok_or_else(|| {
                format!(
                    "Step '{}' not found or not in waiting_for_input state",
                    step_id
                )
            })?;

        self.step_runs[idx].status = StepStatus::Done;
        self.step_runs[idx].result = Some(result.clone());
        self.step_runs[idx].completed_at = Some(Utc::now());

        if let Some(ctx) = self.context.as_object_mut() {
            let steps = ctx
                .entry("steps")
                .or_insert(serde_json::json!({}))
                .as_object_mut()
                .expect("steps must be an object");
            steps.insert(step_id.to_string(), result);
        }

        self.status = WorkflowStatus::Running;
        self.updated_at = Utc::now();
        Ok(idx)
    }

    /// Stuck: blocked steps, nothing running, no path forward.
    pub fn is_stuck(&self) -> bool {
        let has_blocked = self
            .step_runs
            .iter()
            .any(|s| s.status == StepStatus::Blocked);
        let has_pending = self
            .step_runs
            .iter()
            .any(|s| s.status == StepStatus::Pending);
        let has_running = self
            .step_runs
            .iter()
            .any(|s| s.status == StepStatus::Running);

        if !has_blocked || has_running {
            return false;
        }

        if !has_pending {
            return true;
        }

        !self
            .definition
            .steps
            .iter()
            .zip(self.step_runs.iter())
            .any(|(step, run)| {
                run.status == StepStatus::Pending
                    && step.depends_on.iter().all(|dep_id| {
                        self.definition
                            .steps
                            .iter()
                            .zip(self.step_runs.iter())
                            .any(|(s, sr)| {
                                &s.id == dep_id
                                    && matches!(
                                        sr.status,
                                        StepStatus::Done
                                            | StepStatus::Pending
                                            | StepStatus::Running
                                    )
                            })
                    })
            })
    }

    pub fn has_failed(&self) -> bool {
        self.step_runs
            .iter()
            .any(|s| s.status == StepStatus::Failed)
    }

    /// Append a note to the run's log.
    pub fn add_note(&mut self, step_id: &str, message: &str) {
        self.notes.push(WorkflowNote {
            step_id: step_id.to_string(),
            message: message.to_string(),
            at: Utc::now(),
        });
        self.updated_at = Utc::now();
    }

    /// Validate the underlying DAG. Convenience that delegates to the
    /// definition's `detect_cycles`.
    pub fn detect_cycles(&self) -> Result<(), String> {
        self.definition.detect_cycles()
    }
}

// ============================================================================
// Entry Point — named starting positions for multi-entry workflows
// ============================================================================

/// A named entry point into a workflow.
/// Allows workflows to be started at different steps depending on context.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntryPoint {
    /// Unique identifier for this entry point (e.g., "import_from_docs", "grade_only").
    pub id: String,
    /// Human-readable label.
    pub label: String,
    /// Optional description of when to use this entry point.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The step ID where execution begins.
    pub starts_at: String,
    /// Pre-populated step results for steps that are skipped.
    /// Maps step_id → result value. These steps are marked Done before execution starts.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub preset_results: HashMap<String, serde_json::Value>,
    /// Required input fields for this entry point (for UI/validation).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub required_inputs: Vec<String>,
    /// How a channel user reaches this entry point (slash command,
    /// callback button, or free-text catch-all). `None` = not channel-
    /// reachable (e.g. an internal or scheduled entry point).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trigger: Option<distri_types::channel_commands::ChannelTrigger>,
}

// ============================================================================
// Workflow Step (template)
// ============================================================================

/// A single step in a workflow. **Template only** — runtime status /
/// result / timestamps live on `WorkflowStepRun`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowStep {
    pub id: String,
    pub label: String,
    pub kind: StepKind,
    /// IDs of steps that must complete before this one can run.
    #[serde(default)]
    pub depends_on: Vec<String>,
    /// Execution mode for this step.
    #[serde(default)]
    pub execution: StepExecution,
    /// Capabilities required to run this step.
    #[serde(default)]
    pub requires: Vec<StepRequirement>,
    /// Optional explicit input mapping for this step.
    /// Values can reference `{input.X}`, `{steps.step_id.X}`, `{env.X}`.
    /// If omitted, the step receives the full execution context.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub input: Option<serde_json::Value>,
    /// Skip this step if the expression evaluates to true against the workflow context.
    /// Expression format: `{input.field_name}` — truthy check (field exists and is not null/false/empty).
    /// Supports negation: `!{input.field_name}` — skip if field is absent/falsy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub skip_if: Option<String>,
}

impl WorkflowStep {
    fn new_step(id: &str, label: &str, kind: StepKind) -> Self {
        Self {
            id: id.to_string(),
            label: label.to_string(),
            kind,
            depends_on: vec![],
            execution: StepExecution::Sequential,
            requires: vec![],
            input: None,
            skip_if: None,
        }
    }

    pub fn api_call(id: &str, label: &str, method: &str, url: &str) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::ApiCall {
                method: method.to_string(),
                url: url.to_string(),
                body: None,
                headers: None,
            },
        )
    }

    pub fn agent_run(id: &str, label: &str, agent_id: &str, prompt: &str) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::AgentRun {
                agent_id: agent_id.to_string(),
                prompt: prompt.to_string(),
                tools: vec![],
                skills: vec![],
                model: None,
                max_iterations: None,
            },
        )
    }

    pub fn script(id: &str, label: &str, command: &str) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::Script {
                command: command.to_string(),
                args: vec![],
                cwd: None,
                env: None,
                timeout_secs: None,
                output_format: None,
                shell: None,
            },
        )
    }

    pub fn tool_call(id: &str, label: &str, tool_name: &str, input: serde_json::Value) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::ToolCall {
                tool_name: tool_name.to_string(),
                input,
                agent_id: None,
            },
        )
    }

    pub fn condition(
        id: &str,
        label: &str,
        expression: &str,
        if_true: StepKind,
        if_false: Option<StepKind>,
    ) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::Condition {
                expression: expression.to_string(),
                if_true: Box::new(if_true),
                if_false: if_false.map(Box::new),
            },
        )
    }

    pub fn checkpoint(id: &str, label: &str, message: &str) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::Checkpoint {
                message: message.to_string(),
            },
        )
    }

    pub fn wait_for_input(id: &str, label: &str, message: &str) -> Self {
        Self::new_step(
            id,
            label,
            StepKind::WaitForInput {
                message: message.to_string(),
                schema: None,
            },
        )
    }

    pub fn with_body(mut self, body: serde_json::Value) -> Self {
        if let StepKind::ApiCall {
            body: ref mut b, ..
        } = self.kind
        {
            *b = Some(body);
        }
        self
    }

    pub fn with_depends_on(mut self, deps: Vec<&str>) -> Self {
        self.depends_on = deps.into_iter().map(|s| s.to_string()).collect();
        self
    }

    pub fn parallel(mut self) -> Self {
        self.execution = StepExecution::Parallel;
        self
    }

    pub fn with_requires(mut self, requires: Vec<StepRequirement>) -> Self {
        self.requires = requires;
        self
    }

    pub fn with_cwd(mut self, cwd: &str) -> Self {
        if let StepKind::Script { cwd: ref mut c, .. } = self.kind {
            *c = Some(cwd.to_string());
        }
        self
    }

    pub fn with_timeout(mut self, secs: u64) -> Self {
        if let StepKind::Script {
            timeout_secs: ref mut t,
            ..
        } = self.kind
        {
            *t = Some(secs);
        }
        self
    }

    pub fn with_env(mut self, env: HashMap<String, String>) -> Self {
        if let StepKind::Script { env: ref mut e, .. } = self.kind {
            *e = Some(env);
        }
        self
    }

    pub fn with_input_mapping(mut self, input: serde_json::Value) -> Self {
        self.input = Some(input);
        self
    }

    pub fn with_skip_if(mut self, expression: &str) -> Self {
        self.skip_if = Some(expression.to_string());
        self
    }
}

// ============================================================================
// Step Kind — what the step does
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StepKind {
    /// HTTP API call
    ApiCall {
        method: String,
        url: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        body: Option<serde_json::Value>,
        #[serde(skip_serializing_if = "Option::is_none")]
        headers: Option<HashMap<String, String>>,
    },

    /// Shell script / command
    Script {
        command: String,
        #[serde(default)]
        args: Vec<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        cwd: Option<String>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        env: Option<HashMap<String, String>>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        timeout_secs: Option<u64>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        output_format: Option<ScriptOutputFormat>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        shell: Option<ShellType>,
    },

    /// Delegate to a Distri agent (sub-agent run)
    AgentRun {
        agent_id: String,
        prompt: String,
        #[serde(default)]
        tools: Vec<String>,
        /// Skills to load for this agent step
        #[serde(default)]
        skills: Vec<String>,
        /// Override model for this step
        #[serde(default, skip_serializing_if = "Option::is_none")]
        model: Option<String>,
        /// Limit agent loop iterations
        #[serde(default, skip_serializing_if = "Option::is_none")]
        max_iterations: Option<u32>,
    },

    /// Single tool invocation — not a full agent loop
    ToolCall {
        /// Tool name (must be registered)
        tool_name: String,
        /// Tool input parameters
        input: serde_json::Value,
        /// Agent context to execute in (for tools needing agent-scoped permissions)
        #[serde(default, skip_serializing_if = "Option::is_none")]
        agent_id: Option<String>,
    },

    /// Conditional branch — evaluates expression against context
    Condition {
        expression: String,
        if_true: Box<StepKind>,
        #[serde(skip_serializing_if = "Option::is_none")]
        if_false: Option<Box<StepKind>>,
    },

    /// No-op / marker step (for documentation or manual checkpoints)
    Checkpoint { message: String },

    /// Pause execution and wait for external/human input before continuing.
    /// The workflow saves state and stops. A resume call provides the input
    /// as the step result and continues from here.
    WaitForInput {
        /// Message to display to the human (what input is needed)
        message: String,
        /// Optional JSON Schema describing the expected input shape
        #[serde(default, skip_serializing_if = "Option::is_none")]
        schema: Option<serde_json::Value>,
    },

    /// Emit a channel reply (text + optional buttons). Rendered by the
    /// gateway per channel. `text` / button fields support the standard
    /// `{input.x}` / `{steps.id.x}` interpolation; `button_template`
    /// fields additionally support `{item.x}` per `buttons_from`
    /// element.
    Reply {
        text: String,
        /// Static buttons (rows: outer = top-to-bottom).
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        buttons: Vec<Vec<distri_types::channel_commands::ReplyButtonSpec>>,
        /// Context path resolving to an array; one button per element.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        buttons_from: Option<String>,
        /// Template applied per `buttons_from` element.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        button_template: Option<distri_types::channel_commands::ReplyButtonSpec>,
    },
}

// ============================================================================
// Step Requirement — what a step needs to run
// ============================================================================

/// A capability required to execute a step.
/// Uses namespaced skill identifiers:
/// - `native:shell`, `native:browser`, `native:network` — built-in
/// - `{provider}:{service}` — connections (e.g., `google:drive`, `slack:chat`)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct StepRequirement {
    /// Namespaced skill identifier.
    pub skill: String,
    /// Required permissions/scopes within the skill.
    #[serde(default)]
    pub permissions: Vec<String>,
    /// Optional extra constraints.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub config: Option<serde_json::Value>,
}

impl StepRequirement {
    /// Create a native skill requirement (prefixed with "native:").
    pub fn native(skill: &str) -> Self {
        Self {
            skill: format!("native:{}", skill),
            permissions: vec![],
            config: None,
        }
    }

    /// Create a connection requirement (e.g., "google:drive").
    pub fn connection(provider: &str, service: &str) -> Self {
        Self {
            skill: format!("{}:{}", provider, service),
            permissions: vec![],
            config: None,
        }
    }

    pub fn with_permissions(mut self, perms: Vec<&str>) -> Self {
        self.permissions = perms.into_iter().map(|s| s.to_string()).collect();
        self
    }

    /// Get the namespace (part before ':').
    pub fn namespace(&self) -> Option<&str> {
        self.skill.split(':').next()
    }

    /// Get the skill name (part after ':').
    pub fn skill_name(&self) -> Option<&str> {
        self.skill.split(':').nth(1)
    }

    /// Check if this is a native skill.
    pub fn is_native(&self) -> bool {
        self.skill.starts_with("native:")
    }

    /// Validate the requirement. Returns error message if invalid.
    pub fn validate(&self) -> Result<(), String> {
        if !self.skill.contains(':') {
            return Err(format!(
                "Invalid skill identifier '{}': must be namespaced (e.g., 'native:shell', 'google:drive')",
                self.skill
            ));
        }

        if self.is_native() {
            let known = ["shell", "browser", "network", "agent", "tool"];
            if let Some(name) = self.skill_name() {
                if !known.contains(&name) {
                    return Err(format!(
                        "Unknown native skill '{}'. Known: {:?}",
                        name, known
                    ));
                }
            }
        }

        Ok(())
    }
}

// ============================================================================
// Checkpoint Strategy
// ============================================================================

/// How workflow state is checkpointed between steps.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum CheckpointStrategy {
    /// Redis-based, thread+task scoped, auto-TTL.
    Internal {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        ttl_secs: Option<u64>,
    },
    /// Client-registered tool handles persistence.
    /// Tool must support actions: save, load, list.
    External { tool_name: String },
}

impl Default for CheckpointStrategy {
    fn default() -> Self {
        CheckpointStrategy::Internal { ttl_secs: None }
    }
}

/// Metadata about a checkpoint snapshot.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointMeta {
    pub checkpoint_id: String,
    pub workflow_id: String,
    pub step_id: String,
    pub created_at: DateTime<Utc>,
}

// ============================================================================
// Enums
// ============================================================================

/// Top-level run status. Engine-internal; external surfaces translate
/// to `distri_types::TaskStatus` via `From`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum WorkflowStatus {
    #[default]
    Pending,
    Running,
    /// Waiting for human/external input (`WaitForInput` step).
    Paused,
    Completed,
    Failed,
    /// All remaining steps are blocked — requirements cannot be met.
    Blocked,
}

impl From<WorkflowStatus> for TaskStatus {
    fn from(s: WorkflowStatus) -> Self {
        match s {
            WorkflowStatus::Pending => TaskStatus::Pending,
            WorkflowStatus::Running => TaskStatus::Running,
            WorkflowStatus::Paused => TaskStatus::InputRequired,
            WorkflowStatus::Completed => TaskStatus::Completed,
            WorkflowStatus::Failed => TaskStatus::Failed,
            // No 1:1 TaskStatus for Blocked — surface as Failed; the
            // step-level error fields carry the "missing skills:" reason.
            WorkflowStatus::Blocked => TaskStatus::Failed,
        }
    }
}

/// Per-step phase. Engine-internal; richer than `TaskStatus` because
/// the engine cares about the difference between "blocked on a missing
/// requirement" (cannot start) and "failed during execution" (tried,
/// errored). External surfaces translate via `From<StepStatus>`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum StepStatus {
    #[default]
    Pending,
    /// Requirements not met — cannot execute.
    Blocked,
    Running,
    Done,
    Failed,
    Skipped,
    /// Step is waiting for external/human input. Workflow is paused.
    WaitingForInput,
}

impl From<StepStatus> for TaskStatus {
    fn from(s: StepStatus) -> Self {
        match s {
            StepStatus::Pending => TaskStatus::Pending,
            // No TaskStatus::Blocked — surface as Failed (with
            // `step_run.error` carrying the missing-requirement reason).
            StepStatus::Blocked => TaskStatus::Failed,
            StepStatus::Running => TaskStatus::Running,
            StepStatus::Done => TaskStatus::Completed,
            StepStatus::Failed => TaskStatus::Failed,
            // Intentionally not run; semantically a deliberate cancel.
            StepStatus::Skipped => TaskStatus::Canceled,
            StepStatus::WaitingForInput => TaskStatus::InputRequired,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum StepExecution {
    /// Must wait for previous step to complete.
    #[default]
    Sequential,
    /// Can run in parallel with other parallel steps at the same level.
    Parallel,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ScriptOutputFormat {
    Text,
    Json,
    Stream,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ShellType {
    Bash,
    Sh,
    Zsh,
}

// ============================================================================
// Step Result
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepResult {
    pub status: StepStatus,
    pub result: Option<serde_json::Value>,
    pub error: Option<String>,
    /// Updates to merge into workflow context for subsequent steps.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub context_updates: Option<serde_json::Value>,
}

impl StepResult {
    pub fn done(result: serde_json::Value) -> Self {
        Self {
            status: StepStatus::Done,
            result: Some(result),
            error: None,
            context_updates: None,
        }
    }

    pub fn done_with_context(result: serde_json::Value, updates: serde_json::Value) -> Self {
        Self {
            status: StepStatus::Done,
            result: Some(result),
            error: None,
            context_updates: Some(updates),
        }
    }

    pub fn failed(error: &str) -> Self {
        Self {
            status: StepStatus::Failed,
            result: None,
            error: Some(error.to_string()),
            context_updates: None,
        }
    }

    pub fn skipped() -> Self {
        Self {
            status: StepStatus::Skipped,
            result: None,
            error: None,
            context_updates: None,
        }
    }
}

// ============================================================================
// Workflow Note
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowNote {
    pub step_id: String,
    pub message: String,
    pub at: DateTime<Utc>,
}

// ============================================================================
// Workflow Run Summary (returned at end of execution)
// ============================================================================

/// Snapshot of one finished step in a `WorkflowRunSummary`.
///
/// Surfaces `distri_types::TaskStatus` at the boundary — engine-internal
/// `StepStatus` distinctions (Blocked / Skipped) translate via
/// `StepStatus → TaskStatus`. The original phase is still recoverable
/// from the `error` field for Blocked steps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowStepSummary {
    pub id: String,
    pub label: String,
    pub status: TaskStatus,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub result: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Final summary of a workflow run — id, terminal status, and one row
/// per step. Returned to callers (e.g. the WorkflowAgent invoke result)
/// instead of an ad-hoc JSON shape.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowRunSummary {
    pub workflow_id: String,
    pub status: TaskStatus,
    pub steps: Vec<WorkflowStepSummary>,
}

impl WorkflowRunSummary {
    /// Build a summary from a finished `WorkflowRun` and its terminal
    /// `WorkflowStatus`. Translates statuses to `TaskStatus` at the
    /// boundary so consumers don't need to know about the engine's
    /// internal enums.
    pub fn from_run(run: &WorkflowRun, status: WorkflowStatus) -> Self {
        let steps = run
            .steps()
            .iter()
            .zip(run.step_runs.iter())
            .map(|(step, sr)| WorkflowStepSummary {
                id: step.id.clone(),
                label: step.label.clone(),
                status: sr.status.into(),
                result: sr.result.clone(),
                error: sr.error.clone(),
            })
            .collect();
        Self {
            workflow_id: run.id().to_string(),
            status: status.into(),
            steps,
        }
    }
}

// ============================================================================
// Workflow Events (for streaming to clients)
// ============================================================================

/// Events emitted during workflow execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "event", rename_all = "snake_case")]
pub enum WorkflowEvent {
    /// Workflow started
    WorkflowStarted {
        workflow_id: String,
        total_steps: usize,
    },
    /// A step started executing
    StepStarted {
        workflow_id: String,
        step_id: String,
        step_label: String,
    },
    /// A step completed successfully
    StepCompleted {
        workflow_id: String,
        step_id: String,
        step_label: String,
        result: Option<serde_json::Value>,
    },
    /// A step failed
    StepFailed {
        workflow_id: String,
        step_id: String,
        step_label: String,
        error: String,
    },
    /// A step is waiting for external/human input
    StepWaiting {
        workflow_id: String,
        step_id: String,
        step_label: String,
        message: String,
        schema: Option<serde_json::Value>,
    },
    /// Workflow completed (all steps done or failed)
    WorkflowCompleted {
        workflow_id: String,
        status: WorkflowStatus,
        steps_done: usize,
        steps_failed: usize,
    },
}