ironflow-engine 2.41.0

Workflow orchestration engine for ironflow with FSM-based run lifecycle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! [`HumanInputConfig`] -- configuration for typed human input steps.

use std::time::Duration;

use ironflow_store::entities::Assignee;
use serde::{Deserialize, Serialize};

use super::{ApprovalConfig, Approvers, EscalationPolicy};

/// Key of the JSON schema of the expected answer in the stored step input.
///
/// A human input step stores its flattened [`HumanInputConfig`] in
/// `step.input`, plus the JSON schema of the answer type under this key. The
/// API validates a submitted answer against it before the run resumes.
///
/// # Examples
///
/// ```
/// use ironflow_engine::config::HUMAN_INPUT_SCHEMA_KEY;
///
/// assert_eq!(HUMAN_INPUT_SCHEMA_KEY, "schema");
/// ```
pub const HUMAN_INPUT_SCHEMA_KEY: &str = "schema";

/// Configuration for a typed human input step.
///
/// When the workflow reaches a human input step, the run transitions to
/// `AwaitingApproval` and waits for a human to submit an answer matching the
/// JSON schema of the expected type, or to reject the request, via the API.
///
/// The step reuses the approval gate machinery: a deadline, an escalation
/// policy, an assignee and the [`Approvers`] allowed to answer. The first valid
/// answer wins; [`Approvers::at_least`] only restricts who may answer.
///
/// A human input cannot be auto-approved on timeout since there is no value to
/// fill in: [`on_timeout`](Self::on_timeout) refuses
/// [`EscalationPolicy::AutoApprove`].
///
/// # Examples
///
/// ```
/// use std::time::Duration;
/// use ironflow_engine::config::{EscalationPolicy, HumanInputConfig};
///
/// let config = HumanInputConfig::new("Answer the clarification questions")
///     .with_deadline(Duration::from_secs(3600))
///     .on_timeout(EscalationPolicy::AutoReject);
/// assert_eq!(config.message(), "Answer the clarification questions");
/// assert_eq!(config.deadline_secs(), Some(3600));
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HumanInputConfig {
    #[serde(flatten)]
    gate: ApprovalConfig,
}

impl HumanInputConfig {
    /// Create a new human input config with the given message.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// let config = HumanInputConfig::new("Which environment?");
    /// assert_eq!(config.message(), "Which environment?");
    /// ```
    pub fn new(message: &str) -> Self {
        Self {
            gate: ApprovalConfig::new(message),
        }
    }

    /// Set the SLA deadline of this input.
    ///
    /// Sub-second precision is dropped: the deadline is stored in whole seconds.
    ///
    /// # Panics
    ///
    /// Panics if `deadline` rounds down to zero seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// let config = HumanInputConfig::new("Answer?").with_deadline(Duration::from_secs(1800));
    /// assert_eq!(config.deadline(), Some(Duration::from_secs(1800)));
    /// ```
    pub fn with_deadline(self, deadline: Duration) -> Self {
        Self {
            gate: self.gate.with_deadline(deadline),
        }
    }

    /// Set the SLA deadline of this input, in seconds.
    ///
    /// # Panics
    ///
    /// Panics if `secs` is zero.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// let config = HumanInputConfig::new("Answer?").with_deadline_secs(1800);
    /// assert_eq!(config.deadline_secs(), Some(1800));
    /// ```
    pub fn with_deadline_secs(self, secs: u64) -> Self {
        Self {
            gate: self.gate.with_deadline_secs(secs),
        }
    }

    /// Set the policy applied when the deadline fires.
    ///
    /// # Panics
    ///
    /// Panics if the policy is [`EscalationPolicy::AutoApprove`] or a
    /// [`EscalationPolicy::Chain`] containing it: a human input has no value
    /// to fill in automatically.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::{EscalationPolicy, HumanInputConfig};
    ///
    /// let config = HumanInputConfig::new("Answer?")
    ///     .with_deadline_secs(3600)
    ///     .on_timeout(EscalationPolicy::AutoReject);
    /// assert_eq!(config.effective_policy(), EscalationPolicy::AutoReject);
    /// ```
    ///
    /// ```should_panic
    /// use ironflow_engine::config::{EscalationPolicy, HumanInputConfig};
    ///
    /// let _ = HumanInputConfig::new("Answer?").on_timeout(EscalationPolicy::AutoApprove);
    /// ```
    pub fn on_timeout(self, policy: EscalationPolicy) -> Self {
        assert!(
            !allows_auto_approve(&policy),
            "a human input step cannot be auto-approved on timeout"
        );
        Self {
            gate: self.gate.on_timeout(policy),
        }
    }

