ironflow-engine 2.41.2

Workflow orchestration engine for ironflow with FSM-based run lifecycle
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
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
//! Integration tests for typed human input steps (`ctx.human_input::<T>()`).
//!
//! Every test drives a real [`Engine`] over a real [`InMemoryStore`] with a real
//! [`WorkflowHandler`]. An answer is written on the step through the public
//! store API, the way the API server does, and the run is resumed.
//!
//! Test names contain `human_input` so `cargo test -p ironflow-engine
//! human_input` selects the whole suite.

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

use chrono::{TimeDelta, Utc};
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::{Value, from_value, json};
use tokio::time::timeout;
use uuid::Uuid;

use ironflow_core::error::OperationError;
use ironflow_core::provider::AgentProvider;
use ironflow_core::providers::claude::ClaudeCodeProvider;
use ironflow_core::providers::record_replay::RecordReplayProvider;
use ironflow_engine::config::{
    Approvers, Assignee, HUMAN_INPUT_SCHEMA_KEY, HumanInputConfig, ShellConfig,
};
use ironflow_engine::context::WorkflowContext;
use ironflow_engine::engine::Engine;
use ironflow_engine::error::EngineError;
use ironflow_engine::escalation::{ApprovalEscalator, EscalationAction};
use ironflow_engine::handler::{HandlerFuture, WorkflowHandler};
use ironflow_engine::notify::{WorkflowEvent, WorkflowEventBus, WorkflowInputRequiredEvent};
use ironflow_engine::plan::PlanOptions;
use ironflow_engine::testing::{HumanInputOutcome, MockShellOutput, TestEngine};
use ironflow_store::memory::InMemoryStore;
use ironflow_store::models::{
    RunFilter, RunStatus, RunUpdate, Step, StepKind, StepStatus, StepUpdate, TriggerKind,
};
use ironflow_store::store::{RunStore, Store};

/// Test timeout for bodies that touch sockets or spawn processes.
const TEST_TIMEOUT: Duration = Duration::from_secs(10);

/// Workflow name registered by [`Clarify`].
const WORKFLOW: &str = "clarify";

/// The typed answer the handler asks for.
#[derive(Debug, Deserialize, JsonSchema)]
struct Answers {
    answers: Vec<String>,
}

/// An answer type that can be built from `{}`.
#[derive(Debug, Default, Deserialize, JsonSchema)]
#[serde(default)]
struct Options {
    verbose: bool,
}

/// What the handler does with a rejected input.
#[derive(Clone, Copy, PartialEq, Eq)]
enum OnReject {
    Propagate,
    Continue,
}

/// What the handler saw, shared with the test.
type Seen = Arc<Mutex<Vec<String>>>;

/// Asks for [`Answers`], records them, then runs one more step.
///
/// Fails once with a transient error after the input when `fail_first_attempt`
/// is set, so the run is retried.
struct Clarify {
    config: HumanInputConfig,
    on_reject: OnReject,
    seen: Seen,
    fail_first_attempt: bool,
    attempts: AtomicU32,
}

impl Clarify {
    fn new(config: HumanInputConfig, on_reject: OnReject) -> (Self, Seen) {
        let seen = Seen::default();
        let handler = Self {
            config,
            on_reject,
            seen: seen.clone(),
            fail_first_attempt: false,
            attempts: AtomicU32::new(0),
        };
        (handler, seen)
    }
}

impl WorkflowHandler for Clarify {
    fn name(&self) -> &str {
        WORKFLOW
    }

    fn execute<'a>(&'a self, ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            match ctx
                .human_input::<Answers>("clarify", self.config.clone())
                .await
            {
                Ok(answers) => {
                    self.seen.lock().expect("seen lock").extend(answers.answers);
                }
                Err(EngineError::HumanInputRejected { reason, .. })
                    if self.on_reject == OnReject::Continue =>
                {
                    self.seen
                        .lock()
                        .expect("seen lock")
                        .push(format!("rejected: {reason}"));
                }
                Err(err) => return Err(err),
            }

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

            ctx.shell("after", ShellConfig::new("echo after")).await?;
            Ok(())
        })
    }
}

/// Asks for [`Options`], which accept `{}`, then runs one more step.
struct ClarifyOptions;

impl WorkflowHandler for ClarifyOptions {
    fn name(&self) -> &str {
        "clarify-options"
    }