    /// Assign the input to a user or group.
    ///
    /// # Panics
    ///
    /// Panics if the assignee name is empty or only whitespace.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    /// use ironflow_store::entities::Assignee;
    ///
    /// let config = HumanInputConfig::new("Answer?").assigned_to(Assignee::user("alice"));
    /// assert_eq!(config.assignee(), Some(&Assignee::user("alice")));
    /// ```
    pub fn assigned_to(self, assignee: Assignee) -> Self {
        Self {
            gate: self.gate.assigned_to(assignee),
        }
    }

    /// Restrict who may answer. A later call replaces an earlier one.
    ///
    /// The first valid answer resolves the step, whatever the required count.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::{Approvers, HumanInputConfig};
    ///
    /// let config = HumanInputConfig::new("Answer?")
    ///     .requiring(Approvers::any().from_groups(["product"]));
    /// assert!(config.approvers().is_some());
    /// ```
    pub fn requiring(self, approvers: Approvers) -> Self {
        Self {
            gate: self.gate.requiring(approvers),
        }
    }

    /// The message displayed to the person answering.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// assert_eq!(HumanInputConfig::new("Answer?").message(), "Answer?");
    /// ```
    pub fn message(&self) -> &str {
        self.gate.message()
    }

    /// The configured SLA deadline, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::time::Duration;
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// let config = HumanInputConfig::new("Answer?").with_deadline_secs(60);
    /// assert_eq!(config.deadline(), Some(Duration::from_secs(60)));
    /// ```
    pub fn deadline(&self) -> Option<Duration> {
        self.gate.deadline()
    }

    /// The configured SLA deadline in seconds, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// assert!(HumanInputConfig::new("Answer?").deadline_secs().is_none());
    /// ```
    pub fn deadline_secs(&self) -> Option<u64> {
        self.gate.deadline_secs()
    }

    /// The configured escalation policy, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// assert!(HumanInputConfig::new("Answer?").on_timeout_policy().is_none());
    /// ```
    pub fn on_timeout_policy(&self) -> Option<&EscalationPolicy> {
        self.gate.on_timeout_policy()
    }

    /// The user or group the input is assigned to, if any.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// assert!(HumanInputConfig::new("Answer?").assignee().is_none());
    /// ```
    pub fn assignee(&self) -> Option<&Assignee> {
        self.gate.assignee()
    }

    /// The approvers allowed to answer, if any were set.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// assert!(HumanInputConfig::new("Answer?").approvers().is_none());
    /// ```
    pub fn approvers(&self) -> Option<&Approvers> {
        self.gate.approvers()
    }

    /// The deadline actually enforced, in seconds.
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::HumanInputConfig;
    ///
    /// let config = HumanInputConfig::new("Answer?").with_deadline_secs(60);
    /// assert_eq!(config.effective_deadline_secs(), Some(60));
    /// ```
    pub fn effective_deadline_secs(&self) -> Option<u64> {
        self.gate.effective_deadline_secs()
    }

    /// The policy applied when the deadline fires. Defaults to
    /// [`EscalationPolicy::AutoReject`].
    ///
    /// # Examples
    ///
    /// ```
    /// use ironflow_engine::config::{EscalationPolicy, HumanInputConfig};
    ///
    /// let config = HumanInputConfig::new("Answer?").with_deadline_secs(60);
    /// assert_eq!(config.effective_policy(), EscalationPolicy::AutoReject);
    /// ```
    pub fn effective_policy(&self) -> EscalationPolicy {
        self.gate.effective_policy()
    }
}