    fn execute<'a>(&'a self, ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            let options: Options = ctx
                .human_input("options", HumanInputConfig::new("Pick the options"))
                .await?;
            let command = if options.verbose { "echo -v" } else { "echo" };
            ctx.shell("after", ShellConfig::new(command)).await?;
            Ok(())
        })
    }
}

/// Runs a step, asks for [`Answers`] and records them.
///
/// Registered under [`WORKFLOW`] so [`start`] drives it.
struct ClarifyWithBefore {
    seen: Seen,
}

impl WorkflowHandler for ClarifyWithBefore {
    fn name(&self) -> &str {
        WORKFLOW
    }

    fn execute<'a>(&'a self, ctx: &'a mut WorkflowContext) -> HandlerFuture<'a> {
        Box::pin(async move {
            ctx.shell("before", ShellConfig::new("true")).await?;
            let answers: Answers = ctx
                .human_input("clarify", HumanInputConfig::new("Answer?"))
                .await?;
            self.seen.lock().expect("seen lock").extend(answers.answers);
            Ok(())
        })
    }
}

fn provider() -> Arc<dyn AgentProvider> {
    Arc::new(RecordReplayProvider::replay(
        ClaudeCodeProvider::new(),
        "/tmp/ironflow-fixtures",
    ))
}

fn engine_with(store: Arc<InMemoryStore>, handler: impl WorkflowHandler + 'static) -> Engine {
    let store: Arc<dyn Store> = store;
    let mut engine = Engine::new(store, provider());
    engine.register(handler).expect("register handler");
    engine
}

/// Enqueue and execute a run the way the worker does, returning its id.
async fn start(engine: &Engine, store: &Arc<InMemoryStore>, max_retries: u32) -> Uuid {
    let run = engine
        .enqueue_handler(WORKFLOW, TriggerKind::Manual, json!({}), max_retries)
        .await
        .expect("enqueue");
    store
        .update_run_status(run.id, RunStatus::Running)
        .await
        .expect("mark running");
    engine
        .execute_handler_run(run.id)
        .await
        .expect("handler suspends on the input");
    run.id
}

/// The human input steps of a run, oldest first.
async fn input_steps(store: &Arc<InMemoryStore>, run_id: Uuid) -> Vec<Step> {
    store
        .list_steps(run_id)
        .await
        .expect("list steps")
        .into_iter()
        .filter(|s| s.kind == StepKind::HumanInput)
        .collect()
}

/// The single human input step of a run.
async fn input_step(store: &Arc<InMemoryStore>, run_id: Uuid) -> Step {
    let mut steps = input_steps(store, run_id).await;
    assert_eq!(steps.len(), 1, "expected exactly one human input step");
    steps.remove(0)
}

/// Write an answer on the step and mark the run running, like the API does.
async fn answer(store: &Arc<InMemoryStore>, run_id: Uuid, step_id: Uuid, value: Value) {
    store
        .update_step(
            step_id,
            StepUpdate {
                status: Some(StepStatus::Completed),
                output: Some(value),
                completed_at: Some(Utc::now()),
                clear_approval_deadline: true,
                ..StepUpdate::default()
            },
        )
        .await
        .expect("store the answer");
    store
        .update_run_status(run_id, RunStatus::Running)
        .await
        .expect("mark running");
}

/// Reject the step and mark the run running, like the API does.
async fn reject(store: &Arc<InMemoryStore>, run_id: Uuid, step_id: Uuid, reason: &str) {
    store
        .update_step(
            step_id,
            StepUpdate {
                status: Some(StepStatus::Rejected),
                error: Some(reason.to_string()),
                completed_at: Some(Utc::now()),
                clear_approval_deadline: true,
                ..StepUpdate::default()
            },
        )
        .await
        .expect("reject the input");
    store
        .update_run_status(run_id, RunStatus::Running)
        .await
        .expect("mark running");
}

fn seen(seen: &Seen) -> Vec<String> {
    seen.lock().expect("seen lock").clone()
}

#[tokio::test]
async fn human_input_suspends_the_run_and_persists_the_schema() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (handler, seen_answers) = Clarify::new(
            HumanInputConfig::new("Answer the questions"),
            OnReject::Propagate,
        );
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 0).await;

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

        let step = input_step(&store, run_id).await;
        assert_eq!(step.status.state, StepStatus::AwaitingApproval);
        let input = step.input.expect("the step stores its input");
        assert_eq!(input["message"], json!("Answer the questions"));
        assert!(
            input[HUMAN_INPUT_SCHEMA_KEY]["properties"]["answers"].is_object(),
            "got {input}"
        );
        assert!(seen(&seen_answers).is_empty());
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_resumes_with_the_typed_answer() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (handler, seen_answers) =
            Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 0).await;
        let step = input_step(&store, run_id).await;
        answer(
            &store,
            run_id,
            step.id,
            json!({"answers": ["staging", "eu-west"]}),
        )
        .await;

        engine.resume_run(run_id).await.expect("resume");

        let run = store.get_run(run_id).await.unwrap().unwrap();
        assert_eq!(run.status.state, RunStatus::Completed);
        assert_eq!(seen(&seen_answers), vec!["staging", "eu-west"]);
        assert_eq!(input_steps(&store, run_id).await.len(), 1);
    })
    .await
    .expect("test timed out");
}

/// A run requeued after its input was answered keeps `retry_count == 0`, and
/// a worker picks it up through `execute_handler_run`, not `resume_run`: the
/// step before the input must replay from cache and the answer must be found.
#[tokio::test]
async fn human_input_execute_handler_run_replays_answer_without_rerunning_prior_steps() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let seen_answers = Seen::default();
        let handler = ClarifyWithBefore {
            seen: seen_answers.clone(),
        };
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 0).await;
        let step = input_step(&store, run_id).await;
        answer(&store, run_id, step.id, json!({"answers": ["ok"]})).await;

        engine
            .execute_handler_run(run_id)
            .await
            .expect("resume via worker pickup");

        let run = store.get_run(run_id).await.unwrap().unwrap();
        assert_eq!(run.status.state, RunStatus::Completed);
        assert_eq!(run.retry_count, 0);
        assert_eq!(seen(&seen_answers), vec!["ok"]);
        let before = store
            .list_steps(run_id)
            .await
            .unwrap()
            .iter()
            .filter(|s| s.name == "before")
            .count();
        assert_eq!(before, 1);
        assert_eq!(input_steps(&store, run_id).await.len(), 1);
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_answer_that_does_not_match_the_type_fails_the_run() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (handler, _) = Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 1).await;
        let step = input_step(&store, run_id).await;
        answer(&store, run_id, step.id, json!({"answers": 3})).await;

        let err = engine.resume_run(run_id).await.expect_err("type mismatch");
        assert!(matches!(err, EngineError::StepConfig(_)), "got {err:?}");

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

#[tokio::test]
async fn human_input_rejected_step_reaches_the_handler() {
    timeout(TEST_TIMEOUT, async {
        // A handler that catches the rejection keeps going.
        let store = Arc::new(InMemoryStore::new());
        let (handler, seen_answers) =
            Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Continue);
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 0).await;
        let step = input_step(&store, run_id).await;
        reject(&store, run_id, step.id, "out of scope").await;

        engine.resume_run(run_id).await.expect("resume");

        let run = store.get_run(run_id).await.unwrap().unwrap();
        assert_eq!(run.status.state, RunStatus::Completed);
        assert_eq!(seen(&seen_answers), vec!["rejected: out of scope"]);

        // A handler that propagates it fails the run, and the run is not retried.
        let store = Arc::new(InMemoryStore::new());
        let (handler, _) = Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 2).await;
        let step = input_step(&store, run_id).await;
        reject(&store, run_id, step.id, "out of scope").await;

        let err = engine.resume_run(run_id).await.expect_err("rejected");
        match err {
            EngineError::HumanInputRejected {
                run_id: rejected_run,
                step_id,
                reason,
            } => {
                assert_eq!(rejected_run, run_id);
                assert_eq!(step_id, step.id);
                assert_eq!(reason, "out of scope");
            }
            other => panic!("expected HumanInputRejected, got {other:?}"),
        }

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