/// Whether `policy` can end up auto-approving the step.
fn allows_auto_approve(policy: &EscalationPolicy) -> bool {
    match policy {
        EscalationPolicy::AutoApprove => true,
        EscalationPolicy::Chain(policies) => policies.iter().any(allows_auto_approve),
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use serde_json::{from_str, from_value, json, to_string, to_value};

    use super::*;
    use crate::config::NotificationTarget;

    fn webhook() -> EscalationPolicy {
        EscalationPolicy::Notify(vec![NotificationTarget::Webhook {
            url: "https://example.com/sla".to_string(),
        }])
    }

    #[test]
    fn new_sets_message() {
        let config = HumanInputConfig::new("Answer?");
        assert_eq!(config.message(), "Answer?");
        assert!(config.deadline_secs().is_none());
        assert!(config.on_timeout_policy().is_none());
        assert!(config.assignee().is_none());
        assert!(config.approvers().is_none());
    }

    #[test]
    fn builder_values_are_stored() {
        let config = HumanInputConfig::new("Answer?")
            .with_deadline(Duration::from_secs(120))
            .on_timeout(EscalationPolicy::AutoReject)
            .assigned_to(Assignee::group("product"))
            .requiring(Approvers::at_least(2).from_groups(["product"]));

        assert_eq!(config.deadline(), Some(Duration::from_secs(120)));
        assert_eq!(config.effective_deadline_secs(), Some(120));
        assert_eq!(
            config.on_timeout_policy(),
            Some(&EscalationPolicy::AutoReject)
        );
        assert_eq!(config.assignee(), Some(&Assignee::group("product")));
        assert_eq!(config.approvers().map(Approvers::required), Some(2));
    }

    #[test]
    fn with_deadline_secs_is_stored() {
        let config = HumanInputConfig::new("Answer?").with_deadline_secs(30);
        assert_eq!(config.deadline_secs(), Some(30));
    }

    #[test]
    #[should_panic(expected = "approval deadline must be greater than zero")]
    fn with_deadline_secs_rejects_zero() {
        let _ = HumanInputConfig::new("Answer?").with_deadline_secs(0);
    }

    #[test]
    fn on_timeout_accepts_policies_without_auto_approve() {
        let reject = HumanInputConfig::new("Answer?").on_timeout(EscalationPolicy::AutoReject);
        assert_eq!(reject.effective_policy(), EscalationPolicy::AutoReject);

        let notify = HumanInputConfig::new("Answer?").on_timeout(webhook());
        assert_eq!(notify.effective_policy(), webhook());

        let chain = EscalationPolicy::Chain(vec![webhook(), EscalationPolicy::AutoReject]);
        let chained = HumanInputConfig::new("Answer?").on_timeout(chain.clone());
        assert_eq!(chained.effective_policy(), chain);
    }

    #[test]
    #[should_panic(expected = "a human input step cannot be auto-approved on timeout")]
    fn on_timeout_rejects_auto_approve() {
        let _ = HumanInputConfig::new("Answer?").on_timeout(EscalationPolicy::AutoApprove);
    }

    #[test]
    #[should_panic(expected = "a human input step cannot be auto-approved on timeout")]
    fn on_timeout_rejects_a_chain_with_auto_approve() {
        let _ = HumanInputConfig::new("Answer?").on_timeout(EscalationPolicy::Chain(vec![
            webhook(),
            EscalationPolicy::AutoApprove,
        ]));
    }

    #[test]
    fn serde_roundtrip() {
        let config = HumanInputConfig::new("Answer?")
            .with_deadline_secs(600)
            .assigned_to(Assignee::user("alice"))
            .requiring(Approvers::any().from_groups(["product"]));

        let json = to_string(&config).expect("serialize");
        let back: HumanInputConfig = from_str(&json).expect("deserialize");

        assert_eq!(back.message(), config.message());
        assert_eq!(back.deadline_secs(), config.deadline_secs());
        assert_eq!(back.assignee(), config.assignee());
        assert_eq!(back.approvers(), config.approvers());
        assert_eq!(to_string(&back).expect("serialize"), json);
    }

    #[test]
    fn serialized_config_reads_as_an_approval_config() {
        let config = HumanInputConfig::new("Answer?").with_deadline_secs(600);
        let mut value = to_value(&config).expect("serialize");
        value.as_object_mut().expect("object").insert(
            HUMAN_INPUT_SCHEMA_KEY.to_string(),
            json!({"type": "object"}),
        );

        let approval: ApprovalConfig = from_value(value).expect("deserialize");
        assert_eq!(approval.message(), "Answer?");
        assert_eq!(approval.deadline_secs(), Some(600));
    }
}