#[tokio::test]
async fn human_input_resumed_without_answer_suspends_again() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (handler, seen_answers) =
            Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let engine = engine_with(store.clone(), handler);

        let run_id = start(&engine, &store, 0).await;
        let before = input_step(&store, run_id).await;

        store
            .update_run_status(run_id, RunStatus::Running)
            .await
            .unwrap();
        engine
            .resume_run(run_id)
            .await
            .expect("resume suspends again");

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

        let after = input_step(&store, run_id).await;
        assert_eq!(after.id, before.id, "no new step is created");
        assert_eq!(after.status.state, StepStatus::AwaitingApproval);
        assert!(seen(&seen_answers).is_empty());
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_answer_is_carried_over_to_a_retry() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (mut handler, seen_answers) =
            Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        handler.fail_first_attempt = true;
        let engine = engine_with(store.clone(), handler);

        // Attempt 1 suspends, gets its answer, then fails past the input.
        let run_id = start(&engine, &store, 1).await;
        let step = input_step(&store, run_id).await;
        let value = json!({"answers": ["yes"]});
        answer(&store, run_id, step.id, value.clone()).await;
        assert!(engine.resume_run(run_id).await.is_err());

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

        // Attempt 2 runs to completion without asking again.
        store
            .update_run(
                run_id,
                RunUpdate {
                    scheduled_at: Some(Utc::now() - TimeDelta::seconds(1)),
                    ..RunUpdate::default()
                },
            )
            .await
            .unwrap();
        let picked = store.pick_next_pending(None).await.unwrap().unwrap();
        assert_eq!(picked.id, run_id);
        engine
            .execute_handler_run(run_id)
            .await
            .expect("retry runs past the input");

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

        let steps = input_steps(&store, run_id).await;
        let carried = steps
            .iter()
            .find(|s| s.attempt == 2)
            .expect("attempt 2 records the carried-over input");
        assert_eq!(carried.status.state, StepStatus::Completed);
        assert_eq!(carried.output, Some(value));
        assert_eq!(seen(&seen_answers), vec!["yes", "yes"]);
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_publishes_input_required_event() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (handler, _) = Clarify::new(
            HumanInputConfig::new("Answer the questions"),
            OnReject::Propagate,
        );
        let mut engine = engine_with(store.clone(), handler);
        let bus = WorkflowEventBus::new();
        engine.set_event_bus(bus.clone());

        let run = engine
            .enqueue_handler(WORKFLOW, TriggerKind::Manual, json!({}), 0)
            .await
            .unwrap();
        store
            .update_run_status(run.id, RunStatus::Running)
            .await
            .unwrap();
        let mut rx = bus.subscribe(run.id);
        engine.execute_handler_run(run.id).await.unwrap();

        let mut events = Vec::new();
        while let Ok(Ok(event)) = timeout(Duration::from_millis(100), rx.recv()).await {
            events.push(event);
        }

        let inputs: Vec<&WorkflowInputRequiredEvent> = events
            .iter()
            .filter_map(|e| match e {
                WorkflowEvent::InputRequired(payload) => Some(payload),
                _ => None,
            })
            .collect();
        assert_eq!(inputs.len(), 1, "expected one InputRequired event");

        let step = input_step(&store, run.id).await;
        let payload = inputs[0];
        assert_eq!(payload.run_id, run.id);
        assert_eq!(payload.step_id, step.id);
        assert_eq!(payload.step_name, "clarify");
        assert_eq!(payload.step_index, 0);
        assert_eq!(payload.message, "Answer the questions");
        assert!(payload.schema["properties"]["answers"].is_object());
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_plan_mode_records_the_step_without_suspending() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let (handler, _) = Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let mut engine = engine_with(store.clone(), handler);
        engine.register(ClarifyOptions).expect("register");

        // `Answers` cannot be built from `{}`: the plan stops at the input.
        let plan = engine
            .plan_handler(WORKFLOW, json!({}), PlanOptions::default())
            .await
            .expect("plan built");
        let names: Vec<&str> = plan.steps.iter().map(|s| s.name.as_str()).collect();
        assert_eq!(names, vec!["clarify"]);
        assert_eq!(plan.steps[0].kind, StepKind::HumanInput);
        assert!(plan.truncated);
        let reason = plan.incomplete_reason.expect("a reason");
        assert!(
            reason.contains("human input 'clarify' has no answer while planning"),
            "got {reason}"
        );

        // `Options` accepts `{}`: the plan continues past the input.
        let plan = engine
            .plan_handler("clarify-options", json!({}), PlanOptions::default())
            .await
            .expect("plan built");
        let names: Vec<&str> = plan.steps.iter().map(|s| s.name.as_str()).collect();
        assert_eq!(names, vec!["options", "after"]);
        assert_eq!(plan.steps[0].kind, StepKind::HumanInput);
        assert!(!plan.truncated);
        assert!(plan.incomplete_reason.is_none());

        // Planning never touches the store.
        let runs = store
            .list_runs(RunFilter::default(), 1, 10)
            .await
            .expect("list runs");
        assert!(runs.items.is_empty(), "planning must not create a run");
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_test_engine_mock_provides_the_answer() {
    timeout(TEST_TIMEOUT, async {
        let (handler, seen_answers) =
            Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let result = TestEngine::new()
            .with_handler(handler)
            .with_mock_shell(|_cfg| Ok(MockShellOutput::ok("after")))
            .with_mock_human_input(|name, cfg| {
                assert_eq!(name, "clarify");
                assert_eq!(cfg.message(), "Answer?");
                HumanInputOutcome::Provided(json!({"answers": ["mocked"]}))
            })
            .run(json!({}))
            .await
            .expect("run");

        assert_eq!(result.status(), RunStatus::Completed);
        assert_eq!(seen(&seen_answers), vec!["mocked"]);
        let step = result.step("clarify");
        assert_eq!(step.kind(), &StepKind::HumanInput);
        assert_eq!(step.status(), StepStatus::Completed);
        assert_eq!(step.output(), &json!({"answers": ["mocked"]}));
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_test_engine_mock_rejects() {
    timeout(TEST_TIMEOUT, async {
        let (handler, _) = Clarify::new(HumanInputConfig::new("Answer?"), OnReject::Propagate);
        let result = TestEngine::new()
            .with_handler(handler)
            .with_mock_shell(|_cfg| Ok(MockShellOutput::ok("after")))
            .with_mock_human_input(|_name, _cfg| HumanInputOutcome::reject("not now"))
            .run(json!({}))
            .await
            .expect("run");

        assert_eq!(result.status(), RunStatus::Failed);
        let step = result.step("clarify");
        assert_eq!(step.status(), StepStatus::Rejected);
        assert_eq!(step.error(), Some("not now"));
        assert!(result.try_step("after").is_none());
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_deadline_and_assignee_are_armed() {
    timeout(TEST_TIMEOUT, async {
        let store = Arc::new(InMemoryStore::new());
        let config = HumanInputConfig::new("Answer?")
            .with_deadline_secs(600)
            .assigned_to(Assignee::user("alice"))
            .requiring(Approvers::at_least(2).from_groups(["product"]));
        let (handler, _) = Clarify::new(config, OnReject::Propagate);
        let engine = engine_with(store.clone(), handler);

        let before = Utc::now();
        let run_id = start(&engine, &store, 0).await;
        let step = input_step(&store, run_id).await;

        let deadline = step.approval_deadline_at.expect("a deadline");
        assert!(
            deadline >= before + TimeDelta::seconds(599),
            "got {deadline}"
        );
        assert_eq!(step.approval_assignee, Some(Assignee::user("alice")));
        let requirement = step.approval_requirement.expect("a requirement");
        assert_eq!(requirement.required_approvers, 2);
        assert_eq!(requirement.approver_groups, vec!["product".to_string()]);
    })
    .await
    .expect("test timed out");
}

#[tokio::test]
async fn human_input_auto_approve_on_timeout_is_treated_as_a_rejection() {
    timeout(TEST_TIMEOUT, async {
        // `on_timeout` refuses AutoApprove; a stored config can still carry it.
        let config: HumanInputConfig = from_value(json!({
            "message": "Answer?",
            "deadline_secs": 1,
            "on_timeout": "auto_approve",
        }))
        .expect("deserialize");
        let store = Arc::new(InMemoryStore::new());
        let (handler, seen_answers) = Clarify::new(config, OnReject::Propagate);
        let engine = Arc::new(engine_with(store.clone(), handler));

        let run_id = start(&engine, &store, 0).await;
        let step = input_step(&store, run_id).await;
        store
            .update_step(
                step.id,
                StepUpdate {
                    approval_deadline_at: Some(Utc::now() - TimeDelta::seconds(1)),
                    ..StepUpdate::default()
                },
            )
            .await
            .expect("backdate the deadline");

        let records = ApprovalEscalator::new(engine.clone())
            .tick()
            .await
            .expect("tick");
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].action, EscalationAction::Rejected);

        let step = store.get_step(step.id).await.unwrap().unwrap();
        assert_eq!(step.status.state, StepStatus::Failed);
        let run = store.get_run(run_id).await.unwrap().unwrap();
        assert_eq!(run.status.state, RunStatus::Failed);
        assert!(seen(&seen_answers).is_empty());
    })
    .await
    .expect("test timed out");
}